-
Notifications
You must be signed in to change notification settings - Fork 6
/
decodeU8a.ts
103 lines (85 loc) · 3.44 KB
/
decodeU8a.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { Codec, CodecClass, Registry } from '../types/index.ts';
import { u8aToHex } from 'https://deno.land/x/polkadot@0.2.40/util/mod.ts';
/** @internal */
function formatFailure (registry: Registry, fn: 'decodeU8a' | 'decodeU8aStruct' | 'decodeU8aVec', _result: unknown[], { message }: Error, u8a: Uint8Array, i: number, count: number, Type: CodecClass, key?: string): string {
let type = '';
try {
type = `: ${new Type(registry).toRawType()}`;
} catch {
// ignore
}
// This is extra debugging info (we most-probably want this in in some way, shape or form,
// but at this point not quite sure how to include and format it (it can be quite massive)
// console.error(JSON.stringify(result, null, 2));
return `${fn}: failed at ${u8aToHex(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i + 1}/${count})${type}:: ${message}`;
}
/**
* @internal
*
* Given an u8a, and an array of Type constructors, decode the u8a against the
* types, and return an array of decoded values.
*
* @param u8a - The u8a to decode.
* @param result - The result array (will be returned with values pushed)
* @param types - The array of CodecClass to decode the U8a against.
*/
export function decodeU8a <T extends Codec = Codec> (registry: Registry, result: T[], u8a: Uint8Array, [Types, keys]: [CodecClass<T>[], string[]]): [T[], number] {
const count = result.length;
let offset = 0;
let i = 0;
try {
while (i < count) {
const value = new Types[i](registry, u8a.subarray(offset));
offset += value.initialU8aLength || value.encodedLength;
result[i] = value;
i++;
}
} catch (error) {
throw new Error(formatFailure(registry, 'decodeU8a', result, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
}
return [result, offset];
}
/**
* @internal
*
* Split from decodeU8a since this is specialized to zip returns ... while we duplicate, this
* is all on the hot-path, so it is not great, however there is (some) method behind the madness
*/
export function decodeU8aStruct (registry: Registry, result: [string, Codec][], u8a: Uint8Array, [Types, keys]: [CodecClass[], string[]]): [[string, Codec][], number] {
const count = result.length;
let offset = 0;
let i = 0;
try {
while (i < count) {
const value = new Types[i](registry, u8a.subarray(offset));
offset += value.initialU8aLength || value.encodedLength;
result[i] = [keys[i], value];
i++;
}
} catch (error) {
throw new Error(formatFailure(registry, 'decodeU8aStruct', result, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
}
return [result, offset];
}
/**
* @internal
*
* Split from decodeU8a since this is specialized to 1 instance ... while we duplicate, this
* is all on the hot-path, so it is not great, however there is (some) method behind the madness
*/
export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, result: unknown[], u8a: Uint8Array, startAt: number, Type: CodecClass<T>): [number, number] {
const count = result.length;
let offset = startAt;
let i = 0;
try {
while (i < count) {
const value = new Type(registry, u8a.subarray(offset));
offset += value.initialU8aLength || value.encodedLength;
result[i] = value;
i++;
}
} catch (error) {
throw new Error(formatFailure(registry, 'decodeU8aVec', result, error as Error, u8a.subarray(offset), i, count, Type));
}
return [offset, offset - startAt];
}