@leofcoin/peernet 0.9.5 → 0.9.6

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.
Files changed (50) hide show
  1. package/package.json +1 -1
  2. package/src/client.js +71 -0
  3. package/src/codec/codec-format-interface.js +139 -0
  4. package/src/codec/codec.js +114 -0
  5. package/src/codec/codecs.js +79 -0
  6. package/src/dht/dht.js +125 -0
  7. package/src/discovery/peer-discovery.js +75 -0
  8. package/src/errors/errors.js +12 -0
  9. package/src/handlers/message.js +52 -0
  10. package/src/hash/hash.js +145 -0
  11. package/src/http/api.js +115 -0
  12. package/src/http/client/api.js +41 -0
  13. package/src/http/client/client.js +10 -0
  14. package/src/http/client/http-client.js +44 -0
  15. package/src/http/client/storage.js +36 -0
  16. package/src/http/http.js +28 -0
  17. package/src/messages/chat-message.js +14 -0
  18. package/src/messages/data-response.js +14 -0
  19. package/src/messages/data.js +14 -0
  20. package/src/messages/dht-response.js +15 -0
  21. package/src/messages/dht.js +25 -0
  22. package/src/messages/peer-response.js +14 -0
  23. package/src/messages/peer.js +14 -0
  24. package/src/messages/peernet-message.js +14 -0
  25. package/src/messages/ps.js +14 -0
  26. package/src/messages/request.js +14 -0
  27. package/src/messages/response.js +14 -0
  28. package/src/peer-info.js +9 -0
  29. package/src/peer.js +43 -0
  30. package/src/peernet.js +680 -0
  31. package/src/proto/chat-message.proto.js +7 -0
  32. package/src/proto/data-response.proto.js +7 -0
  33. package/src/proto/data.proto.js +7 -0
  34. package/src/proto/dht-response.proto.js +7 -0
  35. package/src/proto/dht.proto.js +7 -0
  36. package/src/proto/peer-response.proto.js +6 -0
  37. package/src/proto/peer.proto.js +6 -0
  38. package/src/proto/peernet.proto.js +9 -0
  39. package/src/proto/ps.proto.js +6 -0
  40. package/src/proto/request.proto.js +6 -0
  41. package/src/proto/response.proto.js +6 -0
  42. package/src/server.js +25 -0
  43. package/src/utils/utils.js +78 -0
  44. package/.nyc_output/39a61420-013f-4db1-a597-7c5444da26e7.json +0 -1
  45. package/.nyc_output/4b387323-32a3-4eee-8f05-d13f2e0a5bf4.json +0 -1
  46. package/.nyc_output/ef71cf24-d9d9-45dd-814f-8d53cb6769f3.json +0 -1
  47. package/.nyc_output/processinfo/39a61420-013f-4db1-a597-7c5444da26e7.json +0 -1
  48. package/.nyc_output/processinfo/4b387323-32a3-4eee-8f05-d13f2e0a5bf4.json +0 -1
  49. package/.nyc_output/processinfo/ef71cf24-d9d9-45dd-814f-8d53cb6769f3.json +0 -1
  50. package/.nyc_output/processinfo/index.json +0 -1
