@leofcoin/peernet 0.18.0 → 0.18.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/exports/browser/{client-13d9b3de.js → client-ed00c639.js} +1 -1
- package/exports/browser/dht/dht.d.ts +30 -0
- package/exports/browser/discovery/peer-discovery.d.ts +7 -0
- package/exports/browser/errors/errors.d.ts +3 -0
- package/exports/browser/handlers/data.d.ts +2 -0
- package/exports/browser/handlers/message.d.ts +30 -0
- package/exports/browser/{index-19803d3b.js → index-7018cab6.js} +1 -1
- package/exports/browser/{index-73b8d9b9.js → index-ddd5774f.js} +2 -2
- package/exports/browser/messages/chat.d.ts +6 -0
- package/exports/browser/messages/data-response.d.ts +6 -0
- package/exports/browser/messages/data.d.ts +10 -0
- package/exports/browser/messages/dht-response.d.ts +6 -0
- package/exports/browser/messages/dht.d.ts +14 -0
- package/exports/browser/messages/file-link.d.ts +10 -0
- package/exports/browser/messages/file.d.ts +10 -0
- package/exports/browser/messages/peer-response.d.ts +6 -0
- package/exports/browser/messages/peer.d.ts +6 -0
- package/exports/browser/messages/peernet.d.ts +6 -0
- package/exports/browser/messages/ps.d.ts +6 -0
- package/exports/browser/messages/request.d.ts +6 -0
- package/exports/browser/messages/response.d.ts +6 -0
- package/exports/browser/messages-452642e3.js +204 -0
- package/exports/browser/messages.d.ts +12 -0
- package/exports/browser/peer-info.d.ts +4 -0
- package/exports/browser/{peernet-24ed8563.js → peernet-2796cdb2.js} +1084 -1064
- package/exports/browser/peernet.js +1 -1
- package/exports/browser/proto/chat-message.proto.d.ts +7 -0
- package/exports/browser/proto/data-response.proto.d.ts +5 -0
- package/exports/browser/proto/data.proto.d.ts +5 -0
- package/exports/browser/proto/dht-response.proto.d.ts +5 -0
- package/exports/browser/proto/dht.proto.d.ts +5 -0
- package/exports/browser/proto/file-link.proto.d.ts +6 -0
- package/exports/browser/proto/file.proto.d.ts +6 -0
- package/exports/browser/proto/peer-response.proto.d.ts +4 -0
- package/exports/browser/proto/peer.proto.d.ts +4 -0
- package/exports/browser/proto/peernet.proto.d.ts +8 -0
- package/exports/browser/proto/ps.proto.d.ts +5 -0
- package/exports/browser/proto/request.proto.d.ts +4 -0
- package/exports/browser/proto/response.proto.d.ts +4 -0
- package/exports/browser/utils/utils.d.ts +7 -0
- package/exports/dht/dht.d.ts +30 -0
- package/exports/discovery/peer-discovery.d.ts +7 -0
- package/exports/errors/errors.d.ts +3 -0
- package/exports/handlers/data.d.ts +2 -0
- package/exports/handlers/message.d.ts +30 -0
- package/exports/messages/chat.d.ts +6 -0
- package/exports/messages/data-response.d.ts +6 -0
- package/exports/messages/data.d.ts +10 -0
- package/exports/messages/dht-response.d.ts +6 -0
- package/exports/messages/dht.d.ts +14 -0
- package/exports/messages/file-link.d.ts +10 -0
- package/exports/messages/file.d.ts +10 -0
- package/exports/messages/peer-response.d.ts +6 -0
- package/exports/messages/peer.d.ts +6 -0
- package/exports/messages/peernet.d.ts +6 -0
- package/exports/messages/ps.d.ts +6 -0
- package/exports/messages/request.d.ts +6 -0
- package/exports/messages/response.d.ts +6 -0
- package/exports/messages-ebdc8c69.js +203 -0
- package/exports/messages.d.ts +12 -0
- package/exports/peer-info.d.ts +4 -0
- package/exports/peernet.js +863 -930
- package/exports/proto/chat-message.proto.d.ts +7 -0
- package/exports/proto/data-response.proto.d.ts +5 -0
- package/exports/proto/data.proto.d.ts +5 -0
- package/exports/proto/dht-response.proto.d.ts +5 -0
- package/exports/proto/dht.proto.d.ts +5 -0
- package/exports/proto/file-link.proto.d.ts +6 -0
- package/exports/proto/file.proto.d.ts +6 -0
- package/exports/proto/peer-response.proto.d.ts +4 -0
- package/exports/proto/peer.proto.d.ts +4 -0
- package/exports/proto/peernet.proto.d.ts +8 -0
- package/exports/proto/ps.proto.d.ts +5 -0
- package/exports/proto/request.proto.d.ts +4 -0
- package/exports/proto/response.proto.d.ts +4 -0
- package/exports/utils/utils.d.ts +7 -0
- package/package.json +3 -2
- package/rollup.config.js +9 -3
- package/src/peernet.ts +26 -20
- package/tsconfig.json +3 -9
- package/exports/browser/messages-bb950ed7.js +0 -224
- package/exports/messages-d8852e16.js +0 -223
|
@@ -1258,6 +1258,187 @@ new Mutex();
|
|
|
1258
1258
|
|
|
1259
1259
|
new Mutex();
|
|
1260
1260
|
|
|
1261
|
+
const blockchainCodecs = [
|
|
1262
|
+
{
|
|
1263
|
+
name: 'leofcoin-block',
|
|
1264
|
+
codec: '0x6c62',
|
|
1265
|
+
hashAlg: 'dbl-keccak-512',
|
|
1266
|
+
},
|
|
1267
|
+
{
|
|
1268
|
+
name: 'leofcoin-tx',
|
|
1269
|
+
codec: '0x6c74',
|
|
1270
|
+
hashAlg: 'dbl-keccak-512',
|
|
1271
|
+
},
|
|
1272
|
+
{
|
|
1273
|
+
name: 'leofcoin-itx',
|
|
1274
|
+
codec: '0x6c69',
|
|
1275
|
+
hashAlg: 'keccak-512',
|
|
1276
|
+
},
|
|
1277
|
+
{
|
|
1278
|
+
name: 'leofcoin-pr',
|
|
1279
|
+
codec: '0x6c70',
|
|
1280
|
+
hashAlg: 'keccak-256',
|
|
1281
|
+
},
|
|
1282
|
+
{
|
|
1283
|
+
name: 'contract-message',
|
|
1284
|
+
codec: '0x63636d',
|
|
1285
|
+
hashAlg: 'keccak-256'
|
|
1286
|
+
},
|
|
1287
|
+
{
|
|
1288
|
+
name: 'transaction-message',
|
|
1289
|
+
codec: '0x746d',
|
|
1290
|
+
hashAlg: 'keccak-256'
|
|
1291
|
+
},
|
|
1292
|
+
{
|
|
1293
|
+
name: 'block-message',
|
|
1294
|
+
codec: '0x626d',
|
|
1295
|
+
hashAlg: 'keccak-256'
|
|
1296
|
+
},
|
|
1297
|
+
{
|
|
1298
|
+
name: 'bw-message',
|
|
1299
|
+
codec: '0x62776d',
|
|
1300
|
+
hashAlg: 'keccak-256'
|
|
1301
|
+
},
|
|
1302
|
+
{
|
|
1303
|
+
name: 'bw-request-message',
|
|
1304
|
+
codec: '0x6277726d',
|
|
1305
|
+
hashAlg: 'keccak-256'
|
|
1306
|
+
},
|
|
1307
|
+
{
|
|
1308
|
+
name: 'validator-message',
|
|
1309
|
+
codec: '0x766d',
|
|
1310
|
+
hashAlg: 'keccak-256'
|
|
1311
|
+
}
|
|
1312
|
+
];
|
|
1313
|
+
|
|
1314
|
+
const internalCodecs = [
|
|
1315
|
+
{
|
|
1316
|
+
name: 'disco-hash',
|
|
1317
|
+
codec: '0x30',
|
|
1318
|
+
hashAlg: 'dbl-keccak-256',
|
|
1319
|
+
},
|
|
1320
|
+
{
|
|
1321
|
+
name: 'peernet-peer-response',
|
|
1322
|
+
codec: '0x707072',
|
|
1323
|
+
hashAlg: 'keccak-256',
|
|
1324
|
+
},
|
|
1325
|
+
{
|
|
1326
|
+
name: 'peernet-peer',
|
|
1327
|
+
codec: '0x7070',
|
|
1328
|
+
hashAlg: 'keccak-256',
|
|
1329
|
+
},
|
|
1330
|
+
{
|
|
1331
|
+
name: 'peernet-dht',
|
|
1332
|
+
codec: '0x706468',
|
|
1333
|
+
hashAlg: 'keccak-256',
|
|
1334
|
+
},
|
|
1335
|
+
{
|
|
1336
|
+
name: 'peernet-dht-response',
|
|
1337
|
+
codec: '0x706472',
|
|
1338
|
+
hashAlg: 'keccak-256',
|
|
1339
|
+
},
|
|
1340
|
+
{
|
|
1341
|
+
name: 'peernet-data',
|
|
1342
|
+
codec: '0x706461',
|
|
1343
|
+
hashAlg: 'keccak-256',
|
|
1344
|
+
},
|
|
1345
|
+
{
|
|
1346
|
+
name: 'peernet-data-response',
|
|
1347
|
+
codec: '0x70646172',
|
|
1348
|
+
hashAlg: 'keccak-256',
|
|
1349
|
+
},
|
|
1350
|
+
{
|
|
1351
|
+
name: 'peernet-message',
|
|
1352
|
+
codec: '0x706d65',
|
|
1353
|
+
hashAlg: 'keccak-256',
|
|
1354
|
+
},
|
|
1355
|
+
{
|
|
1356
|
+
name: 'peernet-ps',
|
|
1357
|
+
codec: '707073',
|
|
1358
|
+
hashAlg: 'keccak-256',
|
|
1359
|
+
},
|
|
1360
|
+
{
|
|
1361
|
+
name: 'peernet-response',
|
|
1362
|
+
codec: '0x7072',
|
|
1363
|
+
hashAlg: 'keccak-256',
|
|
1364
|
+
},
|
|
1365
|
+
{
|
|
1366
|
+
name: 'peernet-request',
|
|
1367
|
+
codec: '0x707271',
|
|
1368
|
+
hashAlg: 'keccak-256',
|
|
1369
|
+
},
|
|
1370
|
+
{
|
|
1371
|
+
name: 'peernet-file',
|
|
1372
|
+
codec: '0x7066',
|
|
1373
|
+
hashAlg: 'keccak-256',
|
|
1374
|
+
},
|
|
1375
|
+
{
|
|
1376
|
+
name: 'peernet-file-response',
|
|
1377
|
+
codec: '0x706672',
|
|
1378
|
+
hashAlg: 'keccak-256',
|
|
1379
|
+
}
|
|
1380
|
+
];
|
|
1381
|
+
|
|
1382
|
+
const codecs = [
|
|
1383
|
+
...internalCodecs,
|
|
1384
|
+
...blockchainCodecs,
|
|
1385
|
+
{
|
|
1386
|
+
name: 'chat-message',
|
|
1387
|
+
codec: '0x70636d',
|
|
1388
|
+
hashAlg: 'dbl-keccak-256',
|
|
1389
|
+
}
|
|
1390
|
+
];
|
|
1391
|
+
|
|
1392
|
+
globalThis.peernet = globalThis.peernet || {};
|
|
1393
|
+
globalThis.peernet.codecs = globalThis.peernet.codecs || {};
|
|
1394
|
+
const addCodec = (codecInput) => {
|
|
1395
|
+
let { hashAlg, codec, name } = codecInput;
|
|
1396
|
+
if (!globalThis.peernet.codecs[name])
|
|
1397
|
+
globalThis.peernet.codecs[name] = {
|
|
1398
|
+
hashAlg,
|
|
1399
|
+
codec: parseInt(codec, 16)
|
|
1400
|
+
};
|
|
1401
|
+
};
|
|
1402
|
+
const getCodec = (name) => globalThis.peernet.codecs[name];
|
|
1403
|
+
const getCodecName = (codec) => {
|
|
1404
|
+
return Object.keys(globalThis.peernet.codecs).reduce((p, c) => {
|
|
1405
|
+
const item = globalThis.peernet.codecs[c];
|
|
1406
|
+
if (item.codec === codec)
|
|
1407
|
+
return c;
|
|
1408
|
+
else
|
|
1409
|
+
return p;
|
|
1410
|
+
}, undefined);
|
|
1411
|
+
};
|
|
1412
|
+
const getCodecByName = (name) => globalThis.peernet.codecs[name];
|
|
1413
|
+
const getHashAlg = (name) => {
|
|
1414
|
+
if (typeof name === 'number')
|
|
1415
|
+
return getCodecByName(getCodecName(name)).hashAlg;
|
|
1416
|
+
return getCodecByName(name).hashAlg;
|
|
1417
|
+
};
|
|
1418
|
+
const isCodec = (codec) => {
|
|
1419
|
+
if (codec.codec !== undefined && codec.hashAlg)
|
|
1420
|
+
return true;
|
|
1421
|
+
return false;
|
|
1422
|
+
};
|
|
1423
|
+
const validateCodec = (codec) => {
|
|
1424
|
+
if (codec.codec === undefined ||
|
|
1425
|
+
codec.hashAlg === undefined ||
|
|
1426
|
+
codec.name === undefined)
|
|
1427
|
+
throw new Error(`invalid codecInput: ${codec}`);
|
|
1428
|
+
};
|
|
1429
|
+
for (const codec of codecs) {
|
|
1430
|
+
addCodec(codec);
|
|
1431
|
+
}
|
|
1432
|
+
var utils = {
|
|
1433
|
+
isCodec,
|
|
1434
|
+
addCodec,
|
|
1435
|
+
getCodec,
|
|
1436
|
+
getHashAlg,
|
|
1437
|
+
getCodecName,
|
|
1438
|
+
validateCodec,
|
|
1439
|
+
codecs: globalThis.peernet.codecs
|
|
1440
|
+
};
|
|
1441
|
+
|
|
1261
1442
|
/**
|
|
1262
1443
|
* @param {string}
|
|
1263
1444
|
*/
|
|
@@ -1268,6 +1449,22 @@ let BasicInterface$1 = class BasicInterface {
|
|
|
1268
1449
|
decoded;
|
|
1269
1450
|
keys;
|
|
1270
1451
|
name;
|
|
1452
|
+
#proto;
|
|
1453
|
+
set proto(value) {
|
|
1454
|
+
this.#proto = value;
|
|
1455
|
+
this.keys = Object.keys(value);
|
|
1456
|
+
}
|
|
1457
|
+
get proto() {
|
|
1458
|
+
return this.#proto;
|
|
1459
|
+
}
|
|
1460
|
+
decode(encoded) {
|
|
1461
|
+
encoded = encoded || this.encoded;
|
|
1462
|
+
return new Object();
|
|
1463
|
+
}
|
|
1464
|
+
encode(decoded) {
|
|
1465
|
+
decoded = decoded || this.decoded;
|
|
1466
|
+
return new Uint8Array();
|
|
1467
|
+
}
|
|
1271
1468
|
// get Codec(): Codec {}
|
|
1272
1469
|
protoEncode(data) {
|
|
1273
1470
|
// check schema
|
|
@@ -1287,12 +1484,10 @@ let BasicInterface$1 = class BasicInterface {
|
|
|
1287
1484
|
return base58$1.isBase58(string);
|
|
1288
1485
|
}
|
|
1289
1486
|
fromBs32(encoded) {
|
|
1290
|
-
this.
|
|
1291
|
-
return this.decode();
|
|
1487
|
+
return this.decode(index$7.decode(encoded));
|
|
1292
1488
|
}
|
|
1293
1489
|
fromBs58(encoded) {
|
|
1294
|
-
this.
|
|
1295
|
-
return this.decode();
|
|
1490
|
+
return this.decode(fromBase58(encoded));
|
|
1296
1491
|
}
|
|
1297
1492
|
async toArray() {
|
|
1298
1493
|
const array = [];
|
|
@@ -1304,20 +1499,16 @@ let BasicInterface$1 = class BasicInterface {
|
|
|
1304
1499
|
fromString(string) {
|
|
1305
1500
|
const array = string.split(',');
|
|
1306
1501
|
const arrayLike = array.map(string => Number(string));
|
|
1307
|
-
this.
|
|
1308
|
-
return this.decode();
|
|
1502
|
+
return this.decode(Uint8Array.from(arrayLike));
|
|
1309
1503
|
}
|
|
1310
1504
|
fromHex(string) {
|
|
1311
|
-
this.
|
|
1312
|
-
return this.decode();
|
|
1505
|
+
return this.decode(fromHex(string));
|
|
1313
1506
|
}
|
|
1314
1507
|
fromArray(array) {
|
|
1315
|
-
this.
|
|
1316
|
-
return this.decode();
|
|
1508
|
+
return this.decode(Uint8Array.from([...array]));
|
|
1317
1509
|
}
|
|
1318
1510
|
fromEncoded(encoded) {
|
|
1319
|
-
this.encoded
|
|
1320
|
-
return this.decode();
|
|
1511
|
+
return this.decode(encoded);
|
|
1321
1512
|
}
|
|
1322
1513
|
toString() {
|
|
1323
1514
|
if (!this.encoded)
|
|
@@ -1327,7 +1518,7 @@ let BasicInterface$1 = class BasicInterface {
|
|
|
1327
1518
|
toHex() {
|
|
1328
1519
|
if (!this.encoded)
|
|
1329
1520
|
this.encode();
|
|
1330
|
-
return toHex(this.encoded);
|
|
1521
|
+
return toHex(this.encoded.toString().split(',').map(number => Number(number)));
|
|
1331
1522
|
}
|
|
1332
1523
|
/**
|
|
1333
1524
|
* @return {String} encoded
|
|
@@ -1362,108 +1553,15 @@ let BasicInterface$1 = class BasicInterface {
|
|
|
1362
1553
|
});
|
|
1363
1554
|
}
|
|
1364
1555
|
this.decoded = decoded;
|
|
1365
|
-
return this.encode();
|
|
1556
|
+
return this.encode(decoded);
|
|
1366
1557
|
}
|
|
1367
1558
|
}
|
|
1368
1559
|
};
|
|
1369
1560
|
|
|
1370
|
-
var codecs = {
|
|
1371
|
-
// just a hash
|
|
1372
|
-
'disco-hash': {
|
|
1373
|
-
codec: parseInt('30', 16),
|
|
1374
|
-
hashAlg: 'dbl-keccak-256', // ,
|
|
1375
|
-
// testnet: 'olivia'
|
|
1376
|
-
},
|
|
1377
|
-
'peernet-peer-response': {
|
|
1378
|
-
codec: parseInt('707072', 16),
|
|
1379
|
-
hashAlg: 'keccak-256',
|
|
1380
|
-
},
|
|
1381
|
-
'peernet-peer': {
|
|
1382
|
-
codec: parseInt('7070', 16),
|
|
1383
|
-
hashAlg: 'keccak-256',
|
|
1384
|
-
},
|
|
1385
|
-
'peernet-dht': {
|
|
1386
|
-
codec: parseInt('706468', 16),
|
|
1387
|
-
hashAlg: 'keccak-256',
|
|
1388
|
-
},
|
|
1389
|
-
'peernet-dht-response': {
|
|
1390
|
-
codec: parseInt('706472', 16),
|
|
1391
|
-
hashAlg: 'keccak-256',
|
|
1392
|
-
},
|
|
1393
|
-
// data
|
|
1394
|
-
'peernet-data': {
|
|
1395
|
-
codec: parseInt('706461', 16),
|
|
1396
|
-
hashAlg: 'keccak-256',
|
|
1397
|
-
},
|
|
1398
|
-
'peernet-data-response': {
|
|
1399
|
-
codec: parseInt('70646172', 16),
|
|
1400
|
-
hashAlg: 'keccak-256',
|
|
1401
|
-
},
|
|
1402
|
-
// message
|
|
1403
|
-
'peernet-message': {
|
|
1404
|
-
codec: parseInt('706d65', 16),
|
|
1405
|
-
hashAlg: 'keccak-256',
|
|
1406
|
-
},
|
|
1407
|
-
// pubsub
|
|
1408
|
-
'peernet-ps': {
|
|
1409
|
-
codec: parseInt('707073', 16),
|
|
1410
|
-
hashAlg: 'keccak-256',
|
|
1411
|
-
},
|
|
1412
|
-
'peernet-response': {
|
|
1413
|
-
codec: parseInt('7072', 16),
|
|
1414
|
-
hashAlg: 'keccak-256',
|
|
1415
|
-
},
|
|
1416
|
-
'peernet-request': {
|
|
1417
|
-
codec: parseInt('707271', 16),
|
|
1418
|
-
hashAlg: 'keccak-256',
|
|
1419
|
-
},
|
|
1420
|
-
// normal block
|
|
1421
|
-
'leofcoin-block': {
|
|
1422
|
-
codec: parseInt('6c62', 16),
|
|
1423
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
1424
|
-
// testnet: 'olivia'
|
|
1425
|
-
},
|
|
1426
|
-
'leofcoin-tx': {
|
|
1427
|
-
codec: parseInt('6c74', 16),
|
|
1428
|
-
hashAlg: 'dbl-keccak-512', // ,
|
|
1429
|
-
// testnet: 'olivia'
|
|
1430
|
-
},
|
|
1431
|
-
// itx
|
|
1432
|
-
'leofcoin-itx': {
|
|
1433
|
-
codec: parseInt('6c69', 16),
|
|
1434
|
-
hashAlg: 'keccak-512', // ,
|
|
1435
|
-
// testnet: 'olivia'
|
|
1436
|
-
},
|
|
1437
|
-
// peer reputation
|
|
1438
|
-
'leofcoin-pr': {
|
|
1439
|
-
codec: parseInt('6c70', 16),
|
|
1440
|
-
hashAlg: 'keccak-256', // ,
|
|
1441
|
-
// testnet: 'olivia'
|
|
1442
|
-
},
|
|
1443
|
-
// chat message
|
|
1444
|
-
'chat-message': {
|
|
1445
|
-
codec: parseInt('70636d', 16),
|
|
1446
|
-
hashAlg: 'dbl-keccak-256',
|
|
1447
|
-
},
|
|
1448
|
-
'peernet-file': {
|
|
1449
|
-
codec: parseInt('7066', 16),
|
|
1450
|
-
hashAlg: 'keccak-256',
|
|
1451
|
-
},
|
|
1452
|
-
'peernet-file-response': {
|
|
1453
|
-
codec: parseInt('706672', 16),
|
|
1454
|
-
hashAlg: 'keccak-256',
|
|
1455
|
-
}
|
|
1456
|
-
};
|
|
1457
|
-
|
|
1458
|
-
var codecs$1 = /*#__PURE__*/Object.freeze({
|
|
1459
|
-
__proto__: null,
|
|
1460
|
-
default: codecs
|
|
1461
|
-
});
|
|
1462
|
-
|
|
1463
1561
|
let Codec$1 = class Codec extends BasicInterface$1 {
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1562
|
+
codecBuffer;
|
|
1563
|
+
codec;
|
|
1564
|
+
hashAlg;
|
|
1467
1565
|
constructor(buffer) {
|
|
1468
1566
|
super();
|
|
1469
1567
|
if (buffer) {
|
|
@@ -1480,17 +1578,18 @@ let Codec$1 = class Codec extends BasicInterface$1 {
|
|
|
1480
1578
|
}
|
|
1481
1579
|
}
|
|
1482
1580
|
else if (buffer instanceof ArrayBuffer) {
|
|
1483
|
-
const
|
|
1484
|
-
|
|
1485
|
-
|
|
1581
|
+
const codec = index$6.decode(buffer);
|
|
1582
|
+
const name = this.getCodecName(codec);
|
|
1583
|
+
if (name) {
|
|
1584
|
+
this.name = name;
|
|
1585
|
+
this.decode(buffer);
|
|
1586
|
+
}
|
|
1587
|
+
else {
|
|
1588
|
+
this.encode(buffer);
|
|
1486
1589
|
}
|
|
1487
|
-
this.encoded = encoded;
|
|
1488
|
-
// this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
1489
|
-
this.decode(buffer);
|
|
1490
|
-
return;
|
|
1491
1590
|
}
|
|
1492
|
-
if (typeof buffer === 'string') {
|
|
1493
|
-
if (
|
|
1591
|
+
else if (typeof buffer === 'string') {
|
|
1592
|
+
if (utils.getCodec(buffer))
|
|
1494
1593
|
this.fromName(buffer);
|
|
1495
1594
|
else if (this.isHex(buffer))
|
|
1496
1595
|
this.fromHex(buffer);
|
|
@@ -1502,7 +1601,7 @@ let Codec$1 = class Codec extends BasicInterface$1 {
|
|
|
1502
1601
|
throw new Error(`unsupported string ${buffer}`);
|
|
1503
1602
|
}
|
|
1504
1603
|
if (!isNaN(buffer))
|
|
1505
|
-
if (
|
|
1604
|
+
if (utils.getCodec(buffer))
|
|
1506
1605
|
this.fromCodec(buffer);
|
|
1507
1606
|
}
|
|
1508
1607
|
}
|
|
@@ -1511,22 +1610,16 @@ let Codec$1 = class Codec extends BasicInterface$1 {
|
|
|
1511
1610
|
const name = this.getCodecName(codec);
|
|
1512
1611
|
this.name = name;
|
|
1513
1612
|
this.encoded = encoded;
|
|
1514
|
-
this.decode(encoded);
|
|
1613
|
+
return this.decode(encoded);
|
|
1515
1614
|
}
|
|
1516
1615
|
getCodec(name) {
|
|
1517
|
-
return
|
|
1616
|
+
return utils.getCodec(name);
|
|
1518
1617
|
}
|
|
1519
1618
|
getCodecName(codec) {
|
|
1520
|
-
return
|
|
1521
|
-
const item = this.codecs[c];
|
|
1522
|
-
if (item.codec === codec)
|
|
1523
|
-
return c;
|
|
1524
|
-
else
|
|
1525
|
-
return p;
|
|
1526
|
-
}, undefined);
|
|
1619
|
+
return utils.getCodecName(codec);
|
|
1527
1620
|
}
|
|
1528
1621
|
getHashAlg(name) {
|
|
1529
|
-
return
|
|
1622
|
+
return utils.getHashAlg(name);
|
|
1530
1623
|
}
|
|
1531
1624
|
fromCodec(codec) {
|
|
1532
1625
|
this.name = this.getCodecName(codec);
|
|
@@ -1541,13 +1634,15 @@ let Codec$1 = class Codec extends BasicInterface$1 {
|
|
|
1541
1634
|
this.hashAlg = this.getHashAlg(name);
|
|
1542
1635
|
this.codecBuffer = index$6.encode(codec);
|
|
1543
1636
|
}
|
|
1544
|
-
decode() {
|
|
1545
|
-
|
|
1637
|
+
decode(encoded) {
|
|
1638
|
+
encoded = encoded || this.encoded;
|
|
1639
|
+
const codec = index$6.decode(encoded);
|
|
1546
1640
|
this.fromCodec(codec);
|
|
1641
|
+
return this.decoded;
|
|
1547
1642
|
}
|
|
1548
|
-
encode() {
|
|
1549
|
-
|
|
1550
|
-
this.encoded = codec;
|
|
1643
|
+
encode(codec) {
|
|
1644
|
+
codec = codec || this.codec;
|
|
1645
|
+
this.encoded = index$6.encode(codec);
|
|
1551
1646
|
return this.encoded;
|
|
1552
1647
|
}
|
|
1553
1648
|
};
|
|
@@ -1683,19 +1778,13 @@ let CodecHash$1 = class CodecHash extends BasicInterface$1 {
|
|
|
1683
1778
|
};
|
|
1684
1779
|
|
|
1685
1780
|
let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
1686
|
-
|
|
1687
|
-
this._proto = value;
|
|
1688
|
-
this.keys = Object.keys(this._proto);
|
|
1689
|
-
}
|
|
1690
|
-
get proto() {
|
|
1691
|
-
return this._proto;
|
|
1692
|
-
}
|
|
1781
|
+
hashFormat;
|
|
1693
1782
|
init(buffer) {
|
|
1694
1783
|
if (buffer instanceof Uint8Array)
|
|
1695
1784
|
this.fromUint8Array(buffer);
|
|
1696
1785
|
else if (buffer instanceof ArrayBuffer)
|
|
1697
1786
|
this.fromArrayBuffer(buffer);
|
|
1698
|
-
else if (buffer?.name === this.name)
|
|
1787
|
+
else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
|
|
1699
1788
|
return buffer;
|
|
1700
1789
|
else if (buffer instanceof String) {
|
|
1701
1790
|
if (this.isHex(buffer))
|
|
@@ -1719,18 +1808,17 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
|
1719
1808
|
if (codec.name)
|
|
1720
1809
|
return true;
|
|
1721
1810
|
}
|
|
1722
|
-
decode() {
|
|
1723
|
-
|
|
1811
|
+
decode(encoded) {
|
|
1812
|
+
encoded = encoded || this.encoded;
|
|
1724
1813
|
const codec = new Codec$1(this.encoded);
|
|
1725
1814
|
if (codec.codecBuffer) {
|
|
1726
1815
|
encoded = encoded.slice(codec.codecBuffer.length);
|
|
1727
1816
|
this.name = codec.name;
|
|
1728
1817
|
this.decoded = this.protoDecode(encoded);
|
|
1729
|
-
try {
|
|
1730
|
-
|
|
1731
|
-
}
|
|
1732
|
-
|
|
1733
|
-
}
|
|
1818
|
+
// try {
|
|
1819
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
1820
|
+
// } catch {
|
|
1821
|
+
// }
|
|
1734
1822
|
}
|
|
1735
1823
|
else {
|
|
1736
1824
|
throw new Error(`no codec found`);
|
|
@@ -1762,11 +1850,11 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
|
|
|
1762
1850
|
* @param {Object} proto - {protoObject}
|
|
1763
1851
|
* @param {Object} options - {hashFormat, name}
|
|
1764
1852
|
*/
|
|
1765
|
-
constructor(buffer, proto, options
|
|
1853
|
+
constructor(buffer, proto, options) {
|
|
1766
1854
|
super();
|
|
1767
1855
|
this.proto = proto;
|
|
1768
|
-
this.hashFormat = options.hashFormat
|
|
1769
|
-
if (options
|
|
1856
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
1857
|
+
if (options?.name)
|
|
1770
1858
|
this.name = options.name;
|
|
1771
1859
|
this.init(buffer);
|
|
1772
1860
|
}
|
|
@@ -1797,54 +1885,55 @@ const FormatInterface = FormatInterface$1;
|
|
|
1797
1885
|
const Codec = Codec$1;
|
|
1798
1886
|
|
|
1799
1887
|
const BufferToUint8Array = data => {
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1888
|
+
if (data.type === 'Buffer') {
|
|
1889
|
+
data = new Uint8Array(data.data);
|
|
1890
|
+
}
|
|
1891
|
+
return data;
|
|
1804
1892
|
};
|
|
1805
|
-
|
|
1806
1893
|
const protoFor = (message) => {
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1894
|
+
const codec = new Codec(message);
|
|
1895
|
+
if (!codec.name)
|
|
1896
|
+
throw new Error('proto not found');
|
|
1897
|
+
const Proto = globalThis.peernet.protos[codec.name];
|
|
1898
|
+
if (!Proto)
|
|
1899
|
+
throw (new Error(`No proto defined for ${codec.name}`));
|
|
1900
|
+
return new Proto(message);
|
|
1812
1901
|
};
|
|
1813
|
-
|
|
1814
1902
|
/**
|
|
1815
1903
|
* wether or not a peernet daemon is active
|
|
1816
1904
|
* @return {Boolean}
|
|
1817
1905
|
*/
|
|
1818
1906
|
const hasDaemon = async () => {
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1907
|
+
try {
|
|
1908
|
+
let response = await fetch('http://127.0.0.1:1000/api/version');
|
|
1909
|
+
response = await response.json();
|
|
1910
|
+
return Boolean(response.client === '@peernet/api/http');
|
|
1911
|
+
}
|
|
1912
|
+
catch (e) {
|
|
1913
|
+
return false;
|
|
1914
|
+
}
|
|
1826
1915
|
};
|
|
1827
|
-
|
|
1828
1916
|
const https = () => {
|
|
1829
|
-
|
|
1830
|
-
|
|
1917
|
+
if (!globalThis.location)
|
|
1918
|
+
return false;
|
|
1919
|
+
return Boolean(globalThis.location.protocol === 'https:');
|
|
1831
1920
|
};
|
|
1832
|
-
|
|
1833
1921
|
/**
|
|
1834
1922
|
* Get current environment
|
|
1835
1923
|
* @return {String} current environment [node, electron, browser]
|
|
1836
1924
|
*/
|
|
1837
1925
|
const environment = () => {
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1926
|
+
const _navigator = globalThis.navigator;
|
|
1927
|
+
if (!_navigator) {
|
|
1928
|
+
return 'node';
|
|
1929
|
+
}
|
|
1930
|
+
else if (_navigator && /electron/i.test(_navigator.userAgent)) {
|
|
1931
|
+
return 'electron';
|
|
1932
|
+
}
|
|
1933
|
+
else {
|
|
1934
|
+
return 'browser';
|
|
1935
|
+
}
|
|
1846
1936
|
};
|
|
1847
|
-
|
|
1848
1937
|
/**
|
|
1849
1938
|
* * Get current environment
|
|
1850
1939
|
* @return {Object} result
|
|
@@ -1852,84 +1941,84 @@ const environment = () => {
|
|
|
1852
1941
|
* @property {Boolean} reult.environment Current environment
|
|
1853
1942
|
*/
|
|
1854
1943
|
const target = async () => {
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1944
|
+
let daemon = false;
|
|
1945
|
+
if (!https())
|
|
1946
|
+
daemon = await hasDaemon();
|
|
1947
|
+
return { daemon, environment: environment() };
|
|
1859
1948
|
};
|
|
1860
1949
|
|
|
1861
1950
|
class PeerDiscovery {
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
}
|
|
1865
|
-
|
|
1866
|
-
_getPeerId(id) {
|
|
1867
|
-
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0) return false
|
|
1868
|
-
|
|
1869
|
-
for (const entry of [...peernet.peerMap.entries()]) {
|
|
1870
|
-
for (const _id of entry[1]) {
|
|
1871
|
-
if (_id === id) return entry[0]
|
|
1872
|
-
}
|
|
1951
|
+
constructor(id) {
|
|
1952
|
+
this.id = id;
|
|
1873
1953
|
}
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
response = await protoFor(response);
|
|
1884
|
-
response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
|
|
1885
|
-
|
|
1886
|
-
id = response.decoded.id;
|
|
1887
|
-
if (id === this.id) return;
|
|
1888
|
-
|
|
1889
|
-
if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id]);
|
|
1890
|
-
else {
|
|
1891
|
-
const connections = peernet.peerMap.get(id);
|
|
1892
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
1893
|
-
connections.push(peer.id);
|
|
1894
|
-
peernet.peerMap.set(peer.id, connections);
|
|
1895
|
-
}
|
|
1954
|
+
_getPeerId(id) {
|
|
1955
|
+
if (!peernet.peerMap || peernet.peerMap && peernet.peerMap.size === 0)
|
|
1956
|
+
return false;
|
|
1957
|
+
for (const entry of [...peernet.peerMap.entries()]) {
|
|
1958
|
+
for (const _id of entry[1]) {
|
|
1959
|
+
if (_id === id)
|
|
1960
|
+
return entry[0];
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1896
1963
|
}
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
}
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1964
|
+
async discover(peer) {
|
|
1965
|
+
let id = this._getPeerId(peer.id);
|
|
1966
|
+
if (id)
|
|
1967
|
+
return id;
|
|
1968
|
+
const data = await new peernet.protos['peernet-peer']({ id: this.id });
|
|
1969
|
+
const node = await peernet.prepareMessage(peer.id, data.encoded);
|
|
1970
|
+
let response = await peer.request(node.encoded);
|
|
1971
|
+
response = await protoFor(response);
|
|
1972
|
+
response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
|
|
1973
|
+
id = response.decoded.id;
|
|
1974
|
+
if (id === this.id)
|
|
1975
|
+
return;
|
|
1976
|
+
if (!peernet.peerMap.has(id))
|
|
1977
|
+
peernet.peerMap.set(id, [peer.id]);
|
|
1978
|
+
else {
|
|
1979
|
+
const connections = peernet.peerMap.get(id);
|
|
1980
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
1981
|
+
connections.push(peer.id);
|
|
1982
|
+
peernet.peerMap.set(peer.id, connections);
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
return id;
|
|
1986
|
+
}
|
|
1987
|
+
async discoverHandler(message, peer) {
|
|
1988
|
+
const { id, proto } = message;
|
|
1989
|
+
// if (typeof message.data === 'string') message.data = Buffer.from(message.data)
|
|
1990
|
+
if (proto.name === 'peernet-peer') {
|
|
1991
|
+
const from = proto.decoded.id;
|
|
1992
|
+
if (from === this.id)
|
|
1993
|
+
return;
|
|
1994
|
+
if (!peernet.peerMap.has(from))
|
|
1995
|
+
peernet.peerMap.set(from, [peer.id]);
|
|
1996
|
+
else {
|
|
1997
|
+
const connections = peernet.peerMap.get(from);
|
|
1998
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
1999
|
+
connections.push(peer.id);
|
|
2000
|
+
peernet.peerMap.set(from, connections);
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
|
|
2004
|
+
const node = await peernet.prepareMessage(from, data.encoded);
|
|
2005
|
+
peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
|
|
2006
|
+
}
|
|
2007
|
+
else if (proto.name === 'peernet-peer-response') {
|
|
2008
|
+
const from = proto.decoded.id;
|
|
2009
|
+
if (from === this.id)
|
|
2010
|
+
return;
|
|
2011
|
+
if (!peernet.peerMap.has(from))
|
|
2012
|
+
peernet.peerMap.set(from, [peer.id]);
|
|
2013
|
+
else {
|
|
2014
|
+
const connections = peernet.peerMap.get(from);
|
|
2015
|
+
if (connections.indexOf(peer.id) === -1) {
|
|
2016
|
+
connections.push(peer.id);
|
|
2017
|
+
peernet.peerMap.set(from, connections);
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
1929
2020
|
}
|
|
1930
|
-
}
|
|
1931
2021
|
}
|
|
1932
|
-
}
|
|
1933
2022
|
}
|
|
1934
2023
|
|
|
1935
2024
|
/**
|
|
@@ -1938,178 +2027,162 @@ class PeerDiscovery {
|
|
|
1938
2027
|
* @property {Object} ptr
|
|
1939
2028
|
*/
|
|
1940
2029
|
const lastFetched = {
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
2030
|
+
address: {
|
|
2031
|
+
value: undefined,
|
|
2032
|
+
timestamp: 0,
|
|
2033
|
+
},
|
|
2034
|
+
ptr: {
|
|
2035
|
+
value: undefined,
|
|
2036
|
+
timestamp: 0,
|
|
2037
|
+
},
|
|
1949
2038
|
};
|
|
1950
|
-
|
|
1951
2039
|
const getAddress = async () => {
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
return address.value
|
|
2040
|
+
const { address } = lastFetched;
|
|
2041
|
+
const now = Math.round(new Date().getTime() / 1000);
|
|
2042
|
+
if (now - address.timestamp > 1200000) {
|
|
2043
|
+
address.value = await fetch('https://icanhazip.com/');
|
|
2044
|
+
address.value = await address.value.text();
|
|
2045
|
+
address.timestamp = Math.round(new Date().getTime() / 1000);
|
|
2046
|
+
lastFetched.address = address;
|
|
2047
|
+
}
|
|
2048
|
+
return address.value;
|
|
1962
2049
|
};
|
|
1963
|
-
|
|
1964
2050
|
const degreesToRadians = (degrees) => {
|
|
1965
|
-
|
|
2051
|
+
return degrees * Math.PI / 180;
|
|
1966
2052
|
};
|
|
1967
|
-
|
|
1968
2053
|
const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
|
1979
|
-
return earthRadiusKm * c;
|
|
2054
|
+
const earthRadiusKm = 6371;
|
|
2055
|
+
const dLat = degreesToRadians(lat2 - lat1);
|
|
2056
|
+
const dLon = degreesToRadians(lon2 - lon1);
|
|
2057
|
+
lat1 = degreesToRadians(lat1);
|
|
2058
|
+
lat2 = degreesToRadians(lat2);
|
|
2059
|
+
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
2060
|
+
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
2061
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
2062
|
+
return earthRadiusKm * c;
|
|
1980
2063
|
};
|
|
1981
|
-
|
|
1982
2064
|
class DhtEarth {
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2065
|
+
/**
|
|
2066
|
+
*
|
|
2067
|
+
*/
|
|
2068
|
+
constructor() {
|
|
2069
|
+
this.providerMap = new Map();
|
|
2070
|
+
}
|
|
2071
|
+
/**
|
|
2072
|
+
* @param {Object} address
|
|
2073
|
+
* @return {Object} {latitude: lat, longitude: lon}
|
|
2074
|
+
*/
|
|
2075
|
+
async getCoordinates(address) {
|
|
2076
|
+
// const {address} = parseAddress(provider)
|
|
2077
|
+
const request = `https://whereis.leofcoin.org/?ip=${address}`;
|
|
2078
|
+
let response = await fetch(request);
|
|
2079
|
+
response = await response.json();
|
|
2080
|
+
const { lat, lon } = response;
|
|
2081
|
+
return { latitude: lat, longitude: lon };
|
|
2082
|
+
}
|
|
2083
|
+
/**
|
|
2084
|
+
* @param {Object} peer
|
|
2085
|
+
* @param {Object} provider
|
|
2086
|
+
* @return {Object} {provider, distance}
|
|
2087
|
+
*/
|
|
2088
|
+
async getDistance(peer, provider) {
|
|
2089
|
+
const { latitude, longitude } = await this.getCoordinates(provider.address);
|
|
2090
|
+
return { provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude) };
|
|
2091
|
+
}
|
|
2092
|
+
/**
|
|
2093
|
+
* @param {Array} providers
|
|
2094
|
+
* @return {Object} closestPeer
|
|
2095
|
+
*/
|
|
2096
|
+
async closestPeer(providers) {
|
|
2097
|
+
let all = [];
|
|
2098
|
+
const address = await getAddress();
|
|
2099
|
+
const peerLoc = await this.getCoordinates(address);
|
|
2100
|
+
for (const provider of providers) {
|
|
2101
|
+
if (provider.address === '127.0.0.1')
|
|
2102
|
+
all.push({ provider, distance: 0 });
|
|
2103
|
+
else
|
|
2104
|
+
all.push(this.getDistance(peerLoc, provider));
|
|
2105
|
+
}
|
|
2106
|
+
all = await Promise.all(all);
|
|
2107
|
+
all = all.sort((previous, current) => previous.distance - current.distance);
|
|
2108
|
+
return all[0].provider;
|
|
2109
|
+
}
|
|
2110
|
+
/**
|
|
2111
|
+
* @param {String} hash
|
|
2112
|
+
* @return {Array} providers
|
|
2113
|
+
*/
|
|
2114
|
+
providersFor(hash) {
|
|
2115
|
+
return this.providerMap.get(hash);
|
|
2116
|
+
}
|
|
2117
|
+
/**
|
|
2118
|
+
* @param {String} address
|
|
2119
|
+
* @param {String} hash
|
|
2120
|
+
* @return {Array} providers
|
|
2121
|
+
*/
|
|
2122
|
+
async addProvider(address, hash) {
|
|
2123
|
+
let providers = [];
|
|
2124
|
+
if (this.providerMap.has(hash))
|
|
2125
|
+
providers = this.providerMap.get(hash);
|
|
2126
|
+
providers = new Set([...providers, address]);
|
|
2127
|
+
this.providerMap.set(hash, providers);
|
|
2128
|
+
return providers;
|
|
2025
2129
|
}
|
|
2026
|
-
|
|
2027
|
-
all = await Promise.all(all);
|
|
2028
|
-
all = all.sort((previous, current) => previous.distance - current.distance);
|
|
2029
|
-
return all[0].provider;
|
|
2030
|
-
}
|
|
2031
|
-
|
|
2032
|
-
/**
|
|
2033
|
-
* @param {String} hash
|
|
2034
|
-
* @return {Array} providers
|
|
2035
|
-
*/
|
|
2036
|
-
providersFor(hash) {
|
|
2037
|
-
return this.providerMap.get(hash);
|
|
2038
|
-
}
|
|
2039
|
-
|
|
2040
|
-
/**
|
|
2041
|
-
* @param {String} address
|
|
2042
|
-
* @param {String} hash
|
|
2043
|
-
* @return {Array} providers
|
|
2044
|
-
*/
|
|
2045
|
-
async addProvider(address, hash) {
|
|
2046
|
-
let providers = [];
|
|
2047
|
-
if (this.providerMap.has(hash)) providers = this.providerMap.get(hash);
|
|
2048
|
-
|
|
2049
|
-
providers = new Set([...providers, address]);
|
|
2050
|
-
this.providerMap.set(hash, providers);
|
|
2051
|
-
return providers;
|
|
2052
|
-
}
|
|
2053
2130
|
}
|
|
2054
2131
|
|
|
2055
2132
|
class MessageHandler {
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2133
|
+
constructor(network) {
|
|
2134
|
+
this.network = network;
|
|
2135
|
+
}
|
|
2136
|
+
/**
|
|
2137
|
+
* hash and sign message
|
|
2138
|
+
*
|
|
2139
|
+
* @param {object} message
|
|
2140
|
+
* @param {Buffer} message.from peer id
|
|
2141
|
+
* @param {Buffer} message.to peer id
|
|
2142
|
+
* @param {string} message.data Peernet message
|
|
2143
|
+
* (PeernetMessage excluded) encoded as a string
|
|
2144
|
+
* @return message
|
|
2145
|
+
*/
|
|
2146
|
+
async hashAndSignMessage(message) {
|
|
2147
|
+
let identity = await walletStore.get('identity');
|
|
2148
|
+
identity = JSON.parse(identity);
|
|
2149
|
+
if (!globalThis.MultiWallet) {
|
|
2150
|
+
const importee = await import(/* webpackChunkName: "multi-wallet" */ './index-7018cab6.js');
|
|
2151
|
+
globalThis.MultiWallet = importee.default;
|
|
2152
|
+
}
|
|
2153
|
+
const wallet = new MultiWallet(this.network);
|
|
2154
|
+
wallet.recover(identity.mnemonic);
|
|
2155
|
+
message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32));
|
|
2156
|
+
return message;
|
|
2157
|
+
}
|
|
2158
|
+
/**
|
|
2159
|
+
* @param {String} from - peer id
|
|
2160
|
+
* @param {String} to - peer id
|
|
2161
|
+
* @param {String|PeernetMessage} data - data encoded message string
|
|
2162
|
+
* or the messageNode itself
|
|
2163
|
+
*/
|
|
2164
|
+
async prepareMessage(message) {
|
|
2165
|
+
if (message.keys.includes('signature')) {
|
|
2166
|
+
message = await this.hashAndSignMessage(message);
|
|
2167
|
+
}
|
|
2168
|
+
return message;
|
|
2091
2169
|
}
|
|
2092
|
-
|
|
2093
|
-
return message
|
|
2094
|
-
}
|
|
2095
2170
|
}
|
|
2096
2171
|
|
|
2097
|
-
const dataHandler = async message => {
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
2104
|
-
};
|
|
2105
|
-
|
|
2106
|
-
const dhtError = (proto) => {
|
|
2107
|
-
const text = `Received proto ${proto.name} expected peernet-dht-response`;
|
|
2108
|
-
return new Error(`Routing error: ${text}`)
|
|
2172
|
+
const dataHandler = async (message) => {
|
|
2173
|
+
if (!message)
|
|
2174
|
+
return;
|
|
2175
|
+
const { data, id, from } = message;
|
|
2176
|
+
const proto = await protoFor(data);
|
|
2177
|
+
peernet._protoHandler({ id, proto }, peernet.client.connections[from], from);
|
|
2109
2178
|
};
|
|
2110
2179
|
|
|
2111
|
-
const
|
|
2112
|
-
|
|
2180
|
+
const dhtError = (proto) => {
|
|
2181
|
+
const text = `Received proto ${proto.name} expected peernet-dht-response`;
|
|
2182
|
+
return new Error(`Routing error: ${text}`);
|
|
2183
|
+
};
|
|
2184
|
+
const nothingFoundError = (hash) => {
|
|
2185
|
+
return new Error(`nothing found for ${hash}`);
|
|
2113
2186
|
};
|
|
2114
2187
|
|
|
2115
2188
|
// import base32 from '@vandeurenglenn/base32'
|
|
@@ -2280,698 +2353,645 @@ class LeofcoinStorage {
|
|
|
2280
2353
|
}
|
|
2281
2354
|
|
|
2282
2355
|
globalThis.LeofcoinStorage = LeofcoinStorage;
|
|
2283
|
-
|
|
2284
2356
|
globalThis.leofcoin = globalThis.leofcoin || {};
|
|
2285
2357
|
globalThis.pubsub = globalThis.pubsub || new LittlePubSub();
|
|
2286
|
-
globalThis.globalSub = globalThis.globalSub || new LittlePubSub(
|
|
2287
|
-
|
|
2358
|
+
globalThis.globalSub = globalThis.globalSub || new LittlePubSub(true);
|
|
2288
2359
|
/**
|
|
2289
2360
|
* @access public
|
|
2290
2361
|
* @example
|
|
2291
2362
|
* const peernet = new Peernet();
|
|
2292
2363
|
*/
|
|
2293
2364
|
class Peernet {
|
|
2294
|
-
|
|
2295
|
-
* @access public
|
|
2296
|
-
* @param {Object} options
|
|
2297
|
-
* @param {String} options.network - desired network
|
|
2298
|
-
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
2299
|
-
* @param {String} options.root - path to root directory
|
|
2300
|
-
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
2301
|
-
*
|
|
2302
|
-
* @return {Promise} instance of Peernet
|
|
2303
|
-
*
|
|
2304
|
-
* @example
|
|
2305
|
-
* const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
|
|
2306
|
-
*/
|
|
2307
|
-
constructor(options = {}) {
|
|
2308
|
-
this._discovered = [];
|
|
2309
|
-
/**
|
|
2310
|
-
* @property {String} network - current network
|
|
2311
|
-
*/
|
|
2312
|
-
this.network = options.network || 'leofcoin';
|
|
2313
|
-
this.stars = options.stars;
|
|
2314
|
-
const parts = this.network.split(':');
|
|
2315
|
-
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
2316
|
-
|
|
2317
|
-
if (!options.storePrefix) options.storePrefix = 'lfc';
|
|
2318
|
-
if (!options.port) options.port = 2000;
|
|
2319
|
-
if (!options.root) {
|
|
2320
|
-
parts[1] ? options.root = `.${parts[0]}/${parts[1]}` : options.root = `.${this.network}`;
|
|
2321
|
-
}
|
|
2322
|
-
|
|
2323
|
-
globalThis.peernet = this;
|
|
2324
|
-
this.bw = {
|
|
2325
|
-
up: 0,
|
|
2326
|
-
down: 0,
|
|
2327
|
-
};
|
|
2328
|
-
return this._init(options)
|
|
2329
|
-
}
|
|
2330
|
-
|
|
2331
|
-
get defaultStores() {
|
|
2332
|
-
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
|
|
2333
|
-
}
|
|
2334
|
-
|
|
2335
|
-
addProto(name, proto) {
|
|
2336
|
-
if (!globalThis.peernet.protos[name]) globalThis.peernet.protos[name] = proto;
|
|
2337
|
-
}
|
|
2338
|
-
|
|
2339
|
-
addCodec(name, codec) {
|
|
2340
|
-
if (!this.codecs[name]) this.codecs[name] = codec;
|
|
2341
|
-
}
|
|
2342
|
-
|
|
2343
|
-
async addStore(name, prefix, root, isPrivate = true) {
|
|
2344
|
-
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
2345
|
-
name === 'data' || name === 'message') isPrivate = false;
|
|
2346
|
-
|
|
2347
|
-
let Storage;
|
|
2348
|
-
|
|
2349
|
-
this.hasDaemon ? Storage = LeofcoinStorageClient : Storage = LeofcoinStorage;
|
|
2350
|
-
|
|
2351
|
-
if (!globalThis[`${name}Store`]) {
|
|
2352
|
-
globalThis[`${name}Store`] = new Storage(name, root);
|
|
2353
|
-
await globalThis[`${name}Store`].init();
|
|
2354
|
-
}
|
|
2355
|
-
|
|
2356
|
-
globalThis[`${name}Store`].private = isPrivate;
|
|
2357
|
-
if (!isPrivate) this.stores.push(name);
|
|
2358
|
-
}
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
/**
|
|
2362
|
-
* @see MessageHandler
|
|
2363
|
-
*/
|
|
2364
|
-
prepareMessage(data) {
|
|
2365
|
-
return this._messageHandler.prepareMessage(data)
|
|
2366
|
-
}
|
|
2367
|
-
|
|
2368
|
-
/**
|
|
2369
|
-
* @access public
|
|
2370
|
-
*
|
|
2371
|
-
* @return {Array} peerId
|
|
2372
|
-
*/
|
|
2373
|
-
get peers() {
|
|
2374
|
-
return Object.keys(this.client.connections)
|
|
2375
|
-
}
|
|
2376
|
-
|
|
2377
|
-
get connections() {
|
|
2378
|
-
return Object.values(this.client.connections)
|
|
2379
|
-
}
|
|
2380
|
-
|
|
2381
|
-
get peerEntries() {
|
|
2382
|
-
return Object.entries(this.client.connections)
|
|
2383
|
-
}
|
|
2384
|
-
|
|
2385
|
-
/**
|
|
2386
|
-
* @return {String} id - peerId
|
|
2387
|
-
*/
|
|
2388
|
-
getConnection(id) {
|
|
2389
|
-
return this.client.connections[id]
|
|
2390
|
-
}
|
|
2391
|
-
|
|
2392
|
-
/**
|
|
2393
|
-
* @private
|
|
2394
|
-
*
|
|
2395
|
-
* @param {Object} options
|
|
2396
|
-
* @param {String} options.root - path to root directory
|
|
2397
|
-
*
|
|
2398
|
-
* @return {Promise} instance of Peernet
|
|
2399
|
-
*/
|
|
2400
|
-
async _init(options) {
|
|
2401
|
-
// peernetDHT aka closesPeer by coordinates
|
|
2365
|
+
stores = [];
|
|
2402
2366
|
/**
|
|
2403
2367
|
* @type {Object}
|
|
2404
2368
|
* @property {Object} peer Instance of Peer
|
|
2405
2369
|
*/
|
|
2406
|
-
|
|
2370
|
+
dht = new DhtEarth();
|
|
2371
|
+
/** @leofcoin/peernet-swarm/client */
|
|
2372
|
+
client;
|
|
2373
|
+
network;
|
|
2374
|
+
stars;
|
|
2375
|
+
networkVersion;
|
|
2376
|
+
bw;
|
|
2407
2377
|
/**
|
|
2408
|
-
* @
|
|
2409
|
-
* @
|
|
2378
|
+
* @access public
|
|
2379
|
+
* @param {Object} options
|
|
2380
|
+
* @param {String} options.network - desired network
|
|
2381
|
+
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
2382
|
+
* @param {String} options.root - path to root directory
|
|
2383
|
+
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
2384
|
+
*
|
|
2385
|
+
* @return {Promise} instance of Peernet
|
|
2386
|
+
*
|
|
2387
|
+
* @example
|
|
2388
|
+
* const peernet = new Peernet({network: 'leofcoin', root: '.leofcoin'});
|
|
2410
2389
|
*/
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2390
|
+
constructor(options) {
|
|
2391
|
+
/**
|
|
2392
|
+
* @property {String} network - current network
|
|
2393
|
+
*/
|
|
2394
|
+
this.network = options.network || 'leofcoin';
|
|
2395
|
+
this.stars = options.stars;
|
|
2396
|
+
const parts = this.network.split(':');
|
|
2397
|
+
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
2398
|
+
if (!options.storePrefix)
|
|
2399
|
+
options.storePrefix = 'lfc';
|
|
2400
|
+
if (!options.port)
|
|
2401
|
+
options.port = 2000;
|
|
2402
|
+
if (!options.root) {
|
|
2403
|
+
parts[1] ? options.root = `.${parts[0]}/${parts[1]}` : options.root = `.${this.network}`;
|
|
2404
|
+
}
|
|
2405
|
+
globalThis.peernet = this;
|
|
2406
|
+
this.bw = {
|
|
2407
|
+
up: 0,
|
|
2408
|
+
down: 0,
|
|
2409
|
+
};
|
|
2410
|
+
return this._init(options);
|
|
2411
|
+
}
|
|
2412
|
+
get defaultStores() {
|
|
2413
|
+
return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message'];
|
|
2414
|
+
}
|
|
2415
|
+
addProto(name, proto) {
|
|
2416
|
+
if (!globalThis.peernet.protos[name])
|
|
2417
|
+
globalThis.peernet.protos[name] = proto;
|
|
2418
|
+
}
|
|
2419
|
+
addCodec(codec) {
|
|
2420
|
+
return utils.addCodec(codec);
|
|
2421
|
+
}
|
|
2422
|
+
async addStore(name, prefix, root, isPrivate = true) {
|
|
2423
|
+
if (name === 'block' || name === 'transaction' || name === 'chain' ||
|
|
2424
|
+
name === 'data' || name === 'message')
|
|
2425
|
+
isPrivate = false;
|
|
2426
|
+
let Storage;
|
|
2427
|
+
this.hasDaemon ? Storage = LeofcoinStorageClient : Storage = LeofcoinStorage;
|
|
2428
|
+
if (!globalThis[`${name}Store`]) {
|
|
2429
|
+
globalThis[`${name}Store`] = new Storage(name, root);
|
|
2430
|
+
await globalThis[`${name}Store`].init();
|
|
2431
|
+
}
|
|
2432
|
+
globalThis[`${name}Store`].private = isPrivate;
|
|
2433
|
+
if (!isPrivate)
|
|
2434
|
+
this.stores.push(name);
|
|
2435
|
+
}
|
|
2433
2436
|
/**
|
|
2434
|
-
*
|
|
2435
|
-
* @type {Object}
|
|
2436
|
-
* @property {PeernetMessage} protos[peernet-message] messageNode
|
|
2437
|
-
* @property {DHTMessage} protos[peernet-dht] messageNode
|
|
2438
|
-
* @property {DHTMessageResponse} protos[peernet-dht-response] messageNode
|
|
2439
|
-
* @property {DataMessage} protos[peernet-data] messageNode
|
|
2440
|
-
* @property {DataMessageResponse} protos[peernet-data-response] messageNode
|
|
2437
|
+
* @see MessageHandler
|
|
2441
2438
|
*/
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
'peernet-request': RequestMessage,
|
|
2445
|
-
'peernet-response': ResponseMessage,
|
|
2446
|
-
'peernet-peer': PeerMessage,
|
|
2447
|
-
'peernet-peer-response': PeerMessageResponse,
|
|
2448
|
-
'peernet-message': PeernetMessage,
|
|
2449
|
-
'peernet-dht': DHTMessage,
|
|
2450
|
-
'peernet-dht-response': DHTMessageResponse,
|
|
2451
|
-
'peernet-data': DataMessage,
|
|
2452
|
-
'peernet-data-response': DataMessageResponse,
|
|
2453
|
-
'peernet-ps': PsMessage,
|
|
2454
|
-
'chat-message': ChatMessage,
|
|
2455
|
-
'peernet-file': PeernetFile
|
|
2456
|
-
};
|
|
2457
|
-
|
|
2458
|
-
this._messageHandler = new MessageHandler(this.network);
|
|
2459
|
-
|
|
2460
|
-
const {daemon, environment} = await target();
|
|
2461
|
-
this.hasDaemon = daemon;
|
|
2462
|
-
|
|
2463
|
-
for (const store of this.defaultStores) {
|
|
2464
|
-
await this.addStore(store, options.storePrefix, options.root);
|
|
2439
|
+
prepareMessage(data) {
|
|
2440
|
+
return this._messageHandler.prepareMessage(data);
|
|
2465
2441
|
}
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
this.accounts = JSON.parse(accounts);
|
|
2480
|
-
} catch {
|
|
2481
|
-
this.accounts = [accounts.split(',')];
|
|
2482
|
-
}
|
|
2483
|
-
} else {
|
|
2484
|
-
const importee = await import(/* webpackChunkName: "generate-account" */ './index-73b8d9b9.js');
|
|
2485
|
-
const generateAccount = importee.default;
|
|
2486
|
-
const {identity, accounts, config} = await generateAccount(this.network);
|
|
2487
|
-
// await accountStore.put('config', JSON.stringify(config));
|
|
2488
|
-
await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
|
|
2489
|
-
|
|
2490
|
-
await walletStore.put('version', String(1));
|
|
2491
|
-
await walletStore.put('accounts', JSON.stringify(accounts));
|
|
2492
|
-
await walletStore.put('selected-account', accounts[0][1]);
|
|
2493
|
-
await walletStore.put('identity', JSON.stringify(identity));
|
|
2494
|
-
|
|
2495
|
-
globalThis.peernet.selectedAccount = accounts[0][1];
|
|
2496
|
-
this.id = identity.walletId;
|
|
2442
|
+
/**
|
|
2443
|
+
* @access public
|
|
2444
|
+
*
|
|
2445
|
+
* @return {Array} peerId
|
|
2446
|
+
*/
|
|
2447
|
+
get peers() {
|
|
2448
|
+
return Object.keys(this.client.connections);
|
|
2449
|
+
}
|
|
2450
|
+
get connections() {
|
|
2451
|
+
return Object.values(this.client.connections);
|
|
2452
|
+
}
|
|
2453
|
+
get peerEntries() {
|
|
2454
|
+
return Object.entries(this.client.connections);
|
|
2497
2455
|
}
|
|
2498
|
-
this._peerHandler = new PeerDiscovery(this.id);
|
|
2499
|
-
this.peerId = this.id;
|
|
2500
|
-
|
|
2501
|
-
pubsub.subscribe('peer:connected', async (peer) => {
|
|
2502
|
-
// console.log(peer);
|
|
2503
|
-
// console.log({connected: peer.id, as: this._getPeerId(peer.id) });
|
|
2504
|
-
// peer.on('peernet.data', async (message) => {
|
|
2505
|
-
// const id = message.id
|
|
2506
|
-
// message = new PeernetMessage(Buffer.from(message.data.data))
|
|
2507
|
-
// const proto = protoFor(message.decoded.data)
|
|
2508
|
-
// this._protoHandler({id, proto}, peer)
|
|
2509
|
-
// })
|
|
2510
|
-
});
|
|
2511
|
-
|
|
2512
2456
|
/**
|
|
2513
|
-
*
|
|
2514
|
-
* @see DataHandler
|
|
2457
|
+
* @return {String} id - peerId
|
|
2515
2458
|
*/
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
const importee = await import(/* webpackChunkName: "peernet-swarm" */ './client-13d9b3de.js');
|
|
2459
|
+
getConnection(id) {
|
|
2460
|
+
return this.client.connections[id];
|
|
2461
|
+
}
|
|
2520
2462
|
/**
|
|
2521
|
-
* @
|
|
2522
|
-
*
|
|
2463
|
+
* @private
|
|
2464
|
+
*
|
|
2465
|
+
* @param {Object} options
|
|
2466
|
+
* @param {String} options.root - path to root directory
|
|
2467
|
+
*
|
|
2468
|
+
* @return {Promise} instance of Peernet
|
|
2523
2469
|
*/
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2470
|
+
async _init(options) {
|
|
2471
|
+
this.requestProtos = {};
|
|
2472
|
+
this.storePrefix = options.storePrefix;
|
|
2473
|
+
this.root = options.root;
|
|
2474
|
+
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
2475
|
+
// FolderMessageResponse
|
|
2476
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-452642e3.js');
|
|
2477
|
+
/**
|
|
2478
|
+
* proto Object containing protos
|
|
2479
|
+
* @type {Object}
|
|
2480
|
+
* @property {PeernetMessage} protos[peernet-message] messageNode
|
|
2481
|
+
* @property {DHTMessage} protos[peernet-dht] messageNode
|
|
2482
|
+
* @property {DHTMessageResponse} protos[peernet-dht-response] messageNode
|
|
2483
|
+
* @property {DataMessage} protos[peernet-data] messageNode
|
|
2484
|
+
* @property {DataMessageResponse} protos[peernet-data-response] messageNode
|
|
2485
|
+
*/
|
|
2486
|
+
globalThis.peernet.protos = {
|
|
2487
|
+
'peernet-request': RequestMessage,
|
|
2488
|
+
'peernet-response': ResponseMessage,
|
|
2489
|
+
'peernet-peer': PeerMessage,
|
|
2490
|
+
'peernet-peer-response': PeerMessageResponse,
|
|
2491
|
+
'peernet-message': PeernetMessage,
|
|
2492
|
+
'peernet-dht': DHTMessage,
|
|
2493
|
+
'peernet-dht-response': DHTMessageResponse,
|
|
2494
|
+
'peernet-data': DataMessage,
|
|
2495
|
+
'peernet-data-response': DataMessageResponse,
|
|
2496
|
+
'peernet-ps': PsMessage,
|
|
2497
|
+
'chat-message': ChatMessage,
|
|
2498
|
+
'peernet-file': PeernetFile
|
|
2499
|
+
};
|
|
2500
|
+
this._messageHandler = new MessageHandler(this.network);
|
|
2501
|
+
const { daemon, environment } = await target();
|
|
2502
|
+
this.hasDaemon = daemon;
|
|
2503
|
+
for (const store of this.defaultStores) {
|
|
2504
|
+
await this.addStore(store, options.storePrefix, options.root);
|
|
2505
|
+
}
|
|
2506
|
+
const accountExists = await accountStore.has('public');
|
|
2507
|
+
if (accountExists) {
|
|
2508
|
+
const pub = await accountStore.get('public');
|
|
2509
|
+
this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
|
|
2510
|
+
let accounts = await walletStore.get('accounts');
|
|
2511
|
+
accounts = new TextDecoder().decode(accounts);
|
|
2512
|
+
const selected = await walletStore.get('selected-account');
|
|
2513
|
+
globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
|
|
2514
|
+
// fixing account issue (string while needs to be a JSON)
|
|
2515
|
+
// TODO: remove when on mainnet
|
|
2516
|
+
try {
|
|
2517
|
+
this.accounts = JSON.parse(accounts);
|
|
2518
|
+
}
|
|
2519
|
+
catch {
|
|
2520
|
+
this.accounts = [accounts.split(',')];
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
else {
|
|
2524
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ './index-ddd5774f.js');
|
|
2525
|
+
const generateAccount = importee.default;
|
|
2526
|
+
const { identity, accounts, config } = await generateAccount(this.network);
|
|
2527
|
+
// await accountStore.put('config', JSON.stringify(config));
|
|
2528
|
+
await accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
|
|
2529
|
+
await walletStore.put('version', String(1));
|
|
2530
|
+
await walletStore.put('accounts', JSON.stringify(accounts));
|
|
2531
|
+
await walletStore.put('selected-account', accounts[0][1]);
|
|
2532
|
+
await walletStore.put('identity', JSON.stringify(identity));
|
|
2533
|
+
globalThis.peernet.selectedAccount = accounts[0][1];
|
|
2534
|
+
this.id = identity.walletId;
|
|
2535
|
+
}
|
|
2536
|
+
this._peerHandler = new PeerDiscovery(this.id);
|
|
2537
|
+
this.peerId = this.id;
|
|
2538
|
+
pubsub.subscribe('peer:connected', async (peer) => {
|
|
2539
|
+
// console.log(peer);
|
|
2540
|
+
// console.log({connected: peer.id, as: this._getPeerId(peer.id) });
|
|
2541
|
+
// peer.on('peernet.data', async (message) => {
|
|
2542
|
+
// const id = message.id
|
|
2543
|
+
// message = new PeernetMessage(Buffer.from(message.data.data))
|
|
2544
|
+
// const proto = protoFor(message.decoded.data)
|
|
2545
|
+
// this._protoHandler({id, proto}, peer)
|
|
2546
|
+
// })
|
|
2547
|
+
});
|
|
2548
|
+
/**
|
|
2549
|
+
* converts data -> message -> proto
|
|
2550
|
+
* @see DataHandler
|
|
2551
|
+
*/
|
|
2552
|
+
pubsub.subscribe('peer:data', dataHandler);
|
|
2553
|
+
const importee = await import('./client-ed00c639.js');
|
|
2554
|
+
/**
|
|
2555
|
+
* @access public
|
|
2556
|
+
* @type {PeernetClient}
|
|
2557
|
+
*/
|
|
2558
|
+
this.client = new importee.default(this.id, this.networkVersion, this.stars);
|
|
2559
|
+
if (globalThis.navigator) {
|
|
2560
|
+
globalThis.addEventListener('beforeunload', async () => this.client.close());
|
|
2561
|
+
}
|
|
2562
|
+
else {
|
|
2563
|
+
process.on('SIGTERM', async () => {
|
|
2564
|
+
process.stdin.resume();
|
|
2565
|
+
await this.client.close();
|
|
2566
|
+
process.exit();
|
|
2567
|
+
});
|
|
2568
|
+
}
|
|
2569
|
+
return this;
|
|
2533
2570
|
}
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
addRequestHandler(name, method) {
|
|
2538
|
-
this.requestProtos[name] = method;
|
|
2539
|
-
}
|
|
2540
|
-
|
|
2541
|
-
sendMessage(peer, id, data) {
|
|
2542
|
-
if (peer.readyState === 'open') {
|
|
2543
|
-
peer.send(data, id);
|
|
2544
|
-
this.bw.up += data.length;
|
|
2545
|
-
} else if (peer.readyState === 'closed') ;
|
|
2546
|
-
|
|
2547
|
-
}
|
|
2548
|
-
|
|
2549
|
-
async handleDHT(peer, id, proto) {
|
|
2550
|
-
let { hash, store } = proto.decoded;
|
|
2551
|
-
let has;
|
|
2552
|
-
|
|
2553
|
-
if (store) {
|
|
2554
|
-
store = globalThis[`${store}Store`];
|
|
2555
|
-
has = store.private ? false : await store.has(hash);
|
|
2556
|
-
} else {
|
|
2557
|
-
has = await this.has(hash);
|
|
2571
|
+
addRequestHandler(name, method) {
|
|
2572
|
+
this.requestProtos[name] = method;
|
|
2558
2573
|
}
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2574
|
+
sendMessage(peer, id, data) {
|
|
2575
|
+
if (peer.readyState === 'open') {
|
|
2576
|
+
peer.send(data, id);
|
|
2577
|
+
this.bw.up += data.length;
|
|
2578
|
+
}
|
|
2579
|
+
else if (peer.readyState === 'closed') ;
|
|
2580
|
+
}
|
|
2581
|
+
async handleDHT(peer, id, proto) {
|
|
2582
|
+
let { hash, store } = proto.decoded;
|
|
2583
|
+
let has;
|
|
2584
|
+
if (store) {
|
|
2585
|
+
store = globalThis[`${store}Store`];
|
|
2586
|
+
has = store.private ? false : await store.has(hash);
|
|
2587
|
+
}
|
|
2588
|
+
else {
|
|
2589
|
+
has = await this.has(hash);
|
|
2590
|
+
}
|
|
2591
|
+
const data = await new globalThis.peernet.protos['peernet-dht-response']({ hash, has });
|
|
2577
2592
|
const node = await this.prepareMessage(data);
|
|
2578
2593
|
this.sendMessage(peer, id, node.encoded);
|
|
2579
|
-
}
|
|
2580
2594
|
}
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2595
|
+
async handleData(peer, id, proto) {
|
|
2596
|
+
let { hash, store } = proto.decoded;
|
|
2597
|
+
let data;
|
|
2598
|
+
store = globalThis[`${store}Store`] || await this.whichStore([...this.stores], hash);
|
|
2599
|
+
if (store && !store.private) {
|
|
2600
|
+
data = await store.get(hash);
|
|
2601
|
+
if (data) {
|
|
2602
|
+
data = await new globalThis.peernet.protos['peernet-data-response']({ hash, data });
|
|
2603
|
+
const node = await this.prepareMessage(data);
|
|
2604
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2589
2607
|
}
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
*/
|
|
2598
|
-
async _protoHandler(message, peer, from) {
|
|
2599
|
-
const {id, proto} = message;
|
|
2600
|
-
this.bw.down += proto.encoded.length;
|
|
2601
|
-
switch(proto.name) {
|
|
2602
|
-
case 'peernet-dht': {
|
|
2603
|
-
this.handleDHT(peer, id, proto);
|
|
2604
|
-
break
|
|
2605
|
-
}
|
|
2606
|
-
case 'peenet-data': {
|
|
2607
|
-
this.handleData(peer, id, proto);
|
|
2608
|
-
break
|
|
2609
|
-
}
|
|
2610
|
-
case 'peernet-request': {
|
|
2611
|
-
this.handleRequest(peer, id, proto);
|
|
2612
|
-
}
|
|
2613
|
-
|
|
2614
|
-
case 'peernet-ps': {
|
|
2615
|
-
if (peer.peerId !== this.id) globalSub.publish(proto.decoded.topic, proto.decoded.data);
|
|
2616
|
-
}
|
|
2608
|
+
async handleRequest(peer, id, proto) {
|
|
2609
|
+
const method = this.requestProtos[proto.decoded.request];
|
|
2610
|
+
if (method) {
|
|
2611
|
+
const data = await method();
|
|
2612
|
+
const node = await this.prepareMessage(data);
|
|
2613
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2614
|
+
}
|
|
2617
2615
|
}
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
2645
|
-
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
2646
|
-
port: peer.connection.remotePort || peer.connection.localPort,
|
|
2647
|
-
id: peer.peerId,
|
|
2648
|
-
};
|
|
2649
|
-
|
|
2650
|
-
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
2651
|
-
};
|
|
2652
|
-
let walks = [];
|
|
2653
|
-
for (const peer of this.connections) {
|
|
2654
|
-
if (peer.peerId !== this.id) {
|
|
2655
|
-
walks.push(walk(peer));
|
|
2656
|
-
}
|
|
2616
|
+
/**
|
|
2617
|
+
* @private
|
|
2618
|
+
*
|
|
2619
|
+
* @param {Buffer} message - peernet message
|
|
2620
|
+
* @param {PeernetPeer} peer - peernet peer
|
|
2621
|
+
*/
|
|
2622
|
+
async _protoHandler(message, peer, from) {
|
|
2623
|
+
const { id, proto } = message;
|
|
2624
|
+
this.bw.down += proto.encoded.length;
|
|
2625
|
+
switch (proto.name) {
|
|
2626
|
+
case 'peernet-dht': {
|
|
2627
|
+
this.handleDHT(peer, id, proto);
|
|
2628
|
+
break;
|
|
2629
|
+
}
|
|
2630
|
+
case 'peenet-data': {
|
|
2631
|
+
this.handleData(peer, id, proto);
|
|
2632
|
+
break;
|
|
2633
|
+
}
|
|
2634
|
+
case 'peernet-request': {
|
|
2635
|
+
this.handleRequest(peer, id, proto);
|
|
2636
|
+
}
|
|
2637
|
+
case 'peernet-ps': {
|
|
2638
|
+
if (peer.peerId !== this.id)
|
|
2639
|
+
globalSub.publish(proto.decoded.topic, proto.decoded.data);
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2657
2642
|
}
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
return
|
|
2697
|
-
},
|
|
2698
|
-
has: async (hash) => await blockStore.has(hash, 'block'),
|
|
2643
|
+
/**
|
|
2644
|
+
* performs a walk and resolves first encounter
|
|
2645
|
+
*
|
|
2646
|
+
* @param {String} hash
|
|
2647
|
+
*/
|
|
2648
|
+
async walk(hash) {
|
|
2649
|
+
if (!hash)
|
|
2650
|
+
throw new Error('hash expected, received undefined');
|
|
2651
|
+
const data = await new globalThis.peernet.protos['peernet-dht']({ hash });
|
|
2652
|
+
this.client.id;
|
|
2653
|
+
const walk = async (peer) => {
|
|
2654
|
+
const node = await this.prepareMessage(data);
|
|
2655
|
+
let result = await peer.request(node.encoded);
|
|
2656
|
+
result = new Uint8Array(Object.values(result));
|
|
2657
|
+
const proto = await protoFor(result);
|
|
2658
|
+
if (proto.name !== 'peernet-dht-response')
|
|
2659
|
+
throw dhtError(proto.name);
|
|
2660
|
+
// TODO: give ip and port (just used for location)
|
|
2661
|
+
if (!peer.connection.remoteAddress || !peer.connection.localAddress) {
|
|
2662
|
+
peer.connection.remoteFamily = 'ipv4';
|
|
2663
|
+
peer.connection.remoteAddress = '127.0.0.1';
|
|
2664
|
+
peer.connection.remotePort = '0000';
|
|
2665
|
+
}
|
|
2666
|
+
const peerInfo = {
|
|
2667
|
+
family: peer.connection.remoteFamily || peer.connection.localFamily,
|
|
2668
|
+
address: peer.connection.remoteAddress || peer.connection.localAddress,
|
|
2669
|
+
port: peer.connection.remotePort || peer.connection.localPort,
|
|
2670
|
+
id: peer.peerId,
|
|
2671
|
+
};
|
|
2672
|
+
if (proto.decoded.has)
|
|
2673
|
+
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
2674
|
+
};
|
|
2675
|
+
let walks = [];
|
|
2676
|
+
for (const peer of this.connections) {
|
|
2677
|
+
if (peer.peerId !== this.id) {
|
|
2678
|
+
walks.push(walk(peer));
|
|
2679
|
+
}
|
|
2680
|
+
}
|
|
2681
|
+
return Promise.all(walks);
|
|
2699
2682
|
}
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2683
|
+
/**
|
|
2684
|
+
* Override DHT behavior, try's finding the content three times
|
|
2685
|
+
*
|
|
2686
|
+
* @param {String} hash
|
|
2687
|
+
*/
|
|
2688
|
+
async providersFor(hash) {
|
|
2689
|
+
let providers = await this.dht.providersFor(hash);
|
|
2690
|
+
// walk the network to find a provider
|
|
2691
|
+
if (!providers || providers.length === 0) {
|
|
2692
|
+
await this.walk(hash);
|
|
2693
|
+
providers = await this.dht.providersFor(hash);
|
|
2694
|
+
// second walk the network to find a provider
|
|
2695
|
+
if (!providers || providers.length === 0) {
|
|
2696
|
+
await this.walk(hash);
|
|
2697
|
+
providers = await this.dht.providersFor(hash);
|
|
2698
|
+
}
|
|
2699
|
+
// last walk
|
|
2700
|
+
if (!providers || providers.length === 0) {
|
|
2701
|
+
await this.walk(hash);
|
|
2702
|
+
providers = await this.dht.providersFor(hash);
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
// undefined if no providers given
|
|
2706
|
+
return providers;
|
|
2714
2707
|
}
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
});
|
|
2731
|
-
|
|
2732
|
-
let data = await new globalThis.peernet.protos['peernet-data']({hash, store: store?.name || store});
|
|
2733
|
-
|
|
2734
|
-
const node = await this.prepareMessage(data);
|
|
2735
|
-
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2736
|
-
else {
|
|
2737
|
-
closest = this.connections.filter((peer) => {
|
|
2738
|
-
if (peer.peerId === id) return peer
|
|
2739
|
-
});
|
|
2740
|
-
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2741
|
-
}
|
|
2742
|
-
data = new Uint8Array(Object.values(data));
|
|
2743
|
-
const proto = await protoFor(data);
|
|
2744
|
-
// TODO: store data automaticly or not
|
|
2745
|
-
return BufferToUint8Array(proto.decoded.data)
|
|
2746
|
-
|
|
2747
|
-
// this.put(hash, proto.decoded.data)
|
|
2708
|
+
get block() {
|
|
2709
|
+
return {
|
|
2710
|
+
get: async (hash) => {
|
|
2711
|
+
const data = await blockStore.has(hash);
|
|
2712
|
+
if (data)
|
|
2713
|
+
return blockStore.get(hash);
|
|
2714
|
+
return this.requestData(hash, 'block');
|
|
2715
|
+
},
|
|
2716
|
+
put: async (hash, data) => {
|
|
2717
|
+
if (await blockStore.has(hash))
|
|
2718
|
+
return;
|
|
2719
|
+
return await blockStore.put(hash, data);
|
|
2720
|
+
},
|
|
2721
|
+
has: async (hash) => await blockStore.has(hash, 'block'),
|
|
2722
|
+
};
|
|
2748
2723
|
}
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
return this.requestData(hash, 'message')
|
|
2765
|
-
},
|
|
2766
|
-
/**
|
|
2767
|
-
* put message content
|
|
2768
|
-
*
|
|
2769
|
-
* @param {String} hash
|
|
2770
|
-
* @param {Buffer} message
|
|
2771
|
-
*/
|
|
2772
|
-
put: async (hash, message) => await messageStore.put(hash, message),
|
|
2773
|
-
/**
|
|
2774
|
-
* @param {String} hash
|
|
2775
|
-
* @return {Boolean}
|
|
2776
|
-
*/
|
|
2777
|
-
has: async (hash) => await messageStore.has(hash),
|
|
2724
|
+
get transaction() {
|
|
2725
|
+
return {
|
|
2726
|
+
get: async (hash) => {
|
|
2727
|
+
const data = await transactionStore.has(hash);
|
|
2728
|
+
if (data)
|
|
2729
|
+
return await transactionStore.get(hash);
|
|
2730
|
+
return this.requestData(hash, 'transaction');
|
|
2731
|
+
},
|
|
2732
|
+
put: async (hash, data) => {
|
|
2733
|
+
if (await transactionStore.has(hash))
|
|
2734
|
+
return;
|
|
2735
|
+
return await transactionStore.put(hash, data);
|
|
2736
|
+
},
|
|
2737
|
+
has: async (hash) => await transactionStore.has(hash),
|
|
2738
|
+
};
|
|
2778
2739
|
}
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2740
|
+
async requestData(hash, store) {
|
|
2741
|
+
const providers = await this.providersFor(hash);
|
|
2742
|
+
if (!providers || providers.size === 0)
|
|
2743
|
+
throw nothingFoundError(hash);
|
|
2744
|
+
debug(`found ${providers.size} provider(s) for ${hash}`);
|
|
2745
|
+
// get closest peer on earth
|
|
2746
|
+
const closestPeer = await this.dht.closestPeer(providers);
|
|
2747
|
+
// get peer instance by id
|
|
2748
|
+
if (!closestPeer || !closestPeer.id)
|
|
2749
|
+
return this.requestData(hash, store?.name || store);
|
|
2750
|
+
const id = closestPeer.id;
|
|
2751
|
+
if (this.connections) {
|
|
2752
|
+
let closest = this.connections.filter((peer) => {
|
|
2753
|
+
if (peer.peerId === id)
|
|
2754
|
+
return peer;
|
|
2755
|
+
});
|
|
2756
|
+
let data = await new globalThis.peernet.protos['peernet-data']({ hash, store: store?.name || store });
|
|
2757
|
+
const node = await this.prepareMessage(data);
|
|
2758
|
+
if (closest[0])
|
|
2759
|
+
data = await closest[0].request(node.encoded);
|
|
2760
|
+
else {
|
|
2761
|
+
closest = this.connections.filter((peer) => {
|
|
2762
|
+
if (peer.peerId === id)
|
|
2763
|
+
return peer;
|
|
2764
|
+
});
|
|
2765
|
+
if (closest[0])
|
|
2766
|
+
data = await closest[0].request(node.encoded);
|
|
2767
|
+
}
|
|
2768
|
+
data = new Uint8Array(Object.values(data));
|
|
2769
|
+
const proto = await protoFor(data);
|
|
2770
|
+
// TODO: store data automaticly or not
|
|
2771
|
+
return BufferToUint8Array(proto.decoded.data);
|
|
2772
|
+
// this.put(hash, proto.decoded.data)
|
|
2773
|
+
}
|
|
2774
|
+
return;
|
|
2806
2775
|
}
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2776
|
+
get message() {
|
|
2777
|
+
return {
|
|
2778
|
+
/**
|
|
2779
|
+
* Get content for given message hash
|
|
2780
|
+
*
|
|
2781
|
+
* @param {String} hash
|
|
2782
|
+
*/
|
|
2783
|
+
get: async (hash) => {
|
|
2784
|
+
debug(`get message ${hash}`);
|
|
2785
|
+
const message = await messageStore.has(hash);
|
|
2786
|
+
if (message)
|
|
2787
|
+
return await messageStore.get(hash);
|
|
2788
|
+
return this.requestData(hash, 'message');
|
|
2789
|
+
},
|
|
2790
|
+
/**
|
|
2791
|
+
* put message content
|
|
2792
|
+
*
|
|
2793
|
+
* @param {String} hash
|
|
2794
|
+
* @param {Buffer} message
|
|
2795
|
+
*/
|
|
2796
|
+
put: async (hash, message) => await messageStore.put(hash, message),
|
|
2797
|
+
/**
|
|
2798
|
+
* @param {String} hash
|
|
2799
|
+
* @return {Boolean}
|
|
2800
|
+
*/
|
|
2801
|
+
has: async (hash) => await messageStore.has(hash),
|
|
2802
|
+
};
|
|
2834
2803
|
}
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
for (const {path, hash} of node.decoded.links) {
|
|
2863
|
-
paths.push({path, hash});
|
|
2864
|
-
}
|
|
2865
|
-
if (options?.pin) await dataStore.put(hash, node.encoded);
|
|
2866
|
-
return paths
|
|
2867
|
-
}
|
|
2868
|
-
|
|
2869
|
-
async cat(hash, options) {
|
|
2870
|
-
let data;
|
|
2871
|
-
const has = await dataStore.has(hash);
|
|
2872
|
-
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
2873
|
-
const node = await new peernet.protos['peernet-file'](data);
|
|
2874
|
-
|
|
2875
|
-
if (node.decoded?.links.length > 0) throw new Error(`${hash} is a directory`)
|
|
2876
|
-
if (options?.pin) await dataStore.put(hash, node.encoded);
|
|
2877
|
-
return node.decoded.content
|
|
2878
|
-
}
|
|
2879
|
-
|
|
2880
|
-
/**
|
|
2881
|
-
* goes trough given stores and tries to find data for given hash
|
|
2882
|
-
* @param {Array} stores
|
|
2883
|
-
* @param {string} hash
|
|
2884
|
-
*/
|
|
2885
|
-
async whichStore(stores, hash) {
|
|
2886
|
-
let store = stores.pop();
|
|
2887
|
-
store = globalThis[`${store}Store`];
|
|
2888
|
-
if (store) {
|
|
2889
|
-
const has = await store.has(hash);
|
|
2890
|
-
if (has) return store
|
|
2891
|
-
if (stores.length > 0) return this.whichStore(stores, hash)
|
|
2892
|
-
} else return
|
|
2893
|
-
}
|
|
2894
|
-
|
|
2895
|
-
/**
|
|
2896
|
-
* Get content for given hash
|
|
2897
|
-
*
|
|
2898
|
-
* @param {String} hash - the hash of the wanted data
|
|
2899
|
-
* @param {String} store - storeName to access
|
|
2900
|
-
*/
|
|
2901
|
-
async get(hash, store) {
|
|
2902
|
-
debug(`get ${hash}`);
|
|
2903
|
-
let data;
|
|
2904
|
-
if (store) store = globalThis[`${store}Store`];
|
|
2905
|
-
if (!store) store = await this.whichStore([...this.stores], hash);
|
|
2906
|
-
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2907
|
-
if (data) return data
|
|
2908
|
-
|
|
2909
|
-
return this.requestData(hash, store?.name || store)
|
|
2910
|
-
}
|
|
2911
|
-
|
|
2912
|
-
/**
|
|
2913
|
-
* put content
|
|
2914
|
-
*
|
|
2915
|
-
* @param {String} hash
|
|
2916
|
-
* @param {Buffer} data
|
|
2917
|
-
* @param {String} store - storeName to access
|
|
2918
|
-
*/
|
|
2919
|
-
async put(hash, data, store = 'data') {
|
|
2920
|
-
store = globalThis[`${store}Store`];
|
|
2921
|
-
return store.put(hash, data)
|
|
2922
|
-
}
|
|
2923
|
-
|
|
2924
|
-
/**
|
|
2925
|
-
* @param {String} hash
|
|
2926
|
-
* @return {Boolean}
|
|
2927
|
-
*/
|
|
2928
|
-
async has(hash) {
|
|
2929
|
-
const store = await this.whichStore([...this.stores], hash);
|
|
2930
|
-
if (store) {
|
|
2931
|
-
return store.private ? false : true
|
|
2804
|
+
get data() {
|
|
2805
|
+
return {
|
|
2806
|
+
/**
|
|
2807
|
+
* Get content for given data hash
|
|
2808
|
+
*
|
|
2809
|
+
* @param {String} hash
|
|
2810
|
+
*/
|
|
2811
|
+
get: async (hash) => {
|
|
2812
|
+
debug(`get data ${hash}`);
|
|
2813
|
+
const data = await dataStore.has(hash);
|
|
2814
|
+
if (data)
|
|
2815
|
+
return await dataStore.get(hash);
|
|
2816
|
+
return this.requestData(hash, 'data');
|
|
2817
|
+
},
|
|
2818
|
+
/**
|
|
2819
|
+
* put data content
|
|
2820
|
+
*
|
|
2821
|
+
* @param {String} hash
|
|
2822
|
+
* @param {Buffer} data
|
|
2823
|
+
*/
|
|
2824
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
2825
|
+
/**
|
|
2826
|
+
* @param {String} hash
|
|
2827
|
+
* @return {Boolean}
|
|
2828
|
+
*/
|
|
2829
|
+
has: async (hash) => await dataStore.has(hash),
|
|
2830
|
+
};
|
|
2932
2831
|
}
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2832
|
+
get folder() {
|
|
2833
|
+
return {
|
|
2834
|
+
/**
|
|
2835
|
+
* Get content for given data hash
|
|
2836
|
+
*
|
|
2837
|
+
* @param {String} hash
|
|
2838
|
+
*/
|
|
2839
|
+
get: async (hash) => {
|
|
2840
|
+
debug(`get data ${hash}`);
|
|
2841
|
+
const data = await dataStore.has(hash);
|
|
2842
|
+
if (data)
|
|
2843
|
+
return await dataStore.get(hash);
|
|
2844
|
+
return this.requestData(hash, 'data');
|
|
2845
|
+
},
|
|
2846
|
+
/**
|
|
2847
|
+
* put data content
|
|
2848
|
+
*
|
|
2849
|
+
* @param {String} hash
|
|
2850
|
+
* @param {Buffer} data
|
|
2851
|
+
*/
|
|
2852
|
+
put: async (hash, data) => await dataStore.put(hash, data),
|
|
2853
|
+
/**
|
|
2854
|
+
* @param {String} hash
|
|
2855
|
+
* @return {Boolean}
|
|
2856
|
+
*/
|
|
2857
|
+
has: async (hash) => await dataStore.has(hash),
|
|
2858
|
+
};
|
|
2859
|
+
}
|
|
2860
|
+
async addFolder(files) {
|
|
2861
|
+
const links = [];
|
|
2862
|
+
for (const file of files) {
|
|
2863
|
+
const fileNode = await new globalThis.peernet.protos['peernet-file'](file);
|
|
2864
|
+
const hash = await fileNode.hash;
|
|
2865
|
+
await dataStore.put(hash, fileNode.encoded);
|
|
2866
|
+
links.push({ hash, path: file.path });
|
|
2867
|
+
}
|
|
2868
|
+
const node = await new globalThis.peernet.protos['peernet-file']({ path: '/', links });
|
|
2869
|
+
const hash = await node.hash;
|
|
2870
|
+
await dataStore.put(hash, node.encoded);
|
|
2871
|
+
return hash;
|
|
2872
|
+
}
|
|
2873
|
+
async ls(hash, options) {
|
|
2874
|
+
let data;
|
|
2875
|
+
const has = await dataStore.has(hash);
|
|
2876
|
+
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
2877
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
2878
|
+
await node.decode();
|
|
2879
|
+
console.log(data);
|
|
2880
|
+
const paths = [];
|
|
2881
|
+
if (node.decoded?.links.length === 0)
|
|
2882
|
+
throw new Error(`${hash} is a file`);
|
|
2883
|
+
for (const { path, hash } of node.decoded.links) {
|
|
2884
|
+
paths.push({ path, hash });
|
|
2885
|
+
}
|
|
2886
|
+
if (options?.pin)
|
|
2887
|
+
await dataStore.put(hash, node.encoded);
|
|
2888
|
+
return paths;
|
|
2889
|
+
}
|
|
2890
|
+
async cat(hash, options) {
|
|
2891
|
+
let data;
|
|
2892
|
+
const has = await dataStore.has(hash);
|
|
2893
|
+
data = has ? await dataStore.get(hash) : await this.requestData(hash, 'data');
|
|
2894
|
+
const node = await new peernet.protos['peernet-file'](data);
|
|
2895
|
+
if (node.decoded?.links.length > 0)
|
|
2896
|
+
throw new Error(`${hash} is a directory`);
|
|
2897
|
+
if (options?.pin)
|
|
2898
|
+
await dataStore.put(hash, node.encoded);
|
|
2899
|
+
return node.decoded.content;
|
|
2900
|
+
}
|
|
2901
|
+
/**
|
|
2902
|
+
* goes trough given stores and tries to find data for given hash
|
|
2903
|
+
* @param {Array} stores
|
|
2904
|
+
* @param {string} hash
|
|
2905
|
+
*/
|
|
2906
|
+
async whichStore(stores, hash) {
|
|
2907
|
+
let store = stores.pop();
|
|
2908
|
+
store = globalThis[`${store}Store`];
|
|
2909
|
+
if (store) {
|
|
2910
|
+
const has = await store.has(hash);
|
|
2911
|
+
if (has)
|
|
2912
|
+
return store;
|
|
2913
|
+
if (stores.length > 0)
|
|
2914
|
+
return this.whichStore(stores, hash);
|
|
2915
|
+
}
|
|
2916
|
+
else
|
|
2917
|
+
return;
|
|
2918
|
+
}
|
|
2919
|
+
/**
|
|
2920
|
+
* Get content for given hash
|
|
2921
|
+
*
|
|
2922
|
+
* @param {String} hash - the hash of the wanted data
|
|
2923
|
+
* @param {String} store - storeName to access
|
|
2924
|
+
*/
|
|
2925
|
+
async get(hash, store) {
|
|
2926
|
+
debug(`get ${hash}`);
|
|
2927
|
+
let data;
|
|
2928
|
+
if (store)
|
|
2929
|
+
store = globalThis[`${store}Store`];
|
|
2930
|
+
if (!store)
|
|
2931
|
+
store = await this.whichStore([...this.stores], hash);
|
|
2932
|
+
if (store && await store.has(hash))
|
|
2933
|
+
data = await store.get(hash);
|
|
2934
|
+
if (data)
|
|
2935
|
+
return data;
|
|
2936
|
+
return this.requestData(hash, store?.name || store);
|
|
2937
|
+
}
|
|
2938
|
+
/**
|
|
2939
|
+
* put content
|
|
2940
|
+
*
|
|
2941
|
+
* @param {String} hash
|
|
2942
|
+
* @param {Buffer} data
|
|
2943
|
+
* @param {String} store - storeName to access
|
|
2944
|
+
*/
|
|
2945
|
+
async put(hash, data, store = 'data') {
|
|
2946
|
+
store = globalThis[`${store}Store`];
|
|
2947
|
+
return store.put(hash, data);
|
|
2948
|
+
}
|
|
2949
|
+
/**
|
|
2950
|
+
* @param {String} hash
|
|
2951
|
+
* @return {Boolean}
|
|
2952
|
+
*/
|
|
2953
|
+
async has(hash) {
|
|
2954
|
+
const store = await this.whichStore([...this.stores], hash);
|
|
2955
|
+
if (store) {
|
|
2956
|
+
return store.private ? false : true;
|
|
2957
|
+
}
|
|
2958
|
+
return false;
|
|
2959
|
+
}
|
|
2960
|
+
/**
|
|
2961
|
+
*
|
|
2962
|
+
* @param {String} topic
|
|
2963
|
+
* @param {String|Object|Array|Boolean|Buffer} data
|
|
2964
|
+
*/
|
|
2965
|
+
async publish(topic, data) {
|
|
2966
|
+
// globalSub.publish(topic, data)
|
|
2967
|
+
const id = Math.random().toString(36).slice(-12);
|
|
2968
|
+
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
2969
|
+
for (const peer of this.connections) {
|
|
2970
|
+
if (peer.peerId !== this.peerId) {
|
|
2971
|
+
const node = await this.prepareMessage(data);
|
|
2972
|
+
this.sendMessage(peer, id, node.encoded);
|
|
2973
|
+
}
|
|
2974
|
+
// TODO: if peer subscribed
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
createHash(data, name) {
|
|
2978
|
+
return new CodeHash(data, { name });
|
|
2979
|
+
}
|
|
2980
|
+
/**
|
|
2981
|
+
*
|
|
2982
|
+
* @param {String} topic
|
|
2983
|
+
* @param {Method} cb
|
|
2984
|
+
*/
|
|
2985
|
+
async subscribe(topic, callback) {
|
|
2986
|
+
// TODO: if peer subscribed
|
|
2987
|
+
globalSub.subscribe(topic, callback);
|
|
2988
|
+
}
|
|
2989
|
+
async removePeer(peer) {
|
|
2990
|
+
return this.client.removePeer(peer);
|
|
2991
|
+
}
|
|
2992
|
+
get Buffer() {
|
|
2993
|
+
return Buffer;
|
|
2951
2994
|
}
|
|
2952
|
-
}
|
|
2953
|
-
|
|
2954
|
-
createHash(data, name) {
|
|
2955
|
-
return new CodeHash(data, {name})
|
|
2956
|
-
}
|
|
2957
|
-
|
|
2958
|
-
/**
|
|
2959
|
-
*
|
|
2960
|
-
* @param {String} topic
|
|
2961
|
-
* @param {Method} cb
|
|
2962
|
-
*/
|
|
2963
|
-
async subscribe(topic, callback) {
|
|
2964
|
-
// TODO: if peer subscribed
|
|
2965
|
-
globalSub.subscribe(topic, callback);
|
|
2966
|
-
}
|
|
2967
|
-
|
|
2968
|
-
async removePeer(peer) {
|
|
2969
|
-
return this.client.removePeer(peer)
|
|
2970
|
-
}
|
|
2971
|
-
|
|
2972
|
-
get Buffer() {
|
|
2973
|
-
return Buffer
|
|
2974
|
-
}
|
|
2975
2995
|
}
|
|
2976
2996
|
globalThis.Peernet = Peernet;
|
|
2977
2997
|
|