@leofcoin/codec-format-interface 1.5.0 → 1.5.2
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/dist/index.js +1104 -5179
- package/package.json +4 -3
- package/src/basic-interface.ts +126 -0
- package/src/{codec-format-interface.js → codec-format-interface.ts} +48 -57
- package/src/codec-hash.js +31 -19
- package/src/{codec.js → codec.ts} +1 -1
- package/src/index.ts +1 -2
- package/test/index.js +16 -3
- package/tsconfig.js +2 -1
- package/typings/Base58.d.ts +5 -0
- package/typings/BasicInterface.d.ts +48 -0
- package/typings/FormatInterface.d.ts +5 -0
- package/typings/index.d.ts +15 -0
- package/src/basic-interface.js +0 -123
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/codec-format-interface",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"typings": "typings/basicInterface.d.ts",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
10
|
"build": "rollup -c"
|
|
@@ -21,9 +22,9 @@
|
|
|
21
22
|
"homepage": "https://github.com/leofcoin/codec-format-interface#readme",
|
|
22
23
|
"dependencies": {
|
|
23
24
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
24
|
-
"@vandeurenglenn/base58": "^1.1.
|
|
25
|
+
"@vandeurenglenn/base58": "^1.1.1",
|
|
25
26
|
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
26
|
-
"
|
|
27
|
+
"hash-wasm": "^4.9.0",
|
|
27
28
|
"varint": "^6.0.0"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import bs32 from '@vandeurenglenn/base32';
|
|
2
|
+
import base58 from '@vandeurenglenn/base58';
|
|
3
|
+
import isHex from '@vandeurenglenn/is-hex';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export default class BasicInterface implements basicInterface {
|
|
7
|
+
encoded: Uint8Array;
|
|
8
|
+
decoded: object | string;
|
|
9
|
+
keys: string[]
|
|
10
|
+
name: string
|
|
11
|
+
// get Codec(): Codec {}
|
|
12
|
+
|
|
13
|
+
async protoEncode(data: string): Promise<Uint8Array> {
|
|
14
|
+
// check schema
|
|
15
|
+
return new TextEncoder().encode(data)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async protoDecode(data: Uint8Array): Promise<string> {
|
|
19
|
+
// check schema
|
|
20
|
+
return new TextDecoder().decode(data)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
decode: () => DecodeResult;
|
|
24
|
+
|
|
25
|
+
encode:() => Promise<Uint8Array>;
|
|
26
|
+
|
|
27
|
+
isHex(string: any) {
|
|
28
|
+
return isHex(string)
|
|
29
|
+
}
|
|
30
|
+
isBase32(string: any) {
|
|
31
|
+
return bs32.isBase32(string)
|
|
32
|
+
}
|
|
33
|
+
isBase58(string: any) {
|
|
34
|
+
return base58.isBase58(string)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fromBs32(encoded: base58): DecodeResult {
|
|
38
|
+
this.encoded = bs32.decode(encoded)
|
|
39
|
+
return this.decode()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
fromBs58(encoded: any): DecodeResult {
|
|
44
|
+
this.encoded = base58.decode(encoded)
|
|
45
|
+
return this.decode()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async toArray() {
|
|
49
|
+
const array: number[] = []
|
|
50
|
+
for await (const value of this.encoded.values()) {
|
|
51
|
+
array.push(value)
|
|
52
|
+
}
|
|
53
|
+
return array
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fromString(string: string): object {
|
|
57
|
+
const array: string[] = string.split(',')
|
|
58
|
+
const arrayLike = array.map(string => Number(string))
|
|
59
|
+
this.encoded = Uint8Array.from(arrayLike)
|
|
60
|
+
return this.decode()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fromArray(array: number[]): object {
|
|
64
|
+
this.encoded = Uint8Array.from([...array])
|
|
65
|
+
return this.decode()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fromEncoded(encoded: Uint8Array) {
|
|
69
|
+
this.encoded = encoded
|
|
70
|
+
return this.decode()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fromHex(encoded: string): Promise<string | object> {
|
|
74
|
+
this.encoded = Buffer.from(encoded, 'hex')
|
|
75
|
+
return this.decode()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async toString(encoding: string = 'utf8'): Promise<string> {
|
|
79
|
+
if (!this.encoded) await this.encode()
|
|
80
|
+
return this.encoded.toString(encoding)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @return {String} encoded
|
|
85
|
+
*/
|
|
86
|
+
toHex() {
|
|
87
|
+
return this.toString('hex')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @return {String} encoded
|
|
92
|
+
*/
|
|
93
|
+
async toBs32() {
|
|
94
|
+
if (!this.encoded) await this.encode()
|
|
95
|
+
return bs32.encode(this.encoded)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @return {String} encoded
|
|
100
|
+
*/
|
|
101
|
+
async toBs58() {
|
|
102
|
+
if (!this.encoded) await this.encode()
|
|
103
|
+
return base58.encode(this.encoded)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {Object} data
|
|
108
|
+
*/
|
|
109
|
+
create(data: object) {
|
|
110
|
+
const decoded = {}
|
|
111
|
+
if (this.keys?.length > 0) {
|
|
112
|
+
for (const key of this.keys) {
|
|
113
|
+
Object.defineProperties(decoded, {
|
|
114
|
+
[key]: {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
set: (value) => value = data[key],
|
|
118
|
+
get: () => data[key]
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
this.decoded = decoded
|
|
123
|
+
return this.encode()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
import BasicInterface from './basic-interface.js'
|
|
2
|
-
import Codec from './codec.js';
|
|
3
2
|
import Hash from './codec-hash.js'
|
|
3
|
+
import Codec from './codec.js';
|
|
4
4
|
|
|
5
|
-
export default class FormatInterface extends BasicInterface {
|
|
6
|
-
|
|
7
|
-
async protoEncode(data) {
|
|
8
|
-
// check schema
|
|
9
|
-
return new TextEncoder().encode(data)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async protoDecode(data) {
|
|
13
|
-
// check schema
|
|
14
|
-
return new TextDecoder().decode(data)
|
|
15
|
-
}
|
|
16
|
-
|
|
5
|
+
export default class FormatInterface extends BasicInterface implements FormatInterface {
|
|
17
6
|
async init(buffer) {
|
|
18
7
|
if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer)
|
|
19
8
|
else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer)
|
|
@@ -29,6 +18,50 @@ export default class FormatInterface extends BasicInterface {
|
|
|
29
18
|
return this
|
|
30
19
|
}
|
|
31
20
|
|
|
21
|
+
hasCodec() {
|
|
22
|
+
if (!this.encoded) return false
|
|
23
|
+
const codec = new Codec(this.encoded)
|
|
24
|
+
if (codec.name) return true
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async decode() {
|
|
28
|
+
let encoded = this.encoded;
|
|
29
|
+
const codec = new Codec(this.encoded)
|
|
30
|
+
if (codec.codecBuffer) {
|
|
31
|
+
encoded = encoded.slice(codec.codecBuffer.length)
|
|
32
|
+
this.name = codec.name
|
|
33
|
+
this.decoded = await this.protoDecode(encoded)
|
|
34
|
+
try {
|
|
35
|
+
this.decoded = JSON.parse(this.decoded)
|
|
36
|
+
} catch {
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error(`no codec found`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.decoded
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
async encode(decoded?: object | string) {
|
|
48
|
+
let encoded: Uint8Array
|
|
49
|
+
if (!decoded) decoded = this.decoded;
|
|
50
|
+
const codec = new Codec(this.name)
|
|
51
|
+
|
|
52
|
+
if (decoded instanceof Uint8Array) encoded = decoded
|
|
53
|
+
else encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded)
|
|
54
|
+
|
|
55
|
+
if (codec.codecBuffer) {
|
|
56
|
+
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
|
|
57
|
+
uint8Array.set(codec.codecBuffer)
|
|
58
|
+
uint8Array.set(encoded, codec.codecBuffer.length)
|
|
59
|
+
this.encoded = uint8Array
|
|
60
|
+
} else {
|
|
61
|
+
throw new Error(`invalid codec`)
|
|
62
|
+
}
|
|
63
|
+
return this.encoded
|
|
64
|
+
}
|
|
32
65
|
/**
|
|
33
66
|
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
34
67
|
* @param {Object} proto - {encode, decode}
|
|
@@ -52,52 +85,10 @@ export default class FormatInterface extends BasicInterface {
|
|
|
52
85
|
/**
|
|
53
86
|
* @return {peernetHash}
|
|
54
87
|
*/
|
|
55
|
-
|
|
88
|
+
async hash() {
|
|
56
89
|
const upper = this.hashFormat.charAt(0).toUpperCase()
|
|
57
90
|
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`
|
|
58
|
-
return this.peernetHash[`to${format}`]()
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @return {Object}
|
|
63
|
-
*/
|
|
64
|
-
async decode() {
|
|
65
|
-
let encoded = this.encoded;
|
|
66
|
-
const discoCodec = new Codec(this.encoded)
|
|
67
|
-
encoded = encoded.slice(discoCodec.codecBuffer.length)
|
|
68
|
-
this.name = discoCodec.name
|
|
69
|
-
this.decoded = await this.protoDecode(encoded)
|
|
70
|
-
try {
|
|
71
|
-
this.decoded = JSON.parse(this.decoded)
|
|
72
|
-
} catch {
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
return this.decoded
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* @return {Buffer}
|
|
80
|
-
*/
|
|
81
|
-
async encode(decoded) {
|
|
82
|
-
let encoded
|
|
83
|
-
if (!decoded) decoded = this.decoded;
|
|
84
|
-
const codec = new Codec(this.name)
|
|
85
|
-
|
|
86
|
-
if (decoded instanceof Uint8Array) encoded = decoded
|
|
87
|
-
else encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded)
|
|
88
|
-
|
|
89
|
-
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
|
|
90
|
-
uint8Array.set(codec.codecBuffer)
|
|
91
|
-
uint8Array.set(encoded, codec.codecBuffer.length)
|
|
92
|
-
|
|
93
|
-
this.encoded = uint8Array
|
|
94
|
-
return this.encoded
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
hasCodec() {
|
|
98
|
-
if (!this.encoded) return false
|
|
99
|
-
const codec = new Codec(this.encoded)
|
|
100
|
-
if (codec.name) return true
|
|
91
|
+
return (await this.peernetHash)[`to${format}`]()
|
|
101
92
|
}
|
|
102
93
|
|
|
103
94
|
fromUint8Array(buffer) {
|
package/src/codec-hash.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {createKeccak} from 'hash-wasm';
|
|
2
2
|
import varint from 'varint';
|
|
3
3
|
import BasicInterface from './basic-interface.js'
|
|
4
|
-
import Codec from './codec';
|
|
4
|
+
import Codec from './codec.js';
|
|
5
5
|
|
|
6
6
|
export default class CodecHash extends BasicInterface {
|
|
7
7
|
constructor(buffer, options = {}) {
|
|
@@ -9,28 +9,32 @@ export default class CodecHash extends BasicInterface {
|
|
|
9
9
|
if (options.name) this.name = options.name
|
|
10
10
|
else this.name = 'disco-hash'
|
|
11
11
|
if (options.codecs) this.codecs = options.codecs
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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)
|
|
15
19
|
const name = this.discoCodec.name
|
|
16
20
|
|
|
17
21
|
if (name) {
|
|
18
22
|
this.name = name
|
|
19
|
-
this.decode(
|
|
23
|
+
this.decode(uint8Array)
|
|
20
24
|
} else {
|
|
21
|
-
this.encode(
|
|
25
|
+
await this.encode(uint8Array)
|
|
22
26
|
}
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
if (typeof
|
|
26
|
-
if (this.isHex(
|
|
27
|
-
if (this.isBase32(
|
|
28
|
-
else if (this.isBase58(
|
|
29
|
-
else throw new Error(`unsupported string ${
|
|
30
|
-
} else if (typeof
|
|
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)
|
|
31
35
|
}
|
|
36
|
+
return this
|
|
32
37
|
}
|
|
33
|
-
|
|
34
38
|
get prefix() {
|
|
35
39
|
const length = this.length
|
|
36
40
|
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
|
|
@@ -56,17 +60,25 @@ export default class CodecHash extends BasicInterface {
|
|
|
56
60
|
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
57
61
|
}
|
|
58
62
|
|
|
59
|
-
encode(buffer, name) {
|
|
63
|
+
async encode(buffer, name) {
|
|
60
64
|
if (!this.name && name) this.name = name;
|
|
61
65
|
if (!buffer) buffer = this.buffer;
|
|
62
66
|
this.discoCodec = new Codec(this.name, this.codecs)
|
|
63
67
|
this.discoCodec.fromName(this.name)
|
|
64
68
|
let hashAlg = this.discoCodec.hashAlg
|
|
69
|
+
const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1])
|
|
70
|
+
|
|
65
71
|
if (hashAlg.includes('dbl')) {
|
|
66
72
|
hashAlg = hashAlg.replace('dbl-', '')
|
|
67
|
-
|
|
73
|
+
const hasher = await createKeccak(hashVariant)
|
|
74
|
+
await hasher.init()
|
|
75
|
+
hasher.update(buffer)
|
|
76
|
+
buffer = hasher.digest('binary')
|
|
68
77
|
}
|
|
69
|
-
|
|
78
|
+
const hasher = await createKeccak(hashVariant)
|
|
79
|
+
await hasher.init()
|
|
80
|
+
hasher.update(buffer)
|
|
81
|
+
this.digest = hasher.digest('binary')
|
|
70
82
|
this.size = this.digest.length
|
|
71
83
|
|
|
72
84
|
this.codec = this.discoCodec.encode();
|
|
@@ -80,13 +92,13 @@ export default class CodecHash extends BasicInterface {
|
|
|
80
92
|
return this.encoded
|
|
81
93
|
}
|
|
82
94
|
|
|
83
|
-
validate(buffer) {
|
|
95
|
+
async validate(buffer) {
|
|
84
96
|
if (Buffer.isBuffer(buffer)) {
|
|
85
97
|
const codec = varint.decode(buffer);
|
|
86
98
|
if (this.codecs[codec]) {
|
|
87
99
|
this.decode(buffer)
|
|
88
100
|
} else {
|
|
89
|
-
this.encode(buffer)
|
|
101
|
+
await this.encode(buffer)
|
|
90
102
|
}
|
|
91
103
|
}
|
|
92
104
|
if (typeof buffer === 'string') {
|
|
@@ -2,7 +2,7 @@ import varint from 'varint';
|
|
|
2
2
|
import codecs from './codecs.js'
|
|
3
3
|
import BasicInterface from './basic-interface.js'
|
|
4
4
|
|
|
5
|
-
export default class
|
|
5
|
+
export default class Codec extends BasicInterface {
|
|
6
6
|
get codecs() {
|
|
7
7
|
return {...globalThis.peernet.codecs, ...codecs}
|
|
8
8
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,10 +2,9 @@ import basicInterface from './basic-interface.js'
|
|
|
2
2
|
import formatInterface from './codec-format-interface.js'
|
|
3
3
|
import codecHash from './codec-hash.js'
|
|
4
4
|
import codec from './codec.js'
|
|
5
|
-
|
|
5
|
+
export * as codecs from './codecs.js'
|
|
6
6
|
|
|
7
7
|
export const BasicInterface = basicInterface
|
|
8
8
|
export const FormatInterface = formatInterface
|
|
9
9
|
export const CodecHash = codecHash
|
|
10
10
|
export const Codec = codec
|
|
11
|
-
export const Codecs = codecs
|
package/test/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import test from 'tape'
|
|
2
|
-
import {FormatInterface} from './../dist/index.js'
|
|
2
|
+
import {FormatInterface, codecs} from './../dist/index.js'
|
|
3
3
|
|
|
4
4
|
globalThis.peernet = {codecs: {}}
|
|
5
5
|
class FormatTest extends FormatInterface {
|
|
@@ -18,9 +18,22 @@ const encoded = [48, 10, 5, 104, 101, 108, 108, 111]
|
|
|
18
18
|
test('format', async (tape) => {
|
|
19
19
|
tape.plan(2)
|
|
20
20
|
const message = await new FormatTest({somedata: 'hello'})
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
const m2 = await new FormatTest(message.encoded)
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
tape.ok(message.encoded, 'can encode')
|
|
25
25
|
tape.ok(m2.decoded.somedata === 'hello', 'can decode')
|
|
26
26
|
})
|
|
27
|
+
|
|
28
|
+
test('format can hash', async (tape) => {
|
|
29
|
+
tape.plan(1)
|
|
30
|
+
const message = await new FormatTest({somedata: 'hello'})
|
|
31
|
+
const hash = await message.hash()
|
|
32
|
+
tape.ok(hash, 'can hash')
|
|
33
|
+
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('has codecs', async (tape) => {
|
|
37
|
+
tape.plan(1)
|
|
38
|
+
tape.ok(Object.keys(codecs).length !== 0)
|
|
39
|
+
})
|
package/tsconfig.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
type DecodeResult = Promise<string | object>;
|
|
3
|
+
|
|
4
|
+
declare interface basicInterface {
|
|
5
|
+
encoded: Uint8Array;
|
|
6
|
+
decoded: object | string;
|
|
7
|
+
name: string;
|
|
8
|
+
protoDecode: (encoded: Uint8Array) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* @return Promise(resolve: Uint8Array)
|
|
11
|
+
*/
|
|
12
|
+
encode: (decoded?: object | string) => Promise<Uint8Array>;
|
|
13
|
+
/**
|
|
14
|
+
* @return {Promise(resolve: object|string)}
|
|
15
|
+
*/
|
|
16
|
+
decode: () => Promise<object | string>;
|
|
17
|
+
toString: (encoding: string) => Promise<string>;
|
|
18
|
+
keys: string[];
|
|
19
|
+
toArray: () => Promise<number[]>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {Uint8Array} source
|
|
23
|
+
*/
|
|
24
|
+
fromEncoded: (source: Uint8Array) => Promise<object | string>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {String} encoded
|
|
28
|
+
*/
|
|
29
|
+
fromHex: (source: string) => Promise<object | string>;
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
isHex(string): boolean;
|
|
33
|
+
isBase32(string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param {base58} string
|
|
37
|
+
*/
|
|
38
|
+
isBase58(string: base58): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* @param {base58} encoded
|
|
41
|
+
*/
|
|
42
|
+
fromBs32(encoded: base58): Promise<string | object>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {base58} encoded
|
|
46
|
+
*/
|
|
47
|
+
fromBs58(encoded: base58): Promise<string | object>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare interface base58String {
|
|
2
|
+
|
|
3
|
+
}
|
|
4
|
+
declare class Codec {
|
|
5
|
+
construct():any
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare const codecs: object
|
|
9
|
+
|
|
10
|
+
declare module '@leofcoin/code-format-interface' {
|
|
11
|
+
export module basicInterface {}
|
|
12
|
+
export module formatInterface {}
|
|
13
|
+
export module codec {}
|
|
14
|
+
export module codecs {}
|
|
15
|
+
}
|
package/src/basic-interface.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import bs32 from '@vandeurenglenn/base32';
|
|
2
|
-
import base58 from '@vandeurenglenn/base58';
|
|
3
|
-
import isHex from '@vandeurenglenn/is-hex';
|
|
4
|
-
|
|
5
|
-
export default class BasicInterface {
|
|
6
|
-
handleDecode() {
|
|
7
|
-
if (!this.decode) throw new Error('bad implementation: needs decode func')
|
|
8
|
-
return this.decode()
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
handleEncode() {
|
|
12
|
-
if (!this.encode) throw new Error('bad implementation: needs encode func')
|
|
13
|
-
return this.encode()
|
|
14
|
-
}
|
|
15
|
-
isHex(string) {
|
|
16
|
-
return isHex(string)
|
|
17
|
-
}
|
|
18
|
-
isBase32(string) {
|
|
19
|
-
return bs32.isBase32(string)
|
|
20
|
-
}
|
|
21
|
-
isBase58(string) {
|
|
22
|
-
return base58.isBase58(string)
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* @param {String} encoded
|
|
26
|
-
*/
|
|
27
|
-
fromBs32(encoded) {
|
|
28
|
-
this.encoded = bs32.decode(encoded)
|
|
29
|
-
return this.handleDecode()
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param {String} encoded
|
|
34
|
-
*/
|
|
35
|
-
frombase58(encoded) {
|
|
36
|
-
this.encoded = base58.decode(encoded)
|
|
37
|
-
return this.handleDecode()
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async toArray() {
|
|
41
|
-
const array = []
|
|
42
|
-
for await (const value of this.encoded.values()) {
|
|
43
|
-
array.push(value)
|
|
44
|
-
}
|
|
45
|
-
return array
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
fromString(string) {
|
|
49
|
-
this.encoded = new Uint8Array(string.split(','))
|
|
50
|
-
return this.handleDecode()
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
fromArray(array) {
|
|
54
|
-
this.encoded = new Uint8Array([...array])
|
|
55
|
-
return this.handleDecode()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @param {Buffer} encoded
|
|
60
|
-
*/
|
|
61
|
-
fromEncoded(encoded) {
|
|
62
|
-
this.encoded = encoded
|
|
63
|
-
return this.handleDecode()
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @param {String} encoded
|
|
68
|
-
*/
|
|
69
|
-
fromHex(encoded) {
|
|
70
|
-
this.encoded = Buffer.from(encoded, 'hex')
|
|
71
|
-
return this.handleDecode()
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async toString(encoding = 'utf8') {
|
|
75
|
-
if (!this.encoded) await this.handleEncode()
|
|
76
|
-
return this.encoded.toString(encoding)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @return {String} encoded
|
|
81
|
-
*/
|
|
82
|
-
toHex() {
|
|
83
|
-
return this.toString('hex')
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @return {String} encoded
|
|
88
|
-
*/
|
|
89
|
-
async toBs32() {
|
|
90
|
-
if (!this.encoded) await this.handleEncode()
|
|
91
|
-
return bs32.encode(this.encoded)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* @return {String} encoded
|
|
96
|
-
*/
|
|
97
|
-
async tobase58() {
|
|
98
|
-
if (!this.encoded) await this.handleEncode()
|
|
99
|
-
return base58.encode(this.encoded)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @param {Object} data
|
|
104
|
-
*/
|
|
105
|
-
create(data) {
|
|
106
|
-
const decoded = {}
|
|
107
|
-
if (this.keys?.length > 0) {
|
|
108
|
-
for (const key of this.keys) {
|
|
109
|
-
Object.defineProperties(decoded, {
|
|
110
|
-
[key]: {
|
|
111
|
-
enumerable: true,
|
|
112
|
-
configurable: true,
|
|
113
|
-
set: (val) => value = data[key],
|
|
114
|
-
get: () => data[key]
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
this.decoded = decoded
|
|
120
|
-
return this.encode()
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|