@leofcoin/peernet 0.11.24 → 0.11.25
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/browser/peernet.js +1861 -1847
- package/dist/commonjs/dht-response.js +2 -9
- package/dist/commonjs/dht.js +2 -9
- package/dist/commonjs/{http-b45d1330.js → http-78686629.js} +1 -1
- package/dist/commonjs/peernet-message.js +2 -9
- package/dist/commonjs/peernet.js +15 -20
- package/dist/commonjs/request.js +2 -9
- package/dist/commonjs/response.js +2 -9
- package/dist/module/peernet.js +6 -542
- package/package.json +2 -1
- package/rollup.config.js +2 -2
- package/src/handlers/message.js +1 -1
- package/src/messages/chat-message.js +2 -2
- package/src/messages/data-response.js +2 -2
- package/src/messages/data.js +2 -2
- package/src/messages/dht-response.js +2 -2
- package/src/messages/dht.js +2 -2
- package/src/messages/peer-response.js +2 -2
- package/src/messages/peer.js +2 -2
- package/src/messages/peernet-message.js +2 -2
- package/src/messages/ps.js +2 -2
- package/src/messages/request.js +2 -2
- package/src/messages/response.js +2 -2
- package/src/peernet.js +1 -2
- package/src/utils/utils.js +2 -1
- package/dist/commonjs/codec-45796010.js +0 -215
- package/dist/commonjs/codec-format-interface.js +0 -206
- package/dist/commonjs/codec.js +0 -11
- package/dist/commonjs/hash.js +0 -164
- package/src/codec/codec-format-interface.js +0 -194
- package/src/codec/codec.js +0 -124
- package/src/codec/codecs.js +0 -79
- package/src/hash/hash.js +0 -152
package/src/messages/peer.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/peer.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class PeerMessage extends
|
|
5
|
+
export default class PeerMessage extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['id']
|
|
8
8
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/peernet.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class PeernetMessage extends
|
|
5
|
+
export default class PeernetMessage extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['data', 'signature', 'from', 'to', 'id']
|
|
8
8
|
}
|
package/src/messages/ps.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/ps.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class PsMessage extends
|
|
5
|
+
export default class PsMessage extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['data', 'topic']
|
|
8
8
|
}
|
package/src/messages/request.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/request.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class RequestMessage extends
|
|
5
|
+
export default class RequestMessage extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['request']
|
|
8
8
|
}
|
package/src/messages/response.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import protons from 'protons'
|
|
2
2
|
import proto from './../proto/response.proto.js'
|
|
3
|
-
import
|
|
3
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
4
|
|
|
5
|
-
export default class ResponseMessage extends
|
|
5
|
+
export default class ResponseMessage extends FormatInterface {
|
|
6
6
|
get keys() {
|
|
7
7
|
return ['response']
|
|
8
8
|
}
|
package/src/peernet.js
CHANGED
|
@@ -15,8 +15,7 @@ import DataMessageResponse from './messages/data-response.js'
|
|
|
15
15
|
import ChatMessage from './messages/chat-message.js'
|
|
16
16
|
import PeerDiscovery from './discovery/peer-discovery'
|
|
17
17
|
import DHT from './dht/dht.js'
|
|
18
|
-
import Hash from '
|
|
19
|
-
import codecs from './codec/codecs'
|
|
18
|
+
import { CodecHash as Hash, codecs} from '@leofcoin/codec-format-interface'
|
|
20
19
|
import { protoFor, target } from './utils/utils.js'
|
|
21
20
|
import generateAccount from './../node_modules/@leofcoin/generate-account/dist/module/generate-account'
|
|
22
21
|
import MessageHandler from './handlers/message.js'
|
package/src/utils/utils.js
CHANGED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var varint = require('varint');
|
|
4
|
-
var bs32 = require('@vandeurenglenn/base32');
|
|
5
|
-
var bs58 = require('@vandeurenglenn/base58');
|
|
6
|
-
var isHex = require('@vandeurenglenn/is-hex');
|
|
7
|
-
|
|
8
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
-
|
|
10
|
-
var varint__default = /*#__PURE__*/_interopDefaultLegacy(varint);
|
|
11
|
-
var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
|
|
12
|
-
var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
|
|
13
|
-
var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
14
|
-
|
|
15
|
-
var codecs = {
|
|
16
|
-
// just a hash
|
|
17
|
-
'disco-hash': {
|
|
18
|
-
codec: parseInt('30', 16),
|
|
19
|
-
hashAlg: 'dbl-keccak-256', // ,
|
|
20
|
-
// testnet: 'olivia'
|
|
21
|
-
},
|
|
22
|
-
'peernet-peer-response': {
|
|
23
|
-
codec: parseInt('707072', 16),
|
|
24
|
-
hashAlg: 'keccak-256',
|
|
25
|
-
},
|
|
26
|
-
'peernet-peer': {
|
|
27
|
-
codec: parseInt('7070', 16),
|
|
28
|
-
hashAlg: 'keccak-256',
|
|
29
|
-
},
|
|
30
|
-
'peernet-dht': {
|
|
31
|
-
codec: parseInt('706468', 16),
|
|
32
|
-
hashAlg: 'keccak-256',
|
|
33
|
-
},
|
|
34
|
-
'peernet-dht-response': {
|
|
35
|
-
codec: parseInt('706472', 16),
|
|
36
|
-
hashAlg: 'keccak-256',
|
|
37
|
-
},
|
|
38
|
-
// data
|
|
39
|
-
'peernet-data': {
|
|
40
|
-
codec: parseInt('706461', 16),
|
|
41
|
-
hashAlg: 'keccak-256',
|
|
42
|
-
},
|
|
43
|
-
'peernet-data-response': {
|
|
44
|
-
codec: parseInt('70646172', 16),
|
|
45
|
-
hashAlg: 'keccak-256',
|
|
46
|
-
},
|
|
47
|
-
// message
|
|
48
|
-
'peernet-message': {
|
|
49
|
-
codec: parseInt('706d65', 16),
|
|
50
|
-
hashAlg: 'keccak-256',
|
|
51
|
-
},
|
|
52
|
-
// pubsub
|
|
53
|
-
'peernet-ps': {
|
|
54
|
-
codec: parseInt('707073', 16),
|
|
55
|
-
hashAlg: 'keccak-256',
|
|
56
|
-
},
|
|
57
|
-
'peernet-response': {
|
|
58
|
-
codec: parseInt('7072', 16),
|
|
59
|
-
hashAlg: 'keccak-256',
|
|
60
|
-
},
|
|
61
|
-
'peernet-request': {
|
|
62
|
-
codec: parseInt('707271', 16),
|
|
63
|
-
hashAlg: 'keccak-256',
|
|
64
|
-
},
|
|
65
|
-
// normal block
|
|
66
|
-
'leofcoin-block': {
|
|
67
|
-
codec: parseInt('6c62', 16),
|
|
68
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
69
|
-
// testnet: 'olivia'
|
|
70
|
-
},
|
|
71
|
-
'leofcoin-tx': {
|
|
72
|
-
codec: parseInt('6c74', 16),
|
|
73
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
74
|
-
// testnet: 'olivia'
|
|
75
|
-
},
|
|
76
|
-
// itx
|
|
77
|
-
'leofcoin-itx': {
|
|
78
|
-
codec: parseInt('6c69', 16),
|
|
79
|
-
hashAlg: 'keccak-512', // ,
|
|
80
|
-
// testnet: 'olivia'
|
|
81
|
-
},
|
|
82
|
-
// peer reputation
|
|
83
|
-
'leofcoin-pr': {
|
|
84
|
-
codec: parseInt('6c70', 16),
|
|
85
|
-
hashAlg: 'keccak-256', // ,
|
|
86
|
-
// testnet: 'olivia'
|
|
87
|
-
},
|
|
88
|
-
// chat message
|
|
89
|
-
'chat-message': {
|
|
90
|
-
codec: parseInt('636d', 16),
|
|
91
|
-
hashAlg: 'dbl-keccak-256',
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
class PeernetCodec {
|
|
96
|
-
get codecs() {
|
|
97
|
-
return {...globalThis.peernet.codecs, ...codecs}
|
|
98
|
-
}
|
|
99
|
-
constructor(buffer) {
|
|
100
|
-
if (buffer) {
|
|
101
|
-
if (buffer instanceof Uint8Array) {
|
|
102
|
-
const codec = varint__default["default"].decode(buffer);
|
|
103
|
-
const name = this.getCodecName(codec);
|
|
104
|
-
if (name) {
|
|
105
|
-
this.name = name;
|
|
106
|
-
this.encoded = buffer;
|
|
107
|
-
this.decode(buffer);
|
|
108
|
-
} else {
|
|
109
|
-
this.encode(buffer);
|
|
110
|
-
}
|
|
111
|
-
} else if (buffer instanceof ArrayBuffer) {
|
|
112
|
-
const encoded = new Uint8Array(buffer.byteLength);
|
|
113
|
-
|
|
114
|
-
for (let i = 0; i < buffer.byteLength; i++) {
|
|
115
|
-
encoded[i] = buffer[i];
|
|
116
|
-
}
|
|
117
|
-
this.encoded = encoded;
|
|
118
|
-
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
119
|
-
this.decode(buffer);
|
|
120
|
-
return
|
|
121
|
-
}
|
|
122
|
-
if (typeof buffer === 'string') {
|
|
123
|
-
if (this.codecs[buffer]) this.fromName(buffer);
|
|
124
|
-
else if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
125
|
-
else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
|
|
126
|
-
else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
|
|
127
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
128
|
-
}
|
|
129
|
-
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
fromEncoded(encoded) {
|
|
134
|
-
const codec = varint__default["default"].decode(encoded);
|
|
135
|
-
const name = this.getCodecName(codec);
|
|
136
|
-
this.name = name;
|
|
137
|
-
this.encoded = encoded;
|
|
138
|
-
this.decode(encoded);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
fromHex(hex) {
|
|
142
|
-
this.encoded = Buffer.from(hex, 'hex');
|
|
143
|
-
this.decode();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
fromBs32(input) {
|
|
147
|
-
this.encoded = bs32__default["default"].decode(input);
|
|
148
|
-
this.decode();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
fromBs58(input) {
|
|
152
|
-
this.encoded = bs58__default["default"].decode(input);
|
|
153
|
-
this.decode();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
getCodec(name) {
|
|
157
|
-
return this.codecs[name].codec
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
getCodecName(codec) {
|
|
161
|
-
return Object.keys(this.codecs).reduce((p, c) => {
|
|
162
|
-
const item = this.codecs[c];
|
|
163
|
-
if (item.codec === codec) return c;
|
|
164
|
-
else return p;
|
|
165
|
-
}, undefined)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
getHashAlg(name) {
|
|
169
|
-
return this.codecs[name].hashAlg
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
fromCodec(codec) {
|
|
173
|
-
this.name = this.getCodecName(codec);
|
|
174
|
-
this.hashAlg = this.getHashAlg(this.name);
|
|
175
|
-
|
|
176
|
-
this.codec = this.getCodec(this.name);
|
|
177
|
-
this.codecBuffer = varint__default["default"].encode(codec);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
fromName(name) {
|
|
181
|
-
const codec = this.getCodec(name);
|
|
182
|
-
this.name = name;
|
|
183
|
-
this.codec = codec;
|
|
184
|
-
this.hashAlg = this.getHashAlg(name);
|
|
185
|
-
this.codecBuffer = varint__default["default"].encode(codec);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
toBs32() {
|
|
189
|
-
this.encode();
|
|
190
|
-
return bs32__default["default"].encode(this.encoded)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
toBs58() {
|
|
194
|
-
this.encode();
|
|
195
|
-
return bs58__default["default"].encode(this.encoded)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
toHex() {
|
|
199
|
-
return this.encoded.toString('hex')
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
decode() {
|
|
203
|
-
const codec = varint__default["default"].decode(this.encoded);
|
|
204
|
-
this.fromCodec(codec);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
encode() {
|
|
208
|
-
const codec = varint__default["default"].encode(this.decoded);
|
|
209
|
-
this.encoded = codec;
|
|
210
|
-
return this.encoded
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
exports.PeernetCodec = PeernetCodec;
|
|
215
|
-
exports.codecs = codecs;
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var bs32 = require('@vandeurenglenn/base32');
|
|
4
|
-
var bs58 = require('@vandeurenglenn/base58');
|
|
5
|
-
var isHex = require('@vandeurenglenn/is-hex');
|
|
6
|
-
var codec = require('./codec-45796010.js');
|
|
7
|
-
var hash = require('./hash.js');
|
|
8
|
-
require('varint');
|
|
9
|
-
require('keccak');
|
|
10
|
-
|
|
11
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
-
|
|
13
|
-
var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
|
|
14
|
-
var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
|
|
15
|
-
var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
16
|
-
|
|
17
|
-
class FormatInterface {
|
|
18
|
-
/**
|
|
19
|
-
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
20
|
-
* @param {Object} proto - {encode, decode}
|
|
21
|
-
* @param {Object} options - {hashFormat, name}
|
|
22
|
-
*/
|
|
23
|
-
constructor(buffer, proto, options = {}) {
|
|
24
|
-
this.protoEncode = proto.encode;
|
|
25
|
-
this.protoDecode = proto.decode;
|
|
26
|
-
this.hashFormat = options.hashFormat || 'bs32';
|
|
27
|
-
if (options.name) this.name = options.name;
|
|
28
|
-
if (buffer instanceof Uint8Array) this.fromUint8Array(buffer);
|
|
29
|
-
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer);
|
|
30
|
-
else if (buffer.name === options.name) return buffer
|
|
31
|
-
else if (buffer instanceof String) {
|
|
32
|
-
if (isHex__default["default"](buffer)) this.fromHex(buffer);
|
|
33
|
-
else if (bs32__default["default"].isBase32(buffer)) this.fromBs32(buffer);
|
|
34
|
-
else if (bs58__default["default"].isBase58(buffer)) this.fromBs58(buffer);
|
|
35
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
36
|
-
} else {
|
|
37
|
-
this.create(buffer);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @return {PeernetHash}
|
|
43
|
-
*/
|
|
44
|
-
get peernetHash() {
|
|
45
|
-
return new hash(this.decoded, {name: this.name})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @return {peernetHash}
|
|
50
|
-
*/
|
|
51
|
-
get hash() {
|
|
52
|
-
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
53
|
-
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
54
|
-
return this.peernetHash[`to${format}`]()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @return {Object}
|
|
59
|
-
*/
|
|
60
|
-
decode() {
|
|
61
|
-
let encoded = this.encoded;
|
|
62
|
-
const discoCodec = new codec.PeernetCodec(this.encoded);
|
|
63
|
-
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
64
|
-
this.name = discoCodec.name;
|
|
65
|
-
this.decoded = this.protoDecode(encoded);
|
|
66
|
-
return this.decoded
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @return {Buffer}
|
|
71
|
-
*/
|
|
72
|
-
encode(decoded) {
|
|
73
|
-
if (!decoded) decoded = this.decoded;
|
|
74
|
-
const codec$1 = new codec.PeernetCodec(this.name);
|
|
75
|
-
const encoded = this.protoEncode(decoded);
|
|
76
|
-
const uint8Array = new Uint8Array(encoded.length + codec$1.codecBuffer.length);
|
|
77
|
-
uint8Array.set(codec$1.codecBuffer);
|
|
78
|
-
uint8Array.set(encoded, codec$1.codecBuffer.length);
|
|
79
|
-
this.encoded = uint8Array;
|
|
80
|
-
return this.encoded
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
hasCodec() {
|
|
84
|
-
if (!this.encoded) return false
|
|
85
|
-
const codec$1 = new codec.PeernetCodec(this.encoded);
|
|
86
|
-
if (codec$1.name) return true
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
fromUint8Array(buffer) {
|
|
90
|
-
this.encoded = buffer;
|
|
91
|
-
if (!this.hasCodec()) this.create(
|
|
92
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
93
|
-
);
|
|
94
|
-
else this.decode();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
fromArrayBuffer(buffer) {
|
|
98
|
-
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
99
|
-
if (!this.hasCodec()) this.create(
|
|
100
|
-
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
101
|
-
);
|
|
102
|
-
else this.decode();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
toString() {
|
|
106
|
-
return this.encoded.toString()
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async toArray() {
|
|
110
|
-
const array = [];
|
|
111
|
-
for await (const value of this.encoded.values()) {
|
|
112
|
-
array.push(value);
|
|
113
|
-
}
|
|
114
|
-
return array
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
fromString(string) {
|
|
118
|
-
this.encoded = new Uint8Array(string.split(','));
|
|
119
|
-
this.decode();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
fromArray(array) {
|
|
123
|
-
this.encoded = new Uint8Array([...array]);
|
|
124
|
-
this.decode();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* @param {Buffer} encoded
|
|
129
|
-
*/
|
|
130
|
-
fromEncoded(encoded) {
|
|
131
|
-
this.encoded = encoded;
|
|
132
|
-
this.decode();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @param {String} encoded
|
|
137
|
-
*/
|
|
138
|
-
fromHex(encoded) {
|
|
139
|
-
this.encoded = Buffer.from(encoded, 'hex');
|
|
140
|
-
this.decode();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* @param {String} encoded
|
|
145
|
-
*/
|
|
146
|
-
fromBs32(encoded) {
|
|
147
|
-
this.encoded = bs32__default["default"].decode(encoded);
|
|
148
|
-
this.decode();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @param {String} encoded
|
|
153
|
-
*/
|
|
154
|
-
fromBs58(encoded) {
|
|
155
|
-
this.encoded = bs58__default["default"].decode(encoded);
|
|
156
|
-
this.decode();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* @return {String} encoded
|
|
161
|
-
*/
|
|
162
|
-
toHex() {
|
|
163
|
-
if (!this.encoded) this.encode();
|
|
164
|
-
return this.encoded.toString('hex')
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* @return {String} encoded
|
|
169
|
-
*/
|
|
170
|
-
toBs32() {
|
|
171
|
-
if (!this.encoded) this.encode();
|
|
172
|
-
return bs32__default["default"].encode(this.encoded)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* @return {String} encoded
|
|
177
|
-
*/
|
|
178
|
-
toBs58() {
|
|
179
|
-
if (!this.encoded) this.encode();
|
|
180
|
-
return bs58__default["default"].encode(this.encoded)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* @param {Object} data
|
|
185
|
-
*/
|
|
186
|
-
create(data) {
|
|
187
|
-
const decoded = {};
|
|
188
|
-
if (this.keys?.length > 0) {
|
|
189
|
-
for (const key of this.keys) {
|
|
190
|
-
Object.defineProperties(decoded, {
|
|
191
|
-
[key]: {
|
|
192
|
-
enumerable: true,
|
|
193
|
-
configurable: true,
|
|
194
|
-
set: (val) => value = data[key],
|
|
195
|
-
get: () => data[key]
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
this.decoded = decoded;
|
|
201
|
-
this.encode();
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
module.exports = FormatInterface;
|
package/dist/commonjs/codec.js
DELETED
package/dist/commonjs/hash.js
DELETED
|
@@ -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;
|