@leofcoin/codec-format-interface 1.6.16 → 1.6.18

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.
@@ -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?: object): Uint8Array;
10
+ encode(decoded?: any): Uint8Array | Promise<Uint8Array>;
11
11
  protoEncode(data: object): Uint8Array;
12
12
  protoDecode(data: Uint8Array): object;
13
13
  isHex(string: string): boolean;
@@ -0,0 +1,98 @@
1
+ import bs32 from '@vandeurenglenn/base32';
2
+ import base58 from '@vandeurenglenn/base58';
3
+ import isHex from '@vandeurenglenn/is-hex';
4
+ import proto from '@vandeurenglenn/proto-array';
5
+ import { fromBase58, fromHex, toBase32, toBase58, toHex } from '@vandeurenglenn/typed-array-utils';
6
+ export default class BasicInterface {
7
+ encoded;
8
+ decoded;
9
+ keys;
10
+ name;
11
+ #proto;
12
+ set proto(value) {
13
+ this.#proto = value;
14
+ this.keys = Object.keys(value);
15
+ }
16
+ get proto() {
17
+ return this.#proto;
18
+ }
19
+ decode(encoded) {
20
+ encoded = encoded || this.encoded;
21
+ return new Object();
22
+ }
23
+ encode(decoded) {
24
+ decoded = decoded || this.decoded;
25
+ return new Uint8Array();
26
+ }
27
+ // get Codec(): Codec {}
28
+ protoEncode(data) {
29
+ // check schema
30
+ return proto.encode(this.proto, data, false);
31
+ }
32
+ protoDecode(data) {
33
+ // check schema
34
+ return proto.decode(this.proto, data, false);
35
+ }
36
+ isHex(string) {
37
+ return isHex(string);
38
+ }
39
+ isBase32(string) {
40
+ return bs32.isBase32(string);
41
+ }
42
+ isBase58(string) {
43
+ return base58.isBase58(string);
44
+ }
45
+ fromBs32(encoded) {
46
+ return this.decode(bs32.decode(encoded));
47
+ }
48
+ fromBs58(encoded) {
49
+ return this.decode(fromBase58(encoded));
50
+ }
51
+ async toArray() {
52
+ const array = [];
53
+ for await (const value of this.encoded.values()) {
54
+ array.push(value);
55
+ }
56
+ return array;
57
+ }
58
+ fromString(string) {
59
+ const array = string.split(',');
60
+ const arrayLike = array.map(string => Number(string));
61
+ return this.decode(Uint8Array.from(arrayLike));
62
+ }
63
+ fromHex(string) {
64
+ return this.decode(fromHex(string));
65
+ }
66
+ fromArray(array) {
67
+ return this.decode(Uint8Array.from([...array]));
68
+ }
69
+ fromEncoded(encoded) {
70
+ return this.decode(encoded);
71
+ }
72
+ toString() {
73
+ if (!this.encoded)
74
+ this.encode();
75
+ return this.encoded.toString();
76
+ }
77
+ toHex() {
78
+ if (!this.encoded)
79
+ this.encode();
80
+ return toHex(this.encoded);
81
+ }
82
+ /**
83
+ * @return {String} encoded
84
+ */
85
+ toBs32() {
86
+ if (!this.encoded)
87
+ this.encode();
88
+ return toBase32(this.encoded);
89
+ }
90
+ /**
91
+ * @return {String} encoded
92
+ */
93
+ toBs58() {
94
+ if (!this.encoded)
95
+ this.encode();
96
+ return toBase58(this.encoded);
97
+ }
98
+ }
@@ -0,0 +1,120 @@
1
+ import BasicInterface from './basic-interface.js';
2
+ import Hash from './codec-hash.js';
3
+ import Codec from './codec.js';
4
+ export default class FormatInterface extends BasicInterface {
5
+ hashFormat;
6
+ init(buffer) {
7
+ if (buffer instanceof Uint8Array)
8
+ this.fromUint8Array(buffer);
9
+ else if (buffer instanceof ArrayBuffer)
10
+ this.fromArrayBuffer(buffer);
11
+ else if (buffer instanceof FormatInterface && buffer?.name === this.name)
12
+ return buffer;
13
+ else if (typeof buffer === 'string') {
14
+ if (this.isHex(buffer))
15
+ this.fromHex(buffer);
16
+ else if (this.isBase58(buffer))
17
+ this.fromBs58(buffer);
18
+ else if (this.isBase32(buffer))
19
+ this.fromBs32(buffer);
20
+ else
21
+ this.fromString(buffer);
22
+ }
23
+ else {
24
+ this.create(buffer);
25
+ }
26
+ return this;
27
+ }
28
+ hasCodec() {
29
+ if (!this.encoded)
30
+ return false;
31
+ const codec = new Codec(this.encoded);
32
+ if (codec.name)
33
+ return true;
34
+ }
35
+ decode(encoded) {
36
+ encoded = encoded || this.encoded;
37
+ const codec = new Codec(this.encoded);
38
+ if (codec.codecBuffer) {
39
+ encoded = encoded.slice(codec.codecBuffer.length);
40
+ this.name = codec.name;
41
+ this.decoded = this.protoDecode(encoded);
42
+ // try {
43
+ // this.decoded = JSON.parse(this.decoded)
44
+ // } catch {
45
+ // }
46
+ }
47
+ else {
48
+ throw new Error(`no codec found`);
49
+ }
50
+ return this.decoded;
51
+ }
52
+ encode(decoded) {
53
+ let encoded;
54
+ if (!decoded)
55
+ decoded = this.decoded;
56
+ const codec = new Codec(this.name);
57
+ if (decoded instanceof Uint8Array)
58
+ encoded = decoded;
59
+ else
60
+ encoded = this.protoEncode(decoded);
61
+ if (codec.codecBuffer) {
62
+ const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
63
+ uint8Array.set(codec.codecBuffer);
64
+ uint8Array.set(encoded, codec.codecBuffer.length);
65
+ this.encoded = uint8Array;
66
+ }
67
+ else {
68
+ throw new Error(`invalid codec`);
69
+ }
70
+ return this.encoded;
71
+ }
72
+ /**
73
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
74
+ * @param {Object} proto - {protoObject}
75
+ * @param {Object} options - {hashFormat, name}
76
+ */
77
+ constructor(buffer, proto, options) {
78
+ super();
79
+ this.proto = proto;
80
+ this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
81
+ if (options?.name)
82
+ this.name = options.name;
83
+ this.init(buffer);
84
+ }
85
+ /**
86
+ * @return {PeernetHash}
87
+ */
88
+ get peernetHash() {
89
+ return new Hash(this.decoded, { name: this.name });
90
+ }
91
+ /**
92
+ * @return {peernetHash}
93
+ */
94
+ async hash() {
95
+ const upper = this.hashFormat.charAt(0).toUpperCase();
96
+ const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
97
+ return (await this.peernetHash)[`to${format}`]();
98
+ }
99
+ fromUint8Array(buffer) {
100
+ this.encoded = buffer;
101
+ return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
102
+ }
103
+ fromArrayBuffer(buffer) {
104
+ this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
105
+ return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
106
+ }
107
+ /**
108
+ * @param {Object} data
109
+ */
110
+ create(data) {
111
+ const decoded = {};
112
+ if (this.keys?.length > 0) {
113
+ for (const key of this.keys) {
114
+ decoded[key] = data[key];
115
+ }
116
+ this.decoded = decoded;
117
+ return this.encode(decoded);
118
+ }
119
+ }
120
+ }
@@ -17,7 +17,7 @@ export default class CodecHash extends BasicInterface {
17
17
  fromJSON(json: any): Promise<Uint8Array>;
18
18
  encode(buffer: Uint8Array, name?: string): Promise<Uint8Array>;
19
19
  validate(buffer: any): Promise<void>;
20
- decode(buffer: any): {
20
+ decode(encoded: Uint8Array): {
21
21
  codec: Codec;
22
22
  name: string;
23
23
  size: number;
@@ -0,0 +1,136 @@
1
+ import { createKeccak } from 'hash-wasm';
2
+ import varint from 'varint';
3
+ import BasicInterface from './basic-interface.js';
4
+ import Codec from './codec.js';
5
+ export default class CodecHash extends BasicInterface {
6
+ codec;
7
+ codecs;
8
+ digest;
9
+ size;
10
+ constructor(buffer, options) {
11
+ super();
12
+ if (options.name)
13
+ this.name = options.name;
14
+ else
15
+ this.name = 'disco-hash';
16
+ if (options.codecs)
17
+ this.codecs = options.codecs;
18
+ // @ts-ignore
19
+ return this.init(buffer);
20
+ }
21
+ async init(uint8Array) {
22
+ if (uint8Array) {
23
+ if (uint8Array instanceof Uint8Array) {
24
+ this.codec = new Codec(uint8Array);
25
+ const name = this.codec.name;
26
+ if (name) {
27
+ this.name = name;
28
+ this.decode(uint8Array);
29
+ }
30
+ else {
31
+ await this.encode(uint8Array);
32
+ }
33
+ }
34
+ if (typeof uint8Array === 'string') {
35
+ if (this.isHex(uint8Array))
36
+ await this.fromHex(uint8Array);
37
+ if (this.isBase32(uint8Array))
38
+ await this.fromBs32(uint8Array);
39
+ else if (this.isBase58(uint8Array))
40
+ await this.fromBs58(uint8Array);
41
+ else
42
+ throw new Error(`unsupported string ${uint8Array}`);
43
+ }
44
+ else if (typeof uint8Array === 'object')
45
+ await this.fromJSON(uint8Array);
46
+ }
47
+ return this;
48
+ }
49
+ get prefix() {
50
+ const length = this.length;
51
+ const uint8Array = new Uint8Array(length.length + this.codec.codecBuffer.length);
52
+ uint8Array.set(length);
53
+ uint8Array.set(this.codec.codecBuffer, length.length);
54
+ return uint8Array;
55
+ }
56
+ get length() {
57
+ return varint.encode(this.size);
58
+ }
59
+ get buffer() {
60
+ return this.encoded;
61
+ }
62
+ get hash() {
63
+ return this.encoded;
64
+ }
65
+ fromJSON(json) {
66
+ return this.encode(new TextEncoder().encode(JSON.stringify(json)));
67
+ }
68
+ async encode(buffer, name) {
69
+ if (!this.name && name)
70
+ this.name = name;
71
+ if (!buffer)
72
+ buffer = this.buffer;
73
+ this.codec = new Codec(this.name);
74
+ this.codec.fromName(this.name);
75
+ let hashAlg = this.codec.hashAlg;
76
+ const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
77
+ if (hashAlg.includes('dbl')) {
78
+ hashAlg = hashAlg.replace('dbl-', '');
79
+ const hasher = await createKeccak(hashVariant);
80
+ await hasher.init();
81
+ hasher.update(buffer);
82
+ buffer = hasher.digest('binary');
83
+ }
84
+ const hasher = await createKeccak(hashVariant);
85
+ await hasher.init();
86
+ hasher.update(buffer);
87
+ this.digest = hasher.digest('binary');
88
+ this.size = this.digest.length;
89
+ const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
90
+ uint8Array.set(this.prefix);
91
+ uint8Array.set(this.digest, this.prefix.length);
92
+ this.encoded = uint8Array;
93
+ return this.encoded;
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
+ }
101
+ else {
102
+ await this.encode(buffer);
103
+ }
104
+ }
105
+ if (typeof buffer === 'string') {
106
+ if (this.isHex(buffer))
107
+ this.fromHex(buffer);
108
+ if (this.isBase32(buffer))
109
+ this.fromBs32(buffer);
110
+ }
111
+ if (typeof buffer === 'object')
112
+ this.fromJSON(buffer);
113
+ }
114
+ decode(encoded) {
115
+ this.encoded = encoded;
116
+ const codec = varint.decode(encoded);
117
+ this.codec = new Codec(codec);
118
+ // TODO: validate codec
119
+ encoded = encoded.slice(varint.decode.bytes);
120
+ this.size = varint.decode(encoded);
121
+ this.digest = encoded.slice(varint.decode.bytes);
122
+ if (this.digest.length !== this.size) {
123
+ throw new Error(`hash length inconsistent: ${this.encoded.toString()}`);
124
+ }
125
+ // const codec = new Codec(codec, this.codecs)
126
+ this.name = this.codec.name;
127
+ this.size = this.digest.length;
128
+ return {
129
+ codec: this.codec,
130
+ name: this.name,
131
+ size: this.size,
132
+ length: this.length,
133
+ digest: this.digest,
134
+ };
135
+ }
136
+ }
@@ -0,0 +1,91 @@
1
+ import varint from 'varint';
2
+ import { utils as codecUtils } from '@leofcoin/codecs';
3
+ import BasicInterface from './basic-interface.js';
4
+ export default class Codec extends BasicInterface {
5
+ codecBuffer;
6
+ codec;
7
+ hashAlg;
8
+ constructor(buffer) {
9
+ super();
10
+ if (buffer) {
11
+ if (buffer instanceof Uint8Array) {
12
+ const codec = varint.decode(buffer);
13
+ const name = this.getCodecName(codec);
14
+ if (name) {
15
+ this.name = name;
16
+ this.encoded = buffer;
17
+ this.decode(buffer);
18
+ }
19
+ else {
20
+ this.encode(Number(new TextDecoder().decode(buffer)));
21
+ }
22
+ }
23
+ else if (buffer instanceof ArrayBuffer) {
24
+ const codec = varint.decode(new Uint8Array(buffer));
25
+ const name = this.getCodecName(codec);
26
+ if (name) {
27
+ this.name = name;
28
+ this.decode(buffer);
29
+ }
30
+ else {
31
+ this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
32
+ }
33
+ }
34
+ else if (typeof buffer === 'string') {
35
+ if (codecUtils.getCodec(buffer))
36
+ this.fromName(buffer);
37
+ else if (this.isHex(buffer))
38
+ this.fromHex(buffer);
39
+ else if (this.isBase32(buffer))
40
+ this.fromBs32(buffer);
41
+ else if (this.isBase58(buffer))
42
+ this.fromBs58(buffer);
43
+ else
44
+ this.fromString(buffer);
45
+ }
46
+ if (!isNaN(buffer))
47
+ if (codecUtils.getCodec(buffer))
48
+ this.fromCodec(buffer);
49
+ }
50
+ }
51
+ fromEncoded(encoded) {
52
+ const codec = varint.decode(encoded);
53
+ const name = this.getCodecName(codec);
54
+ this.name = name;
55
+ this.encoded = encoded;
56
+ return this.decode(encoded);
57
+ }
58
+ getCodec(name) {
59
+ return codecUtils.getCodec(name);
60
+ }
61
+ getCodecName(codec) {
62
+ return codecUtils.getCodecName(codec);
63
+ }
64
+ getHashAlg(name) {
65
+ return codecUtils.getHashAlg(name);
66
+ }
67
+ fromCodec(codec) {
68
+ this.name = this.getCodecName(codec);
69
+ this.hashAlg = this.getHashAlg(this.name);
70
+ this.codec = this.getCodec(this.name);
71
+ this.codecBuffer = varint.encode(this.codec);
72
+ }
73
+ fromName(name) {
74
+ const codec = this.getCodec(name);
75
+ this.name = name;
76
+ this.codec = codec;
77
+ this.hashAlg = this.getHashAlg(name);
78
+ this.codecBuffer = varint.encode(this.codec);
79
+ }
80
+ decode(encoded) {
81
+ encoded = encoded || this.encoded;
82
+ const codec = varint.decode(encoded);
83
+ this.fromCodec(codec);
84
+ return this.decoded;
85
+ }
86
+ encode(codec) {
87
+ codec = codec || this.codec;
88
+ this.encoded = varint.encode(codec);
89
+ return this.encoded;
90
+ }
91
+ }
@@ -0,0 +1,9 @@
1
+ import basicInterface from './basic-interface.js';
2
+ import formatInterface from './codec-format-interface.js';
3
+ import codecHash from './codec-hash.js';
4
+ import codec from './codec.js';
5
+ export { codecs } from '@leofcoin/codecs';
6
+ export const BasicInterface = basicInterface;
7
+ export const FormatInterface = formatInterface;
8
+ export const CodecHash = codecHash;
9
+ export const Codec = codec;
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.6.16",
3
+ "version": "1.6.18",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
7
- ".": "./dist/index.js"
7
+ ".": {
8
+ "import": "./exports/index.js",
9
+ "types": "./exports/index.d.ts"
10
+ }
8
11
  },
