@leofcoin/codec-format-interface 1.7.5 → 1.7.7
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.
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { base58String } from '@vandeurenglenn/base58';
|
|
2
|
+
import type { base32String } from '@vandeurenglenn/base32';
|
|
1
3
|
export default class BasicInterface {
|
|
2
4
|
#private;
|
|
3
|
-
keys: string[];
|
|
4
5
|
name: string;
|
|
6
|
+
get keys(): string[];
|
|
5
7
|
get encoded(): Uint8Array;
|
|
6
8
|
set encoded(value: Uint8Array);
|
|
7
9
|
get decoded(): object;
|
|
@@ -27,7 +29,7 @@ export default class BasicInterface {
|
|
|
27
29
|
/**
|
|
28
30
|
* @return {String} encoded
|
|
29
31
|
*/
|
|
30
|
-
toBs32():
|
|
32
|
+
toBs32(): base32String;
|
|
31
33
|
/**
|
|
32
34
|
* @return {String} encoded
|
|
33
35
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import base32 from '@vandeurenglenn/base32';
|
|
2
2
|
import base58 from '@vandeurenglenn/base58';
|
|
3
3
|
import isHex from '@vandeurenglenn/is-hex';
|
|
4
4
|
import proto from '@vandeurenglenn/proto-array';
|
|
@@ -7,9 +7,13 @@ import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/
|
|
|
7
7
|
class BasicInterface {
|
|
8
8
|
#encoded;
|
|
9
9
|
#decoded;
|
|
10
|
-
keys;
|
|
11
10
|
name;
|
|
12
11
|
#proto;
|
|
12
|
+
get keys() {
|
|
13
|
+
// handles proto keys
|
|
14
|
+
// protokey -> key
|
|
15
|
+
return Object.keys(this.#proto).map((key) => (key.endsWith('?') ? key.split('?')[0] : key));
|
|
16
|
+
}
|
|
13
17
|
get encoded() {
|
|
14
18
|
if (!this.#encoded)
|
|
15
19
|
this.#encoded = this.encode();
|
|
@@ -28,7 +32,6 @@ class BasicInterface {
|
|
|
28
32
|
}
|
|
29
33
|
set proto(value) {
|
|
30
34
|
this.#proto = value;
|
|
31
|
-
this.keys = Object.keys(value);
|
|
32
35
|
}
|
|
33
36
|
get proto() {
|
|
34
37
|
return this.#proto;
|
|
@@ -54,13 +57,13 @@ class BasicInterface {
|
|
|
54
57
|
return isHex(string);
|
|
55
58
|
}
|
|
56
59
|
isBase32(string) {
|
|
57
|
-
return
|
|
60
|
+
return base32.isBase32(string);
|
|
58
61
|
}
|
|
59
62
|
isBase58(string) {
|
|
60
63
|
return base58.isBase58(string);
|
|
61
64
|
}
|
|
62
65
|
fromBs32(encoded) {
|
|
63
|
-
return this.decode(
|
|
66
|
+
return this.decode(base32.decode(encoded));
|
|
64
67
|
}
|
|
65
68
|
fromBs58(encoded) {
|
|
66
69
|
return this.decode(fromBase58(encoded));
|
|
@@ -74,7 +77,7 @@ class BasicInterface {
|
|
|
74
77
|
}
|
|
75
78
|
fromString(string) {
|
|
76
79
|
const array = string.split(',');
|
|
77
|
-
const arrayLike = array.map(string => Number(string));
|
|
80
|
+
const arrayLike = array.map((string) => Number(string));
|
|
78
81
|
return this.decode(Uint8Array.from(arrayLike));
|
|
79
82
|
}
|
|
80
83
|
fromHex(string) {
|
|
@@ -94,7 +97,10 @@ class BasicInterface {
|
|
|
94
97
|
toHex() {
|
|
95
98
|
if (!this.encoded)
|
|
96
99
|
this.encode();
|
|
97
|
-
return toHex(this.encoded
|
|
100
|
+
return toHex(this.encoded
|
|
101
|
+
.toString()
|
|
102
|
+
.split(',')
|
|
103
|
+
.map((number) => Number(number)));
|
|
98
104
|
}
|
|
99
105
|
/**
|
|
100
106
|
* @return {String} encoded
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import BasicInterface from
|
|
2
|
-
import Hash from
|
|
1
|
+
import BasicInterface from "./basic-interface.js";
|
|
2
|
+
import Hash from "./codec-hash.js";
|
|
3
3
|
export default class FormatInterface extends BasicInterface implements FormatInterface {
|
|
4
4
|
#private;
|
|
5
5
|
hashFormat: string;
|
|
@@ -25,10 +25,10 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
25
25
|
* @return {peernetHash}
|
|
26
26
|
*/
|
|
27
27
|
hash(): Promise<any>;
|
|
28
|
-
fromUint8Array(buffer: any): object;
|
|
29
|
-
fromArrayBuffer(buffer: any): object;
|
|
28
|
+
fromUint8Array(buffer: any): void | object;
|
|
29
|
+
fromArrayBuffer(buffer: any): void | object;
|
|
30
30
|
/**
|
|
31
31
|
* @param {Object} data
|
|
32
32
|
*/
|
|
33
|
-
create(data: object):
|
|
33
|
+
create(data: object): void;
|
|
34
34
|
}
|
|
@@ -13,13 +13,13 @@ class FormatInterface extends BasicInterface {
|
|
|
13
13
|
hashFormat;
|
|
14
14
|
#hash;
|
|
15
15
|
init(buffer) {
|
|
16
|
-
if (buffer instanceof
|
|
16
|
+
if (buffer instanceof FormatInterface && buffer?.name === this.name)
|
|
17
|
+
return buffer;
|
|
18
|
+
else if (buffer instanceof Uint8Array)
|
|
17
19
|
this.fromUint8Array(buffer);
|
|
18
20
|
else if (buffer instanceof ArrayBuffer)
|
|
19
21
|
this.fromArrayBuffer(buffer);
|
|
20
|
-
else if (
|
|
21
|
-
return buffer;
|
|
22
|
-
else if (typeof buffer === 'string') {
|
|
22
|
+
else if (typeof buffer === "string") {
|
|
23
23
|
if (this.isHex(buffer))
|
|
24
24
|
this.fromHex(buffer);
|
|
25
25
|
else if (this.isBase58(buffer))
|
|
@@ -85,7 +85,7 @@ class FormatInterface extends BasicInterface {
|
|
|
85
85
|
constructor(buffer, proto, options) {
|
|
86
86
|
super();
|
|
87
87
|
this.proto = proto;
|
|
88
|
-
this.hashFormat = options?.hashFormat ? options.hashFormat :
|
|
88
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : "bs32";
|
|
89
89
|
if (options?.name)
|
|
90
90
|
this.name = options.name;
|
|
91
91
|
this.init(buffer);
|
|
@@ -116,17 +116,22 @@ class FormatInterface extends BasicInterface {
|
|
|
116
116
|
}
|
|
117
117
|
fromUint8Array(buffer) {
|
|
118
118
|
this.encoded = buffer;
|
|
119
|
-
return this.hasCodec()
|
|
119
|
+
return this.hasCodec()
|
|
120
|
+
? this.decode()
|
|
121
|
+
: this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
120
122
|
}
|
|
121
123
|
fromArrayBuffer(buffer) {
|
|
122
124
|
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
123
|
-
return this.hasCodec()
|
|
125
|
+
return this.hasCodec()
|
|
126
|
+
? this.decode()
|
|
127
|
+
: this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
124
128
|
}
|
|
125
129
|
/**
|
|
126
130
|
* @param {Object} data
|
|
127
131
|
*/
|
|
128
132
|
create(data) {
|
|
129
133
|
const decoded = {};
|
|
134
|
+
// @ts-ignore
|
|
130
135
|
if (data.hash)
|
|
131
136
|
this.#hash = data.hash;
|
|
132
137
|
if (this.keys?.length > 0) {
|