Skip to content

Commit

Permalink
use client decode function
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Sep 16, 2024
1 parent 4b29d39 commit 8ccc4eb
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions clients/tfchain-client-go/kvstore.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package substrate

import (
"bytes"
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"

"github.com/pkg/errors"
Expand Down Expand Up @@ -117,12 +115,14 @@ func (s *Substrate) KVStoreList(identity Identity) (map[string]string, error) {
pairs := make(map[string]string)
for _, q := range query {
for _, c := range q.Changes {
key, err := decodeSecondKey(c.StorageKey, identity)
var key, val []byte

key, err = decodeSecondKey(c.StorageKey, identity)
if err != nil {
return nil, err
}

val, err := decodeVecU8Val(c.StorageData)
err = Decode(c.StorageData, &val)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode value %v", string(c.StorageData))
}
Expand All @@ -135,7 +135,7 @@ func (s *Substrate) KVStoreList(identity Identity) (map[string]string, error) {
}

// decodeSecondKey extracts and decodes the second key(Vec<u8>) from the storage key.
func decodeSecondKey(storageKey types.StorageKey, identity Identity) ([]byte, error) {
func decodeSecondKey(storageKey types.StorageKey, identity Identity) (key []byte, err error) {
// remove 16 bytes(32 in hex) pallet and map prefixes.
// pallet prefix (8 bytes): twox64(pallet_name)
// map prefix (8bytes) twox64(map_name)
Expand All @@ -152,13 +152,6 @@ func decodeSecondKey(storageKey types.StorageKey, identity Identity) ([]byte, er
return nil, errors.New(fmt.Sprintf("failed to decode second key, storage key len should not be less than %d bytes", offset))
}

return decodeVecU8Val(storageKey[offset:])
}

// decodeVecU8Val decodes a value of type (Vec<u8>) from SCALE-encoded bytes.
func decodeVecU8Val(encodedData []byte) (data []byte, err error) {
decoder := scale.NewDecoder(bytes.NewReader(encodedData))
err = decoder.Decode(&data)

return
err = Decode(storageKey[offset:], &key)
return key, err
}

0 comments on commit 8ccc4eb

Please sign in to comment.