9
12
  "files": [
10
- "dist"
13
+ "exports"
11
14
  ],
12
- "typings": "dist/index.d.ts",
13
15
  "scripts": {
14
16
  "prepublish": "npm run build && npm version patch",
15
- "build": "rollup -c",
17
+ "build": "npx tsc",
16
18
  "test": "node test"
17
19
  },
18
20
  "repository": {
@@ -30,19 +32,15 @@
30
32
  "@leofcoin/codecs": "^1.0.0",
31
33
  "@vandeurenglenn/base32": "^1.1.0",
32
34
  "@vandeurenglenn/base58": "^1.1.1",
33
- "@vandeurenglenn/is-hex": "^1.0.0",
35
+ "@vandeurenglenn/is-hex": "^1.1.1",
34
36
  "@vandeurenglenn/proto-array": "^1.0.0",
35
37
  "@vandeurenglenn/typed-array-utils": "^1.1.0",
36
38
  "hash-wasm": "^4.9.0",
37
39
  "varint": "^6.0.0"
38
40
  },
39
41
  "devDependencies": {
40
- "@rollup/plugin-commonjs": "^23.0.3",
41
- "@rollup/plugin-node-resolve": "^15.0.1",
42
- "@rollup/plugin-typescript": "^10.0.1",
43
42
  "@types/varint": "^6.0.1",
44
- "rollup": "^3.5.1",
45
43
  "tape": "^5.5.3",
46
- "tslib": "^2.4.1"
44
+ "typescript": "^5.2.2"
47
45
  }
