@leofcoin/codec-format-interface 1.6.11 → 1.6.13
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 +20 -20
- package/dist/basic-interface.d.ts +33 -33
- package/dist/codec-format-interface.d.ts +32 -32
- package/dist/codec-hash.d.ts +27 -26
- package/dist/codec.d.ts +15 -15
- package/dist/index.d.ts +9 -9
- package/dist/index.js +435 -433
- package/package.json +48 -48
- package/dist/codecs.d.ts +0 -75
package/dist/index.js
CHANGED
|
@@ -12,447 +12,449 @@ export { codecs } from '@leofcoin/codecs';
|
|
|
12
12
|
*/
|
|
13
13
|
var isHex = (function (string) { return /^[A-F0-9]+$/i.test(string); });
|
|
14
14
|
|
|
15
|
-
let BasicInterface$1 = class BasicInterface {
|
|
16
|
-
encoded;
|
|
17
|
-
decoded;
|
|
18
|
-
keys;
|
|
19
|
-
name;
|
|
20
|
-
#proto;
|
|
21
|
-
set proto(value) {
|
|
22
|
-
this.#proto = value;
|
|
23
|
-
this.keys = Object.keys(value);
|
|
24
|
-
}
|
|
25
|
-
get proto() {
|
|
26
|
-
return this.#proto;
|
|
27
|
-
}
|
|
28
|
-
decode(encoded) {
|
|
29
|
-
encoded = encoded || this.encoded;
|
|
30
|
-
return new Object();
|
|
31
|
-
}
|
|
32
|
-
encode(decoded) {
|
|
33
|
-
decoded = decoded || this.decoded;
|
|
34
|
-
return new Uint8Array();
|
|
35
|
-
}
|
|
36
|
-
// get Codec(): Codec {}
|
|
37
|
-
protoEncode(data) {
|
|
38
|
-
// check schema
|
|
39
|
-
return proto.encode(this.proto, data);
|
|
40
|
-
}
|
|
41
|
-
protoDecode(data) {
|
|
42
|
-
// check schema
|
|
43
|
-
return proto.decode(this.proto, data);
|
|
44
|
-
}
|
|
45
|
-
isHex(string) {
|
|
46
|
-
return isHex(string);
|
|
47
|
-
}
|
|
48
|
-
isBase32(string) {
|
|
49
|
-
return bs32.isBase32(string);
|
|
50
|
-
}
|
|
51
|
-
isBase58(string) {
|
|
52
|
-
return base58.isBase58(string);
|
|
53
|
-
}
|
|
54
|
-
fromBs32(encoded) {
|
|
55
|
-
return this.decode(bs32.decode(encoded));
|
|
56
|
-
}
|
|
57
|
-
fromBs58(encoded) {
|
|
58
|
-
return this.decode(fromBase58(encoded));
|
|
59
|
-
}
|
|
60
|
-
async toArray() {
|
|
61
|
-
const array = [];
|
|
62
|
-
for await (const value of this.encoded.values()) {
|
|
63
|
-
array.push(value);
|
|
64
|
-
}
|
|
65
|
-
return array;
|
|
66
|
-
}
|
|
67
|
-
fromString(string) {
|
|
68
|
-
const array = string.split(',');
|
|
69
|
-
const arrayLike = array.map(string => Number(string));
|
|
70
|
-
return this.decode(Uint8Array.from(arrayLike));
|
|
71
|
-
}
|
|
72
|
-
fromHex(string) {
|
|
73
|
-
return this.decode(fromHex(string));
|
|
74
|
-
}
|
|
75
|
-
fromArray(array) {
|
|
76
|
-
return this.decode(Uint8Array.from([...array]));
|
|
77
|
-
}
|
|
78
|
-
fromEncoded(encoded) {
|
|
79
|
-
return this.decode(encoded);
|
|
80
|
-
}
|
|
81
|
-
toString() {
|
|
82
|
-
if (!this.encoded)
|
|
83
|
-
this.encode();
|
|
84
|
-
return this.encoded.toString();
|
|
85
|
-
}
|
|
86
|
-
toHex() {
|
|
87
|
-
if (!this.encoded)
|
|
88
|
-
this.encode();
|
|
89
|
-
return toHex(this.encoded.toString().split(',').map(number => Number(number)));
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* @return {String} encoded
|
|
93
|
-
*/
|
|
94
|
-
toBs32() {
|
|
95
|
-
if (!this.encoded)
|
|
96
|
-
this.encode();
|
|
97
|
-
return toBase32(this.encoded);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* @return {String} encoded
|
|
101
|
-
*/
|
|
102
|
-
toBs58() {
|
|
103
|
-
if (!this.encoded)
|
|
104
|
-
this.encode();
|
|
105
|
-
return toBase58(this.encoded);
|
|
106
|
-
}
|
|
15
|
+
let BasicInterface$1 = class BasicInterface {
|
|
16
|
+
encoded;
|
|
17
|
+
decoded;
|
|
18
|
+
keys;
|
|
19
|
+
name;
|
|
20
|
+
#proto;
|
|
21
|
+
set proto(value) {
|
|
22
|
+
this.#proto = value;
|
|
23
|
+
this.keys = Object.keys(value);
|
|
24
|
+
}
|
|
25
|
+
get proto() {
|
|
26
|
+
return this.#proto;
|
|
27
|
+
}
|
|
28
|
+
decode(encoded) {
|
|
29
|
+
encoded = encoded || this.encoded;
|
|
30
|
+
return new Object();
|
|
31
|
+
}
|
|
32
|
+
encode(decoded) {
|
|
33
|
+
decoded = decoded || this.decoded;
|
|
34
|
+
return new Uint8Array();
|
|
35
|
+
}
|
|
36
|
+
// get Codec(): Codec {}
|
|
37
|
+
protoEncode(data) {
|
|
38
|
+
// check schema
|
|
39
|
+
return proto.encode(this.proto, data);
|
|
40
|
+
}
|
|
41
|
+
protoDecode(data) {
|
|
42
|
+
// check schema
|
|
43
|
+
return proto.decode(this.proto, data);
|
|
44
|
+
}
|
|
45
|
+
isHex(string) {
|
|
46
|
+
return isHex(string);
|
|
47
|
+
}
|
|
48
|
+
isBase32(string) {
|
|
49
|
+
return bs32.isBase32(string);
|
|
50
|
+
}
|
|
51
|
+
isBase58(string) {
|
|
52
|
+
return base58.isBase58(string);
|
|
53
|
+
}
|
|
54
|
+
fromBs32(encoded) {
|
|
55
|
+
return this.decode(bs32.decode(encoded));
|
|
56
|
+
}
|
|
57
|
+
fromBs58(encoded) {
|
|
58
|
+
return this.decode(fromBase58(encoded));
|
|
59
|
+
}
|
|
60
|
+
async toArray() {
|
|
61
|
+
const array = [];
|
|
62
|
+
for await (const value of this.encoded.values()) {
|
|
63
|
+
array.push(value);
|
|
64
|
+
}
|
|
65
|
+
return array;
|
|
66
|
+
}
|
|
67
|
+
fromString(string) {
|
|
68
|
+
const array = string.split(',');
|
|
69
|
+
const arrayLike = array.map(string => Number(string));
|
|
70
|
+
return this.decode(Uint8Array.from(arrayLike));
|
|
71
|
+
}
|
|
72
|
+
fromHex(string) {
|
|
73
|
+
return this.decode(fromHex(string));
|
|
74
|
+
}
|
|
75
|
+
fromArray(array) {
|
|
76
|
+
return this.decode(Uint8Array.from([...array]));
|
|
77
|
+
}
|
|
78
|
+
fromEncoded(encoded) {
|
|
79
|
+
return this.decode(encoded);
|
|
80
|
+
}
|
|
81
|
+
toString() {
|
|
82
|
+
if (!this.encoded)
|
|
83
|
+
this.encode();
|
|
84
|
+
return this.encoded.toString();
|
|
85
|
+
}
|
|
86
|
+
toHex() {
|
|
87
|
+
if (!this.encoded)
|
|
88
|
+
this.encode();
|
|
89
|
+
return toHex(this.encoded.toString().split(',').map(number => Number(number)));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @return {String} encoded
|
|
93
|
+
*/
|
|
94
|
+
toBs32() {
|
|
95
|
+
if (!this.encoded)
|
|
96
|
+
this.encode();
|
|
97
|
+
return toBase32(this.encoded);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* @return {String} encoded
|
|
101
|
+
*/
|
|
102
|
+
toBs58() {
|
|
103
|
+
if (!this.encoded)
|
|
104
|
+
this.encode();
|
|
105
|
+
return toBase58(this.encoded);
|
|
106
|
+
}
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
-
let Codec$1 = class Codec extends BasicInterface$1 {
|
|
110
|
-
codecBuffer;
|
|
111
|
-
codec;
|
|
112
|
-
hashAlg;
|
|
113
|
-
constructor(buffer) {
|
|
114
|
-
super();
|
|
115
|
-
if (buffer) {
|
|
116
|
-
if (buffer instanceof Uint8Array) {
|
|
117
|
-
const codec = varint.decode(buffer);
|
|
118
|
-
const name = this.getCodecName(codec);
|
|
119
|
-
if (name) {
|
|
120
|
-
this.name = name;
|
|
121
|
-
this.encoded = buffer;
|
|
122
|
-
this.decode(buffer);
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
this.encode(Number(new TextDecoder().decode(buffer)));
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
else if (buffer instanceof ArrayBuffer) {
|
|
129
|
-
const codec = varint.decode(buffer);
|
|
130
|
-
const name = this.getCodecName(codec);
|
|
131
|
-
if (name) {
|
|
132
|
-
this.name = name;
|
|
133
|
-
this.decode(buffer);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
else if (typeof buffer === 'string') {
|
|
140
|
-
if (utils.getCodec(buffer))
|
|
141
|
-
this.fromName(buffer);
|
|
142
|
-
else if (this.isHex(buffer))
|
|
143
|
-
this.fromHex(buffer);
|
|
144
|
-
else if (this.isBase32(buffer))
|
|
145
|
-
this.fromBs32(buffer);
|
|
146
|
-
else if (this.isBase58(buffer))
|
|
147
|
-
this.fromBs58(buffer);
|
|
148
|
-
else
|
|
149
|
-
this.fromString(buffer);
|
|
150
|
-
}
|
|
151
|
-
if (!isNaN(buffer))
|
|
152
|
-
if (utils.getCodec(buffer))
|
|
153
|
-
this.fromCodec(buffer);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
fromEncoded(encoded) {
|
|
157
|
-
const codec = varint.decode(encoded);
|
|
158
|
-
const name = this.getCodecName(codec);
|
|
159
|
-
this.name = name;
|
|
160
|
-
this.encoded = encoded;
|
|
161
|
-
return this.decode(encoded);
|
|
162
|
-
}
|
|
163
|
-
getCodec(name) {
|
|
164
|
-
return utils.getCodec(name);
|
|
165
|
-
}
|
|
166
|
-
getCodecName(codec) {
|
|
167
|
-
return utils.getCodecName(codec);
|
|
168
|
-
}
|
|
169
|
-
getHashAlg(name) {
|
|
170
|
-
return utils.getHashAlg(name);
|
|
171
|
-
}
|
|
172
|
-
fromCodec(codec) {
|
|
173
|
-
this.name = this.getCodecName(codec);
|
|
174
|
-
this.hashAlg = this.getHashAlg(this.name);
|
|
175
|
-
this.codec = this.getCodec(this.name);
|
|
176
|
-
this.codecBuffer = varint.encode(this.codec);
|
|
177
|
-
}
|
|
178
|
-
fromName(name) {
|
|
179
|
-
const codec = this.getCodec(name);
|
|
180
|
-
this.name = name;
|
|
181
|
-
this.codec = codec;
|
|
182
|
-
this.hashAlg = this.getHashAlg(name);
|
|
183
|
-
this.codecBuffer = varint.encode(this.codec);
|
|
184
|
-
}
|
|
185
|
-
decode(encoded) {
|
|
186
|
-
encoded = encoded || this.encoded;
|
|
187
|
-
const codec = varint.decode(encoded);
|
|
188
|
-
this.fromCodec(codec);
|
|
189
|
-
return this.decoded;
|
|
190
|
-
}
|
|
191
|
-
encode(codec) {
|
|
192
|
-
codec = codec || this.codec;
|
|
193
|
-
this.encoded = varint.encode(codec);
|
|
194
|
-
return this.encoded;
|
|
195
|
-
}
|
|
109
|
+
let Codec$1 = class Codec extends BasicInterface$1 {
|
|
110
|
+
codecBuffer;
|
|
111
|
+
codec;
|
|
112
|
+
hashAlg;
|
|
113
|
+
constructor(buffer) {
|
|
114
|
+
super();
|
|
115
|
+
if (buffer) {
|
|
116
|
+
if (buffer instanceof Uint8Array) {
|
|
117
|
+
const codec = varint.decode(buffer);
|
|
118
|
+
const name = this.getCodecName(codec);
|
|
119
|
+
if (name) {
|
|
120
|
+
this.name = name;
|
|
121
|
+
this.encoded = buffer;
|
|
122
|
+
this.decode(buffer);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
this.encode(Number(new TextDecoder().decode(buffer)));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
129
|
+
const codec = varint.decode(new Uint8Array(buffer));
|
|
130
|
+
const name = this.getCodecName(codec);
|
|
131
|
+
if (name) {
|
|
132
|
+
this.name = name;
|
|
133
|
+
this.decode(buffer);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else if (typeof buffer === 'string') {
|
|
140
|
+
if (utils.getCodec(buffer))
|
|
141
|
+
this.fromName(buffer);
|
|
142
|
+
else if (this.isHex(buffer))
|
|
143
|
+
this.fromHex(buffer);
|
|
144
|
+
else if (this.isBase32(buffer))
|
|
145
|
+
this.fromBs32(buffer);
|
|
146
|
+
else if (this.isBase58(buffer))
|
|
147
|
+
this.fromBs58(buffer);
|
|
148
|
+
else
|
|
149
|
+
this.fromString(buffer);
|
|
150
|
+
}
|
|
151
|
+
if (!isNaN(buffer))
|
|
152
|
+
if (utils.getCodec(buffer))
|
|
153
|
+
this.fromCodec(buffer);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
fromEncoded(encoded) {
|
|
157
|
+
const codec = varint.decode(encoded);
|
|
158
|
+
const name = this.getCodecName(codec);
|
|
159
|
+
this.name = name;
|
|
160
|
+
this.encoded = encoded;
|
|
161
|
+
return this.decode(encoded);
|
|
162
|
+
}
|
|
163
|
+
getCodec(name) {
|
|
164
|
+
return utils.getCodec(name);
|
|
165
|
+
}
|
|
166
|
+
getCodecName(codec) {
|
|
167
|
+
return utils.getCodecName(codec);
|
|
168
|
+
}
|
|
169
|
+
getHashAlg(name) {
|
|
170
|
+
return utils.getHashAlg(name);
|
|
171
|
+
}
|
|
172
|
+
fromCodec(codec) {
|
|
173
|
+
this.name = this.getCodecName(codec);
|
|
174
|
+
this.hashAlg = this.getHashAlg(this.name);
|
|
175
|
+
this.codec = this.getCodec(this.name);
|
|
176
|
+
this.codecBuffer = varint.encode(this.codec);
|
|
177
|
+
}
|
|
178
|
+
fromName(name) {
|
|
179
|
+
const codec = this.getCodec(name);
|
|
180
|
+
this.name = name;
|
|
181
|
+
this.codec = codec;
|
|
182
|
+
this.hashAlg = this.getHashAlg(name);
|
|
183
|
+
this.codecBuffer = varint.encode(this.codec);
|
|
184
|
+
}
|
|
185
|
+
decode(encoded) {
|
|
186
|
+
encoded = encoded || this.encoded;
|
|
187
|
+
const codec = varint.decode(encoded);
|
|
188
|
+
this.fromCodec(codec);
|
|
189
|
+
return this.decoded;
|
|
190
|
+
}
|
|
191
|
+
encode(codec) {
|
|
192
|
+
codec = codec || this.codec;
|
|
193
|
+
this.encoded = varint.encode(codec);
|
|
194
|
+
return this.encoded;
|
|
195
|
+
}
|
|
196
196
|
};
|
|
197
197
|
|
|
198
|
-
let CodecHash$1 = class CodecHash extends BasicInterface$1 {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (options.
|
|
206
|
-
this.
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
await this.
|
|
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
|
-
uint8Array
|
|
282
|
-
this.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (this.
|
|
299
|
-
this.
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
this.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
198
|
+
let CodecHash$1 = class CodecHash extends BasicInterface$1 {
|
|
199
|
+
codec;
|
|
200
|
+
codecs;
|
|
201
|
+
digest;
|
|
202
|
+
size;
|
|
203
|
+
constructor(buffer, options) {
|
|
204
|
+
super();
|
|
205
|
+
if (options.name)
|
|
206
|
+
this.name = options.name;
|
|
207
|
+
else
|
|
208
|
+
this.name = 'disco-hash';
|
|
209
|
+
if (options.codecs)
|
|
210
|
+
this.codecs = options.codecs;
|
|
211
|
+
return this.init(buffer);
|
|
212
|
+
}
|
|
213
|
+
async init(uint8Array) {
|
|
214
|
+
if (uint8Array) {
|
|
215
|
+
if (uint8Array instanceof Uint8Array) {
|
|
216
|
+
this.codec = new Codec$1(uint8Array);
|
|
217
|
+
const name = this.codec.name;
|
|
218
|
+
if (name) {
|
|
219
|
+
this.name = name;
|
|
220
|
+
this.decode(uint8Array);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
await this.encode(uint8Array);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (typeof uint8Array === 'string') {
|
|
227
|
+
if (this.isHex(uint8Array))
|
|
228
|
+
await this.fromHex(uint8Array);
|
|
229
|
+
if (this.isBase32(uint8Array))
|
|
230
|
+
await this.fromBs32(uint8Array);
|
|
231
|
+
else if (this.isBase58(uint8Array))
|
|
232
|
+
await this.fromBs58(uint8Array);
|
|
233
|
+
else
|
|
234
|
+
throw new Error(`unsupported string ${uint8Array}`);
|
|
235
|
+
}
|
|
236
|
+
else if (typeof uint8Array === 'object')
|
|
237
|
+
await this.fromJSON(uint8Array);
|
|
238
|
+
}
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
get prefix() {
|
|
242
|
+
const length = this.length;
|
|
243
|
+
const uint8Array = new Uint8Array(length.length + this.codec.codecBuffer.length);
|
|
244
|
+
uint8Array.set(length);
|
|
245
|
+
uint8Array.set(this.codec.codecBuffer, length.length);
|
|
246
|
+
return uint8Array;
|
|
247
|
+
}
|
|
248
|
+
get length() {
|
|
249
|
+
return varint.encode(this.size);
|
|
250
|
+
}
|
|
251
|
+
get buffer() {
|
|
252
|
+
return this.encoded;
|
|
253
|
+
}
|
|
254
|
+
get hash() {
|
|
255
|
+
return this.encoded;
|
|
256
|
+
}
|
|
257
|
+
fromJSON(json) {
|
|
258
|
+
return this.encode(new TextEncoder().encode(JSON.stringify(json)));
|
|
259
|
+
}
|
|
260
|
+
async encode(buffer, name) {
|
|
261
|
+
if (!this.name && name)
|
|
262
|
+
this.name = name;
|
|
263
|
+
if (!buffer)
|
|
264
|
+
buffer = this.buffer;
|
|
265
|
+
this.codec = new Codec$1(this.name);
|
|
266
|
+
this.codec.fromName(this.name);
|
|
267
|
+
let hashAlg = this.codec.hashAlg;
|
|
268
|
+
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
269
|
+
if (hashAlg.includes('dbl')) {
|
|
270
|
+
hashAlg = hashAlg.replace('dbl-', '');
|
|
271
|
+
const hasher = await createKeccak(hashVariant);
|
|
272
|
+
await hasher.init();
|
|
273
|
+
hasher.update(buffer);
|
|
274
|
+
buffer = hasher.digest('binary');
|
|
275
|
+
}
|
|
276
|
+
const hasher = await createKeccak(hashVariant);
|
|
277
|
+
await hasher.init();
|
|
278
|
+
hasher.update(buffer);
|
|
279
|
+
this.digest = hasher.digest('binary');
|
|
280
|
+
this.size = this.digest.length;
|
|
281
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
282
|
+
uint8Array.set(this.prefix);
|
|
283
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
284
|
+
this.encoded = uint8Array;
|
|
285
|
+
return this.encoded;
|
|
286
|
+
}
|
|
287
|
+
async validate(buffer) {
|
|
288
|
+
if (Buffer.isBuffer(buffer)) {
|
|
289
|
+
const codec = varint.decode(buffer);
|
|
290
|
+
if (this.codecs[codec]) {
|
|
291
|
+
this.decode(buffer);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
await this.encode(buffer);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (typeof buffer === 'string') {
|
|
298
|
+
if (this.isHex(buffer))
|
|
299
|
+
this.fromHex(buffer);
|
|
300
|
+
if (this.isBase32(buffer))
|
|
301
|
+
this.fromBs32(buffer);
|
|
302
|
+
}
|
|
303
|
+
if (typeof buffer === 'object')
|
|
304
|
+
this.fromJSON(buffer);
|
|
305
|
+
}
|
|
306
|
+
decode(buffer) {
|
|
307
|
+
this.encoded = buffer;
|
|
308
|
+
const codec = varint.decode(buffer);
|
|
309
|
+
this.codec = new Codec$1(codec);
|
|
310
|
+
// TODO: validate codec
|
|
311
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
312
|
+
this.size = varint.decode(buffer);
|
|
313
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
314
|
+
if (this.digest.length !== this.size) {
|
|
315
|
+
throw new Error(`hash length inconsistent: ${this.encoded.toString()}`);
|
|
316
|
+
}
|
|
317
|
+
// const codec = new Codec(codec, this.codecs)
|
|
318
|
+
this.name = this.codec.name;
|
|
319
|
+
this.size = this.digest.length;
|
|
320
|
+
return {
|
|
321
|
+
codec: this.codec,
|
|
322
|
+
name: this.name,
|
|
323
|
+
size: this.size,
|
|
324
|
+
length: this.length,
|
|
325
|
+
digest: this.digest,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
326
328
|
};
|
|
327
329
|
|
|
328
|
-
let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
329
|
-
hashFormat;
|
|
330
|
-
init(buffer) {
|
|
331
|
-
if (buffer instanceof Uint8Array)
|
|
332
|
-
this.fromUint8Array(buffer);
|
|
333
|
-
else if (buffer instanceof ArrayBuffer)
|
|
334
|
-
this.fromArrayBuffer(buffer);
|
|
335
|
-
else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
|
|
336
|
-
return buffer;
|
|
337
|
-
else if (typeof buffer === 'string') {
|
|
338
|
-
if (this.isHex(buffer))
|
|
339
|
-
this.fromHex(buffer);
|
|
340
|
-
else if (this.isBase58(buffer))
|
|
341
|
-
this.fromBs58(buffer);
|
|
342
|
-
else if (this.isBase32(buffer))
|
|
343
|
-
this.fromBs32(buffer);
|
|
344
|
-
else
|
|
345
|
-
this.fromString(buffer);
|
|
346
|
-
}
|
|
347
|
-
else {
|
|
348
|
-
this.create(buffer);
|
|
349
|
-
}
|
|
350
|
-
return this;
|
|
351
|
-
}
|
|
352
|
-
hasCodec() {
|
|
353
|
-
if (!this.encoded)
|
|
354
|
-
return false;
|
|
355
|
-
const codec = new Codec$1(this.encoded);
|
|
356
|
-
if (codec.name)
|
|
357
|
-
return true;
|
|
358
|
-
}
|
|
359
|
-
decode(encoded) {
|
|
360
|
-
encoded = encoded || this.encoded;
|
|
361
|
-
const codec = new Codec$1(this.encoded);
|
|
362
|
-
if (codec.codecBuffer) {
|
|
363
|
-
encoded = encoded.slice(codec.codecBuffer.length);
|
|
364
|
-
this.name = codec.name;
|
|
365
|
-
this.decoded = this.protoDecode(encoded);
|
|
366
|
-
// try {
|
|
367
|
-
// this.decoded = JSON.parse(this.decoded)
|
|
368
|
-
// } catch {
|
|
369
|
-
// }
|
|
370
|
-
}
|
|
371
|
-
else {
|
|
372
|
-
throw new Error(`no codec found`);
|
|
373
|
-
}
|
|
374
|
-
return this.decoded;
|
|
375
|
-
}
|
|
376
|
-
encode(decoded) {
|
|
377
|
-
let encoded;
|
|
378
|
-
if (!decoded)
|
|
379
|
-
decoded = this.decoded;
|
|
380
|
-
const codec = new Codec$1(this.name);
|
|
381
|
-
if (decoded instanceof Uint8Array)
|
|
382
|
-
encoded = decoded;
|
|
383
|
-
else
|
|
384
|
-
encoded = this.protoEncode(decoded);
|
|
385
|
-
if (codec.codecBuffer) {
|
|
386
|
-
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
387
|
-
uint8Array.set(codec.codecBuffer);
|
|
388
|
-
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
389
|
-
this.encoded = uint8Array;
|
|
390
|
-
}
|
|
391
|
-
else {
|
|
392
|
-
throw new Error(`invalid codec`);
|
|
393
|
-
}
|
|
394
|
-
return this.encoded;
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
398
|
-
* @param {Object} proto - {protoObject}
|
|
399
|
-
* @param {Object} options - {hashFormat, name}
|
|
400
|
-
*/
|
|
401
|
-
constructor(buffer, proto, options) {
|
|
402
|
-
super();
|
|
403
|
-
this.proto = proto;
|
|
404
|
-
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
405
|
-
if (options?.name)
|
|
406
|
-
this.name = options.name;
|
|
407
|
-
this.init(buffer);
|
|
408
|
-
}
|
|
409
|
-
/**
|
|
410
|
-
* @return {PeernetHash}
|
|
411
|
-
*/
|
|
412
|
-
get peernetHash() {
|
|
413
|
-
return new CodecHash$1(this.decoded, { name: this.name });
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* @return {peernetHash}
|
|
417
|
-
*/
|
|
418
|
-
async hash() {
|
|
419
|
-
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
420
|
-
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
421
|
-
return (await this.peernetHash)[`to${format}`]();
|
|
422
|
-
}
|
|
423
|
-
fromUint8Array(buffer) {
|
|
424
|
-
this.encoded = buffer;
|
|
425
|
-
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
426
|
-
}
|
|
427
|
-
fromArrayBuffer(buffer) {
|
|
428
|
-
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
429
|
-
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* @param {Object} data
|
|
433
|
-
*/
|
|
434
|
-
create(data) {
|
|
435
|
-
const decoded = {};
|
|
436
|
-
if (this.keys?.length > 0) {
|
|
437
|
-
for (const key of this.keys) {
|
|
438
|
-
Object.defineProperties(decoded, {
|
|
439
|
-
[key]: {
|
|
440
|
-
enumerable: true,
|
|
441
|
-
configurable: true,
|
|
442
|
-
set: (value) => data[key],
|
|
443
|
-
get: () => data[key]
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
this.decoded = decoded;
|
|
448
|
-
return this.encode(decoded);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
330
|
+
let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
331
|
+
hashFormat;
|
|
332
|
+
init(buffer) {
|
|
333
|
+
if (buffer instanceof Uint8Array)
|
|
334
|
+
this.fromUint8Array(buffer);
|
|
335
|
+
else if (buffer instanceof ArrayBuffer)
|
|
336
|
+
this.fromArrayBuffer(buffer);
|
|
337
|
+
else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
|
|
338
|
+
return buffer;
|
|
339
|
+
else if (typeof buffer === 'string') {
|
|
340
|
+
if (this.isHex(buffer))
|
|
341
|
+
this.fromHex(buffer);
|
|
342
|
+
else if (this.isBase58(buffer))
|
|
343
|
+
this.fromBs58(buffer);
|
|
344
|
+
else if (this.isBase32(buffer))
|
|
345
|
+
this.fromBs32(buffer);
|
|
346
|
+
else
|
|
347
|
+
this.fromString(buffer);
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
this.create(buffer);
|
|
351
|
+
}
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
hasCodec() {
|
|
355
|
+
if (!this.encoded)
|
|
356
|
+
return false;
|
|
357
|
+
const codec = new Codec$1(this.encoded);
|
|
358
|
+
if (codec.name)
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
decode(encoded) {
|
|
362
|
+
encoded = encoded || this.encoded;
|
|
363
|
+
const codec = new Codec$1(this.encoded);
|
|
364
|
+
if (codec.codecBuffer) {
|
|
365
|
+
encoded = encoded.slice(codec.codecBuffer.length);
|
|
366
|
+
this.name = codec.name;
|
|
367
|
+
this.decoded = this.protoDecode(encoded);
|
|
368
|
+
// try {
|
|
369
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
370
|
+
// } catch {
|
|
371
|
+
// }
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
throw new Error(`no codec found`);
|
|
375
|
+
}
|
|
376
|
+
return this.decoded;
|
|
377
|
+
}
|
|
378
|
+
encode(decoded) {
|
|
379
|
+
let encoded;
|
|
380
|
+
if (!decoded)
|
|
381
|
+
decoded = this.decoded;
|
|
382
|
+
const codec = new Codec$1(this.name);
|
|
383
|
+
if (decoded instanceof Uint8Array)
|
|
384
|
+
encoded = decoded;
|
|
385
|
+
else
|
|
386
|
+
encoded = this.protoEncode(decoded);
|
|
387
|
+
if (codec.codecBuffer) {
|
|
388
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
389
|
+
uint8Array.set(codec.codecBuffer);
|
|
390
|
+
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
391
|
+
this.encoded = uint8Array;
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
throw new Error(`invalid codec`);
|
|
395
|
+
}
|
|
396
|
+
return this.encoded;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
400
|
+
* @param {Object} proto - {protoObject}
|
|
401
|
+
* @param {Object} options - {hashFormat, name}
|
|
402
|
+
*/
|
|
403
|
+
constructor(buffer, proto, options) {
|
|
404
|
+
super();
|
|
405
|
+
this.proto = proto;
|
|
406
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
407
|
+
if (options?.name)
|
|
408
|
+
this.name = options.name;
|
|
409
|
+
this.init(buffer);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* @return {PeernetHash}
|
|
413
|
+
*/
|
|
414
|
+
get peernetHash() {
|
|
415
|
+
return new CodecHash$1(this.decoded, { name: this.name });
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* @return {peernetHash}
|
|
419
|
+
*/
|
|
420
|
+
async hash() {
|
|
421
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
422
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
423
|
+
return (await this.peernetHash)[`to${format}`]();
|
|
424
|
+
}
|
|
425
|
+
fromUint8Array(buffer) {
|
|
426
|
+
this.encoded = buffer;
|
|
427
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
428
|
+
}
|
|
429
|
+
fromArrayBuffer(buffer) {
|
|
430
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
431
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* @param {Object} data
|
|
435
|
+
*/
|
|
436
|
+
create(data) {
|
|
437
|
+
const decoded = {};
|
|
438
|
+
if (this.keys?.length > 0) {
|
|
439
|
+
for (const key of this.keys) {
|
|
440
|
+
Object.defineProperties(decoded, {
|
|
441
|
+
[key]: {
|
|
442
|
+
enumerable: true,
|
|
443
|
+
configurable: true,
|
|
444
|
+
set: (value) => data[key],
|
|
445
|
+
get: () => data[key]
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
this.decoded = decoded;
|
|
450
|
+
return this.encode(decoded);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
451
453
|
};
|
|
452
454
|
|
|
453
|
-
const BasicInterface = BasicInterface$1;
|
|
454
|
-
const FormatInterface = FormatInterface$1;
|
|
455
|
-
const CodecHash = CodecHash$1;
|
|
455
|
+
const BasicInterface = BasicInterface$1;
|
|
456
|
+
const FormatInterface = FormatInterface$1;
|
|
457
|
+
const CodecHash = CodecHash$1;
|
|
456
458
|
const Codec = Codec$1;
|
|
457
459
|
|
|
458
460
|
export { BasicInterface, Codec, CodecHash, FormatInterface };
|