@leofcoin/peernet 0.11.0 → 0.11.3
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/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/codec-format-interface.js.html +637 -0
- package/coverage/lcov-report/dht-response.js.html +193 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +199 -0
- package/dist/browser/326.peernet.js +1 -0
- package/dist/browser/peernet.js +2 -108930
- package/dist/browser/peernet.js.LICENSE.txt +40 -0
- package/dist/commonjs/{client-bd0caeb7.js → client-1a1f75e6.js} +41 -41
- package/dist/commonjs/{codec-4a768e5e.js → codec-8c8c652f.js} +118 -118
- package/dist/commonjs/codec-format-interface.js +167 -167
- package/dist/commonjs/codec.js +2 -2
- package/dist/commonjs/dht-response.js +11 -11
- package/dist/commonjs/dht.js +2 -2
- package/dist/commonjs/hash.js +149 -149
- package/dist/commonjs/{http-2b0735ef.js → http-e088ccdb.js} +1 -1
- package/dist/commonjs/peernet-message.js +4 -4
- package/dist/commonjs/peernet.js +850 -943
- package/dist/commonjs/request.js +2 -2
- package/dist/commonjs/response.js +3 -3
- package/dist/module/peernet.js +1373 -1466
- package/index.html +2 -2
- package/package.json +6 -21
- package/src/client.js +75 -75
- package/src/codec/codec-format-interface.js +172 -172
- package/src/codec/codec.js +124 -124
- package/src/dht/dht.js +121 -121
- package/src/discovery/peer-discovery.js +75 -75
- package/src/handlers/message.js +2 -4
- package/src/hash/hash.js +155 -155
- package/src/http/client/http-client.js +44 -44
- package/src/messages/dht-response.js +14 -14
- package/src/peer.js +67 -67
- package/src/peernet.js +613 -612
- package/src/proto/chat-message.proto.js +7 -7
- package/src/proto/peernet.proto.js +2 -2
- package/src/proto/response.proto.js +1 -1
- package/src/utils/utils.js +78 -78
- package/test.js +1 -1
- package/webpack.config.js +10 -4
package/index.html
CHANGED
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
<script>
|
|
12
12
|
|
|
13
13
|
(async () => {
|
|
14
|
-
const peernet = await new Peernet()
|
|
15
|
-
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: 100}))
|
|
14
|
+
const peernet = await new Peernet.default()
|
|
15
|
+
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: new TextEncoder().encode(100)}))
|
|
16
16
|
})()
|
|
17
17
|
</script>
|
|
18
18
|
</body>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -24,31 +24,17 @@
|
|
|
24
24
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
|
-
"@leofcoin/multi-wallet": "^2.1.
|
|
27
|
+
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
28
|
"@leofcoin/peernet-swarm": "^0.1.9",
|
|
29
|
-
"@leofcoin/storage": "^2.
|
|
29
|
+
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
32
|
+
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
32
33
|
"@vandeurenglenn/little-pubsub": "^1.3.1",
|
|
33
|
-
"acorn-class-fields": "^1.0.0",
|
|
34
|
-
"acorn-node": "^2.0.1",
|
|
35
|
-
"acorn-private-class-elements": "^1.0.0",
|
|
36
|
-
"acorn-private-methods": "^1.0.0",
|
|
37
|
-
"acorn-stage3": "^4.0.0",
|
|
38
|
-
"async_hooks": "^1.0.0",
|
|
39
|
-
"bittorrent-tracker": "^9.18.5",
|
|
40
|
-
"bs32": "^0.1.6",
|
|
41
|
-
"bs58": "^4.0.1",
|
|
42
|
-
"bs58check": "^2.1.2",
|
|
43
|
-
"is-hex": "^1.1.3",
|
|
44
34
|
"keccak": "^3.0.1",
|
|
45
35
|
"koa": "^2.13.1",
|
|
46
|
-
"multiformats": "^9.6.4",
|
|
47
36
|
"node-fetch": "^2.6.1",
|
|
48
|
-
"on-finished": "^2.4.1",
|
|
49
|
-
"p2pt": "^1.5.0",
|
|
50
37
|
"protons": "^2.0.1",
|
|
51
|
-
"simple-peer": "^9.11.1",
|
|
52
38
|
"socket-request-client": "^1.5.0",
|
|
53
39
|
"socket-request-server": "^1.5.0",
|
|
54
40
|
"stream-browserify": "^3.0.0",
|
|
@@ -60,8 +46,6 @@
|
|
|
60
46
|
"@rollup/plugin-eslint": "^8.0.1",
|
|
61
47
|
"@rollup/plugin-json": "^4.1.0",
|
|
62
48
|
"@rollup/plugin-node-resolve": "^13.0.4",
|
|
63
|
-
"acorn": "^8.7.0",
|
|
64
|
-
"browserify": "^17.0.0",
|
|
65
49
|
"coveralls": "^3.1.1",
|
|
66
50
|
"esdoc": "^1.1.0",
|
|
67
51
|
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
|
|
@@ -78,6 +62,7 @@
|
|
|
78
62
|
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
79
63
|
"tape": "^5.2.2",
|
|
80
64
|
"webpack": "^5.72.0",
|
|
81
|
-
"webpack-cli": "^4.9.2"
|
|
65
|
+
"webpack-cli": "^4.9.2",
|
|
66
|
+
"webpack-node-externals": "^3.0.0"
|
|
82
67
|
}
|
|
83
68
|
}
|
package/src/client.js
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import Pubsub from '@vandeurenglenn/little-pubsub'
|
|
2
|
-
import PeernetPeer from './peer.js'
|
|
3
|
-
import sha256 from 'crypto-js/sha256'
|
|
4
|
-
|
|
5
|
-
import P2P from 'p2pt'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Array of peers
|
|
10
|
-
* @type {Array}
|
|
11
|
-
* @property {PeernetPeer} peer Instance of Peer
|
|
12
|
-
*/
|
|
13
|
-
globalThis.connections = new Map()
|
|
14
|
-
globalThis.recentConnections = new Map()
|
|
15
|
-
globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
|
|
16
|
-
|
|
17
|
-
export default class PeernetClient {
|
|
18
|
-
constructor(options = {}) {
|
|
19
|
-
if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
|
|
20
|
-
if (!options.networkVersion) options.networkVersion = 'v0.1.0'
|
|
21
|
-
if (!options.networkName) options.networkName = 'peernet'
|
|
22
|
-
this.id = options.id
|
|
23
|
-
|
|
24
|
-
this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
|
|
25
|
-
|
|
26
|
-
const trackers = [
|
|
27
|
-
'wss://star.leofcoin.org',
|
|
28
|
-
'wss://tracker.openwebtorrent.com',
|
|
29
|
-
// 'wss://tracker.sloppyta.co:443/announce',
|
|
30
|
-
]
|
|
31
|
-
this.p2p = new P2P(trackers, this.topic.slice(0, 20))
|
|
32
|
-
this.p2p.on('peerconnect', (peer) => {
|
|
33
|
-
peer = new PeernetPeer(peer.id, peer)
|
|
34
|
-
connections.set(peer.id, peer)
|
|
35
|
-
pubsub.publish('peer:discovered', peer)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
this.p2p.on('peerclose', (peer) => {
|
|
39
|
-
// TODO: close peernetPeer
|
|
40
|
-
const peernetPeer = connections.get(peer.id)
|
|
41
|
-
if (peernetPeer) {
|
|
42
|
-
peernetPeer.close()
|
|
43
|
-
}
|
|
44
|
-
connections.delete(peer.id)
|
|
45
|
-
pubsub.publish('peer:disconnected', peer)
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
this.p2p.start()
|
|
49
|
-
|
|
50
|
-
if (globalThis.process) {
|
|
51
|
-
process.on('SIGINT', async () => {
|
|
52
|
-
console.log('Caught interrupt signal')
|
|
53
|
-
this.close()
|
|
54
|
-
setTimeout(async () => {
|
|
55
|
-
process.exit();
|
|
56
|
-
}, 100);
|
|
57
|
-
})
|
|
58
|
-
} else {
|
|
59
|
-
globalThis.onbeforeunload = () => {
|
|
60
|
-
this.close()
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
//
|
|
64
|
-
// this.sw.on('close', () => {
|
|
65
|
-
// })
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
close() {
|
|
69
|
-
return this.p2p.destroy()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
_peers() {
|
|
73
|
-
return this.p2p.getPeers()
|
|
74
|
-
}
|
|
75
|
-
}
|
|
1
|
+
import Pubsub from '@vandeurenglenn/little-pubsub'
|
|
2
|
+
import PeernetPeer from './peer.js'
|
|
3
|
+
import sha256 from 'crypto-js/sha256'
|
|
4
|
+
|
|
5
|
+
import P2P from 'p2pt'
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Array of peers
|
|
10
|
+
* @type {Array}
|
|
11
|
+
* @property {PeernetPeer} peer Instance of Peer
|
|
12
|
+
*/
|
|
13
|
+
globalThis.connections = new Map()
|
|
14
|
+
globalThis.recentConnections = new Map()
|
|
15
|
+
globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
|
|
16
|
+
|
|
17
|
+
export default class PeernetClient {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
|
|
20
|
+
if (!options.networkVersion) options.networkVersion = 'v0.1.0'
|
|
21
|
+
if (!options.networkName) options.networkName = 'peernet'
|
|
22
|
+
this.id = options.id
|
|
23
|
+
|
|
24
|
+
this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
|
|
25
|
+
|
|
26
|
+
const trackers = [
|
|
27
|
+
'wss://star.leofcoin.org',
|
|
28
|
+
'wss://tracker.openwebtorrent.com',
|
|
29
|
+
// 'wss://tracker.sloppyta.co:443/announce',
|
|
30
|
+
]
|
|
31
|
+
this.p2p = new P2P(trackers, this.topic.slice(0, 20))
|
|
32
|
+
this.p2p.on('peerconnect', (peer) => {
|
|
33
|
+
peer = new PeernetPeer(peer.id, peer)
|
|
34
|
+
connections.set(peer.id, peer)
|
|
35
|
+
pubsub.publish('peer:discovered', peer)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
this.p2p.on('peerclose', (peer) => {
|
|
39
|
+
// TODO: close peernetPeer
|
|
40
|
+
const peernetPeer = connections.get(peer.id)
|
|
41
|
+
if (peernetPeer) {
|
|
42
|
+
peernetPeer.close()
|
|
43
|
+
}
|
|
44
|
+
connections.delete(peer.id)
|
|
45
|
+
pubsub.publish('peer:disconnected', peer)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
this.p2p.start()
|
|
49
|
+
|
|
50
|
+
if (globalThis.process) {
|
|
51
|
+
process.on('SIGINT', async () => {
|
|
52
|
+
console.log('Caught interrupt signal')
|
|
53
|
+
this.close()
|
|
54
|
+
setTimeout(async () => {
|
|
55
|
+
process.exit();
|
|
56
|
+
}, 100);
|
|
57
|
+
})
|
|
58
|
+
} else {
|
|
59
|
+
globalThis.onbeforeunload = () => {
|
|
60
|
+
this.close()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//
|
|
64
|
+
// this.sw.on('close', () => {
|
|
65
|
+
// })
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
close() {
|
|
69
|
+
return this.p2p.destroy()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
_peers() {
|
|
73
|
+
return this.p2p.getPeers()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,172 +1,172 @@
|
|
|
1
|
-
import bs32 from '@vandeurenglenn/base32';
|
|
2
|
-
import bs58 from '@vandeurenglenn/base58';
|
|
3
|
-
import isHex from '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) return this.fromUint8Array(buffer)
|
|
19
|
-
else if (buffer instanceof ArrayBuffer) return this.fromArrayBuffer(buffer)
|
|
20
|
-
else if (buffer.name === options.name) return buffer
|
|
21
|
-
else if (typeof buffer === '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
|
-
/**
|
|
96
|
-
* @param {Buffer} encoded
|
|
97
|
-
*/
|
|
98
|
-
fromEncoded(encoded) {
|
|
99
|
-
this.encoded = encoded
|
|
100
|
-
this.decode()
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @param {String} encoded
|
|
105
|
-
*/
|
|
106
|
-
fromHex(encoded) {
|
|
107
|
-
this.encoded = Buffer.from(encoded, 'hex')
|
|
108
|
-
this.decode()
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @param {String} encoded
|
|
113
|
-
*/
|
|
114
|
-
fromBs32(encoded) {
|
|
115
|
-
this.encoded = bs32.decode(encoded)
|
|
116
|
-
this.decode()
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* @param {String} encoded
|
|
121
|
-
*/
|
|
122
|
-
fromBs58(encoded) {
|
|
123
|
-
this.encoded = bs58.decode(encoded)
|
|
124
|
-
this.decode()
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* @return {String} encoded
|
|
129
|
-
*/
|
|
130
|
-
toHex() {
|
|
131
|
-
if (!this.encoded) this.encode()
|
|
132
|
-
return this.encoded.toString('hex')
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @return {String} encoded
|
|
137
|
-
*/
|
|
138
|
-
toBs32() {
|
|
139
|
-
if (!this.encoded) this.encode()
|
|
140
|
-
return bs32.encode(this.encoded)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* @return {String} encoded
|
|
145
|
-
*/
|
|
146
|
-
toBs58() {
|
|
147
|
-
if (!this.encoded) this.encode()
|
|
148
|
-
return bs58.encode(this.encoded)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @param {Object} data
|
|
153
|
-
*/
|
|
154
|
-
create(data) {
|
|
155
|
-
const decoded = {}
|
|
156
|
-
if (this.keys?.length > 0) {
|
|
157
|
-
for (const key of this.keys) {
|
|
158
|
-
Object.defineProperties(decoded, {
|
|
159
|
-
[key]: {
|
|
160
|
-
enumerable: true,
|
|
161
|
-
configurable: true,
|
|
162
|
-
set: (val) => value = data[key],
|
|
163
|
-
get: () => data[key]
|
|
164
|
-
}
|
|
165
|
-
})
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
this.decoded = decoded
|
|
169
|
-
this.encode()
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
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) return this.fromUint8Array(buffer)
|
|
19
|
+
else if (buffer instanceof ArrayBuffer) return this.fromArrayBuffer(buffer)
|
|
20
|
+
else if (buffer.name === options.name) return buffer
|
|
21
|
+
else if (typeof buffer === '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
|
+
/**
|
|
96
|
+
* @param {Buffer} encoded
|
|
97
|
+
*/
|
|
98
|
+
fromEncoded(encoded) {
|
|
99
|
+
this.encoded = encoded
|
|
100
|
+
this.decode()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {String} encoded
|
|
105
|
+
*/
|
|
106
|
+
fromHex(encoded) {
|
|
107
|
+
this.encoded = Buffer.from(encoded, 'hex')
|
|
108
|
+
this.decode()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {String} encoded
|
|
113
|
+
*/
|
|
114
|
+
fromBs32(encoded) {
|
|
115
|
+
this.encoded = bs32.decode(encoded)
|
|
116
|
+
this.decode()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param {String} encoded
|
|
121
|
+
*/
|
|
122
|
+
fromBs58(encoded) {
|
|
123
|
+
this.encoded = bs58.decode(encoded)
|
|
124
|
+
this.decode()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @return {String} encoded
|
|
129
|
+
*/
|
|
130
|
+
toHex() {
|
|
131
|
+
if (!this.encoded) this.encode()
|
|
132
|
+
return this.encoded.toString('hex')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @return {String} encoded
|
|
137
|
+
*/
|
|
138
|
+
toBs32() {
|
|
139
|
+
if (!this.encoded) this.encode()
|
|
140
|
+
return bs32.encode(this.encoded)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @return {String} encoded
|
|
145
|
+
*/
|
|
146
|
+
toBs58() {
|
|
147
|
+
if (!this.encoded) this.encode()
|
|
148
|
+
return bs58.encode(this.encoded)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {Object} data
|
|
153
|
+
*/
|
|
154
|
+
create(data) {
|
|
155
|
+
const decoded = {}
|
|
156
|
+
if (this.keys?.length > 0) {
|
|
157
|
+
for (const key of this.keys) {
|
|
158
|
+
Object.defineProperties(decoded, {
|
|
159
|
+
[key]: {
|
|
160
|
+
enumerable: true,
|
|
161
|
+
configurable: true,
|
|
162
|
+
set: (val) => value = data[key],
|
|
163
|
+
get: () => data[key]
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
this.decoded = decoded
|
|
169
|
+
this.encode()
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|