-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Serializer.ts
393 lines (363 loc) · 11 KB
/
Serializer.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* eslint-disable @typescript-eslint/require-await */
import { Root, Type, INamespace, parse } from "protobufjs";
import * as koinosPbToProto from "@roamin/koinos-pb-to-proto";
import { TypeField } from "./interface";
import {
btypeDecodeValue,
btypeEncodeValue,
decodeBase64url,
decodeBase64,
} from "./utils";
const OP_BYTES_1 = "(btype)";
const OP_BYTES_2 = "(koinos.btype)";
const nativeTypes = [
"double",
"float",
"int32",
"int64",
"uint32",
"uint64",
"sint32",
"sint64",
"fixed32",
"fixed64",
"sfixed32",
"sfixed64",
"bool",
"string",
"bytes",
];
/**
* The serializer class serialize and deserialize data using
* protocol buffers. It accepts the descriptor in JSON or binary format
*
* NOTE: This class uses the [protobufjs](https://www.npmjs.com/package/protobufjs)
* library internally, which uses reflection (use of _eval_
* and _new Function_) for the construction of the types.
* This could cause issues in environments where _eval_ is not
* allowed, like in browser extensions. In such cases, this class
* must be confined in a [sandbox environment](https://developer.chrome.com/docs/apps/app_external/#sandboxing)
* where _eval_ is allowed. This is the principal reason of
* having the serializer in a separate class.
*
* @example
*
* ```ts
* // using descriptor JSON
* const descriptorJson = {
* nested: {
* awesomepackage: {
* nested: {
* AwesomeMessage: {
* fields: {
* awesome_field: {
* type: "string",
* id: 1,
* },
* },
* },
* },
* },
* },
* };
* const serializer1 = new Serializer(descriptorJson);
* const message1 = await serializer1.deserialize(
* "CgZrb2lub3M=",
* "AwesomeMessage"
* );
* console.log(message1);
* // { awesome_field: 'koinos' }
*
* // using descriptor binary
* const descriptorBinary =
* "Cl4KDWF3ZXNvbWUucHJvdG8SDmF3ZXNvbWVwYWN" +
* "rYWdlIjUKDkF3ZXNvbWVNZXNzYWdlEiMKDWF3ZX" +
* "NvbWVfZmllbGQYASABKAlSDGF3ZXNvbWVGaWVsZ" +
* "GIGcHJvdG8z";
* const serializer2 = new Serializer(descriptorBinary);
* const message2 = await serializer2.deserialize(
* "CgZrb2lub3M=",
* "AwesomeMessage"
* );
* console.log(message2);
* // { awesome_field: 'koinos' }
* ```
*/
export class Serializer {
/**
* Protobuffers descriptor in JSON format.
* See https://www.npmjs.com/package/protobufjs#using-json-descriptors
*/
types: INamespace | string;
/**
* Protobuffer definitions
*/
root: Root;
/**
* Default type for all serializations
*/
defaultType?: Type;
/**
* Type name for arguments when using
* [[Provider.invokeSystemCall]]
*/
argumentsTypeName?: string;
/**
* Type name for the output when using
* [[Provider.invokeSystemCall]]
*/
returnTypeName?: string;
/**
* Preformat bytes for base64url, base58 or hex string
*/
bytesConversion = true;
/**
* Verify checksum in addresses during serialization
* or deserialization
*/
verifyChecksum = {
serialize: true,
deserialize: false,
};
constructor(
types: INamespace | string,
opts?: {
/**
* Default type name. Use this option when you
* always want to serialize/deserialize the same type
*/
defaultTypeName?: string;
/**
* Type name for arguments when using
* [[Provider.invokeSystemCall]]
*/
argumentsTypeName?: string;
/**
* Type name for the output when using
* [[Provider.invokeSystemCall]]
*/
returnTypeName?: string;
/**
* Bytes conversion. Option to preformat bytes
* when "(koinos_bytes_type)" is defined in the type
* definitions. By default it is true.
*/
bytesConversion?: boolean;
}
) {
this.types = types;
if (typeof types === "string") {
const protos = koinosPbToProto.convert(decodeBase64(types) as Buffer);
this.root = new Root();
for (const proto of protos) {
parse(proto.definition, this.root, { keepCase: true });
}
} else {
this.root = Root.fromJSON(types);
}
if (opts?.defaultTypeName)
this.defaultType = this.root.lookupType(opts.defaultTypeName);
if (opts?.argumentsTypeName)
this.argumentsTypeName = opts.argumentsTypeName;
if (opts?.returnTypeName) this.returnTypeName = opts.returnTypeName;
if (opts && typeof opts.bytesConversion !== "undefined")
this.bytesConversion = opts.bytesConversion;
}
btypeDecode(
valueBtypeEncoded: Record<string, unknown> | unknown[],
protobufType: Type,
verifyChecksum: boolean
) {
const valueBtypeDecoded = {} as Record<string, unknown>;
Object.keys(protobufType.fields).forEach((fieldName) => {
// @ts-ignore
const { options, name, type, rule } = protobufType.fields[fieldName];
if (!valueBtypeEncoded[name]) return;
const typeField: TypeField = { type };
if (options) {
if (options[OP_BYTES_1])
typeField.btype = options[OP_BYTES_1] as string;
else if (options[OP_BYTES_2])
typeField.btype = options[OP_BYTES_2] as string;
}
// arrays
if (rule === "repeated") {
valueBtypeDecoded[name] = (valueBtypeEncoded[name] as unknown[]).map(
(itemEncoded) => {
// custom objects
if (!nativeTypes.includes(type)) {
const protoBuf = this.root.lookupTypeOrEnum(type);
if (!protoBuf.fields) {
// it's an enum
return itemEncoded;
}
return this.btypeDecode(
itemEncoded as Record<string, unknown>,
protoBuf,
verifyChecksum
);
}
// native types
return btypeDecodeValue(itemEncoded, typeField, verifyChecksum);
}
);
return;
}
// custom objects
if (!nativeTypes.includes(type)) {
const protoBuf = this.root.lookupTypeOrEnum(type);
if (!protoBuf.fields) {
// it's an enum
valueBtypeDecoded[name] = valueBtypeEncoded[name];
return;
}
valueBtypeDecoded[name] = this.btypeDecode(
valueBtypeEncoded[name] as Record<string, unknown>,
protoBuf,
verifyChecksum
);
return;
}
// native types
valueBtypeDecoded[name] = btypeDecodeValue(
valueBtypeEncoded[name],
typeField,
verifyChecksum
);
});
return valueBtypeDecoded;
}
btypeEncode(
valueBtypeDecoded: Record<string, unknown> | unknown[],
protobufType: Type,
verifyChecksum: boolean
) {
const valueBtypeEncoded = {} as Record<string, unknown>;
Object.keys(protobufType.fields).forEach((fieldName) => {
// @ts-ignore
const { options, name, type, rule } = protobufType.fields[fieldName];
if (!valueBtypeDecoded[name]) return;
const typeField: TypeField = { type };
if (options) {
if (options[OP_BYTES_1])
typeField.btype = options[OP_BYTES_1] as string;
else if (options[OP_BYTES_2])
typeField.btype = options[OP_BYTES_2] as string;
}
// arrays
if (rule === "repeated") {
valueBtypeEncoded[name] = (valueBtypeDecoded[name] as unknown[]).map(
(itemDecoded) => {
// custom objects
if (!nativeTypes.includes(type)) {
const protoBuf = this.root.lookupTypeOrEnum(type);
if (!protoBuf.fields) {
// it's an enum
return itemDecoded;
}
return this.btypeEncode(
itemDecoded as Record<string, unknown>,
protoBuf,
verifyChecksum
);
}
// native types
return btypeEncodeValue(itemDecoded, typeField, verifyChecksum);
}
);
return;
}
// custom objects
if (!nativeTypes.includes(type)) {
const protoBuf = this.root.lookupTypeOrEnum(type);
if (!protoBuf.fields) {
// it's an enum
valueBtypeEncoded[name] = valueBtypeDecoded[name];
return;
}
valueBtypeEncoded[name] = this.btypeEncode(
valueBtypeDecoded[name] as Record<string, unknown>,
protoBuf,
verifyChecksum
);
return;
}
// native types
valueBtypeEncoded[name] = btypeEncodeValue(
valueBtypeDecoded[name],
typeField,
verifyChecksum
);
});
return valueBtypeEncoded;
}
/**
* Function to encode a type using the protobuffer definitions
* It also prepares the bytes for special cases (base58, hex string)
* when bytesConversion param is true.
*/
async serialize(
valueDecoded: Record<string, unknown>,
typeName?: string,
opts?: { bytesConversion?: boolean; verifyChecksum?: boolean }
): Promise<Uint8Array> {
let protobufType: Type;
if (this.defaultType) protobufType = this.defaultType;
else if (!typeName) throw new Error("no typeName defined");
else protobufType = this.root.lookupType(typeName);
let object: Record<string, unknown> = {};
const bytesConversion =
opts?.bytesConversion === undefined
? this.bytesConversion
: opts.bytesConversion;
const verifyChecksum =
opts?.verifyChecksum === undefined
? this.verifyChecksum.serialize
: opts.verifyChecksum;
if (bytesConversion) {
object = this.btypeDecode(valueDecoded, protobufType, verifyChecksum);
} else {
object = valueDecoded;
}
const message = protobufType.create(object);
const buffer = protobufType.encode(message).finish();
return buffer;
}
/**
* Function to decode bytes using the protobuffer definitions
* It also encodes the bytes for special cases (base58, hex string)
* when bytesConversion param is true.
*/
async deserialize<T = Record<string, unknown>>(
valueEncoded: string | Uint8Array,
typeName?: string,
opts?: { bytesConversion?: boolean; verifyChecksum?: boolean }
): Promise<T> {
const valueBuffer =
typeof valueEncoded === "string"
? decodeBase64url(valueEncoded)
: valueEncoded;
let protobufType: Type;
if (this.defaultType) protobufType = this.defaultType;
else if (!typeName) throw new Error("no typeName defined");
else protobufType = this.root.lookupType(typeName);
const message = protobufType.decode(valueBuffer);
const object = protobufType.toObject(message, {
longs: String,
defaults: true,
});
const bytesConversion =
opts?.bytesConversion === undefined
? this.bytesConversion
: opts.bytesConversion;
const verifyChecksum =
opts?.verifyChecksum === undefined
? this.verifyChecksum.deserialize
: opts.verifyChecksum;
if (bytesConversion) {
return this.btypeEncode(object, protobufType, verifyChecksum) as T;
}
return object as T;
}
}
export default Serializer;