@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 +528 -457
- package/dist/module.js +4778 -424
- package/package.json +33 -47
- package/dist/browser.js +0 -4228
- package/dist/browser.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/module.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,488 +1,559 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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 $
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
*/
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
*/
|
|
75
|
+
fromEncoded(encoded) {
|
|
76
|
+
this.encoded = encoded;
|
|
77
|
+
return this.handleDecode()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
74
81
|
* @param {String} encoded
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
*/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
*/
|
|
96
|
+
toHex() {
|
|
97
|
+
return this.toString('hex')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
89
101
|
* @return {String} encoded
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
*/
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
//
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
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
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if (
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
-
*/
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
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
|
-
*/
|
|
426
|
-
|
|
427
|
-
|
|
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
|
-
*/
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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
|
-
*/
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
-
*/
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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
|
-
|
|
482
|
-
|
|
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;
|