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 and harrysolovay committed Oct 18, 2022
1 parent 7ccd7fb commit 25b2dc7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
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",
);
});
18 changes: 12 additions & 6 deletions frame_metadata/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ 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);
Expand All @@ -46,7 +52,7 @@ export function $storageKey(props: StorageKeyProps): $.Codec<unknown[]> {
_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 25b2dc7

Please sign in to comment.