@leofcoin/peernet 0.11.25 → 0.11.28
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/peernet.js +14 -39
- package/dist/commonjs/peernet.js +15 -48
- package/dist/module/peernet.js +14 -39
- package/package.json +2 -2
- package/rollup.config.js +1 -9
- package/src/peernet.js +2 -1
- package/dist/commonjs/client-5633ba04.js +0 -321
- package/dist/commonjs/http-78686629.js +0 -328
- package/src/http/api.js +0 -115
- package/src/http/client/api.js +0 -41
- package/src/http/client/client.js +0 -10
- package/src/http/client/http-client.js +0 -44
- package/src/http/client/storage.js +0 -36
- package/src/http/http.js +0 -28
package/dist/browser/peernet.js
CHANGED
|
@@ -449,10 +449,20 @@ class Peer {
|
|
|
449
449
|
async #init() {
|
|
450
450
|
try {
|
|
451
451
|
const iceServers = [{
|
|
452
|
-
|
|
452
|
+
urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
|
|
453
|
+
}, {
|
|
454
|
+
urls: "stun:openrelay.metered.ca:80",
|
|
455
|
+
}, {
|
|
456
|
+
urls: "turn:openrelay.metered.ca:443",
|
|
457
|
+
username: "openrelayproject",
|
|
458
|
+
credential: "openrelayproject",
|
|
459
|
+
}, {
|
|
460
|
+
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
|
461
|
+
username: "openrelayproject",
|
|
462
|
+
credential: "openrelayproject",
|
|
453
463
|
}];
|
|
454
464
|
|
|
455
|
-
this.#connection = new wrtc.RTCPeerConnection();
|
|
465
|
+
this.#connection = new wrtc.RTCPeerConnection({iceServers});
|
|
456
466
|
|
|
457
467
|
this.#connection.onicecandidate = ({ candidate }) => {
|
|
458
468
|
if (candidate) {
|
|
@@ -719,43 +729,6 @@ class Client {
|
|
|
719
729
|
|
|
720
730
|
}
|
|
721
731
|
|
|
722
|
-
class LeofcoinStorageClient {
|
|
723
|
-
constructor(name, root) {
|
|
724
|
-
this.name = name;
|
|
725
|
-
this.root = root;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
async get(key) {
|
|
729
|
-
try {
|
|
730
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
731
|
-
name: this.name,
|
|
732
|
-
root: this.root,
|
|
733
|
-
key,
|
|
734
|
-
});
|
|
735
|
-
return result
|
|
736
|
-
} catch (e) {
|
|
737
|
-
console.log(e);
|
|
738
|
-
return undefined
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
async put(key, value) {
|
|
743
|
-
try {
|
|
744
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
745
|
-
name: this.name,
|
|
746
|
-
root: this.root,
|
|
747
|
-
key,
|
|
748
|
-
value,
|
|
749
|
-
method: 'put',
|
|
750
|
-
});
|
|
751
|
-
return result
|
|
752
|
-
} catch (e) {
|
|
753
|
-
console.log(e);
|
|
754
|
-
return undefined
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
|
|
759
732
|
var proto$a = `
|
|
760
733
|
// PeernetMessage
|
|
761
734
|
message PeernetMessage {
|
|
@@ -1989,6 +1962,8 @@ class Peernet {
|
|
|
1989
1962
|
try {
|
|
1990
1963
|
const pub = await accountStore.get('public');
|
|
1991
1964
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1965
|
+
const accounts = await walletStore.get('accounts');
|
|
1966
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts));
|
|
1992
1967
|
} catch (e) {
|
|
1993
1968
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1994
1969
|
const {identity, accounts, config} = await generateAccount(this.network);
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -447,10 +447,20 @@ class Peer {
|
|
|
447
447
|
async #init() {
|
|
448
448
|
try {
|
|
449
449
|
const iceServers = [{
|
|
450
|
-
|
|
450
|
+
urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
|
|
451
|
+
}, {
|
|
452
|
+
urls: "stun:openrelay.metered.ca:80",
|
|
453
|
+
}, {
|
|
454
|
+
urls: "turn:openrelay.metered.ca:443",
|
|
455
|
+
username: "openrelayproject",
|
|
456
|
+
credential: "openrelayproject",
|
|
457
|
+
}, {
|
|
458
|
+
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
|
459
|
+
username: "openrelayproject",
|
|
460
|
+
credential: "openrelayproject",
|
|
451
461
|
}];
|
|
452
462
|
|
|
453
|
-
this.#connection = new wrtc.RTCPeerConnection();
|
|
463
|
+
this.#connection = new wrtc.RTCPeerConnection({iceServers});
|
|
454
464
|
|
|
455
465
|
this.#connection.onicecandidate = ({ candidate }) => {
|
|
456
466
|
if (candidate) {
|
|
@@ -717,43 +727,6 @@ class Client {
|
|
|
717
727
|
|
|
718
728
|
}
|
|
719
729
|
|
|
720
|
-
class LeofcoinStorageClient {
|
|
721
|
-
constructor(name, root) {
|
|
722
|
-
this.name = name;
|
|
723
|
-
this.root = root;
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
async get(key) {
|
|
727
|
-
try {
|
|
728
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
729
|
-
name: this.name,
|
|
730
|
-
root: this.root,
|
|
731
|
-
key,
|
|
732
|
-
});
|
|
733
|
-
return result
|
|
734
|
-
} catch (e) {
|
|
735
|
-
console.log(e);
|
|
736
|
-
return undefined
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
async put(key, value) {
|
|
741
|
-
try {
|
|
742
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
743
|
-
name: this.name,
|
|
744
|
-
root: this.root,
|
|
745
|
-
key,
|
|
746
|
-
value,
|
|
747
|
-
method: 'put',
|
|
748
|
-
});
|
|
749
|
-
return result
|
|
750
|
-
} catch (e) {
|
|
751
|
-
console.log(e);
|
|
752
|
-
return undefined
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
|
|
757
730
|
var proto$5 = `
|
|
758
731
|
// PeernetDataMessage
|
|
759
732
|
message PeernetDataMessage {
|
|
@@ -1872,15 +1845,7 @@ class Peernet {
|
|
|
1872
1845
|
const {daemon, environment} = await target();
|
|
1873
1846
|
this.hasDaemon = daemon;
|
|
1874
1847
|
|
|
1875
|
-
|
|
1876
|
-
const httpClient = await Promise.resolve().then(function () { return require('./client-5633ba04.js'); });
|
|
1877
|
-
globalThis.peernet.client = await httpClient.default({
|
|
1878
|
-
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1879
|
-
});
|
|
1880
|
-
} else {
|
|
1881
|
-
const http = await Promise.resolve().then(function () { return require('./http-78686629.js'); });
|
|
1882
|
-
if (environment !== 'browser') http.default(options);
|
|
1883
|
-
}
|
|
1848
|
+
|
|
1884
1849
|
|
|
1885
1850
|
for (const store of this.defaultStores) {
|
|
1886
1851
|
await this.addStore(store, options.storePrefix, options.root);
|
|
@@ -1889,6 +1854,8 @@ class Peernet {
|
|
|
1889
1854
|
try {
|
|
1890
1855
|
const pub = await accountStore.get('public');
|
|
1891
1856
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1857
|
+
const accounts = await walletStore.get('accounts');
|
|
1858
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts));
|
|
1892
1859
|
} catch (e) {
|
|
1893
1860
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1894
1861
|
const {identity, accounts, config} = await generateAccount(this.network);
|
package/dist/module/peernet.js
CHANGED
|
@@ -405,10 +405,20 @@ class Peer {
|
|
|
405
405
|
async #init() {
|
|
406
406
|
try {
|
|
407
407
|
const iceServers = [{
|
|
408
|
-
|
|
408
|
+
urls: 'stun:stun.l.google.com:19302' // Google's public STUN server
|
|
409
|
+
}, {
|
|
410
|
+
urls: "stun:openrelay.metered.ca:80",
|
|
411
|
+
}, {
|
|
412
|
+
urls: "turn:openrelay.metered.ca:443",
|
|
413
|
+
username: "openrelayproject",
|
|
414
|
+
credential: "openrelayproject",
|
|
415
|
+
}, {
|
|
416
|
+
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
|
417
|
+
username: "openrelayproject",
|
|
418
|
+
credential: "openrelayproject",
|
|
409
419
|
}];
|
|
410
420
|
|
|
411
|
-
this.#connection = new wrtc.RTCPeerConnection();
|
|
421
|
+
this.#connection = new wrtc.RTCPeerConnection({iceServers});
|
|
412
422
|
|
|
413
423
|
this.#connection.onicecandidate = ({ candidate }) => {
|
|
414
424
|
if (candidate) {
|
|
@@ -675,43 +685,6 @@ class Client {
|
|
|
675
685
|
|
|
676
686
|
}
|
|
677
687
|
|
|
678
|
-
class LeofcoinStorageClient {
|
|
679
|
-
constructor(name, root) {
|
|
680
|
-
this.name = name;
|
|
681
|
-
this.root = root;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
async get(key) {
|
|
685
|
-
try {
|
|
686
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
687
|
-
name: this.name,
|
|
688
|
-
root: this.root,
|
|
689
|
-
key,
|
|
690
|
-
});
|
|
691
|
-
return result
|
|
692
|
-
} catch (e) {
|
|
693
|
-
console.log(e);
|
|
694
|
-
return undefined
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
async put(key, value) {
|
|
699
|
-
try {
|
|
700
|
-
const result = await globalThis.peernet.client.request('storage', {
|
|
701
|
-
name: this.name,
|
|
702
|
-
root: this.root,
|
|
703
|
-
key,
|
|
704
|
-
value,
|
|
705
|
-
method: 'put',
|
|
706
|
-
});
|
|
707
|
-
return result
|
|
708
|
-
} catch (e) {
|
|
709
|
-
console.log(e);
|
|
710
|
-
return undefined
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
|
|
715
688
|
var proto$a = `
|
|
716
689
|
// PeernetMessage
|
|
717
690
|
message PeernetMessage {
|
|
@@ -1945,6 +1918,8 @@ class Peernet {
|
|
|
1945
1918
|
try {
|
|
1946
1919
|
const pub = await accountStore.get('public');
|
|
1947
1920
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
1921
|
+
const accounts = await walletStore.get('accounts');
|
|
1922
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts));
|
|
1948
1923
|
} catch (e) {
|
|
1949
1924
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
1950
1925
|
const {identity, accounts, config} = await generateAccount(this.network);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.28",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@leofcoin/codec-format-interface": "^1.0.1",
|
|
27
27
|
"@leofcoin/generate-account": "^1.0.2",
|
|
28
28
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
29
|
-
"@leofcoin/peernet-swarm": "^0.3.
|
|
29
|
+
"@leofcoin/peernet-swarm": "^0.3.1",
|
|
30
30
|
"@leofcoin/storage": "^2.3.0",
|
|
31
31
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
32
32
|
"@vandeurenglenn/base58": "^1.1.0",
|
package/rollup.config.js
CHANGED
|
@@ -19,15 +19,7 @@ export default [{
|
|
|
19
19
|
plugins: [
|
|
20
20
|
json(),
|
|
21
21
|
modify({
|
|
22
|
-
HTTP_IMPORT:
|
|
23
|
-
const httpClient = await import('./http/client/client.js')
|
|
24
|
-
globalThis.peernet.client = await httpClient.default({
|
|
25
|
-
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
26
|
-
})
|
|
27
|
-
} else {
|
|
28
|
-
const http = await import('./http/http.js')
|
|
29
|
-
if (environment !== 'browser') http.default(options)
|
|
30
|
-
}`,
|
|
22
|
+
HTTP_IMPORT: ``,
|
|
31
23
|
SUBTLE_IMPORT: `const { subtle } = require('crypto').webcrypto`
|
|
32
24
|
})
|
|
33
25
|
]
|
package/src/peernet.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug'
|
|
2
2
|
import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
|
|
3
3
|
import LeofcoinStorage from '@leofcoin/storage'
|
|
4
|
-
import LeofcoinStorageClient from './http/client/storage.js'
|
|
5
4
|
import PeernetMessage from './messages/peernet-message.js'
|
|
6
5
|
import DHTMessage from './messages/dht.js'
|
|
7
6
|
import DHTMessageResponse from './messages/dht-response.js'
|
|
@@ -191,6 +190,8 @@ export default class Peernet {
|
|
|
191
190
|
try {
|
|
192
191
|
const pub = await accountStore.get('public')
|
|
193
192
|
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
|
|
193
|
+
const accounts = await walletStore.get('accounts')
|
|
194
|
+
this.accounts = JSON.parse(new TextDecoder().decode(accounts))
|
|
194
195
|
} catch (e) {
|
|
195
196
|
if (e.code === 'ERR_NOT_FOUND') {
|
|
196
197
|
const wallet = {}
|
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/* socket-request-client version 1.6.3 */
|
|
4
|
-
|
|
5
|
-
class LittlePubSub {
|
|
6
|
-
constructor(verbose = true) {
|
|
7
|
-
this.subscribers = {};
|
|
8
|
-
this.verbose = verbose;
|
|
9
|
-
}
|
|
10
|
-
subscribe(event, handler, context) {
|
|
11
|
-
if (typeof context === 'undefined') {
|
|
12
|
-
context = handler;
|
|
13
|
-
}
|
|
14
|
-
this.subscribers[event] = this.subscribers[event] || { handlers: [], value: null};
|
|
15
|
-
this.subscribers[event].handlers.push(handler.bind(context));
|
|
16
|
-
}
|
|
17
|
-
unsubscribe(event, handler, context) {
|
|
18
|
-
if (typeof context === 'undefined') {
|
|
19
|
-
context = handler;
|
|
20
|
-
}
|
|
21
|
-
if (this.subscribers[event]) {
|
|
22
|
-
const index = this.subscribers[event].handlers.indexOf(handler.bind(context));
|
|
23
|
-
this.subscribers[event].handlers.splice(index);
|
|
24
|
-
if (this.subscribers[event].handlers.length === 0) delete this.subscribers[event];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
publish(event, change) {
|
|
28
|
-
if (this.subscribers[event]) {
|
|
29
|
-
if (this.verbose || this.subscribers[event].value !== change) {
|
|
30
|
-
this.subscribers[event].value = change;
|
|
31
|
-
this.subscribers[event].handlers.forEach(handler => {
|
|
32
|
-
handler(change, this.subscribers[event].value);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
var clientApi = _pubsub => {
|
|
40
|
-
const subscribe = (topic, cb) => {
|
|
41
|
-
_pubsub.subscribe(topic, cb);
|
|
42
|
-
};
|
|
43
|
-
const unsubscribe = (topic, cb) => {
|
|
44
|
-
_pubsub.unsubscribe(topic, cb);
|
|
45
|
-
};
|
|
46
|
-
const publish = (topic, value) => {
|
|
47
|
-
_pubsub.publish(topic, value);
|
|
48
|
-
};
|
|
49
|
-
const _connectionState = (state) => {
|
|
50
|
-
switch (state) {
|
|
51
|
-
case 0:
|
|
52
|
-
return 'connecting'
|
|
53
|
-
case 1:
|
|
54
|
-
return 'open'
|
|
55
|
-
case 2:
|
|
56
|
-
return 'closing'
|
|
57
|
-
case 3:
|
|
58
|
-
return 'closed'
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
const request = (client, request) => {
|
|
62
|
-
return new Promise((resolve, reject) => {
|
|
63
|
-
const state = _connectionState(client.readyState);
|
|
64
|
-
if (state !== 'open') return reject(`coudn't send request to ${client.id}, no open connection found.`)
|
|
65
|
-
request.id = Math.random().toString(36).slice(-12);
|
|
66
|
-
const handler = result => {
|
|
67
|
-
if (result && result.error) return reject(result.error)
|
|
68
|
-
resolve({result, id: request.id, handler});
|
|
69
|
-
unsubscribe(request.id, handler);
|
|
70
|
-
};
|
|
71
|
-
subscribe(request.id, handler);
|
|
72
|
-
send(client, request);
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
const send = async (client, request) => {
|
|
76
|
-
return client.send(JSON.stringify(request))
|
|
77
|
-
};
|
|
78
|
-
const pubsub = client => {
|
|
79
|
-
return {
|
|
80
|
-
publish: (topic = 'pubsub', value) => {
|
|
81
|
-
return send(client, {url: 'pubsub', params: { topic, value }})
|
|
82
|
-
},
|
|
83
|
-
subscribe: (topic = 'pubsub', cb) => {
|
|
84
|
-
subscribe(topic, cb);
|
|
85
|
-
return send(client, {url: 'pubsub', params: { topic, subscribe: true }})
|
|
86
|
-
},
|
|
87
|
-
unsubscribe: (topic = 'pubsub', cb) => {
|
|
88
|
-
unsubscribe(topic, cb);
|
|
89
|
-
return send(client, {url: 'pubsub', params: { topic, unsubscribe: true }})
|
|
90
|
-
},
|
|
91
|
-
subscribers: _pubsub.subscribers
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
const server = (client) => {
|
|
95
|
-
return {
|
|
96
|
-
uptime: async () => {
|
|
97
|
-
try {
|
|
98
|
-
const { result, id, handler } = await request(client, {url: 'uptime'});
|
|
99
|
-
unsubscribe(id, handler);
|
|
100
|
-
return result
|
|
101
|
-
} catch (e) {
|
|
102
|
-
throw e
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
ping: async () => {
|
|
106
|
-
try {
|
|
107
|
-
const now = new Date().getTime();
|
|
108
|
-
const { result, id, handler } = await request(client, {url: 'ping'});
|
|
109
|
-
unsubscribe(id, handler);
|
|
110
|
-
return (Number(result) - now)
|
|
111
|
-
} catch (e) {
|
|
112
|
-
throw e
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
const peernet = (client) => {
|
|
118
|
-
return {
|
|
119
|
-
join: async (params) => {
|
|
120
|
-
try {
|
|
121
|
-
params.join = true;
|
|
122
|
-
const requested = { url: 'peernet', params };
|
|
123
|
-
const { result, id, handler } = await request(client, requested);
|
|
124
|
-
unsubscribe(id, handler);
|
|
125
|
-
return result
|
|
126
|
-
} catch (e) {
|
|
127
|
-
throw e
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
leave: async (params) => {
|
|
131
|
-
try {
|
|
132
|
-
params.join = false;
|
|
133
|
-
const requested = { url: 'peernet', params };
|
|
134
|
-
const { result, id, handler } = await request(client, requested);
|
|
135
|
-
unsubscribe(id, handler);
|
|
136
|
-
return result
|
|
137
|
-
} catch (e) {
|
|
138
|
-
throw e
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
return { send, request, pubsub, server, subscribe, unsubscribe, publish, peernet }
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
if (!globalThis.PubSub) globalThis.PubSub = LittlePubSub;
|
|
147
|
-
if (!globalThis.pubsub) globalThis.pubsub = new LittlePubSub({verbose: false});
|
|
148
|
-
const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry: false }) => {
|
|
149
|
-
const { retry } = options;
|
|
150
|
-
const api = clientApi(pubsub);
|
|
151
|
-
let tries = 0;
|
|
152
|
-
const onerror = error => {
|
|
153
|
-
if (pubsub.subscribers['error']) {
|
|
154
|
-
pubsub.publish('error', error);
|
|
155
|
-
} else {
|
|
156
|
-
console.error(error);
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
const onmessage = message => {
|
|
160
|
-
const {value, url, status, id} = JSON.parse(message.data.toString());
|
|
161
|
-
const publisher = id ? id : url;
|
|
162
|
-
if (status === 200) {
|
|
163
|
-
pubsub.publish(publisher, value);
|
|
164
|
-
} else {
|
|
165
|
-
pubsub.publish(publisher, {error: value});
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
const clientConnection = client => {
|
|
169
|
-
const startTime = new Date().getTime();
|
|
170
|
-
return {
|
|
171
|
-
client,
|
|
172
|
-
request: async req => {
|
|
173
|
-
const { result, id, handler } = await api.request(client, req);
|
|
174
|
-
pubsub.unsubscribe(id, handler);
|
|
175
|
-
return result
|
|
176
|
-
},
|
|
177
|
-
send: req => api.send(client, req),
|
|
178
|
-
subscribe: api.subscribe,
|
|
179
|
-
unsubscribe: api.unsubscribe,
|
|
180
|
-
subscribers: api.subscribers,
|
|
181
|
-
publish: api.publish,
|
|
182
|
-
pubsub: api.pubsub(client),
|
|
183
|
-
uptime: () => {
|
|
184
|
-
const now = new Date().getTime();
|
|
185
|
-
return (now - startTime)
|
|
186
|
-
},
|
|
187
|
-
peernet: api.peernet(client),
|
|
188
|
-
server: api.server(client),
|
|
189
|
-
close: exit => {
|
|
190
|
-
client.close();
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
return new Promise((resolve, reject) => {
|
|
195
|
-
const init = () => {
|
|
196
|
-
let ws;
|
|
197
|
-
if (typeof process === 'object' && !globalThis.WebSocket) {
|
|
198
|
-
ws = require('websocket').w3cwebsocket;
|
|
199
|
-
} else {
|
|
200
|
-
ws = WebSocket;
|
|
201
|
-
}
|
|
202
|
-
const client = new ws(url, protocols);
|
|
203
|
-
client.onmessage = onmessage;
|
|
204
|
-
client.onerror = onerror;
|
|
205
|
-
client.onopen = () => {
|
|
206
|
-
tries = 0;
|
|
207
|
-
resolve(clientConnection(client));
|
|
208
|
-
};
|
|
209
|
-
client.onclose = message => {
|
|
210
|
-
tries++;
|
|
211
|
-
if (!retry) return reject(options)
|
|
212
|
-
if (tries > 5) {
|
|
213
|
-
console.log(`${protocols} Client Closed`);
|
|
214
|
-
console.error(`could not connect to - ${url}/`);
|
|
215
|
-
return resolve(clientConnection(client))
|
|
216
|
-
}
|
|
217
|
-
if (message.code === 1006) {
|
|
218
|
-
console.log('Retrying in 10 seconds');
|
|
219
|
-
setTimeout(() => {
|
|
220
|
-
return init();
|
|
221
|
-
}, retry);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
};
|
|
225
|
-
return init();
|
|
226
|
-
});
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
class HttpClientApi$1 {
|
|
230
|
-
constructor(config = {}) {
|
|
231
|
-
if (!config.apiPath) config.apiPath = 'api';
|
|
232
|
-
|
|
233
|
-
const address = `ws://${config.host}:${config.port}`;
|
|
234
|
-
|
|
235
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
236
|
-
return (async () => {
|
|
237
|
-
this.client = await socketRequestClient(address, config.protocol, {pubsub: config.pubsub, retry: 3000});
|
|
238
|
-
return this
|
|
239
|
-
})()
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async get(url, obj) {
|
|
243
|
-
const headers = {};
|
|
244
|
-
let body = null;
|
|
245
|
-
let method = 'GET';
|
|
246
|
-
if (obj) {
|
|
247
|
-
method = 'POST';
|
|
248
|
-
headers['Content-Type'] = 'application/json';
|
|
249
|
-
body = JSON.stringify(obj);
|
|
250
|
-
}
|
|
251
|
-
let response = await this.client.request(url, {headers, body, method});
|
|
252
|
-
const type = response.headers.get('content-type').split(';')[0];
|
|
253
|
-
if (type==='application/json') response = await response.json();
|
|
254
|
-
return response
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async put(url, obj) {
|
|
258
|
-
const headers = {};
|
|
259
|
-
let body = {};
|
|
260
|
-
if (obj) {
|
|
261
|
-
headers['Content-Type'] = 'application/json';
|
|
262
|
-
body = JSON.stringify(obj);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body});
|
|
266
|
-
const type = response.headers.get('content-type').split(';')[0];
|
|
267
|
-
if (type==='application/json') response = await response.json();
|
|
268
|
-
return response
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
class HttpClientApi extends HttpClientApi$1 {
|
|
273
|
-
constructor(config = {}) {
|
|
274
|
-
config.apiPath = 'api';
|
|
275
|
-
return (async () => {
|
|
276
|
-
await super(config);
|
|
277
|
-
|
|
278
|
-
this.properties = {
|
|
279
|
-
wallet: 'get',
|
|
280
|
-
version: 'get',
|
|
281
|
-
addresses: 'get',
|
|
282
|
-
config: 'get',
|
|
283
|
-
account: 'get',
|
|
284
|
-
accounts: 'get',
|
|
285
|
-
transaction: 'any',
|
|
286
|
-
transactions: 'get',
|
|
287
|
-
block: 'get',
|
|
288
|
-
blocks: 'get',
|
|
289
|
-
};
|
|
290
|
-
this.keys = Object.keys(this.properties);
|
|
291
|
-
return this
|
|
292
|
-
})()
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
async request(url, data) {
|
|
296
|
-
return await this.client.request({url, params: data})
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async ready() {
|
|
300
|
-
return await this.request('ready')
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async version() {
|
|
304
|
-
return await this.request('version')
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
async account(index) {
|
|
308
|
-
return await this.request('account', {index})
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
var client = (config = {}) => {
|
|
313
|
-
if (typeof config !== 'object') config = {};
|
|
314
|
-
if (!config.protocol) config.protocol = 'peernet-v0.1.0';
|
|
315
|
-
if (!config.port) config.port = 1000;
|
|
316
|
-
if (!config.host) config.host = '127.0.0.1';
|
|
317
|
-
|
|
318
|
-
return new HttpClientApi(config)
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
exports["default"] = client;
|
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var websocket = require('websocket');
|
|
4
|
-
var PubSub = require('@vandeurenglenn/little-pubsub');
|
|
5
|
-
var http$1 = require('http');
|
|
6
|
-
var Koa = require('koa');
|
|
7
|
-
|
|
8
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
-
|
|
10
|
-
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
11
|
-
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
12
|
-
|
|
13
|
-
var version = "0.11.24";
|
|
14
|
-
|
|
15
|
-
var api$1 = {
|
|
16
|
-
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
17
|
-
ready: ({send}) => {
|
|
18
|
-
if (globalThis.states.ready) send(true);
|
|
19
|
-
else pubsub.subscribe('ready', () => send(true));
|
|
20
|
-
},
|
|
21
|
-
storage: async (params, {send, error}) => {
|
|
22
|
-
console.log(params);
|
|
23
|
-
const {name, root, key, value, method} = params;
|
|
24
|
-
try {
|
|
25
|
-
if (name && root) {
|
|
26
|
-
globalThis[name] = globalThis[name] || await new LeofcoinStorage(name, root);
|
|
27
|
-
} else {
|
|
28
|
-
return error('Expected name & root')
|
|
29
|
-
}
|
|
30
|
-
if (method === 'put') {
|
|
31
|
-
await globalThis[name].put(key, value);
|
|
32
|
-
return send('ok')
|
|
33
|
-
}
|
|
34
|
-
if (method === 'remove') {
|
|
35
|
-
await globalThis[name].remove(key, value);
|
|
36
|
-
return send('ok')
|
|
37
|
-
}
|
|
38
|
-
value = await globalThis[name].get(key);
|
|
39
|
-
return send(value)
|
|
40
|
-
} catch (e) {
|
|
41
|
-
return error(e)
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
getConfig: async (params, {send, error}) => {
|
|
45
|
-
try {
|
|
46
|
-
const config = await api.getConfig(params);
|
|
47
|
-
send(config);
|
|
48
|
-
} catch (e) {
|
|
49
|
-
error(e);
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
setMinerConfig: async (params, {send, error}) => {
|
|
53
|
-
try {
|
|
54
|
-
await api.setMinerConfig(params);
|
|
55
|
-
send('ok');
|
|
56
|
-
} catch (e) {
|
|
57
|
-
error(e);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
getMinerConfig: async ({send, error}) => {
|
|
61
|
-
try {
|
|
62
|
-
const config = await api.getMinerConfig();
|
|
63
|
-
send(config);
|
|
64
|
-
} catch (e) {
|
|
65
|
-
error(e);
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
wallet: async ({send}) => {
|
|
69
|
-
const wallet = await walletStore.get();
|
|
70
|
-
send(wallet);
|
|
71
|
-
},
|
|
72
|
-
addresses: async ({send, error}) => {
|
|
73
|
-
try {
|
|
74
|
-
const adresses = await api.addresses();
|
|
75
|
-
send(adresses);
|
|
76
|
-
} catch (e) {
|
|
77
|
-
error(e);
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
accountNames: async (params, {send, error}) => {
|
|
81
|
-
try {
|
|
82
|
-
const adresses = await api.accountNames(params.index);
|
|
83
|
-
send(adresses);
|
|
84
|
-
} catch (e) {
|
|
85
|
-
error(e);
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
accounts: async ({send}) => {
|
|
89
|
-
const accounts = await accountStore.get();
|
|
90
|
-
send(accounts);
|
|
91
|
-
},
|
|
92
|
-
account: async (params, {send}) => {
|
|
93
|
-
const account = await accountStore.get(params);
|
|
94
|
-
send(account);
|
|
95
|
-
},
|
|
96
|
-
balance: async (params, {send, error}) => {
|
|
97
|
-
console.log('balance');
|
|
98
|
-
try {
|
|
99
|
-
console.log(await api.getBalanceForAddress(params.address));
|
|
100
|
-
const value = await api.getBalanceForAddress(params.address);
|
|
101
|
-
send(value);
|
|
102
|
-
} catch (e) {
|
|
103
|
-
console.log(e);
|
|
104
|
-
error(e);
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
balanceAfter: async (params, {send, error}) => {
|
|
108
|
-
try {
|
|
109
|
-
const value = await api.getBalanceForAddressAfter(params.address, params.index);
|
|
110
|
-
send(value);
|
|
111
|
-
} catch (e) {
|
|
112
|
-
error(e);
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
mine: async (params, {send, error}) => {
|
|
116
|
-
api.mine(params);
|
|
117
|
-
send('ok');
|
|
118
|
-
},
|
|
119
|
-
lastBlock: async ({send, error}) => {
|
|
120
|
-
try {
|
|
121
|
-
const value = await api.lastBlock();
|
|
122
|
-
send(value);
|
|
123
|
-
} catch (e) {
|
|
124
|
-
error(e);
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const fullLog = text => {
|
|
130
|
-
return console.log(`${new Date()}: ${text}`)
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const originIsAllowed = (requestOrigin, origin) => {
|
|
134
|
-
// put logic here to detect whether the specified origin is allowed.
|
|
135
|
-
if (origin && requestOrigin !== origin) return false;
|
|
136
|
-
return true;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* @module socketResponse
|
|
141
|
-
*
|
|
142
|
-
* @param {object} connection socket connection
|
|
143
|
-
* @param {string} route the route to handle
|
|
144
|
-
*/
|
|
145
|
-
var socketConnection = (request, protocol, origin) => {
|
|
146
|
-
if (origin && !originIsAllowed(request.origin, origin)) {
|
|
147
|
-
// Make sure we only accept requests from an allowed origin
|
|
148
|
-
request.reject();
|
|
149
|
-
fullLog(`Connection from origin ${request.origin} rejected.`);
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
// console.log(request);
|
|
153
|
-
const connection = request.accept(protocol, request.origin);
|
|
154
|
-
fullLog(`Connection accepted @${protocol}`);
|
|
155
|
-
return connection;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* @module socketResponse
|
|
160
|
-
*
|
|
161
|
-
* @param {object} connection socket connection
|
|
162
|
-
* @param {string} url the request url
|
|
163
|
-
*/
|
|
164
|
-
var socketResponse = (connection, url, id, customEvent) => {
|
|
165
|
-
const send = (data = 'ok', status = 200) => connection.send(
|
|
166
|
-
JSON.stringify({url, status, value: data, id, customEvent})
|
|
167
|
-
);
|
|
168
|
-
const error = data => connection.send(JSON.stringify({url, value: data}));
|
|
169
|
-
return {
|
|
170
|
-
connection,
|
|
171
|
-
send,
|
|
172
|
-
error
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
if (!globalThis.PubSub) globalThis.PubSub = PubSub__default["default"];
|
|
177
|
-
if (!globalThis.pubsub) globalThis.pubsub = new PubSub__default["default"]();
|
|
178
|
-
|
|
179
|
-
const socketRequestServer = (options, routes = {}) => {
|
|
180
|
-
// if (!routes && !routes.port && options) routes = options;
|
|
181
|
-
// else if (!options && !routes) return console.error('no routes defined');
|
|
182
|
-
|
|
183
|
-
let {httpServer, httpsServer, port, protocol, credentials, origin, pubsub } = options;
|
|
184
|
-
if (!pubsub) pubsub = new PubSub__default["default"]({verbose: false});
|
|
185
|
-
if (!port) port = 6000;
|
|
186
|
-
const connections = [];
|
|
187
|
-
let connection;
|
|
188
|
-
const startTime = new Date().getTime();
|
|
189
|
-
// default routes
|
|
190
|
-
if (!routes.ping) routes.ping = (response) => response.send(new Date().getTime());
|
|
191
|
-
if (!routes.uptime) routes.uptime = (response) => {
|
|
192
|
-
const now = new Date().getTime();
|
|
193
|
-
response.send(now - startTime);
|
|
194
|
-
};
|
|
195
|
-
if (!routes.pubsub) {
|
|
196
|
-
routes.pubsub = (params, response) => {
|
|
197
|
-
if (!response) {
|
|
198
|
-
response = params;
|
|
199
|
-
params = {};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (!params.topic) params.topic = 'pubsub';
|
|
203
|
-
|
|
204
|
-
const topic = params.topic;
|
|
205
|
-
delete params.topic;
|
|
206
|
-
|
|
207
|
-
if (params.subscribe) {
|
|
208
|
-
pubsub.subscribe(topic, message => {
|
|
209
|
-
response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
|
|
210
|
-
});
|
|
211
|
-
response.send('ok', 200);
|
|
212
|
-
} else if (params.unsubscribe) {
|
|
213
|
-
pubsub.unsubscribe(topic, message => {
|
|
214
|
-
response.connection.send(JSON.stringify({url: topic, status: 200, value: message}));
|
|
215
|
-
});
|
|
216
|
-
for (const connection of connections) {
|
|
217
|
-
if (connection !== response.connection) connection.send(JSON.stringify({url: topic, status: 200, value: params}));
|
|
218
|
-
}
|
|
219
|
-
response.send('ok', 200);
|
|
220
|
-
}
|
|
221
|
-
else if (params.value !== undefined)
|
|
222
|
-
// should only be send raw to stars
|
|
223
|
-
// for (const connection of connections) {
|
|
224
|
-
// if (connection !== response.connection) connection.send(JSON.stringify({
|
|
225
|
-
// url: topic, status: 200, value: params
|
|
226
|
-
// }));
|
|
227
|
-
// }
|
|
228
|
-
pubsub.publish(topic, params.value);
|
|
229
|
-
response.send('ok', 200);
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
globalThis.peerMap = new Map();
|
|
234
|
-
if (!routes.peernet) {
|
|
235
|
-
routes.peernet = (params, response) => {
|
|
236
|
-
if (params.join) {
|
|
237
|
-
peerMap.set(params.peerId, params.address);
|
|
238
|
-
response.send([...peerMap.values()]);
|
|
239
|
-
for (const connection of connections) {
|
|
240
|
-
if (connection !== response.connection)
|
|
241
|
-
socketResponse(connection, 'peernet', 'peernet').send({discovered: params.address});
|
|
242
|
-
}
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
if (!params.join) {
|
|
246
|
-
peerMap.delete(params.peerId);
|
|
247
|
-
return response.send()
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
// if (!protocol) protocol = 'echo-protocol';
|
|
252
|
-
if (!httpServer && !httpsServer) {
|
|
253
|
-
const { createServer } = credentials ? require('https') : require('http');
|
|
254
|
-
if (credentials) httpServer = createServer(credentials);
|
|
255
|
-
else httpServer = createServer();
|
|
256
|
-
|
|
257
|
-
httpServer.listen(port, () => {
|
|
258
|
-
console.log(`listening on ${port}`);
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const socketServer = new websocket.server({
|
|
263
|
-
httpServer,
|
|
264
|
-
autoAcceptConnections: false
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
socketServer.on('request', request => {
|
|
268
|
-
|
|
269
|
-
connection = socketConnection(request, protocol, origin);
|
|
270
|
-
connections.push(connection);
|
|
271
|
-
|
|
272
|
-
const routeHandler = message => {
|
|
273
|
-
let data;
|
|
274
|
-
if (message.type) {
|
|
275
|
-
switch (message.type) {
|
|
276
|
-
case 'binary':
|
|
277
|
-
data = message.binaryData.toString();
|
|
278
|
-
break;
|
|
279
|
-
case 'utf8':
|
|
280
|
-
data = message.utf8Data;
|
|
281
|
-
break;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
const { route, params, url, id, customEvent } = JSON.parse(data);
|
|
285
|
-
// ignore api when customEvent is defined
|
|
286
|
-
if (customEvent) return;
|
|
287
|
-
if (routes[url]) {
|
|
288
|
-
if (!params) return routes[url](socketResponse(connection, url, id), connections);
|
|
289
|
-
return routes[url](params, socketResponse(connection, url, id), connections);
|
|
290
|
-
}
|
|
291
|
-
else return socketResponse(connection, url, id).error(`no handler found for '${message.url}'`);
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
connection.on('message', routeHandler);
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
return {
|
|
298
|
-
close: () => socketServer.shutDown(),
|
|
299
|
-
connections
|
|
300
|
-
};
|
|
301
|
-
};
|
|
302
|
-
var server = socketRequestServer;
|
|
303
|
-
|
|
304
|
-
var http = (config = {}) => {
|
|
305
|
-
if (typeof config !== 'object') config = {};
|
|
306
|
-
if (!config.protocol) config.protocol = 'peernet-v0.1.0';
|
|
307
|
-
if (!config.port) config.port = 2000;
|
|
308
|
-
if (!config.host) config.host = '127.0.0.1';
|
|
309
|
-
|
|
310
|
-
const app = new Koa__default["default"]();
|
|
311
|
-
|
|
312
|
-
app.use(async (ctx) => {
|
|
313
|
-
const url = ctx.url.split('/api/')[1];
|
|
314
|
-
if (url === 'version') ctx.body = {client: '@peernet/api/http', version};
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
const httpServer = http$1.createServer(app.callback());
|
|
318
|
-
|
|
319
|
-
config.httpServer = httpServer;
|
|
320
|
-
|
|
321
|
-
httpServer.listen(config.port, () => {
|
|
322
|
-
console.log(`listening on ${config.port}`);
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
return server(config, api$1)
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
exports["default"] = http;
|
package/src/http/api.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
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
|
-
}
|
package/src/http/client/api.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import Client from './../../../node_modules/socket-request-client/dist/es/index'
|
|
2
|
-
|
|
3
|
-
export default class HttpClientApi {
|
|
4
|
-
constructor(config = {}) {
|
|
5
|
-
if (!config.apiPath) config.apiPath = 'api'
|
|
6
|
-
|
|
7
|
-
const address = `ws://${config.host}:${config.port}`
|
|
8
|
-
|
|
9
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
10
|
-
return (async () => {
|
|
11
|
-
this.client = await Client(address, config.protocol, {pubsub: config.pubsub, retry: 3000})
|
|
12
|
-
return this
|
|
13
|
-
})()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async get(url, obj) {
|
|
17
|
-
const headers = {}
|
|
18
|
-
let body = null
|
|
19
|
-
let method = 'GET'
|
|
20
|
-
if (obj) {
|
|
21
|
-
method = 'POST'
|
|
22
|
-
headers['Content-Type'] = 'application/json'
|
|
23
|
-
body = JSON.stringify(obj)
|
|
24
|
-
}
|
|
25
|
-
let response = await this.client.request(url, {headers, body, method})
|
|
26
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
27
|
-
if (type==='application/json') response = await response.json()
|
|
28
|
-
return response
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async put(url, obj) {
|
|
32
|
-
const headers = {}
|
|
33
|
-
let body = {}
|
|
34
|
-
if (obj) {
|
|
35
|
-
headers['Content-Type'] = 'application/json'
|
|
36
|
-
body = JSON.stringify(obj)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body})
|
|
40
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
41
|
-
if (type==='application/json') response = await response.json()
|
|
42
|
-
return response
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
}
|
package/src/http/http.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
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
|
-
}
|