@leofcoin/peernet 1.1.50 → 1.1.51
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/exports/browser/browser-store.js +58 -141
- package/exports/browser/client-3221a5d7.js +17580 -0
- package/exports/browser/dht/dht.d.ts +29 -7
- package/exports/browser/identity.d.ts +15 -0
- package/exports/browser/{index-6311966e.js → index-36ed3c80.js} +2 -2
- package/exports/browser/{messages-54aa3cad.js → messages-b7f50660.js} +2 -2
- package/exports/browser/{peernet-a4156bfd.js → peernet-1b30b405.js} +432 -531
- package/exports/browser/peernet.d.ts +25 -32
- package/exports/browser/peernet.js +2 -2
- package/exports/browser/types.d.ts +16 -0
- package/exports/browser/value-4e80eeeb.js +73 -0
- package/exports/peernet.js +35 -34
- package/exports/store.js +117 -193
- package/exports/types/dht/dht.d.ts +29 -7
- package/exports/types/identity.d.ts +15 -0
- package/exports/types/peernet.d.ts +25 -32
- package/exports/types/types.d.ts +16 -0
- package/package.json +2 -2
- package/src/dht/dht.ts +36 -16
- package/src/peernet.ts +47 -60
- package/src/types.ts +28 -0
- package/exports/browser/client-6072af1a.js +0 -41092
- package/exports/browser/value-157ab062.js +0 -64
package/exports/store.js
CHANGED
|
@@ -4,67 +4,76 @@ import { readdirSync } from 'fs';
|
|
|
4
4
|
import { mkdirp } from 'mkdirp';
|
|
5
5
|
import { ClassicLevel } from 'classic-level';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// base58,
|
|
12
|
-
// base32
|
|
13
|
-
// }
|
|
14
|
-
|
|
15
|
-
const encode$1 = (string, encoding = 'utf-8') => {
|
|
16
|
-
if (typeof string === 'string') {
|
|
17
|
-
let encoded;
|
|
18
|
-
|
|
19
|
-
// if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
|
|
20
|
-
encoded = new TextEncoder().encode(string);
|
|
21
|
-
return encoded
|
|
22
|
-
}
|
|
23
|
-
throw Error(`expected typeof String instead got ${string}`)
|
|
7
|
+
const encode = (string) => {
|
|
8
|
+
if (typeof string === 'string')
|
|
9
|
+
return new TextEncoder().encode(string);
|
|
10
|
+
throw Error(`expected typeof String instead got ${string}`);
|
|
24
11
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
|
|
30
|
-
decoded = new TextDecoder().decode(uint8Array);
|
|
31
|
-
|
|
32
|
-
return decoded
|
|
33
|
-
}
|
|
34
|
-
throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
|
|
12
|
+
const decode = (uint8Array) => {
|
|
13
|
+
if (uint8Array instanceof Uint8Array)
|
|
14
|
+
return new TextDecoder().decode(uint8Array);
|
|
15
|
+
throw Error(`expected typeof uint8Array instead got ${uint8Array}`);
|
|
35
16
|
};
|
|
36
17
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
18
|
+
const pathSepS = '/';
|
|
19
|
+
class KeyPath {
|
|
20
|
+
uint8Array;
|
|
21
|
+
constructor(input) {
|
|
22
|
+
if (typeof input === 'string') {
|
|
23
|
+
this.uint8Array = encode(input);
|
|
24
|
+
}
|
|
25
|
+
else if (input instanceof Uint8Array) {
|
|
26
|
+
this.uint8Array = input;
|
|
27
|
+
}
|
|
28
|
+
else if (input instanceof KeyPath) {
|
|
29
|
+
this.uint8Array = input.uint8Array;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath');
|
|
33
|
+
}
|
|
51
34
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
35
|
+
isKeyPath() {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
toString() {
|
|
39
|
+
return decode(this.uint8Array);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the `list` representation of this path.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
|
|
47
|
+
* // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
list() {
|
|
51
|
+
return this.toString().split(pathSepS).slice(1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
67
54
|
|
|
55
|
+
class KeyValue {
|
|
56
|
+
uint8Array;
|
|
57
|
+
constructor(input) {
|
|
58
|
+
if (typeof input === 'string') {
|
|
59
|
+
this.uint8Array = encode(input);
|
|
60
|
+
}
|
|
61
|
+
else if (input instanceof Uint8Array) {
|
|
62
|
+
this.uint8Array = input;
|
|
63
|
+
}
|
|
64
|
+
else if (input instanceof KeyValue) {
|
|
65
|
+
this.uint8Array = input.uint8Array;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
isKeyValue() {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
toString() {
|
|
75
|
+
return decode(this.uint8Array);
|
|
76
|
+
}
|
|
68
77
|
}
|
|
69
78
|
|
|
70
79
|
const init = (root, home = true) => {
|
|
@@ -79,149 +88,64 @@ const init = (root, home = true) => {
|
|
|
79
88
|
return _root
|
|
80
89
|
};
|
|
81
90
|
|
|
82
|
-
// import base32 from '@vandeurenglenn/base32'
|
|
83
|
-
// import base58 from '@vandeurenglenn/base58'
|
|
84
|
-
|
|
85
|
-
// export const encodings = {
|
|
86
|
-
// base58,
|
|
87
|
-
// base32
|
|
88
|
-
// }
|
|
89
|
-
|
|
90
|
-
const encode = (string, encoding = 'utf-8') => {
|
|
91
|
-
if (typeof string === 'string') {
|
|
92
|
-
let encoded;
|
|
93
|
-
|
|
94
|
-
// if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
|
|
95
|
-
encoded = new TextEncoder().encode(string);
|
|
96
|
-
return encoded
|
|
97
|
-
}
|
|
98
|
-
throw Error(`expected typeof String instead got ${string}`)
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const decode = (uint8Array, encoding) => {
|
|
102
|
-
if (uint8Array instanceof Uint8Array) {
|
|
103
|
-
let decoded;
|
|
104
|
-
// if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
|
|
105
|
-
decoded = new TextDecoder().decode(uint8Array);
|
|
106
|
-
|
|
107
|
-
return decoded
|
|
108
|
-
}
|
|
109
|
-
throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
const pathSepS = '/';
|
|
113
|
-
class KeyPath {
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* @param {string | Uint8Array} input
|
|
117
|
-
*/
|
|
118
|
-
constructor(input) {
|
|
119
|
-
if (typeof input === 'string') {
|
|
120
|
-
this.uint8Array = encode(input);
|
|
121
|
-
} else if (input instanceof Uint8Array) {
|
|
122
|
-
this.uint8Array = input;
|
|
123
|
-
} else if (input instanceof KeyPath) {
|
|
124
|
-
this.uint8Array = input.uint8Array;
|
|
125
|
-
} else {
|
|
126
|
-
throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath')
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
isKeyPath() {
|
|
131
|
-
return true
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Convert to the string representation
|
|
136
|
-
*
|
|
137
|
-
* @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
|
|
138
|
-
* @returns {string}
|
|
139
|
-
*/
|
|
140
|
-
toString(encoding = 'hex') {
|
|
141
|
-
return decode(this.uint8Array)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Returns the `list` representation of this path.
|
|
146
|
-
*
|
|
147
|
-
* @returns string[]
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* ```js
|
|
151
|
-
* new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
|
|
152
|
-
* // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
list() {
|
|
156
|
-
return this.toString().split(pathSepS).slice(1)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
|
|
161
91
|
class Store {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (!key.isKeyPath()) key = new KeyPath(key);
|
|
172
|
-
return key.toString()
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
toKeyValue(value) {
|
|
176
|
-
if (!value.isKeyValue()) value = new KeyValue(value);
|
|
177
|
-
return value.uint8Array
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
toUint8Array(buffer) {
|
|
181
|
-
return Buffer.isBuffer(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT) : buffer
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
async get(key) {
|
|
185
|
-
return this.toUint8Array(await this.db.get(this.toKeyPath(key)))
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async put(key, value) {
|
|
189
|
-
return this.db.put(this.toKeyPath(key), this.toKeyValue(value))
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async delete(key) {
|
|
193
|
-
return this.db.del(this.toKeyPath(key))
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
async clear() {
|
|
197
|
-
return this.db.clear()
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async values(limit = -1) {
|
|
201
|
-
const values = [];
|
|
202
|
-
for await (const value of this.db.values({limit})) {
|
|
203
|
-
values.push(this.toUint8Array(value));
|
|
92
|
+
db;
|
|
93
|
+
name;
|
|
94
|
+
root;
|
|
95
|
+
version;
|
|
96
|
+
constructor(name = 'storage', root, version = 'v1.0.0') {
|
|
97
|
+
this.name = name;
|
|
98
|
+
this.root = init(root);
|
|
99
|
+
this.version = version;
|
|
100
|
+
this.db = new ClassicLevel(join(this.root, this.name), { valueEncoding: 'view' });
|
|
204
101
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
102
|
+
toKeyPath(key) {
|
|
103
|
+
if (!key.isKeyPath())
|
|
104
|
+
key = new KeyPath(key);
|
|
105
|
+
return key.toString();
|
|
106
|
+
}
|
|
107
|
+
toKeyValue(value) {
|
|
108
|
+
if (!value.isKeyValue())
|
|
109
|
+
value = new KeyValue(value);
|
|
110
|
+
return value.uint8Array;
|
|
111
|
+
}
|
|
112
|
+
toUint8Array(buffer) {
|
|
113
|
+
return Buffer.isBuffer(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT) : buffer;
|
|
114
|
+
}
|
|
115
|
+
async get(key) {
|
|
116
|
+
return this.toUint8Array(await this.db.get(this.toKeyPath(key)));
|
|
117
|
+
}
|
|
118
|
+
async put(key, value) {
|
|
119
|
+
return this.db.put(this.toKeyPath(key), this.toKeyValue(value));
|
|
120
|
+
}
|
|
121
|
+
async delete(key) {
|
|
122
|
+
return this.db.del(this.toKeyPath(key));
|
|
123
|
+
}
|
|
124
|
+
async clear() {
|
|
125
|
+
return this.db.clear();
|
|
126
|
+
}
|
|
127
|
+
async values(limit = -1) {
|
|
128
|
+
const values = [];
|
|
129
|
+
for await (const value of this.db.values({ limit })) {
|
|
130
|
+
values.push(this.toUint8Array(value));
|
|
131
|
+
}
|
|
132
|
+
return values;
|
|
133
|
+
}
|
|
134
|
+
async keys(limit = -1) {
|
|
135
|
+
const keys = [];
|
|
136
|
+
for await (const key of this.db.keys({ limit })) {
|
|
137
|
+
keys.push(key);
|
|
138
|
+
}
|
|
139
|
+
return keys;
|
|
212
140
|
}
|
|
213
|
-
return keys
|
|
214
|
-
}
|
|
215
|
-
|
|
216
141
|
/**
|
|
217
|
-
*
|
|
142
|
+
*
|
|
218
143
|
* @param {object} options { limit, gt, lt, reverse }
|
|
219
|
-
* @returns
|
|
144
|
+
* @returns
|
|
220
145
|
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
146
|
+
iterate(options) {
|
|
147
|
+
return this.db.iterator(options);
|
|
148
|
+
}
|
|
225
149
|
}
|
|
226
150
|
|
|
227
151
|
export { Store as default };
|
|
@@ -1,19 +1,41 @@
|
|
|
1
|
+
export declare type DHTProvider = {
|
|
2
|
+
address: string;
|
|
3
|
+
id: string;
|
|
4
|
+
};
|
|
5
|
+
export declare type DHTProviderDistanceResult = {
|
|
6
|
+
provider: DHTProvider;
|
|
7
|
+
/**
|
|
8
|
+
* distance on earth between peers in km
|
|
9
|
+
*/
|
|
10
|
+
distance: number;
|
|
11
|
+
};
|
|
12
|
+
export declare type DHTProviderMapValue = {
|
|
13
|
+
[index: string]: DHTProvider;
|
|
14
|
+
};
|
|
15
|
+
declare type Coordinates = {
|
|
16
|
+
longitude: number;
|
|
17
|
+
latitude: number;
|
|
18
|
+
};
|
|
1
19
|
export default class DhtEarth {
|
|
2
|
-
providerMap: Map<string,
|
|
20
|
+
providerMap: Map<string, DHTProviderMapValue>;
|
|
3
21
|
/**
|
|
4
22
|
*
|
|
5
23
|
*/
|
|
6
24
|
constructor();
|
|
7
|
-
getCoordinates(address: string): Promise<
|
|
25
|
+
getCoordinates(address: string): Promise<Coordinates>;
|
|
8
26
|
/**
|
|
9
27
|
* @param {Object} peer
|
|
10
28
|
* @param {Object} provider
|
|
11
29
|
* @return {Object} {provider, distance}
|
|
12
30
|
*/
|
|
13
|
-
getDistance(peer:
|
|
14
|
-
|
|
31
|
+
getDistance(peer: {
|
|
32
|
+
latitude: number;
|
|
33
|
+
longitude: number;
|
|
34
|
+
}, provider: DHTProvider): Promise<DHTProviderDistanceResult>;
|
|
35
|
+
closestPeer(providers: Array<any>): Promise<DHTProvider>;
|
|
15
36
|
hasProvider(hash: string): boolean;
|
|
16
|
-
providersFor(hash: string):
|
|
17
|
-
addProvider(
|
|
18
|
-
removeProvider(address: string, hash: string):
|
|
37
|
+
providersFor(hash: string): DHTProviderMapValue;
|
|
38
|
+
addProvider(provider: DHTProvider, hash: string): void;
|
|
39
|
+
removeProvider(address: string, hash: string): void;
|
|
19
40
|
}
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import MultiWallet from '@leofcoin/multi-wallet';
|
|
2
|
+
export default class Identity {
|
|
3
|
+
#private;
|
|
4
|
+
network: MultiWallet.network;
|
|
5
|
+
id: string;
|
|
6
|
+
constructor(network: string);
|
|
7
|
+
get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
|
|
8
|
+
getAccounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
|
|
9
|
+
load(password?: string): Promise<void>;
|
|
10
|
+
sign(hash: Uint8Array): any;
|
|
11
|
+
export(password: string): Promise<string>;
|
|
12
|
+
import(password: any, encrypted: base58String): Promise<void>;
|
|
13
|
+
exportQR(password: string): Promise<any>;
|
|
14
|
+
importQR(image: File | Blob, password: string): Promise<void>;
|
|
15
|
+
}
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import '@vandeurenglenn/debug';
|
|
3
|
-
import
|
|
3
|
+
import PeerDiscovery from './discovery/peer-discovery.js';
|
|
4
4
|
import DHT from './dht/dht.js';
|
|
5
5
|
import MessageHandler from './handlers/message.js';
|
|
6
6
|
import LeofcoinStorageClass from '@leofcoin/storage';
|
|
7
7
|
import Identity from './identity.js';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var pubsub: PubSub;
|
|
11
|
-
var peernet: Peernet;
|
|
12
|
-
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
13
|
-
var LeofcoinStorageClient: any;
|
|
14
|
-
}
|
|
8
|
+
import swarm from '@netpeer/p2pt-swarm';
|
|
9
|
+
import P2PTPeer from '@netpeer/p2pt-swarm/peer';
|
|
15
10
|
/**
|
|
16
11
|
* @access public
|
|
17
12
|
* @example
|
|
@@ -19,20 +14,18 @@ declare global {
|
|
|
19
14
|
*/
|
|
20
15
|
export default class Peernet {
|
|
21
16
|
#private;
|
|
17
|
+
storePrefix: string;
|
|
18
|
+
root: string;
|
|
22
19
|
identity: Identity;
|
|
23
20
|
stores: string[];
|
|
21
|
+
peerId: string;
|
|
24
22
|
/**
|
|
25
23
|
* @type {Object}
|
|
26
24
|
* @property {Object} peer Instance of Peer
|
|
27
25
|
*/
|
|
28
26
|
dht: DHT;
|
|
29
27
|
/** @leofcoin/peernet-swarm/client */
|
|
30
|
-
client:
|
|
31
|
-
connections: [];
|
|
32
|
-
id: string;
|
|
33
|
-
removePeer: (peer: string) => {};
|
|
34
|
-
close: () => {};
|
|
35
|
-
};
|
|
28
|
+
client: swarm;
|
|
36
29
|
network: string;
|
|
37
30
|
stars: string[];
|
|
38
31
|
networkVersion: string;
|
|
@@ -44,6 +37,7 @@ export default class Peernet {
|
|
|
44
37
|
autoStart: boolean;
|
|
45
38
|
requestProtos: {};
|
|
46
39
|
_messageHandler: MessageHandler;
|
|
40
|
+
_peerHandler: PeerDiscovery;
|
|
47
41
|
protos: {};
|
|
48
42
|
/**
|
|
49
43
|
* @access public
|
|
@@ -63,7 +57,7 @@ export default class Peernet {
|
|
|
63
57
|
get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]>;
|
|
64
58
|
get defaultStores(): string[];
|
|
65
59
|
addProto(name: any, proto: any): void;
|
|
66
|
-
addCodec(codec: any):
|
|
60
|
+
addCodec(codec: any): any;
|
|
67
61
|
addStore(name: any, prefix: any, root: any, isPrivate?: boolean): Promise<void>;
|
|
68
62
|
/**
|
|
69
63
|
* @see MessageHandler
|
|
@@ -74,13 +68,13 @@ export default class Peernet {
|
|
|
74
68
|
*
|
|
75
69
|
* @return {Array} peerId
|
|
76
70
|
*/
|
|
77
|
-
get peers(): [string,
|
|
78
|
-
get connections():
|
|
79
|
-
get peerEntries():
|
|
71
|
+
get peers(): [string, P2PTPeer][];
|
|
72
|
+
get connections(): P2PTPeer[];
|
|
73
|
+
get peerEntries(): P2PTPeer[];
|
|
80
74
|
/**
|
|
81
75
|
* @return {String} id - peerId
|
|
82
76
|
*/
|
|
83
|
-
getConnection(id: any):
|
|
77
|
+
getConnection(id: any): any;
|
|
84
78
|
/**
|
|
85
79
|
* @private
|
|
86
80
|
*
|
|
@@ -89,7 +83,7 @@ export default class Peernet {
|
|
|
89
83
|
*
|
|
90
84
|
* @return {Promise} instance of Peernet
|
|
91
85
|
*/
|
|
92
|
-
_init(options: any, password: any): Peernet
|
|
86
|
+
_init(options: any, password: any): Promise<Peernet>;
|
|
93
87
|
start(): Promise<void>;
|
|
94
88
|
addRequestHandler(name: any, method: any): void;
|
|
95
89
|
sendMessage(peer: any, id: any, data: any): Promise<void>;
|
|
@@ -114,16 +108,16 @@ export default class Peernet {
|
|
|
114
108
|
*
|
|
115
109
|
* @param {String} hash
|
|
116
110
|
*/
|
|
117
|
-
providersFor(hash: string, store?: undefined): Promise<
|
|
111
|
+
providersFor(hash: string, store?: undefined): Promise<import("./dht/dht.js").DHTProviderMapValue>;
|
|
118
112
|
get block(): {
|
|
119
|
-
get: (hash:
|
|
120
|
-
put: (hash:
|
|
121
|
-
has: (hash:
|
|
113
|
+
get: (hash: string) => Promise<any>;
|
|
114
|
+
put: (hash: string, data: Uint8Array) => Promise<any>;
|
|
115
|
+
has: (hash: string) => Promise<any>;
|
|
122
116
|
};
|
|
123
117
|
get transaction(): {
|
|
124
|
-
get: (hash:
|
|
125
|
-
put: (hash:
|
|
126
|
-
has: (hash:
|
|
118
|
+
get: (hash: string) => Promise<any>;
|
|
119
|
+
put: (hash: string, data: Uint8Array) => Promise<any>;
|
|
120
|
+
has: (hash: string) => Promise<any>;
|
|
127
121
|
};
|
|
128
122
|
requestData(hash: any, store: any): any;
|
|
129
123
|
get message(): {
|
|
@@ -207,9 +201,9 @@ export default class Peernet {
|
|
|
207
201
|
*
|
|
208
202
|
* @param {String} hash
|
|
209
203
|
* @param {Buffer} data
|
|
210
|
-
* @param {String}
|
|
204
|
+
* @param {String} storeName - storeName to access
|
|
211
205
|
*/
|
|
212
|
-
put(hash:
|
|
206
|
+
put(hash: string, data: Uint8Array, storeName?: string | LeofcoinStorageClass): Promise<any>;
|
|
213
207
|
/**
|
|
214
208
|
* @param {String} hash
|
|
215
209
|
* @return {Boolean}
|
|
@@ -221,13 +215,12 @@ export default class Peernet {
|
|
|
221
215
|
* @param {String|Object|Array|Boolean|Buffer} data
|
|
222
216
|
*/
|
|
223
217
|
publish(topic: any, data: any): Promise<void>;
|
|
224
|
-
createHash(data: any, name: any): any;
|
|
225
218
|
/**
|
|
226
219
|
*
|
|
227
220
|
* @param {String} topic
|
|
228
221
|
* @param {Method} cb
|
|
229
222
|
*/
|
|
230
|
-
subscribe(topic:
|
|
223
|
+
subscribe(topic: string, callback: Function): Promise<void>;
|
|
231
224
|
removePeer(peer: any): Promise<any>;
|
|
232
225
|
get Buffer(): BufferConstructor;
|
|
233
226
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import '@vandeurenglenn/debug';
|
|
2
|
+
import PubSub from '@vandeurenglenn/little-pubsub';
|
|
3
|
+
import Peernet from './peernet.js';
|
|
4
|
+
import { LeofcoinStorage as LeofcoinStorageClass } from '@leofcoin/storage';
|
|
5
|
+
declare global {
|
|
6
|
+
var debug: (message: any) => string;
|
|
7
|
+
var globalSub: PubSub;
|
|
8
|
+
var pubsub: PubSub;
|
|
9
|
+
var peernet: Peernet;
|
|
10
|
+
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
11
|
+
var LeofcoinStorageClient: any;
|
|
12
|
+
var messageStore: typeof LeofcoinStorage;
|
|
13
|
+
var dataStore: typeof LeofcoinStorage;
|
|
14
|
+
var transactionStore: typeof LeofcoinStorage;
|
|
15
|
+
var blockStore: typeof LeofcoinStorage;
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.51",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/peernet.js",
|
|
6
6
|
"exports": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@leofcoin/multi-wallet": "^3.0.1",
|
|
36
36
|
"@leofcoin/peernet-swarm": "^1.1.0",
|
|
37
37
|
"@leofcoin/storage": "^3.0.0",
|
|
38
|
-
"@netpeer/p2pt-swarm": "^1.
|
|
38
|
+
"@netpeer/p2pt-swarm": "^1.3.2",
|
|
39
39
|
"@types/node": "^18.11.18",
|
|
40
40
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
41
41
|
"@vandeurenglenn/base58": "^1.1.0",
|
package/src/dht/dht.ts
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
export declare type DHTProvider = {
|
|
2
|
+
address: string,
|
|
3
|
+
id: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export declare type DHTProviderDistanceResult = {
|
|
7
|
+
provider: DHTProvider,
|
|
8
|
+
/**
|
|
9
|
+
* distance on earth between peers in km
|
|
10
|
+
*/
|
|
11
|
+
distance: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare type DHTProviderMapValue = {[index: string]: DHTProvider}
|
|
15
|
+
|
|
16
|
+
declare type Coordinates = {
|
|
17
|
+
longitude: number,
|
|
18
|
+
latitude: number
|
|
19
|
+
}
|
|
20
|
+
|
|
1
21
|
/**
|
|
2
22
|
* Keep history of fetched address and ptr
|
|
3
23
|
* @property {Object} address
|
|
@@ -47,7 +67,7 @@ const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
|
47
67
|
}
|
|
48
68
|
|
|
49
69
|
export default class DhtEarth {
|
|
50
|
-
providerMap = new Map<string,
|
|
70
|
+
providerMap = new Map<string, DHTProviderMapValue>
|
|
51
71
|
|
|
52
72
|
/**
|
|
53
73
|
*
|
|
@@ -55,12 +75,12 @@ export default class DhtEarth {
|
|
|
55
75
|
constructor() {
|
|
56
76
|
this.providerMap = new Map();
|
|
57
77
|
}
|
|
58
|
-
|
|
78
|
+
|
|
79
|
+
async getCoordinates(address: string): Promise<Coordinates> {
|
|
59
80
|
if (!fetchedCoordinates[address]) {
|
|
60
81
|
const request = `https://whereis.leofcoin.org/?ip=${address}`
|
|
61
82
|
let response = await fetch(request)
|
|
62
|
-
|
|
63
|
-
const {lat, lon} = response;
|
|
83
|
+
const {lat, lon} = await response.json() as { lat: number, lon: number}
|
|
64
84
|
fetchedCoordinates[address] = {latitude: lat, longitude: lon}
|
|
65
85
|
}
|
|
66
86
|
return fetchedCoordinates[address]
|
|
@@ -71,20 +91,22 @@ export default class DhtEarth {
|
|
|
71
91
|
* @param {Object} provider
|
|
72
92
|
* @return {Object} {provider, distance}
|
|
73
93
|
*/
|
|
74
|
-
async getDistance(peer:
|
|
94
|
+
async getDistance(peer: { latitude: number, longitude: number } , provider: DHTProvider): Promise<DHTProviderDistanceResult> {
|
|
75
95
|
const {latitude, longitude} = await this.getCoordinates(provider.address)
|
|
76
96
|
return {provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude)}
|
|
77
97
|
}
|
|
78
98
|
|
|
79
|
-
async closestPeer(providers: Array<any>):
|
|
99
|
+
async closestPeer(providers: Array<any>): Promise<DHTProvider> {
|
|
80
100
|
let all = []
|
|
81
101
|
const address = await getAddress();
|
|
82
102
|
const peerLoc = await this.getCoordinates(address)
|
|
103
|
+
|
|
83
104
|
for (const provider of providers) {
|
|
84
105
|
if (provider.address === '127.0.0.1' || provider.address === '::1') all.push({provider, distance: 0})
|
|
85
106
|
else all.push(this.getDistance(peerLoc, provider))
|
|
86
107
|
}
|
|
87
108
|
|
|
109
|
+
// todo queue
|
|
88
110
|
all = await Promise.all(all);
|
|
89
111
|
all = all.sort((previous, current) => previous.distance - current.distance)
|
|
90
112
|
return all[0].provider;
|
|
@@ -94,28 +116,26 @@ export default class DhtEarth {
|
|
|
94
116
|
return this.providerMap.has(hash)
|
|
95
117
|
}
|
|
96
118
|
|
|
97
|
-
providersFor(hash: string):
|
|
98
|
-
let providers
|
|
119
|
+
providersFor(hash: string): DHTProviderMapValue {
|
|
120
|
+
let providers: DHTProviderMapValue
|
|
99
121
|
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash)
|
|
100
122
|
return providers
|
|
101
123
|
}
|
|
102
124
|
|
|
103
|
-
addProvider(
|
|
104
|
-
let providers:
|
|
125
|
+
addProvider(provider: DHTProvider, hash: string) {
|
|
126
|
+
let providers: DHTProviderMapValue
|
|
105
127
|
if (this.providerMap.has(hash)) {
|
|
106
128
|
providers = this.providerMap.get(hash)
|
|
107
129
|
}
|
|
108
|
-
providers.
|
|
130
|
+
providers[provider.address] = provider
|
|
109
131
|
this.providerMap.set(hash, providers)
|
|
110
132
|
}
|
|
111
133
|
|
|
112
|
-
removeProvider(address: string, hash: string)
|
|
113
|
-
let deleted = undefined
|
|
134
|
+
removeProvider(address: string, hash: string){
|
|
114
135
|
if (this.providerMap.has(hash)) {
|
|
115
136
|
const providers = this.providerMap.get(hash)
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
delete providers[address]
|
|
138
|
+
this.providerMap.set(hash, providers)
|
|
118
139
|
}
|
|
119
|
-
return deleted;
|
|
120
140
|
}
|
|
121
141
|
}
|