@leofcoin/codec-format-interface 1.7.12 → 1.7.14

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 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.
@@ -1,5 +1,7 @@
1
1
  import type { base58String } from '@vandeurenglenn/base58';
2
2
  import type { base32String } from '@vandeurenglenn/base32';
3
+ export declare const jsonStringifyBigInt: (key: any, value: any) => any;
4
+ export declare const jsonParseBigInt: (key: any, value: any) => any;
3
5
  export default class BasicInterface {
4
6
  #private;
5
7
  name: string;
@@ -4,6 +4,8 @@ import isHex from '@vandeurenglenn/is-hex';
4
4
  import proto from '@vandeurenglenn/proto-array';
5
5
  import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/typed-array-utils';
6
6
 
7
+ const jsonStringifyBigInt = (key, value) => typeof value === 'bigint' ? { $bigint: value.toString() } : value;
8
+ const jsonParseBigInt = (key, value) => typeof value === 'object' && value.$bigint ? BigInt(value.$bigint) : value;
7
9
  class BasicInterface {
8
10
  #encoded;
9
11
  #decoded;
@@ -120,4 +122,4 @@ class BasicInterface {
120
122
  }
121
123
  }
122
124
 
123
- export { BasicInterface as default };
125
+ export { BasicInterface as default, jsonParseBigInt, jsonStringifyBigInt };
@@ -1,4 +1,4 @@
1
- import BasicInterface from './basic-interface.js';
1
+ import BasicInterface, { jsonParseBigInt } from './basic-interface.js';
2
2
  import Codec from './codec.js';
3
3
  import CodecHash from './codec-hash.js';
4
4
  import '@vandeurenglenn/base32';
@@ -128,13 +128,13 @@ class FormatInterface extends BasicInterface {
128
128
  this.encoded = buffer;
129
129
  return this.hasCodec()
130
130
  ? this.decode()
131
- : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
131
+ : this.create(JSON.parse(new TextDecoder().decode(this.encoded), jsonParseBigInt));
132
132
  }
133
133
  fromArrayBuffer(buffer) {
134
134
  this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
135
135
  return this.hasCodec()
136
136
  ? this.decode()
137
- : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
137
+ : this.create(JSON.parse(new TextDecoder().decode(this.encoded), jsonParseBigInt));
138
138
  }
139
139
  /**
140
140
  * @param {Object} data
@@ -1,8 +1,14 @@
1
1
  import BasicInterface from './basic-interface.js';
2
+ import Codec from './codec.js';
3
+ type CodecHashOptions = {
4
+ name: string;
5
+ codecs: object;
6
+ };
2
7
  export default class CodecHash extends BasicInterface {
3
- codec: any;
4
- discoCodec: any;
5
- constructor(buffer: any, options?: {});
8
+ codec: Uint8Array;
9
+ discoCodec: Codec;
10
+ size: number;
11
+ constructor(buffer: any, options: CodecHashOptions);
6
12
  init(uint8Array: any): Promise<this>;
7
13
  get prefix(): Uint8Array;
8
14
  get length(): number[];
@@ -12,10 +18,11 @@ export default class CodecHash extends BasicInterface {
12
18
  encode(buffer: any, name?: any): Promise<Uint8Array>;
13
19
  validate(buffer: any): Promise<void>;
14
20
  decode(buffer: any): {
15
- codec: any;
21
+ codec: Uint8Array;
16
22
  name: string;
17
- size: any;
23
+ size: number;
18
24
  length: number[];
19
25
  digest: any;
20
26
  };
21
27
  }
28
+ export {};
@@ -1,5 +1,5 @@
1
1
  import varint from 'varint';
2
- import BasicInterface from './basic-interface.js';
2
+ import BasicInterface, { jsonStringifyBigInt } from './basic-interface.js';
3
3
  import Codec from './codec.js';
4
4
  import '@vandeurenglenn/base32';
5
5
  import '@vandeurenglenn/base58';
@@ -11,20 +11,21 @@ import '@leofcoin/codecs';
11
11
  class CodecHash extends BasicInterface {
12
12
  codec;
13
13
  discoCodec;
14
- constructor(buffer, options = {}) {
14
+ size;
15
+ constructor(buffer, options) {
15
16
  super();
16
- if (options.name)
17
+ if (options?.name)
17
18
  this.name = options.name;
18
19
  else
19
20
  this.name = 'disco-hash';
20
- if (options.codecs)
21
+ if (options?.codecs)
21
22
  this.codecs = options.codecs;
22
23
  return this.init(buffer);
23
24
  }
24
25
  async init(uint8Array) {
25
26
  if (uint8Array) {
26
27
  if (uint8Array instanceof Uint8Array) {
27
- this.discoCodec = new Codec(uint8Array, this.codecs);
28
+ this.discoCodec = new Codec(uint8Array);
28
29
  const name = this.discoCodec.name;
29
30
  if (name) {
30
31
  this.name = name;
@@ -66,7 +67,7 @@ class CodecHash extends BasicInterface {
66
67
  return this.encoded;
67
68
  }
68
69
  fromJSON(json) {
69
- return this.encode(new TextEncoder().encode(JSON.stringify(json)));
70
+ return this.encode(new TextEncoder().encode(JSON.stringify(json, jsonStringifyBigInt)));
70
71
  }
71
72
  async encode(buffer, name) {
72
73
  if (!this.name && name)
@@ -140,7 +141,7 @@ class CodecHash extends BasicInterface {
140
141
  name: this.name,
141
142
  size: this.size,
142
143
  length: this.length,
143
- digest: this.digest,
144
+ digest: this.digest
144
145
  };
145
146
  }
146
147
  }
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
- {
2
- "name": "@leofcoin/codec-format-interface",
3
- "version": "1.7.12",
4
- "description": "",
5
- "type": "module",
6
- "exports": {
7
- ".": "./exports/index.js"
8
- },
9
- "files": [
10
- "exports"
11
- ],
12
- "typings": "exports/index.d.ts",
13
- "scripts": {
14
- "build": "rollup -c",
15
- "test": "node test"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/leofcoin/codec-format-interface.git"
20
- },
21
- "keywords": [],
22
- "author": "",
23
- "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/leofcoin/codec-format-interface/issues"
26
- },
27
- "homepage": "https://github.com/leofcoin/codec-format-interface#readme",
28
- "dependencies": {
29
- "@leofcoin/codecs": "^1.0.6",
30
- "@leofcoin/crypto": "^0.2.24",
31
- "@vandeurenglenn/base32": "^1.2.4",
32
- "@vandeurenglenn/base58": "^1.1.9",
33
- "@vandeurenglenn/is-hex": "^1.1.1",
34
- "@vandeurenglenn/proto-array": "^1.0.16",
35
- "@vandeurenglenn/typed-array-utils": "^1.2.0",
36
- "hash-wasm": "^4.11.0",
37
- "varint": "^6.0.0"
38
- },
39
- "devDependencies": {
40
- "@rollup/plugin-typescript": "^11.1.6",
41
- "@types/varint": "^6.0.3",
42
- "rollup": "^4.22.2",
43
- "tape": "^5.9.0",
44
- "tinybench": "^2.9.0",
45
- "tslib": "^2.7.0"
46
- }
47
- }
1
+ {
2
+ "name": "@leofcoin/codec-format-interface",
3
+ "version": "1.7.14",
4
+ "description": "",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./exports/index.js"
8
+ },
9
+ "files": [
10
+ "exports"
11
+ ],
12
+ "typings": "exports/index.d.ts",
13
+ "scripts": {
14
+ "build": "rollup -c",
15
+ "test": "node test"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/leofcoin/codec-format-interface.git"
20
+ },
21
+ "keywords": [],
22
+ "author": "",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/leofcoin/codec-format-interface/issues"
26
+ },
27
+ "homepage": "https://github.com/leofcoin/codec-format-interface#readme",
28
+ "dependencies": {
29
+ "@leofcoin/codecs": "^1.0.7",
30
+ "@leofcoin/crypto": "^0.2.37",
31
+ "@vandeurenglenn/base32": "^1.2.4",
32
+ "@vandeurenglenn/base58": "^1.1.9",
33
+ "@vandeurenglenn/is-hex": "^1.1.1",
34
+ "@vandeurenglenn/proto-array": "^1.0.17",
35
+ "@vandeurenglenn/typed-array-utils": "^1.2.0",
36
+ "hash-wasm": "^4.12.0",
37
+ "varint": "^6.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@rollup/plugin-typescript": "^12.3.0",
41
+ "@types/varint": "^6.0.3",
42
+ "rollup": "^4.53.3",
43
+ "tape": "^5.9.0",
44
+ "tinybench": "^5.1.0",
45
+ "tslib": "^2.8.1"
46
+ }
47
+ }
@@ -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 };
@@ -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 };