@leofcoin/peernet 0.10.8 → 0.11.0
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/dist/browser/peernet.js +101813 -92946
- package/dist/commonjs/client-bd0caeb7.js +324 -0
- package/dist/commonjs/{codec-6367213c.js → codec-4a768e5e.js} +99 -89
- package/dist/commonjs/codec-format-interface.js +41 -24
- package/dist/commonjs/codec.js +3 -3
- package/dist/commonjs/dht-response.js +3 -3
- package/dist/commonjs/dht.js +23 -23
- package/dist/commonjs/hash.js +17 -7
- package/dist/commonjs/{http-42a6e555.js → http-2b0735ef.js} +19 -15
- package/dist/commonjs/peernet-message.js +12 -12
- package/dist/commonjs/peernet.js +1162 -964
- package/dist/commonjs/request.js +12 -12
- package/dist/commonjs/response.js +12 -12
- package/dist/module/peernet.js +1323 -1118
- package/index.html +4 -6
- package/package.json +31 -7
- package/rollup.config.js +33 -5
- package/rollup0.config.js +7 -0
- package/src/codec/codec-format-interface.js +40 -23
- package/src/codec/codec.js +21 -11
- package/src/codec/codecs.js +79 -79
- package/src/handlers/message.js +52 -52
- package/src/hash/hash.js +16 -6
- package/src/http/client/http-client.js +1 -1
- 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 +0 -1
- 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/peernet.js +29 -114
- 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/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/codec-format-interface.js.html +0 -533
- package/coverage/lcov-report/codec.js.html +0 -677
- package/coverage/lcov-report/dht-response.js.html +0 -188
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/hash.js.html +0 -551
- package/coverage/lcov-report/index.html +0 -156
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov.info +0 -459
- 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-43f4fafe.js +0 -324
- package/dist/commonjs/http-a94c5a81.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
package/index.html
CHANGED
|
@@ -7,15 +7,13 @@
|
|
|
7
7
|
<body>
|
|
8
8
|
<script src="./dist/browser/peernet.js">
|
|
9
9
|
</script>
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
<script>
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
(async () => {
|
|
14
14
|
const peernet = await new Peernet()
|
|
15
|
-
peernet.
|
|
16
|
-
console.log(data)
|
|
17
|
-
})
|
|
15
|
+
peernet.addRequestHandler('lastBlock', () => new peernet.protos['peernet-response']({response: 100}))
|
|
18
16
|
})()
|
|
19
17
|
</script>
|
|
20
18
|
</body>
|
|
21
|
-
</html>
|
|
19
|
+
</html>
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
|
+
"module": "dist/module/peernet.js",
|
|
7
|
+
"browser": "dist/browser/peernet.js",
|
|
6
8
|
"scripts": {
|
|
9
|
+
"build": "webpack",
|
|
7
10
|
"test": "node test/index.js",
|
|
8
11
|
"server": "discovery-swarm-webrtc --port=4000",
|
|
9
12
|
"demo": "jsproject --serve ./ --port 6868",
|
|
@@ -13,37 +16,52 @@
|
|
|
13
16
|
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
|
14
17
|
"c": "rollup -c",
|
|
15
18
|
"w": "rollup -c -w",
|
|
16
|
-
"b": "browserify browser.js -o dist/browser/peernet.js --standalone Peernet && rm browser.js"
|
|
19
|
+
"b": "browserify dist/browser/peernet.js -o dist/browser/peernet.js --standalone Peernet && rm browser.js"
|
|
17
20
|
},
|
|
18
21
|
"keywords": [],
|
|
19
22
|
"author": "",
|
|
20
23
|
"license": "MIT",
|
|
24
|
+
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
21
25
|
"dependencies": {
|
|
22
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
23
|
-
"@leofcoin/multi-wallet": "^2.
|
|
24
|
-
"@leofcoin/
|
|
27
|
+
"@leofcoin/multi-wallet": "^2.1.1",
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.1.9",
|
|
29
|
+
"@leofcoin/storage": "^2.1.2",
|
|
30
|
+
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
|
+
"@vandeurenglenn/base58": "^1.1.0",
|
|
25
32
|
"@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",
|
|
26
38
|
"async_hooks": "^1.0.0",
|
|
39
|
+
"bittorrent-tracker": "^9.18.5",
|
|
27
40
|
"bs32": "^0.1.6",
|
|
28
41
|
"bs58": "^4.0.1",
|
|
29
42
|
"bs58check": "^2.1.2",
|
|
30
43
|
"is-hex": "^1.1.3",
|
|
31
44
|
"keccak": "^3.0.1",
|
|
32
45
|
"koa": "^2.13.1",
|
|
46
|
+
"multiformats": "^9.6.4",
|
|
33
47
|
"node-fetch": "^2.6.1",
|
|
34
48
|
"on-finished": "^2.4.1",
|
|
35
49
|
"p2pt": "^1.5.0",
|
|
36
50
|
"protons": "^2.0.1",
|
|
51
|
+
"simple-peer": "^9.11.1",
|
|
37
52
|
"socket-request-client": "^1.5.0",
|
|
38
53
|
"socket-request-server": "^1.5.0",
|
|
54
|
+
"stream-browserify": "^3.0.0",
|
|
39
55
|
"varint": "^6.0.0"
|
|
40
56
|
},
|
|
41
57
|
"devDependencies": {
|
|
58
|
+
"@babel/plugin-proposal-private-methods": "^7.16.11",
|
|
42
59
|
"@rollup/plugin-commonjs": "^19.0.1",
|
|
43
60
|
"@rollup/plugin-eslint": "^8.0.1",
|
|
44
61
|
"@rollup/plugin-json": "^4.1.0",
|
|
45
62
|
"@rollup/plugin-node-resolve": "^13.0.4",
|
|
46
|
-
"
|
|
63
|
+
"acorn": "^8.7.0",
|
|
64
|
+
"browserify": "^17.0.0",
|
|
47
65
|
"coveralls": "^3.1.1",
|
|
48
66
|
"esdoc": "^1.1.0",
|
|
49
67
|
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
|
|
@@ -51,9 +69,15 @@
|
|
|
51
69
|
"eslint": "^7.31.0",
|
|
52
70
|
"eslint-config-google": "^0.14.0",
|
|
53
71
|
"nyc": "^15.1.0",
|
|
54
|
-
"
|
|
72
|
+
"os-browserify": "^0.3.0",
|
|
73
|
+
"path-browserify": "^1.0.1",
|
|
74
|
+
"rollup": "^2.70.2",
|
|
55
75
|
"rollup-plugin-modify": "^3.0.0",
|
|
76
|
+
"rollup-plugin-natives": "^0.7.5",
|
|
56
77
|
"rollup-plugin-node-globals": "^1.4.0",
|
|
57
|
-
"
|
|
78
|
+
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
79
|
+
"tape": "^5.2.2",
|
|
80
|
+
"webpack": "^5.72.0",
|
|
81
|
+
"webpack-cli": "^4.9.2"
|
|
58
82
|
}
|
|
59
83
|
}
|
package/rollup.config.js
CHANGED
|
@@ -4,7 +4,14 @@ import resolve from '@rollup/plugin-node-resolve'
|
|
|
4
4
|
import cjs from '@rollup/plugin-commonjs'
|
|
5
5
|
import json from '@rollup/plugin-json'
|
|
6
6
|
import modify from 'rollup-plugin-modify'
|
|
7
|
+
import nativePlugin from 'rollup-plugin-natives';
|
|
8
|
+
import polyfills from 'rollup-plugin-node-polyfills'
|
|
7
9
|
|
|
10
|
+
try {
|
|
11
|
+
execSync('rm dist -r')
|
|
12
|
+
} catch (e) {
|
|
13
|
+
|
|
14
|
+
}
|
|
8
15
|
export default [{
|
|
9
16
|
input: ['src/peernet.js', 'src/codec/codec.js', 'src/hash/hash.js', 'src/messages/dht.js',
|
|
10
17
|
'src/messages/peernet-message.js', 'src/messages/dht-response.js', 'src/messages/request.js',
|
|
@@ -14,21 +21,42 @@ export default [{
|
|
|
14
21
|
format: 'cjs'
|
|
15
22
|
},
|
|
16
23
|
plugins: [
|
|
17
|
-
json()
|
|
24
|
+
json(),
|
|
25
|
+
modify({
|
|
26
|
+
HTTP_IMPORT: `if (this.hasDaemon) {
|
|
27
|
+
const httpClient = await import('./http/client/client.js')
|
|
28
|
+
globalThis.peernet.client = await httpClient.default({
|
|
29
|
+
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
30
|
+
})
|
|
31
|
+
} else {
|
|
32
|
+
const http = await import('./http/http.js')
|
|
33
|
+
if (environment !== 'browser') http.default(options)
|
|
34
|
+
}`
|
|
35
|
+
})
|
|
18
36
|
]
|
|
19
37
|
}, {
|
|
20
38
|
input: 'src/peernet.js',
|
|
21
39
|
output: {
|
|
22
|
-
|
|
40
|
+
dir: 'dist/browser/',
|
|
23
41
|
format: 'cjs'
|
|
24
42
|
},
|
|
25
43
|
plugins: [
|
|
26
44
|
json(),
|
|
27
45
|
modify({
|
|
28
|
-
"import fetch from 'node-fetch'": ''
|
|
46
|
+
"import fetch from 'node-fetch'": '',
|
|
47
|
+
HTTP_IMPORT: ``
|
|
29
48
|
}),
|
|
30
|
-
|
|
31
|
-
//
|
|
49
|
+
// nativePlugin(),
|
|
50
|
+
// polyfills(),
|
|
51
|
+
// resolve({
|
|
52
|
+
// preferBuiltins: true,
|
|
53
|
+
// mainFields: ["browser", "module", "main"],
|
|
54
|
+
// extensions: ['.mjs', '.js', '.json']
|
|
55
|
+
// }),
|
|
56
|
+
// cjs({
|
|
57
|
+
// exclude: ['*.node'],
|
|
58
|
+
// extensions: ['.js']
|
|
59
|
+
// }),
|
|
32
60
|
// lint({
|
|
33
61
|
// fix: true,
|
|
34
62
|
// exclude: ['package.json', "package-lock.json"]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import bs32 from '
|
|
2
|
-
import bs58 from '
|
|
1
|
+
import bs32 from '@vandeurenglenn/base32';
|
|
2
|
+
import bs58 from '@vandeurenglenn/base58';
|
|
3
3
|
import isHex from 'is-hex';
|
|
4
4
|
import Codec from './codec';
|
|
5
5
|
import Hash from './../hash/hash'
|
|
@@ -13,25 +13,18 @@ export default class FormatInterface {
|
|
|
13
13
|
constructor(buffer, proto, options = {}) {
|
|
14
14
|
this.protoEncode = proto.encode
|
|
15
15
|
this.protoDecode = proto.decode
|
|
16
|
-
if (options.name) this.name = options.name
|
|
17
16
|
this.hashFormat = options.hashFormat || 'bs32'
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.fromEncoded(buffer)
|
|
24
|
-
} else {
|
|
25
|
-
this.create(buffer)
|
|
26
|
-
}
|
|
27
|
-
} else {
|
|
28
|
-
if (typeof buffer === 'string') {
|
|
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') {
|
|
29
22
|
if (isHex(buffer)) this.fromHex(buffer)
|
|
30
|
-
else if (bs32.
|
|
31
|
-
else this.fromBs58(buffer)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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)
|
|
35
28
|
}
|
|
36
29
|
}
|
|
37
30
|
|
|
@@ -56,7 +49,7 @@ export default class FormatInterface {
|
|
|
56
49
|
*/
|
|
57
50
|
decode() {
|
|
58
51
|
let encoded = this.encoded;
|
|
59
|
-
const discoCodec = new Codec(this.encoded
|
|
52
|
+
const discoCodec = new Codec(this.encoded)
|
|
60
53
|
encoded = encoded.slice(discoCodec.codecBuffer.length)
|
|
61
54
|
this.name = discoCodec.name
|
|
62
55
|
this.decoded = this.protoDecode(encoded)
|
|
@@ -69,16 +62,40 @@ export default class FormatInterface {
|
|
|
69
62
|
encode(decoded) {
|
|
70
63
|
if (!decoded) decoded = this.decoded;
|
|
71
64
|
const codec = new Codec(this.name)
|
|
72
|
-
|
|
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
|
|
73
70
|
return this.encoded
|
|
74
71
|
}
|
|
75
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
|
+
|
|
76
95
|
/**
|
|
77
96
|
* @param {Buffer} encoded
|
|
78
97
|
*/
|
|
79
98
|
fromEncoded(encoded) {
|
|
80
|
-
const codec = new Codec(encoded)
|
|
81
|
-
this.name = codec.name
|
|
82
99
|
this.encoded = encoded
|
|
83
100
|
this.decode()
|
|
84
101
|
}
|
package/src/codec/codec.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import varint from 'varint';
|
|
2
|
-
import bs32 from '
|
|
3
|
-
import bs58 from '
|
|
2
|
+
import bs32 from '@vandeurenglenn/base32';
|
|
3
|
+
import bs58 from '@vandeurenglenn/base58';
|
|
4
4
|
import isHex from 'is-hex';
|
|
5
5
|
import codecs from './codecs'
|
|
6
6
|
|
|
@@ -10,10 +10,9 @@ export default class PeernetCodec {
|
|
|
10
10
|
}
|
|
11
11
|
constructor(buffer) {
|
|
12
12
|
if (buffer) {
|
|
13
|
-
if (
|
|
13
|
+
if (buffer instanceof Uint8Array) {
|
|
14
14
|
const codec = varint.decode(buffer);
|
|
15
15
|
const name = this.getCodecName(codec)
|
|
16
|
-
|
|
17
16
|
if (name) {
|
|
18
17
|
this.name = name
|
|
19
18
|
this.encoded = buffer
|
|
@@ -21,12 +20,23 @@ export default class PeernetCodec {
|
|
|
21
20
|
} else {
|
|
22
21
|
this.encode(buffer)
|
|
23
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
|
|
24
33
|
}
|
|
25
34
|
if (typeof buffer === 'string') {
|
|
26
35
|
if (this.codecs[buffer]) this.fromName(buffer)
|
|
27
36
|
else if (isHex(buffer)) this.fromHex(buffer)
|
|
28
|
-
else if (bs32.
|
|
29
|
-
else this.fromBs58(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}`)
|
|
30
40
|
}
|
|
31
41
|
if (!isNaN(buffer)) if (this.codecs[this.getCodecName(buffer)]) this.fromCodec(buffer)
|
|
32
42
|
}
|
|
@@ -61,7 +71,8 @@ export default class PeernetCodec {
|
|
|
61
71
|
|
|
62
72
|
getCodecName(codec) {
|
|
63
73
|
return Object.keys(this.codecs).reduce((p, c) => {
|
|
64
|
-
|
|
74
|
+
const item = this.codecs[c]
|
|
75
|
+
if (item.codec === codec) return c;
|
|
65
76
|
else return p;
|
|
66
77
|
}, undefined)
|
|
67
78
|
}
|
|
@@ -75,7 +86,7 @@ export default class PeernetCodec {
|
|
|
75
86
|
this.hashAlg = this.getHashAlg(this.name)
|
|
76
87
|
|
|
77
88
|
this.codec = this.getCodec(this.name)
|
|
78
|
-
this.codecBuffer =
|
|
89
|
+
this.codecBuffer = varint.encode(codec)
|
|
79
90
|
}
|
|
80
91
|
|
|
81
92
|
fromName(name) {
|
|
@@ -83,7 +94,7 @@ export default class PeernetCodec {
|
|
|
83
94
|
this.name = name
|
|
84
95
|
this.codec = codec
|
|
85
96
|
this.hashAlg = this.getHashAlg(name)
|
|
86
|
-
this.codecBuffer =
|
|
97
|
+
this.codecBuffer = varint.encode(codec)
|
|
87
98
|
}
|
|
88
99
|
|
|
89
100
|
toBs32() {
|
|
@@ -103,11 +114,10 @@ export default class PeernetCodec {
|
|
|
103
114
|
decode() {
|
|
104
115
|
const codec = varint.decode(this.encoded);
|
|
105
116
|
this.fromCodec(codec)
|
|
106
|
-
this.name = this.getCodecName(codec)
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
encode() {
|
|
110
|
-
const codec =
|
|
120
|
+
const codec = varint.encode(this.decoded)
|
|
111
121
|
this.encoded = codec
|
|
112
122
|
return this.encoded
|
|
113
123
|
}
|
package/src/codec/codecs.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
// just a hash
|
|
3
|
-
'disco-hash': {
|
|
4
|
-
codec: '30',
|
|
5
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
6
|
-
// testnet: 'olivia'
|
|
7
|
-
},
|
|
8
|
-
'peernet-peer-response': {
|
|
9
|
-
codec: '707072',
|
|
10
|
-
hashAlg: 'keccak-256',
|
|
11
|
-
},
|
|
12
|
-
'peernet-peer': {
|
|
13
|
-
codec: '7070',
|
|
14
|
-
hashAlg: 'keccak-256',
|
|
15
|
-
},
|
|
16
|
-
'peernet-dht': {
|
|
17
|
-
codec: '706468',
|
|
18
|
-
hashAlg: 'keccak-256',
|
|
19
|
-
},
|
|
20
|
-
'peernet-dht-response': {
|
|
21
|
-
codec: '706472',
|
|
22
|
-
hashAlg: 'keccak-256',
|
|
23
|
-
},
|
|
24
|
-
// data
|
|
25
|
-
'peernet-data': {
|
|
26
|
-
codec: '706461',
|
|
27
|
-
hashAlg: 'keccak-256',
|
|
28
|
-
},
|
|
29
|
-
'peernet-data-response': {
|
|
30
|
-
codec: '70646172',
|
|
31
|
-
hashAlg: 'keccak-256',
|
|
32
|
-
},
|
|
33
|
-
// message
|
|
34
|
-
'peernet-message': {
|
|
35
|
-
codec: '706d65',
|
|
36
|
-
hashAlg: 'keccak-512',
|
|
37
|
-
},
|
|
38
|
-
// pubsub
|
|
39
|
-
'peernet-ps': {
|
|
40
|
-
codec: '707073',
|
|
41
|
-
hashAlg: 'keccak-256',
|
|
42
|
-
},
|
|
43
|
-
'peernet-response': {
|
|
44
|
-
codec: '7072',
|
|
45
|
-
hashAlg: 'keccak-256',
|
|
46
|
-
},
|
|
47
|
-
'peernet-request': {
|
|
48
|
-
codec: '707271',
|
|
49
|
-
hashAlg: 'keccak-256',
|
|
50
|
-
},
|
|
51
|
-
// normal block
|
|
52
|
-
'leofcoin-block': {
|
|
53
|
-
codec: '6c62',
|
|
54
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
55
|
-
// testnet: 'olivia'
|
|
56
|
-
},
|
|
57
|
-
'leofcoin-tx': {
|
|
58
|
-
codec: '6c74',
|
|
59
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
60
|
-
// testnet: 'olivia'
|
|
61
|
-
},
|
|
62
|
-
// itx
|
|
63
|
-
'leofcoin-itx': {
|
|
64
|
-
codec: '6c69',
|
|
65
|
-
hashAlg: 'keccak-512', // ,
|
|
66
|
-
// testnet: 'olivia'
|
|
67
|
-
},
|
|
68
|
-
// peer reputation
|
|
69
|
-
'leofcoin-pr': {
|
|
70
|
-
codec: '6c70',
|
|
71
|
-
hashAlg: 'keccak-256', // ,
|
|
72
|
-
// testnet: 'olivia'
|
|
73
|
-
},
|
|
74
|
-
// chat message
|
|
75
|
-
'chat-message': {
|
|
76
|
-
codec: '636d',
|
|
77
|
-
hashAlg: 'dbl-keccak-512',
|
|
78
|
-
},
|
|
79
|
-
}
|
|
1
|
+
export default {
|
|
2
|
+
// just a hash
|
|
3
|
+
'disco-hash': {
|
|
4
|
+
codec: parseInt('30', 16),
|
|
5
|
+
hashAlg: 'dbl-keccak-512', // ,
|
|
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-512',
|
|
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-512',
|
|
78
|
+
},
|
|
79
|
+
}
|
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,7 +1,7 @@
|
|
|
1
1
|
import createKeccakHash from 'keccak';
|
|
2
2
|
import varint from 'varint';
|
|
3
|
-
import bs32 from '
|
|
4
|
-
import bs58 from '
|
|
3
|
+
import bs32 from '@vandeurenglenn/base32';
|
|
4
|
+
import bs58 from '@vandeurenglenn/base58';
|
|
5
5
|
import isHex from 'is-hex';
|
|
6
6
|
import Codec from './../codec/codec';
|
|
7
7
|
|
|
@@ -25,18 +25,28 @@ export default class PeernetHash {
|
|
|
25
25
|
|
|
26
26
|
if (typeof buffer === 'string') {
|
|
27
27
|
if (isHex(buffer)) this.fromHex(buffer)
|
|
28
|
-
if (bs32.
|
|
29
|
-
else this.fromBs58(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}`)
|
|
30
31
|
} else if (typeof buffer === 'object') this.fromJSON(buffer)
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
get prefix() {
|
|
35
|
-
|
|
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
|
|
36
46
|
}
|
|
37
47
|
|
|
38
48
|
get length() {
|
|
39
|
-
return
|
|
49
|
+
return varint.encode(this.size)
|
|
40
50
|
}
|
|
41
51
|
|
|
42
52
|
get buffer() {
|