@@ -0,0 +1,145 @@
1
+ import createKeccakHash from 'keccak';
2
+ import varint from 'varint';
3
+ import bs32 from 'bs32';
4
+ import bs58 from 'bs58';
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.test(buffer)) this.fromBs32(buffer)
29
+ else this.fromBs58(buffer)
30
+ } else if (typeof buffer === 'object') this.fromJSON(buffer)
31
+ }
32
+ }
33
+
34
+ get prefix() {
35
+ return Buffer.concat([this.discoCodec.codecBuffer, this.length])
36
+ }
37
+
38
+ get length() {
39
+ return Buffer.from(varint.encode(this.size))
40
+ }
41
+
42
+ get buffer() {
43
+ return this.hash
44
+ }
45
+
46
+ toHex() {
47
+ return this.hash.toString('hex')
48
+ }
49
+
50
+ fromHex(hex) {
51
+ return this.decode(Buffer.from(hex, 'hex'))
52
+ }
53
+
54
+ fromJSON(json) {
55
+ return this.encode(Buffer.from(JSON.stringify(json)))
56
+ }
57
+
58
+ toBs32() {
59
+ return bs32.encode(this.hash)
60
+ }
61
+
62
+ fromBs32(bs) {
63
+ return this.decode(bs32.decode(bs))
64
+ }
65
+
66
+ toBs58() {
67
+ return bs58.encode(this.hash)
68
+ }
69
+
70
+ fromBs58(bs) {
71
+ return this.decode(bs58.decode(bs))
72
+ }
73
+
74
+ toString(encoding = 'utf8') {
75
+ return this.hash.toString(encoding)
76
+ }
77
+
78
+ encode(buffer, name) {
79
+ if (!this.name && name) this.name = name;
80
+ if (!buffer) buffer = this.buffer;
81
+ this.discoCodec = new Codec(this.name, this.codecs)
82
+ this.discoCodec.fromName(this.name)
83
+ let hashAlg = this.discoCodec.hashAlg
84
+ if (hashAlg.includes('dbl')) {
85
+ hashAlg = hashAlg.replace('dbl-', '')
86
+ buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
87
+ }
88
+ this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
89
+ this.size = this.digest.length
90
+
91
+ this.codec = this.discoCodec.encode();
92
+ this.codec = this.discoCodec.codecBuffer
93
+ this.hash = Buffer.concat([
94
+ this.prefix,
95
+ this.digest,
96
+ ])
97
+
98
+ return this.hash
99
+ }
100
+
101
+ validate(buffer) {
102
+ if (Buffer.isBuffer(buffer)) {
103
+ const codec = varint.decode(buffer);
104
+ if (this.codecs[codec]) {
105
+ this.decode(buffer)
106
+ } else {
107
+ this.encode(buffer)
108
+ }
109
+ }
110
+ if (typeof buffer === 'string') {
111
+ if (isHex(buffer)) this.fromHex(buffer)
112
+ if (bs32.test(buffer)) this.fromBs32(buffer)
113
+ }
114
+ if (typeof buffer === 'object') this.fromJSON(buffer)
115
+ }
116
+
117
+ decode(buffer) {
118
+ this.hash = buffer
119
+ const codec = varint.decode(buffer);
120
+
121
+ this.discoCodec = new Codec(codec, this.codecs)
122
+ // TODO: validate codec
123
+ buffer = buffer.slice(varint.decode.bytes);
124
+ this.size = varint.decode(buffer);
125
+ this.digest = buffer.slice(varint.decode.bytes);
126
+ if (this.digest.length !== this.size) {
127
+ throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
128
+ }
129
+
130
+ // const discoCodec = new Codec(codec, this.codecs)
131
+
132
+ this.name = this.discoCodec.name
133
+
134
+
135
+ this.size = this.digest.length
136
+
137
+ return {
138
+ codec: this.codec,
139
+ name: this.name,
140
+ size: this.size,
141
+ length: this.length,
142
+ digest: this.digest,
143
+ }
144
+ }
145
+ }
@@ -0,0 +1,115 @@
1
+ import {version} from './../../package.json'
2
+
3
+ export default {
4
+ version: ({send}) => send({client: '@peernet/api/http', version}),
5
+ ready: ({send}) => {
6
+ if (globalThis.states.ready) send(true)
7
+ else pubsub.subscribe('ready', () => send(true))
8
+ },
9
+ storage: async (params, {send, error}) => {
10
+ console.log(params);
11
+ const {name, root, key, value, method} = params
12
+ try {
13
+ if (name && root) {
14
+ globalThis[name] = globalThis[name] || await new LeofcoinStorage(name, root)
15
+ } else {
16
+ return error('Expected name & root')
17
+ }
18
+ if (method === 'put') {
19
+ await globalThis[name].put(key, value)
20
+ return send('ok')
21
+ }
22
+ if (method === 'remove') {
23
+ await globalThis[name].remove(key, value)
24
+ return send('ok')
25
+ }
26
+ value = await globalThis[name].get(key)
27
+ return send(value)
28
+ } catch (e) {
29
+ return error(e)
30
+ }
31
+ },
32
+ getConfig: async (params, {send, error}) => {
33
+ try {
34
+ const config = await api.getConfig(params)
35
+ send(config)
36
+ } catch (e) {
37
+ error(e)
38
+ }
39
+ },
40
+ setMinerConfig: async (params, {send, error}) => {
41
+ try {
42
+ await api.setMinerConfig(params)
43
+ send('ok')
44
+ } catch (e) {
45
+ error(e)
46
+ }
47
+ },
48
+ getMinerConfig: async ({send, error}) => {
49
+ try {
50
+ const config = await api.getMinerConfig()
51
+ send(config)
52
+ } catch (e) {
53
+ error(e)
54
+ }
55
+ },
56
+ wallet: async ({send}) => {
57
+ const wallet = await walletStore.get()
58
+ send(wallet)
59
+ },
60
+ addresses: async ({send, error}) => {
61
+ try {
62
+ const adresses = await api.addresses()
63
+ send(adresses)
64
+ } catch (e) {
65
+ error(e)
66
+ }
67
+ },
68
+ accountNames: async (params, {send, error}) => {
69
+ try {
70
+ const adresses = await api.accountNames(params.index)
71
+ send(adresses)
72
+ } catch (e) {
73
+ error(e)
74
+ }
75
+ },
76
+ accounts: async ({send}) => {
77
+ const accounts = await accountStore.get()
78
+ send(accounts)
79
+ },
80
+ account: async (params, {send}) => {
81
+ const account = await accountStore.get(params)
82
+ send(account)
83
+ },
84
+ balance: async (params, {send, error}) => {
85
+ console.log('balance');
86
+ try {
87
+ console.log(await api.getBalanceForAddress(params.address));
88
+ const value = await api.getBalanceForAddress(params.address)
89
+ send(value)
90
+ } catch (e) {
91
+ console.log(e);
92
+ error(e)
93
+ }
94
+ },
95
+ balanceAfter: async (params, {send, error}) => {
96
+ try {
97
+ const value = await api.getBalanceForAddressAfter(params.address, params.index)
98
+ send(value)
99
+ } catch (e) {
100
+ error(e)
101
+ }
102
+ },
103
+ mine: async (params, {send, error}) => {
104
+ api.mine(params)
105
+ send('ok')
106
+ },
107
+ lastBlock: async ({send, error}) => {
108
+ try {
109
+ const value = await api.lastBlock()
110
+ send(value)
111
+ } catch (e) {
112
+ error(e)
113
+ }
114
+ },
115
+ }
@@ -0,0 +1,41 @@
1
+ import HttpClientApi from './http-client.js'
2
+
3
+ export default class extends HttpClientApi {
4
+ constructor(config = {}) {
5
+ config.apiPath = 'api';
6
+ return (async () => {
7
+ await super(config)
8
+
9
+ this.properties = {
10
+ wallet: 'get',
11
+ version: 'get',
12
+ addresses: 'get',
13
+ config: 'get',
14
+ account: 'get',
15
+ accounts: 'get',
16
+ transaction: 'any',
17
+ transactions: 'get',
18
+ block: 'get',
19
+ blocks: 'get',
20
+ }
21
+ this.keys = Object.keys(this.properties)
22
+ return this
23
+ })()
24
+ }
25
+
26
+ async request(url, data) {
27
+ return await this.client.request({url, params: data})
28
+ }
29
+
30
+ async ready() {
31
+ return await this.request('ready')
32
+ }
33
+
34
+ async version() {
35
+ return await this.request('version')
36
+ }
37
+
38
+ async account(index) {
39
+ return await this.request('account', {index})
40
+ }
41
+ }
@@ -0,0 +1,10 @@
1
+ import HttpClientApi from './api.js'
2
+
3
+ export default (config = {}) => {
4
+ if (typeof config !== 'object') config = {}
5
+ if (!config.protocol) config.protocol = 'peernet-v0.1.0'
6
+ if (!config.port) config.port = 1000
7
+ if (!config.host) config.host = '127.0.0.1'
8
+
9
+ return new HttpClientApi(config)
10
+ }
@@ -0,0 +1,44 @@
1
+ import Client from './../../../node_modules/socket-request-client/src/index.js'
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
+ }
@@ -0,0 +1,36 @@
1
+ export default class LeofcoinStorageClient {
2
+ constructor(name, root) {
3
+ this.name = name
4
+ this.root = root
5
+ }
6
+
7
+ async get(key) {
8
+ try {
9
+ const result = await globalThis.peernet.client.request('storage', {
10
+ name: this.name,
11
+ root: this.root,
12
+ key,
13
+ })
14
+ return result
15
+ } catch (e) {
16
+ console.log(e);
17
+ return undefined
18
+ }
19
+ }
20
+
21
+ async put(key, value) {
22
+ try {
23
+ const result = await globalThis.peernet.client.request('storage', {
24
+ name: this.name,
25
+ root: this.root,
26
+ key,
27
+ value,
28
+ method: 'put',
29
+ })
30
+ return result
31
+ } catch (e) {
32
+ console.log(e);
33
+ return undefined
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,28 @@
1
+ import api from './api.js'
2
+ import server from './../../node_modules/socket-request-server/src/index'
3
+ import {createServer} from 'http'
4
+ import Koa from 'koa'
5
+ import {version} from './../../package.json'
6
+ export default (config = {}) => {
7
+ if (typeof config !== 'object') config = {}
8
+ if (!config.protocol) config.protocol = 'peernet-v0.1.0'
9
+ if (!config.port) config.port = 2000
10
+ if (!config.host) config.host = '127.0.0.1'
11
+
12
+ const app = new Koa()
13
+
14
+ app.use(async (ctx) => {
15
+ const url = ctx.url.split('/api/')[1]
16
+ if (url === 'version') ctx.body = {client: '@peernet/api/http', version}
17
+ })
18
+
19
+ const httpServer = createServer(app.callback())
20
+
21
+ config.httpServer = httpServer
22
+
23
+ httpServer.listen(config.port, () => {
24
+ console.log(`listening on ${config.port}`);
25
+ });
26
+
27
+ return server(config, api)
28
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/chat-message.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class ChatMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['author', 'value', 'timestamp', 'files']
8
+ }
9
+
10
+ constructor(buffer) {
11
+ const name = 'chat-message'
12
+ super(buffer, protons(proto).ChatMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/data-response.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class DataMessageResponse extends CodecFormat {
6
+ get keys() {
7
+ return ['hash', 'data']
8
+ }
9
+
10
+ constructor(data) {
11
+ const name = 'peernet-data-response'
12
+ super(data, protons(proto).PeernetDataMessageResponse, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/data.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class DataMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['hash', 'store']
8
+ }
9
+
10
+ constructor(data) {
11
+ const name = 'peernet-data'
12
+ super(data, protons(proto).PeernetDataMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,15 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/dht-response.proto.js'
3
+ import {encode, decode} from 'bs32'
4
+ import CodecFormat from './../codec/codec-format-interface.js'
5
+
6
+ export default class DHTMessageResponse extends CodecFormat {
7
+ get keys() {
8
+ return ['hash', 'has']
9
+ }
10
+
11
+ constructor(data) {
12
+ const name = 'peernet-dht-response'
13
+ super(data, protons(proto).PeernetDHTMessageResponse, {name})
14
+ }
15
+ }
@@ -0,0 +1,25 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/dht.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ /**
6
+ * @example `
7
+ new DHTMessage(hash, store)
8
+ // store = optional if not set, peernet checks every store
9
+ let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
10
+ message = new DHTMessage('hashmvbs124xcfd...', 'block')
11
+ `
12
+ */
13
+ export default class DHTMessage extends CodecFormat {
14
+ /**
15
+ *
16
+ */
17
+ get keys() {
18
+ return ['hash', 'store']
19
+ }
20
+
21
+ constructor(data) {
22
+ const name = 'peernet-dht'
23
+ super(data, protons(proto).PeernetDHTMessage, {name})
24
+ }
25
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/peer-response.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class PeerMessageResponse extends CodecFormat {
6
+ get keys() {
7
+ return ['id']
8
+ }
9
+
10
+ constructor(data) {
11
+ const name = 'peernet-peer-response'
12
+ super(data, protons(proto).PeernetPeerMessageResponse, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/peer.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class PeerMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['id']
8
+ }
9
+
10
+ constructor(data) {
11
+ const name = 'peernet-peer'
12
+ super(data, protons(proto).PeernetPeerMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/peernet.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class PeernetMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['data', 'signature', 'from', 'to', 'id']
8
+ }
9
+
10
+ constructor(buffer) {
11
+ const name = 'peernet-message'
12
+ super(buffer, protons(proto).PeernetMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/ps.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class PsMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['data', 'topic']
8
+ }
9
+
10
+ constructor(buffer) {
11
+ const name = 'peernet-ps'
12
+ super(buffer, protons(proto).PsMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/request.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class RequestMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['request']
8
+ }
9
+
10
+ constructor(data) {
11
+ const name = 'peernet-request'
12
+ super(data, protons(proto).PeernetRequestMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import protons from 'protons'
2
+ import proto from './../proto/response.proto.js'
3
+ import CodecFormat from './../codec/codec-format-interface.js'
4
+
5
+ export default class ResponseMessage extends CodecFormat {
6
+ get keys() {
7
+ return ['response']
8
+ }
9
+
10
+ constructor(data) {
11
+ const name = 'peernet-response'
12
+ super(data, protons(proto).PeernetResponseMessage, {name})
13
+ }
14
+ }
@@ -0,0 +1,9 @@
1
+ import CodecFormat from './codec/codec-format-interface'
2
+
3
+ export default class PeerInfo extends CodecFormat {
4
+ constructor(data, options) {
5
+ super(data, options)
6
+
7
+ this.keys = ['id', 'address', 'family']
8
+ }
9
+ }
package/src/peer.js ADDED
@@ -0,0 +1,43 @@
1
+ export default class PeernetPeer {
2
+ constructor(id, connection) {
3
+ this.id = id
4
+ this.connection = connection
5
+
6
+ this.connection.on('data', (message) => pubsub.publish('peernet.data', JSON.parse(message.toString())))
7
+ }
8
+
9
+ request(data) {
10
+ return new Promise((resolve, reject) => {
11
+ const id = Math.random().toString(36).slice(-12)
12
+ data = Buffer.from(JSON.stringify({id, data}))
13
+ const _onData = (message) => {
14
+ if (message.id !== id) return
15
+
16
+ resolve(message.data)
17
+ }
18
+
19
+ pubsub.subscribe('peernet.data', _onData)
20
+
21
+ // cleanup subscriptions
22
+ setTimeout(() => {
23
+ pubsub.unsubscribe('peernet.data', _onData)
24
+ }, 5000);
25
+
26
+ this.write(data)
27
+ });
28
+ }
29
+
30
+ write(data) {
31
+ if (!Buffer.isBuffer(data)) data = Buffer.from(data)
32
+ this.connection.write(data)
33
+ }
34
+
35
+ on(event = 'peernet.data', cb) {
36
+ pubsub.subscribe(event, cb)
37
+ // this.connection.on(event, cb)
38
+ }
39
+
40
+ removeListener(event = 'data', cb) {
41
+ pubsub.unsubscribe(event, cb)
42
+ }
43
+ }