Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add partial storage key support (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
kratico authored Oct 17, 2022
1 parent 8fe8176 commit 83df003
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion effect/std/readKeyPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function readKeyPage<
});
const $storageKey = a.$storageKey(deriveCodec, palletMetadata, entryMetadata);
const startKey = start ? a.storageKey($storageKey, start) : undefined;
const storageKey = a.storageKey($storageKey, []);
const storageKey = a.storageKey($storageKey);
const call = a.rpcCall(config, "state_getKeysPaged", [storageKey, count, startKey, blockHash]);
const $key = a.$key(deriveCodec, palletMetadata, entryMetadata);
const keysEncoded = a.select(call, "result");
Expand Down
23 changes: 23 additions & 0 deletions frame_metadata/Key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Deno.test("System Accounts Key", async () => {
pallet,
storageEntry,
});
const partialKey: unknown[] = [];
assertEquals(
U.hex.encode($key.encode(partialKey)),
"26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da9",
);
const key = [T.alice.publicKey];
const encoded = $key.encode(key);
assertEquals(
Expand Down Expand Up @@ -42,3 +47,21 @@ Deno.test("Auction Winning Key", async () => {
const decoded = $key.decode(encoded);
assertEquals(key, decoded);
});

Deno.test("Multisig Multisigs partial storage Key", async () => {
const [metadata, deriveCodec] = await setup("polkadot");
const [pallet, storageEntry] = U.throwIfError(
getPalletAndEntry(metadata, "Multisig", "Multisigs"),
);
const $key = $storageKey({
deriveCodec,
pallet,
storageEntry,
});
const key = [T.alice.publicKey];
const encoded = $key.encode(key);
assertEquals(
U.hex.encode(encoded),
"7474449cca95dc5d0c00e71735a6d17d3cd15a3fd6e04e47bee3922dbfa92c8d518366b5b1bc7c99d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",
);
});
21 changes: 14 additions & 7 deletions frame_metadata/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,30 @@ export function $storageKey(props: StorageKeyProps): $.Codec<unknown[]> {
}
const palletHash = H.Twox128.hash(new TextEncoder().encode(props.pallet.name));
const entryHash = H.Twox128.hash(new TextEncoder().encode(props.storageEntry.name));
const $keys = $.tuple(
...keyCodecs.map(($key, i) =>
H[(props.storageEntry as M.MapStorageEntryType).hashers[i]!].$hash($key)
),
const $keys = [...Array(keyCodecs.length + 1).keys()].reduce(
(keys, i) => {
keys[i] = $.tuple(
...keyCodecs.slice(0, i).map(($key, i) =>
H[(props.storageEntry as M.MapStorageEntryType).hashers[i]!].$hash($key)
),
);
return keys;
},
{} as Record<number, $.Codec<any[]>>,
);
return $.createCodec({
_metadata: [$storageKey, props],
_staticSize: $keys._staticSize,
_staticSize: $keys[Object.values($keys).length - 1]!._staticSize,
_encode(buffer, key) {
buffer.insertArray(palletHash);
buffer.insertArray(entryHash);
$keys._encode(buffer, key);
if (key.length === 0) return;
$keys[key.length]!._encode(buffer, key);
},
_decode(buffer) {
// Ignore initial hashes
buffer.index += 32;
return $keys._decode(buffer);
return $keys[Object.values($keys).length - 1]!._decode(buffer);
},
});
}
Expand Down
1 change: 1 addition & 0 deletions words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,4 @@ trufflesecurity
tailaiw
trufflehog
amannn
multisigs

0 comments on commit 83df003

Please sign in to comment.