@witnet/sdk 1.2.7 → 3.0.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/.env_witnet +18 -18
- package/LICENSE +21 -21
- package/README.md +116 -116
- package/dist/package.json +14 -3
- package/dist/src/bin/helpers.d.ts +1 -0
- package/dist/src/bin/helpers.d.ts.map +1 -1
- package/dist/src/bin/helpers.js +31 -54
- package/dist/src/lib/crypto/account.d.ts.map +1 -1
- package/dist/src/lib/crypto/account.js +3 -9
- package/dist/src/lib/crypto/coinbase.d.ts.map +1 -1
- package/dist/src/lib/crypto/coinbase.js +2 -2
- package/dist/src/lib/crypto/interfaces.d.ts.map +1 -1
- package/dist/src/lib/crypto/interfaces.js +1 -1
- package/dist/src/lib/crypto/payloads/DataRequestPayload.d.ts +4 -6
- package/dist/src/lib/crypto/payloads/DataRequestPayload.d.ts.map +1 -1
- package/dist/src/lib/crypto/payloads/DataRequestPayload.js +20 -71
- package/dist/src/lib/crypto/payloads/StakePayload.d.ts.map +1 -1
- package/dist/src/lib/crypto/payloads/StakePayload.js +4 -9
- package/dist/src/lib/crypto/payloads/UnstakePayload.d.ts.map +1 -1
- package/dist/src/lib/crypto/payloads/UnstakePayload.js +4 -7
- package/dist/src/lib/crypto/payloads/ValueTransferPayload.d.ts.map +1 -1
- package/dist/src/lib/crypto/payloads/ValueTransferPayload.js +4 -9
- package/dist/src/lib/crypto/payloads.d.ts.map +1 -1
- package/dist/src/lib/crypto/payloads.js +6 -13
- package/dist/src/lib/crypto/signer.d.ts.map +1 -1
- package/dist/src/lib/crypto/signer.js +2 -2
- package/dist/src/lib/crypto/transmitters/DataRequests.d.ts.map +1 -1
- package/dist/src/lib/crypto/transmitters/DataRequests.js +2 -2
- package/dist/src/lib/crypto/transmitters/StakeDeposits.d.ts.map +1 -1
- package/dist/src/lib/crypto/transmitters/StakeDeposits.js +2 -2
- package/dist/src/lib/crypto/transmitters/StakeWithdrawals.d.ts.map +1 -1
- package/dist/src/lib/crypto/transmitters/StakeWithdrawals.js +2 -2
- package/dist/src/lib/crypto/transmitters/ValueTransfers.d.ts.map +1 -1
- package/dist/src/lib/crypto/transmitters/ValueTransfers.js +2 -2
- package/dist/src/lib/crypto/transmitters.d.ts.map +1 -1
- package/dist/src/lib/crypto/transmitters.js +22 -31
- package/dist/src/lib/crypto/types.d.ts.map +1 -1
- package/dist/src/lib/crypto/types.js +6 -13
- package/dist/src/lib/crypto/utils.d.ts.map +1 -1
- package/dist/src/lib/crypto/utils.js +5 -9
- package/dist/src/lib/crypto/wallet.d.ts.map +1 -1
- package/dist/src/lib/crypto/wallet.js +9 -23
- package/dist/src/lib/radon/ccdr/eth.d.ts.map +1 -1
- package/dist/src/lib/radon/ccdr/eth.js +12 -32
- package/dist/src/lib/radon/ccdr/wit.d.ts.map +1 -1
- package/dist/src/lib/radon/ccdr/wit.js +3 -6
- package/dist/src/lib/radon/index.d.ts.map +1 -1
- package/dist/src/lib/radon/index.js +18 -41
- package/dist/src/lib/radon/reducers.js +2 -2
- package/dist/src/lib/radon/types.d.ts.map +1 -1
- package/dist/src/lib/radon/types.js +6 -32
- package/dist/src/lib/radon/utils.d.ts +1 -1
- package/dist/src/lib/radon/utils.d.ts.map +1 -1
- package/dist/src/lib/radon/utils.js +8 -9
- package/dist/src/lib/rest/kermit.d.ts.map +1 -1
- package/dist/src/lib/rest/kermit.js +1 -1
- package/dist/src/lib/rpc/nodes.d.ts.map +1 -1
- package/dist/src/lib/rpc/nodes.js +8 -28
- package/dist/src/lib/rpc/provider.d.ts.map +1 -1
- package/dist/src/lib/rpc/provider.js +16 -38
- package/dist/src/lib/rpc/types.d.ts.map +1 -1
- package/dist/src/lib/rpc/types.js +1 -1
- package/dist/src/lib/utils.d.ts.map +1 -1
- package/dist/src/lib/utils.js +2 -5
- package/dist/witnet/assets/index.cjs +1 -1
- package/dist/witnet/assets/modals/index.cjs +1 -1
- package/dist/witnet/assets/modals/web3/eth.cjs +2 -2
- package/dist/witnet/assets/modals/web3/ipfs.cjs +2 -2
- package/dist/witnet/assets/modals/web3/wit.cjs +4 -11
- package/dist/witnet/assets/requests.cjs +3 -11
- package/package.json +7 -3
- package/src/bin/bots/watcher.cjs +27 -93
- package/src/bin/cli/inspect.js +30 -119
- package/src/bin/cli/network.js +46 -202
- package/src/bin/cli/nodes.js +12 -58
- package/src/bin/cli/radon.js +161 -396
- package/src/bin/cli/wallet.js +69 -241
- package/src/bin/helpers.js +66 -162
- package/src/bin/index.js +23 -52
- package/witnet/assets/_index.cjs +5 -5
- package/witnet/assets/_requests.cjs +25 -25
- package/witnet/assets/_sources.cjs +36 -36
- package/witnet/assets/_templates.cjs +36 -36
- package/witnet/assets/index.cjs +4 -4
- package/witnet/assets/modals/index.cjs +7 -7
- package/witnet/assets/modals/web3/eth.cjs +22 -27
- package/witnet/assets/modals/web3/ipfs.cjs +17 -22
- package/witnet/assets/modals/web3/wit.cjs +16 -28
- package/witnet/assets/requests.cjs +41 -49
package/src/bin/cli/wallet.js
CHANGED
|
@@ -45,8 +45,7 @@ const options = {
|
|
|
45
45
|
/// CLI SUBMODULE CONSTANTS ===========================================================================================
|
|
46
46
|
|
|
47
47
|
export const envars = {
|
|
48
|
-
WITNET_SDK_PROVIDER_URL:
|
|
49
|
-
"=> Wit/Oracle RPC provider(s) to connect to, if no otherwise specified.",
|
|
48
|
+
WITNET_SDK_PROVIDER_URL: "=> Wit/Oracle RPC provider(s) to connect to, if no otherwise specified.",
|
|
50
49
|
WITNET_SDK_WALLET_MASTER_KEY:
|
|
51
50
|
"=> Wallet's master key in XPRV format, as exported from either a node, Sheikah or myWitWallet.",
|
|
52
51
|
};
|
|
@@ -271,17 +270,12 @@ async function signMessage(options = {}, [...words]) {
|
|
|
271
270
|
const text = words.join(" ");
|
|
272
271
|
let ledger;
|
|
273
272
|
if (options?.signer) {
|
|
274
|
-
ledger =
|
|
275
|
-
options.signer === wallet.coinbase.pkh
|
|
276
|
-
? wallet.coinbase
|
|
277
|
-
: wallet.getAccount(options.signer);
|
|
273
|
+
ledger = options.signer === wallet.coinbase.pkh ? wallet.coinbase : wallet.getAccount(options.signer);
|
|
278
274
|
} else {
|
|
279
275
|
ledger = wallet;
|
|
280
276
|
}
|
|
281
277
|
if (!ledger) {
|
|
282
|
-
throw Error(
|
|
283
|
-
`no private key available for signer address ${options?.signer}`,
|
|
284
|
-
);
|
|
278
|
+
throw Error(`no private key available for signer address ${options?.signer}`);
|
|
285
279
|
}
|
|
286
280
|
console.info(ledger.getSigner().signMessage(text));
|
|
287
281
|
}
|
|
@@ -307,9 +301,7 @@ async function verifyMessage(options = {}, [...words]) {
|
|
|
307
301
|
console.info("-".repeat(120));
|
|
308
302
|
if (utils.ecdsaVerify(digest, publicKey, signature.slice(2))) {
|
|
309
303
|
console.info(
|
|
310
|
-
`^ Signed by ${Witnet.PublicKey.fromUint8Array(
|
|
311
|
-
utils.fromHexString(publicKey),
|
|
312
|
-
)
|
|
304
|
+
`^ Signed by ${Witnet.PublicKey.fromUint8Array(utils.fromHexString(publicKey))
|
|
313
305
|
.hash()
|
|
314
306
|
.toBech32("mainnet")}.\n^ Message is authentic.`,
|
|
315
307
|
);
|
|
@@ -346,9 +338,7 @@ async function accounts(options = {}, args = []) {
|
|
|
346
338
|
}
|
|
347
339
|
addrs.forEach((account) => {
|
|
348
340
|
qrcodes.generate(account.pkh);
|
|
349
|
-
console.info(
|
|
350
|
-
`Wallet account #${account.index + 1}: ${colors.lmagenta(account.pkh)}\n`,
|
|
351
|
-
);
|
|
341
|
+
console.info(`Wallet account #${account.index + 1}: ${colors.lmagenta(account.pkh)}\n`);
|
|
352
342
|
});
|
|
353
343
|
return;
|
|
354
344
|
}
|
|
@@ -370,12 +360,7 @@ async function accounts(options = {}, args = []) {
|
|
|
370
360
|
|
|
371
361
|
if (coinbase) {
|
|
372
362
|
const coinbaseUtxos = await wallet.coinbase.getUtxos();
|
|
373
|
-
records.push([
|
|
374
|
-
0,
|
|
375
|
-
coinbaseColor(wallet.coinbase.pkh),
|
|
376
|
-
coinbaseUtxos.length,
|
|
377
|
-
coinbaseBalance,
|
|
378
|
-
]);
|
|
363
|
+
records.push([0, coinbaseColor(wallet.coinbase.pkh), coinbaseUtxos.length, coinbaseBalance]);
|
|
379
364
|
}
|
|
380
365
|
records.push(
|
|
381
366
|
...(await Promise.all(
|
|
@@ -384,9 +369,7 @@ async function accounts(options = {}, args = []) {
|
|
|
384
369
|
const utxos = await account.getUtxos();
|
|
385
370
|
return [
|
|
386
371
|
account.index + 1,
|
|
387
|
-
balance.unlocked > 0
|
|
388
|
-
? colors.mmagenta(account.pkh)
|
|
389
|
-
: colors.magenta(account.pkh),
|
|
372
|
+
balance.unlocked > 0 ? colors.mmagenta(account.pkh) : colors.magenta(account.pkh),
|
|
390
373
|
utxos.length,
|
|
391
374
|
balance,
|
|
392
375
|
...(ethereum
|
|
@@ -419,10 +402,7 @@ async function accounts(options = {}, args = []) {
|
|
|
419
402
|
Witnet.Coins.fromNanowits(balance.unlocked).wits.toFixed(2),
|
|
420
403
|
utils.totalCoins(balance).wits.toFixed(2),
|
|
421
404
|
]
|
|
422
|
-
: [
|
|
423
|
-
count,
|
|
424
|
-
Witnet.Coins.fromNanowits(balance.unlocked).wits.toFixed(2),
|
|
425
|
-
]),
|
|
405
|
+
: [count, Witnet.Coins.fromNanowits(balance.unlocked).wits.toFixed(2)]),
|
|
426
406
|
];
|
|
427
407
|
}),
|
|
428
408
|
{
|
|
@@ -430,31 +410,15 @@ async function accounts(options = {}, args = []) {
|
|
|
430
410
|
"INDEX",
|
|
431
411
|
`:WITNET WALLET ${wallet.network.toUpperCase()} ACCOUNTS`,
|
|
432
412
|
...(ethereum
|
|
433
|
-
? [
|
|
434
|
-
`ETHEREUM ${wallet.network.toUpperCase()} WRAPPING AUTHORIZATION CODE [${ethereum}]`,
|
|
435
|
-
]
|
|
413
|
+
? [`ETHEREUM ${wallet.network.toUpperCase()} WRAPPING AUTHORIZATION CODE [${ethereum}]`]
|
|
436
414
|
: verbose
|
|
437
|
-
? [
|
|
438
|
-
"# UTXOS",
|
|
439
|
-
"Locked ($WIT)",
|
|
440
|
-
"Staked ($WIT)",
|
|
441
|
-
"Available ($WIT)",
|
|
442
|
-
"BALANCE ($WIT)",
|
|
443
|
-
]
|
|
415
|
+
? ["# UTXOS", "Locked ($WIT)", "Staked ($WIT)", "Available ($WIT)", "BALANCE ($WIT)"]
|
|
444
416
|
: ["# UTXOS", "Available ($WIT)"]),
|
|
445
417
|
],
|
|
446
418
|
humanizers: [
|
|
447
419
|
helpers.commas,
|
|
448
420
|
undefined,
|
|
449
|
-
...(ethereum
|
|
450
|
-
? [undefined]
|
|
451
|
-
: [
|
|
452
|
-
helpers.commas,
|
|
453
|
-
helpers.commas,
|
|
454
|
-
helpers.commas,
|
|
455
|
-
helpers.commas,
|
|
456
|
-
helpers.commas,
|
|
457
|
-
]),
|
|
421
|
+
...(ethereum ? [undefined] : [helpers.commas, helpers.commas, helpers.commas, helpers.commas, helpers.commas]),
|
|
458
422
|
],
|
|
459
423
|
colors: [
|
|
460
424
|
undefined,
|
|
@@ -462,21 +426,13 @@ async function accounts(options = {}, args = []) {
|
|
|
462
426
|
...(ethereum
|
|
463
427
|
? [colors.mblue, colors.gray]
|
|
464
428
|
: verbose
|
|
465
|
-
? [
|
|
466
|
-
colors.white,
|
|
467
|
-
colors.gray,
|
|
468
|
-
colors.yellow,
|
|
469
|
-
colors.myellow,
|
|
470
|
-
colors.lyellow,
|
|
471
|
-
]
|
|
429
|
+
? [colors.white, colors.gray, colors.yellow, colors.myellow, colors.lyellow]
|
|
472
430
|
: [colors.white, colors.myellow]),
|
|
473
431
|
],
|
|
474
432
|
maxColumnWidth: 132,
|
|
475
433
|
},
|
|
476
434
|
);
|
|
477
|
-
console.info(
|
|
478
|
-
`^ Available balance: ${colors.lyellow(whole_wits(unlocked, 2))}`,
|
|
479
|
-
);
|
|
435
|
+
console.info(`^ Available balance: ${colors.lyellow(whole_wits(unlocked, 2))}`);
|
|
480
436
|
}
|
|
481
437
|
|
|
482
438
|
async function coinbase(options = {}) {
|
|
@@ -493,10 +449,7 @@ async function coinbase(options = {}) {
|
|
|
493
449
|
wallet = masterWallet;
|
|
494
450
|
}
|
|
495
451
|
|
|
496
|
-
const coinbaseColor =
|
|
497
|
-
utils.totalCoins(await wallet.coinbase.getBalance()) > 0
|
|
498
|
-
? colors.mred
|
|
499
|
-
: colors.lcyan;
|
|
452
|
+
const coinbaseColor = utils.totalCoins(await wallet.coinbase.getBalance()) > 0 ? colors.mred : colors.lcyan;
|
|
500
453
|
console.info(
|
|
501
454
|
`> ${options["node-master-key"] ? "Coinbase " : "Wallet's coinbase"} address: ${coinbaseColor(wallet.coinbase.pkh)}`,
|
|
502
455
|
);
|
|
@@ -521,12 +474,10 @@ async function coinbase(options = {}) {
|
|
|
521
474
|
const withdrawer =
|
|
522
475
|
record.key.withdrawer === wallet.coinbase.pkh ||
|
|
523
476
|
record.key.withdrawer === masterWallet.coinbase.PublicKeyHash
|
|
524
|
-
? record.value.epochs.witnessing > record.value.nonce ||
|
|
525
|
-
record.value.epochs.mining > record.value.nonce
|
|
477
|
+
? record.value.epochs.witnessing > record.value.nonce || record.value.epochs.mining > record.value.nonce
|
|
526
478
|
? colors.mred(record.key.withdrawer)
|
|
527
479
|
: colors.red(record.key.withdrawer)
|
|
528
|
-
: record.value.epochs.witnessing > record.value.nonce ||
|
|
529
|
-
record.value.epochs.mining > record.value.nonce
|
|
480
|
+
: record.value.epochs.witnessing > record.value.nonce || record.value.epochs.mining > record.value.nonce
|
|
530
481
|
? masterWallet.getSigner(record.key.withdrawer)
|
|
531
482
|
? colors.mgreen(record.key.withdrawer)
|
|
532
483
|
: colors.mmagenta(record.key.withdrawer)
|
|
@@ -534,20 +485,14 @@ async function coinbase(options = {}) {
|
|
|
534
485
|
? colors.green(record.key.withdrawer)
|
|
535
486
|
: colors.magenta(record.key.withdrawer);
|
|
536
487
|
const nonce =
|
|
537
|
-
record.value.epochs.witnessing > record.value.nonce ||
|
|
538
|
-
record.value.epochs.mining > record.value.nonce
|
|
488
|
+
record.value.epochs.witnessing > record.value.nonce || record.value.epochs.mining > record.value.nonce
|
|
539
489
|
? record.value.nonce
|
|
540
490
|
: colors.gray(record.value.nonce || "");
|
|
541
491
|
return [
|
|
542
492
|
index + 1,
|
|
543
493
|
withdrawer,
|
|
544
494
|
nonce,
|
|
545
|
-
...(verbose
|
|
546
|
-
? [
|
|
547
|
-
record.value.epochs.witnessing || "",
|
|
548
|
-
record.value.epochs.mining || "",
|
|
549
|
-
]
|
|
550
|
-
: []),
|
|
495
|
+
...(verbose ? [record.value.epochs.witnessing || "", record.value.epochs.mining || ""] : []),
|
|
551
496
|
colors.yellow(Witnet.Coins.fromNanowits(record.value.coins).wits),
|
|
552
497
|
];
|
|
553
498
|
}),
|
|
@@ -561,18 +506,14 @@ async function coinbase(options = {}) {
|
|
|
561
506
|
humanizers: [
|
|
562
507
|
undefined,
|
|
563
508
|
undefined,
|
|
564
|
-
...(verbose
|
|
565
|
-
? [helpers.commas, helpers.commas, helpers.commas]
|
|
566
|
-
: [helpers.commas]),
|
|
509
|
+
...(verbose ? [helpers.commas, helpers.commas, helpers.commas] : [helpers.commas]),
|
|
567
510
|
helpers.commas,
|
|
568
511
|
],
|
|
569
512
|
colors: [
|
|
570
513
|
undefined,
|
|
571
514
|
undefined,
|
|
572
515
|
undefined,
|
|
573
|
-
...(verbose
|
|
574
|
-
? [colors.magenta, colors.cyan, colors.myellow]
|
|
575
|
-
: [colors.myellow]),
|
|
516
|
+
...(verbose ? [colors.magenta, colors.cyan, colors.myellow] : [colors.myellow]),
|
|
576
517
|
],
|
|
577
518
|
},
|
|
578
519
|
);
|
|
@@ -639,30 +580,19 @@ async function stake(options = {}, [authorization]) {
|
|
|
639
580
|
let available = 0;
|
|
640
581
|
let ledger;
|
|
641
582
|
if (options?.from) {
|
|
642
|
-
ledger =
|
|
643
|
-
options.from === wallet.coinbase.pkh
|
|
644
|
-
? wallet.coinbase
|
|
645
|
-
: wallet.getAccount(options.from);
|
|
583
|
+
ledger = options.from === wallet.coinbase.pkh ? wallet.coinbase : wallet.getAccount(options.from);
|
|
646
584
|
if (ledger) available = (await ledger.getBalance()).unlocked;
|
|
647
585
|
} else {
|
|
648
586
|
ledger = wallet;
|
|
649
|
-
available =
|
|
650
|
-
(await wallet.getBalance()).unlocked +
|
|
651
|
-
(await wallet.coinbase.getBalance()).unlocked;
|
|
587
|
+
available = (await wallet.getBalance()).unlocked + (await wallet.coinbase.getBalance()).unlocked;
|
|
652
588
|
}
|
|
653
589
|
if (!ledger) {
|
|
654
590
|
throw Error(`--from address ${options?.from} doesn't belong to the wallet`);
|
|
655
591
|
}
|
|
656
592
|
|
|
657
593
|
// validate withdrawer address
|
|
658
|
-
const withdrawer = Witnet.PublicKeyHash.fromBech32(
|
|
659
|
-
|
|
660
|
-
).toBech32(wallet.network);
|
|
661
|
-
if (
|
|
662
|
-
!wallet.getAccount(withdrawer) &&
|
|
663
|
-
withdrawer !== wallet.coinbase.pkh &&
|
|
664
|
-
!options?.force
|
|
665
|
-
) {
|
|
594
|
+
const withdrawer = Witnet.PublicKeyHash.fromBech32(options?.withdrawer).toBech32(wallet.network);
|
|
595
|
+
if (!wallet.getAccount(withdrawer) && withdrawer !== wallet.coinbase.pkh && !options?.force) {
|
|
666
596
|
const user = await prompt([
|
|
667
597
|
{
|
|
668
598
|
message: `Withdrawer ${withdrawer} doesn't belong to the wallet. Proceed anyway?`,
|
|
@@ -683,13 +613,9 @@ async function stake(options = {}, [authorization]) {
|
|
|
683
613
|
? Witnet.Coins.fromPedros(available - params.fees.pedros)
|
|
684
614
|
: Witnet.Coins.fromWits(params?.value);
|
|
685
615
|
if (available < coins.pedros) {
|
|
686
|
-
throw Error(
|
|
687
|
-
`Insufficient funds ${options?.from ? `on address ${options.from}.` : "on wallet."}`,
|
|
688
|
-
);
|
|
616
|
+
throw Error(`Insufficient funds ${options?.from ? `on address ${options.from}.` : "on wallet."}`);
|
|
689
617
|
} else if (params?.fees && coins.pedros <= params.fees.pedros) {
|
|
690
|
-
throw Error(
|
|
691
|
-
`The fees cannot be greater than the value: ${params.fees.pedros} > ${coins.pedros} $pedros`,
|
|
692
|
-
);
|
|
618
|
+
throw Error(`The fees cannot be greater than the value: ${params.fees.pedros} > ${coins.pedros} $pedros`);
|
|
693
619
|
}
|
|
694
620
|
|
|
695
621
|
// todo: validate withdrawer matches delegatee's withdrawer
|
|
@@ -716,10 +642,7 @@ async function transfer(options = {}) {
|
|
|
716
642
|
let available = 0n;
|
|
717
643
|
let ledger;
|
|
718
644
|
if (options?.from) {
|
|
719
|
-
ledger =
|
|
720
|
-
options.from === wallet.coinbase.pkh
|
|
721
|
-
? wallet.coinbase
|
|
722
|
-
: wallet.getAccount(options.from);
|
|
645
|
+
ledger = options.from === wallet.coinbase.pkh ? wallet.coinbase : wallet.getAccount(options.from);
|
|
723
646
|
} else {
|
|
724
647
|
ledger = wallet;
|
|
725
648
|
}
|
|
@@ -736,37 +659,25 @@ async function transfer(options = {}) {
|
|
|
736
659
|
if (!utils.isHexStringOfLength(options.metadata, 20)) {
|
|
737
660
|
throw Error(`--metadata must be a 20-byte hex string`);
|
|
738
661
|
}
|
|
739
|
-
metadata = Witnet.PublicKeyHash.fromHexString(options.metadata).toBech32(
|
|
740
|
-
wallet.network,
|
|
741
|
-
);
|
|
662
|
+
metadata = Witnet.PublicKeyHash.fromHexString(options.metadata).toBech32(wallet.network);
|
|
742
663
|
}
|
|
743
664
|
|
|
744
665
|
// validate recipient address
|
|
745
666
|
if (!options?.into) {
|
|
746
667
|
throw Error("--into address must be specified");
|
|
747
668
|
}
|
|
748
|
-
const into = Witnet.PublicKeyHash.fromBech32(options?.into).toBech32(
|
|
749
|
-
wallet.network,
|
|
750
|
-
);
|
|
669
|
+
const into = Witnet.PublicKeyHash.fromBech32(options?.into).toBech32(wallet.network);
|
|
751
670
|
|
|
752
671
|
// determine transfer params
|
|
753
672
|
const params = await _loadTransactionParams({ ...options });
|
|
754
673
|
const coins =
|
|
755
674
|
params?.value === "all"
|
|
756
|
-
? Witnet.Coins.fromPedros(
|
|
757
|
-
BigInt(
|
|
758
|
-
available - params.fees.pedros - BigInt(options?.metadata ? 1 : 0),
|
|
759
|
-
),
|
|
760
|
-
)
|
|
675
|
+
? Witnet.Coins.fromPedros(BigInt(available - params.fees.pedros - BigInt(options?.metadata ? 1 : 0)))
|
|
761
676
|
: Witnet.Coins.fromWits(params?.value);
|
|
762
677
|
if (available < coins.pedros) {
|
|
763
|
-
throw Error(
|
|
764
|
-
`Insufficient funds ${options?.from ? `on address ${options.from}.` : "on wallet."}`,
|
|
765
|
-
);
|
|
678
|
+
throw Error(`Insufficient funds ${options?.from ? `on address ${options.from}.` : "on wallet."}`);
|
|
766
679
|
} else if (params?.fees && coins.pedros <= params.fees.pedros) {
|
|
767
|
-
throw Error(
|
|
768
|
-
`The fees cannot be greater than the value: ${params.fees.pedros} > ${coins.pedros} $pedros`,
|
|
769
|
-
);
|
|
680
|
+
throw Error(`The fees cannot be greater than the value: ${params.fees.pedros} > ${coins.pedros} $pedros`);
|
|
770
681
|
}
|
|
771
682
|
|
|
772
683
|
// transfer value
|
|
@@ -791,20 +702,12 @@ async function unstake(options = {}) {
|
|
|
791
702
|
// determine validator address:
|
|
792
703
|
let validator;
|
|
793
704
|
if (options?.from) {
|
|
794
|
-
validator = Witnet.PublicKeyHash.fromBech32(options?.from).toBech32(
|
|
795
|
-
wallet.network,
|
|
796
|
-
);
|
|
705
|
+
validator = Witnet.PublicKeyHash.fromBech32(options?.from).toBech32(wallet.network);
|
|
797
706
|
} else {
|
|
798
|
-
const delegatees = (
|
|
799
|
-
await wallet.getDelegatees(
|
|
800
|
-
{ by: Witnet.StakesOrderBy.Coins, reverse: true },
|
|
801
|
-
false,
|
|
802
|
-
)
|
|
803
|
-
).filter(
|
|
707
|
+
const delegatees = (await wallet.getDelegatees({ by: Witnet.StakesOrderBy.Coins, reverse: true }, false)).filter(
|
|
804
708
|
(entry) => !options?.into || entry.key.withdrawer === options?.into,
|
|
805
709
|
);
|
|
806
|
-
if (delegatees.length === 0)
|
|
807
|
-
throw new Error(`No delegatees to withdraw from.`);
|
|
710
|
+
if (delegatees.length === 0) throw new Error(`No delegatees to withdraw from.`);
|
|
808
711
|
else if (delegatees.length === 1) validator = delegatees[0].key.validator;
|
|
809
712
|
else {
|
|
810
713
|
const choices = delegatees
|
|
@@ -826,15 +729,12 @@ async function unstake(options = {}) {
|
|
|
826
729
|
// determine withdrawer address:
|
|
827
730
|
let withdrawer;
|
|
828
731
|
if (options?.into) {
|
|
829
|
-
withdrawer = Witnet.PublicKeyHash.fromBech32(options?.into).toBech32(
|
|
830
|
-
wallet.network,
|
|
831
|
-
);
|
|
732
|
+
withdrawer = Witnet.PublicKeyHash.fromBech32(options?.into).toBech32(wallet.network);
|
|
832
733
|
} else {
|
|
833
|
-
const stakes = (
|
|
834
|
-
|
|
835
|
-
)
|
|
836
|
-
if (stakes.length === 0)
|
|
837
|
-
throw new Error(`Nothing to withdraw from validator ${validator}.`);
|
|
734
|
+
const stakes = (await wallet.provider.stakes({ filter: { validator } })).filter(
|
|
735
|
+
(entry) => wallet.getSigner(entry.key.withdrawer) !== undefined,
|
|
736
|
+
);
|
|
737
|
+
if (stakes.length === 0) throw new Error(`Nothing to withdraw from validator ${validator}.`);
|
|
838
738
|
else if (stakes.length === 1) withdrawer = stakes[0].key.withdrawer;
|
|
839
739
|
else {
|
|
840
740
|
const choices = stakes.map(
|
|
@@ -862,8 +762,7 @@ async function unstake(options = {}) {
|
|
|
862
762
|
|
|
863
763
|
// valite delegatee address:
|
|
864
764
|
const delegatee = (await ledger.getDelegatees()).find(
|
|
865
|
-
(stake) =>
|
|
866
|
-
stake.key.validator === validator && stake.key.withdrawer === withdrawer,
|
|
765
|
+
(stake) => stake.key.validator === validator && stake.key.withdrawer === withdrawer,
|
|
867
766
|
);
|
|
868
767
|
if (!delegatee) {
|
|
869
768
|
throw Error(`Nothing to withdraw from ${validator} into ${withdrawer}`);
|
|
@@ -882,9 +781,7 @@ async function unstake(options = {}) {
|
|
|
882
781
|
if (available < value.pedros + params?.fees.pedros) {
|
|
883
782
|
throw Error(`Cannot withdraw that much: ${value.pedros} > ${available}`);
|
|
884
783
|
} else if (params?.fees && value.pedros <= params.fees.pedros) {
|
|
885
|
-
throw Error(
|
|
886
|
-
`The fees cannot be greater than the value: ${params.fees.pedros} > ${value.pedros} $pedros`,
|
|
887
|
-
);
|
|
784
|
+
throw Error(`The fees cannot be greater than the value: ${params.fees.pedros} > ${value.pedros} $pedros`);
|
|
888
785
|
}
|
|
889
786
|
|
|
890
787
|
// withdraw deposit from validator into withdrawer:
|
|
@@ -903,14 +800,11 @@ async function utxos(options = {}, [from]) {
|
|
|
903
800
|
let ledger;
|
|
904
801
|
let available = 0n;
|
|
905
802
|
if (from) {
|
|
906
|
-
ledger =
|
|
907
|
-
from === wallet.coinbase.pkh ? wallet.coinbase : wallet.getAccount(from);
|
|
803
|
+
ledger = from === wallet.coinbase.pkh ? wallet.coinbase : wallet.getAccount(from);
|
|
908
804
|
if (ledger) available = (await ledger.getBalance()).unlocked;
|
|
909
805
|
} else {
|
|
910
806
|
ledger = wallet;
|
|
911
|
-
available =
|
|
912
|
-
(await wallet.getBalance()).unlocked +
|
|
913
|
-
(await wallet.coinbase.getBalance()).unlocked;
|
|
807
|
+
available = (await wallet.getBalance()).unlocked + (await wallet.coinbase.getBalance()).unlocked;
|
|
914
808
|
}
|
|
915
809
|
if (!ledger) {
|
|
916
810
|
throw Error(`Address ${from} doesn't belong to the wallet`);
|
|
@@ -919,12 +813,7 @@ async function utxos(options = {}, [from]) {
|
|
|
919
813
|
// determine into address
|
|
920
814
|
let into = options?.into;
|
|
921
815
|
if (into) {
|
|
922
|
-
if (
|
|
923
|
-
into !== from &&
|
|
924
|
-
!wallet.getAccount(into) &&
|
|
925
|
-
into !== wallet.coinbase.pkh &&
|
|
926
|
-
!options?.force
|
|
927
|
-
) {
|
|
816
|
+
if (into !== from && !wallet.getAccount(into) && into !== wallet.coinbase.pkh && !options?.force) {
|
|
928
817
|
const user = await prompt([
|
|
929
818
|
{
|
|
930
819
|
message: `Recipient address ${into} doesn't belong to the wallet. Proceed anyway?`,
|
|
@@ -959,19 +848,12 @@ async function utxos(options = {}, [from]) {
|
|
|
959
848
|
? Witnet.Coins.fromWits(params?.value)
|
|
960
849
|
: Witnet.Coins.fromPedros(available);
|
|
961
850
|
if (available < value.pedros) {
|
|
962
|
-
throw Error(
|
|
963
|
-
`Insufficient funds ${from ? `on address ${from}.` : "on wallet."}`,
|
|
964
|
-
);
|
|
851
|
+
throw Error(`Insufficient funds ${from ? `on address ${from}.` : "on wallet."}`);
|
|
965
852
|
} else if (params?.fees && value.pedros <= params.fees.pedros) {
|
|
966
|
-
throw Error(
|
|
967
|
-
`The fees cannot be greater than the value: ${params.fees.pedros} > ${value.pedros} $pedros`,
|
|
968
|
-
);
|
|
853
|
+
throw Error(`The fees cannot be greater than the value: ${params.fees.pedros} > ${value.pedros} $pedros`);
|
|
969
854
|
}
|
|
970
855
|
const utxos = await ledger.selectUtxos({ value });
|
|
971
|
-
const covered =
|
|
972
|
-
utxos
|
|
973
|
-
.map((utxo) => BigInt(utxo.value))
|
|
974
|
-
?.reduce((prev, curr) => prev + curr, 0n) || 0n;
|
|
856
|
+
const covered = utxos.map((utxo) => BigInt(utxo.value))?.reduce((prev, curr) => prev + curr, 0n) || 0n;
|
|
975
857
|
// if (value && covered < value.pedros) {
|
|
976
858
|
// console.log(value, covered)
|
|
977
859
|
// throw Error(`Insufficient unlocked UTXOs in ${from ? `wallet account ${ledger.pkh}` : "wallet"}`)
|
|
@@ -983,12 +865,8 @@ async function utxos(options = {}, [from]) {
|
|
|
983
865
|
helpers.traceTable(
|
|
984
866
|
utxos.map((utxo, index) => [
|
|
985
867
|
index + 1,
|
|
986
|
-
utxo.signer === wallet.coinbase.pkh
|
|
987
|
-
|
|
988
|
-
: colors.mmagenta(utxo.signer),
|
|
989
|
-
utxo?.internal
|
|
990
|
-
? colors.green(utxo.output_pointer)
|
|
991
|
-
: colors.mgreen(utxo.output_pointer),
|
|
868
|
+
utxo.signer === wallet.coinbase.pkh ? colors.mcyan(utxo.signer) : colors.mmagenta(utxo.signer),
|
|
869
|
+
utxo?.internal ? colors.green(utxo.output_pointer) : colors.mgreen(utxo.output_pointer),
|
|
992
870
|
utxo.value,
|
|
993
871
|
]),
|
|
994
872
|
{
|
|
@@ -1002,9 +880,7 @@ async function utxos(options = {}, [from]) {
|
|
|
1002
880
|
colors: [undefined, undefined, undefined, colors.myellow],
|
|
1003
881
|
},
|
|
1004
882
|
);
|
|
1005
|
-
console.info(
|
|
1006
|
-
`^ Available balance: ${colors.lyellow(whole_wits(covered, 2))}`,
|
|
1007
|
-
);
|
|
883
|
+
console.info(`^ Available balance: ${colors.lyellow(whole_wits(covered, 2))}`);
|
|
1008
884
|
}
|
|
1009
885
|
|
|
1010
886
|
const valueTransfer = Witnet.ValueTransfers.from(ledger);
|
|
@@ -1030,9 +906,7 @@ async function utxos(options = {}, [from]) {
|
|
|
1030
906
|
if (splits > 50) {
|
|
1031
907
|
throw Error("Not possible to split into more than 50 UTXOs");
|
|
1032
908
|
}
|
|
1033
|
-
value = Witnet.Coins.fromPedros(
|
|
1034
|
-
BigInt(Math.floor(Number(value.pedros) / splits)),
|
|
1035
|
-
);
|
|
909
|
+
value = Witnet.Coins.fromPedros(BigInt(Math.floor(Number(value.pedros) / splits)));
|
|
1036
910
|
recipients.push(...Array(splits).fill([into, value]));
|
|
1037
911
|
await helpers.traceTransaction(valueTransfer, {
|
|
1038
912
|
headline: `SPLITTING UTXOs: ${utxos.length} -> ${Number(value.pedros) * splits < covered ? splits + 1 : splits}`,
|
|
@@ -1066,28 +940,17 @@ async function validators(options = {}) {
|
|
|
1066
940
|
: record.key.validator !== ""
|
|
1067
941
|
? colors.mmagenta(record.key.withdrawer)
|
|
1068
942
|
: colors.magenta(record.key.withdrawer),
|
|
1069
|
-
...(record.value.epochs.witnessing > record.value.nonce ||
|
|
1070
|
-
record.value.epochs.mining > record.value.nonce
|
|
943
|
+
...(record.value.epochs.witnessing > record.value.nonce || record.value.epochs.mining > record.value.nonce
|
|
1071
944
|
? [colors.mcyan(record.key.validator), record.value.nonce]
|
|
1072
|
-
: [
|
|
1073
|
-
|
|
1074
|
-
colors.gray(record.value.nonce || ""),
|
|
1075
|
-
]),
|
|
1076
|
-
...(verbose
|
|
1077
|
-
? [
|
|
1078
|
-
record.value.epochs.witnessing || "",
|
|
1079
|
-
record.value.epochs.mining || "",
|
|
1080
|
-
]
|
|
1081
|
-
: []),
|
|
945
|
+
: [colors.cyan(record.key.validator), colors.gray(record.value.nonce || "")]),
|
|
946
|
+
...(verbose ? [record.value.epochs.witnessing || "", record.value.epochs.mining || ""] : []),
|
|
1082
947
|
colors.yellow(record.value.coins),
|
|
1083
948
|
];
|
|
1084
949
|
}),
|
|
1085
950
|
{
|
|
1086
951
|
headlines: [
|
|
1087
952
|
// "INDEX",
|
|
1088
|
-
coinbase
|
|
1089
|
-
? "WITNET WALLET COINBASE"
|
|
1090
|
-
: `WITNET WALLET ${wallet.network.toUpperCase()} ACCOUNTS`,
|
|
953
|
+
coinbase ? "WITNET WALLET COINBASE" : `WITNET WALLET ${wallet.network.toUpperCase()} ACCOUNTS`,
|
|
1091
954
|
"STAKE DELEGATEES",
|
|
1092
955
|
...(verbose ? ["Nonce", "LW_Epoch", "LM_Epoch"] : ["Nonce"]),
|
|
1093
956
|
"STAKED ($pedros)",
|
|
@@ -1095,18 +958,14 @@ async function validators(options = {}) {
|
|
|
1095
958
|
humanizers: [
|
|
1096
959
|
undefined,
|
|
1097
960
|
undefined,
|
|
1098
|
-
...(verbose
|
|
1099
|
-
? [helpers.commas, helpers.commas, helpers.commas]
|
|
1100
|
-
: [helpers.commas]),
|
|
961
|
+
...(verbose ? [helpers.commas, helpers.commas, helpers.commas] : [helpers.commas]),
|
|
1101
962
|
helpers.commas,
|
|
1102
963
|
],
|
|
1103
964
|
colors: [
|
|
1104
965
|
undefined,
|
|
1105
966
|
undefined,
|
|
1106
967
|
undefined,
|
|
1107
|
-
...(verbose
|
|
1108
|
-
? [colors.magenta, colors.cyan, colors.myellow]
|
|
1109
|
-
: [colors.myellow]),
|
|
968
|
+
...(verbose ? [colors.magenta, colors.cyan, colors.myellow] : [colors.myellow]),
|
|
1110
969
|
],
|
|
1111
970
|
},
|
|
1112
971
|
);
|
|
@@ -1121,11 +980,7 @@ async function validators(options = {}) {
|
|
|
1121
980
|
|
|
1122
981
|
async function _loadRadonRequest(options = {}) {
|
|
1123
982
|
const args = options?.args || [];
|
|
1124
|
-
if (
|
|
1125
|
-
options?.pattern &&
|
|
1126
|
-
typeof options.pattern === "string" &&
|
|
1127
|
-
utils.isHexString(options.pattern)
|
|
1128
|
-
) {
|
|
983
|
+
if (options?.pattern && typeof options.pattern === "string" && utils.isHexString(options.pattern)) {
|
|
1129
984
|
// if (utils.isHexStringOfLength(options.pattern, 32)) {
|
|
1130
985
|
// throw `Searching RADON_BYTECODE by RAD_HASH not yet supported.`
|
|
1131
986
|
// } else
|
|
@@ -1147,9 +1002,7 @@ async function _loadRadonRequest(options = {}) {
|
|
|
1147
1002
|
|
|
1148
1003
|
if (args.length > 0) {
|
|
1149
1004
|
// ignore RadonRequests if args were passed from the CLI
|
|
1150
|
-
assets = assets.filter(
|
|
1151
|
-
([, artifact]) => !(artifact instanceof Witnet.Radon.RadonRequest),
|
|
1152
|
-
);
|
|
1005
|
+
assets = assets.filter(([, artifact]) => !(artifact instanceof Witnet.Radon.RadonRequest));
|
|
1153
1006
|
}
|
|
1154
1007
|
|
|
1155
1008
|
// sort Radon assets alphabetically
|
|
@@ -1194,9 +1047,7 @@ async function _loadRadonRequest(options = {}) {
|
|
|
1194
1047
|
]);
|
|
1195
1048
|
templateArgs = artifact.samples[sample.key];
|
|
1196
1049
|
} else if (args.length === 1 && artifact?.samples) {
|
|
1197
|
-
const sample = Object.keys(artifact.samples).find(
|
|
1198
|
-
(sample) => sample.toLowerCase() === args[0].toLowerCase(),
|
|
1199
|
-
);
|
|
1050
|
+
const sample = Object.keys(artifact.samples).find((sample) => sample.toLowerCase() === args[0].toLowerCase());
|
|
1200
1051
|
if (sample) templateArgs = artifact.samples[sample];
|
|
1201
1052
|
}
|
|
1202
1053
|
|
|
@@ -1213,10 +1064,7 @@ async function _loadRadonRequest(options = {}) {
|
|
|
1213
1064
|
} else {
|
|
1214
1065
|
if (artifact instanceof Witnet.Radon.RadonModal) {
|
|
1215
1066
|
if (templateArgs.length === 0) templateArgs = [...args];
|
|
1216
|
-
if (
|
|
1217
|
-
templateArgs.length === 0 &&
|
|
1218
|
-
templateArgs.length < artifact.argsCount + 1
|
|
1219
|
-
) {
|
|
1067
|
+
if (templateArgs.length === 0 && templateArgs.length < artifact.argsCount + 1) {
|
|
1220
1068
|
throw Error(
|
|
1221
1069
|
`${key}: missing ${artifact.argsCount + 1 - templateArgs.length} out of ${artifact.argsCount + 1} parameters.`,
|
|
1222
1070
|
);
|
|
@@ -1230,9 +1078,7 @@ async function _loadRadonRequest(options = {}) {
|
|
|
1230
1078
|
templateArgs[index] = args.splice(0, retrieval.argsCount);
|
|
1231
1079
|
if (templateArgs[index].length < retrieval.argsCount) {
|
|
1232
1080
|
throw Error(
|
|
1233
|
-
`${key}: missing ${
|
|
1234
|
-
retrieval.argsCount - templateArgs[index].length
|
|
1235
|
-
} out of ${
|
|
1081
|
+
`${key}: missing ${retrieval.argsCount - templateArgs[index].length} out of ${
|
|
1236
1082
|
retrieval.argsCount
|
|
1237
1083
|
} expected args for template source #${index + 1}`,
|
|
1238
1084
|
);
|
|
@@ -1241,9 +1087,7 @@ async function _loadRadonRequest(options = {}) {
|
|
|
1241
1087
|
}
|
|
1242
1088
|
artifact = artifact.buildRadonRequest(templateArgs);
|
|
1243
1089
|
} else {
|
|
1244
|
-
throw Error(
|
|
1245
|
-
`${key}: unsupported Radon asset type ${artifact?.constructor.name}`,
|
|
1246
|
-
);
|
|
1090
|
+
throw Error(`${key}: unsupported Radon asset type ${artifact?.constructor.name}`);
|
|
1247
1091
|
}
|
|
1248
1092
|
}
|
|
1249
1093
|
}
|
|
@@ -1251,29 +1095,18 @@ async function _loadRadonRequest(options = {}) {
|
|
|
1251
1095
|
}
|
|
1252
1096
|
|
|
1253
1097
|
async function _loadTransactionParams(options = {}) {
|
|
1254
|
-
const confirmations = options?.confirmations
|
|
1255
|
-
? parseInt(options?.confirmations, 10)
|
|
1256
|
-
: options?.await
|
|
1257
|
-
? 0
|
|
1258
|
-
: undefined;
|
|
1098
|
+
const confirmations = options?.confirmations ? parseInt(options?.confirmations, 10) : options?.await ? 0 : undefined;
|
|
1259
1099
|
let fees = options?.fees
|
|
1260
1100
|
? Witnet.Coins.fromWits(options.fees)
|
|
1261
1101
|
: options?.fees === 0n
|
|
1262
1102
|
? Witnet.Coins.zero()
|
|
1263
1103
|
: undefined;
|
|
1264
|
-
const value = options?.value
|
|
1265
|
-
? options?.value.toLowerCase() === "all"
|
|
1266
|
-
? "all"
|
|
1267
|
-
: options.value
|
|
1268
|
-
: undefined;
|
|
1104
|
+
const value = options?.value ? (options?.value.toLowerCase() === "all" ? "all" : options.value) : undefined;
|
|
1269
1105
|
if (fees === undefined) {
|
|
1270
1106
|
if (value === "all") {
|
|
1271
1107
|
throw Error("--fees must be specified if --value is set to `all`");
|
|
1272
1108
|
}
|
|
1273
|
-
let priority =
|
|
1274
|
-
!options?.priority && options?.force
|
|
1275
|
-
? Witnet.TransactionPriority.Medium
|
|
1276
|
-
: options?.priority;
|
|
1109
|
+
let priority = !options?.priority && options?.force ? Witnet.TransactionPriority.Medium : options?.priority;
|
|
1277
1110
|
if (!priority) {
|
|
1278
1111
|
const priorities = {
|
|
1279
1112
|
"< 60 seconds": Witnet.TransactionPriority.Opulent,
|
|
@@ -1294,10 +1127,7 @@ async function _loadTransactionParams(options = {}) {
|
|
|
1294
1127
|
} else if (!Object.values(Witnet.TransactionPriority).includes(priority)) {
|
|
1295
1128
|
throw Error(`Invalid priority "${priority}"`);
|
|
1296
1129
|
}
|
|
1297
|
-
fees =
|
|
1298
|
-
Witnet.TransactionPriority[
|
|
1299
|
-
priority.charAt(0).toUpperCase() + priority.slice(1)
|
|
1300
|
-
];
|
|
1130
|
+
fees = Witnet.TransactionPriority[priority.charAt(0).toUpperCase() + priority.slice(1)];
|
|
1301
1131
|
}
|
|
1302
1132
|
return {
|
|
1303
1133
|
await: options?.await,
|
|
@@ -1325,9 +1155,7 @@ async function _loadWallet(options = {}) {
|
|
|
1325
1155
|
if (options?.strategy && !strategies[options.strategy]) {
|
|
1326
1156
|
throw Error(`Unrecognised UTXO selection strategy "${options.strategy}"`);
|
|
1327
1157
|
}
|
|
1328
|
-
const strategy =
|
|
1329
|
-
strategies[options?.strategy || "slim-fit"] ||
|
|
1330
|
-
Witnet.UtxoSelectionStrategy.SlimFit;
|
|
1158
|
+
const strategy = strategies[options?.strategy || "slim-fit"] || Witnet.UtxoSelectionStrategy.SlimFit;
|
|
1331
1159
|
const gap = options?.gap || 10;
|
|
1332
1160
|
let wallet;
|
|
1333
1161
|
const xprv = options?.xprv || process.env.WITNET_SDK_WALLET_MASTER_KEY;
|