@leofcoin/codec-format-interface 1.3.0 → 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/browser.js +4228 -0
- package/dist/browser.js.map +1 -0
- package/dist/index.js +457 -528
- package/dist/index.js.map +1 -0
- package/dist/module.js +424 -4778
- package/dist/module.js.map +1 -0
- package/package.json +16 -2
package/dist/index.js
CHANGED
|
@@ -1,559 +1,488 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
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
|
+
}
|
|
4
13
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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);
|
|
10
19
|
|
|
11
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
20
|
|
|
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);
|
|
18
21
|
|
|
19
|
-
class
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
/**
|
|
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
|
+
/**
|
|
39
41
|
* @param {String} encoded
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
/**
|
|
42
|
+
*/ fromBs32(encoded) {
|
|
43
|
+
this.encoded = (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase32))).decode(encoded);
|
|
44
|
+
return this.handleDecode();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
47
|
* @param {String} encoded
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.encoded = new Uint8Array([...array]);
|
|
69
|
-
return this.handleDecode()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
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
|
+
/**
|
|
73
68
|
* @param {Buffer} encoded
|
|
74
|
-
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
/**
|
|
69
|
+
*/ fromEncoded(encoded) {
|
|
70
|
+
this.encoded = encoded;
|
|
71
|
+
return this.handleDecode();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
81
74
|
* @param {String} encoded
|
|
82
|
-
*/
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
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
|
+
/**
|
|
94
84
|
* @return {String} encoded
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
/**
|
|
85
|
+
*/ toHex() {
|
|
86
|
+
return this.toString("hex");
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
101
89
|
* @return {String} encoded
|
|
102
|
-
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
/**
|
|
90
|
+
*/ async toBs32() {
|
|
91
|
+
if (!this.encoded) await this.handleEncode();
|
|
92
|
+
return (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase32))).encode(this.encoded);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
109
95
|
* @return {String} encoded
|
|
110
|
-
*/
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
/**
|
|
96
|
+
*/ async toBs58() {
|
|
97
|
+
if (!this.encoded) await this.handleEncode();
|
|
98
|
+
return (0, ($parcel$interopDefault($kmf1g$vandeurenglennbase58))).encode(this.encoded);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
117
101
|
* @param {Object} data
|
|
118
|
-
*/
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.decoded = decoded;
|
|
134
|
-
return this.encode()
|
|
135
|
-
}
|
|
136
|
-
}
|
|
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
|
+
}
|
|
137
117
|
}
|
|
138
118
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
hashAlg: 'keccak-256',
|
|
224
|
-
}
|
|
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
|
+
}
|
|
225
203
|
};
|
|
226
204
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
encode() {
|
|
312
|
-
const codec = varint__default["default"].encode(this.decoded);
|
|
313
|
-
this.encoded = codec;
|
|
314
|
-
return this.encoded
|
|
315
|
-
}
|
|
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
|
+
}
|
|
316
287
|
}
|
|
317
288
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
this.
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
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
|
-
}
|
|
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
|
+
}
|
|
439
388
|
}
|
|
440
389
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
}
|
|
465
|
-
return this
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/**
|
|
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
|
+
/**
|
|
469
413
|
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
470
414
|
* @param {Object} proto - {encode, decode}
|
|
471
415
|
* @param {Object} options - {hashFormat, name}
|
|
472
|
-
*/
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
/**
|
|
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
|
+
/**
|
|
482
424
|
* @return {PeernetHash}
|
|
483
|
-
*/
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
425
|
+
*/ get peernetHash() {
|
|
426
|
+
return new (0, $49a5bac9b2dfe7a8$export$2e2bcd8739ae039)(this.decoded, {
|
|
427
|
+
name: this.name
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
489
431
|
* @return {peernetHash}
|
|
490
|
-
*/
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
/**
|
|
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
|
+
/**
|
|
498
438
|
* @return {Object}
|
|
499
|
-
*/
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
return this.decoded
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
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
|
+
/**
|
|
515
451
|
* @return {Buffer}
|
|
516
|
-
*/
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
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
|
-
}
|
|
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
|
+
}
|
|
547
475
|
}
|
|
548
476
|
|
|
549
|
-
const BasicInterface = BasicInterface$1;
|
|
550
|
-
const FormatInterface = FormatInterface$1;
|
|
551
|
-
const CodecHash = CodecHash$1;
|
|
552
|
-
const Codec = PeernetCodec;
|
|
553
|
-
const Codecs = codecs;
|
|
554
477
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
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
|