@leofcoin/codec-format-interface 1.6.18 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/exports/basic-interface.d.ts +1 -1
- package/exports/basic-interface.js +6 -3
- package/exports/codec-a830f1a0.js +284 -0
- package/exports/codec-format-interface-07c55dde.js +268 -0
- package/exports/codec-format-interface-31a2dd5c.js +268 -0
- package/exports/codec-format-interface.d.ts +2 -0
- package/exports/codec-format-interface.js +28 -7
- package/exports/codec-hash.d.ts +8 -14
- package/exports/codec-hash.js +45 -33
- package/exports/codec.js +16 -8
- package/exports/index.js +17 -8
- package/package.json +50 -46
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 vandeurenglenn
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 vandeurenglenn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
@@ -7,7 +7,7 @@ export default class BasicInterface {
|
|
|
7
7
|
set proto(value: object);
|
|
8
8
|
get proto(): object;
|
|
9
9
|
decode(encoded?: Uint8Array): Object;
|
|
10
|
-
encode(decoded?:
|
|
10
|
+
encode(decoded?: object): Uint8Array;
|
|
11
11
|
protoEncode(data: object): Uint8Array;
|
|
12
12
|
protoDecode(data: Uint8Array): object;
|
|
13
13
|
isHex(string: string): boolean;
|
|
@@ -2,8 +2,9 @@ import bs32 from '@vandeurenglenn/base32';
|
|
|
2
2
|
import base58 from '@vandeurenglenn/base58';
|
|
3
3
|
import isHex from '@vandeurenglenn/is-hex';
|
|
4
4
|
import proto from '@vandeurenglenn/proto-array';
|
|
5
|
-
import { fromBase58, fromHex, toBase32, toBase58
|
|
6
|
-
|
|
5
|
+
import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/typed-array-utils';
|
|
6
|
+
|
|
7
|
+
class BasicInterface {
|
|
7
8
|
encoded;
|
|
8
9
|
decoded;
|
|
9
10
|
keys;
|
|
@@ -77,7 +78,7 @@ export default class BasicInterface {
|
|
|
77
78
|
toHex() {
|
|
78
79
|
if (!this.encoded)
|
|
79
80
|
this.encode();
|
|
80
|
-
return toHex(this.encoded);
|
|
81
|
+
return toHex(this.encoded.toString().split(',').map(number => Number(number)));
|
|
81
82
|
}
|
|
82
83
|
/**
|
|
83
84
|
* @return {String} encoded
|
|
@@ -96,3 +97,5 @@ export default class BasicInterface {
|
|
|
96
97
|
return toBase58(this.encoded);
|
|
97
98
|
}
|
|
98
99
|
}
|
|
100
|
+
|
|
101
|
+
export { BasicInterface as default };
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import varint from 'varint';
|
|
2
|
+
import BasicInterface from './basic-interface.js';
|
|
3
|
+
|
|
4
|
+
const blockchainCodecs = [
|
|
5
|
+
{
|
|
6
|
+
name: 'leofcoin-block',
|
|
7
|
+
codec: '0x6c62',
|
|
8
|
+
hashAlg: 'dbl-keccak-512',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
name: 'leofcoin-tx',
|
|
12
|
+
codec: '0x6c74',
|
|
13
|
+
hashAlg: 'dbl-keccak-512',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
name: 'leofcoin-itx',
|
|
17
|
+
codec: '0x6c69',
|
|
18
|
+
hashAlg: 'keccak-512',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'leofcoin-pr',
|
|
22
|
+
codec: '0x6c70',
|
|
23
|
+
hashAlg: 'keccak-256',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'contract-message',
|
|
27
|
+
codec: '0x63636d',
|
|
28
|
+
hashAlg: 'keccak-256'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'transaction-message',
|
|
32
|
+
codec: '0x746d',
|
|
33
|
+
hashAlg: 'keccak-256'
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'raw-transaction-message',
|
|
37
|
+
codec: '0x772746d',
|
|
38
|
+
hashAlg: 'keccak-256'
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'block-message',
|
|
42
|
+
codec: '0x626d',
|
|
43
|
+
hashAlg: 'keccak-256'
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'bw-message',
|
|
47
|
+
codec: '0x62776d',
|
|
48
|
+
hashAlg: 'keccak-256'
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: 'bw-request-message',
|
|
52
|
+
codec: '0x6277726d',
|
|
53
|
+
hashAlg: 'keccak-256'
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'validator-message',
|
|
57
|
+
codec: '0x766d',
|
|
58
|
+
hashAlg: 'keccak-256'
|
|
59
|
+
}
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
const internalCodecs = [
|
|
63
|
+
{
|
|
64
|
+
name: 'disco-hash',
|
|
65
|
+
codec: '0x30',
|
|
66
|
+
hashAlg: 'dbl-keccak-256',
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'peernet-peer-response',
|
|
70
|
+
codec: '0x707072',
|
|
71
|
+
hashAlg: 'keccak-256',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'peernet-peer',
|
|
75
|
+
codec: '0x7070',
|
|
76
|
+
hashAlg: 'keccak-256',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: 'peernet-dht',
|
|
80
|
+
codec: '0x706468',
|
|
81
|
+
hashAlg: 'keccak-256',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'peernet-dht-response',
|
|
85
|
+
codec: '0x706472',
|
|
86
|
+
hashAlg: 'keccak-256',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'peernet-data',
|
|
90
|
+
codec: '0x706461',
|
|
91
|
+
hashAlg: 'keccak-256',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'peernet-data-response',
|
|
95
|
+
codec: '0x70646172',
|
|
96
|
+
hashAlg: 'keccak-256',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: 'peernet-message',
|
|
100
|
+
codec: '0x706d65',
|
|
101
|
+
hashAlg: 'keccak-256',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'peernet-ps',
|
|
105
|
+
codec: '707073',
|
|
106
|
+
hashAlg: 'keccak-256',
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'peernet-response',
|
|
110
|
+
codec: '0x7072',
|
|
111
|
+
hashAlg: 'keccak-256',
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
name: 'peernet-request',
|
|
115
|
+
codec: '0x707271',
|
|
116
|
+
hashAlg: 'keccak-256',
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: 'peernet-file',
|
|
120
|
+
codec: '0x7066',
|
|
121
|
+
hashAlg: 'keccak-256',
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'peernet-file-response',
|
|
125
|
+
codec: '0x706672',
|
|
126
|
+
hashAlg: 'keccak-256',
|
|
127
|
+
}
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
var social = [{
|
|
131
|
+
name: 'chat-message',
|
|
132
|
+
codec: '0x70636d',
|
|
133
|
+
hashAlg: 'dbl-keccak-256',
|
|
134
|
+
}];
|
|
135
|
+
|
|
136
|
+
const codecs = [
|
|
137
|
+
...internalCodecs,
|
|
138
|
+
...blockchainCodecs,
|
|
139
|
+
...social
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
globalThis.peernetCodecs = globalThis.peernetCodecs || {};
|
|
143
|
+
const addCodec = (codecInput) => {
|
|
144
|
+
let { hashAlg, codec, name } = codecInput;
|
|
145
|
+
if (!globalThis.peernetCodecs[name])
|
|
146
|
+
globalThis.peernetCodecs[name] = {
|
|
147
|
+
hashAlg,
|
|
148
|
+
codec: typeof codec === 'string' ? parseInt(codec, 16) : codec
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
const getCodec = (name) => {
|
|
152
|
+
if (typeof name === 'number')
|
|
153
|
+
return name;
|
|
154
|
+
return getCodecByName(name).codec;
|
|
155
|
+
};
|
|
156
|
+
const getCodecName = (codec) => {
|
|
157
|
+
return Object.keys(globalThis.peernetCodecs).reduce((p, c) => {
|
|
158
|
+
const item = globalThis.peernetCodecs[c];
|
|
159
|
+
if (item.codec === codec)
|
|
160
|
+
return c;
|
|
161
|
+
else
|
|
162
|
+
return p;
|
|
163
|
+
}, undefined);
|
|
164
|
+
};
|
|
165
|
+
const getCodecByName = (name) => globalThis.peernetCodecs[name];
|
|
166
|
+
const getHashAlg = (name) => {
|
|
167
|
+
if (typeof name === 'number')
|
|
168
|
+
return getCodecByName(getCodecName(name)).hashAlg;
|
|
169
|
+
return getCodecByName(name).hashAlg;
|
|
170
|
+
};
|
|
171
|
+
const isCodec = (codec) => {
|
|
172
|
+
if (codec.codec !== undefined && codec.hashAlg)
|
|
173
|
+
return true;
|
|
174
|
+
return false;
|
|
175
|
+
};
|
|
176
|
+
const validateCodec = (codec) => {
|
|
177
|
+
if (codec.codec === undefined ||
|
|
178
|
+
codec.hashAlg === undefined ||
|
|
179
|
+
codec.name === undefined)
|
|
180
|
+
throw new Error(`invalid codecInput: ${codec}`);
|
|
181
|
+
};
|
|
182
|
+
for (const codec of codecs) {
|
|
183
|
+
addCodec(codec);
|
|
184
|
+
}
|
|
185
|
+
var utils = {
|
|
186
|
+
isCodec,
|
|
187
|
+
addCodec,
|
|
188
|
+
getCodec,
|
|
189
|
+
getHashAlg,
|
|
190
|
+
getCodecName,
|
|
191
|
+
validateCodec,
|
|
192
|
+
codecs: globalThis.peernetCodecs
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
class Codec extends BasicInterface {
|
|
196
|
+
codecBuffer;
|
|
197
|
+
codec;
|
|
198
|
+
hashAlg;
|
|
199
|
+
constructor(buffer) {
|
|
200
|
+
super();
|
|
201
|
+
if (buffer) {
|
|
202
|
+
if (buffer instanceof Uint8Array) {
|
|
203
|
+
const codec = varint.decode(buffer);
|
|
204
|
+
const name = this.getCodecName(codec);
|
|
205
|
+
if (name) {
|
|
206
|
+
this.name = name;
|
|
207
|
+
this.encoded = buffer;
|
|
208
|
+
this.decode(buffer);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
this.encode(Number(new TextDecoder().decode(buffer)));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
215
|
+
const codec = varint.decode(buffer);
|
|
216
|
+
const name = this.getCodecName(codec);
|
|
217
|
+
if (name) {
|
|
218
|
+
this.name = name;
|
|
219
|
+
this.decode(buffer);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
else if (typeof buffer === 'string') {
|
|
226
|
+
if (utils.getCodec(buffer))
|
|
227
|
+
this.fromName(buffer);
|
|
228
|
+
else if (this.isHex(buffer))
|
|
229
|
+
this.fromHex(buffer);
|
|
230
|
+
else if (this.isBase32(buffer))
|
|
231
|
+
this.fromBs32(buffer);
|
|
232
|
+
else if (this.isBase58(buffer))
|
|
233
|
+
this.fromBs58(buffer);
|
|
234
|
+
else
|
|
235
|
+
this.fromString(buffer);
|
|
236
|
+
}
|
|
237
|
+
if (!isNaN(buffer))
|
|
238
|
+
if (utils.getCodec(buffer))
|
|
239
|
+
this.fromCodec(buffer);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
fromEncoded(encoded) {
|
|
243
|
+
const codec = varint.decode(encoded);
|
|
244
|
+
const name = this.getCodecName(codec);
|
|
245
|
+
this.name = name;
|
|
246
|
+
this.encoded = encoded;
|
|
247
|
+
return this.decode(encoded);
|
|
248
|
+
}
|
|
249
|
+
getCodec(name) {
|
|
250
|
+
return utils.getCodec(name);
|
|
251
|
+
}
|
|
252
|
+
getCodecName(codec) {
|
|
253
|
+
return utils.getCodecName(codec);
|
|
254
|
+
}
|
|
255
|
+
getHashAlg(name) {
|
|
256
|
+
return utils.getHashAlg(name);
|
|
257
|
+
}
|
|
258
|
+
fromCodec(codec) {
|
|
259
|
+
this.name = this.getCodecName(codec);
|
|
260
|
+
this.hashAlg = this.getHashAlg(this.name);
|
|
261
|
+
this.codec = this.getCodec(this.name);
|
|
262
|
+
this.codecBuffer = varint.encode(this.codec);
|
|
263
|
+
}
|
|
264
|
+
fromName(name) {
|
|
265
|
+
const codec = this.getCodec(name);
|
|
266
|
+
this.name = name;
|
|
267
|
+
this.codec = codec;
|
|
268
|
+
this.hashAlg = this.getHashAlg(name);
|
|
269
|
+
this.codecBuffer = varint.encode(this.codec);
|
|
270
|
+
}
|
|
271
|
+
decode(encoded) {
|
|
272
|
+
encoded = encoded || this.encoded;
|
|
273
|
+
const codec = varint.decode(encoded);
|
|
274
|
+
this.fromCodec(codec);
|
|
275
|
+
return this.decoded;
|
|
276
|
+
}
|
|
277
|
+
encode(codec) {
|
|
278
|
+
codec = codec || this.codec;
|
|
279
|
+
this.encoded = varint.encode(codec);
|
|
280
|
+
return this.encoded;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export { Codec as C, codecs as c };
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import BasicInterface from './basic-interface.js';
|
|
2
|
+
import { createKeccak } from 'hash-wasm';
|
|
3
|
+
import varint from 'varint';
|
|
4
|
+
import { C as Codec } from './codec-a830f1a0.js';
|
|
5
|
+
|
|
6
|
+
class CodecHash extends BasicInterface {
|
|
7
|
+
constructor(buffer, options = {}) {
|
|
8
|
+
super();
|
|
9
|
+
if (options.name) this.name = options.name;
|
|
10
|
+
else this.name = 'disco-hash';
|
|
11
|
+
if (options.codecs) this.codecs = options.codecs;
|
|
12
|
+
return this.init(buffer)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async init(uint8Array) {
|
|
16
|
+
if (uint8Array) {
|
|
17
|
+
if (uint8Array instanceof Uint8Array) {
|
|
18
|
+
this.discoCodec = new Codec(uint8Array, this.codecs);
|
|
19
|
+
const name = this.discoCodec.name;
|
|
20
|
+
|
|
21
|
+
if (name) {
|
|
22
|
+
this.name = name;
|
|
23
|
+
this.decode(uint8Array);
|
|
24
|
+
} else {
|
|
25
|
+
await this.encode(uint8Array);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (typeof uint8Array === 'string') {
|
|
30
|
+
if (this.isHex(uint8Array)) await this.fromHex(uint8Array);
|
|
31
|
+
if (this.isBase32(uint8Array)) await this.fromBs32(uint8Array);
|
|
32
|
+
else if (this.isBase58(uint8Array)) await this.fromBs58(uint8Array);
|
|
33
|
+
else throw new Error(`unsupported string ${uint8Array}`)
|
|
34
|
+
} else if (typeof uint8Array === 'object') await this.fromJSON(uint8Array);
|
|
35
|
+
}
|
|
36
|
+
return this
|
|
37
|
+
}
|
|
38
|
+
get prefix() {
|
|
39
|
+
const length = this.length;
|
|
40
|
+
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
41
|
+
uint8Array.set(length);
|
|
42
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
43
|
+
|
|
44
|
+
return uint8Array
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get length() {
|
|
48
|
+
return varint.encode(this.size)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get buffer() {
|
|
52
|
+
return this.encoded
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get hash() {
|
|
56
|
+
return this.encoded
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fromJSON(json) {
|
|
60
|
+
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async encode(buffer, name) {
|
|
64
|
+
if (!this.name && name) this.name = name;
|
|
65
|
+
if (!buffer) buffer = this.buffer;
|
|
66
|
+
this.discoCodec = new Codec(this.name, this.codecs);
|
|
67
|
+
this.discoCodec.fromName(this.name);
|
|
68
|
+
let hashAlg = this.discoCodec.hashAlg;
|
|
69
|
+
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
70
|
+
|
|
71
|
+
if (hashAlg.includes('dbl')) {
|
|
72
|
+
hashAlg = hashAlg.replace('dbl-', '');
|
|
73
|
+
const hasher = await createKeccak(hashVariant);
|
|
74
|
+
await hasher.init();
|
|
75
|
+
hasher.update(buffer);
|
|
76
|
+
buffer = hasher.digest('binary');
|
|
77
|
+
}
|
|
78
|
+
const hasher = await createKeccak(hashVariant);
|
|
79
|
+
await hasher.init();
|
|
80
|
+
hasher.update(buffer);
|
|
81
|
+
this.digest = hasher.digest('binary');
|
|
82
|
+
this.size = this.digest.length;
|
|
83
|
+
|
|
84
|
+
this.codec = this.discoCodec.encode();
|
|
85
|
+
this.codec = this.discoCodec.codecBuffer;
|
|
86
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
87
|
+
uint8Array.set(this.prefix);
|
|
88
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
89
|
+
|
|
90
|
+
this.encoded = uint8Array;
|
|
91
|
+
|
|
92
|
+
return this.encoded
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async validate(buffer) {
|
|
96
|
+
if (Buffer.isBuffer(buffer)) {
|
|
97
|
+
const codec = varint.decode(buffer);
|
|
98
|
+
if (this.codecs[codec]) {
|
|
99
|
+
this.decode(buffer);
|
|
100
|
+
} else {
|
|
101
|
+
await this.encode(buffer);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (typeof buffer === 'string') {
|
|
105
|
+
if (this.isHex(buffer)) this.fromHex(buffer);
|
|
106
|
+
if (this.isBase32(buffer)) this.fromBs32(buffer);
|
|
107
|
+
}
|
|
108
|
+
if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
decode(buffer) {
|
|
112
|
+
this.encoded = buffer;
|
|
113
|
+
const codec = varint.decode(buffer);
|
|
114
|
+
|
|
115
|
+
this.discoCodec = new Codec(codec, this.codecs);
|
|
116
|
+
// TODO: validate codec
|
|
117
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
118
|
+
this.size = varint.decode(buffer);
|
|
119
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
120
|
+
if (this.digest.length !== this.size) {
|
|
121
|
+
throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
125
|
+
|
|
126
|
+
this.name = this.discoCodec.name;
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
this.size = this.digest.length;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
codec: this.codec,
|
|
133
|
+
name: this.name,
|
|
134
|
+
size: this.size,
|
|
135
|
+
length: this.length,
|
|
136
|
+
digest: this.digest,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class FormatInterface extends BasicInterface {
|
|
142
|
+
hashFormat;
|
|
143
|
+
init(buffer) {
|
|
144
|
+
if (buffer instanceof Uint8Array)
|
|
145
|
+
this.fromUint8Array(buffer);
|
|
146
|
+
else if (buffer instanceof ArrayBuffer)
|
|
147
|
+
this.fromArrayBuffer(buffer);
|
|
148
|
+
else if (buffer instanceof FormatInterface && buffer?.name === this.name)
|
|
149
|
+
return buffer;
|
|
150
|
+
else if (typeof buffer === 'string') {
|
|
151
|
+
if (this.isHex(buffer))
|
|
152
|
+
this.fromHex(buffer);
|
|
153
|
+
else if (this.isBase58(buffer))
|
|
154
|
+
this.fromBs58(buffer);
|
|
155
|
+
else if (this.isBase32(buffer))
|
|
156
|
+
this.fromBs32(buffer);
|
|
157
|
+
else
|
|
158
|
+
this.fromString(buffer);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
this.create(buffer);
|
|
162
|
+
}
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
hasCodec() {
|
|
166
|
+
if (!this.encoded)
|
|
167
|
+
return false;
|
|
168
|
+
const codec = new Codec(this.encoded);
|
|
169
|
+
if (codec.name)
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
decode(encoded) {
|
|
173
|
+
encoded = encoded || this.encoded;
|
|
174
|
+
const codec = new Codec(encoded);
|
|
175
|
+
if (codec.codecBuffer) {
|
|
176
|
+
encoded = encoded.slice(codec.codecBuffer.length);
|
|
177
|
+
this.name = codec.name;
|
|
178
|
+
this.decoded = this.protoDecode(encoded);
|
|
179
|
+
// try {
|
|
180
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
181
|
+
// } catch {
|
|
182
|
+
// }
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
throw new Error(`no codec found`);
|
|
186
|
+
}
|
|
187
|
+
return this.decoded;
|
|
188
|
+
}
|
|
189
|
+
encode(decoded) {
|
|
190
|
+
let encoded;
|
|
191
|
+
decoded = decoded || this.decoded;
|
|
192
|
+
const codec = new Codec(this.name);
|
|
193
|
+
if (decoded instanceof Uint8Array)
|
|
194
|
+
encoded = decoded;
|
|
195
|
+
else
|
|
196
|
+
encoded = this.protoEncode(decoded);
|
|
197
|
+
if (codec.codecBuffer) {
|
|
198
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
199
|
+
uint8Array.set(codec.codecBuffer);
|
|
200
|
+
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
201
|
+
this.encoded = uint8Array;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
throw new Error(`invalid codec`);
|
|
205
|
+
}
|
|
206
|
+
return this.encoded;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
210
|
+
* @param {Object} proto - {protoObject}
|
|
211
|
+
* @param {Object} options - {hashFormat, name}
|
|
212
|
+
*/
|
|
213
|
+
constructor(buffer, proto, options) {
|
|
214
|
+
super();
|
|
215
|
+
this.proto = proto;
|
|
216
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
217
|
+
if (options?.name)
|
|
218
|
+
this.name = options.name;
|
|
219
|
+
this.init(buffer);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* @return {PeernetHash}
|
|
223
|
+
*/
|
|
224
|
+
get peernetHash() {
|
|
225
|
+
const decoded = this.decoded;
|
|
226
|
+
// @ts-ignore
|
|
227
|
+
delete decoded.hash;
|
|
228
|
+
return new CodecHash(decoded, { name: this.name });
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* @return {peernetHash}
|
|
232
|
+
*/
|
|
233
|
+
async hash() {
|
|
234
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
235
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
236
|
+
return (await this.peernetHash)[`to${format}`]();
|
|
237
|
+
}
|
|
238
|
+
fromUint8Array(buffer) {
|
|
239
|
+
this.encoded = buffer;
|
|
240
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
241
|
+
}
|
|
242
|
+
fromArrayBuffer(buffer) {
|
|
243
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
244
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* @param {Object} data
|
|
248
|
+
*/
|
|
249
|
+
create(data) {
|
|
250
|
+
const decoded = {};
|
|
251
|
+
if (this.keys?.length > 0) {
|
|
252
|
+
for (const key of this.keys) {
|
|
253
|
+
Object.defineProperties(decoded, {
|
|
254
|
+
[key]: {
|
|
255
|
+
enumerable: true,
|
|
256
|
+
configurable: true,
|
|
257
|
+
set: (value) => data[key],
|
|
258
|
+
get: () => data[key]
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
this.decoded = decoded;
|
|
263
|
+
return this.encode(decoded);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export { CodecHash as C, FormatInterface as F };
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import BasicInterface from './basic-interface.js';
|
|
2
|
+
import { createKeccak } from 'hash-wasm';
|
|
3
|
+
import varint from 'varint';
|
|
4
|
+
import { C as Codec } from './codec-a830f1a0.js';
|
|
5
|
+
|
|
6
|
+
class CodecHash extends BasicInterface {
|
|
7
|
+
constructor(buffer, options = {}) {
|
|
8
|
+
super();
|
|
9
|
+
if (options.name) this.name = options.name;
|
|
10
|
+
else this.name = 'disco-hash';
|
|
11
|
+
if (options.codecs) this.codecs = options.codecs;
|
|
12
|
+
return this.init(buffer)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async init(uint8Array) {
|
|
16
|
+
if (uint8Array) {
|
|
17
|
+
if (uint8Array instanceof Uint8Array) {
|
|
18
|
+
this.discoCodec = new Codec(uint8Array, this.codecs);
|
|
19
|
+
const name = this.discoCodec.name;
|
|
20
|
+
|
|
21
|
+
if (name) {
|
|
22
|
+
this.name = name;
|
|
23
|
+
this.decode(uint8Array);
|
|
24
|
+
} else {
|
|
25
|
+
await this.encode(uint8Array);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (typeof uint8Array === 'string') {
|
|
30
|
+
if (this.isHex(uint8Array)) await this.fromHex(uint8Array);
|
|
31
|
+
if (this.isBase32(uint8Array)) await this.fromBs32(uint8Array);
|
|
32
|
+
else if (this.isBase58(uint8Array)) await this.fromBs58(uint8Array);
|
|
33
|
+
else throw new Error(`unsupported string ${uint8Array}`)
|
|
34
|
+
} else if (typeof uint8Array === 'object') await this.fromJSON(uint8Array);
|
|
35
|
+
}
|
|
36
|
+
return this
|
|
37
|
+
}
|
|
38
|
+
get prefix() {
|
|
39
|
+
const length = this.length;
|
|
40
|
+
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
41
|
+
uint8Array.set(length);
|
|
42
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
43
|
+
|
|
44
|
+
return uint8Array
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get length() {
|
|
48
|
+
return varint.encode(this.size)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get buffer() {
|
|
52
|
+
return this.encoded
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get hash() {
|
|
56
|
+
return this.encoded
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fromJSON(json) {
|
|
60
|
+
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async encode(buffer, name) {
|
|
64
|
+
if (!this.name && name) this.name = name;
|
|
65
|
+
if (!buffer) buffer = this.buffer;
|
|
66
|
+
this.discoCodec = new Codec(this.name, this.codecs);
|
|
67
|
+
this.discoCodec.fromName(this.name);
|
|
68
|
+
let hashAlg = this.discoCodec.hashAlg;
|
|
69
|
+
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
70
|
+
|
|
71
|
+
if (hashAlg.includes('dbl')) {
|
|
72
|
+
hashAlg = hashAlg.replace('dbl-', '');
|
|
73
|
+
const hasher = await createKeccak(hashVariant);
|
|
74
|
+
await hasher.init();
|
|
75
|
+
hasher.update(buffer);
|
|
76
|
+
buffer = hasher.digest('binary');
|
|
77
|
+
}
|
|
78
|
+
const hasher = await createKeccak(hashVariant);
|
|
79
|
+
await hasher.init();
|
|
80
|
+
hasher.update(buffer);
|
|
81
|
+
this.digest = hasher.digest('binary');
|
|
82
|
+
this.size = this.digest.length;
|
|
83
|
+
|
|
84
|
+
this.codec = this.discoCodec.encode();
|
|
85
|
+
this.codec = this.discoCodec.codecBuffer;
|
|
86
|
+
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
87
|
+
uint8Array.set(this.prefix);
|
|
88
|
+
uint8Array.set(this.digest, this.prefix.length);
|
|
89
|
+
|
|
90
|
+
this.encoded = uint8Array;
|
|
91
|
+
|
|
92
|
+
return this.encoded
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async validate(buffer) {
|
|
96
|
+
if (Buffer.isBuffer(buffer)) {
|
|
97
|
+
const codec = varint.decode(buffer);
|
|
98
|
+
if (this.codecs[codec]) {
|
|
99
|
+
this.decode(buffer);
|
|
100
|
+
} else {
|
|
101
|
+
await this.encode(buffer);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (typeof buffer === 'string') {
|
|
105
|
+
if (this.isHex(buffer)) this.fromHex(buffer);
|
|
106
|
+
if (this.isBase32(buffer)) this.fromBs32(buffer);
|
|
107
|
+
}
|
|
108
|
+
if (typeof buffer === 'object') this.fromJSON(buffer);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
decode(buffer) {
|
|
112
|
+
this.encoded = buffer;
|
|
113
|
+
const codec = varint.decode(buffer);
|
|
114
|
+
|
|
115
|
+
this.discoCodec = new Codec(codec, this.codecs);
|
|
116
|
+
// TODO: validate codec
|
|
117
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
118
|
+
this.size = varint.decode(buffer);
|
|
119
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
120
|
+
if (this.digest.length !== this.size) {
|
|
121
|
+
throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
125
|
+
|
|
126
|
+
this.name = this.discoCodec.name;
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
this.size = this.digest.length;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
codec: this.codec,
|
|
133
|
+
name: this.name,
|
|
134
|
+
size: this.size,
|
|
135
|
+
length: this.length,
|
|
136
|
+
digest: this.digest,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class FormatInterface extends BasicInterface {
|
|
142
|
+
hashFormat;
|
|
143
|
+
init(buffer) {
|
|
144
|
+
if (buffer instanceof Uint8Array)
|
|
145
|
+
this.fromUint8Array(buffer);
|
|
146
|
+
else if (buffer instanceof ArrayBuffer)
|
|
147
|
+
this.fromArrayBuffer(buffer);
|
|
148
|
+
else if (buffer instanceof FormatInterface && buffer?.name === this.name)
|
|
149
|
+
return buffer;
|
|
150
|
+
else if (typeof buffer === 'string') {
|
|
151
|
+
if (this.isHex(buffer))
|
|
152
|
+
this.fromHex(buffer);
|
|
153
|
+
else if (this.isBase58(buffer))
|
|
154
|
+
this.fromBs58(buffer);
|
|
155
|
+
else if (this.isBase32(buffer))
|
|
156
|
+
this.fromBs32(buffer);
|
|
157
|
+
else
|
|
158
|
+
this.fromString(buffer);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
this.create(buffer);
|
|
162
|
+
}
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
hasCodec() {
|
|
166
|
+
if (!this.encoded)
|
|
167
|
+
return false;
|
|
168
|
+
const codec = new Codec(this.encoded);
|
|
169
|
+
if (codec.name)
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
decode(encoded) {
|
|
173
|
+
encoded = encoded || this.encoded;
|
|
174
|
+
const codec = new Codec(encoded);
|
|
175
|
+
if (codec.codecBuffer) {
|
|
176
|
+
encoded = encoded.slice(codec.codecBuffer.length);
|
|
177
|
+
this.name = codec.name;
|
|
178
|
+
this.decoded = this.protoDecode(encoded);
|
|
179
|
+
// try {
|
|
180
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
181
|
+
// } catch {
|
|
182
|
+
// }
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
throw new Error(`no codec found`);
|
|
186
|
+
}
|
|
187
|
+
return this.decoded;
|
|
188
|
+
}
|
|
189
|
+
encode(decoded) {
|
|
190
|
+
let encoded;
|
|
191
|
+
decoded = decoded || this.decoded;
|
|
192
|
+
const codec = new Codec(this.name);
|
|
193
|
+
if (decoded instanceof Uint8Array)
|
|
194
|
+
encoded = decoded;
|
|
195
|
+
else
|
|
196
|
+
encoded = this.protoEncode(decoded);
|
|
197
|
+
if (codec.codecBuffer) {
|
|
198
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
199
|
+
uint8Array.set(codec.codecBuffer);
|
|
200
|
+
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
201
|
+
this.encoded = uint8Array;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
throw new Error(`invalid codec`);
|
|
205
|
+
}
|
|
206
|
+
return this.encoded;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
210
|
+
* @param {Object} proto - {protoObject}
|
|
211
|
+
* @param {Object} options - {hashFormat, name}
|
|
212
|
+
*/
|
|
213
|
+
constructor(buffer, proto, options) {
|
|
214
|
+
super();
|
|
215
|
+
this.proto = proto;
|
|
216
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
217
|
+
if (options?.name)
|
|
218
|
+
this.name = options.name;
|
|
219
|
+
this.init(buffer);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* @return {PeernetHash}
|
|
223
|
+
*/
|
|
224
|
+
get peernetHash() {
|
|
225
|
+
const decoded = this.decoded;
|
|
226
|
+
// @ts-ignore
|
|
227
|
+
delete decoded.hash;
|
|
228
|
+
return new CodecHash(decoded, { name: this.name });
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* @return {peernetHash}
|
|
232
|
+
*/
|
|
233
|
+
async hash() {
|
|
234
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
235
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
236
|
+
return (await this.peernetHash)[`to${format}`]();
|
|
237
|
+
}
|
|
238
|
+
fromUint8Array(buffer) {
|
|
239
|
+
this.encoded = buffer;
|
|
240
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
241
|
+
}
|
|
242
|
+
fromArrayBuffer(buffer) {
|
|
243
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
244
|
+
return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* @param {Object} data
|
|
248
|
+
*/
|
|
249
|
+
create(data) {
|
|
250
|
+
const decoded = {};
|
|
251
|
+
if (this.keys?.length > 0) {
|
|
252
|
+
for (const key of this.keys) {
|
|
253
|
+
Object.defineProperties(decoded, {
|
|
254
|
+
[key]: {
|
|
255
|
+
enumerable: true,
|
|
256
|
+
configurable: true,
|
|
257
|
+
set: (value) => data[key],
|
|
258
|
+
get: () => data[key]
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
this.decoded = decoded;
|
|
263
|
+
// return this.encode(decoded)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export { CodecHash as C, FormatInterface as F };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import BasicInterface from './basic-interface.js';
|
|
2
2
|
import Hash from './codec-hash.js';
|
|
3
3
|
export default class FormatInterface extends BasicInterface implements FormatInterface {
|
|
4
|
+
#private;
|
|
4
5
|
hashFormat: string;
|
|
5
6
|
init(buffer: Uint8Array | ArrayBuffer | FormatInterface | string): FormatInterface;
|
|
6
7
|
hasCodec(): boolean;
|
|
@@ -15,6 +16,7 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
15
16
|
hashFormat?: string;
|
|
16
17
|
name?: string;
|
|
17
18
|
});
|
|
19
|
+
get format(): string;
|
|
18
20
|
/**
|
|
19
21
|
* @return {PeernetHash}
|
|
20
22
|
*/
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import BasicInterface from './basic-interface.js';
|
|
2
|
-
import Hash from './codec-hash.js';
|
|
3
2
|
import Codec from './codec.js';
|
|
4
|
-
|
|
3
|
+
import CodecHash from './codec-hash.js';
|
|
4
|
+
import '@vandeurenglenn/base32';
|
|
5
|
+
import '@vandeurenglenn/base58';
|
|
6
|
+
import '@vandeurenglenn/is-hex';
|
|
7
|
+
import '@vandeurenglenn/proto-array';
|
|
8
|
+
import '@vandeurenglenn/typed-array-utils';
|
|
9
|
+
import 'varint';
|
|
10
|
+
import '@leofcoin/codecs';
|
|
11
|
+
|
|
12
|
+
class FormatInterface extends BasicInterface {
|
|
5
13
|
hashFormat;
|
|
14
|
+
#hash;
|
|
6
15
|
init(buffer) {
|
|
7
16
|
if (buffer instanceof Uint8Array)
|
|
8
17
|
this.fromUint8Array(buffer);
|
|
@@ -34,7 +43,7 @@ export default class FormatInterface extends BasicInterface {
|
|
|
34
43
|
}
|
|
35
44
|
decode(encoded) {
|
|
36
45
|
encoded = encoded || this.encoded;
|
|
37
|
-
const codec = new Codec(
|
|
46
|
+
const codec = new Codec(encoded);
|
|
38
47
|
if (codec.codecBuffer) {
|
|
39
48
|
encoded = encoded.slice(codec.codecBuffer.length);
|
|
40
49
|
this.name = codec.name;
|
|
@@ -51,8 +60,7 @@ export default class FormatInterface extends BasicInterface {
|
|
|
51
60
|
}
|
|
52
61
|
encode(decoded) {
|
|
53
62
|
let encoded;
|
|
54
|
-
|
|
55
|
-
decoded = this.decoded;
|
|
63
|
+
decoded = decoded || this.decoded;
|
|
56
64
|
const codec = new Codec(this.name);
|
|
57
65
|
if (decoded instanceof Uint8Array)
|
|
58
66
|
encoded = decoded;
|
|
@@ -82,16 +90,25 @@ export default class FormatInterface extends BasicInterface {
|
|
|
82
90
|
this.name = options.name;
|
|
83
91
|
this.init(buffer);
|
|
84
92
|
}
|
|
93
|
+
get format() {
|
|
94
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
95
|
+
return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
96
|
+
}
|
|
85
97
|
/**
|
|
86
98
|
* @return {PeernetHash}
|
|
87
99
|
*/
|
|
88
100
|
get peernetHash() {
|
|
89
|
-
|
|
101
|
+
const decoded = this.decoded;
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
delete decoded.hash;
|
|
104
|
+
return new CodecHash(decoded, { name: this.name });
|
|
90
105
|
}
|
|
91
106
|
/**
|
|
92
107
|
* @return {peernetHash}
|
|
93
108
|
*/
|
|
94
109
|
async hash() {
|
|
110
|
+
if (this.#hash)
|
|
111
|
+
return this.#hash;
|
|
95
112
|
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
96
113
|
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
97
114
|
return (await this.peernetHash)[`to${format}`]();
|
|
@@ -109,12 +126,16 @@ export default class FormatInterface extends BasicInterface {
|
|
|
109
126
|
*/
|
|
110
127
|
create(data) {
|
|
111
128
|
const decoded = {};
|
|
129
|
+
if (data.hash)
|
|
130
|
+
this.#hash = data.hash;
|
|
112
131
|
if (this.keys?.length > 0) {
|
|
113
132
|
for (const key of this.keys) {
|
|
114
133
|
decoded[key] = data[key];
|
|
115
134
|
}
|
|
116
135
|
this.decoded = decoded;
|
|
117
|
-
return this.encode(decoded)
|
|
136
|
+
// return this.encode(decoded)
|
|
118
137
|
}
|
|
119
138
|
}
|
|
120
139
|
}
|
|
140
|
+
|
|
141
|
+
export { FormatInterface as default };
|
package/exports/codec-hash.d.ts
CHANGED
|
@@ -1,27 +1,21 @@
|
|
|
1
1
|
import BasicInterface from './basic-interface.js';
|
|
2
|
-
import Codec from './codec.js';
|
|
3
2
|
export default class CodecHash extends BasicInterface {
|
|
4
|
-
codec:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
size: number;
|
|
8
|
-
constructor(buffer: any, options?: {
|
|
9
|
-
name: string;
|
|
10
|
-
codecs?: {};
|
|
11
|
-
});
|
|
3
|
+
codec: any;
|
|
4
|
+
discoCodec: any;
|
|
5
|
+
constructor(buffer: any, options?: {});
|
|
12
6
|
init(uint8Array: any): Promise<this>;
|
|
13
7
|
get prefix(): Uint8Array;
|
|
14
8
|
get length(): number[];
|
|
15
9
|
get buffer(): Uint8Array;
|
|
16
10
|
get hash(): Uint8Array;
|
|
17
11
|
fromJSON(json: any): Promise<Uint8Array>;
|
|
18
|
-
encode(buffer:
|
|
12
|
+
encode(buffer: any, name: any): Promise<Uint8Array>;
|
|
19
13
|
validate(buffer: any): Promise<void>;
|
|
20
|
-
decode(
|
|
21
|
-
codec:
|
|
14
|
+
decode(buffer: any): {
|
|
15
|
+
codec: any;
|
|
22
16
|
name: string;
|
|
23
|
-
size:
|
|
17
|
+
size: any;
|
|
24
18
|
length: number[];
|
|
25
|
-
digest:
|
|
19
|
+
digest: any;
|
|
26
20
|
};
|
|
27
21
|
}
|
package/exports/codec-hash.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { createKeccak } from 'hash-wasm';
|
|
2
1
|
import varint from 'varint';
|
|
3
2
|
import BasicInterface from './basic-interface.js';
|
|
4
3
|
import Codec from './codec.js';
|
|
5
|
-
|
|
4
|
+
import '@vandeurenglenn/base32';
|
|
5
|
+
import '@vandeurenglenn/base58';
|
|
6
|
+
import '@vandeurenglenn/is-hex';
|
|
7
|
+
import '@vandeurenglenn/proto-array';
|
|
8
|
+
import '@vandeurenglenn/typed-array-utils';
|
|
9
|
+
import '@leofcoin/codecs';
|
|
10
|
+
|
|
11
|
+
class CodecHash extends BasicInterface {
|
|
6
12
|
codec;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
size;
|
|
10
|
-
constructor(buffer, options) {
|
|
13
|
+
discoCodec;
|
|
14
|
+
constructor(buffer, options = {}) {
|
|
11
15
|
super();
|
|
12
16
|
if (options.name)
|
|
13
17
|
this.name = options.name;
|
|
@@ -15,14 +19,13 @@ export default class CodecHash extends BasicInterface {
|
|
|
15
19
|
this.name = 'disco-hash';
|
|
16
20
|
if (options.codecs)
|
|
17
21
|
this.codecs = options.codecs;
|
|
18
|
-
// @ts-ignore
|
|
19
22
|
return this.init(buffer);
|
|
20
23
|
}
|
|
21
24
|
async init(uint8Array) {
|
|
22
25
|
if (uint8Array) {
|
|
23
26
|
if (uint8Array instanceof Uint8Array) {
|
|
24
|
-
this.
|
|
25
|
-
const name = this.
|
|
27
|
+
this.discoCodec = new Codec(uint8Array, this.codecs);
|
|
28
|
+
const name = this.discoCodec.name;
|
|
26
29
|
if (name) {
|
|
27
30
|
this.name = name;
|
|
28
31
|
this.decode(uint8Array);
|
|
@@ -48,9 +51,9 @@ export default class CodecHash extends BasicInterface {
|
|
|
48
51
|
}
|
|
49
52
|
get prefix() {
|
|
50
53
|
const length = this.length;
|
|
51
|
-
const uint8Array = new Uint8Array(length.length + this.
|
|
54
|
+
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
|
|
52
55
|
uint8Array.set(length);
|
|
53
|
-
uint8Array.set(this.
|
|
56
|
+
uint8Array.set(this.discoCodec.codecBuffer, length.length);
|
|
54
57
|
return uint8Array;
|
|
55
58
|
}
|
|
56
59
|
get length() {
|
|
@@ -63,29 +66,36 @@ export default class CodecHash extends BasicInterface {
|
|
|
63
66
|
return this.encoded;
|
|
64
67
|
}
|
|
65
68
|
fromJSON(json) {
|
|
66
|
-
return this.encode(
|
|
69
|
+
return this.encode(Buffer.from(JSON.stringify(json)));
|
|
67
70
|
}
|
|
68
71
|
async encode(buffer, name) {
|
|
69
72
|
if (!this.name && name)
|
|
70
73
|
this.name = name;
|
|
71
74
|
if (!buffer)
|
|
72
75
|
buffer = this.buffer;
|
|
73
|
-
this.
|
|
74
|
-
this.
|
|
75
|
-
let hashAlg = this.
|
|
76
|
+
this.discoCodec = new Codec(this.name, this.codecs);
|
|
77
|
+
this.discoCodec.fromName(this.name);
|
|
78
|
+
let hashAlg = this.discoCodec.hashAlg;
|
|
76
79
|
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
|
|
77
80
|
if (hashAlg.includes('dbl')) {
|
|
78
81
|
hashAlg = hashAlg.replace('dbl-', '');
|
|
79
|
-
const hasher = await createKeccak(hashVariant)
|
|
80
|
-
await hasher.init()
|
|
81
|
-
hasher.update(buffer)
|
|
82
|
-
buffer = hasher.digest('binary')
|
|
82
|
+
// const hasher = await createKeccak(hashVariant)
|
|
83
|
+
// await hasher.init()
|
|
84
|
+
// hasher.update(buffer)
|
|
85
|
+
// buffer = hasher.digest('binary')
|
|
86
|
+
buffer = await crypto.subtle.digest(`SHA-${hashVariant}`, buffer);
|
|
87
|
+
}
|
|
88
|
+
// const hasher = await createKeccak(hashVariant)
|
|
89
|
+
// await hasher.init()
|
|
90
|
+
// hasher.update(buffer)
|
|
91
|
+
// this.digest = hasher.digest('binary')
|
|
92
|
+
this.digest = await crypto.subtle.digest(`SHA-${hashVariant}`, buffer);
|
|
93
|
+
if (this.digest instanceof ArrayBuffer) {
|
|
94
|
+
this.digest = new Uint8Array(this.digest);
|
|
83
95
|
}
|
|
84
|
-
const hasher = await createKeccak(hashVariant);
|
|
85
|
-
await hasher.init();
|
|
86
|
-
hasher.update(buffer);
|
|
87
|
-
this.digest = hasher.digest('binary');
|
|
88
96
|
this.size = this.digest.length;
|
|
97
|
+
this.codec = this.discoCodec.encode();
|
|
98
|
+
this.codec = this.discoCodec.codecBuffer;
|
|
89
99
|
const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
|
|
90
100
|
uint8Array.set(this.prefix);
|
|
91
101
|
uint8Array.set(this.digest, this.prefix.length);
|
|
@@ -111,19 +121,19 @@ export default class CodecHash extends BasicInterface {
|
|
|
111
121
|
if (typeof buffer === 'object')
|
|
112
122
|
this.fromJSON(buffer);
|
|
113
123
|
}
|
|
114
|
-
decode(
|
|
115
|
-
this.encoded =
|
|
116
|
-
const codec = varint.decode(
|
|
117
|
-
this.
|
|
124
|
+
decode(buffer) {
|
|
125
|
+
this.encoded = buffer;
|
|
126
|
+
const codec = varint.decode(buffer);
|
|
127
|
+
this.discoCodec = new Codec(codec, this.codecs);
|
|
118
128
|
// TODO: validate codec
|
|
119
|
-
|
|
120
|
-
this.size = varint.decode(
|
|
121
|
-
this.digest =
|
|
129
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
130
|
+
this.size = varint.decode(buffer);
|
|
131
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
122
132
|
if (this.digest.length !== this.size) {
|
|
123
|
-
throw new Error(`hash length inconsistent: ${this.encoded.toString()}`);
|
|
133
|
+
throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`);
|
|
124
134
|
}
|
|
125
|
-
// const
|
|
126
|
-
this.name = this.
|
|
135
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
136
|
+
this.name = this.discoCodec.name;
|
|
127
137
|
this.size = this.digest.length;
|
|
128
138
|
return {
|
|
129
139
|
codec: this.codec,
|
|
@@ -134,3 +144,5 @@ export default class CodecHash extends BasicInterface {
|
|
|
134
144
|
};
|
|
135
145
|
}
|
|
136
146
|
}
|
|
147
|
+
|
|
148
|
+
export { CodecHash as default };
|
package/exports/codec.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import varint from 'varint';
|
|
2
|
-
import { utils
|
|
2
|
+
import { utils } from '@leofcoin/codecs';
|
|
3
3
|
import BasicInterface from './basic-interface.js';
|
|
4
|
-
|
|
4
|
+
import '@vandeurenglenn/base32';
|
|
5
|
+
import '@vandeurenglenn/base58';
|
|
6
|
+
import '@vandeurenglenn/is-hex';
|
|
7
|
+
import '@vandeurenglenn/proto-array';
|
|
8
|
+
import '@vandeurenglenn/typed-array-utils';
|
|
9
|
+
|
|
10
|
+
class Codec extends BasicInterface {
|
|
5
11
|
codecBuffer;
|
|
6
12
|
codec;
|
|
7
13
|
hashAlg;
|
|
@@ -21,7 +27,7 @@ export default class Codec extends BasicInterface {
|
|
|
21
27
|
}
|
|
22
28
|
}
|
|
23
29
|
else if (buffer instanceof ArrayBuffer) {
|
|
24
|
-
const codec = varint.decode(
|
|
30
|
+
const codec = varint.decode(buffer);
|
|
25
31
|
const name = this.getCodecName(codec);
|
|
26
32
|
if (name) {
|
|
27
33
|
this.name = name;
|
|
@@ -32,7 +38,7 @@ export default class Codec extends BasicInterface {
|
|
|
32
38
|
}
|
|
33
39
|
}
|
|
34
40
|
else if (typeof buffer === 'string') {
|
|
35
|
-
if (
|
|
41
|
+
if (utils.getCodec(buffer))
|
|
36
42
|
this.fromName(buffer);
|
|
37
43
|
else if (this.isHex(buffer))
|
|
38
44
|
this.fromHex(buffer);
|
|
@@ -44,7 +50,7 @@ export default class Codec extends BasicInterface {
|
|
|
44
50
|
this.fromString(buffer);
|
|
45
51
|
}
|
|
46
52
|
if (!isNaN(buffer))
|
|
47
|
-
if (
|
|
53
|
+
if (utils.getCodec(buffer))
|
|
48
54
|
this.fromCodec(buffer);
|
|
49
55
|
}
|
|
50
56
|
}
|
|
@@ -56,13 +62,13 @@ export default class Codec extends BasicInterface {
|
|
|
56
62
|
return this.decode(encoded);
|
|
57
63
|
}
|
|
58
64
|
getCodec(name) {
|
|
59
|
-
return
|
|
65
|
+
return utils.getCodec(name);
|
|
60
66
|
}
|
|
61
67
|
getCodecName(codec) {
|
|
62
|
-
return
|
|
68
|
+
return utils.getCodecName(codec);
|
|
63
69
|
}
|
|
64
70
|
getHashAlg(name) {
|
|
65
|
-
return
|
|
71
|
+
return utils.getHashAlg(name);
|
|
66
72
|
}
|
|
67
73
|
fromCodec(codec) {
|
|
68
74
|
this.name = this.getCodecName(codec);
|
|
@@ -89,3 +95,5 @@ export default class Codec extends BasicInterface {
|
|
|
89
95
|
return this.encoded;
|
|
90
96
|
}
|
|
91
97
|
}
|
|
98
|
+
|
|
99
|
+
export { Codec as default };
|
package/exports/index.js
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import BasicInterface$1 from './basic-interface.js';
|
|
2
|
+
import FormatInterface$1 from './codec-format-interface.js';
|
|
3
|
+
import CodecHash$1 from './codec-hash.js';
|
|
4
|
+
import Codec$1 from './codec.js';
|
|
5
5
|
export { codecs } from '@leofcoin/codecs';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
import '@vandeurenglenn/base32';
|
|
7
|
+
import '@vandeurenglenn/base58';
|
|
8
|
+
import '@vandeurenglenn/is-hex';
|
|
9
|
+
import '@vandeurenglenn/proto-array';
|
|
10
|
+
import '@vandeurenglenn/typed-array-utils';
|
|
11
|
+
import 'varint';
|
|
12
|
+
|
|
13
|
+
const BasicInterface = BasicInterface$1;
|
|
14
|
+
const FormatInterface = FormatInterface$1;
|
|
15
|
+
const CodecHash = CodecHash$1;
|
|
16
|
+
const Codec = Codec$1;
|
|
17
|
+
|
|
18
|
+
export { BasicInterface, Codec, CodecHash, FormatInterface };
|
package/package.json
CHANGED
|
@@ -1,46 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@leofcoin/codec-format-interface",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"exports": {
|
|
7
|
-
".":
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"@
|
|
33
|
-
"@vandeurenglenn/
|
|
34
|
-
"@vandeurenglenn/
|
|
35
|
-
"@vandeurenglenn/
|
|
36
|
-
"@vandeurenglenn/
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"@
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@leofcoin/codec-format-interface",
|
|
3
|
+
"version": "1.7.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"exports"
|
|
11
|
+
],
|
|
12
|
+
"typings": "dist/index.d.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prepublish": "npm run build && npm version patch",
|
|
15
|
+
"build": "rollup -c",
|
|
16
|
+
"test": "node test"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/leofcoin/codec-format-interface.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [],
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/leofcoin/codec-format-interface/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/leofcoin/codec-format-interface#readme",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@leofcoin/codecs": "^1.0.0",
|
|
31
|
+
"@leofcoin/crypto": "^0.2.7",
|
|
32
|
+
"@vandeurenglenn/base32": "^1.1.0",
|
|
33
|
+
"@vandeurenglenn/base58": "^1.1.1",
|
|
34
|
+
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
35
|
+
"@vandeurenglenn/proto-array": "^1.0.0",
|
|
36
|
+
"@vandeurenglenn/typed-array-utils": "^1.1.0",
|
|
37
|
+
"hash-wasm": "^4.9.0",
|
|
38
|
+
"varint": "^6.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rollup/plugin-commonjs": "^23.0.3",
|
|
42
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
43
|
+
"@rollup/plugin-typescript": "^10.0.1",
|
|
44
|
+
"@types/varint": "^6.0.1",
|
|
45
|
+
"rollup": "^3.5.1",
|
|
46
|
+
"tape": "^5.5.3",
|
|
47
|
+
"tinybench": "^2.5.1",
|
|
48
|
+
"tslib": "^2.4.1"
|
|
49
|
+
}
|
|
50
|
+
}
|