@leofcoin/codec-format-interface 1.6.17 → 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.
@@ -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
  */
@@ -0,0 +1,141 @@
1
+ import BasicInterface from './basic-interface.js';
2
+ import Codec from './codec.js';
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 {
13
+ hashFormat;
14
+ #hash;
15
+ init(buffer) {
16
+ if (buffer instanceof Uint8Array)
17
+ this.fromUint8Array(buffer);
18
+ else if (buffer instanceof ArrayBuffer)
19
+ this.fromArrayBuffer(buffer);
20
+ else if (buffer instanceof FormatInterface && buffer?.name === this.name)
21
+ return buffer;
22
+ else if (typeof buffer === 'string') {
23
+ if (this.isHex(buffer))
24
+ this.fromHex(buffer);
25
+ else if (this.isBase58(buffer))
26
+ this.fromBs58(buffer);
27
+ else if (this.isBase32(buffer))
28
+ this.fromBs32(buffer);
29
+ else
30
+ this.fromString(buffer);
31
+ }
32
+ else {
33
+ this.create(buffer);
34
+ }
35
+ return this;
36
+ }
37
+ hasCodec() {
38
+ if (!this.encoded)
39
+ return false;
40
+ const codec = new Codec(this.encoded);
41
+ if (codec.name)
42
+ return true;
43
+ }
44
+ decode(encoded) {
45
+ encoded = encoded || this.encoded;
46
+ const codec = new Codec(encoded);
47
+ if (codec.codecBuffer) {
48
+ encoded = encoded.slice(codec.codecBuffer.length);
49
+ this.name = codec.name;
50
+ this.decoded = this.protoDecode(encoded);
51
+ // try {
52
+ // this.decoded = JSON.parse(this.decoded)
53
+ // } catch {
54
+ // }
55
+ }
56
+ else {
57
+ throw new Error(`no codec found`);
58
+ }
59
+ return this.decoded;
60
+ }
61
+ encode(decoded) {
62
+ let encoded;
63
+ decoded = decoded || this.decoded;
64
+ const codec = new Codec(this.name);
65
+ if (decoded instanceof Uint8Array)
66
+ encoded = decoded;
67
+ else
68
+ encoded = this.protoEncode(decoded);
69
+ if (codec.codecBuffer) {
70
+ const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
71
+ uint8Array.set(codec.codecBuffer);
72
+ uint8Array.set(encoded, codec.codecBuffer.length);
73
+ this.encoded = uint8Array;
74
+ }
75
+ else {
76
+ throw new Error(`invalid codec`);
77
+ }
78
+ return this.encoded;
79
+ }
80
+ /**
81
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
82
+ * @param {Object} proto - {protoObject}
83
+ * @param {Object} options - {hashFormat, name}
84
+ */
85
+ constructor(buffer, proto, options) {
86
+ super();
87
+ this.proto = proto;
88
+ this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
89
+ if (options?.name)
90
+ this.name = options.name;
91
+ this.init(buffer);
92
+ }
93
+ get format() {
94
+ const upper = this.hashFormat.charAt(0).toUpperCase();
95
+ return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
96
+ }
97
+ /**
98
+ * @return {PeernetHash}
99
+ */
100
+ get peernetHash() {
101
+ const decoded = this.decoded;
102
+ // @ts-ignore
103
+ delete decoded.hash;
104
+ return new CodecHash(decoded, { name: this.name });
105
+ }
106
+ /**
107
+ * @return {peernetHash}
108
+ */
109
+ async hash() {
110
+ if (this.#hash)
111
+ return this.#hash;
112
+ const upper = this.hashFormat.charAt(0).toUpperCase();
113
+ const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
114
+ return (await this.peernetHash)[`to${format}`]();
115
+ }
116
+ fromUint8Array(buffer) {
117
+ this.encoded = buffer;
118
+ return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
119
+ }
120
+ fromArrayBuffer(buffer) {
121
+ this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
122
+ return this.hasCodec() ? this.decode() : this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
123
+ }
124
+ /**
125
+ * @param {Object} data
126
+ */
127
+ create(data) {
128
+ const decoded = {};
129
+ if (data.hash)
130
+ this.#hash = data.hash;
131
+ if (this.keys?.length > 0) {
132
+ for (const key of this.keys) {
133
+ decoded[key] = data[key];
134
+ }
135
+ this.decoded = decoded;
136
+ // return this.encode(decoded)
137
+ }
138
+ }
139
+ }
140
+
141
+ export { FormatInterface as default };
@@ -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: Codec;
5
- codecs: {};
6
- digest: Uint8Array;
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: Uint8Array, name?: string): Promise<Uint8Array>;
12
+ encode(buffer: any, name: any): Promise<Uint8Array>;
19
13
  validate(buffer: any): Promise<void>;