48
46
  }
package/dist/index.js DELETED
@@ -1,453 +0,0 @@
1
- import bs32 from '@vandeurenglenn/base32';
2
- import base58 from '@vandeurenglenn/base58';
3
- import proto from '@vandeurenglenn/proto-array';
4
- import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/typed-array-utils';
5
- import { createKeccak } from 'hash-wasm';
6
- import varint from 'varint';
7
- import { utils } from '@leofcoin/codecs';
8
- export { codecs } from '@leofcoin/codecs';
9
-
10
- /**
11
- * @param {string}
12
- */
13
- var isHex = (function (string) { return /^[A-F0-9]+$/i.test(string); });
14
-
15
- let BasicInterface$1 = class BasicInterface {
16
- encoded;
17
- decoded;
18
- keys;
19
- name;
20
- #proto;
21
- set proto(value) {
22
- this.#proto = value;
23
- this.keys = Object.keys(value);
24
- }
25
- get proto() {
26
- return this.#proto;
27
- }
28
- decode(encoded) {
29
- encoded = encoded || this.encoded;
30
- return new Object();
31
- }
32
- encode(decoded) {
33
- decoded = decoded || this.decoded;
34
- return new Uint8Array();
35
- }
36
- // get Codec(): Codec {}
37
- protoEncode(data) {
38
- // check schema
39
- return proto.encode(this.proto, data, false);
40
- }
41
- protoDecode(data) {
42
- // check schema
43
- return proto.decode(this.proto, data, false);
44
- }
45
- isHex(string) {
46
- return isHex(string);
47
- }
48
- isBase32(string) {
49
- return bs32.isBase32(string);
50
- }
51
- isBase58(string) {
52
- return base58.isBase58(string);
53
- }
54
- fromBs32(encoded) {
55
- return this.decode(bs32.decode(encoded));
56
- }
57
- fromBs58(encoded) {
58
- return this.decode(fromBase58(encoded));
59
- }
60
- async toArray() {
61
- const array = [];
62
- for await (const value of this.encoded.values()) {
63
- array.push(value);
64
- }
65
- return array;
66
- }
67
- fromString(string) {
68
- const array = string.split(',');
69
- const arrayLike = array.map(string => Number(string));
70
- return this.decode(Uint8Array.from(arrayLike));
71
- }
72
- fromHex(string) {
73
- return this.decode(fromHex(string));
74
- }
75
- fromArray(array) {
76
- return this.decode(Uint8Array.from([...array]));
77
- }
78
- fromEncoded(encoded) {
79
- return this.decode(encoded);
80
- }
81
- toString() {
82
- if (!this.encoded)
83
- this.encode();
84
- return this.encoded.toString();
85
- }
86
- toHex() {
87
- if (!this.encoded)
88
- this.encode();
89
- return toHex(this.encoded.toString().split(',').map(number => Number(number)));
90
- }
91
- /**
92
- * @return {String} encoded
93
- */
94
- toBs32() {
95
- if (!this.encoded)
96
- this.encode();
97
- return toBase32(this.encoded);
98
- }
99
- /**
100
- * @return {String} encoded
101
- */
102
- toBs58() {
103
- if (!this.encoded)
104
- this.encode();
105
- return toBase58(this.encoded);
106
- }
107
- };
108
-
109
- let Codec$1 = class Codec extends BasicInterface$1 {
110
- codecBuffer;
111
- codec;
112
- hashAlg;
113
- constructor(buffer) {
114
- super();
115
- if (buffer) {
116
- if (buffer instanceof Uint8Array) {
117
- const codec = varint.decode(buffer);
118
- const name = this.getCodecName(codec);
119
- if (name) {
120
- this.name = name;
121
- this.encoded = buffer;
122
- this.decode(buffer);
123
- }
124
- else {
125
- this.encode(Number(new TextDecoder().decode(buffer)));
126
- }
127
- }
128
- else if (buffer instanceof ArrayBuffer) {
129
- const codec = varint.decode(new Uint8Array(buffer));
130
- const name = this.getCodecName(codec);
131
- if (name) {
132
- this.name = name;
133
- this.decode(buffer);
134
- }
135
- else {
136
- this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
137
- }
138
- }
139
- else if (typeof buffer === 'string') {
140
- if (utils.getCodec(buffer))
141
- this.fromName(buffer);
142
- else if (this.isHex(buffer))
143
- this.fromHex(buffer);
144
- else if (this.isBase32(buffer))
145
- this.fromBs32(buffer);
146
- else if (this.isBase58(buffer))
147
- this.fromBs58(buffer);
148
- else
149
- this.fromString(buffer);
150
- }
151
- if (!isNaN(buffer))
152
- if (utils.getCodec(buffer))
153
- this.fromCodec(buffer);
154
- }
155
- }
156
- fromEncoded(encoded) {
157
- const codec = varint.decode(encoded);
158
- const name = this.getCodecName(codec);
159
- this.name = name;
160
- this.encoded = encoded;
161
- return this.decode(encoded);
162
- }
163
- getCodec(name) {
164
- return utils.getCodec(name);
165
- }
166
- getCodecName(codec) {
167
- return utils.getCodecName(codec);
168
- }
169
- getHashAlg(name) {
170
- return utils.getHashAlg(name);
171
- }
172
- fromCodec(codec) {
173
- this.name = this.getCodecName(codec);
174
- this.hashAlg = this.getHashAlg(this.name);
175
- this.codec = this.getCodec(this.name);
176
- this.codecBuffer = varint.encode(this.codec);
177
- }
178
- fromName(name) {
179
- const codec = this.getCodec(name);
180
- this.name = name;
181
- this.codec = codec;
182
- this.hashAlg = this.getHashAlg(name);
183
- this.codecBuffer = varint.encode(this.codec);
184
- }
185
- decode(encoded) {
186
- encoded = encoded || this.encoded;
187
- const codec = varint.decode(encoded);
188
- this.fromCodec(codec);
189
- return this.decoded;
190
- }
191
- encode(codec) {
192
- codec = codec || this.codec;
193
- this.encoded = varint.encode(codec);
194
- return this.encoded;
195
- }
196
- };
197
-
198
- let CodecHash$1 = class CodecHash extends BasicInterface$1 {
199
- codec;
200
- codecs;
201
- digest;
202
- size;
203
- constructor(buffer, options) {
204
- super();
205
- if (options.name)
206
- this.name = options.name;
207
- else
208
- this.name = 'disco-hash';
209
- if (options.codecs)
210
- this.codecs = options.codecs;
211
- return this.init(buffer);
212
- }
213
- async init(uint8Array) {
214
- if (uint8Array) {
215
- if (uint8Array instanceof Uint8Array) {
216
- this.codec = new Codec$1(uint8Array);
217
- const name = this.codec.name;
218
- if (name) {
219
- this.name = name;
220
- this.decode(uint8Array);
221
- }
222
- else {
223
- await this.encode(uint8Array);
224
- }
225
- }
226
- if (typeof uint8Array === 'string') {
227
- if (this.isHex(uint8Array))
228
- await this.fromHex(uint8Array);
229
- if (this.isBase32(uint8Array))
230
- await this.fromBs32(uint8Array);
231
- else if (this.isBase58(uint8Array))
232
- await this.fromBs58(uint8Array);
233
- else
234
- throw new Error(`unsupported string ${uint8Array}`);
235
- }
236
- else if (typeof uint8Array === 'object')
237
- await this.fromJSON(uint8Array);
238
- }
239
- return this;
240
- }
241
- get prefix() {
242
- const length = this.length;
243
- const uint8Array = new Uint8Array(length.length + this.codec.codecBuffer.length);
244
- uint8Array.set(length);
245
- uint8Array.set(this.codec.codecBuffer, length.length);
246
- return uint8Array;
247
- }
248
- get length() {
249
- return varint.encode(this.size);
250
- }
251
- get buffer() {
252
- return this.encoded;
253
- }
254
- get hash() {
255
- return this.encoded;
256
- }
257
- fromJSON(json) {
258
- return this.encode(new TextEncoder().encode(JSON.stringify(json)));
259
- }
260
- async encode(buffer, name) {
261
- if (!this.name && name)
262
- this.name = name;
263
- if (!buffer)
264
- buffer = this.buffer;
265
- this.codec = new Codec$1(this.name);
266
- this.codec.fromName(this.name);
267
- let hashAlg = this.codec.hashAlg;
268
- const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
269
- if (hashAlg.includes('dbl')) {
270
- hashAlg = hashAlg.replace('dbl-', '');
271
- const hasher = await createKeccak(hashVariant);
272
- await hasher.init();
273
- hasher.update(buffer);
274
- buffer = hasher.digest('binary');
275
- }
276
- const hasher = await createKeccak(hashVariant);
277
- await hasher.init();
278
- hasher.update(buffer);
279
- this.digest = hasher.digest('binary');
280
- this.size = this.digest.length;
281
- const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
282
- uint8Array.set(this.prefix);
283
- uint8Array.set(this.digest, this.prefix.length);
284
- this.encoded = uint8Array;
285
- return this.encoded;
286
- }
287
- async validate(buffer) {
288
- if (Buffer.isBuffer(buffer)) {
289
- const codec = varint.decode(buffer);
290
- if (this.codecs[codec]) {
291
- this.decode(buffer);
292
- }
293
- else {
294
- await this.encode(buffer);
295
- }
296
- }
297
- if (typeof buffer === 'string') {
298
- if (this.isHex(buffer))
299
- this.fromHex(buffer);
300
- if (this.isBase32(buffer))
301
- this.fromBs32(buffer);
302
- }
303
- if (typeof buffer === 'object')
304
- this.fromJSON(buffer);
305
- }
306
- decode(buffer) {
307
- this.encoded = buffer;
308
- const codec = varint.decode(buffer);
309
- this.codec = new Codec$1(codec);
310
- // TODO: validate codec
311
- buffer = buffer.slice(varint.decode.bytes);
312
- this.size = varint.decode(buffer);
313
- this.digest = buffer.slice(varint.decode.bytes);
314
- if (this.digest.length !== this.size) {
315
- throw new Error(`hash length inconsistent: ${this.encoded.toString()}`);
316
- }
317
- // const codec = new Codec(codec, this.codecs)
318
- this.name = this.codec.name;
319
- this.size = this.digest.length;
320
- return {
321
- codec: this.codec,
322
- name: this.name,
323
- size: this.size,
324
- length: this.length,
325
- digest: this.digest,
326
- };
327
- }
328
- };
329
-
330
- let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
331
- hashFormat;
332
- init(buffer) {
333
- if (buffer instanceof Uint8Array)
334
- this.fromUint8Array(buffer);
335
- else if (buffer instanceof ArrayBuffer)
336
- this.fromArrayBuffer(buffer);
337
- else if (buffer instanceof FormatInterface && buffer?.name === this.name)
338
- return buffer;
339
- else if (typeof buffer === 'string') {
340
- if (this.isHex(buffer))
341
- this.fromHex(buffer);
342
- else if (this.isBase58(buffer))
343
- this.fromBs58(buffer);
344
- else if (this.isBase32(buffer))
345
- this.fromBs32(buffer);
346
- else
347
- this.fromString(buffer);
348
- }
349
- else {
350
- this.create(buffer);
351
- }
352
- return this;
353
- }
354
- hasCodec() {
355
- if (!this.encoded)
356
- return false;
357
- const codec = new Codec$1(this.encoded);
358
- if (codec.name)
359
- return true;
360
- }
361
- decode(encoded) {
362
- encoded = encoded || this.encoded;
363
- const codec = new Codec$1(this.encoded);
364
- if (codec.codecBuffer) {
365
- encoded = encoded.slice(codec.codecBuffer.length);
366
- this.name = codec.name;
367
- this.decoded = this.protoDecode(encoded);
368
- // try {
369
- // this.decoded = JSON.parse(this.decoded)
370
- // } catch {
371
- // }
372
- }
373
- else {
374
- throw new Error(`no codec found`);
375
- }
376
- return this.decoded;
377
- }
378
- encode(decoded) {
379
- let encoded;
380
- if (!decoded)
381
- decoded = this.decoded;
382
- const codec = new Codec$1(this.name);
383
- if (decoded instanceof Uint8Array)
384
- encoded = decoded;
385
- else
386
- encoded = this.protoEncode(decoded);
387
- if (codec.codecBuffer) {
388
- const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
389
- uint8Array.set(codec.codecBuffer);
390
- uint8Array.set(encoded, codec.codecBuffer.length);
391
- this.encoded = uint8Array;
392
- }
393
- else {
394
- throw new Error(`invalid codec`);
395
- }
396
- return this.encoded;
397
- }
398
- /**
399
- * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
400
- * @param {Object} proto - {protoObject}
401
- * @param {Object} options - {hashFormat, name}
402
- */
403
- constructor(buffer, proto, options) {
404
- super();
405
- this.proto = proto;
406
- this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
407
- if (options?.name)
408
- this.name = options.name;
409
- this.init(buffer);
410
- }
411
- /**
412
- * @return {PeernetHash}
413
- */
414
- get peernetHash() {
415
- return new CodecHash$1(this.encoded, { name: this.name });
416
- }
417
- /**
418
- * @return {peernetHash}
419
- */
420
- async hash() {
421
- const upper = this.hashFormat.charAt(0).toUpperCase();
422
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
423
- return (await this.peernetHash)[`to${format}`]();
424
- }
425
- fromUint8Array(buffer) {
426
- this.encoded = buffer;
427
- return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
428
- }
429
- fromArrayBuffer(buffer) {
430
- this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
431
- return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
432
- }
433
- /**
434
- * @param {Object} data
435
- */
436
- create(data) {
437
- const decoded = {};
438
- if (this.keys?.length > 0) {
439
- for (const key of this.keys) {
440
- decoded[key] = data[key];
441
- }
442
- this.decoded = decoded;
443
- return this.encode(decoded);
444
- }
445
- }
446
- };
447
-
448
- const BasicInterface = BasicInterface$1;
449
- const FormatInterface = FormatInterface$1;
450
- const CodecHash = CodecHash$1;
451
- const Codec = Codec$1;
452
-
453
- export { BasicInterface, Codec, CodecHash, FormatInterface };
File without changes
File without changes
File without changes