@leofcoin/codec-format-interface 1.2.7 → 1.3.1

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/dist/index.js CHANGED
@@ -1,578 +1,488 @@
1
- 'use strict';
1
+ var $kmf1g$vandeurenglennbase32 = require("@vandeurenglenn/base32");
2
+ var $kmf1g$vandeurenglennbase58 = require("@vandeurenglenn/base58");
3
+ var $kmf1g$vandeurenglennishex = require("@vandeurenglenn/is-hex");
4
+ var $kmf1g$varint = require("varint");
5
+ var $kmf1g$keccak = require("keccak");
2
6
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var bs32 = require('@vandeurenglenn/base32');
6
- var bs58 = require('@vandeurenglenn/base58');
7
- var isHex = require('@vandeurenglenn/is-hex');
8
- var varint = require('varint');
9
- var createKeccakHash = require('keccak');
7
+ function $parcel$export(e, n, v, s) {
8
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
9
+ }
10
+ function $parcel$interopDefault(a) {
11
+ return a && a.__esModule ? a.default : a;
12
+ }
10
13
 
11
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
+ $parcel$export(module.exports, "BasicInterface", () => $ddc88a2f9ee6b3c1$export$1b7dbb60aef3044);
15
+ $parcel$export(module.exports, "FormatInterface", () => $ddc88a2f9ee6b3c1$export$a3360ca1fc583f9e);
16
+ $parcel$export(module.exports, "CodecHash", () => $ddc88a2f9ee6b3c1$export$ea841ad44f0598a2);
17
+ $parcel$export(module.exports, "Codec", () => $ddc88a2f9ee6b3c1$export$a111e9ced3f1d2d0);
18
+ $parcel$export(module.exports, "Codecs", () => $ddc88a2f9ee6b3c1$export$1c4d583cbe1d2b10);
12
19
 
13
- function _interopNamespace(e) {
14
- if (e && e.__esModule) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () { return e[k]; }
23
- });
24
- }
25
- });
26
- }
27
- n["default"] = e;
28
- return Object.freeze(n);
29
- }
30
20
 
31
- var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
32
- var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
33
- var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
34
- var varint__default = /*#__PURE__*/_interopDefaultLegacy(varint);
35
- var createKeccakHash__default = /*#__PURE__*/_interopDefaultLegacy(createKeccakHash);
36
21
 
