@leofcoin/codec-format-interface 1.7.13 → 1.7.15
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/README.md +35 -10
- package/exports/README.md +8 -0
- package/exports/basic-interface.d.ts +4 -0
- package/exports/basic-interface.js +92 -14
- package/exports/basic-interface.js.map +1 -0
- package/exports/cjs/basic-interface.js +209 -0
- package/exports/cjs/basic-interface.js.map +1 -0
- package/exports/cjs/codec-format-interface.js +175 -0
- package/exports/cjs/codec-format-interface.js.map +1 -0
- package/exports/cjs/codec-hash.js +223 -0
- package/exports/cjs/codec-hash.js.map +1 -0
- package/exports/cjs/codec.js +114 -0
- package/exports/cjs/codec.js.map +1 -0
- package/exports/cjs/index.js +42 -0
- package/exports/cjs/index.js.map +1 -0
- package/exports/codec-format-interface.d.ts +1 -1
- package/exports/codec-format-interface.js +23 -7
- package/exports/codec-format-interface.js.map +1 -0
- package/exports/codec-hash.d.ts +2 -0
- package/exports/codec-hash.js +85 -13
- package/exports/codec-hash.js.map +1 -0
- package/exports/codec.js +1 -0
- package/exports/codec.js.map +1 -0
- package/exports/index.d.ts +15 -0
- package/exports/index.js +15 -0
- package/exports/index.js.map +1 -0
- package/exports/json-interface.d.ts +1 -1
- package/package.json +49 -47
- package/exports/codec-a830f1a0.js +0 -284
- package/exports/codec-format-interface-07c55dde.js +0 -268
- package/exports/codec-format-interface-31a2dd5c.js +0 -268
|
@@ -1,284 +0,0 @@
|
|
|
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 };
|
|
@@ -1,268 +0,0 @@
|
|
|
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 };
|