@leofcoin/peernet 0.11.31 → 0.12.2
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/generate-account.js +50 -0
- package/dist/browser/messages.js +439 -0
- package/dist/browser/pako.js +6731 -0
- package/dist/browser/peernet-swarm.js +836 -0
- package/dist/browser/peernet.js +7576 -54848
- package/dist/browser/protons.js +3786 -0
- package/dist/browser/storage.js +12 -0
- package/dist/browser/wrtc.js +28 -0
- package/dist/commonjs/dht-6a1b6246.js +45 -0
- package/dist/commonjs/dht-response-e4a603ea.js +34 -0
- package/dist/commonjs/dht-response.js +3 -24
- package/dist/commonjs/dht.js +3 -35
- package/dist/commonjs/messages-bcb02ee9.js +189 -0
- package/dist/commonjs/{peernet-message.js → peernet-6eef77d5.js} +12 -7
- package/dist/commonjs/peernet.js +60 -1368
- package/dist/commonjs/peernet2.js +8 -0
- package/dist/commonjs/request-95ed03ec.js +33 -0
- package/dist/commonjs/request.js +3 -23
- package/dist/commonjs/response-bae4e2a2.js +33 -0
- package/dist/commonjs/response.js +3 -23
- package/dist/module/messages-421f88db.js +323 -0
- package/dist/module/peernet.js +53 -1451
- package/package.json +7 -12
- package/rollup.config.js +2 -21
- package/src/discovery/peer-discovery.js +4 -4
- package/src/handlers/data.js +2 -2
- package/src/handlers/message.js +4 -5
- package/src/messages/{chat-message.js → chat.js} +5 -2
- package/src/messages/data-response.js +5 -2
- package/src/messages/data.js +5 -2
- package/src/messages/dht-response.js +5 -2
- package/src/messages/dht.js +5 -2
- package/src/messages/peer-response.js +5 -2
- package/src/messages/peer.js +5 -2
- package/src/messages/{peernet-message.js → peernet.js} +5 -2
- package/src/messages/ps.js +5 -2
- package/src/messages/request.js +5 -2
- package/src/messages/response.js +5 -2
- package/src/messages.js +11 -0
- package/src/peernet.js +45 -32
- package/webpack.config.js +6 -3
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
|
-
"module": "
|
|
7
|
-
"browser": "dist/browser/peernet.js",
|
|
6
|
+
"module": "src/module/peernet.js",
|
|
8
7
|
"scripts": {
|
|
9
8
|
"build": "npm run c && webpack",
|
|
10
9
|
"test": "node test/index.js",
|
|
@@ -23,29 +22,26 @@
|
|
|
23
22
|
"license": "MIT",
|
|
24
23
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"@leofcoin/codec-format-interface": "^1.0
|
|
27
|
-
"@leofcoin/generate-account": "^1.0.
|
|
25
|
+
"@leofcoin/codec-format-interface": "^1.2.0",
|
|
26
|
+
"@leofcoin/generate-account": "^1.0.4",
|
|
28
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
29
|
-
"@leofcoin/peernet-swarm": "^0.3.
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.3.3",
|
|
30
29
|
"@leofcoin/storage": "^2.3.0",
|
|
31
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
32
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
32
|
+
"@vandeurenglenn/debug": "^1.0.0",
|
|
33
33
|
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
34
34
|
"@vandeurenglenn/little-pubsub": "^1.3.1",
|
|
35
35
|
"keccak": "^3.0.1",
|
|
36
36
|
"koa": "^2.13.1",
|
|
37
37
|
"node-fetch": "^2.6.1",
|
|
38
|
-
"protons": "^2.0.1",
|
|
39
38
|
"socket-request-client": "^1.5.0",
|
|
40
39
|
"socket-request-server": "^1.5.0",
|
|
41
40
|
"stream-browserify": "^3.0.0",
|
|
42
41
|
"varint": "^6.0.0"
|
|
43
42
|
},
|
|
44
43
|
"devDependencies": {
|
|
45
|
-
"@babel/plugin-proposal-private-methods": "^7.16.11",
|
|
46
|
-
"@rollup/plugin-eslint": "^8.0.1",
|
|
47
44
|
"@rollup/plugin-json": "^4.1.0",
|
|
48
|
-
"@vandeurenglenn/debug": "^1.0.0",
|
|
49
45
|
"coveralls": "^3.1.1",
|
|
50
46
|
"eslint": "^7.31.0",
|
|
51
47
|
"eslint-config-google": "^0.14.0",
|
|
@@ -57,7 +53,6 @@
|
|
|
57
53
|
"tape": "^5.2.2",
|
|
58
54
|
"vm-browserify": "^1.1.2",
|
|
59
55
|
"webpack": "^5.72.0",
|
|
60
|
-
"webpack-cli": "^4.9.2"
|
|
61
|
-
"webpack-node-externals": "^3.0.0"
|
|
56
|
+
"webpack-cli": "^4.9.2"
|
|
62
57
|
}
|
|
63
58
|
}
|
package/rollup.config.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { execSync } from 'child_process';
|
|
2
|
-
import lint from '@rollup/plugin-eslint'
|
|
3
2
|
import json from '@rollup/plugin-json'
|
|
4
3
|
import modify from 'rollup-plugin-modify'
|
|
5
4
|
|
|
@@ -10,7 +9,7 @@ try {
|
|
|
10
9
|
}
|
|
11
10
|
export default [{
|
|
12
11
|
input: ['src/peernet.js', 'src/messages/dht.js',
|
|
13
|
-
'src/messages/peernet
|
|
12
|
+
'src/messages/peernet.js', 'src/messages/dht-response.js', 'src/messages/request.js',
|
|
14
13
|
'src/messages/response.js'],
|
|
15
14
|
output: {
|
|
16
15
|
dir: 'dist/commonjs',
|
|
@@ -26,25 +25,7 @@ export default [{
|
|
|
26
25
|
}, {
|
|
27
26
|
input: 'src/peernet.js',
|
|
28
27
|
output: {
|
|
29
|
-
dir: 'dist/
|
|
30
|
-
format: 'cjs'
|
|
31
|
-
},
|
|
32
|
-
plugins: [
|
|
33
|
-
json(),
|
|
34
|
-
modify({
|
|
35
|
-
"import fetch from 'node-fetch'": '',
|
|
36
|
-
HTTP_IMPORT: ``,
|
|
37
|
-
SUBTLE_IMPORT: `const { subtle } = crypto`
|
|
38
|
-
}),
|
|
39
|
-
// lint({
|
|
40
|
-
// fix: true,
|
|
41
|
-
// exclude: ['package.json', "package-lock.json"]
|
|
42
|
-
// })
|
|
43
|
-
]
|
|
44
|
-
}, {
|
|
45
|
-
input: 'src/peernet.js',
|
|
46
|
-
output: {
|
|
47
|
-
file: 'dist/module/peernet.js',
|
|
28
|
+
dir: 'dist/module',
|
|
48
29
|
format: 'es'
|
|
49
30
|
},
|
|
50
31
|
plugins: [
|
|
@@ -18,12 +18,12 @@ export default class PeerDiscovery {
|
|
|
18
18
|
async discover(peer) {
|
|
19
19
|
let id = this._getPeerId(peer.id)
|
|
20
20
|
if (id) return id
|
|
21
|
-
const data = new peernet.protos['peernet-peer']({id: this.id})
|
|
21
|
+
const data = await new peernet.protos['peernet-peer']({id: this.id})
|
|
22
22
|
const node = await peernet.prepareMessage(peer.id, data.encoded)
|
|
23
23
|
|
|
24
24
|
let response = await peer.request(node.encoded)
|
|
25
|
-
response = protoFor(response)
|
|
26
|
-
response = new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
25
|
+
response = await protoFor(response)
|
|
26
|
+
response = await new peernet.protos['peernet-peer-response'](response.decoded.data)
|
|
27
27
|
|
|
28
28
|
id = response.decoded.id
|
|
29
29
|
if (id === this.id) return;
|
|
@@ -54,7 +54,7 @@ export default class PeerDiscovery {
|
|
|
54
54
|
peernet.peerMap.set(from, connections)
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
const data = new peernet.protos['peernet-peer-response']({id: this.id})
|
|
57
|
+
const data = await new peernet.protos['peernet-peer-response']({id: this.id})
|
|
58
58
|
const node = await peernet.prepareMessage(from, data.encoded)
|
|
59
59
|
|
|
60
60
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
package/src/handlers/data.js
CHANGED
|
@@ -5,8 +5,8 @@ const dataHandler = async message => {
|
|
|
5
5
|
|
|
6
6
|
const {data, id} = message
|
|
7
7
|
|
|
8
|
-
message = protoFor(data)
|
|
9
|
-
const proto = protoFor(message.decoded.data)
|
|
8
|
+
message = await protoFor(data)
|
|
9
|
+
const proto = await protoFor(message.decoded.data)
|
|
10
10
|
const from = message.decoded.from
|
|
11
11
|
|
|
12
12
|
peernet._protoHandler({id, proto}, peernet.client.connections[from], from)
|
package/src/handlers/message.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import MultiWallet from '
|
|
2
|
-
import { CodecHash
|
|
3
|
-
import PeernetMessage from './../messages/peernet-message.js'
|
|
1
|
+
import MultiWallet from '@leofcoin/multi-wallet'
|
|
2
|
+
import { CodecHash } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
5
4
|
export default class MessageHandler {
|
|
6
5
|
constructor(network) {
|
|
@@ -17,7 +16,7 @@ export default class MessageHandler {
|
|
|
17
16
|
* @return signature
|
|
18
17
|
*/
|
|
19
18
|
async hashAndSignMessage(message) {
|
|
20
|
-
const hasher = new
|
|
19
|
+
const hasher = new CodecHash(message, {name: 'peernet-message'})
|
|
21
20
|
let identity = await walletStore.get('identity')
|
|
22
21
|
identity = JSON.parse(new TextDecoder().decode(identity))
|
|
23
22
|
const wallet = new MultiWallet(this.network)
|
|
@@ -40,7 +39,7 @@ export default class MessageHandler {
|
|
|
40
39
|
data,
|
|
41
40
|
}
|
|
42
41
|
const signature = await this.hashAndSignMessage(message)
|
|
43
|
-
const node = new
|
|
42
|
+
const node = await new globalThis.peernet.protos['peernet-message']({
|
|
44
43
|
...message,
|
|
45
44
|
signature,
|
|
46
45
|
})
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/chat-message.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class ChatMessage extends FormatInterface {
|
|
|
7
6
|
return ['author', 'value', 'timestamp', 'files']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'ChatMessage'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(buffer) {
|
|
11
14
|
const name = 'chat-message'
|
|
12
|
-
super(buffer,
|
|
15
|
+
super(buffer, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/data-response.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class DataMessageResponse extends FormatInterface {
|
|
|
7
6
|
return ['hash', 'data']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetDataMessageResponse'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(data) {
|
|
11
14
|
const name = 'peernet-data-response'
|
|
12
|
-
super(data,
|
|
15
|
+
super(data, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages/data.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/data.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -9,10 +8,14 @@ export default class DataMessage extends FormatInterface {
|
|
|
9
8
|
get keys() {
|
|
10
9
|
return ['hash', 'store']
|
|
11
10
|
}
|
|
11
|
+
|
|
12
|
+
get messageName() {
|
|
13
|
+
return 'PeernetDataMessage'
|
|
14
|
+
}
|
|
12
15
|
/**
|
|
13
16
|
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
14
17
|
*/
|
|
15
18
|
constructor(data) {
|
|
16
|
-
super(data,
|
|
19
|
+
super(data, proto, {name: 'peernet-data'})
|
|
17
20
|
}
|
|
18
21
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/dht-response.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class DHTMessageResponse extends FormatInterface {
|
|
|
7
6
|
return ['hash', 'has']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetDHTMessageResponse'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(data) {
|
|
11
14
|
const name = 'peernet-dht-response'
|
|
12
|
-
super(data,
|
|
15
|
+
super(data, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages/dht.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/dht.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -18,8 +17,12 @@ export default class DHTMessage extends FormatInterface {
|
|
|
18
17
|
return ['hash', 'store']
|
|
19
18
|
}
|
|
20
19
|
|
|
20
|
+
get messageName() {
|
|
21
|
+
return 'PeernetDHTMessage'
|
|
22
|
+
}
|
|
23
|
+
|
|
21
24
|
constructor(data) {
|
|
22
25
|
const name = 'peernet-dht'
|
|
23
|
-
super(data,
|
|
26
|
+
super(data, proto, {name})
|
|
24
27
|
}
|
|
25
28
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/peer-response.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class PeerMessageResponse extends FormatInterface {
|
|
|
7
6
|
return ['id']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetPeerMessageResponse'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(data) {
|
|
11
14
|
const name = 'peernet-peer-response'
|
|
12
|
-
super(data,
|
|
15
|
+
super(data, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages/peer.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/peer.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class PeerMessage extends FormatInterface {
|
|
|
7
6
|
return ['id']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetPeerMessage'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(data) {
|
|
11
14
|
const name = 'peernet-peer'
|
|
12
|
-
super(data,
|
|
15
|
+
super(data, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/peernet.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class PeernetMessage extends FormatInterface {
|
|
|
7
6
|
return ['data', 'signature', 'from', 'to', 'id']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetMessage'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(buffer) {
|
|
11
14
|
const name = 'peernet-message'
|
|
12
|
-
super(buffer,
|
|
15
|
+
super(buffer, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages/ps.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/ps.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class PsMessage extends FormatInterface {
|
|
|
7
6
|
return ['data', 'topic']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PsMessage'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(buffer) {
|
|
11
14
|
const name = 'peernet-ps'
|
|
12
|
-
super(buffer,
|
|
15
|
+
super(buffer, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages/request.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/request.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class RequestMessage extends FormatInterface {
|
|
|
7
6
|
return ['request']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetRequestMessage'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(data) {
|
|
11
14
|
const name = 'peernet-request'
|
|
12
|
-
super(data,
|
|
15
|
+
super(data, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages/response.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
1
|
import proto from './../proto/response.proto.js'
|
|
3
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
4
3
|
|
|
@@ -7,8 +6,12 @@ export default class ResponseMessage extends FormatInterface {
|
|
|
7
6
|
return ['response']
|
|
8
7
|
}
|
|
9
8
|
|
|
9
|
+
get messageName() {
|
|
10
|
+
return 'PeernetResponseMessage'
|
|
11
|
+
}
|
|
12
|
+
|
|
10
13
|
constructor(data) {
|
|
11
14
|
const name = 'peernet-response'
|
|
12
|
-
super(data,
|
|
15
|
+
super(data, proto, {name})
|
|
13
16
|
}
|
|
14
17
|
}
|
package/src/messages.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * as PeernetMessage from './messages/peernet.js'
|
|
2
|
+
export * as DHTMessage from './messages/dht.js'
|
|
3
|
+
export * as DHTMessageResponse from './messages/dht-response.js'
|
|
4
|
+
export * as DataMessage from './messages/data.js'
|
|
5
|
+
export * as PsMessage from './messages/ps.js'
|
|
6
|
+
export * as PeerMessage from './messages/peer.js'
|
|
7
|
+
export * as RequestMessage from './messages/request.js'
|
|
8
|
+
export * as ResponseMessage from './messages/response.js'
|
|
9
|
+
export * as PeerMessageResponse from './messages/peer-response.js'
|
|
10
|
+
export * as DataMessageResponse from './messages/data-response.js'
|
|
11
|
+
export * as ChatMessage from './messages/chat.js'
|
package/src/peernet.js
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug'
|
|
2
|
-
import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
|
|
3
|
-
import LeofcoinStorage from '@leofcoin/storage'
|
|
4
|
-
import PeernetMessage from './messages/peernet-message.js'
|
|
5
|
-
import DHTMessage from './messages/dht.js'
|
|
6
|
-
import DHTMessageResponse from './messages/dht-response.js'
|
|
7
|
-
import DataMessage from './messages/data.js'
|
|
8
|
-
import PsMessage from './messages/ps.js'
|
|
9
|
-
import PeerMessage from './messages/peer.js'
|
|
10
|
-
import RequestMessage from './messages/request.js'
|
|
11
|
-
import ResponseMessage from './messages/response.js'
|
|
12
|
-
import PeerMessageResponse from './messages/peer-response.js'
|
|
13
|
-
import DataMessageResponse from './messages/data-response.js'
|
|
14
|
-
import ChatMessage from './messages/chat-message.js'
|
|
15
2
|
import PeerDiscovery from './discovery/peer-discovery'
|
|
16
3
|
import DHT from './dht/dht.js'
|
|
17
|
-
import { CodecHash
|
|
4
|
+
import { CodecHash, codecs} from '@leofcoin/codec-format-interface'
|
|
18
5
|
import { protoFor, target } from './utils/utils.js'
|
|
19
|
-
import generateAccount from './../node_modules/@leofcoin/generate-account/dist/module/generate-account'
|
|
20
6
|
import MessageHandler from './handlers/message.js'
|
|
21
7
|
import dataHandler from './handlers/data.js'
|
|
22
8
|
import { encapsulatedError, dhtError,
|
|
@@ -69,6 +55,14 @@ export default class Peernet {
|
|
|
69
55
|
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
|
|
70
56
|
}
|
|
71
57
|
|
|
58
|
+
get protos() {
|
|
59
|
+
return globalThis.peernet.protos
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get codecs() {
|
|
63
|
+
return codecs
|
|
64
|
+
}
|
|
65
|
+
|
|
72
66
|
addProto(name, proto) {
|
|
73
67
|
if (!this.protos[name]) this.protos[name] = proto
|
|
74
68
|
}
|
|
@@ -78,6 +72,10 @@ export default class Peernet {
|
|
|
78
72
|
}
|
|
79
73
|
|
|
80
74
|
async addStore(name, prefix, root, isPrivate = true) {
|
|
75
|
+
if (!globalThis.LeofcoinStorage) {
|
|
76
|
+
const importee = await import(/* webpackChunkName: "storage" */ '@leofcoin/storage')
|
|
77
|
+
globalThis.LeofcoinStorage = importee.default
|
|
78
|
+
}
|
|
81
79
|
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
82
80
|
name === 'data' || name === 'message') isPrivate = false
|
|
83
81
|
|
|
@@ -85,7 +83,7 @@ export default class Peernet {
|
|
|
85
83
|
if (this.hasDaemon) {
|
|
86
84
|
Storage = LeofcoinStorageClient
|
|
87
85
|
} else {
|
|
88
|
-
Storage =
|
|
86
|
+
Storage = LeofcoinStorage
|
|
89
87
|
}
|
|
90
88
|
globalThis[`${name}Store`] = globalThis[`${name}Store`] ||
|
|
91
89
|
await new Storage(name, root)
|
|
@@ -150,6 +148,20 @@ export default class Peernet {
|
|
|
150
148
|
this.storePrefix = options.storePrefix
|
|
151
149
|
this.root = options.root
|
|
152
150
|
|
|
151
|
+
const {
|
|
152
|
+
RequestMessage,
|
|
153
|
+
ResponseMessage,
|
|
154
|
+
PeerMessage,
|
|
155
|
+
PeerMessageResponse,
|
|
156
|
+
PeernetMessage,
|
|
157
|
+
DHTMessage,
|
|
158
|
+
DHTMessageResponse,
|
|
159
|
+
DataMessage,
|
|
160
|
+
DataMessageResponse,
|
|
161
|
+
PsMessage,
|
|
162
|
+
ChatMessage
|
|
163
|
+
} = await import(/* webpackChunkName: "messages" */ './messages.js')
|
|
164
|
+
|
|
153
165
|
/**
|
|
154
166
|
* proto Object containing protos
|
|
155
167
|
* @type {Object}
|
|
@@ -159,6 +171,7 @@ export default class Peernet {
|
|
|
159
171
|
* @property {DataMessage} protos[peernet-data] messageNode
|
|
160
172
|
* @property {DataMessageResponse} protos[peernet-data-response] messageNode
|
|
161
173
|
*/
|
|
174
|
+
|
|
162
175
|
globalThis.peernet.protos = {
|
|
163
176
|
'peernet-request': RequestMessage,
|
|
164
177
|
'peernet-response': ResponseMessage,
|
|
@@ -173,16 +186,11 @@ export default class Peernet {
|
|
|
173
186
|
'chat-message': ChatMessage,
|
|
174
187
|
}
|
|
175
188
|
|
|
176
|
-
this.protos = globalThis.peernet.protos
|
|
177
|
-
this.codecs = codecs
|
|
178
|
-
|
|
179
189
|
this._messageHandler = new MessageHandler(this.network)
|
|
180
190
|
|
|
181
191
|
const {daemon, environment} = await target()
|
|
182
192
|
this.hasDaemon = daemon
|
|
183
193
|
|
|
184
|
-
HTTP_IMPORT
|
|
185
|
-
|
|
186
194
|
for (const store of this.defaultStores) {
|
|
187
195
|
await this.addStore(store, options.storePrefix, options.root)
|
|
188
196
|
}
|
|
@@ -202,6 +210,9 @@ export default class Peernet {
|
|
|
202
210
|
}
|
|
203
211
|
} catch (e) {
|
|
204
212
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
213
|
+
|
|
214
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
|
|
215
|
+
const generateAccount = importee.default
|
|
205
216
|
const wallet = {}
|
|
206
217
|
const {identity, accounts, config} = await generateAccount(this.network)
|
|
207
218
|
walletStore.put('version', new TextEncoder().encode(1))
|
|
@@ -235,11 +246,13 @@ export default class Peernet {
|
|
|
235
246
|
*/
|
|
236
247
|
pubsub.subscribe('peer:data', dataHandler)
|
|
237
248
|
|
|
249
|
+
|
|
250
|
+
const importee = await import(/* webpackChunkName: "peernet-swarm" */ '@leofcoin/peernet-swarm')
|
|
238
251
|
/**
|
|
239
252
|
* @access public
|
|
240
253
|
* @type {PeernetClient}
|
|
241
254
|
*/
|
|
242
|
-
this.client = new
|
|
255
|
+
this.client = new importee.default(this.id)
|
|
243
256
|
if (globalThis.onbeforeunload) {
|
|
244
257
|
globalThis.addEventListener('beforeunload', async () => this.client.close());
|
|
245
258
|
}
|
|
@@ -281,7 +294,7 @@ export default class Peernet {
|
|
|
281
294
|
if (store.private) has = false
|
|
282
295
|
else has = await store.has(hash)
|
|
283
296
|
}
|
|
284
|
-
const data = new
|
|
297
|
+
const data = await new this.protos['peernet-dht-response']({hash, has})
|
|
285
298
|
const node = await this.prepareMessage(from, data.encoded)
|
|
286
299
|
|
|
287
300
|
this.sendMessage(peer, id, node.encoded)
|
|
@@ -297,7 +310,7 @@ export default class Peernet {
|
|
|
297
310
|
data = await store.get(hash)
|
|
298
311
|
|
|
299
312
|
if (data) {
|
|
300
|
-
data = new
|
|
313
|
+
data = await new this.protos['peernet-data-response']({hash, data});
|
|
301
314
|
|
|
302
315
|
const node = await this.prepareMessage(from, data.encoded)
|
|
303
316
|
this.sendMessage(peer, id, node.encoded)
|
|
@@ -326,16 +339,16 @@ export default class Peernet {
|
|
|
326
339
|
*/
|
|
327
340
|
async walk(hash) {
|
|
328
341
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
329
|
-
const data = new
|
|
342
|
+
const data = await new this.protos['peernet-dht']({hash})
|
|
330
343
|
const clientId = this.client.id
|
|
331
344
|
const walk = async peer => {
|
|
332
345
|
const node = await this.prepareMessage(peer.peerId, data.encoded)
|
|
333
346
|
let result = await peer.request(node.encoded)
|
|
334
347
|
result = new Uint8Array(Object.values(result))
|
|
335
|
-
let proto = protoFor(result)
|
|
348
|
+
let proto = await protoFor(result)
|
|
336
349
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
337
350
|
const from = proto.decoded.from
|
|
338
|
-
proto = protoFor(proto.decoded.data)
|
|
351
|
+
proto = await protoFor(proto.decoded.data)
|
|
339
352
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
340
353
|
|
|
341
354
|
// TODO: give ip and port (just used for location)
|
|
@@ -434,7 +447,7 @@ export default class Peernet {
|
|
|
434
447
|
if (peer.peerId === id) return peer
|
|
435
448
|
})
|
|
436
449
|
|
|
437
|
-
let data = new
|
|
450
|
+
let data = await new this.protos['peernet-data']({hash, store: store?.name ? store?.name : store});
|
|
438
451
|
|
|
439
452
|
const node = await this.prepareMessage(id, data.encoded)
|
|
440
453
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
@@ -445,8 +458,8 @@ export default class Peernet {
|
|
|
445
458
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
446
459
|
}
|
|
447
460
|
data = new Uint8Array(Object.values(data))
|
|
448
|
-
let proto = protoFor(data)
|
|
449
|
-
proto = protoFor(proto.decoded.data)
|
|
461
|
+
let proto = await protoFor(data)
|
|
462
|
+
proto = await protoFor(proto.decoded.data)
|
|
450
463
|
// TODO: store data automaticly or not
|
|
451
464
|
return proto.decoded.data
|
|
452
465
|
|
|
@@ -580,7 +593,7 @@ export default class Peernet {
|
|
|
580
593
|
if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic)
|
|
581
594
|
if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
|
|
582
595
|
const id = Math.random().toString(36).slice(-12)
|
|
583
|
-
data = new
|
|
596
|
+
data = await new this.protos['peernet-ps']({data, topic})
|
|
584
597
|
for (const peer of this.connections) {
|
|
585
598
|
if (peer.peerId !== this.peerId) {
|
|
586
599
|
const node = await this.prepareMessage(peer.peerId, data.encoded)
|
|
@@ -591,7 +604,7 @@ export default class Peernet {
|
|
|
591
604
|
}
|
|
592
605
|
|
|
593
606
|
createHash(data, name) {
|
|
594
|
-
return new
|
|
607
|
+
return new CodeHash(data, {name})
|
|
595
608
|
}
|
|
596
609
|
|
|
597
610
|
/**
|
package/webpack.config.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const webpack = require('webpack');
|
|
3
3
|
module.exports = [{
|
|
4
|
-
entry: './
|
|
4
|
+
entry: './src/peernet.js',
|
|
5
5
|
mode: 'production',
|
|
6
6
|
plugins: [
|
|
7
7
|
// Work around for Buffer is undefined:
|
|
@@ -26,6 +26,9 @@ module.exports = [{
|
|
|
26
26
|
optimization: {
|
|
27
27
|
minimize: false
|
|
28
28
|
},
|
|
29
|
+
experiments: {
|
|
30
|
+
outputModule: true
|
|
31
|
+
},
|
|
29
32
|
resolve: {
|
|
30
33
|
extensions: [ '.ts', '.js' ],
|
|
31
34
|
fallback: {
|
|
@@ -41,9 +44,9 @@ resolve: {
|
|
|
41
44
|
},
|
|
42
45
|
output: {
|
|
43
46
|
library: {
|
|
44
|
-
|
|
45
|
-
type: 'global'
|
|
47
|
+
type: 'module'
|
|
46
48
|
},
|
|
49
|
+
chunkFilename: '[name].js',
|
|
47
50
|
filename: 'peernet.js',
|
|
48
51
|
path: path.resolve(__dirname, 'dist', 'browser'),
|
|
49
52
|
},
|