37
- class BasicInterface$1 {
38
- #handleDecode() {
39
- if (!this.decode) throw new Error('bad implementation: needs decode func')
40
- return this.decode()
41
- }
42
-
43
- #handleEncode() {
44
- if (!this.encode) throw new Error('bad implementation: needs encode func')
45
- return this.encode()
46
- }
47
- isHex(string) {
48
- return isHex__default["default"](string)
49
- }
50
- isBase32(string) {
51
- return bs32__default["default"].isBase32(string)
52
- }
53
- isBase58(string) {
54
- return bs58__default["default"].isBase58(string)
55
- }
56
- /**
22
+ class $78f9a5b9b57d161a$export$2e2bcd8739ae039 {
23
+ handleDecode() {
24
+ if (!this.decode) throw new Error("bad implementation: needs decode func");
25
+ return this.decode();
26
+ }
27
+ handleEncode() {
28
+ if (!this.encode) throw new Error("bad implementation: needs encode func");
29
+ return this.encode();
30
+ }
31
+ isHex(string) {
32
+ return (0, ($parcel$interopDefault($kmf1g$vandeurenglennishex)))(string);
33
+ }
34
+ isBase32(string) {
35
+ return (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase32))).isBase32(string);
36
+ }
37
+ isBase58(string) {
38
+ return (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase58))).isBase58(string);
39
+ }
40
+ /**
57
41
  * @param {String} encoded
58
- */
59
- fromBs32(encoded) {
60
- this.encoded = bs32__default["default"].decode(encoded);
61
- return this.#handleDecode()
62
- }
63
-
64
- /**
42
+ */ fromBs32(encoded) {
43
+ this.encoded = (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase32))).decode(encoded);
44
+ return this.handleDecode();
45
+ }
46
+ /**
65
47
  * @param {String} encoded
66
- */
67
- fromBs58(encoded) {
68
- this.encoded = bs58__default["default"].decode(encoded);
69
- return this.#handleDecode()
70
- }
71
-
72
- async toArray() {
73
- const array = [];
74
- for await (const value of this.encoded.values()) {
75
- array.push(value);
76
- }
77
- return array
78
- }
79
-
80
- fromString(string) {
81
- this.encoded = new Uint8Array(string.split(','));
82
- return this.#handleDecode()
83
- }
84
-
85
- fromArray(array) {
86
- this.encoded = new Uint8Array([...array]);
87
- return this.#handleDecode()
88
- }
89
-
90
- /**
48
+ */ fromBs58(encoded) {
49
+ this.encoded = (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase58))).decode(encoded);
50
+ return this.handleDecode();
51
+ }
52
+ async toArray() {
53
+ const array = [];
54
+ for await (const value1 of this.encoded.values())array.push(value1);
55
+ return array;
56
+ }
57
+ fromString(string) {
58
+ this.encoded = new Uint8Array(string.split(","));
59
+ return this.handleDecode();
60
+ }
61
+ fromArray(array) {
62
+ this.encoded = new Uint8Array([
63
+ ...array
64
+ ]);
65
+ return this.handleDecode();
66
+ }
67
+ /**
91
68
  * @param {Buffer} encoded
92
- */
93
- fromEncoded(encoded) {
94
- this.encoded = encoded;
95
- return this.#handleDecode()
96
- }
97
-
98
- /**
69
+ */ fromEncoded(encoded) {
70
+ this.encoded = encoded;
71
+ return this.handleDecode();
72
+ }
73
+ /**
99
74
  * @param {String} encoded
100
- */
101
- fromHex(encoded) {
102
- this.encoded = Buffer.from(encoded, 'hex');
103
- return this.#handleDecode()
104
- }
105
-
106
- async toString(encoding = 'utf8') {
107
- if (!this.encoded) await this.#handleEncode();
108
- return this.encoded.toString(encoding)
109
- }
110
-
111
- /**
75
+ */ fromHex(encoded) {
76
+ this.encoded = Buffer.from(encoded, "hex");
77
+ return this.handleDecode();
78
+ }
79
+ async toString(encoding = "utf8") {
80
+ if (!this.encoded) await this.handleEncode();
81
+ return this.encoded.toString(encoding);
82
+ }
83
+ /**
112
84
  * @return {String} encoded
113
- */
114
- toHex() {
115
- return this.toString('hex')
116
- }
117
-
118
- /**
85
+ */ toHex() {
86
+ return this.toString("hex");
87
+ }
88
+ /**
119
89
  * @return {String} encoded
120
- */
121
- async toBs32() {
122
- if (!this.encoded) await this.#handleEncode();
123
- return bs32__default["default"].encode(this.encoded)
124
- }
125
-
126
- /**
90
+ */ async toBs32() {
91
+ if (!this.encoded) await this.handleEncode();
92
+ return (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase32))).encode(this.encoded);
93
+ }
94
+ /**
127
95
  * @return {String} encoded
128
- */
129
- async toBs58() {
130
- if (!this.encoded) await this.#handleEncode();
131
- return bs58__default["default"].encode(this.encoded)
132
- }
133
-
134
- /**
96
+ */ async toBs58() {
97
+ if (!this.encoded) await this.handleEncode();
98
+ return (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase58))).encode(this.encoded);
99
+ }
100
+ /**
135
101
  * @param {Object} data
136
- */
137
- create(data) {
138
- const decoded = {};
139
- if (this.keys?.length > 0) {
140
- for (const key of this.keys) {
141
- Object.defineProperties(decoded, {
142
- [key]: {
143
- enumerable: true,
144
- configurable: true,
145
- set: (val) => value = data[key],
146
- get: () => data[key]
147
- }
148
- });
149
- }
150
-
151
- this.decoded = decoded;
152
- return this.encode()
153
- }
154
- }
102
+ */ create(data) {
103
+ const decoded = {};
104
+ if (this.keys?.length > 0) {
105
+ for (const key of this.keys)Object.defineProperties(decoded, {
106
+ [key]: {
107
+ enumerable: true,
108
+ configurable: true,
109
+ set: (val)=>value = data[key],
110
+ get: ()=>data[key]
111
+ }
112
+ });
113
+ this.decoded = decoded;
114
+ return this.encode();
115
+ }
116
+ }
155
117
  }
156
118
 
