@polkadot-api/merkleize-metadata 1.0.0
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/LICENSE +21 -0
- package/README.md +49 -0
- package/dist/index.d.mts +1206 -0
- package/dist/index.d.ts +1206 -0
- package/dist/index.js +629 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +649 -0
- package/dist/index.mjs.map +1 -0
- package/dist/min/index.d.ts +1206 -0
- package/dist/min/index.js +2 -0
- package/dist/min/index.js.map +1 -0
- package/package.json +47 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
extraInfo: () => extraInfo,
|
|
24
|
+
extrinsicMetadata: () => extrinsicMetadata,
|
|
25
|
+
hash: () => hash,
|
|
26
|
+
lookup: () => lookup,
|
|
27
|
+
lookupType: () => lookupType,
|
|
28
|
+
merkleizeMetadata: () => merkleizeMetadata,
|
|
29
|
+
proof: () => proof
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
32
|
+
|
|
33
|
+
// src/main.ts
|
|
34
|
+
var import_substrate_bindings4 = require("@polkadot-api/substrate-bindings");
|
|
35
|
+
|
|
36
|
+
// src/codecs.ts
|
|
37
|
+
var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
|
|
38
|
+
var extraInfoInner = {
|
|
39
|
+
specVersion: import_substrate_bindings.u32,
|
|
40
|
+
specName: import_substrate_bindings.str,
|
|
41
|
+
base58Prefix: import_substrate_bindings.u16,
|
|
42
|
+
decimals: import_substrate_bindings.u8,
|
|
43
|
+
tokenSymbol: import_substrate_bindings.str
|
|
44
|
+
};
|
|
45
|
+
var extraInfo = (0, import_substrate_bindings.Struct)(extraInfoInner);
|
|
46
|
+
var hash = (0, import_substrate_bindings.Bytes)(32);
|
|
47
|
+
var metadataDigest = (0, import_substrate_bindings.ScaleEnum)({
|
|
48
|
+
V0: import_substrate_bindings._void,
|
|
49
|
+
V1: (0, import_substrate_bindings.Struct)({
|
|
50
|
+
typeInformationTreeRoot: hash,
|
|
51
|
+
extrinsicMetadataHash: hash,
|
|
52
|
+
...extraInfoInner
|
|
53
|
+
})
|
|
54
|
+
});
|
|
55
|
+
var scalePrimitive = (0, import_substrate_bindings.ScaleEnum)({
|
|
56
|
+
bool: import_substrate_bindings._void,
|
|
57
|
+
char: import_substrate_bindings._void,
|
|
58
|
+
str: import_substrate_bindings._void,
|
|
59
|
+
u8: import_substrate_bindings._void,
|
|
60
|
+
u16: import_substrate_bindings._void,
|
|
61
|
+
u32: import_substrate_bindings._void,
|
|
62
|
+
u64: import_substrate_bindings._void,
|
|
63
|
+
u128: import_substrate_bindings._void,
|
|
64
|
+
u256: import_substrate_bindings._void,
|
|
65
|
+
i8: import_substrate_bindings._void,
|
|
66
|
+
i16: import_substrate_bindings._void,
|
|
67
|
+
i32: import_substrate_bindings._void,
|
|
68
|
+
i64: import_substrate_bindings._void,
|
|
69
|
+
i128: import_substrate_bindings._void,
|
|
70
|
+
i256: import_substrate_bindings._void
|
|
71
|
+
});
|
|
72
|
+
var typeRef = (0, import_substrate_bindings.ScaleEnum)({
|
|
73
|
+
bool: import_substrate_bindings._void,
|
|
74
|
+
char: import_substrate_bindings._void,
|
|
75
|
+
str: import_substrate_bindings._void,
|
|
76
|
+
u8: import_substrate_bindings._void,
|
|
77
|
+
u16: import_substrate_bindings._void,
|
|
78
|
+
u32: import_substrate_bindings._void,
|
|
79
|
+
u64: import_substrate_bindings._void,
|
|
80
|
+
u128: import_substrate_bindings._void,
|
|
81
|
+
u256: import_substrate_bindings._void,
|
|
82
|
+
i8: import_substrate_bindings._void,
|
|
83
|
+
i16: import_substrate_bindings._void,
|
|
84
|
+
i32: import_substrate_bindings._void,
|
|
85
|
+
i64: import_substrate_bindings._void,
|
|
86
|
+
i128: import_substrate_bindings._void,
|
|
87
|
+
i256: import_substrate_bindings._void,
|
|
88
|
+
compactU8: import_substrate_bindings._void,
|
|
89
|
+
compactU16: import_substrate_bindings._void,
|
|
90
|
+
compactU32: import_substrate_bindings._void,
|
|
91
|
+
compactU64: import_substrate_bindings._void,
|
|
92
|
+
compactU128: import_substrate_bindings._void,
|
|
93
|
+
compactU256: import_substrate_bindings._void,
|
|
94
|
+
void: import_substrate_bindings._void,
|
|
95
|
+
perId: import_substrate_bindings.compactNumber
|
|
96
|
+
});
|
|
97
|
+
var field = (0, import_substrate_bindings.Struct)({
|
|
98
|
+
name: (0, import_substrate_bindings.Option)(import_substrate_bindings.str),
|
|
99
|
+
ty: typeRef,
|
|
100
|
+
typeName: (0, import_substrate_bindings.Option)(import_substrate_bindings.str)
|
|
101
|
+
});
|
|
102
|
+
var typeDef = (0, import_substrate_bindings.ScaleEnum)({
|
|
103
|
+
composite: (0, import_substrate_bindings.Vector)(field),
|
|
104
|
+
enumeration: (0, import_substrate_bindings.Struct)({
|
|
105
|
+
name: import_substrate_bindings.str,
|
|
106
|
+
fields: (0, import_substrate_bindings.Vector)(field),
|
|
107
|
+
index: import_substrate_bindings.compactNumber
|
|
108
|
+
}),
|
|
109
|
+
sequence: typeRef,
|
|
110
|
+
array: (0, import_substrate_bindings.Struct)({
|
|
111
|
+
len: import_substrate_bindings.u32,
|
|
112
|
+
typeParam: typeRef
|
|
113
|
+
}),
|
|
114
|
+
tuple: (0, import_substrate_bindings.Vector)(typeRef),
|
|
115
|
+
bitSequence: (0, import_substrate_bindings.Struct)({
|
|
116
|
+
numBytes: import_substrate_bindings.u8,
|
|
117
|
+
leastSignificantBitFirst: import_substrate_bindings.bool
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
var lookupType = (0, import_substrate_bindings.Struct)({
|
|
121
|
+
path: (0, import_substrate_bindings.Vector)(import_substrate_bindings.str),
|
|
122
|
+
typeDef,
|
|
123
|
+
typeId: import_substrate_bindings.compactNumber
|
|
124
|
+
});
|
|
125
|
+
var lookup = (0, import_substrate_bindings.Vector)(lookupType);
|
|
126
|
+
var extrinsicMetadata = (0, import_substrate_bindings.Struct)({
|
|
127
|
+
version: import_substrate_bindings.u8,
|
|
128
|
+
addressTy: typeRef,
|
|
129
|
+
callTy: typeRef,
|
|
130
|
+
signatureTy: typeRef,
|
|
131
|
+
signedExtensions: (0, import_substrate_bindings.Vector)(
|
|
132
|
+
(0, import_substrate_bindings.Struct)({
|
|
133
|
+
identifier: import_substrate_bindings.str,
|
|
134
|
+
includedInExtrinsic: typeRef,
|
|
135
|
+
includedInSignedData: typeRef
|
|
136
|
+
})
|
|
137
|
+
)
|
|
138
|
+
});
|
|
139
|
+
var versionDecoder = (0, import_substrate_bindings.enhanceDecoder)(import_substrate_bindings.u8[1], (value) => ({
|
|
140
|
+
version: value & ~(1 << 7),
|
|
141
|
+
signed: !!(value & 1 << 7)
|
|
142
|
+
}));
|
|
143
|
+
var extrinsicDec = import_substrate_bindings.Tuple.dec(
|
|
144
|
+
import_substrate_bindings.compact[1],
|
|
145
|
+
versionDecoder,
|
|
146
|
+
(0, import_substrate_bindings.Bytes)(Infinity)[1]
|
|
147
|
+
);
|
|
148
|
+
var proof = (0, import_substrate_bindings.Struct)({
|
|
149
|
+
leaves: lookup,
|
|
150
|
+
leafIdxs: (0, import_substrate_bindings.Vector)(import_substrate_bindings.u32),
|
|
151
|
+
proofs: (0, import_substrate_bindings.Vector)(hash),
|
|
152
|
+
extrinsic: extrinsicMetadata,
|
|
153
|
+
info: extraInfo
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// src/get-accessible-types.ts
|
|
157
|
+
var getAccessibleTypes = (metadata2, definitions) => {
|
|
158
|
+
const types = /* @__PURE__ */ new Set();
|
|
159
|
+
const collectTypesFromId = (id) => {
|
|
160
|
+
if (types.has(id)) return;
|
|
161
|
+
const { tag, value } = definitions.get(id).def;
|
|
162
|
+
switch (tag) {
|
|
163
|
+
case "composite":
|
|
164
|
+
if (!value.length) break;
|
|
165
|
+
types.add(id);
|
|
166
|
+
value.forEach(({ type }) => {
|
|
167
|
+
collectTypesFromId(type);
|
|
168
|
+
});
|
|
169
|
+
break;
|
|
170
|
+
case "variant":
|
|
171
|
+
if (!value.length) break;
|
|
172
|
+
types.add(id);
|
|
173
|
+
value.forEach(({ fields }) => {
|
|
174
|
+
fields.forEach(({ type }) => {
|
|
175
|
+
collectTypesFromId(type);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
break;
|
|
179
|
+
case "tuple":
|
|
180
|
+
if (!value.length) break;
|
|
181
|
+
types.add(id);
|
|
182
|
+
value.forEach(collectTypesFromId);
|
|
183
|
+
break;
|
|
184
|
+
case "sequence":
|
|
185
|
+
types.add(id);
|
|
186
|
+
collectTypesFromId(value);
|
|
187
|
+
break;
|
|
188
|
+
case "array":
|
|
189
|
+
types.add(id);
|
|
190
|
+
collectTypesFromId(value.type);
|
|
191
|
+
break;
|
|
192
|
+
case "bitSequence":
|
|
193
|
+
types.add(id);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
collectTypesFromId(metadata2.extrinsic.call);
|
|
197
|
+
collectTypesFromId(metadata2.extrinsic.address);
|
|
198
|
+
collectTypesFromId(metadata2.extrinsic.signature);
|
|
199
|
+
metadata2.extrinsic.signedExtensions.forEach(({ type, additionalSigned }) => {
|
|
200
|
+
collectTypesFromId(type);
|
|
201
|
+
collectTypesFromId(additionalSigned);
|
|
202
|
+
});
|
|
203
|
+
const sortedTypes = [...types].sort((a, b) => a - b);
|
|
204
|
+
return new Map(sortedTypes.map((value, idx) => [value, idx]));
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// src/get-lookup.ts
|
|
208
|
+
var bitSequenceBytes = {
|
|
209
|
+
u8: 1,
|
|
210
|
+
u16: 2,
|
|
211
|
+
u32: 4,
|
|
212
|
+
u64: 8
|
|
213
|
+
};
|
|
214
|
+
var constructTypeDef = (definitions, getTypeRef, getPrimitive, frameId) => {
|
|
215
|
+
const {
|
|
216
|
+
def: { tag, value }
|
|
217
|
+
} = definitions.get(frameId);
|
|
218
|
+
switch (tag) {
|
|
219
|
+
case "composite":
|
|
220
|
+
return [
|
|
221
|
+
{
|
|
222
|
+
tag,
|
|
223
|
+
value: value.map((f) => ({
|
|
224
|
+
name: f.name,
|
|
225
|
+
typeName: f.typeName,
|
|
226
|
+
ty: getTypeRef(f.type)
|
|
227
|
+
}))
|
|
228
|
+
}
|
|
229
|
+
];
|
|
230
|
+
case "variant": {
|
|
231
|
+
return value.map((v) => ({
|
|
232
|
+
tag: "enumeration",
|
|
233
|
+
value: {
|
|
234
|
+
name: v.name,
|
|
235
|
+
index: v.index,
|
|
236
|
+
fields: v.fields.map((f) => ({
|
|
237
|
+
name: f.name,
|
|
238
|
+
typeName: f.typeName,
|
|
239
|
+
ty: getTypeRef(f.type)
|
|
240
|
+
}))
|
|
241
|
+
}
|
|
242
|
+
}));
|
|
243
|
+
}
|
|
244
|
+
case "sequence":
|
|
245
|
+
return [
|
|
246
|
+
{
|
|
247
|
+
tag,
|
|
248
|
+
value: getTypeRef(value)
|
|
249
|
+
}
|
|
250
|
+
];
|
|
251
|
+
case "array":
|
|
252
|
+
return [
|
|
253
|
+
{
|
|
254
|
+
tag,
|
|
255
|
+
value: {
|
|
256
|
+
len: value.len,
|
|
257
|
+
typeParam: getTypeRef(value.type)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
];
|
|
261
|
+
case "tuple":
|
|
262
|
+
return [
|
|
263
|
+
{
|
|
264
|
+
tag,
|
|
265
|
+
value: value.map(getTypeRef)
|
|
266
|
+
}
|
|
267
|
+
];
|
|
268
|
+
case "bitSequence": {
|
|
269
|
+
const primitive = getPrimitive(value.bitStoreType);
|
|
270
|
+
const numBytes = bitSequenceBytes[primitive];
|
|
271
|
+
if (!numBytes) throw new Error("Invalid primitive for BitSequence");
|
|
272
|
+
const storeOrderPath = definitions.get(value.bitOrderType).path;
|
|
273
|
+
const leastSignificantBitFirst = storeOrderPath.includes("Lsb0");
|
|
274
|
+
if (!leastSignificantBitFirst && !storeOrderPath.includes("Msb0"))
|
|
275
|
+
throw new Error("BitOrderType not recognized");
|
|
276
|
+
return [
|
|
277
|
+
{
|
|
278
|
+
tag: "bitSequence",
|
|
279
|
+
value: { numBytes, leastSignificantBitFirst }
|
|
280
|
+
}
|
|
281
|
+
];
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
throw new Error(`FrameId(${frameId}) should have been filtered out`);
|
|
285
|
+
};
|
|
286
|
+
var getLookup = (definitions, accessibleTypes, getTypeRef, getPrimitive) => {
|
|
287
|
+
const typeTree = [];
|
|
288
|
+
[...accessibleTypes.entries()].forEach(([frameId, typeId]) => {
|
|
289
|
+
const { path } = definitions.get(frameId);
|
|
290
|
+
constructTypeDef(definitions, getTypeRef, getPrimitive, frameId).forEach(
|
|
291
|
+
(typeDef2) => {
|
|
292
|
+
typeTree.push({
|
|
293
|
+
path,
|
|
294
|
+
typeId,
|
|
295
|
+
typeDef: typeDef2
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
});
|
|
300
|
+
typeTree.sort((a, b) => {
|
|
301
|
+
if (a.typeId !== b.typeId) return a.typeId - b.typeId;
|
|
302
|
+
if (a.typeDef.tag !== "enumeration" || b.typeDef.tag !== "enumeration")
|
|
303
|
+
throw new Error("Found two types with same id");
|
|
304
|
+
return a.typeDef.value.index - b.typeDef.value.index;
|
|
305
|
+
});
|
|
306
|
+
return typeTree;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
// src/get-metadata.ts
|
|
310
|
+
var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
|
|
311
|
+
var opaqueBytes = (0, import_substrate_bindings2.Bytes)();
|
|
312
|
+
var optionOpaque = (0, import_substrate_bindings2.Option)(opaqueBytes);
|
|
313
|
+
var opaqueOpaqueBytes = (0, import_substrate_bindings2.Tuple)(import_substrate_bindings2.compact, opaqueBytes);
|
|
314
|
+
var getAnyMetadata = (input) => {
|
|
315
|
+
try {
|
|
316
|
+
return import_substrate_bindings2.metadata.dec(input);
|
|
317
|
+
} catch (_) {
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
return import_substrate_bindings2.metadata.dec(optionOpaque.dec(input));
|
|
321
|
+
} catch (_) {
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
return import_substrate_bindings2.metadata.dec(opaqueBytes.dec(input));
|
|
325
|
+
} catch (_) {
|
|
326
|
+
}
|
|
327
|
+
try {
|
|
328
|
+
return import_substrate_bindings2.metadata.dec(opaqueOpaqueBytes.dec(input)[1]);
|
|
329
|
+
} catch (_) {
|
|
330
|
+
}
|
|
331
|
+
throw null;
|
|
332
|
+
};
|
|
333
|
+
var getMetadata = (input) => {
|
|
334
|
+
try {
|
|
335
|
+
const { metadata: metadata2 } = getAnyMetadata(input);
|
|
336
|
+
if (metadata2.tag !== "v15") throw new Error("Wrong metadata version");
|
|
337
|
+
return metadata2.value;
|
|
338
|
+
} catch (e) {
|
|
339
|
+
throw e || new Error("Unable to decode metadata");
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
// src/utils.ts
|
|
344
|
+
var import_utils = require("@polkadot-api/utils");
|
|
345
|
+
var mergeUint8 = (inputs) => {
|
|
346
|
+
const len = inputs.length;
|
|
347
|
+
let totalLen = 0;
|
|
348
|
+
for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength;
|
|
349
|
+
const result = new Uint8Array(totalLen);
|
|
350
|
+
for (let idx = 0, at = 0; idx < len; idx++) {
|
|
351
|
+
const current = inputs[idx];
|
|
352
|
+
result.set(current, at);
|
|
353
|
+
at += current.byteLength;
|
|
354
|
+
}
|
|
355
|
+
return result;
|
|
356
|
+
};
|
|
357
|
+
var toBytes = (input) => typeof input === "string" ? (0, import_utils.fromHex)(input) : input;
|
|
358
|
+
var compactTypeRefs = {
|
|
359
|
+
null: "void",
|
|
360
|
+
u8: "compactU8",
|
|
361
|
+
u16: "compactU16",
|
|
362
|
+
u32: "compactU32",
|
|
363
|
+
u64: "compactU64",
|
|
364
|
+
u128: "compactU128",
|
|
365
|
+
u256: "compactU256"
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/decode-and-collect.ts
|
|
369
|
+
var import_substrate_bindings3 = require("@polkadot-api/substrate-bindings");
|
|
370
|
+
var typeRefDecoders = {
|
|
371
|
+
bool: import_substrate_bindings3.u8,
|
|
372
|
+
char: import_substrate_bindings3.u8,
|
|
373
|
+
str: import_substrate_bindings3.str,
|
|
374
|
+
u8: import_substrate_bindings3.u8,
|
|
375
|
+
u16: import_substrate_bindings3.u16,
|
|
376
|
+
u32: import_substrate_bindings3.u32,
|
|
377
|
+
u64: import_substrate_bindings3.u64,
|
|
378
|
+
u128: import_substrate_bindings3.u128,
|
|
379
|
+
u256: import_substrate_bindings3.u256,
|
|
380
|
+
i8: import_substrate_bindings3.i8,
|
|
381
|
+
i16: import_substrate_bindings3.i16,
|
|
382
|
+
i32: import_substrate_bindings3.i32,
|
|
383
|
+
i64: import_substrate_bindings3.i64,
|
|
384
|
+
i128: import_substrate_bindings3.i128,
|
|
385
|
+
i256: import_substrate_bindings3.i256,
|
|
386
|
+
void: import_substrate_bindings3._void,
|
|
387
|
+
compactU8: import_substrate_bindings3.compact,
|
|
388
|
+
compactU16: import_substrate_bindings3.compact,
|
|
389
|
+
compactU32: import_substrate_bindings3.compact,
|
|
390
|
+
compactU64: import_substrate_bindings3.compact,
|
|
391
|
+
compactU128: import_substrate_bindings3.compact,
|
|
392
|
+
compactU256: import_substrate_bindings3.compact
|
|
393
|
+
};
|
|
394
|
+
var innerDecodeAndCollect = (input, typeRef2, idToLookups, lookup2, collected) => {
|
|
395
|
+
if (typeRef2.tag !== "perId") {
|
|
396
|
+
typeRefDecoders[typeRef2.tag][1](input);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const handleTypeRef = (typeRef3) => {
|
|
400
|
+
innerDecodeAndCollect(input, typeRef3, idToLookups, lookup2, collected);
|
|
401
|
+
};
|
|
402
|
+
const lookupIdxs = idToLookups.get(typeRef2.value);
|
|
403
|
+
const [currentIdx] = lookupIdxs;
|
|
404
|
+
const current = lookup2[currentIdx];
|
|
405
|
+
if (lookupIdxs.length === 1) collected.add(currentIdx);
|
|
406
|
+
switch (current.typeDef.tag) {
|
|
407
|
+
case "enumeration": {
|
|
408
|
+
const selectedIdx = import_substrate_bindings3.u8.dec(input);
|
|
409
|
+
const [selected, collectedIdx] = lookupIdxs.map(
|
|
410
|
+
(lookupIdx) => [lookup2[lookupIdx].typeDef, lookupIdx]
|
|
411
|
+
).find(([x]) => x.value.index === selectedIdx);
|
|
412
|
+
collected.add(collectedIdx);
|
|
413
|
+
selected.value.fields.forEach(({ ty }) => {
|
|
414
|
+
handleTypeRef(ty);
|
|
415
|
+
});
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
case "sequence": {
|
|
419
|
+
const len = import_substrate_bindings3.compact.dec(input);
|
|
420
|
+
for (let i = 0; i < len; i++) handleTypeRef(current.typeDef.value);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case "array": {
|
|
424
|
+
for (let i = 0; i < current.typeDef.value.len; i++)
|
|
425
|
+
handleTypeRef(current.typeDef.value.typeParam);
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case "composite": {
|
|
429
|
+
current.typeDef.value.forEach((x) => {
|
|
430
|
+
handleTypeRef(x.ty);
|
|
431
|
+
});
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case "tuple": {
|
|
435
|
+
current.typeDef.value.forEach(handleTypeRef);
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
case "bitSequence":
|
|
439
|
+
throw new Error("bitSequence is not supported");
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
var decodeAndCollectKnownLeafs = (data, typeRefs, lookup2) => {
|
|
443
|
+
let input = new Uint8Array();
|
|
444
|
+
(0, import_substrate_bindings3.createDecoder)((_input) => {
|
|
445
|
+
input = _input;
|
|
446
|
+
})(data);
|
|
447
|
+
const idToLookups = /* @__PURE__ */ new Map();
|
|
448
|
+
lookup2.forEach((lookup3, idx) => {
|
|
449
|
+
const arr = idToLookups.get(lookup3.typeId);
|
|
450
|
+
if (arr) arr.push(idx);
|
|
451
|
+
else idToLookups.set(lookup3.typeId, [idx]);
|
|
452
|
+
});
|
|
453
|
+
const result = /* @__PURE__ */ new Set();
|
|
454
|
+
typeRefs.forEach((typeRef2) => {
|
|
455
|
+
innerDecodeAndCollect(input, typeRef2, idToLookups, lookup2, result);
|
|
456
|
+
});
|
|
457
|
+
return [...result].sort((a, b) => a - b);
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// src/proof.ts
|
|
461
|
+
var getLevelFromIdx = (idx) => Math.log2(idx + 1) | 0;
|
|
462
|
+
var getAncestorIdx = (from, nLevels) => (from + 1 >> nLevels) - 1;
|
|
463
|
+
function getProofData(leaves, knownLeavesIdxs) {
|
|
464
|
+
const knownLeaves = knownLeavesIdxs.map((idx) => leaves[idx]);
|
|
465
|
+
const startingIdx = leaves.length - 1;
|
|
466
|
+
const leafIdxs = knownLeavesIdxs.map((idx) => startingIdx + idx);
|
|
467
|
+
const proofIdxs = [];
|
|
468
|
+
if (leafIdxs.length) {
|
|
469
|
+
const nLevels = getLevelFromIdx(leafIdxs.at(-1));
|
|
470
|
+
const splitPosition = Math.pow(2, nLevels) - 1;
|
|
471
|
+
const splitIdx = leafIdxs.findIndex((x) => x >= splitPosition);
|
|
472
|
+
if (splitIdx > 0) {
|
|
473
|
+
leafIdxs.unshift(...leafIdxs.splice(splitIdx));
|
|
474
|
+
knownLeaves.unshift(...knownLeaves.splice(splitIdx));
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
let targetIdx = 0;
|
|
478
|
+
const traverse = (nodeIdx) => {
|
|
479
|
+
if (targetIdx === leafIdxs.length) {
|
|
480
|
+
proofIdxs.push(nodeIdx);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const target = leafIdxs[targetIdx];
|
|
484
|
+
if (target === nodeIdx) {
|
|
485
|
+
++targetIdx;
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const currentLevel = getLevelFromIdx(nodeIdx);
|
|
489
|
+
const targetLevel = getLevelFromIdx(target);
|
|
490
|
+
if (nodeIdx !== getAncestorIdx(target, targetLevel - currentLevel)) {
|
|
491
|
+
proofIdxs.push(nodeIdx);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const leftSon = 2 * nodeIdx + 1;
|
|
495
|
+
traverse(leftSon);
|
|
496
|
+
traverse(leftSon + 1);
|
|
497
|
+
};
|
|
498
|
+
traverse(0);
|
|
499
|
+
return {
|
|
500
|
+
leaves: knownLeaves,
|
|
501
|
+
leafIdxs,
|
|
502
|
+
proofIdxs
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// src/main.ts
|
|
507
|
+
var merkleizeMetadata = (metadataBytes, info) => {
|
|
508
|
+
const metadata2 = getMetadata(metadataBytes);
|
|
509
|
+
const definitions = new Map(
|
|
510
|
+
metadata2.lookup.map((value) => [value.id, value])
|
|
511
|
+
);
|
|
512
|
+
const accessibleTypes = getAccessibleTypes(metadata2, definitions);
|
|
513
|
+
const getPrimitive = (frameId) => {
|
|
514
|
+
const {
|
|
515
|
+
def: { tag, value }
|
|
516
|
+
} = definitions.get(frameId);
|
|
517
|
+
if (tag === "primitive") return value.tag;
|
|
518
|
+
if (tag !== "composite" && tag !== "tuple" || value.length > 1)
|
|
519
|
+
throw new Error("The provided definition doesn't map to a primitive");
|
|
520
|
+
return value.length === 0 ? null : getPrimitive(tag === "tuple" ? value[0] : value[0].type);
|
|
521
|
+
};
|
|
522
|
+
const getTypeRef = (frameId) => {
|
|
523
|
+
const { def } = definitions.get(frameId);
|
|
524
|
+
if (def.tag === "primitive") return { tag: def.value.tag, value: void 0 };
|
|
525
|
+
if (def.tag === "compact") {
|
|
526
|
+
const primitive = getPrimitive(def.value);
|
|
527
|
+
const tag = compactTypeRefs[primitive];
|
|
528
|
+
if (!tag) throw new Error("Invalid primitive for Compact");
|
|
529
|
+
return { tag, value: void 0 };
|
|
530
|
+
}
|
|
531
|
+
return accessibleTypes.has(frameId) ? { tag: "perId", value: accessibleTypes.get(frameId) } : { tag: "void", value: void 0 };
|
|
532
|
+
};
|
|
533
|
+
const extrinsic = {
|
|
534
|
+
version: metadata2.extrinsic.version,
|
|
535
|
+
addressTy: getTypeRef(metadata2.extrinsic.address),
|
|
536
|
+
callTy: getTypeRef(metadata2.extrinsic.call),
|
|
537
|
+
signatureTy: getTypeRef(metadata2.extrinsic.signature),
|
|
538
|
+
signedExtensions: metadata2.extrinsic.signedExtensions.map((se) => ({
|
|
539
|
+
identifier: se.identifier,
|
|
540
|
+
includedInExtrinsic: getTypeRef(se.type),
|
|
541
|
+
includedInSignedData: getTypeRef(se.additionalSigned)
|
|
542
|
+
}))
|
|
543
|
+
};
|
|
544
|
+
const lookup2 = getLookup(
|
|
545
|
+
definitions,
|
|
546
|
+
accessibleTypes,
|
|
547
|
+
getTypeRef,
|
|
548
|
+
getPrimitive
|
|
549
|
+
);
|
|
550
|
+
const lookupEncoded = lookup2.map(lookupType.enc);
|
|
551
|
+
let hashTree;
|
|
552
|
+
const getHashTree = () => {
|
|
553
|
+
if (hashTree) return hashTree;
|
|
554
|
+
if (!lookupEncoded.length) return hashTree = [new Uint8Array(32).fill(0)];
|
|
555
|
+
hashTree = new Array(lookupEncoded.length * 2 - 1);
|
|
556
|
+
let leavesStartIdx = lookupEncoded.length - 1;
|
|
557
|
+
for (let i = 0; i < lookupEncoded.length; i++)
|
|
558
|
+
hashTree[leavesStartIdx + i] = (0, import_substrate_bindings4.Blake3256)(lookupEncoded[i]);
|
|
559
|
+
for (let i = hashTree.length - 2; i > 0; i -= 2)
|
|
560
|
+
hashTree[(i - 1) / 2] = (0, import_substrate_bindings4.Blake3256)(
|
|
561
|
+
mergeUint8([hashTree[i], hashTree[i + 1]])
|
|
562
|
+
);
|
|
563
|
+
return hashTree;
|
|
564
|
+
};
|
|
565
|
+
let digested;
|
|
566
|
+
const digest = () => {
|
|
567
|
+
if (digested) return digested;
|
|
568
|
+
const rootLookupHash = getHashTree()[0];
|
|
569
|
+
const digest2 = {
|
|
570
|
+
tag: "V1",
|
|
571
|
+
value: {
|
|
572
|
+
typeInformationTreeRoot: rootLookupHash,
|
|
573
|
+
extrinsicMetadataHash: (0, import_substrate_bindings4.Blake3256)(extrinsicMetadata.enc(extrinsic)),
|
|
574
|
+
...info
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
return digested = (0, import_substrate_bindings4.Blake3256)(metadataDigest.enc(digest2));
|
|
578
|
+
};
|
|
579
|
+
const generateProof = (knownIndexes) => {
|
|
580
|
+
const proofData = getProofData(lookupEncoded, knownIndexes);
|
|
581
|
+
const hashTree2 = getHashTree();
|
|
582
|
+
const proofs = proofData.proofIdxs.map((idx) => hashTree2[idx]);
|
|
583
|
+
return mergeUint8([
|
|
584
|
+
import_substrate_bindings4.compact.enc(proofData.leaves.length),
|
|
585
|
+
...proofData.leaves,
|
|
586
|
+
import_substrate_bindings4.compact.enc(proofData.leafIdxs.length),
|
|
587
|
+
...proofData.leafIdxs.map((x) => import_substrate_bindings4.u32.enc(x)),
|
|
588
|
+
import_substrate_bindings4.compact.enc(proofs.length),
|
|
589
|
+
...proofs,
|
|
590
|
+
extrinsicMetadata.enc(extrinsic),
|
|
591
|
+
extraInfo.enc(info)
|
|
592
|
+
]);
|
|
593
|
+
};
|
|
594
|
+
const getProofForExtrinsicParts = (callData, includedInExtrinsic, includedInSignedData) => {
|
|
595
|
+
const bytes = mergeUint8(
|
|
596
|
+
[callData, includedInExtrinsic, includedInSignedData].map(toBytes)
|
|
597
|
+
);
|
|
598
|
+
const typeRefs = [
|
|
599
|
+
extrinsic.callTy,
|
|
600
|
+
...extrinsic.signedExtensions.map((x) => x.includedInExtrinsic),
|
|
601
|
+
...extrinsic.signedExtensions.map((x) => x.includedInSignedData)
|
|
602
|
+
];
|
|
603
|
+
return generateProof(decodeAndCollectKnownLeafs(bytes, typeRefs, lookup2));
|
|
604
|
+
};
|
|
605
|
+
const getProofForExtrinsic = (transaction, txAdditionalSigned) => {
|
|
606
|
+
let [, { version, signed }, bytes] = extrinsicDec(transaction);
|
|
607
|
+
if (version !== extrinsic.version)
|
|
608
|
+
throw new Error("Incorrect extrinsic version");
|
|
609
|
+
const typeRefs = signed ? [
|
|
610
|
+
extrinsic.addressTy,
|
|
611
|
+
extrinsic.signatureTy,
|
|
612
|
+
...extrinsic.signedExtensions.map((x) => x.includedInExtrinsic),
|
|
613
|
+
extrinsic.callTy
|
|
614
|
+
] : [extrinsic.callTy];
|
|
615
|
+
if (txAdditionalSigned) {
|
|
616
|
+
bytes = mergeUint8([bytes, toBytes(txAdditionalSigned)]);
|
|
617
|
+
typeRefs.push(
|
|
618
|
+
...extrinsic.signedExtensions.map((x) => x.includedInSignedData)
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
return generateProof(decodeAndCollectKnownLeafs(bytes, typeRefs, lookup2));
|
|
622
|
+
};
|
|
623
|
+
return {
|
|
624
|
+
digest,
|
|
625
|
+
getProofForExtrinsic,
|
|
626
|
+
getProofForExtrinsicParts
|
|
627
|
+
};
|
|
628
|
+
};
|
|
629
|
+
//# sourceMappingURL=index.js.map
|