@leofcoin/codec-format-interface 1.6.0 → 1.6.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.
@@ -1,123 +0,0 @@
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 { fromBase32, fromBase58, fromString, fromHex, fromArrayLike, fromUintArrayString, toBase32, toBase58, toHex } from '@vandeurenglenn/typed-array-utils'
6
-
7
-
8
- export default class BasicInterface {
9
- encoded: Uint8Array;
10
- decoded: object | string;
11
- keys: string[]
12
- name: string
13
- // get Codec(): Codec {}
14
-
15
- protoEncode(data: object): Uint8Array {
16
- // check schema
17
-
18
- return proto.encode(this.proto, data)
19
- }
20
-
21
- protoDecode(data: Uint8Array): object {
22
- // check schema
23
- return proto.decode(this.proto, data)
24
- }
25
-
26
- isHex(string: any) {
27
- return isHex(string)
28
- }
29
- isBase32(string: any) {
30
- return bs32.isBase32(string)
31
- }
32
- isBase58(string: any) {
33
- return base58.isBase58(string)
34
- }
35
-
36
- fromBs32(encoded: base58String): object {
37
- this.encoded = bs32.decode(encoded)
38
- return this.decode()
39
- }
40
-
41
-
42
- fromBs58(encoded: any): object {
43
- this.encoded = fromBase58(encoded)
44
- return this.decode()
45
- }
46
-
47
- async toArray() {
48
- const array: number[] = []
49
- for await (const value of this.encoded.values()) {
50
- array.push(value)
51
- }
52
- return array
53
- }
54
-
55
- fromString(string: string): object {
56
- const array: string[] = string.split(',')
57
- const arrayLike = array.map(string => Number(string))
58
- this.encoded = Uint8Array.from(arrayLike)
59
- return this.decode()
60
- }
61
-
62
- fromHex(string) {
63
- this.encoded = fromHex(string)
64
- return this.decode()
65
- }
66
-
67
- fromArray(array: number[]): object {
68
- this.encoded = Uint8Array.from([...array])
69
- return this.decode()
70
- }
71
-
72
- fromEncoded(encoded: Uint8Array) {
73
- this.encoded = encoded
74
- return this.decode()
75
- }
76
-
77
- toString(): string {
78
- if (!this.encoded) this.encode()
79
- return this.encoded.toString()
80
- }
81
-
82
- toHex() {
83
- if (!this.encoded) this.encode()
84
- return toHex(this.encoded)
85
- }
86
-
87
- /**
88
- * @return {String} encoded
89
- */
90
- toBs32() {
91
- if (!this.encoded) this.encode()
92
- return toBase32(this.encoded)
93
- }
94
-
95
- /**
96
- * @return {String} encoded
97
- */
98
- toBs58() {
99
- if (!this.encoded) this.encode()
100
- return toBase58(this.encoded)
101
- }
102
-
103
- /**
104
- * @param {Object} data
105
- */
106
- create(data: object) {
107
- const decoded = {}
108
- if (this.keys?.length > 0) {
109
- for (const key of this.keys) {
110
- Object.defineProperties(decoded, {
111
- [key]: {
112
- enumerable: true,
113
- configurable: true,
114
- set: (value) => value = data[key],
115
- get: () => data[key]
116
- }
117
- })
118
- }
119
- this.decoded = decoded
120
- return this.encode()
121
- }
122
- }
123
- }
@@ -1,116 +0,0 @@
1
- import BasicInterface from './basic-interface.js'
2
- import Hash from './codec-hash.js'
3
- import Codec from './codec.js';
4
-
5
- export default class FormatInterface extends BasicInterface implements FormatInterface {
6
-
7
- set proto(value) {
8
- this._proto = value
9
- this.keys = Object.keys(this._proto)
10
- }
11
-
12
- get proto() {
13
- return this._proto
14
- }
15
- init(buffer) {
16
- if (buffer instanceof Uint8Array) this.fromUint8Array(buffer)
17
- else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer)
18
- else if (buffer?.name === this.name) return buffer
19
- else if (buffer instanceof String) {
20
- if (this.isHex(buffer)) this.fromHex(buffer)
21
- else if (this.isBase58(buffer)) this.fromBs58(buffer)
22
- else if (this.isBase32(buffer)) this.fromBs32(buffer)
23
- else throw new Error(`unsupported ${buffer}`)
24
- } else {
25
- this.create(buffer)
26
- }
27
- return this
28
- }
29
-
30
- hasCodec() {
31
- if (!this.encoded) return false
32
- const codec = new Codec(this.encoded)
33
- if (codec.name) return true
34
- }
35
-
36
- decode() {
37
- let encoded = this.encoded;
38
- const codec = new Codec(this.encoded)
39
- if (codec.codecBuffer) {
40
- encoded = encoded.slice(codec.codecBuffer.length)
41
- this.name = codec.name
42
- this.decoded = this.protoDecode(encoded)
43
- try {
44
- this.decoded = JSON.parse(this.decoded)
45
- } catch {
46
-
47
- }
48
- } else {
49
- throw new Error(`no codec found`)
50
- }
51
-
52
- return this.decoded
53
- }
54
-
55
-
56
- encode(decoded?: object) {
57
- let encoded: Uint8Array
58
- if (!decoded) decoded = this.decoded;
59
- const codec = new Codec(this.name)
60
-
61
- if (decoded instanceof Uint8Array) encoded = decoded
62
- else encoded = this.protoEncode(decoded)
63
-
64
- if (codec.codecBuffer) {
65
- const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
66
- uint8Array.set(codec.codecBuffer)
67
- uint8Array.set(encoded, codec.codecBuffer.length)
68
- this.encoded = uint8Array
69
- } else {
70
- throw new Error(`invalid codec`)
71
- }
72
- return this.encoded
73
- }
74
- /**
75
- * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
76
- * @param {Object} proto - {protoObject}
77
- * @param {Object} options - {hashFormat, name}
78
- */
79
- constructor(buffer, proto, options = {}) {
80
- super()
81
- this.proto = proto
82
- this.hashFormat = options.hashFormat || 'bs32'
83
- if (options.name) this.name = options.name
84
- this.init(buffer)
85
- }
86
-
87
- /**
88
- * @return {PeernetHash}
89
- */
90
- get peernetHash() {
91
- return new Hash(this.decoded, {name: this.name})
92
- }
93
-
94
- /**
95
- * @return {peernetHash}
96
- */
97
- async hash() {
98
- const upper = this.hashFormat.charAt(0).toUpperCase()
99
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`
100
- return (await this.peernetHash)[`to${format}`]()
101
- }
102
-
103
- fromUint8Array(buffer) {
104
- this.encoded = buffer
105
- return this.hasCodec() ? this.decode() : this.create(
106
- JSON.parse(new TextDecoder().decode(this.encoded))
107
- )
108
- }
109
-
110
- fromArrayBuffer(buffer) {
111
- this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
112
- return this.hasCodec() ? this.decode() : this.create(
113
- JSON.parse(new TextDecoder().decode(this.encoded))
114
- )
115
- }
116
- }
package/src/codec-hash.js DELETED
@@ -1,139 +0,0 @@
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
-
6
- export default 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
- }
package/src/codec.ts DELETED
@@ -1,94 +0,0 @@
1
- import varint from 'varint';
2
- import codecs from './codecs.js'
3
- import BasicInterface from './basic-interface.js'
4
-
5
- export default class Codec extends BasicInterface {
6
- get codecs() {
7
- return {...globalThis.peernet.codecs, ...codecs}
8
- }
9
- constructor(buffer) {
10
- super()
11
- if (buffer) {
12
- if (buffer instanceof Uint8Array) {
13
- const codec = varint.decode(buffer);
14
- const name = this.getCodecName(codec)
15
- if (name) {
16
- this.name = name
17
- this.encoded = buffer
18
- this.decode(buffer)
19
- } else {
20
- this.encode(buffer)
21
- }
22
- } else if (buffer instanceof ArrayBuffer) {
23
- const encoded = new Uint8Array(buffer.byteLength)
24
-
25
- for (let i = 0; i < buffer.byteLength; i++) {
26
- encoded[i] = buffer[i]
27
- }
28
- this.encoded = encoded
29
- // this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
30
- this.decode(buffer)
31
- return
32
- }
33
- if (typeof buffer === 'string') {
34
- if (this.codecs[buffer]) this.fromName(buffer)
35
- else if (this.isHex(buffer)) this.fromHex(buffer)
36
- else if (this.isBase32(buffer)) this.fromBs32(buffer)
37
- else if (this.isBase58(buffer)) this.fromBs58(buffer)
38
- else throw new Error(`unsupported string ${buffer}`)
39
- }
40
- if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer)
41
- }
42
- }
43
-
44
- fromEncoded(encoded) {
45
- const codec = varint.decode(encoded);
46
- const name = this.getCodecName(codec)
47
- this.name = name
48
- this.encoded = encoded
49
- this.decode(encoded)
50
- }
51
-
52
- getCodec(name) {
53
- return this.codecs[name].codec
54
- }
55
-
56
- getCodecName(codec) {
57
- return Object.keys(this.codecs).reduce((p, c) => {
58
- const item = this.codecs[c]
59
- if (item.codec === codec) return c;
60
- else return p;
61
- }, undefined)
62
- }
63
-
64
- getHashAlg(name) {
65
- return this.codecs[name].hashAlg
66
- }
67
-
68
- fromCodec(codec) {
69
- this.name = this.getCodecName(codec)
70
- this.hashAlg = this.getHashAlg(this.name)
71
-
72
- this.codec = this.getCodec(this.name)
73
- this.codecBuffer = varint.encode(codec)
74
- }
75
-
76
- fromName(name) {
77
- const codec = this.getCodec(name)
78
- this.name = name
79
- this.codec = codec
80
- this.hashAlg = this.getHashAlg(name)
81
- this.codecBuffer = varint.encode(codec)
82
- }
83
-
84
- decode() {
85
- const codec = varint.decode(this.encoded);
86
- this.fromCodec(codec)
87
- }
88
-
89
- encode() {
90
- const codec = varint.encode(this.decoded)
91
- this.encoded = codec
92
- return this.encoded
93
- }
94
- }
package/src/codecs.js DELETED
@@ -1,87 +0,0 @@
1
- export default {
2
- // just a hash
3
- 'disco-hash': {
4
- codec: parseInt('30', 16),
5
- hashAlg: 'dbl-keccak-256', // ,
6
- // testnet: 'olivia'
7
- },
8
- 'peernet-peer-response': {
9
- codec: parseInt('707072', 16),
10
- hashAlg: 'keccak-256',
11
- },
12
- 'peernet-peer': {
13
- codec: parseInt('7070', 16),
14
- hashAlg: 'keccak-256',
15
- },
16
- 'peernet-dht': {
17
- codec: parseInt('706468', 16),
18
- hashAlg: 'keccak-256',
19
- },
20
- 'peernet-dht-response': {
21
- codec: parseInt('706472', 16),
22
- hashAlg: 'keccak-256',
23
- },
24
- // data
25
- 'peernet-data': {
26
- codec: parseInt('706461', 16),
27
- hashAlg: 'keccak-256',
28
- },
29
- 'peernet-data-response': {
30
- codec: parseInt('70646172', 16),
31
- hashAlg: 'keccak-256',
32
- },
33
- // message
34
- 'peernet-message': {
35
- codec: parseInt('706d65', 16),
36
- hashAlg: 'keccak-256',
37
- },
38
- // pubsub
39
- 'peernet-ps': {
40
- codec: parseInt('707073', 16),
41
- hashAlg: 'keccak-256',
42
- },
43
- 'peernet-response': {
44
- codec: parseInt('7072', 16),
45
- hashAlg: 'keccak-256',
46
- },
47
- 'peernet-request': {
48
- codec: parseInt('707271', 16),
49
- hashAlg: 'keccak-256',
50
- },
51
- // normal block
52
- 'leofcoin-block': {
53
- codec: parseInt('6c62', 16),
54
- hashAlg: 'dbl-keccak-512', // ,
55
- // testnet: 'olivia'
56
- },
57
- 'leofcoin-tx': {
58
- codec: parseInt('6c74', 16),
59
- hashAlg: 'dbl-keccak-512', // ,
60
- // testnet: 'olivia'
61
- },
62
- // itx
63
- 'leofcoin-itx': {
64
- codec: parseInt('6c69', 16),
65
- hashAlg: 'keccak-512', // ,
66
- // testnet: 'olivia'
67
- },
68
- // peer reputation
69
- 'leofcoin-pr': {
70
- codec: parseInt('6c70', 16),
71
- hashAlg: 'keccak-256', // ,
72
- // testnet: 'olivia'
73
- },
74
- // chat message
75
- 'chat-message': {
76
- codec: parseInt('70636d', 16),
77
- hashAlg: 'dbl-keccak-256',
78
- },
79
- 'peernet-file' : {
80
- codec: parseInt('7066', 16),
81
- hashAlg: 'keccak-256',
82
- },
83
- 'peernet-file-response' : {
84
- codec: parseInt('706672', 16),
85
- hashAlg: 'keccak-256',
86
- }
87
- }
package/src/index.ts DELETED
@@ -1,10 +0,0 @@
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 * as codecs from './codecs.js'
6
-
7
- export const BasicInterface = basicInterface
8
- export const FormatInterface = formatInterface
9
- export const CodecHash = codecHash
10
- export const Codec = codec
package/test/index.js DELETED
@@ -1,38 +0,0 @@
1
- import test from 'tape'
2
- import {FormatInterface, codecs} from './../dist/index.js'
3
-
4
- globalThis.peernet = {codecs: {}}
5
- class FormatTest extends FormatInterface {
6
- get messageName() { return 'Message' }
7
-
8
- constructor(data) {
9
- super(data, {
10
- somedata: 'test'
11
- } , {name: 'disco-hash'})
12
- }
13
- }
14
-
15
- const encoded = [48, 10, 5, 104, 101, 108, 108, 111]
16
-
17
- test('format', async (tape) => {
18
- tape.plan(2)
19
- const message = new FormatTest({somedata: 'hello'})
20
-
21
- const m2 = new FormatTest(message.encoded)
22
-
23
- tape.ok(message.encoded, 'can encode')
24
- tape.ok(m2.decoded.somedata === 'hello', 'can decode')
25
- })
26
-
27
- test('format can hash', async (tape) => {
28
- tape.plan(1)
29
- const message = new FormatTest({somedata: 'hello'})
30
- const hash = await message.hash()
31
- tape.ok(hash, 'can hash')
32
-
33
- })
34
-
35
- test('has codecs', async (tape) => {
36
- tape.plan(1)
37
- tape.ok(Object.keys(codecs).length !== 0)
38
- })
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "./dist",
4
- "esModuleInterop": true,
5
- "allowSyntheticDefaultImports": true,
6
- "allowJs": true,
7
- "target": "es2022",
8
- "module": "es2022",
9
- "moduleResolution": "nodeNext",
10
- "declaration": true
11
- },
12
- "include": [
13
- "./src/**/*"
14
- ],
15
- "exclude": [
16
- "./node_modules/**/*"
17
- ]
18
- }
@@ -1,5 +0,0 @@
1
-
2
- declare interface base58 {
3
- ALPHABET: string;
4
- encode: () => base58String;
5
- }
@@ -1,48 +0,0 @@
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
- }
@@ -1,5 +0,0 @@
1
- import { BasicInterface } from "../dist";
2
-
3
- declare class FormatInterface extends BasicInterface {
4
- decode: () => Promise<object | string>
5
- }
@@ -1,15 +0,0 @@
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
- }