157
- var codecs = {
158
- // just a hash
159
- 'disco-hash': {
160
- codec: parseInt('30', 16),
161
- hashAlg: 'dbl-keccak-256', // ,
162
- // testnet: 'olivia'
163
- },
164
- 'peernet-peer-response': {
165
- codec: parseInt('707072', 16),
166
- hashAlg: 'keccak-256',
167
- },
168
- 'peernet-peer': {
169
- codec: parseInt('7070', 16),
170
- hashAlg: 'keccak-256',
171
- },
172
- 'peernet-dht': {
173
- codec: parseInt('706468', 16),
174
- hashAlg: 'keccak-256',
175
- },
176
- 'peernet-dht-response': {
177
- codec: parseInt('706472', 16),
178
- hashAlg: 'keccak-256',
179
- },
180
- // data
181
- 'peernet-data': {
182
- codec: parseInt('706461', 16),
183
- hashAlg: 'keccak-256',
184
- },
185
- 'peernet-data-response': {
186
- codec: parseInt('70646172', 16),
187
- hashAlg: 'keccak-256',
188
- },
189
- // message
190
- 'peernet-message': {
191
- codec: parseInt('706d65', 16),
192
- hashAlg: 'keccak-256',
193
- },
194
- // pubsub
195
- 'peernet-ps': {
196
- codec: parseInt('707073', 16),
197
- hashAlg: 'keccak-256',
198
- },
199
- 'peernet-response': {
200
- codec: parseInt('7072', 16),
201
- hashAlg: 'keccak-256',
202
- },
203
- 'peernet-request': {
204
- codec: parseInt('707271', 16),
205
- hashAlg: 'keccak-256',
206
- },
207
- // normal block
208
- 'leofcoin-block': {
209
- codec: parseInt('6c62', 16),
210
- hashAlg: 'dbl-keccak-512', // ,
211
- // testnet: 'olivia'
212
- },
213
- 'leofcoin-tx': {
214
- codec: parseInt('6c74', 16),
215
- hashAlg: 'dbl-keccak-512', // ,
216
- // testnet: 'olivia'
217
- },
218
- // itx
219
- 'leofcoin-itx': {
220
- codec: parseInt('6c69', 16),
221
- hashAlg: 'keccak-512', // ,
222
- // testnet: 'olivia'
223
- },
224
- // peer reputation
225
- 'leofcoin-pr': {
226
- codec: parseInt('6c70', 16),
227
- hashAlg: 'keccak-256', // ,
228
- // testnet: 'olivia'
229
- },
230
- // chat message
231
- 'chat-message': {
232
- codec: parseInt('636d', 16),
233
- hashAlg: 'dbl-keccak-256',
234
- },
235
- 'peernet-file' : {
236
- codec: parseInt('7066', 16),
237
- hashAlg: 'keccak-256',
238
- },
239
- 'peernet-file-response' : {
240
- codec: parseInt('706672', 16),
241
- hashAlg: 'keccak-256',
242
- }
119
+
120
+
121
+
122
+ var $e02c0eac1ab634b5$export$2e2bcd8739ae039 = {
123
+ // just a hash
124
+ "disco-hash": {
125
+ codec: parseInt("30", 16),
126
+ hashAlg: "dbl-keccak-256"
127
+ },
128
+ "peernet-peer-response": {
129
+ codec: parseInt("707072", 16),
130
+ hashAlg: "keccak-256"
131
+ },
132
+ "peernet-peer": {
133
+ codec: parseInt("7070", 16),
134
+ hashAlg: "keccak-256"
135
+ },
136
+ "peernet-dht": {
137
+ codec: parseInt("706468", 16),
138
+ hashAlg: "keccak-256"
139
+ },
140
+ "peernet-dht-response": {
141
+ codec: parseInt("706472", 16),
142
+ hashAlg: "keccak-256"
143
+ },
144
+ // data
145
+ "peernet-data": {
146
+ codec: parseInt("706461", 16),
147
+ hashAlg: "keccak-256"
148
+ },
149
+ "peernet-data-response": {
150
+ codec: parseInt("70646172", 16),
151
+ hashAlg: "keccak-256"
152
+ },
153
+ // message
154
+ "peernet-message": {
155
+ codec: parseInt("706d65", 16),
156
+ hashAlg: "keccak-256"
157
+ },
158
+ // pubsub
159
+ "peernet-ps": {
160
+ codec: parseInt("707073", 16),
161
+ hashAlg: "keccak-256"
162
+ },
163
+ "peernet-response": {
164
+ codec: parseInt("7072", 16),
165
+ hashAlg: "keccak-256"
166
+ },
167
+ "peernet-request": {
168
+ codec: parseInt("707271", 16),
169
+ hashAlg: "keccak-256"
170
+ },
171
+ // normal block
172
+ "leofcoin-block": {
173
+ codec: parseInt("6c62", 16),
174
+ hashAlg: "dbl-keccak-512"
175
+ },
176
+ "leofcoin-tx": {
177
+ codec: parseInt("6c74", 16),
178
+ hashAlg: "dbl-keccak-512"
179
+ },
180
+ // itx
181
+ "leofcoin-itx": {
182
+ codec: parseInt("6c69", 16),
183
+ hashAlg: "keccak-512"
184
+ },
185
+ // peer reputation
186
+ "leofcoin-pr": {
187
+ codec: parseInt("6c70", 16),
188
+ hashAlg: "keccak-256"
189
+ },
190
+ // chat message
191
+ "chat-message": {
192
+ codec: parseInt("636d", 16),
193
+ hashAlg: "dbl-keccak-256"
194
+ },
195
+ "peernet-file": {
196
+ codec: parseInt("7066", 16),
197
+ hashAlg: "keccak-256"
198
+ },
199
+ "peernet-file-response": {
200
+ codec: parseInt("706672", 16),
201
+ hashAlg: "keccak-256"
202
+ }
243
203
  };
244
204
 
245
- class PeernetCodec extends BasicInterface$1 {
246
- get codecs() {
247
- return {...globalThis.peernet.codecs, ...codecs}
248
- }
249
- constructor(buffer) {
250
- super();
251
- if (buffer) {
252
- if (buffer instanceof Uint8Array) {
253
- const codec = varint__default["default"].decode(buffer);
254
- const name = this.getCodecName(codec);
255
- if (name) {
256
- this.name = name;
257
- this.encoded = buffer;
258
- this.decode(buffer);
259
- } else {
260
- this.encode(buffer);
261
- }
262
- } else if (buffer instanceof ArrayBuffer) {
263
- const encoded = new Uint8Array(buffer.byteLength);
264
-
265
- for (let i = 0; i < buffer.byteLength; i++) {
266
- encoded[i] = buffer[i];
267
- }
268
- this.encoded = encoded;
269
- // this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
270
- this.decode(buffer);
271
- return
272
- }
273
- if (typeof buffer === 'string') {
274
- if (this.codecs[buffer]) this.fromName(buffer);
275
- else if (this.isHex(buffer)) this.fromHex(buffer);
276
- else if (this.isBase32(buffer)) this.fromBs32(buffer);
277
- else if (this.isBase58(buffer)) this.fromBs58(buffer);
278
- else throw new Error(`unsupported string ${buffer}`)
279
- }
280
- if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
281
- }
282
- }
283
-
284
- fromEncoded(encoded) {
285
- const codec = varint__default["default"].decode(encoded);
286
- const name = this.getCodecName(codec);
287
- this.name = name;
288
- this.encoded = encoded;
289
- this.decode(encoded);
290
- }
291
-
292
- getCodec(name) {
293
- return this.codecs[name].codec
294
- }
295
-
296
- getCodecName(codec) {
297
- return Object.keys(this.codecs).reduce((p, c) => {
298
- const item = this.codecs[c];
299
- if (item.codec === codec) return c;
300
- else return p;
301
- }, undefined)
302
- }
303
-
304
- getHashAlg(name) {
305
- return this.codecs[name].hashAlg
306
- }
307
-
308
- fromCodec(codec) {
309
- this.name = this.getCodecName(codec);
310
- this.hashAlg = this.getHashAlg(this.name);
311
-
312
- this.codec = this.getCodec(this.name);
313
- this.codecBuffer = varint__default["default"].encode(codec);
314
- }
315
-
316
- fromName(name) {
317
- const codec = this.getCodec(name);
318
- this.name = name;
319
- this.codec = codec;
320
- this.hashAlg = this.getHashAlg(name);
321
- this.codecBuffer = varint__default["default"].encode(codec);
322
- }
323
-
324
- decode() {
325
- const codec = varint__default["default"].decode(this.encoded);
326
- this.fromCodec(codec);
327
- }
328
-
329
- encode() {
330
- const codec = varint__default["default"].encode(this.decoded);
331
- this.encoded = codec;
332
- return this.encoded
333
- }
205
+
206
+
207
+ class $40c292043964c309$export$2e2bcd8739ae039 extends (0, $78f9a5b9b57d161a$export$2e2bcd8739ae039) {
208
+ get codecs() {
209
+ return {
210
+ ...globalThis.peernet.codecs,
211
+ ...(0, $e02c0eac1ab634b5$export$2e2bcd8739ae039)
212
+ };
213
+ }
214
+ constructor(buffer){
215
+ super();
216
+ if (buffer) {
217
+ if (buffer instanceof Uint8Array) {
218
+ const codec = (0, ($parcel$interopDefault($kmf1g$varint))).decode(buffer);
219
+ const name = this.getCodecName(codec);
220
+ if (name) {
221
+ this.name = name;
222
+ this.encoded = buffer;
223
+ this.decode(buffer);
224
+ } else this.encode(buffer);
225
+ } else if (buffer instanceof ArrayBuffer) {
226
+ const encoded = new Uint8Array(buffer.byteLength);
227
+ for(let i = 0; i < buffer.byteLength; i++)encoded[i] = buffer[i];
228
+ this.encoded = encoded;
229
+ // this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
230
+ this.decode(buffer);
231
+ return;
232
+ }
233
+ if (typeof buffer === "string") {
234
+ if (this.codecs[buffer]) this.fromName(buffer);
235
+ else if (this.isHex(buffer)) this.fromHex(buffer);
236
+ else if (this.isBase32(buffer)) this.fromBs32(buffer);
237
+ else if (this.isBase58(buffer)) this.fromBs58(buffer);
238
+ else throw new Error(`unsupported string ${buffer}`);
239
+ }
240
+ if (!isNaN(buffer)) {
241
+ if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
242
+ }
243
+ }
244
+ }
245
+ fromEncoded(encoded) {
246
+ const codec = (0, ($parcel$interopDefault($kmf1g$varint))).decode(encoded);
247
+ const name = this.getCodecName(codec);
248
+ this.name = name;
249
+ this.encoded = encoded;
250
+ this.decode(encoded);
251
+ }
252
+ getCodec(name) {
253
+ return this.codecs[name].codec;
254
+ }
255
+ getCodecName(codec) {
256
+ return Object.keys(this.codecs).reduce((p, c)=>{
257
+ const item = this.codecs[c];
258
+ if (item.codec === codec) return c;
259
+ else return p;
260
+ }, undefined);
261
+ }
262
+ getHashAlg(name) {
263
+ return this.codecs[name].hashAlg;
264
+ }
265
+ fromCodec(codec) {
266
+ this.name = this.getCodecName(codec);
267
+ this.hashAlg = this.getHashAlg(this.name);
268
+ this.codec = this.getCodec(this.name);
269
+ this.codecBuffer = (0, ($parcel$interopDefault($kmf1g$varint))).encode(codec);
270
+ }
271
+ fromName(name) {
272
+ const codec = this.getCodec(name);
273
+ this.name = name;
274
+ this.codec = codec;
275
+ this.hashAlg = this.getHashAlg(name);
276
+ this.codecBuffer = (0, ($parcel$interopDefault($kmf1g$varint))).encode(codec);
277
+ }
278
+ decode() {
279
+ const codec = (0, ($parcel$interopDefault($kmf1g$varint))).decode(this.encoded);
280
+ this.fromCodec(codec);
281
+ }
282
+ encode() {
283
+ const codec = (0, ($parcel$interopDefault($kmf1g$varint))).encode(this.decoded);
284
+ this.encoded = codec;
285
+ return this.encoded;
286
+ }
334
287
  }
335
288
 
336
- class CodecHash$1 extends BasicInterface$1 {
337
- constructor(buffer, options = {}) {
338
- super();
339
- if (options.name) this.name = options.name;
340
- else this.name = 'disco-hash';
341
- if (options.codecs) this.codecs = options.codecs;
342
- if (buffer) {
343
- if (buffer instanceof Uint8Array) {
344
- this.discoCodec = new PeernetCodec(buffer, this.codecs);
345
- const name = this.discoCodec.name;
346
-
347
- if (name) {
348
- this.name = name;
349
- this.decode(buffer);
350
- } else {
351
- this.encode(buffer);
352
- }
353
- }
354
-
355
- if (typeof buffer === 'string') {
356
- if (this.isHex(buffer)) this.fromHex(buffer);
357
- if (this.isBase32(buffer)) this.fromBs32(buffer);
358
- else if (this.isBase58(buffer)) this.fromBs58(buffer);
359
- else throw new Error(`unsupported string ${buffer}`)
360
- } else if (typeof buffer === 'object') this.fromJSON(buffer);
361
- }
362
- }
363
-
364
- get prefix() {
365
- const length = this.length;
366
- const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
367
- uint8Array.set(length);
368
- uint8Array.set(this.discoCodec.codecBuffer, length.length);
369
-
370
- return uint8Array
371
- }
372
-
373
- get length() {
374
- return varint__default["default"].encode(this.size)
375
- }
376
-
377
- get buffer() {
378
- return this.encoded
379
- }
380
-
381
- get hash() {
382
- return this.encoded
383
- }
384
-
385
- fromJSON(json) {
386
- return this.encode(Buffer.from(JSON.stringify(json)))
387
- }
388
-
389
- encode(buffer, name) {
390
- if (!this.name && name) this.name = name;
391
- if (!buffer) buffer = this.buffer;
392
- this.discoCodec = new PeernetCodec(this.name, this.codecs);
393
- this.discoCodec.fromName(this.name);
394
- let hashAlg = this.discoCodec.hashAlg;
395
- if (hashAlg.includes('dbl')) {
396
- hashAlg = hashAlg.replace('dbl-', '');
397
- buffer = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
398
- }
399
- this.digest = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
400
- this.size = this.digest.length;
401
-
402
- this.codec = this.discoCodec.encode();
403
- this.codec = this.discoCodec.codecBuffer;
404
- const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
405
- uint8Array.set(this.prefix);
406
- uint8Array.set(this.digest, this.prefix.length);
407
-
408
- this.encoded = uint8Array;
409
-
410
- return this.encoded
411
- }
412
-
413
- validate(buffer) {
414
- if (Buffer.isBuffer(buffer)) {
415
- const codec = varint__default["default"].decode(buffer);
416
- if (this.codecs[codec]) {
417
- this.decode(buffer);
418
- } else {
419
- this.encode(buffer);
420
- }
421
- }
422
- if (typeof buffer === 'string') {
423
- if (this.isHex(buffer)) this.fromHex(buffer);
424
- if (this.isBase32(buffer)) this.fromBs32(buffer);
425
- }
426
- if (typeof buffer === 'object') this.fromJSON(buffer);
427
- }
428
-
429
- decode(buffer) {
430
- this.encoded = buffer;
431
- const codec = varint__default["default"].decode(buffer);
432
-
433
- this.discoCodec = new PeernetCodec(codec, this.codecs);
434
- // TODO: validate codec
435
- buffer = buffer.slice(varint__default["default"].decode.bytes);
436
- this.size = varint__default["default"].decode(buffer);
437
- this.digest = buffer.slice(varint__default["default"].decode.bytes);
438
- if (this.digest.length !== this.size) {
439
- throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`)
440
- }
441
-
442
- // const discoCodec = new Codec(codec, this.codecs)
443
-
444
- this.name = this.discoCodec.name;
445
-
446
-
447
- this.size = this.digest.length;
448
-
449
- return {
450
- codec: this.codec,
451
- name: this.name,
452
- size: this.size,
453
- length: this.length,
454
- digest: this.digest,
455
- }
456
- }
289
+
290
+
291
+
292
+
293
+
294
+ class $49a5bac9b2dfe7a8$export$2e2bcd8739ae039 extends (0, $78f9a5b9b57d161a$export$2e2bcd8739ae039) {
295
+ constructor(buffer, options = {}){
296
+ super();
297
+ if (options.name) this.name = options.name;
298
+ else this.name = "disco-hash";
299
+ if (options.codecs) this.codecs = options.codecs;
300
+ if (buffer) {
301
+ if (buffer instanceof Uint8Array) {
302
+ this.discoCodec = new (0, $40c292043964c309$export$2e2bcd8739ae039)(buffer, this.codecs);
303
+ const name = this.discoCodec.name;
304
+ if (name) {
305
+ this.name = name;
306
+ this.decode(buffer);
307
+ } else this.encode(buffer);
308
+ }
309
+ if (typeof buffer === "string") {
310
+ if (this.isHex(buffer)) this.fromHex(buffer);
311
+ if (this.isBase32(buffer)) this.fromBs32(buffer);
312
+ else if (this.isBase58(buffer)) this.fromBs58(buffer);
313
+ else throw new Error(`unsupported string ${buffer}`);
314
+ } else if (typeof buffer === "object") this.fromJSON(buffer);
315
+ }
316
+ }
317
+ get prefix() {
318
+ const length = this.length;
319
+ const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
320
+ uint8Array.set(length);
321
+ uint8Array.set(this.discoCodec.codecBuffer, length.length);
322
+ return uint8Array;
323
+ }
324
+ get length() {
325
+ return (0, ($parcel$interopDefault($kmf1g$varint))).encode(this.size);
326
+ }
327
+ get buffer() {
328
+ return this.encoded;
329
+ }
330
+ get hash() {
331
+ return this.encoded;
332
+ }
333
+ fromJSON(json) {
334
+ return this.encode(Buffer.from(JSON.stringify(json)));
335
+ }
336
+ encode(buffer, name) {
337
+ if (!this.name && name) this.name = name;
338
+ if (!buffer) buffer = this.buffer;
339
+ this.discoCodec = new (0, $40c292043964c309$export$2e2bcd8739ae039)(this.name, this.codecs);
340
+ this.discoCodec.fromName(this.name);
341
+ let hashAlg = this.discoCodec.hashAlg;
342
+ if (hashAlg.includes("dbl")) {
343
+ hashAlg = hashAlg.replace("dbl-", "");
344
+ buffer = (0, ($parcel$interopDefault($kmf1g$keccak)))(hashAlg.replace("-", "")).update(buffer).digest();
345
+ }
346
+ this.digest = (0, ($parcel$interopDefault($kmf1g$keccak)))(hashAlg.replace("-", "")).update(buffer).digest();
347
+ this.size = this.digest.length;
348
+ this.codec = this.discoCodec.encode();
349
+ this.codec = this.discoCodec.codecBuffer;
350
+ const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
351
+ uint8Array.set(this.prefix);
352
+ uint8Array.set(this.digest, this.prefix.length);
353
+ this.encoded = uint8Array;
354
+ return this.encoded;
355
+ }
356
+ validate(buffer) {
357
+ if (Buffer.isBuffer(buffer)) {
358
+ const codec = (0, ($parcel$interopDefault($kmf1g$varint))).decode(buffer);
359
+ if (this.codecs[codec]) this.decode(buffer);
360
+ else this.encode(buffer);
361
+ }
362
+ if (typeof buffer === "string") {
363
+ if (this.isHex(buffer)) this.fromHex(buffer);
364
+ if (this.isBase32(buffer)) this.fromBs32(buffer);
365
+ }
366
+ if (typeof buffer === "object") this.fromJSON(buffer);
367
+ }
368
+ decode(buffer) {
369
+ this.encoded = buffer;
370
+ const codec = (0, ($parcel$interopDefault($kmf1g$varint))).decode(buffer);
371
+ this.discoCodec = new (0, $40c292043964c309$export$2e2bcd8739ae039)(codec, this.codecs);
372
+ // TODO: validate codec
373
+ buffer = buffer.slice((0, ($parcel$interopDefault($kmf1g$varint))).decode.bytes);
374
+ this.size = (0, ($parcel$interopDefault($kmf1g$varint))).decode(buffer);
375
+ this.digest = buffer.slice((0, ($parcel$interopDefault($kmf1g$varint))).decode.bytes);
376
+ if (this.digest.length !== this.size) throw new Error(`hash length inconsistent: 0x${this.encoded.toString("hex")}`);
377
+ // const discoCodec = new Codec(codec, this.codecs)
378
+ this.name = this.discoCodec.name;
379
+ this.size = this.digest.length;
380
+ return {
381
+ codec: this.codec,
382
+ name: this.name,
383
+ size: this.size,
384
+ length: this.length,
385
+ digest: this.digest
386
+ };
387
+ }
457
388
  }
458
389
 
459
- let protons;
460
-
461
- class FormatInterface$1 extends BasicInterface$1 {
462
- async #importProtons() {
463
- let importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "protons" */ 'protons')); });
464
- return importee.default
465
- }
466
-
467
- async protoEncode(data) {
468
- if (!protons) protons = await this.#importProtons();
469
- return protons(this.proto)[this.messageName].encode(data)
470
- }
471
-
472
- async protoDecode(data) {
473
- if (!protons) protons = await this.#importProtons();
474
- return protons(this.proto)[this.messageName].decode(data)
475
- }
476
-
477
- async #init(buffer) {
478
- if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer);
479
- else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer);
480
- else if (buffer?.name === this.name) return buffer
481
- else if (buffer instanceof String) {
482
- if (this.isHex(buffer)) await this.fromHex(buffer);
483
- else if (this.isBase32(buffer)) await this.fromBs32(buffer);
484
- else if (this.isBase58(buffer)) await this.fromBs58(buffer);
485
- else throw new Error(`unsupported string ${buffer}`)
486
- } else {
487
- await this.create(buffer);
488
- }
489
- return this
490
- }
491
-
492
- /**
390
+
391
+ class $ac9428d02c14dab1$export$2e2bcd8739ae039 extends (0, $78f9a5b9b57d161a$export$2e2bcd8739ae039) {
392
+ async protoEncode(data) {
393
+ // check schema
394
+ return new TextEncoder().encode(data);
395
+ }
396
+ async protoDecode(data) {
397
+ // check schema
398
+ return new TextDecoder().decode(data);
399
+ }
400
+ async init(buffer) {
401
+ if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer);
402
+ else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer);
403
+ else if (buffer?.name === this.name) return buffer;
404
+ else if (buffer instanceof String) {
405
+ if (this.isHex(buffer)) await this.fromHex(buffer);
406
+ else if (this.isBase32(buffer)) await this.fromBs32(buffer);
407
+ else if (this.isBase58(buffer)) await this.fromBs58(buffer);
408
+ else throw new Error(`unsupported string ${buffer}`);
409
+ } else await this.create(buffer);
410
+ return this;
411
+ }
412
+ /**
493
413
  * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
494
414
  * @param {Object} proto - {encode, decode}
495
415
  * @param {Object} options - {hashFormat, name}
496
- */
497
- constructor(buffer, proto, options = {}) {
498
- super();
499
- this.proto = proto;
500
- this.hashFormat = options.hashFormat || 'bs32';
501
- if (options.name) this.name = options.name;
502
- return this.#init(buffer)
503
- }
504
-
505
- /**
416
+ */ constructor(buffer, proto, options = {}){
417
+ super();
418
+ this.proto = proto;
419
+ this.hashFormat = options.hashFormat || "bs32";
420
+ if (options.name) this.name = options.name;
421
+ return this.init(buffer);
422
+ }
423
+ /**
506
424
  * @return {PeernetHash}
507
- */
508
- get peernetHash() {
509
- return new CodecHash$1(this.decoded, {name: this.name})
510
- }
511
-
512
- /**
425
+ */ get peernetHash() {
426
+ return new (0, $49a5bac9b2dfe7a8$export$2e2bcd8739ae039)(this.decoded, {
427
+ name: this.name
428
+ });
429
+ }
430
+ /**
513
431
  * @return {peernetHash}
514
- */
515
- get hash() {
516
- const upper = this.hashFormat.charAt(0).toUpperCase();
517
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
518
- return this.peernetHash[`to${format}`]()
519
- }
520
-
521
- /**
432
+ */ get hash() {
433
+ const upper = this.hashFormat.charAt(0).toUpperCase();
434
+ const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
435
+ return this.peernetHash[`to${format}`]();
436
+ }
437
+ /**
522
438
  * @return {Object}
523
- */
524
- async decode() {
525
- let encoded = this.encoded;
526
- const discoCodec = new PeernetCodec(this.encoded);
527
- encoded = encoded.slice(discoCodec.codecBuffer.length);
528
- this.name = discoCodec.name;
529
- this.decoded = await this.protoDecode(encoded);
530
- return this.decoded
531
- }
532
-
533
- /**
439
+ */ async decode() {
440
+ let encoded = this.encoded;
441
+ const discoCodec = new (0, $40c292043964c309$export$2e2bcd8739ae039)(this.encoded);
442
+ encoded = encoded.slice(discoCodec.codecBuffer.length);
443
+ this.name = discoCodec.name;
444
+ this.decoded = await this.protoDecode(encoded);
445
+ try {
446
+ this.decoded = JSON.parse(this.decoded);
447
+ } catch {}
448
+ return this.decoded;
449
+ }
450
+ /**
534
451
  * @return {Buffer}
535
- */
536
- async encode(decoded) {
537
- if (!decoded) decoded = this.decoded;
538
- const codec = new PeernetCodec(this.name);
539
- const encoded = await this.protoEncode(decoded);
540
- const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
541
- uint8Array.set(codec.codecBuffer);
542
- uint8Array.set(encoded, codec.codecBuffer.length);
543
- this.encoded = uint8Array;
544
- return this.encoded
545
- }
546
-
547
- hasCodec() {
548
- if (!this.encoded) return false
549
- const codec = new PeernetCodec(this.encoded);
550
- if (codec.name) return true
551
- }
552
-
553
- fromUint8Array(buffer) {
554
- this.encoded = buffer;
555
- return this.hasCodec() ? this.decode() : this.create(
556
- JSON.parse(new TextDecoder().decode(this.encoded))
557
- )
558
- }
559
-
560
- fromArrayBuffer(buffer) {
561
- this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
562
- return this.hasCodec() ? this.decode() : this.create(
563
- JSON.parse(new TextDecoder().decode(this.encoded))
564
- )
565
- }
452
+ */ async encode(decoded) {
453
+ if (!decoded) decoded = this.decoded;
454
+ const codec = new (0, $40c292043964c309$export$2e2bcd8739ae039)(this.name);
455
+ const encoded = await this.protoEncode(typeof decoded === "object" ? JSON.stringify(decoded) : decoded);
456
+ const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
457
+ uint8Array.set(codec.codecBuffer);
458
+ uint8Array.set(encoded, codec.codecBuffer.length);
459
+ this.encoded = uint8Array;
460
+ return this.encoded;
461
+ }
462
+ hasCodec() {
463
+ if (!this.encoded) return false;
464
+ const codec = new (0, $40c292043964c309$export$2e2bcd8739ae039)(this.encoded);
465
+ if (codec.name) return true;
466
+ }
467
+ fromUint8Array(buffer) {
468
+ this.encoded = buffer;
469
+ return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
470
+ }
471
+ fromArrayBuffer(buffer) {
472
+ this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
473
+ return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
474
+ }
566
475
  }
567
476
 
568
- const BasicInterface = BasicInterface$1;
569
- const FormatInterface = FormatInterface$1;
570
- const CodecHash = CodecHash$1;
571
- const Codec = PeernetCodec;
572
- const Codecs = codecs;
573
477
 
574
- exports.BasicInterface = BasicInterface;
575
- exports.Codec = Codec;
576
- exports.CodecHash = CodecHash;
577
- exports.Codecs = Codecs;
578
- exports.FormatInterface = FormatInterface;
478
+
479
+
480
+
481
+ const $ddc88a2f9ee6b3c1$export$1b7dbb60aef3044 = (0, $78f9a5b9b57d161a$export$2e2bcd8739ae039);
482
+ const $ddc88a2f9ee6b3c1$export$a3360ca1fc583f9e = (0, $ac9428d02c14dab1$export$2e2bcd8739ae039);
483
+ const $ddc88a2f9ee6b3c1$export$ea841ad44f0598a2 = (0, $49a5bac9b2dfe7a8$export$2e2bcd8739ae039);
484
+ const $ddc88a2f9ee6b3c1$export$a111e9ced3f1d2d0 = (0, $40c292043964c309$export$2e2bcd8739ae039);
485
+ const $ddc88a2f9ee6b3c1$export$1c4d583cbe1d2b10 = (0, $e02c0eac1ab634b5$export$2e2bcd8739ae039);
486
+
487
+
488
+ //# sourceMappingURL=index.js.map