@polkadot-api/merkleize-metadata 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/codecs.mjs +122 -0
- package/dist/esm/codecs.mjs.map +1 -0
- package/dist/esm/decode-and-collect.mjs +94 -0
- package/dist/esm/decode-and-collect.mjs.map +1 -0
- package/dist/esm/get-accessible-types.mjs +52 -0
- package/dist/esm/get-accessible-types.mjs.map +1 -0
- package/dist/esm/get-lookup.mjs +103 -0
- package/dist/esm/get-lookup.mjs.map +1 -0
- package/dist/esm/get-metadata.mjs +36 -0
- package/dist/esm/get-metadata.mjs.map +1 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/main.mjs +140 -0
- package/dist/esm/main.mjs.map +1 -0
- package/dist/esm/proof.mjs +47 -0
- package/dist/esm/proof.mjs.map +1 -0
- package/dist/esm/utils.mjs +27 -0
- package/dist/esm/utils.mjs.map +1 -0
- package/dist/index.js +179 -213
- package/dist/index.js.map +1 -1
- package/package.json +11 -10
- package/dist/index.d.mts +0 -1239
- package/dist/index.mjs +0 -655
- package/dist/index.mjs.map +0 -1
- package/dist/min/index.d.ts +0 -1239
- package/dist/min/index.js +0 -2
- package/dist/min/index.js.map +0 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const getLevelFromIdx = (idx) => Math.log2(idx + 1) | 0;
|
|
2
|
+
const getAncestorIdx = (from, nLevels) => (from + 1 >> nLevels) - 1;
|
|
3
|
+
function getProofData(leaves, knownLeavesIdxs) {
|
|
4
|
+
const knownLeaves = knownLeavesIdxs.map((idx) => leaves[idx]);
|
|
5
|
+
const startingIdx = leaves.length - 1;
|
|
6
|
+
const leafIdxs = knownLeavesIdxs.map((idx) => startingIdx + idx);
|
|
7
|
+
const proofIdxs = [];
|
|
8
|
+
if (leafIdxs.length) {
|
|
9
|
+
const nLevels = getLevelFromIdx(leafIdxs.at(-1));
|
|
10
|
+
const splitPosition = Math.pow(2, nLevels) - 1;
|
|
11
|
+
const splitIdx = leafIdxs.findIndex((x) => x >= splitPosition);
|
|
12
|
+
if (splitIdx > 0) {
|
|
13
|
+
leafIdxs.unshift(...leafIdxs.splice(splitIdx));
|
|
14
|
+
knownLeaves.unshift(...knownLeaves.splice(splitIdx));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let targetIdx = 0;
|
|
18
|
+
const traverse = (nodeIdx) => {
|
|
19
|
+
if (targetIdx === leafIdxs.length) {
|
|
20
|
+
proofIdxs.push(nodeIdx);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const target = leafIdxs[targetIdx];
|
|
24
|
+
if (target === nodeIdx) {
|
|
25
|
+
++targetIdx;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const currentLevel = getLevelFromIdx(nodeIdx);
|
|
29
|
+
const targetLevel = getLevelFromIdx(target);
|
|
30
|
+
if (nodeIdx !== getAncestorIdx(target, targetLevel - currentLevel)) {
|
|
31
|
+
proofIdxs.push(nodeIdx);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const leftSon = 2 * nodeIdx + 1;
|
|
35
|
+
traverse(leftSon);
|
|
36
|
+
traverse(leftSon + 1);
|
|
37
|
+
};
|
|
38
|
+
traverse(0);
|
|
39
|
+
return {
|
|
40
|
+
leaves: knownLeaves,
|
|
41
|
+
leafIdxs,
|
|
42
|
+
proofIdxs
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { getProofData };
|
|
47
|
+
//# sourceMappingURL=proof.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof.mjs","sources":["../../src/proof.ts"],"sourcesContent":["const getLevelFromIdx = (idx: number) => Math.log2(idx + 1) | 0\nconst getAncestorIdx = (from: number, nLevels: number) =>\n ((from + 1) >> nLevels) - 1\n\nexport function getProofData(\n leaves: Array<Uint8Array>,\n knownLeavesIdxs: Array<number>,\n) {\n const knownLeaves = knownLeavesIdxs.map((idx) => leaves[idx])\n\n const startingIdx = leaves.length - 1\n const leafIdxs = knownLeavesIdxs.map((idx) => startingIdx + idx)\n\n const proofIdxs: Array<number> = []\n if (leafIdxs.length) {\n const nLevels = getLevelFromIdx(leafIdxs.at(-1)!)\n const splitPosition = Math.pow(2, nLevels) - 1\n const splitIdx = leafIdxs.findIndex((x) => x >= splitPosition)\n if (splitIdx > 0) {\n leafIdxs.unshift(...leafIdxs.splice(splitIdx))\n knownLeaves.unshift(...knownLeaves.splice(splitIdx))\n }\n }\n\n let targetIdx = 0\n const traverse = (nodeIdx: number): void => {\n if (targetIdx === leafIdxs.length) {\n proofIdxs.push(nodeIdx)\n return\n }\n\n const target = leafIdxs[targetIdx]\n if (target === nodeIdx) {\n ++targetIdx\n return\n }\n\n const currentLevel = getLevelFromIdx(nodeIdx)\n const targetLevel = getLevelFromIdx(target)\n\n if (nodeIdx !== getAncestorIdx(target, targetLevel - currentLevel)) {\n proofIdxs.push(nodeIdx)\n return\n }\n\n const leftSon = 2 * nodeIdx + 1\n traverse(leftSon)\n traverse(leftSon + 1)\n }\n traverse(0)\n\n return {\n leaves: knownLeaves,\n leafIdxs,\n proofIdxs,\n }\n}\n"],"names":[],"mappings":"AAAA,MAAM,kBAAkB,CAAC,GAAA,KAAgB,KAAK,IAAK,CAAA,GAAA,GAAM,CAAC,CAAI,GAAA,CAAA,CAAA;AAC9D,MAAM,iBAAiB,CAAC,IAAA,EAAc,OAClC,KAAA,CAAA,IAAA,GAAO,KAAM,OAAW,IAAA,CAAA,CAAA;AAEZ,SAAA,YAAA,CACd,QACA,eACA,EAAA;AACA,EAAA,MAAM,cAAc,eAAgB,CAAA,GAAA,CAAI,CAAC,GAAQ,KAAA,MAAA,CAAO,GAAG,CAAC,CAAA,CAAA;AAE5D,EAAM,MAAA,WAAA,GAAc,OAAO,MAAS,GAAA,CAAA,CAAA;AACpC,EAAA,MAAM,WAAW,eA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AAA;AAClE,MAAA,SAAA,CAAU,KAAK,OAAO,CAAA,CAAA;AACtB,MAAA,OAAA;AAAA,KACF;AAEA,IAAM,MAAA,OAAA,GAAU,IAAI,OAAU,GAAA,CAAA,CAAA;AAC9B,IAAA,QAAA,CAAS,OAAO,CAAA,CAAA;AAChB,IAAA,QAAA,CAAS,UAAU,CAAC,CAAA,CAAA;AAAA,GACtB,CAAA;AACA,EAAA,QAAA,CAAS,CAAC,CAAA,CAAA;AAEV,EAAO,OAAA;AAAA,IACL,MAAQ,EAAA,WAAA;AAAA,IACR,QAAA;AAAA,IACA,SAAA;AAAA,GACF,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { fromHex } from '@polkadot-api/utils';
|
|
2
|
+
|
|
3
|
+
const mergeUint8 = (inputs) => {
|
|
4
|
+
const len = inputs.length;
|
|
5
|
+
let totalLen = 0;
|
|
6
|
+
for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength;
|
|
7
|
+
const result = new Uint8Array(totalLen);
|
|
8
|
+
for (let idx = 0, at = 0; idx < len; idx++) {
|
|
9
|
+
const current = inputs[idx];
|
|
10
|
+
result.set(current, at);
|
|
11
|
+
at += current.byteLength;
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
};
|
|
15
|
+
const toBytes = (input) => typeof input === "string" ? fromHex(input) : input;
|
|
16
|
+
const compactTypeRefs = {
|
|
17
|
+
null: "void",
|
|
18
|
+
u8: "compactU8",
|
|
19
|
+
u16: "compactU16",
|
|
20
|
+
u32: "compactU32",
|
|
21
|
+
u64: "compactU64",
|
|
22
|
+
u128: "compactU128",
|
|
23
|
+
u256: "compactU256"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { compactTypeRefs, mergeUint8, toBytes };
|
|
27
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sources":["../../src/utils.ts"],"sourcesContent":["import { type HexString } from \"@polkadot-api/substrate-bindings\"\nimport { fromHex } from \"@polkadot-api/utils\"\n\nexport const mergeUint8 = (inputs: Array<Uint8Array>): Uint8Array => {\n const len = inputs.length\n let totalLen = 0\n for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength\n const result = new Uint8Array(totalLen)\n\n for (let idx = 0, at = 0; idx < len; idx++) {\n const current = inputs[idx]\n result.set(current, at)\n at += current.byteLength\n }\n\n return result\n}\n\nexport const toBytes = (input: Uint8Array | HexString) =>\n typeof input === \"string\" ? fromHex(input) : input\n\nexport const compactTypeRefs = {\n null: \"void\" as const,\n u8: \"compactU8\" as const,\n u16: \"compactU16\" as const,\n u32: \"compactU32\" as const,\n u64: \"compactU64\" as const,\n u128: \"compactU128\" as const,\n u256: \"compactU256\" as const,\n}\n"],"names":[],"mappings":";;AAGa,MAAA,UAAA,GAAa,CAAC,MAA0C,KAAA;AACnE,EAAA,MAAM,MAAM,MAAO,CAAA,MAAA,CAAA;AACnB,EAAA,IAAI,QAAW,GAAA,CAAA,CAAA;AACf,EAAS,KAAA,IAAA,CAAA,GAAI,GAAG,CAAI,GAAA,GAAA,EAAK,KAAiB,QAAA,IAAA,MAAA,CAAO,CAAC,CAAE,CAAA,UAAA,CAAA;AACpD,EAAM,MAAA,MAAA,GAAS,IAAI,UAAA,CAAW,QAAQ,CAAA,CAAA;AAEtC,EAAA,KAAA,IAAS,MAAM,CAAG,EAAA,EAAA,GAAK,CAAG,EAAA,GAAA,GAAM,KAAK,GAAO,EAAA,EAAA;AAC1C,IAAM,MAAA,OAAA,GAAU,OAAO,GAAG,CAAA,CAAA;AAC1B,IAAO,MAAA,CAAA,GAAA,CAAI,SAAS,EAAE,CAAA,CAAA;AACtB,IAAA,EAAA,IAAM,OAAQ,CAAA,UAAA,CAAA;AAAA,GAChB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT,EAAA;AAEa,MAAA,OAAA,GAAU,CAAC,KACtB,KAAA,OAAO,UAAU,QAAW,GAAA,OAAA,CAAQ,KAAK,CAAI,GAAA,MAAA;AAExC,MAAM,eAAkB,GAAA;AAAA,EAC7B,IAAM,EAAA,MAAA;AAAA,EACN,EAAI,EAAA,WAAA;AAAA,EACJ,GAAK,EAAA,YAAA;AAAA,EACL,GAAK,EAAA,YAAA;AAAA,EACL,GAAK,EAAA,YAAA;AAAA,EACL,IAAM,EAAA,aAAA;AAAA,EACN,IAAM,EAAA,aAAA;AACR;;;;"}
|