20
14
  decode(buffer: any): {
21
- codec: Codec;
15
+ codec: any;
22
16
  name: string;
23
- size: number;
17
+ size: any;
24
18
  length: number[];
25
- digest: Uint8Array;
19
+ digest: any;
26
20
  };
27
21
  }
@@ -0,0 +1,148 @@
1
+ import varint from 'varint';
2
+ import BasicInterface from './basic-interface.js';
3
+ import Codec from './codec.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 '@leofcoin/codecs';
10
+
11
+ class CodecHash extends BasicInterface {
12
+ codec;
13
+ discoCodec;
14
+ constructor(buffer, options = {}) {
15
+ super();
16
+ if (options.name)
17
+ this.name = options.name;
18
+ else
19
+ this.name = 'disco-hash';
20
+ if (options.codecs)
21
+ this.codecs = options.codecs;
22
+ return this.init(buffer);
23
+ }
24
+ async init(uint8Array) {
25
+ if (uint8Array) {
26
+ if (uint8Array instanceof Uint8Array) {
27
+ this.discoCodec = new Codec(uint8Array, this.codecs);
28
+ const name = this.discoCodec.name;
29
+ if (name) {
30
+ this.name = name;
31
+ this.decode(uint8Array);
32
+ }
33
+ else {
34
+ await this.encode(uint8Array);
35
+ }
36
+ }
37
+ if (typeof uint8Array === 'string') {
38
+ if (this.isHex(uint8Array))
39
+ await this.fromHex(uint8Array);
40
+ if (this.isBase32(uint8Array))
41
+ await this.fromBs32(uint8Array);
42
+ else if (this.isBase58(uint8Array))
43
+ await this.fromBs58(uint8Array);
44
+ else
45
+ throw new Error(`unsupported string ${uint8Array}`);
46
+ }
47
+ else if (typeof uint8Array === 'object')
48
+ await this.fromJSON(uint8Array);
49
+ }
50
+ return this;
51
+ }
52
+ get prefix() {
53
+ const length = this.length;
54
+ const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
55
+ uint8Array.set(length);
56
+ uint8Array.set(this.discoCodec.codecBuffer, length.length);
57
+ return uint8Array;
58
+ }
59
+ get length() {
60
+ return varint.encode(this.size);
61
+ }
62
+ get buffer() {
63
+ return this.encoded;
64
+ }
65
+ get hash() {
66
+ return this.encoded;
67
+ }
68
+ fromJSON(json) {
69
+ return this.encode(Buffer.from(JSON.stringify(json)));
70
+ }
71
+ async encode(buffer, name) {
72
+ if (!this.name && name)
73
+ this.name = name;
74
+ if (!buffer)
75
+ buffer = this.buffer;
76
+ this.discoCodec = new Codec(this.name, this.codecs);
77
+ this.discoCodec.fromName(this.name);
78
+ let hashAlg = this.discoCodec.hashAlg;
79
+ const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
80
+ if (hashAlg.includes('dbl')) {
81
+ hashAlg = hashAlg.replace('dbl-', '');
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);
95
+ }
96
+ this.size = this.digest.length;
97
+ this.codec = this.discoCodec.encode();
98
+ this.codec = this.discoCodec.codecBuffer;
99
+ const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
100
+ uint8Array.set(this.prefix);
101
+ uint8Array.set(this.digest, this.prefix.length);
102
+ this.encoded = uint8Array;
103
+ return this.encoded;
104
+ }
105
+ async validate(buffer) {
106
+ if (Buffer.isBuffer(buffer)) {
107
+ const codec = varint.decode(buffer);
108
+ if (this.codecs[codec]) {
109
+ this.decode(buffer);
110
+ }
111
+ else {
112
+ await this.encode(buffer);
113
+ }
114
+ }
115
+ if (typeof buffer === 'string') {
116
+ if (this.isHex(buffer))
117
+ this.fromHex(buffer);
118
+ if (this.isBase32(buffer))
119
+ this.fromBs32(buffer);
120
+ }
121
+ if (typeof buffer === 'object')
122
+ this.fromJSON(buffer);
123
+ }
124
+ decode(buffer) {
125
+ this.encoded = buffer;
126
+ const codec = varint.decode(buffer);
127
+ this.discoCodec = new Codec(codec, this.codecs);
128
+ // TODO: validate codec
129
+ buffer = buffer.slice(varint.decode.bytes);
130
+ this.size = varint.decode(buffer);
131
+ this.digest = buffer.slice(varint.decode.bytes);
132
+ if (this.digest.length !== this.size) {
133
+ throw new Error(`hash length inconsistent: 0x${this.encoded.toString('hex')}`);
134
+ }
135
+ // const discoCodec = new Codec(codec, this.codecs)
136
+ this.name = this.discoCodec.name;
137
+ this.size = this.digest.length;
138
+ return {
139
+ codec: this.codec,
140
+ name: this.name,
141
+ size: this.size,
142
+ length: this.length,
143
+ digest: this.digest,
144
+ };
145
+ }
146
+ }
147
+
148
+ export { CodecHash as default };
@@ -0,0 +1,99 @@
1
+ import varint from 'varint';
2
+ import { utils } from '@leofcoin/codecs';
3
+ import BasicInterface from './basic-interface.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
+
10
+ class Codec extends BasicInterface {
11
+ codecBuffer;
12
+ codec;
13
+ hashAlg;
14
+ constructor(buffer) {
15
+ super();
16
+ if (buffer) {
17
+ if (buffer instanceof Uint8Array) {
18
+ const codec = varint.decode(buffer);
19
+ const name = this.getCodecName(codec);
20
+ if (name) {
21
+ this.name = name;
22
+ this.encoded = buffer;
23
+ this.decode(buffer);
24
+ }
25
+ else {
26
+ this.encode(Number(new TextDecoder().decode(buffer)));
27
+ }
28
+ }
29
+ else if (buffer instanceof ArrayBuffer) {
30
+ const codec = varint.decode(buffer);
31
+ const name = this.getCodecName(codec);
32
+ if (name) {
33
+ this.name = name;
34
+ this.decode(buffer);
35
+ }
36
+ else {
37
+ this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
38
+ }
39
+ }
40
+ else if (typeof buffer === 'string') {
41
+ if (utils.getCodec(buffer))
42
+ this.fromName(buffer);
43
+ else if (this.isHex(buffer))
44
+ this.fromHex(buffer);
45
+ else if (this.isBase32(buffer))
46
+ this.fromBs32(buffer);
47
+ else if (this.isBase58(buffer))
48
+ this.fromBs58(buffer);
49
+ else
50
+ this.fromString(buffer);
51
+ }
52
+ if (!isNaN(buffer))
53
+ if (utils.getCodec(buffer))
54
+ this.fromCodec(buffer);
55
+ }
56
+ }
57
+ fromEncoded(encoded) {
58
+ const codec = varint.decode(encoded);
59
+ const name = this.getCodecName(codec);
60
+ this.name = name;
61
+ this.encoded = encoded;
62
+ return this.decode(encoded);
63
+ }
64
+ getCodec(name) {
65
+ return utils.getCodec(name);
66
+ }
67
+ getCodecName(codec) {
68
+ return utils.getCodecName(codec);
69
+ }
70
+ getHashAlg(name) {
71
+ return utils.getHashAlg(name);
72
+ }
73
+ fromCodec(codec) {
74
+ this.name = this.getCodecName(codec);
75
+ this.hashAlg = this.getHashAlg(this.name);
76
+ this.codec = this.getCodec(this.name);
77
+ this.codecBuffer = varint.encode(this.codec);
78
+ }
79
+ fromName(name) {
80
+ const codec = this.getCodec(name);
81
+ this.name = name;
82
+ this.codec = codec;
83
+ this.hashAlg = this.getHashAlg(name);
84
+ this.codecBuffer = varint.encode(this.codec);
85
+ }
86
+ decode(encoded) {
87
+ encoded = encoded || this.encoded;
88
+ const codec = varint.decode(encoded);
89
+ this.fromCodec(codec);
90
+ return this.decoded;
91
+ }
92
+ encode(codec) {
93
+ codec = codec || this.codec;
94
+ this.encoded = varint.encode(codec);
95
+ return this.encoded;
96
+ }
97
+ }
98
+
99
+ export { Codec as default };