@leofcoin/codec-format-interface 1.3.1 → 1.3.2

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