-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbinaryMarshal.ts
More file actions
339 lines (318 loc) · 10.6 KB
/
Copy pathbinaryMarshal.ts
File metadata and controls
339 lines (318 loc) · 10.6 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import BigNumber from "bignumber.js";
import { ErrorWithCode, RenJSError } from "../../errors";
import { utils } from "../../internal";
import { isPackListType, isPackStructType } from "./common";
import {
PackListType,
PackPrimitive,
PackStructType,
PackTypeDefinition,
TypedPackValue,
} from "./types";
// === Pack Types ==============================================================
/**
* Convert a PackType string to its numeric ID, as defined in the Go reference
* implementation.
* (https://github.com/renproject/pack/blob/e0f417fbbd472eccd99e4bf304b19dc04a31a950/kind.go#L19)
*/
export const encodePackType = (type: PackTypeDefinition): number => {
// Primitive types.
switch (type) {
case "nil":
return 0;
// KindBool is the kind of all Bool values.
case PackPrimitive.Bool:
return 1;
// KindU8 is the kind of all U8 values.
case PackPrimitive.U8:
return 2;
// KindU16 is the kind of all U16 values.
case PackPrimitive.U16:
return 3;
// KindU32 is the kind of all U32 values.
case PackPrimitive.U32:
return 4;
// KindU64 is the kind of all U64 values.
case PackPrimitive.U64:
return 5;
// KindU128 is the kind of all U128 values.
case PackPrimitive.U128:
return 6;
// KindU256 is the kind of all U256 values.
case PackPrimitive.U256:
return 7;
// KindString is the kind of all utf8 strings.
case PackPrimitive.Str:
return 10;
// KindBytes is the kind of all dynamic byte arrays.
case PackPrimitive.Bytes:
return 11;
// KindBytes32 is the kind of all 32-byte arrays.
case PackPrimitive.Bytes32:
return 12;
// KindBytes65 is the kind of all 65-byte arrays.
case PackPrimitive.Bytes65:
return 13;
// KindBytes64 is the kind of all 64-byte arrays.
case PackPrimitive.Bytes64:
return 14;
}
// Complex types.
if (typeof type === "object" && Object.keys(type).length === 1) {
switch (Object.keys(type)[0]) {
// KindStruct is the kind of all struct values. It is abstract, because it does
// not specify the fields in the struct.
case "struct":
return 20;
// KindList is the kind of all list values. It is abstract, because it does
// not specify the type of the elements in the list.
case "list":
return 21;
}
}
throw new Error(`Unknown type ${String(type)}.`);
};
/**
* Convert a JavaScript number to a big-endian Uint8Array of the provided length.
*/
export const encodeUint = (
value: BigNumber | string | number,
bits: number,
): Uint8Array => {
try {
return utils.toNBytes(
typeof value === "number"
? value
: BigNumber.isBigNumber(value)
? value.toFixed()
: value.toString(),
bits / 8,
);
} catch (error: unknown) {
throw ErrorWithCode.updateError(
error,
(error as ErrorWithCode).code || RenJSError.INTERNAL_ERROR,
`Unable to encode uint${bits} '${String(value)}'`,
);
}
};
export const encodeU =
(bits: number) =>
(value: BigNumber | string | number): Uint8Array =>
encodeUint(value, bits);
export const encodeU8 = encodeU(8);
export const encodeU16 = encodeU(16);
export const encodeU32 = encodeU(32);
export const encodeU64 = encodeU(64);
export const encodeU128 = encodeU(128);
export const encodeU256 = encodeU(256);
export const withLength = (value: Uint8Array): Uint8Array =>
utils.concat([encodeU32(value.length), value]);
/**
* Encode a string, prefixed by its length.
*/
export const encodeString = (value: string): Uint8Array =>
withLength(utils.fromUTF8String(value));
/**
* Encode a struct type by prefixing the `struct` pack type ID and the number
* of struct entries, and then each field name followed by the field's
* encoded type definition.
*/
export const encodePackStructType = (type: PackStructType): Uint8Array => {
const length = encodeU32(type.struct.length);
return utils.concat([
new Uint8Array([encodePackType(type)]),
length,
...type.struct.map((field) => {
const keys = Object.keys(field);
if (keys.length === 0) {
throw new Error(`Invalid struct field with no entries.`);
}
if (keys.length > 1) {
throw new Error(`Invalid struct field with multiple entries.`);
}
const key = Object.keys(field)[0];
const fieldType = field[key];
return utils.concat([
encodeString(key),
encodePackTypeDefinition(fieldType),
]);
}),
]);
};
/**
* Encode a list type by concatenating the `list` pack type ID followed by the
* encoded type definition of the list's sub-type.
*/
export const encodePackListType = (type: PackListType): Uint8Array =>
utils.concat([
new Uint8Array([encodePackType(type)]),
encodePackTypeDefinition(type.list),
]);
/**
* Encode a pack type, as defined above for each type.
*/
export const encodePackTypeDefinition = (
type: PackTypeDefinition,
): Uint8Array => {
if (isPackStructType(type)) {
return encodePackStructType(type);
} else if (isPackListType(type)) {
return encodePackListType(type);
} else if (typeof type === "string") {
return new Uint8Array([encodePackType(type)]);
}
throw new Error(`Unable to encode type ${String(type)}.`);
};
// === Pack Values =============================================================
/**
* Encode a JavaScript value with an associated pack type into a Uint8Array.
*/
export const encodePackPrimitive = (
type: PackPrimitive,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
): Uint8Array => {
switch (type) {
// Booleans
case PackPrimitive.Bool:
return encodeU8(value ? 1 : 0);
// Integers
case PackPrimitive.U8:
return encodeU8(value);
case PackPrimitive.U16:
return encodeU16(value);
case PackPrimitive.U32:
return encodeU32(value);
case PackPrimitive.U64:
return encodeU64(value);
case PackPrimitive.U128:
return encodeU128(value);
case PackPrimitive.U256:
return encodeU256(value);
// Strings
case PackPrimitive.Str: {
return encodeString(value);
}
// Bytes
case PackPrimitive.Bytes: {
return withLength(
value instanceof Uint8Array
? value
: // Supports base64 url format
utils.fromBase64(value),
);
}
case PackPrimitive.Bytes32:
case PackPrimitive.Bytes65:
case PackPrimitive.Bytes64:
return value instanceof Uint8Array
? value
: // Supports base64 url format
utils.fromBase64(value);
}
};
/**
* Encode a pack struct by concatenating the encoded values of each of the
* pack's fields.
*/
export const encodePackStruct = (
type: PackStructType,
value: unknown,
): Uint8Array =>
utils.concat(
type.struct.map((member) => {
const keys = Object.keys(member);
if (keys.length === 0) {
throw new Error(`Invalid struct member with no entries.`);
}
if (keys.length > 1) {
throw new Error(`Invalid struct member with multiple entries.`);
}
if (typeof value !== "object") {
throw new Error(
`Invalid struct value of type "${typeof value}".`,
);
}
if (value === null) {
throw new Error(`Invalid struct value "null".`);
}
const key = Object.keys(member)[0];
const memberType = member[key];
try {
return encodePackValue(memberType, value[key]);
} catch (error: unknown) {
throw ErrorWithCode.updateError(
error,
(error as ErrorWithCode).code || RenJSError.INTERNAL_ERROR,
`Unable to encode struct field ${key}`,
);
}
}),
);
/**
* Encode a pack list by concatenating the encoded values of each of the
* list's values.
*/
export const encodeListStruct = (
type: PackListType,
value: unknown[],
): Uint8Array => {
const subtype = type.list;
return utils.concat(
value.map((element, i) => {
try {
return encodePackValue(subtype, element);
} catch (error: unknown) {
if (error instanceof Error) {
error.message = `Unable to encode array element #${i}: ${String(
error.message,
)}`;
}
throw error;
}
}),
);
};
/**
* Encode a pack value by using the encoding defined for the provided
* pack type.
*/
export const encodePackValue = (
type: PackTypeDefinition,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
): Uint8Array => {
if (isPackStructType(type)) {
return encodePackStruct(type, value);
} else if (isPackListType(type)) {
return encodeListStruct(type, value);
} else if (typeof type === "string") {
if (type === "nil") return new Uint8Array();
return encodePackPrimitive(type, value);
}
throw new Error(
`Unknown value type ${String(type)}${
!type ? ` for value ${String(value)}` : ""
}`,
);
};
/**
* Encode a `{ t, v }` pair by concatenating the pack type-encoding of `t`
* followed by the pack value-encoding of `v`.
*/
export const encodeTypedPackValue = ({ t, v }: TypedPackValue): Uint8Array => {
try {
const encodedType = encodePackTypeDefinition(t);
const encodedValue = encodePackValue(t, v);
return utils.concat([encodedType, encodedValue]);
} catch (error: unknown) {
if (error instanceof Error) {
error.message = `Error encoding typed pack value: ${error.message}`;
throw error;
}
throw new Error(
`Error encoding typed pack value: ${utils.extractError(error)}`,
);
}
};