@leofcoin/peernet 0.11.23 → 0.11.26

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,164 +0,0 @@
1
- 'use strict';
2
-
3
- var createKeccakHash = require('keccak');
4
- var varint = require('varint');
5
- var bs32 = require('@vandeurenglenn/base32');
6
- var bs58 = require('@vandeurenglenn/base58');
7
- var isHex = require('@vandeurenglenn/is-hex');
8
- var codec = require('./codec-45796010.js');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var createKeccakHash__default = /*#__PURE__*/_interopDefaultLegacy(createKeccakHash);
13
- var varint__default = /*#__PURE__*/_interopDefaultLegacy(varint);
14
- var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
15
- var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
16
- var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
17
-
18
- class PeernetHash {
19
- constructor(buffer, options = {}) {
20
- if (options.name) this.name = options.name;
21
- else this.name = 'disco-hash';
22
- if (options.codecs) this.codecs = options.codecs;
23
- if (buffer) {
24
- if (buffer instanceof Uint8Array) {
25
- this.discoCodec = new codec.PeernetCodec(buffer, this.codecs);
26
- const name = this.discoCodec.name;
27
-
28
- if (name) {
29
- this.name = name;
30
- this.decode(buffer);
31
- } else {
32
- this.encode(buffer);
33
- }
34
- }
35
-
36
- if (typeof buffer === 'string') {
37
- if (isHex__default["default"](buffer)) this.fromHex(buffer);
38
- if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
39
- else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
40
- else throw new Error(`unsupported string ${buffer}`)
41
- } else if (typeof buffer === 'object') this.fromJSON(buffer);
42
- }
43
- }
44
-
45
- get prefix() {
46
- const length = this.length;
47
- const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
48
- uint8Array.set(length);
49
- uint8Array.set(this.discoCodec.codecBuffer, length.length);
50
-
51
- return uint8Array
52
- }
53
-
54
- get length() {
55
- return varint__default["default"].encode(this.size)
56
- }
57
-
58
- get buffer() {
59
- return this.hash
60
- }
61
-
62
- toHex() {
63
- return this.hash.toString('hex')
64
- }
65
-
66
- fromHex(hex) {
67
- return this.decode(Buffer.from(hex, 'hex'))
68
- }
69
-
70
- fromJSON(json) {
71
- return this.encode(Buffer.from(JSON.stringify(json)))
72
- }
73
-
74
- toBs32() {
75
- return bs32__default["default"].encode(this.hash)
76
- }
77
-
78
- fromBs32(bs) {
79
- return this.decode(bs32__default["default"].decode(bs))
80
- }
81
-
82
- toBs58() {
83
- return bs58__default["default"].encode(this.hash)
84
- }
85
-
86
- fromBs58(bs) {
87
- return this.decode(bs58__default["default"].decode(bs))
88
- }
89
-
90
- toString(encoding = 'utf8') {
91
- return this.hash.toString(encoding)
92
- }
93
-
94
- encode(buffer, name) {
95
- if (!this.name && name) this.name = name;
96
- if (!buffer) buffer = this.buffer;
97
- this.discoCodec = new codec.PeernetCodec(this.name, this.codecs);
98
- this.discoCodec.fromName(this.name);
99
- let hashAlg = this.discoCodec.hashAlg;
100
- if (hashAlg.includes('dbl')) {
101
- hashAlg = hashAlg.replace('dbl-', '');
102
- buffer = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
103
- }
104
- this.digest = createKeccakHash__default["default"](hashAlg.replace('-', '')).update(buffer).digest();
105
- this.size = this.digest.length;
106
-
107
- this.codec = this.discoCodec.encode();
108
- this.codec = this.discoCodec.codecBuffer;
109
- const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
110
- uint8Array.set(this.prefix);
111
- uint8Array.set(this.digest, this.prefix.length);
112
-
113
- this.hash = uint8Array;
114
-
115
- return this.hash
116
- }
117
-
118
- validate(buffer) {
119
- if (Buffer.isBuffer(buffer)) {
120
- const codec = varint__default["default"].decode(buffer);
121
- if (this.codecs[codec]) {
122
- this.decode(buffer);
123
- } else {
124
- this.encode(buffer);
125
- }
126
- }
127
- if (typeof buffer === 'string') {
128
- if (isHex__default["default"](buffer)) this.fromHex(buffer);
129
- if (bs32__default["default"].test(buffer)) this.fromBs32(buffer);
130
- }
131
- if (typeof buffer === 'object') this.fromJSON(buffer);
132
- }
133
-
134
- decode(buffer) {
135
- this.hash = buffer;
136
- const codec$1 = varint__default["default"].decode(buffer);
137
-
138
- this.discoCodec = new codec.PeernetCodec(codec$1, this.codecs);
139
- // TODO: validate codec
140
- buffer = buffer.slice(varint__default["default"].decode.bytes);
141
- this.size = varint__default["default"].decode(buffer);
142
- this.digest = buffer.slice(varint__default["default"].decode.bytes);
143
- if (this.digest.length !== this.size) {
144
- throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
145
- }
146
-
147
- // const discoCodec = new Codec(codec, this.codecs)
148
-
149
- this.name = this.discoCodec.name;
150
-
151
-
152
- this.size = this.digest.length;
153
-
154
- return {
155
- codec: this.codec,
156
- name: this.name,
157
- size: this.size,
158
- length: this.length,
159
- digest: this.digest,
160
- }
161
- }
162
- }
163
-
164
- module.exports = PeernetHash;
@@ -1,194 +0,0 @@
1
- import bs32 from '@vandeurenglenn/base32';
2
- import bs58 from '@vandeurenglenn/base58';
3
- import isHex from '@vandeurenglenn/is-hex';
4
- import Codec from './codec';
5
- import Hash from './../hash/hash'
6
-
7
- export default class FormatInterface {
8
- /**
9
- * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
10
- * @param {Object} proto - {encode, decode}
11
- * @param {Object} options - {hashFormat, name}
12
- */
13
- constructor(buffer, proto, options = {}) {
14
- this.protoEncode = proto.encode
15
- this.protoDecode = proto.decode
16
- this.hashFormat = options.hashFormat || 'bs32'
17
- if (options.name) this.name = options.name
18
- if (buffer instanceof Uint8Array) this.fromUint8Array(buffer)
19
- else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer)
20
- else if (buffer.name === options.name) return buffer
21
- else if (buffer instanceof String) {
22
- if (isHex(buffer)) this.fromHex(buffer)
23
- else if (bs32.isBase32(buffer)) this.fromBs32(buffer)
24
- else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
25
- else throw new Error(`unsupported string ${buffer}`)
26
- } else {
27
- this.create(buffer)
28
- }
29
- }
30
-
31
- /**
32
- * @return {PeernetHash}
33
- */
34
- get peernetHash() {
35
- return new Hash(this.decoded, {name: this.name})
36
- }
37
-
38
- /**
39
- * @return {peernetHash}
40
- */
41
- get hash() {
42
- const upper = this.hashFormat.charAt(0).toUpperCase()
43
- const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`
44
- return this.peernetHash[`to${format}`]()
45
- }
46
-
47
- /**
48
- * @return {Object}
49
- */
50
- decode() {
51
- let encoded = this.encoded;
52
- const discoCodec = new Codec(this.encoded)
53
- encoded = encoded.slice(discoCodec.codecBuffer.length)
54
- this.name = discoCodec.name
55
- this.decoded = this.protoDecode(encoded)
56
- return this.decoded
57
- }
58
-
59
- /**
60
- * @return {Buffer}
61
- */
62
- encode(decoded) {
63
- if (!decoded) decoded = this.decoded;
64
- const codec = new Codec(this.name)
65
- const encoded = this.protoEncode(decoded)
66
- const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
67
- uint8Array.set(codec.codecBuffer)
68
- uint8Array.set(encoded, codec.codecBuffer.length)
69
- this.encoded = uint8Array
70
- return this.encoded
71
- }
72
-
73
- hasCodec() {
74
- if (!this.encoded) return false
75
- const codec = new Codec(this.encoded)
76
- if (codec.name) return true
77
- }
78
-
79
- fromUint8Array(buffer) {
80
- this.encoded = buffer
81
- if (!this.hasCodec()) this.create(
82
- JSON.parse(new TextDecoder().decode(this.encoded))
83
- )
84
- else this.decode()
85
- }
86
-
87
- fromArrayBuffer(buffer) {
88
- this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
89
- if (!this.hasCodec()) this.create(
90
- JSON.parse(new TextDecoder().decode(this.encoded))
91
- )
92
- else this.decode()
93
- }
94
-
95
- toString() {
96
- return this.encoded.toString()
97
- }
98
-
99
- async toArray() {
100
- const array = []
101
- for await (const value of this.encoded.values()) {
102
- array.push(value)
103
- }
104
- return array
105
- }
106
-
107
- fromString(string) {
108
- this.encoded = new Uint8Array(string.split(','))
109
- this.decode()
110
- }
111
-
112
- fromArray(array) {
113
- this.encoded = new Uint8Array([...array])
114
- this.decode()
115
- }
116
-
117
- /**
118
- * @param {Buffer} encoded
119
- */
120
- fromEncoded(encoded) {
121
- this.encoded = encoded
122
- this.decode()
123
- }
124
-
125
- /**
126
- * @param {String} encoded
127
- */
128
- fromHex(encoded) {
129
- this.encoded = Buffer.from(encoded, 'hex')
130
- this.decode()
131
- }
132
-
133
- /**
134
- * @param {String} encoded
135
- */
136
- fromBs32(encoded) {
137
- this.encoded = bs32.decode(encoded)
138
- this.decode()
139
- }
140
-
141
- /**
142
- * @param {String} encoded
143
- */
144
- fromBs58(encoded) {
145
- this.encoded = bs58.decode(encoded)
146
- this.decode()
147
- }
148
-
149
- /**
150
- * @return {String} encoded
151
- */
152
- toHex() {
153
- if (!this.encoded) this.encode()
154
- return this.encoded.toString('hex')
155
- }
156
-
157
- /**
158
- * @return {String} encoded
159
- */
160
- toBs32() {
161
- if (!this.encoded) this.encode()
162
- return bs32.encode(this.encoded)
163
- }
164
-
165
- /**
166
- * @return {String} encoded
167
- */
168
- toBs58() {
169
- if (!this.encoded) this.encode()
170
- return bs58.encode(this.encoded)
171
- }
172
-
173
- /**
174
- * @param {Object} data
175
- */
176
- create(data) {
177
- const decoded = {}
178
- if (this.keys?.length > 0) {
179
- for (const key of this.keys) {
180
- Object.defineProperties(decoded, {
181
- [key]: {
182
- enumerable: true,
183
- configurable: true,
184
- set: (val) => value = data[key],
185
- get: () => data[key]
186
- }
187
- })
188
- }
189
-
190
- this.decoded = decoded
191
- this.encode()
192
- }
193
- }
194
- }
@@ -1,124 +0,0 @@
1
- import varint from 'varint';
2
- import bs32 from '@vandeurenglenn/base32';
3
- import bs58 from '@vandeurenglenn/base58';
4
- import isHex from '@vandeurenglenn/is-hex';
5
- import codecs from './codecs'
6
-
7
- export default class PeernetCodec {
8
- get codecs() {
9
- return {...globalThis.peernet.codecs, ...codecs}
10
- }
11
- constructor(buffer) {
12
- if (buffer) {
13
- if (buffer instanceof Uint8Array) {
14
- const codec = varint.decode(buffer);
15
- const name = this.getCodecName(codec)
16
- if (name) {
17
- this.name = name
18
- this.encoded = buffer
19
- this.decode(buffer)
20
- } else {
21
- this.encode(buffer)
22
- }
23
- } else if (buffer instanceof ArrayBuffer) {
24
- const encoded = new Uint8Array(buffer.byteLength)
25
-
26
- for (let i = 0; i < buffer.byteLength; i++) {
27
- encoded[i] = buffer[i]
28
- }
29
- this.encoded = encoded
30
- // this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
31
- this.decode(buffer)
32
- return
33
- }
34
- if (typeof buffer === 'string') {
35
- if (this.codecs[buffer]) this.fromName(buffer)
36
- else if (isHex(buffer)) this.fromHex(buffer)
37
- else if (bs32.isBase32(buffer)) this.fromBs32(buffer)
38
- else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
39
- else throw new Error(`unsupported string ${buffer}`)
40
- }
41
- if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer)
42
- }
43
- }
44
-
45
- fromEncoded(encoded) {
46
- const codec = varint.decode(encoded);
47
- const name = this.getCodecName(codec)
48
- this.name = name
49
- this.encoded = encoded
50
- this.decode(encoded)
51
- }
52
-
53
- fromHex(hex) {
54
- this.encoded = Buffer.from(hex, 'hex')
55
- this.decode()
56
- }
57
-
58
- fromBs32(input) {
59
- this.encoded = bs32.decode(input)
60
- this.decode()
61
- }
62
-
63
- fromBs58(input) {
64
- this.encoded = bs58.decode(input)
65
- this.decode()
66
- }
67
-
68
- getCodec(name) {
69
- return this.codecs[name].codec
70
- }
71
-
72
- getCodecName(codec) {
73
- return Object.keys(this.codecs).reduce((p, c) => {
74
- const item = this.codecs[c]
75
- if (item.codec === codec) return c;
76
- else return p;
77
- }, undefined)
78
- }
79
-
80
- getHashAlg(name) {
81
- return this.codecs[name].hashAlg
82
- }
83
-
84
- fromCodec(codec) {
85
- this.name = this.getCodecName(codec)
86
- this.hashAlg = this.getHashAlg(this.name)
87
-
88
- this.codec = this.getCodec(this.name)
89
- this.codecBuffer = varint.encode(codec)
90
- }
91
-
92
- fromName(name) {
93
- const codec = this.getCodec(name)
94
- this.name = name
95
- this.codec = codec
96
- this.hashAlg = this.getHashAlg(name)
97
- this.codecBuffer = varint.encode(codec)
98
- }
99
-
100
- toBs32() {
101
- this.encode()
102
- return bs32.encode(this.encoded)
103
- }
104
-
105
- toBs58() {
106
- this.encode()
107
- return bs58.encode(this.encoded)
108
- }
109
-
110
- toHex() {
111
- return this.encoded.toString('hex')
112
- }
113
-
114
- decode() {
115
- const codec = varint.decode(this.encoded);
116
- this.fromCodec(codec)
117
- }
118
-
119
- encode() {
120
- const codec = varint.encode(this.decoded)
121
- this.encoded = codec
122
- return this.encoded
123
- }
124
- }
@@ -1,79 +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('636d', 16),
77
- hashAlg: 'dbl-keccak-256',
78
- },
79
- }
package/src/hash/hash.js DELETED
@@ -1,152 +0,0 @@
1
- import createKeccakHash from 'keccak';
2
- import varint from 'varint';
3
- import bs32 from '@vandeurenglenn/base32';
4
- import bs58 from '@vandeurenglenn/base58';
5
- import isHex from '@vandeurenglenn/is-hex';
6
- import Codec from './../codec/codec';
7
-
8
- export default class PeernetHash {
9
- constructor(buffer, options = {}) {
10
- if (options.name) this.name = options.name
11
- else this.name = 'disco-hash'
12
- if (options.codecs) this.codecs = options.codecs
13
- if (buffer) {
14
- if (buffer instanceof Uint8Array) {
15
- this.discoCodec = new Codec(buffer, this.codecs)
16
- const name = this.discoCodec.name
17
-
18
- if (name) {
19
- this.name = name
20
- this.decode(buffer)
21
- } else {
22
- this.encode(buffer)
23
- }
24
- }
25
-
26
- if (typeof buffer === 'string') {
27
- if (isHex(buffer)) this.fromHex(buffer)
28
- if (bs32.isBase32(buffer)) this.fromBs32(buffer)
29
- else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
30
- else throw new Error(`unsupported string ${buffer}`)
31
- } else if (typeof buffer === 'object') this.fromJSON(buffer)
32
- }
33
- }
34
-
35
- get prefix() {
36
- const length = this.length
37
- const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
38
- uint8Array.set(length)
39
- uint8Array.set(this.discoCodec.codecBuffer, length.length)
40
-
41
- return uint8Array
42
- }
43
-
44
- get length() {
45
- return varint.encode(this.size)
46
- }
47
-
48
- get buffer() {
49
- return this.hash
50
- }
51
-
52
- toHex() {
53
- return this.hash.toString('hex')
54
- }
55
-
56
- fromHex(hex) {
57
- return this.decode(Buffer.from(hex, 'hex'))
58
- }
59
-
60
- fromJSON(json) {
61
- return this.encode(Buffer.from(JSON.stringify(json)))
62
- }
63
-
64
- toBs32() {
65
- return bs32.encode(this.hash)
66
- }
67
-
68
- fromBs32(bs) {
69
- return this.decode(bs32.decode(bs))
70
- }
71
-
72
- toBs58() {
73
- return bs58.encode(this.hash)
74
- }
75
-
76
- fromBs58(bs) {
77
- return this.decode(bs58.decode(bs))
78
- }
79
-
80
- toString(encoding = 'utf8') {
81
- return this.hash.toString(encoding)
82
- }
83
-
84
- encode(buffer, name) {
85
- if (!this.name && name) this.name = name;
86
- if (!buffer) buffer = this.buffer;
87
- this.discoCodec = new Codec(this.name, this.codecs)
88
- this.discoCodec.fromName(this.name)
89
- let hashAlg = this.discoCodec.hashAlg
90
- if (hashAlg.includes('dbl')) {
91
- hashAlg = hashAlg.replace('dbl-', '')
92
- buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
93
- }
94
- this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
95
- this.size = this.digest.length
96
-
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
-
103
- this.hash = uint8Array
104
-
105
- return this.hash
106
- }
107
-
108
- validate(buffer) {
109
- if (Buffer.isBuffer(buffer)) {
110
- const codec = varint.decode(buffer);
111
- if (this.codecs[codec]) {
112
- this.decode(buffer)
113
- } else {
114
- this.encode(buffer)
115
- }
116
- }
117
- if (typeof buffer === 'string') {
118
- if (isHex(buffer)) this.fromHex(buffer)
119
- if (bs32.test(buffer)) this.fromBs32(buffer)
120
- }
121
- if (typeof buffer === 'object') this.fromJSON(buffer)
122
- }
123
-
124
- decode(buffer) {
125
- this.hash = buffer
126
- const codec = varint.decode(buffer);
127
-
128
- this.discoCodec = new Codec(codec, this.codecs)
129
- // TODO: validate codec
130
- buffer = buffer.slice(varint.decode.bytes);
131
- this.size = varint.decode(buffer);
132
- this.digest = buffer.slice(varint.decode.bytes);
133
- if (this.digest.length !== this.size) {
134
- throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
135
- }
136
-
137
- // const discoCodec = new Codec(codec, this.codecs)
138
-
139
- this.name = this.discoCodec.name
140
-
141
-
142
- this.size = this.digest.length
143
-
144
- return {
145
- codec: this.codec,
146
- name: this.name,
147
- size: this.size,
148
- length: this.length,
149
- digest: this.digest,
150
- }
151
- }
152
- }