@leofcoin/peernet 0.10.7 → 0.11.1
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/LICENSE +1 -1
- package/README.md +49 -49
- package/coverage/lcov-report/block-navigation.js +8 -0
- package/coverage/lcov-report/codec-format-interface.js.html +224 -120
- package/coverage/lcov-report/dht-response.js.html +44 -39
- package/coverage/lcov-report/index.html +39 -64
- package/coverage/lcov-report/sorter.js +26 -0
- package/coverage/lcov.info +164 -424
- package/dist/browser/peernet.js +101788 -93018
- package/dist/commonjs/client-1a1f75e6.js +324 -0
- package/dist/commonjs/{codec-6367213c.js → codec-8c8c652f.js} +198 -188
- package/dist/commonjs/codec-format-interface.js +169 -152
- package/dist/commonjs/codec.js +4 -4
- package/dist/commonjs/dht-response.js +13 -13
- package/dist/commonjs/dht.js +24 -24
- package/dist/commonjs/hash.js +151 -141
- package/dist/commonjs/{http-a94c5a81.js → http-7bbac90a.js} +19 -15
- package/dist/commonjs/peernet-message.js +13 -13
- package/dist/commonjs/peernet.js +1901 -1794
- package/dist/commonjs/request.js +13 -13
- package/dist/commonjs/response.js +13 -13
- package/dist/module/peernet.js +2460 -2346
- package/index.html +4 -6
- package/package.json +22 -14
- package/rollup.config.js +33 -5
- package/rollup0.config.js +7 -0
- package/src/client.js +75 -75
- package/src/codec/codec-format-interface.js +172 -155
- package/src/codec/codec.js +124 -114
- package/src/codec/codecs.js +79 -79
- package/src/dht/dht.js +121 -121
- package/src/discovery/peer-discovery.js +75 -75
- package/src/handlers/message.js +52 -52
- package/src/hash/hash.js +155 -145
- package/src/http/client/http-client.js +44 -44
- package/src/messages/chat-message.js +14 -14
- package/src/messages/data-response.js +14 -14
- package/src/messages/data.js +18 -18
- package/src/messages/dht-response.js +14 -15
- package/src/messages/dht.js +25 -25
- package/src/messages/peer-response.js +14 -14
- package/src/messages/peer.js +14 -14
- package/src/messages/peernet-message.js +14 -14
- package/src/messages/ps.js +14 -14
- package/src/messages/request.js +14 -14
- package/src/messages/response.js +14 -14
- package/src/peer.js +67 -67
- package/src/peernet.js +612 -697
- package/src/proto/chat-message.proto.js +7 -7
- package/src/utils/utils.js +78 -78
- package/test/codec.js +3 -2
- package/test/messages.js +7 -4
- package/test.js +11 -4
- package/webpack.config.js +35 -0
- package/coverage/lcov-report/codec.js.html +0 -677
- package/coverage/lcov-report/hash.js.html +0 -551
- package/debug.log +0 -3
- package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
- package/dist/codec/codec-format-interface.js +0 -433
- package/dist/codec/codec.js +0 -199
- package/dist/commonjs/codec-73adfc0f.js +0 -205
- package/dist/commonjs/http-2c603501.js +0 -324
- package/dist/commonjs/http-42a6e555.js +0 -324
- package/dist/commonjs/http-43f4fafe.js +0 -324
- package/dist/commonjs/peernet-message-b6925673.js +0 -32
- package/dist/hash/hash.js +0 -340
- package/dist/messages/dht-response.js +0 -454
- package/dist/messages/dht.js +0 -453
- package/dist/messages/peernet.js +0 -456
- package/dist/module/http-273664bd.js +0 -317
- package/dist/module/http-8fe3c0d7.js +0 -317
- package/dist/module/http-c780c991.js +0 -317
- package/dist/module/http-f13e0d77.js +0 -317
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { protoFor } from './../utils/utils.js'
|
|
2
|
-
|
|
3
|
-
export default class PeerDiscovery {
|
|
4
|
-
constructor(id) {
|
|
5
|
-
this.id = id
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
_getPeerId(id) {
|
|
9
|
-
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0) return false
|
|
10
|
-
|
|
11
|
-
for (const entry of [...peernet.peerMap.entries()]) {
|
|
12
|
-
for (const _id of entry[1]) {
|
|
13
|
-
if (_id === id) return entry[0]
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async discover(peer) {
|
|
19
|
-
let id = this._getPeerId(peer.id)
|
|
20
|
-
if (id) return id
|
|
21
|
-
const data = new peernet.protos['peernet-peer']({id: this.id})
|
|
22
|
-
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
|
-
|
|
24
|
-
let response = await peer.request(node.encoded)
|
|
25
|
-
response = protoFor(response)
|
|
26
|
-
response = new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
|
-
|
|
28
|
-
id = response.decoded.id
|
|
29
|
-
if (id === this.id) return;
|
|
30
|
-
|
|
31
|
-
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id])
|
|
32
|
-
else {
|
|
33
|
-
const connections = peernet.peerMap.get(id)
|
|
34
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
35
|
-
connections.push(peer.id)
|
|
36
|
-
peernet.peerMap.set(peer.id, connections)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return id
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async discoverHandler(message, peer) {
|
|
43
|
-
const {id, proto} = message
|
|
44
|
-
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
45
|
-
if (proto.name === 'peernet-peer') {
|
|
46
|
-
const from = proto.decoded.id
|
|
47
|
-
if (from === this.id) return;
|
|
48
|
-
|
|
49
|
-
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
50
|
-
else {
|
|
51
|
-
const connections = peernet.peerMap.get(from)
|
|
52
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
53
|
-
connections.push(peer.id)
|
|
54
|
-
peernet.peerMap.set(from, connections)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const data = new peernet.protos['peernet-peer-response']({id: this.id})
|
|
58
|
-
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
|
-
|
|
60
|
-
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
61
|
-
} else if (proto.name === 'peernet-peer-response') {
|
|
62
|
-
const from = proto.decoded.id
|
|
63
|
-
if (from === this.id) return;
|
|
64
|
-
|
|
65
|
-
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
66
|
-
else {
|
|
67
|
-
const connections = peernet.peerMap.get(from)
|
|
68
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
69
|
-
connections.push(peer.id)
|
|
70
|
-
peernet.peerMap.set(from, connections)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
1
|
+
import { protoFor } from './../utils/utils.js'
|
|
2
|
+
|
|
3
|
+
export default class PeerDiscovery {
|
|
4
|
+
constructor(id) {
|
|
5
|
+
this.id = id
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
_getPeerId(id) {
|
|
9
|
+
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0) return false
|
|
10
|
+
|
|
11
|
+
for (const entry of [...peernet.peerMap.entries()]) {
|
|
12
|
+
for (const _id of entry[1]) {
|
|
13
|
+
if (_id === id) return entry[0]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async discover(peer) {
|
|
19
|
+
let id = this._getPeerId(peer.id)
|
|
20
|
+
if (id) return id
|
|
21
|
+
const data = new peernet.protos['peernet-peer']({id: this.id})
|
|
22
|
+
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
|
+
|
|
24
|
+
let response = await peer.request(node.encoded)
|
|
25
|
+
response = protoFor(response)
|
|
26
|
+
response = new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
|
+
|
|
28
|
+
id = response.decoded.id
|
|
29
|
+
if (id === this.id) return;
|
|
30
|
+
|
|
31
|
+
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id])
|
|
32
|
+
else {
|
|
33
|
+
const connections = peernet.peerMap.get(id)
|
|
34
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
35
|
+
connections.push(peer.id)
|
|
36
|
+
peernet.peerMap.set(peer.id, connections)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return id
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async discoverHandler(message, peer) {
|
|
43
|
+
const {id, proto} = message
|
|
44
|
+
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
45
|
+
if (proto.name === 'peernet-peer') {
|
|
46
|
+
const from = proto.decoded.id
|
|
47
|
+
if (from === this.id) return;
|
|
48
|
+
|
|
49
|
+
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
50
|
+
else {
|
|
51
|
+
const connections = peernet.peerMap.get(from)
|
|
52
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
53
|
+
connections.push(peer.id)
|
|
54
|
+
peernet.peerMap.set(from, connections)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const data = new peernet.protos['peernet-peer-response']({id: this.id})
|
|
58
|
+
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
|
+
|
|
60
|
+
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
61
|
+
} else if (proto.name === 'peernet-peer-response') {
|
|
62
|
+
const from = proto.decoded.id
|
|
63
|
+
if (from === this.id) return;
|
|
64
|
+
|
|
65
|
+
if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id])
|
|
66
|
+
else {
|
|
67
|
+
const connections = peernet.peerMap.get(from)
|
|
68
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
69
|
+
connections.push(peer.id)
|
|
70
|
+
peernet.peerMap.set(from, connections)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/handlers/message.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import MultiWallet from '
|
|
2
|
-
import Hash from './../hash/hash.js'
|
|
3
|
-
import PeernetMessage from './../messages/peernet-message.js'
|
|
4
|
-
|
|
5
|
-
export default class MessageHandler {
|
|
6
|
-
constructor(network) {
|
|
7
|
-
this.network = network
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* hash and sign message
|
|
11
|
-
*
|
|
12
|
-
* @param {object} message
|
|
13
|
-
* @param {Buffer} message.from peer id
|
|
14
|
-
* @param {Buffer} message.to peer id
|
|
15
|
-
* @param {string} message.data Peernet message
|
|
16
|
-
* (PeernetMessage excluded) encoded as a string
|
|
17
|
-
* @return signature
|
|
18
|
-
*/
|
|
19
|
-
async hashAndSignMessage(message) {
|
|
20
|
-
const hasher = new Hash(message, {name: 'peernet-message'})
|
|
21
|
-
const identity = await walletStore.get('identity')
|
|
22
|
-
|
|
23
|
-
const wallet = new MultiWallet(this.network)
|
|
24
|
-
wallet.import(identity.multiWIF)
|
|
25
|
-
return wallet.sign(hasher.hash.slice(0, 32))
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @param {String} from - peer id
|
|
30
|
-
* @param {String} to - peer id
|
|
31
|
-
* @param {String|PeernetMessage} data - data encoded message string
|
|
32
|
-
* or the messageNode itself
|
|
33
|
-
*/
|
|
34
|
-
async prepareMessage(from, to, data) {
|
|
35
|
-
if (!Buffer.isBuffer(from)) from = new Buffer.from(from)
|
|
36
|
-
if (!Buffer.isBuffer(to)) to = new Buffer.from(to)
|
|
37
|
-
if (data.encoded) data = data.encoded
|
|
38
|
-
|
|
39
|
-
const message = {
|
|
40
|
-
from,
|
|
41
|
-
to,
|
|
42
|
-
data,
|
|
43
|
-
}
|
|
44
|
-
const signature = await this.hashAndSignMessage(message)
|
|
45
|
-
const node = new PeernetMessage({
|
|
46
|
-
...message,
|
|
47
|
-
signature,
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
return node
|
|
51
|
-
}
|
|
52
|
-
}
|
|
1
|
+
import MultiWallet from './../../node_modules/@leofcoin/multi-wallet/src/index'
|
|
2
|
+
import Hash from './../hash/hash.js'
|
|
3
|
+
import PeernetMessage from './../messages/peernet-message.js'
|
|
4
|
+
|
|
5
|
+
export default class MessageHandler {
|
|
6
|
+
constructor(network) {
|
|
7
|
+
this.network = network
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* hash and sign message
|
|
11
|
+
*
|
|
12
|
+
* @param {object} message
|
|
13
|
+
* @param {Buffer} message.from peer id
|
|
14
|
+
* @param {Buffer} message.to peer id
|
|
15
|
+
* @param {string} message.data Peernet message
|
|
16
|
+
* (PeernetMessage excluded) encoded as a string
|
|
17
|
+
* @return signature
|
|
18
|
+
*/
|
|
19
|
+
async hashAndSignMessage(message) {
|
|
20
|
+
const hasher = new Hash(message, {name: 'peernet-message'})
|
|
21
|
+
const identity = await walletStore.get('identity')
|
|
22
|
+
|
|
23
|
+
const wallet = new MultiWallet(this.network)
|
|
24
|
+
wallet.import(identity.multiWIF)
|
|
25
|
+
return wallet.sign(hasher.hash.slice(0, 32))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {String} from - peer id
|
|
30
|
+
* @param {String} to - peer id
|
|
31
|
+
* @param {String|PeernetMessage} data - data encoded message string
|
|
32
|
+
* or the messageNode itself
|
|
33
|
+
*/
|
|
34
|
+
async prepareMessage(from, to, data, id) {
|
|
35
|
+
if (!Buffer.isBuffer(from)) from = new Buffer.from(from)
|
|
36
|
+
if (!Buffer.isBuffer(to)) to = new Buffer.from(to)
|
|
37
|
+
if (data.encoded) data = data.encoded
|
|
38
|
+
|
|
39
|
+
const message = {
|
|
40
|
+
from,
|
|
41
|
+
to,
|
|
42
|
+
data,
|
|
43
|
+
}
|
|
44
|
+
const signature = await this.hashAndSignMessage(message)
|
|
45
|
+
const node = new PeernetMessage({
|
|
46
|
+
...message,
|
|
47
|
+
signature,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return node
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/hash/hash.js
CHANGED
|
@@ -1,145 +1,155 @@
|
|
|
1
|
-
import createKeccakHash from 'keccak';
|
|
2
|
-
import varint from 'varint';
|
|
3
|
-
import bs32 from '
|
|
4
|
-
import bs58 from '
|
|
5
|
-
import isHex from '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.isBuffer(buffer)) {
|
|
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.
|
|
29
|
-
else this.fromBs58(buffer)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
this.
|
|
90
|
-
|
|
91
|
-
this.
|
|
92
|
-
this.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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.isBuffer(buffer)) {
|
|
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
|
+
for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
|
|
39
|
+
uint8Array[i] = this.discoCodec.codecBuffer[i]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
43
|
+
uint8Array[i] = length[i]
|
|
44
|
+
}
|
|
45
|
+
return uint8Array
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get length() {
|
|
49
|
+
return varint.encode(this.size)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get buffer() {
|
|
53
|
+
return this.hash
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toHex() {
|
|
57
|
+
return this.hash.toString('hex')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fromHex(hex) {
|
|
61
|
+
return this.decode(Buffer.from(hex, 'hex'))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fromJSON(json) {
|
|
65
|
+
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
toBs32() {
|
|
69
|
+
return bs32.encode(this.hash)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fromBs32(bs) {
|
|
73
|
+
return this.decode(bs32.decode(bs))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
toBs58() {
|
|
77
|
+
return bs58.encode(this.hash)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fromBs58(bs) {
|
|
81
|
+
return this.decode(bs58.decode(bs))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
toString(encoding = 'utf8') {
|
|
85
|
+
return this.hash.toString(encoding)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
encode(buffer, name) {
|
|
89
|
+
if (!this.name && name) this.name = name;
|
|
90
|
+
if (!buffer) buffer = this.buffer;
|
|
91
|
+
this.discoCodec = new Codec(this.name, this.codecs)
|
|
92
|
+
this.discoCodec.fromName(this.name)
|
|
93
|
+
let hashAlg = this.discoCodec.hashAlg
|
|
94
|
+
if (hashAlg.includes('dbl')) {
|
|
95
|
+
hashAlg = hashAlg.replace('dbl-', '')
|
|
96
|
+
buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
|
|
97
|
+
}
|
|
98
|
+
this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
|
|
99
|
+
this.size = this.digest.length
|
|
100
|
+
|
|
101
|
+
this.codec = this.discoCodec.encode();
|
|
102
|
+
this.codec = this.discoCodec.codecBuffer
|
|
103
|
+
this.hash = Buffer.concat([
|
|
104
|
+
this.prefix,
|
|
105
|
+
this.digest,
|
|
106
|
+
])
|
|
107
|
+
|
|
108
|
+
return this.hash
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
validate(buffer) {
|
|
112
|
+
if (Buffer.isBuffer(buffer)) {
|
|
113
|
+
const codec = varint.decode(buffer);
|
|
114
|
+
if (this.codecs[codec]) {
|
|
115
|
+
this.decode(buffer)
|
|
116
|
+
} else {
|
|
117
|
+
this.encode(buffer)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (typeof buffer === 'string') {
|
|
121
|
+
if (isHex(buffer)) this.fromHex(buffer)
|
|
122
|
+
if (bs32.test(buffer)) this.fromBs32(buffer)
|
|
123
|
+
}
|
|
124
|
+
if (typeof buffer === 'object') this.fromJSON(buffer)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
decode(buffer) {
|
|
128
|
+
this.hash = buffer
|
|
129
|
+
const codec = varint.decode(buffer);
|
|
130
|
+
|
|
131
|
+
this.discoCodec = new Codec(codec, this.codecs)
|
|
132
|
+
// TODO: validate codec
|
|
133
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
134
|
+
this.size = varint.decode(buffer);
|
|
135
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
136
|
+
if (this.digest.length !== this.size) {
|
|
137
|
+
throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
141
|
+
|
|
142
|
+
this.name = this.discoCodec.name
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
this.size = this.digest.length
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
codec: this.codec,
|
|
149
|
+
name: this.name,
|
|
150
|
+
size: this.size,
|
|
151
|
+
length: this.length,
|
|
152
|
+
digest: this.digest,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import Client from './../../../node_modules/socket-request-client/
|
|
2
|
-
|
|
3
|
-
export default class HttpClientApi {
|
|
4
|
-
constructor(config = {}) {
|
|
5
|
-
if (!config.apiPath) config.apiPath = 'api'
|
|
6
|
-
|
|
7
|
-
const address = `ws://${config.host}:${config.port}`
|
|
8
|
-
|
|
9
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
10
|
-
return (async () => {
|
|
11
|
-
this.client = await Client(address, config.protocol, {pubsub: config.pubsub, retry: 3000})
|
|
12
|
-
return this
|
|
13
|
-
})()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async get(url, obj) {
|
|
17
|
-
const headers = {}
|
|
18
|
-
let body = null
|
|
19
|
-
let method = 'GET'
|
|
20
|
-
if (obj) {
|
|
21
|
-
method = 'POST'
|
|
22
|
-
headers['Content-Type'] = 'application/json'
|
|
23
|
-
body = JSON.stringify(obj)
|
|
24
|
-
}
|
|
25
|
-
let response = await this.client.request(url, {headers, body, method})
|
|
26
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
27
|
-
if (type==='application/json') response = await response.json()
|
|
28
|
-
return response
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async put(url, obj) {
|
|
32
|
-
const headers = {}
|
|
33
|
-
let body = {}
|
|
34
|
-
if (obj) {
|
|
35
|
-
headers['Content-Type'] = 'application/json'
|
|
36
|
-
body = JSON.stringify(obj)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body})
|
|
40
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
41
|
-
if (type==='application/json') response = await response.json()
|
|
42
|
-
return response
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
import Client from './../../../node_modules/socket-request-client/dist/es/index'
|
|
2
|
+
|
|
3
|
+
export default class HttpClientApi {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
if (!config.apiPath) config.apiPath = 'api'
|
|
6
|
+
|
|
7
|
+
const address = `ws://${config.host}:${config.port}`
|
|
8
|
+
|
|
9
|
+
this.apiUrl = (url) => `${address}/${url}`;
|
|
10
|
+
return (async () => {
|
|
11
|
+
this.client = await Client(address, config.protocol, {pubsub: config.pubsub, retry: 3000})
|
|
12
|
+
return this
|
|
13
|
+
})()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async get(url, obj) {
|
|
17
|
+
const headers = {}
|
|
18
|
+
let body = null
|
|
19
|
+
let method = 'GET'
|
|
20
|
+
if (obj) {
|
|
21
|
+
method = 'POST'
|
|
22
|
+
headers['Content-Type'] = 'application/json'
|
|
23
|
+
body = JSON.stringify(obj)
|
|
24
|
+
}
|
|
25
|
+
let response = await this.client.request(url, {headers, body, method})
|
|
26
|
+
const type = response.headers.get('content-type').split(';')[0]
|
|
27
|
+
if (type==='application/json') response = await response.json()
|
|
28
|
+
return response
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async put(url, obj) {
|
|
32
|
+
const headers = {}
|
|
33
|
+
let body = {}
|
|
34
|
+
if (obj) {
|
|
35
|
+
headers['Content-Type'] = 'application/json'
|
|
36
|
+
body = JSON.stringify(obj)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body})
|
|
40
|
+
const type = response.headers.get('content-type').split(';')[0]
|
|
41
|
+
if (type==='application/json') response = await response.json()
|
|
42
|
+
return response
|
|
43
|
+
}
|
|
44
|
+
}
|