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

feat: add partial storage key support #297

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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