@utexo/rgb-sdk 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/README.md +1 -1
- package/cli/run.mjs +19 -2
- package/dist/index.cjs +591 -201
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +33 -31
- package/dist/index.d.ts +33 -31
- package/dist/index.mjs +591 -201
- package/dist/index.mjs.map +1 -1
- package/package.json +22 -6
package/dist/index.mjs
CHANGED
|
@@ -83,7 +83,7 @@ var CryptoError = class _CryptoError extends SDKError {
|
|
|
83
83
|
}
|
|
84
84
|
};
|
|
85
85
|
var ConfigurationError = class _ConfigurationError extends SDKError {
|
|
86
|
-
constructor(message,
|
|
86
|
+
constructor(message, _field) {
|
|
87
87
|
super(message, "CONFIGURATION_ERROR");
|
|
88
88
|
this.name = "ConfigurationError";
|
|
89
89
|
Object.setPrototypeOf(this, _ConfigurationError.prototype);
|
|
@@ -192,12 +192,12 @@ var mainnetPreset = {
|
|
|
192
192
|
// TODO: Update to production network ID
|
|
193
193
|
assets: [
|
|
194
194
|
{
|
|
195
|
-
assetId: "rgb:
|
|
195
|
+
assetId: "rgb:nkHbmy97-R4cjRCe-j~VvT~E-0UQ0OW8-jOCCW6O-EqeCq9M",
|
|
196
196
|
// TODO: Update to production asset ID
|
|
197
197
|
tokenName: "tUSD",
|
|
198
198
|
longName: "USDT",
|
|
199
199
|
precision: 6,
|
|
200
|
-
tokenId:
|
|
200
|
+
tokenId: 3
|
|
201
201
|
}
|
|
202
202
|
]
|
|
203
203
|
}),
|
|
@@ -207,12 +207,12 @@ var mainnetPreset = {
|
|
|
207
207
|
// TODO: Update to production network ID
|
|
208
208
|
assets: [
|
|
209
209
|
{
|
|
210
|
-
assetId: "rgb:
|
|
210
|
+
assetId: "rgb:nkHbmy97-R4cjRCe-j~VvT~E-0UQ0OW8-jOCCW6O-EqeCq9M",
|
|
211
211
|
// TODO: Update to production asset ID
|
|
212
212
|
tokenName: "tUSD",
|
|
213
213
|
longName: "USDT",
|
|
214
214
|
precision: 6,
|
|
215
|
-
tokenId:
|
|
215
|
+
tokenId: 3
|
|
216
216
|
}
|
|
217
217
|
]
|
|
218
218
|
}),
|
|
@@ -222,12 +222,12 @@ var mainnetPreset = {
|
|
|
222
222
|
// TODO: Update to production network ID
|
|
223
223
|
assets: [
|
|
224
224
|
{
|
|
225
|
-
assetId: "rgb:
|
|
225
|
+
assetId: "rgb:0yyfySrb-TArdWKB-6Y0yhUX-dbqMpN3-NnjsV2F-2fMhOI4",
|
|
226
226
|
// TODO: Update to production asset ID
|
|
227
227
|
tokenName: "tUSD",
|
|
228
228
|
longName: "USDT",
|
|
229
229
|
precision: 6,
|
|
230
|
-
tokenId:
|
|
230
|
+
tokenId: 3
|
|
231
231
|
}
|
|
232
232
|
]
|
|
233
233
|
})
|
|
@@ -244,13 +244,18 @@ function getUtxoNetworkConfig(preset) {
|
|
|
244
244
|
}
|
|
245
245
|
var utexoNetworkMap = testnetPreset.networkMap;
|
|
246
246
|
var utexoNetworkIdMap = testnetPreset.networkIdMap;
|
|
247
|
-
function getDestinationAsset(senderNetwork, destinationNetwork, assetIdSender) {
|
|
248
|
-
const
|
|
247
|
+
function getDestinationAsset(senderNetwork, destinationNetwork, assetIdSender, networkIdMap) {
|
|
248
|
+
const config = networkIdMap ?? utexoNetworkIdMap;
|
|
249
|
+
const destinationConfig = config[destinationNetwork];
|
|
249
250
|
if (assetIdSender == null) return destinationConfig.assets[0];
|
|
250
|
-
const senderConfig =
|
|
251
|
-
const senderAsset = senderConfig.assets.find(
|
|
251
|
+
const senderConfig = config[senderNetwork];
|
|
252
|
+
const senderAsset = senderConfig.assets.find(
|
|
253
|
+
(a) => a.assetId === assetIdSender
|
|
254
|
+
);
|
|
252
255
|
if (!senderAsset) return void 0;
|
|
253
|
-
return destinationConfig.assets.find(
|
|
256
|
+
return destinationConfig.assets.find(
|
|
257
|
+
(a) => a.tokenId === senderAsset.tokenId
|
|
258
|
+
);
|
|
254
259
|
}
|
|
255
260
|
|
|
256
261
|
// src/constants/network.ts
|
|
@@ -300,7 +305,13 @@ var DEFAULT_MAX_RETRIES = 3;
|
|
|
300
305
|
var DEFAULT_LOG_LEVEL = 3;
|
|
301
306
|
|
|
302
307
|
// src/utils/validation.ts
|
|
303
|
-
var VALID_NETWORKS = [
|
|
308
|
+
var VALID_NETWORKS = [
|
|
309
|
+
"mainnet",
|
|
310
|
+
"testnet",
|
|
311
|
+
"testnet4",
|
|
312
|
+
"signet",
|
|
313
|
+
"regtest"
|
|
314
|
+
];
|
|
304
315
|
function validateNetwork(network) {
|
|
305
316
|
const key = String(network);
|
|
306
317
|
const normalized = NETWORK_MAP[key];
|
|
@@ -339,6 +350,7 @@ function validateBase64(base64, field = "data") {
|
|
|
339
350
|
try {
|
|
340
351
|
Buffer.from(base64.trim(), "base64");
|
|
341
352
|
} catch (error) {
|
|
353
|
+
console.error(error);
|
|
342
354
|
throw new ValidationError(`Invalid base64 encoding for ${field}`, field);
|
|
343
355
|
}
|
|
344
356
|
}
|
|
@@ -346,7 +358,10 @@ function validatePsbt(psbt, field = "psbt") {
|
|
|
346
358
|
validateBase64(psbt, field);
|
|
347
359
|
const psbtString = String(psbt).trim();
|
|
348
360
|
if (psbtString.length < 50) {
|
|
349
|
-
throw new ValidationError(
|
|
361
|
+
throw new ValidationError(
|
|
362
|
+
`${field} appears to be too short to be a valid PSBT`,
|
|
363
|
+
field
|
|
364
|
+
);
|
|
350
365
|
}
|
|
351
366
|
}
|
|
352
367
|
function validateHex(hex, field = "data") {
|
|
@@ -370,11 +385,11 @@ function validateString(value, field) {
|
|
|
370
385
|
}
|
|
371
386
|
function mapNetworkToRgbLib(network) {
|
|
372
387
|
const networkMap = {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
388
|
+
mainnet: "Mainnet",
|
|
389
|
+
testnet: "Testnet",
|
|
390
|
+
testnet4: "Testnet4",
|
|
391
|
+
signet: "Signet",
|
|
392
|
+
regtest: "Regtest"
|
|
378
393
|
};
|
|
379
394
|
const networkStr = String(network).toLowerCase();
|
|
380
395
|
return networkMap[networkStr] || "Regtest";
|
|
@@ -385,7 +400,10 @@ var restoreWallet = (params) => {
|
|
|
385
400
|
throw new ValidationError("Backup file not found", "backup");
|
|
386
401
|
}
|
|
387
402
|
if (!fs2.existsSync(dataDir)) {
|
|
388
|
-
throw new ValidationError(
|
|
403
|
+
throw new ValidationError(
|
|
404
|
+
`Restore directory does not exist: ${dataDir}`,
|
|
405
|
+
"restoreDir"
|
|
406
|
+
);
|
|
389
407
|
}
|
|
390
408
|
rgblib.restoreBackup(backupFilePath, password, dataDir);
|
|
391
409
|
return {
|
|
@@ -395,7 +413,9 @@ var restoreWallet = (params) => {
|
|
|
395
413
|
var restoreFromVss = (params) => {
|
|
396
414
|
const anyLib = rgblib;
|
|
397
415
|
if (typeof anyLib.restoreFromVss !== "function") {
|
|
398
|
-
throw new WalletError(
|
|
416
|
+
throw new WalletError(
|
|
417
|
+
"VSS restore is not available in the current rgb-lib build."
|
|
418
|
+
);
|
|
399
419
|
}
|
|
400
420
|
if (!params.targetDir) {
|
|
401
421
|
throw new ValidationError("targetDir is required", "targetDir");
|
|
@@ -438,7 +458,7 @@ var RGBLibClient = class {
|
|
|
438
458
|
accountXpubColored: this.xpubCol,
|
|
439
459
|
masterFingerprint: this.masterFingerprint,
|
|
440
460
|
maxAllocationsPerUtxo: "1",
|
|
441
|
-
vanillaKeychain: "
|
|
461
|
+
vanillaKeychain: "0",
|
|
442
462
|
supportedSchemas: [
|
|
443
463
|
rgblib.AssetSchema.Cfa,
|
|
444
464
|
rgblib.AssetSchema.Nia,
|
|
@@ -449,7 +469,11 @@ var RGBLibClient = class {
|
|
|
449
469
|
this.wallet = new rgblib.Wallet(new rgblib.WalletData(walletData));
|
|
450
470
|
} catch (error) {
|
|
451
471
|
console.log("error", error);
|
|
452
|
-
throw new WalletError(
|
|
472
|
+
throw new WalletError(
|
|
473
|
+
"Failed to initialize rgb-lib wallet",
|
|
474
|
+
void 0,
|
|
475
|
+
error
|
|
476
|
+
);
|
|
453
477
|
}
|
|
454
478
|
}
|
|
455
479
|
/**
|
|
@@ -462,7 +486,11 @@ var RGBLibClient = class {
|
|
|
462
486
|
try {
|
|
463
487
|
this.online = this.wallet.goOnline(false, this.indexerUrl);
|
|
464
488
|
} catch (error) {
|
|
465
|
-
throw new WalletError(
|
|
489
|
+
throw new WalletError(
|
|
490
|
+
"Failed to establish online connection",
|
|
491
|
+
void 0,
|
|
492
|
+
error
|
|
493
|
+
);
|
|
466
494
|
}
|
|
467
495
|
}
|
|
468
496
|
/**
|
|
@@ -499,7 +527,14 @@ var RGBLibClient = class {
|
|
|
499
527
|
const size = params.size !== void 0 ? String(params.size) : null;
|
|
500
528
|
const feeRate = params.feeRate ? String(params.feeRate) : "1";
|
|
501
529
|
const skipSync = false;
|
|
502
|
-
return this.wallet.createUtxosBegin(
|
|
530
|
+
return this.wallet.createUtxosBegin(
|
|
531
|
+
online,
|
|
532
|
+
upTo,
|
|
533
|
+
num,
|
|
534
|
+
size,
|
|
535
|
+
feeRate,
|
|
536
|
+
skipSync
|
|
537
|
+
);
|
|
503
538
|
}
|
|
504
539
|
createUtxosEnd(params) {
|
|
505
540
|
const online = this.getOnline();
|
|
@@ -533,22 +568,33 @@ var RGBLibClient = class {
|
|
|
533
568
|
transportEndpoints = [this.transportEndpoint];
|
|
534
569
|
}
|
|
535
570
|
if (!assetId) {
|
|
536
|
-
throw new ValidationError(
|
|
571
|
+
throw new ValidationError(
|
|
572
|
+
"asset_id is required for send operation",
|
|
573
|
+
"asset_id"
|
|
574
|
+
);
|
|
537
575
|
}
|
|
538
576
|
if (!recipientId) {
|
|
539
|
-
throw new ValidationError(
|
|
577
|
+
throw new ValidationError(
|
|
578
|
+
"Could not extract recipient_id from invoice",
|
|
579
|
+
"invoice"
|
|
580
|
+
);
|
|
540
581
|
}
|
|
541
582
|
if (!amount) {
|
|
542
|
-
throw new ValidationError(
|
|
583
|
+
throw new ValidationError(
|
|
584
|
+
"amount is required for send operation",
|
|
585
|
+
"amount"
|
|
586
|
+
);
|
|
543
587
|
}
|
|
544
588
|
const assignment = { Fungible: amount };
|
|
545
589
|
const recipientMap = {
|
|
546
|
-
[assetId]: [
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
590
|
+
[assetId]: [
|
|
591
|
+
{
|
|
592
|
+
recipientId,
|
|
593
|
+
witnessData,
|
|
594
|
+
assignment,
|
|
595
|
+
transportEndpoints
|
|
596
|
+
}
|
|
597
|
+
]
|
|
552
598
|
};
|
|
553
599
|
const psbt = this.wallet.sendBegin(
|
|
554
600
|
online,
|
|
@@ -569,16 +615,25 @@ var RGBLibClient = class {
|
|
|
569
615
|
const donation = params.donation ?? true;
|
|
570
616
|
const { recipientMap } = params;
|
|
571
617
|
if (!recipientMap || typeof recipientMap !== "object") {
|
|
572
|
-
throw new ValidationError(
|
|
618
|
+
throw new ValidationError(
|
|
619
|
+
"recipientMap is required and must be a non-empty object",
|
|
620
|
+
"recipientMap"
|
|
621
|
+
);
|
|
573
622
|
}
|
|
574
623
|
const assetIds = Object.keys(recipientMap);
|
|
575
624
|
if (assetIds.length === 0) {
|
|
576
|
-
throw new ValidationError(
|
|
625
|
+
throw new ValidationError(
|
|
626
|
+
"recipientMap must contain at least one asset id",
|
|
627
|
+
"recipientMap"
|
|
628
|
+
);
|
|
577
629
|
}
|
|
578
630
|
for (const assetId of assetIds) {
|
|
579
631
|
const recipients = recipientMap[assetId];
|
|
580
632
|
if (!Array.isArray(recipients) || recipients.length === 0) {
|
|
581
|
-
throw new ValidationError(
|
|
633
|
+
throw new ValidationError(
|
|
634
|
+
`recipientMap["${assetId}"] must be a non-empty array of recipients`,
|
|
635
|
+
"recipientMap"
|
|
636
|
+
);
|
|
582
637
|
}
|
|
583
638
|
}
|
|
584
639
|
const psbt = this.wallet.sendBegin(
|
|
@@ -623,8 +678,10 @@ var RGBLibClient = class {
|
|
|
623
678
|
}
|
|
624
679
|
}
|
|
625
680
|
return result;
|
|
626
|
-
} catch (
|
|
627
|
-
console.warn(
|
|
681
|
+
} catch (_error) {
|
|
682
|
+
console.warn(
|
|
683
|
+
"rgb-lib estimation fee are not available, using default fee rate 2"
|
|
684
|
+
);
|
|
628
685
|
return 2;
|
|
629
686
|
}
|
|
630
687
|
}
|
|
@@ -666,14 +723,29 @@ var RGBLibClient = class {
|
|
|
666
723
|
const amounts = params.amounts.map((a) => String(a));
|
|
667
724
|
return this.wallet.issueAssetNIA(ticker, name, precision, amounts);
|
|
668
725
|
}
|
|
726
|
+
// @ts-ignore
|
|
727
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
669
728
|
issueAssetIfa(params) {
|
|
670
|
-
throw new ValidationError(
|
|
729
|
+
throw new ValidationError(
|
|
730
|
+
"issueAssetIfa is not fully supported in rgb-lib. Use RGB Node server for IFA assets.",
|
|
731
|
+
"asset"
|
|
732
|
+
);
|
|
671
733
|
}
|
|
734
|
+
// @ts-ignore
|
|
735
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
672
736
|
inflateBegin(params) {
|
|
673
|
-
throw new ValidationError(
|
|
737
|
+
throw new ValidationError(
|
|
738
|
+
"inflateBegin is not fully supported in rgb-lib. Use RGB Node server for inflation operations.",
|
|
739
|
+
"asset"
|
|
740
|
+
);
|
|
674
741
|
}
|
|
742
|
+
// @ts-ignore
|
|
743
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
675
744
|
inflateEnd(params) {
|
|
676
|
-
throw new ValidationError(
|
|
745
|
+
throw new ValidationError(
|
|
746
|
+
"inflateEnd is not fully supported in rgb-lib. Use RGB Node server for inflation operations.",
|
|
747
|
+
"asset"
|
|
748
|
+
);
|
|
677
749
|
}
|
|
678
750
|
listAssets() {
|
|
679
751
|
const filterAssetSchemas = [];
|
|
@@ -719,7 +791,12 @@ var RGBLibClient = class {
|
|
|
719
791
|
const batchTransferIdx = params.batchTransferIdx !== void 0 ? params.batchTransferIdx : null;
|
|
720
792
|
const noAssetOnly = params.noAssetOnly ?? false;
|
|
721
793
|
const skipSync = params.skipSync ?? false;
|
|
722
|
-
return this.wallet.failTransfers(
|
|
794
|
+
return this.wallet.failTransfers(
|
|
795
|
+
online,
|
|
796
|
+
batchTransferIdx,
|
|
797
|
+
noAssetOnly,
|
|
798
|
+
skipSync
|
|
799
|
+
);
|
|
723
800
|
}
|
|
724
801
|
deleteTransfers(params) {
|
|
725
802
|
const batchTransferIdx = params.batchTransferIdx !== void 0 ? params.batchTransferIdx : null;
|
|
@@ -738,9 +815,15 @@ var RGBLibClient = class {
|
|
|
738
815
|
throw new ValidationError("password is required", "password");
|
|
739
816
|
}
|
|
740
817
|
if (!fs2.existsSync(params.backupPath)) {
|
|
741
|
-
throw new ValidationError(
|
|
818
|
+
throw new ValidationError(
|
|
819
|
+
`Backup directory does not exist: ${params.backupPath}`,
|
|
820
|
+
"backupPath"
|
|
821
|
+
);
|
|
742
822
|
}
|
|
743
|
-
const fullBackupPath = path3.join(
|
|
823
|
+
const fullBackupPath = path3.join(
|
|
824
|
+
params.backupPath,
|
|
825
|
+
`${this.masterFingerprint}.backup`
|
|
826
|
+
);
|
|
744
827
|
this.wallet.backup(fullBackupPath, params.password);
|
|
745
828
|
return {
|
|
746
829
|
message: "Backup created successfully",
|
|
@@ -755,7 +838,9 @@ var RGBLibClient = class {
|
|
|
755
838
|
const walletAny = this.wallet;
|
|
756
839
|
const anyLib = rgblib;
|
|
757
840
|
if (!walletAny || typeof anyLib.VssBackupClient !== "function" || typeof walletAny[methodName] !== "function") {
|
|
758
|
-
throw new WalletError(
|
|
841
|
+
throw new WalletError(
|
|
842
|
+
"VSS backup is not available in the current rgb-lib build."
|
|
843
|
+
);
|
|
759
844
|
}
|
|
760
845
|
return { walletAny, anyLib };
|
|
761
846
|
}
|
|
@@ -765,7 +850,9 @@ var RGBLibClient = class {
|
|
|
765
850
|
configureVssBackup(config) {
|
|
766
851
|
const walletAny = this.wallet;
|
|
767
852
|
if (!walletAny || typeof walletAny.configureVssBackup !== "function") {
|
|
768
|
-
throw new WalletError(
|
|
853
|
+
throw new WalletError(
|
|
854
|
+
"VSS backup is not available in the current rgb-lib build."
|
|
855
|
+
);
|
|
769
856
|
}
|
|
770
857
|
const mappedConfig = {
|
|
771
858
|
server_url: config.serverUrl,
|
|
@@ -789,7 +876,9 @@ var RGBLibClient = class {
|
|
|
789
876
|
disableVssAutoBackup() {
|
|
790
877
|
const walletAny = this.wallet;
|
|
791
878
|
if (!walletAny || typeof walletAny.disableVssAutoBackup !== "function") {
|
|
792
|
-
throw new WalletError(
|
|
879
|
+
throw new WalletError(
|
|
880
|
+
"VSS backup is not available in the current rgb-lib build."
|
|
881
|
+
);
|
|
793
882
|
}
|
|
794
883
|
walletAny.disableVssAutoBackup();
|
|
795
884
|
}
|
|
@@ -884,14 +973,18 @@ function convertToArrayBuffer(data) {
|
|
|
884
973
|
const uint8 = new Uint8Array(data);
|
|
885
974
|
return uint8.buffer;
|
|
886
975
|
} catch (error) {
|
|
887
|
-
throw new Error(
|
|
976
|
+
throw new Error(
|
|
977
|
+
`convertToArrayBuffer: Failed to convert data to ArrayBuffer: ${error instanceof Error ? error.message : String(error)}`
|
|
978
|
+
);
|
|
888
979
|
}
|
|
889
980
|
}
|
|
890
981
|
try {
|
|
891
982
|
const uint8 = new Uint8Array(data);
|
|
892
983
|
return uint8.buffer;
|
|
893
984
|
} catch (error) {
|
|
894
|
-
throw new Error(
|
|
985
|
+
throw new Error(
|
|
986
|
+
`convertToArrayBuffer: Failed to convert data to ArrayBuffer: ${error instanceof Error ? error.message : String(error)}`
|
|
987
|
+
);
|
|
895
988
|
}
|
|
896
989
|
}
|
|
897
990
|
async function sha256(data) {
|
|
@@ -1013,14 +1106,18 @@ function normalizeSeedBuffer(seed) {
|
|
|
1013
1106
|
try {
|
|
1014
1107
|
seedBuffer = new Uint8Array(seed);
|
|
1015
1108
|
} catch (error) {
|
|
1016
|
-
throw new CryptoError(
|
|
1109
|
+
throw new CryptoError(
|
|
1110
|
+
`Failed to convert seed to Uint8Array (buffer property invalid): ${error instanceof Error ? error.message : String(error)}`
|
|
1111
|
+
);
|
|
1017
1112
|
}
|
|
1018
1113
|
}
|
|
1019
1114
|
} else {
|
|
1020
1115
|
try {
|
|
1021
1116
|
seedBuffer = new Uint8Array(seed);
|
|
1022
1117
|
} catch (error) {
|
|
1023
|
-
throw new CryptoError(
|
|
1118
|
+
throw new CryptoError(
|
|
1119
|
+
`Failed to convert seed to Uint8Array: ${error instanceof Error ? error.message : String(error)}`
|
|
1120
|
+
);
|
|
1024
1121
|
}
|
|
1025
1122
|
}
|
|
1026
1123
|
} else {
|
|
@@ -1050,7 +1147,9 @@ async function loadBaseDependencies() {
|
|
|
1050
1147
|
const { createRequire } = await import(nodeModule);
|
|
1051
1148
|
const requireFromModule = createRequire(import.meta.url);
|
|
1052
1149
|
const bip392 = requireFromModule("bip39");
|
|
1053
|
-
const eccModule2 = requireFromModule(
|
|
1150
|
+
const eccModule2 = requireFromModule(
|
|
1151
|
+
"@bitcoinerlab/secp256k1"
|
|
1152
|
+
);
|
|
1054
1153
|
const ecc2 = eccModule2 && typeof eccModule2 === "object" && "default" in eccModule2 ? eccModule2.default : eccModule2;
|
|
1055
1154
|
const bip322 = requireFromModule("bip32");
|
|
1056
1155
|
return {
|
|
@@ -1101,7 +1200,9 @@ async function loadSignerDependencies() {
|
|
|
1101
1200
|
const Psbt2 = bitcoinjs.Psbt;
|
|
1102
1201
|
const payments2 = bitcoinjs.payments;
|
|
1103
1202
|
const networks2 = bitcoinjs.networks;
|
|
1104
|
-
const bip3412 = requireFromModule(
|
|
1203
|
+
const bip3412 = requireFromModule(
|
|
1204
|
+
"bitcoinjs-lib/src/payments/bip341.js"
|
|
1205
|
+
);
|
|
1105
1206
|
const toXOnly2 = bip3412.toXOnly || ((pubkey) => Buffer.from(pubkey.slice(1)));
|
|
1106
1207
|
return {
|
|
1107
1208
|
...base,
|
|
@@ -1154,14 +1255,23 @@ function normalizeSeedInput(seed, field = "seed") {
|
|
|
1154
1255
|
if (typeof seed === "string") {
|
|
1155
1256
|
const trimmed = seed.trim();
|
|
1156
1257
|
if (!trimmed) {
|
|
1157
|
-
throw new ValidationError(
|
|
1258
|
+
throw new ValidationError(
|
|
1259
|
+
`${field} must be a non-empty hex string`,
|
|
1260
|
+
field
|
|
1261
|
+
);
|
|
1158
1262
|
}
|
|
1159
1263
|
const hex = trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
|
|
1160
1264
|
if (hex.length % 2 !== 0) {
|
|
1161
|
-
throw new ValidationError(
|
|
1265
|
+
throw new ValidationError(
|
|
1266
|
+
`${field} hex string must have even length`,
|
|
1267
|
+
field
|
|
1268
|
+
);
|
|
1162
1269
|
}
|
|
1163
1270
|
if (hex.length !== 128) {
|
|
1164
|
-
throw new ValidationError(
|
|
1271
|
+
throw new ValidationError(
|
|
1272
|
+
`${field} must be 64 bytes (128 hex characters)`,
|
|
1273
|
+
field
|
|
1274
|
+
);
|
|
1165
1275
|
}
|
|
1166
1276
|
if (!/^[0-9a-fA-F]+$/.test(hex)) {
|
|
1167
1277
|
throw new ValidationError(`${field} must be a valid hex string`, field);
|
|
@@ -1179,7 +1289,10 @@ function normalizeSeedInput(seed, field = "seed") {
|
|
|
1179
1289
|
}
|
|
1180
1290
|
return new Uint8Array(seed);
|
|
1181
1291
|
}
|
|
1182
|
-
throw new ValidationError(
|
|
1292
|
+
throw new ValidationError(
|
|
1293
|
+
`${field} must be a 64-byte hex string or Uint8Array`,
|
|
1294
|
+
field
|
|
1295
|
+
);
|
|
1183
1296
|
}
|
|
1184
1297
|
function getCoinType(bitcoinNetwork, rgb) {
|
|
1185
1298
|
const net = toNetworkName(bitcoinNetwork);
|
|
@@ -1275,7 +1388,10 @@ async function buildKeysOutputFromSeed(seed, bitcoinNetwork) {
|
|
|
1275
1388
|
try {
|
|
1276
1389
|
root = bip32.fromSeed(seedBuffer, versions);
|
|
1277
1390
|
} catch (error) {
|
|
1278
|
-
throw new CryptoError(
|
|
1391
|
+
throw new CryptoError(
|
|
1392
|
+
"Failed to create BIP32 root node from seed",
|
|
1393
|
+
error
|
|
1394
|
+
);
|
|
1279
1395
|
}
|
|
1280
1396
|
return buildGeneratedKeysFromRoot(root, normalizedNetwork, "");
|
|
1281
1397
|
}
|
|
@@ -1295,7 +1411,9 @@ async function generateKeys2(bitcoinNetwork = "regtest") {
|
|
|
1295
1411
|
try {
|
|
1296
1412
|
const { bip39 } = await ensureBaseDependencies();
|
|
1297
1413
|
if (!bip39 || typeof bip39.generateMnemonic !== "function") {
|
|
1298
|
-
throw new Error(
|
|
1414
|
+
throw new Error(
|
|
1415
|
+
"bip39 not loaded. Dependencies may not have initialized correctly."
|
|
1416
|
+
);
|
|
1299
1417
|
}
|
|
1300
1418
|
const mnemonic = bip39.generateMnemonic(128);
|
|
1301
1419
|
return await buildKeysOutput(mnemonic, bitcoinNetwork);
|
|
@@ -1304,8 +1422,10 @@ async function generateKeys2(bitcoinNetwork = "regtest") {
|
|
|
1304
1422
|
throw new CryptoError("Failed to load dependencies", error);
|
|
1305
1423
|
}
|
|
1306
1424
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1307
|
-
|
|
1308
|
-
|
|
1425
|
+
throw new CryptoError(
|
|
1426
|
+
`Failed to generate mnemonic: ${errorMessage}`,
|
|
1427
|
+
error
|
|
1428
|
+
);
|
|
1309
1429
|
}
|
|
1310
1430
|
}
|
|
1311
1431
|
async function deriveKeysFromMnemonic(bitcoinNetwork = "regtest", mnemonic) {
|
|
@@ -1315,14 +1435,20 @@ async function deriveKeysFromMnemonic(bitcoinNetwork = "regtest", mnemonic) {
|
|
|
1315
1435
|
const { bip39 } = await ensureBaseDependencies();
|
|
1316
1436
|
const trimmedMnemonic = mnemonic.trim();
|
|
1317
1437
|
if (!bip39 || !bip39.validateMnemonic(trimmedMnemonic)) {
|
|
1318
|
-
throw new ValidationError(
|
|
1438
|
+
throw new ValidationError(
|
|
1439
|
+
"Invalid mnemonic format - failed BIP39 validation",
|
|
1440
|
+
"mnemonic"
|
|
1441
|
+
);
|
|
1319
1442
|
}
|
|
1320
1443
|
return await buildKeysOutput(trimmedMnemonic, normalizedNetwork);
|
|
1321
1444
|
} catch (error) {
|
|
1322
1445
|
if (error instanceof ValidationError) {
|
|
1323
1446
|
throw error;
|
|
1324
1447
|
}
|
|
1325
|
-
throw new CryptoError(
|
|
1448
|
+
throw new CryptoError(
|
|
1449
|
+
"Failed to derive keys from mnemonic",
|
|
1450
|
+
error
|
|
1451
|
+
);
|
|
1326
1452
|
}
|
|
1327
1453
|
}
|
|
1328
1454
|
async function deriveKeysFromSeed(bitcoinNetwork = "regtest", seed) {
|
|
@@ -1370,7 +1496,10 @@ async function getXprivFromMnemonic(bitcoinNetwork = "regtest", mnemonic) {
|
|
|
1370
1496
|
if (error instanceof ValidationError) {
|
|
1371
1497
|
throw error;
|
|
1372
1498
|
}
|
|
1373
|
-
throw new CryptoError(
|
|
1499
|
+
throw new CryptoError(
|
|
1500
|
+
"Failed to derive xpriv from mnemonic",
|
|
1501
|
+
error
|
|
1502
|
+
);
|
|
1374
1503
|
}
|
|
1375
1504
|
}
|
|
1376
1505
|
async function getXpubFromXpriv(xpriv, bitcoinNetwork) {
|
|
@@ -1405,17 +1534,31 @@ async function accountXpubsFromMnemonic(bitcoinNetwork = "regtest", mnemonic) {
|
|
|
1405
1534
|
try {
|
|
1406
1535
|
const { bip39 } = await ensureBaseDependencies();
|
|
1407
1536
|
if (!bip39 || !bip39.validateMnemonic(mnemonic)) {
|
|
1408
|
-
throw new ValidationError(
|
|
1537
|
+
throw new ValidationError(
|
|
1538
|
+
"Invalid mnemonic format - failed BIP39 validation",
|
|
1539
|
+
"mnemonic"
|
|
1540
|
+
);
|
|
1409
1541
|
}
|
|
1410
1542
|
return {
|
|
1411
|
-
account_xpub_vanilla: await getAccountXpub(
|
|
1412
|
-
|
|
1543
|
+
account_xpub_vanilla: await getAccountXpub(
|
|
1544
|
+
mnemonic,
|
|
1545
|
+
bitcoinNetwork,
|
|
1546
|
+
false
|
|
1547
|
+
),
|
|
1548
|
+
account_xpub_colored: await getAccountXpub(
|
|
1549
|
+
mnemonic,
|
|
1550
|
+
bitcoinNetwork,
|
|
1551
|
+
true
|
|
1552
|
+
)
|
|
1413
1553
|
};
|
|
1414
1554
|
} catch (error) {
|
|
1415
1555
|
if (error instanceof ValidationError) {
|
|
1416
1556
|
throw error;
|
|
1417
1557
|
}
|
|
1418
|
-
throw new CryptoError(
|
|
1558
|
+
throw new CryptoError(
|
|
1559
|
+
"Failed to derive account xpubs from mnemonic",
|
|
1560
|
+
error
|
|
1561
|
+
);
|
|
1419
1562
|
}
|
|
1420
1563
|
}
|
|
1421
1564
|
|
|
@@ -1475,7 +1618,10 @@ function preprocessPsbtForBDK(psbtBase64, rootNode, fp, network, deps) {
|
|
|
1475
1618
|
}
|
|
1476
1619
|
const pubkeyBuffer = pubkey instanceof Buffer ? pubkey : Buffer.from(pubkey);
|
|
1477
1620
|
const xOnly = toXOnly(pubkeyBuffer);
|
|
1478
|
-
const p2tr = payments.p2tr({
|
|
1621
|
+
const p2tr = payments.p2tr({
|
|
1622
|
+
internalPubkey: xOnly,
|
|
1623
|
+
network: bjsNet
|
|
1624
|
+
});
|
|
1479
1625
|
const expectedScript = p2tr.output;
|
|
1480
1626
|
if (!expectedScript) {
|
|
1481
1627
|
return;
|
|
@@ -1503,7 +1649,7 @@ function preprocessPsbtForBDK(psbtBase64, rootNode, fp, network, deps) {
|
|
|
1503
1649
|
if (!deriv.pubkey || !deriv.pubkey.equals(xOnly)) {
|
|
1504
1650
|
deriv.pubkey = xOnly;
|
|
1505
1651
|
}
|
|
1506
|
-
} catch (
|
|
1652
|
+
} catch (_e) {
|
|
1507
1653
|
}
|
|
1508
1654
|
});
|
|
1509
1655
|
}
|
|
@@ -1535,7 +1681,7 @@ function detectPsbtType(psbtBase64, deps) {
|
|
|
1535
1681
|
}
|
|
1536
1682
|
}
|
|
1537
1683
|
return "create_utxo";
|
|
1538
|
-
} catch (
|
|
1684
|
+
} catch (_e) {
|
|
1539
1685
|
return "create_utxo";
|
|
1540
1686
|
}
|
|
1541
1687
|
}
|
|
@@ -1585,12 +1731,20 @@ async function signPsbtFromSeedInternal(seed, psbtBase64, network, options = {},
|
|
|
1585
1731
|
try {
|
|
1586
1732
|
rootNode = bip32.fromSeed(seedBuffer, versions);
|
|
1587
1733
|
} catch (error) {
|
|
1588
|
-
throw new CryptoError(
|
|
1734
|
+
throw new CryptoError(
|
|
1735
|
+
"Failed to derive root node from seed",
|
|
1736
|
+
error
|
|
1737
|
+
);
|
|
1589
1738
|
}
|
|
1590
1739
|
const fp = await getMasterFingerprint(rootNode);
|
|
1591
1740
|
const psbtType = detectPsbtType(psbtBase64, deps);
|
|
1592
1741
|
const needsPreprocessing = psbtType === "send";
|
|
1593
|
-
const { external, internal } = deriveDescriptors(
|
|
1742
|
+
const { external, internal } = deriveDescriptors(
|
|
1743
|
+
rootNode,
|
|
1744
|
+
fp,
|
|
1745
|
+
network,
|
|
1746
|
+
psbtType
|
|
1747
|
+
);
|
|
1594
1748
|
let wallet2;
|
|
1595
1749
|
try {
|
|
1596
1750
|
wallet2 = bdk.Wallet.create(network, external, internal);
|
|
@@ -1600,7 +1754,13 @@ async function signPsbtFromSeedInternal(seed, psbtBase64, network, options = {},
|
|
|
1600
1754
|
let processedPsbt = psbtBase64.trim();
|
|
1601
1755
|
if (needsPreprocessing || options.preprocess) {
|
|
1602
1756
|
try {
|
|
1603
|
-
processedPsbt = preprocessPsbtForBDK(
|
|
1757
|
+
processedPsbt = preprocessPsbtForBDK(
|
|
1758
|
+
psbtBase64,
|
|
1759
|
+
rootNode,
|
|
1760
|
+
fp,
|
|
1761
|
+
network,
|
|
1762
|
+
deps
|
|
1763
|
+
);
|
|
1604
1764
|
} catch (error) {
|
|
1605
1765
|
throw new CryptoError("Failed to preprocess PSBT", error);
|
|
1606
1766
|
}
|
|
@@ -1629,17 +1789,26 @@ async function signPsbt(mnemonic, psbtBase64, network = "testnet", options = {})
|
|
|
1629
1789
|
let seed;
|
|
1630
1790
|
try {
|
|
1631
1791
|
seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1632
|
-
} catch (
|
|
1792
|
+
} catch (_error) {
|
|
1633
1793
|
throw new ValidationError("Invalid mnemonic format", "mnemonic");
|
|
1634
1794
|
}
|
|
1635
1795
|
const normalizedNetwork = normalizeNetwork(network);
|
|
1636
1796
|
const deps = await ensureSignerDependencies();
|
|
1637
|
-
return await signPsbtFromSeedInternal(
|
|
1797
|
+
return await signPsbtFromSeedInternal(
|
|
1798
|
+
seed,
|
|
1799
|
+
psbtBase64,
|
|
1800
|
+
normalizedNetwork,
|
|
1801
|
+
options,
|
|
1802
|
+
deps
|
|
1803
|
+
);
|
|
1638
1804
|
} catch (error) {
|
|
1639
1805
|
if (error instanceof ValidationError || error instanceof CryptoError) {
|
|
1640
1806
|
throw error;
|
|
1641
1807
|
}
|
|
1642
|
-
throw new CryptoError(
|
|
1808
|
+
throw new CryptoError(
|
|
1809
|
+
"Unexpected error during PSBT signing",
|
|
1810
|
+
error
|
|
1811
|
+
);
|
|
1643
1812
|
}
|
|
1644
1813
|
}
|
|
1645
1814
|
async function signPsbtSync(mnemonic, psbtBase64, network = "testnet", options = {}) {
|
|
@@ -1649,7 +1818,13 @@ async function signPsbtFromSeed(seed, psbtBase64, network = "testnet", options =
|
|
|
1649
1818
|
const normalizedSeed = normalizeSeedInput(seed);
|
|
1650
1819
|
const normalizedNetwork = normalizeNetwork(network);
|
|
1651
1820
|
const deps = await ensureSignerDependencies();
|
|
1652
|
-
return signPsbtFromSeedInternal(
|
|
1821
|
+
return signPsbtFromSeedInternal(
|
|
1822
|
+
normalizedSeed,
|
|
1823
|
+
psbtBase64,
|
|
1824
|
+
normalizedNetwork,
|
|
1825
|
+
options,
|
|
1826
|
+
deps
|
|
1827
|
+
);
|
|
1653
1828
|
}
|
|
1654
1829
|
function ensureMessageInput(message) {
|
|
1655
1830
|
if (typeof message === "string") {
|
|
@@ -1664,7 +1839,10 @@ function ensureMessageInput(message) {
|
|
|
1664
1839
|
}
|
|
1665
1840
|
return Buffer.from(message);
|
|
1666
1841
|
}
|
|
1667
|
-
throw new ValidationError(
|
|
1842
|
+
throw new ValidationError(
|
|
1843
|
+
"message must be a string or Uint8Array",
|
|
1844
|
+
"message"
|
|
1845
|
+
);
|
|
1668
1846
|
}
|
|
1669
1847
|
async function deriveRootFromSeedInput(seed, network) {
|
|
1670
1848
|
const { ecc, factory } = await ensureBaseDependencies();
|
|
@@ -1674,7 +1852,10 @@ async function deriveRootFromSeedInput(seed, network) {
|
|
|
1674
1852
|
try {
|
|
1675
1853
|
return bip32.fromSeed(normalizedSeed, versions);
|
|
1676
1854
|
} catch (error) {
|
|
1677
|
-
throw new CryptoError(
|
|
1855
|
+
throw new CryptoError(
|
|
1856
|
+
"Failed to create BIP32 root node from seed",
|
|
1857
|
+
error
|
|
1858
|
+
);
|
|
1678
1859
|
}
|
|
1679
1860
|
}
|
|
1680
1861
|
var DEFAULT_RELATIVE_PATH = "0/0";
|
|
@@ -1699,7 +1880,9 @@ async function signMessage(params) {
|
|
|
1699
1880
|
throw new CryptoError("Schnorr signing not supported by ECC module");
|
|
1700
1881
|
}
|
|
1701
1882
|
const messageHash = await sha256(messageBytes);
|
|
1702
|
-
const signature = Buffer.from(
|
|
1883
|
+
const signature = Buffer.from(
|
|
1884
|
+
ecc.signSchnorr(messageHash, privateKey)
|
|
1885
|
+
).toString("base64");
|
|
1703
1886
|
return signature;
|
|
1704
1887
|
}
|
|
1705
1888
|
async function verifyMessage(params) {
|
|
@@ -1716,7 +1899,7 @@ async function verifyMessage(params) {
|
|
|
1716
1899
|
let accountNode;
|
|
1717
1900
|
try {
|
|
1718
1901
|
accountNode = factory(ecc).fromBase58(accountXpub, versions);
|
|
1719
|
-
} catch (
|
|
1902
|
+
} catch (_error) {
|
|
1720
1903
|
throw new ValidationError("Invalid account xpub provided", "accountXpub");
|
|
1721
1904
|
}
|
|
1722
1905
|
const child = accountNode.derivePath(relativePath);
|
|
@@ -1759,11 +1942,7 @@ function deriveVssSigningKeyFromMnemonic(mnemonic) {
|
|
|
1759
1942
|
return Array.from(digest).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1760
1943
|
}
|
|
1761
1944
|
var restoreFromBackup = (params) => {
|
|
1762
|
-
const {
|
|
1763
|
-
backupFilePath,
|
|
1764
|
-
password,
|
|
1765
|
-
dataDir
|
|
1766
|
-
} = params;
|
|
1945
|
+
const { backupFilePath, password, dataDir } = params;
|
|
1767
1946
|
if (!backupFilePath) {
|
|
1768
1947
|
throw new ValidationError("backup file is required", "backup");
|
|
1769
1948
|
}
|
|
@@ -1792,7 +1971,10 @@ var WalletManager = class {
|
|
|
1792
1971
|
throw new ValidationError("xpubCol is required", "xpubCol");
|
|
1793
1972
|
}
|
|
1794
1973
|
if (!params.masterFingerprint) {
|
|
1795
|
-
throw new ValidationError(
|
|
1974
|
+
throw new ValidationError(
|
|
1975
|
+
"masterFingerprint is required",
|
|
1976
|
+
"masterFingerprint"
|
|
1977
|
+
);
|
|
1796
1978
|
}
|
|
1797
1979
|
this.network = normalizeNetwork(params.network ?? "regtest");
|
|
1798
1980
|
this.xpubVan = params.xpubVan;
|
|
@@ -1801,7 +1983,12 @@ var WalletManager = class {
|
|
|
1801
1983
|
this.mnemonic = params.mnemonic ?? null;
|
|
1802
1984
|
this.xpub = params.xpub ?? null;
|
|
1803
1985
|
this.masterFingerprint = params.masterFingerprint;
|
|
1804
|
-
this.dataDir = params.dataDir ?? path3__default.join(
|
|
1986
|
+
this.dataDir = params.dataDir ?? path3__default.join(
|
|
1987
|
+
process.cwd(),
|
|
1988
|
+
".rgb-wallet",
|
|
1989
|
+
this.network,
|
|
1990
|
+
this.masterFingerprint
|
|
1991
|
+
);
|
|
1805
1992
|
this.client = new RGBLibClient({
|
|
1806
1993
|
xpubVan: params.xpubVan,
|
|
1807
1994
|
xpubCol: params.xpubCol,
|
|
@@ -1815,7 +2002,7 @@ var WalletManager = class {
|
|
|
1815
2002
|
async initialize() {
|
|
1816
2003
|
console.log("initializing is not reqire");
|
|
1817
2004
|
}
|
|
1818
|
-
async goOnline(
|
|
2005
|
+
async goOnline() {
|
|
1819
2006
|
this.client.getOnline();
|
|
1820
2007
|
}
|
|
1821
2008
|
/**
|
|
@@ -2098,7 +2285,9 @@ var WalletManager = class {
|
|
|
2098
2285
|
if (this.seed) {
|
|
2099
2286
|
return await signPsbtFromSeed(this.seed, psbt, this.network);
|
|
2100
2287
|
}
|
|
2101
|
-
throw new WalletError(
|
|
2288
|
+
throw new WalletError(
|
|
2289
|
+
"mnemonic is required. Provide it as parameter or initialize wallet with mnemonic."
|
|
2290
|
+
);
|
|
2102
2291
|
}
|
|
2103
2292
|
/**
|
|
2104
2293
|
* Complete send operation: begin → sign → end
|
|
@@ -2111,7 +2300,12 @@ var WalletManager = class {
|
|
|
2111
2300
|
const signedPsbt = await this.signPsbt(psbt, mnemonic);
|
|
2112
2301
|
return await this.sendEnd({ signedPsbt });
|
|
2113
2302
|
}
|
|
2114
|
-
async createUtxos({
|
|
2303
|
+
async createUtxos({
|
|
2304
|
+
upTo,
|
|
2305
|
+
num,
|
|
2306
|
+
size,
|
|
2307
|
+
feeRate
|
|
2308
|
+
}) {
|
|
2115
2309
|
this.ensureNotDisposed();
|
|
2116
2310
|
const psbt = await this.createUtxosBegin({ upTo, num, size, feeRate });
|
|
2117
2311
|
const signedPsbt = await this.signPsbt(psbt);
|
|
@@ -2126,7 +2320,9 @@ var WalletManager = class {
|
|
|
2126
2320
|
throw new ValidationError("message is required", "message");
|
|
2127
2321
|
}
|
|
2128
2322
|
if (!this.seed) {
|
|
2129
|
-
throw new WalletError(
|
|
2323
|
+
throw new WalletError(
|
|
2324
|
+
"Wallet seed is required for message signing. Initialize the wallet with a seed."
|
|
2325
|
+
);
|
|
2130
2326
|
}
|
|
2131
2327
|
return signMessage({
|
|
2132
2328
|
message,
|
|
@@ -2144,7 +2340,7 @@ var WalletManager = class {
|
|
|
2144
2340
|
return verifyMessage({
|
|
2145
2341
|
message,
|
|
2146
2342
|
signature,
|
|
2147
|
-
accountXpub: this.xpubVan,
|
|
2343
|
+
accountXpub: accountXpub ?? this.xpubVan,
|
|
2148
2344
|
network: this.network
|
|
2149
2345
|
});
|
|
2150
2346
|
}
|
|
@@ -2164,25 +2360,25 @@ var wallet = new Proxy({}, {
|
|
|
2164
2360
|
|
|
2165
2361
|
// src/utexo/utexo-protocol.ts
|
|
2166
2362
|
var LightningProtocol = class {
|
|
2167
|
-
async createLightningInvoice(
|
|
2363
|
+
async createLightningInvoice(_params) {
|
|
2168
2364
|
throw new Error("createLightningInvoice not implemented");
|
|
2169
2365
|
}
|
|
2170
|
-
async getLightningReceiveRequest(
|
|
2366
|
+
async getLightningReceiveRequest(_id) {
|
|
2171
2367
|
throw new Error("getLightningReceiveRequest not implemented");
|
|
2172
2368
|
}
|
|
2173
|
-
async getLightningSendRequest(
|
|
2369
|
+
async getLightningSendRequest(_id) {
|
|
2174
2370
|
throw new Error("getLightningSendRequest not implemented");
|
|
2175
2371
|
}
|
|
2176
|
-
async getLightningSendFeeEstimate(
|
|
2372
|
+
async getLightningSendFeeEstimate(_params) {
|
|
2177
2373
|
throw new Error("getLightningSendFeeEstimate not implemented");
|
|
2178
2374
|
}
|
|
2179
|
-
async payLightningInvoiceBegin(
|
|
2375
|
+
async payLightningInvoiceBegin(_params) {
|
|
2180
2376
|
throw new Error("payLightningInvoiceBegin not implemented");
|
|
2181
2377
|
}
|
|
2182
|
-
async payLightningInvoiceEnd(
|
|
2378
|
+
async payLightningInvoiceEnd(_params) {
|
|
2183
2379
|
throw new Error("payLightningInvoiceEnd not implemented");
|
|
2184
2380
|
}
|
|
2185
|
-
async payLightningInvoice(
|
|
2381
|
+
async payLightningInvoice(_params, _mnemonic) {
|
|
2186
2382
|
throw new Error("payLightningInvoice not implemented");
|
|
2187
2383
|
}
|
|
2188
2384
|
async listLightningPayments() {
|
|
@@ -2190,22 +2386,22 @@ var LightningProtocol = class {
|
|
|
2190
2386
|
}
|
|
2191
2387
|
};
|
|
2192
2388
|
var OnchainProtocol = class {
|
|
2193
|
-
async onchainReceive(
|
|
2389
|
+
async onchainReceive(_params) {
|
|
2194
2390
|
throw new Error("onchainReceive not implemented");
|
|
2195
2391
|
}
|
|
2196
|
-
async onchainSendBegin(
|
|
2392
|
+
async onchainSendBegin(_params) {
|
|
2197
2393
|
throw new Error("onchainSendBegin not implemented");
|
|
2198
2394
|
}
|
|
2199
|
-
async onchainSendEnd(
|
|
2395
|
+
async onchainSendEnd(_params) {
|
|
2200
2396
|
throw new Error("onchainSendEnd not implemented");
|
|
2201
2397
|
}
|
|
2202
|
-
async onchainSend(
|
|
2398
|
+
async onchainSend(_params, _mnemonic) {
|
|
2203
2399
|
throw new Error("onchainSend not implemented");
|
|
2204
2400
|
}
|
|
2205
|
-
async getOnchainSendStatus(
|
|
2401
|
+
async getOnchainSendStatus(_send_id) {
|
|
2206
2402
|
throw new Error("getOnchainSendStatus not implemented");
|
|
2207
2403
|
}
|
|
2208
|
-
async listOnchainTransfers(
|
|
2404
|
+
async listOnchainTransfers(_asset_id) {
|
|
2209
2405
|
throw new Error("listOnchainTransfers not implemented");
|
|
2210
2406
|
}
|
|
2211
2407
|
};
|
|
@@ -2236,7 +2432,7 @@ var UTEXOProtocol = class extends LightningProtocol {
|
|
|
2236
2432
|
|
|
2237
2433
|
// src/utexo/config/gateway.ts
|
|
2238
2434
|
var DEFAULT_GATEWAY_BASE_URLS = {
|
|
2239
|
-
mainnet: "https://gateway.utexo.
|
|
2435
|
+
mainnet: "https://gateway.utexo.utexo.com/",
|
|
2240
2436
|
testnet: "https://dev.gateway.utexo.tricorn.network/"
|
|
2241
2437
|
};
|
|
2242
2438
|
|
|
@@ -2261,19 +2457,19 @@ var encodeTransferStatus = (transferStatus) => {
|
|
|
2261
2457
|
var UtexoBridgeApiClient = class {
|
|
2262
2458
|
/**
|
|
2263
2459
|
* Creates a new UtexoBridgeApiClient instance
|
|
2264
|
-
*
|
|
2460
|
+
*
|
|
2265
2461
|
* @param axiosInstance - Axios instance to use for HTTP requests (required)
|
|
2266
2462
|
* @param basePath - Base path for API endpoints (defaults to '/v1/utexo/bridge')
|
|
2267
|
-
*
|
|
2463
|
+
*
|
|
2268
2464
|
* @example
|
|
2269
2465
|
* ```typescript
|
|
2270
2466
|
* import axios from 'axios';
|
|
2271
2467
|
* import { UtexoBridgeApiClient } from './utexoBridge';
|
|
2272
|
-
*
|
|
2468
|
+
*
|
|
2273
2469
|
* const axiosInstance = axios.create({
|
|
2274
2470
|
* baseURL: 'https://api.example.com'
|
|
2275
2471
|
* });
|
|
2276
|
-
*
|
|
2472
|
+
*
|
|
2277
2473
|
* const client = new UtexoBridgeApiClient(axiosInstance);
|
|
2278
2474
|
* ```
|
|
2279
2475
|
*/
|
|
@@ -2283,7 +2479,7 @@ var UtexoBridgeApiClient = class {
|
|
|
2283
2479
|
}
|
|
2284
2480
|
/**
|
|
2285
2481
|
* Gets bridge-in signature for a transfer
|
|
2286
|
-
*
|
|
2482
|
+
*
|
|
2287
2483
|
* @param request - Bridge-in signature request data
|
|
2288
2484
|
* @returns Promise resolving to bridge-in signature response
|
|
2289
2485
|
* @throws {ApiError} If the request fails
|
|
@@ -2306,7 +2502,7 @@ var UtexoBridgeApiClient = class {
|
|
|
2306
2502
|
}
|
|
2307
2503
|
/**
|
|
2308
2504
|
* Submits a signed transaction to the blockchain
|
|
2309
|
-
*
|
|
2505
|
+
*
|
|
2310
2506
|
* @param request - Submit transaction request data
|
|
2311
2507
|
* @returns Promise resolving to transaction hash
|
|
2312
2508
|
* @throws {ApiError} If the request fails
|
|
@@ -2320,7 +2516,7 @@ var UtexoBridgeApiClient = class {
|
|
|
2320
2516
|
}
|
|
2321
2517
|
/**
|
|
2322
2518
|
* Verifies a bridge-in transaction after it has been sent
|
|
2323
|
-
*
|
|
2519
|
+
*
|
|
2324
2520
|
* @param request - Verify bridge-in request data
|
|
2325
2521
|
* @returns Promise that resolves when verification is complete
|
|
2326
2522
|
* @throws {ApiError} If the request fails
|
|
@@ -2330,7 +2526,7 @@ var UtexoBridgeApiClient = class {
|
|
|
2330
2526
|
}
|
|
2331
2527
|
/**
|
|
2332
2528
|
* Gets receiver invoice by transfer ID and network ID
|
|
2333
|
-
*
|
|
2529
|
+
*
|
|
2334
2530
|
* @param transferId - Transfer ID
|
|
2335
2531
|
* @param networkId - Network ID
|
|
2336
2532
|
* @returns Promise resolving to invoice string
|
|
@@ -2343,21 +2539,21 @@ var UtexoBridgeApiClient = class {
|
|
|
2343
2539
|
return data.invoice;
|
|
2344
2540
|
}
|
|
2345
2541
|
async getWithdrawTransfer(invoice, networkId) {
|
|
2346
|
-
const { data } = await this.axios.get(
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
"limit": String(10),
|
|
2353
|
-
"address": "rgb-address"
|
|
2354
|
-
}
|
|
2542
|
+
const { data } = await this.axios.get(`${this.basePath}/transfers/history`, {
|
|
2543
|
+
params: {
|
|
2544
|
+
network_id: String(networkId),
|
|
2545
|
+
offset: String(0),
|
|
2546
|
+
limit: String(10),
|
|
2547
|
+
address: "rgb-address"
|
|
2355
2548
|
}
|
|
2356
|
-
);
|
|
2549
|
+
});
|
|
2357
2550
|
if (data.transfers.length === 0) {
|
|
2358
2551
|
return null;
|
|
2359
2552
|
}
|
|
2360
|
-
const withdrawTransfer = data.transfers.map((transfer) => ({
|
|
2553
|
+
const withdrawTransfer = data.transfers.map((transfer) => ({
|
|
2554
|
+
...transfer,
|
|
2555
|
+
status: TransferStatuses[encodeTransferStatus(transfer.status)]
|
|
2556
|
+
})).find((transfer) => transfer.recipient.address === invoice);
|
|
2361
2557
|
if (!withdrawTransfer) {
|
|
2362
2558
|
return null;
|
|
2363
2559
|
}
|
|
@@ -2365,7 +2561,7 @@ var UtexoBridgeApiClient = class {
|
|
|
2365
2561
|
}
|
|
2366
2562
|
/**
|
|
2367
2563
|
* Gets transfer information by mainnet invoice
|
|
2368
|
-
*
|
|
2564
|
+
*
|
|
2369
2565
|
* @param mainnetInvoice - Mainnet invoice string
|
|
2370
2566
|
* @param networkId - Network ID
|
|
2371
2567
|
* @returns Promise resolving to transfer information
|
|
@@ -2383,10 +2579,13 @@ var UtexoBridgeApiClient = class {
|
|
|
2383
2579
|
}
|
|
2384
2580
|
);
|
|
2385
2581
|
if (data) {
|
|
2386
|
-
return {
|
|
2582
|
+
return {
|
|
2583
|
+
...data,
|
|
2584
|
+
status: TransferStatuses[encodeTransferStatus(data.status)]
|
|
2585
|
+
};
|
|
2387
2586
|
}
|
|
2388
2587
|
return data;
|
|
2389
|
-
} catch (
|
|
2588
|
+
} catch (_error) {
|
|
2390
2589
|
console.log("Mainnet invoice not found");
|
|
2391
2590
|
return null;
|
|
2392
2591
|
}
|
|
@@ -2456,12 +2655,25 @@ function prepareUtxoBackupDirs(backupPath, masterFingerprint) {
|
|
|
2456
2655
|
storeId,
|
|
2457
2656
|
layer1TmpDir,
|
|
2458
2657
|
utexoTmpDir,
|
|
2459
|
-
layer1FinalPath: path3__default.join(
|
|
2460
|
-
|
|
2658
|
+
layer1FinalPath: path3__default.join(
|
|
2659
|
+
backupPath,
|
|
2660
|
+
`${storeId}_layer1${BACKUP_FILE_SUFFIX}`
|
|
2661
|
+
),
|
|
2662
|
+
utexoFinalPath: path3__default.join(
|
|
2663
|
+
backupPath,
|
|
2664
|
+
`${storeId}_utexo${BACKUP_FILE_SUFFIX}`
|
|
2665
|
+
)
|
|
2461
2666
|
};
|
|
2462
2667
|
}
|
|
2463
2668
|
function finalizeUtxoBackupPaths(params) {
|
|
2464
|
-
const {
|
|
2669
|
+
const {
|
|
2670
|
+
layer1BackupPath,
|
|
2671
|
+
utexoBackupPath,
|
|
2672
|
+
layer1FinalPath,
|
|
2673
|
+
utexoFinalPath,
|
|
2674
|
+
layer1TmpDir,
|
|
2675
|
+
utexoTmpDir
|
|
2676
|
+
} = params;
|
|
2465
2677
|
fs2__default.renameSync(layer1BackupPath, layer1FinalPath);
|
|
2466
2678
|
fs2__default.renameSync(utexoBackupPath, utexoFinalPath);
|
|
2467
2679
|
fs2__default.rmdirSync(layer1TmpDir);
|
|
@@ -2477,7 +2689,13 @@ async function buildVssConfigFromMnemonic(mnemonic, serverUrl, networkPreset = "
|
|
|
2477
2689
|
};
|
|
2478
2690
|
}
|
|
2479
2691
|
async function restoreUtxoWalletFromVss(params) {
|
|
2480
|
-
const {
|
|
2692
|
+
const {
|
|
2693
|
+
mnemonic,
|
|
2694
|
+
targetDir,
|
|
2695
|
+
config: providedConfig,
|
|
2696
|
+
networkPreset = "testnet",
|
|
2697
|
+
vssServerUrl
|
|
2698
|
+
} = params;
|
|
2481
2699
|
if (!mnemonic || !mnemonic.trim()) {
|
|
2482
2700
|
throw new ValidationError("mnemonic is required", "mnemonic");
|
|
2483
2701
|
}
|
|
@@ -2485,13 +2703,23 @@ async function restoreUtxoWalletFromVss(params) {
|
|
|
2485
2703
|
throw new ValidationError("targetDir is required", "targetDir");
|
|
2486
2704
|
}
|
|
2487
2705
|
const serverUrl = vssServerUrl ?? DEFAULT_VSS_SERVER_URL;
|
|
2488
|
-
const config = providedConfig ?? await buildVssConfigFromMnemonic(
|
|
2706
|
+
const config = providedConfig ?? await buildVssConfigFromMnemonic(
|
|
2707
|
+
mnemonic.trim(),
|
|
2708
|
+
serverUrl,
|
|
2709
|
+
networkPreset
|
|
2710
|
+
);
|
|
2489
2711
|
const presetConfig = getUtxoNetworkConfig(networkPreset);
|
|
2490
2712
|
const layer1Network = String(presetConfig.networkMap.mainnet);
|
|
2491
2713
|
const utexoNetwork = String(presetConfig.networkMap.utexo);
|
|
2492
2714
|
const masterFingerprint = config.storeId.replace(/^wallet_/, "") || config.storeId;
|
|
2493
|
-
const layer1Config = {
|
|
2494
|
-
|
|
2715
|
+
const layer1Config = {
|
|
2716
|
+
...config,
|
|
2717
|
+
storeId: `${config.storeId}_layer1`
|
|
2718
|
+
};
|
|
2719
|
+
const utexoConfig = {
|
|
2720
|
+
...config,
|
|
2721
|
+
storeId: `${config.storeId}_utexo`
|
|
2722
|
+
};
|
|
2495
2723
|
const { walletPath: layer1Path } = restoreFromVss({
|
|
2496
2724
|
config: layer1Config,
|
|
2497
2725
|
targetDir: path3__default.join(targetDir, layer1Network, masterFingerprint)
|
|
@@ -2505,10 +2733,16 @@ async function restoreUtxoWalletFromVss(params) {
|
|
|
2505
2733
|
function restoreUtxoWalletFromBackup(params) {
|
|
2506
2734
|
const { backupPath, password, targetDir, networkPreset = "testnet" } = params;
|
|
2507
2735
|
if (!backupPath || !password || !targetDir) {
|
|
2508
|
-
throw new ValidationError(
|
|
2736
|
+
throw new ValidationError(
|
|
2737
|
+
"backupPath, password, and targetDir are required",
|
|
2738
|
+
"restoreUtxoWalletFromBackup"
|
|
2739
|
+
);
|
|
2509
2740
|
}
|
|
2510
2741
|
if (!fs2__default.existsSync(backupPath) || !fs2__default.statSync(backupPath).isDirectory()) {
|
|
2511
|
-
throw new ValidationError(
|
|
2742
|
+
throw new ValidationError(
|
|
2743
|
+
"backupPath must be an existing directory",
|
|
2744
|
+
"backupPath"
|
|
2745
|
+
);
|
|
2512
2746
|
}
|
|
2513
2747
|
const files = fs2__default.readdirSync(backupPath);
|
|
2514
2748
|
const layer1File = files.find((f) => f.endsWith(LAYER1_BACKUP_SUFFIX));
|
|
@@ -2545,8 +2779,16 @@ function restoreUtxoWalletFromBackup(params) {
|
|
|
2545
2779
|
fs2__default.mkdirSync(dir, { recursive: true });
|
|
2546
2780
|
}
|
|
2547
2781
|
}
|
|
2548
|
-
restoreWallet({
|
|
2549
|
-
|
|
2782
|
+
restoreWallet({
|
|
2783
|
+
backupFilePath: layer1BackupFile,
|
|
2784
|
+
password,
|
|
2785
|
+
dataDir: layer1DataDir
|
|
2786
|
+
});
|
|
2787
|
+
restoreWallet({
|
|
2788
|
+
backupFilePath: utexoBackupFile,
|
|
2789
|
+
password,
|
|
2790
|
+
dataDir: utexoDataDir
|
|
2791
|
+
});
|
|
2550
2792
|
return {
|
|
2551
2793
|
layer1Path: layer1DataDir,
|
|
2552
2794
|
utexoPath: utexoDataDir,
|
|
@@ -2569,7 +2811,7 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2569
2811
|
this.utexoRGBWallet = null;
|
|
2570
2812
|
this.mnemonicOrSeed = mnemonicOrSeed;
|
|
2571
2813
|
this.options = options;
|
|
2572
|
-
const preset = options.network ?? "
|
|
2814
|
+
const preset = options.network ?? "mainnet";
|
|
2573
2815
|
const networkConfig = getUtxoNetworkConfig(preset);
|
|
2574
2816
|
this.networkMap = networkConfig.networkMap;
|
|
2575
2817
|
this.networkIdMap = networkConfig.networkIdMap;
|
|
@@ -2590,7 +2832,7 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2590
2832
|
});
|
|
2591
2833
|
this.layer1RGBWallet = new WalletManager({
|
|
2592
2834
|
xpubVan: this.layer1Keys.accountXpubVanilla,
|
|
2593
|
-
xpubCol: this.layer1Keys.
|
|
2835
|
+
xpubCol: this.layer1Keys.accountXpubColored,
|
|
2594
2836
|
masterFingerprint: this.layer1Keys.masterFingerprint,
|
|
2595
2837
|
network: this.networkMap.mainnet,
|
|
2596
2838
|
mnemonic: this.mnemonicOrSeed,
|
|
@@ -2599,12 +2841,15 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2599
2841
|
}
|
|
2600
2842
|
/**
|
|
2601
2843
|
* Derive public keys from mnemonic or seed
|
|
2602
|
-
* @param network - BitcoinNetwork identifier
|
|
2844
|
+
* @param network - BitcoinNetwork identifier
|
|
2603
2845
|
* @returns Promise resolving to PublicKeys containing xpub, accountXpubVanilla, accountXpubColored, and masterFingerprint
|
|
2604
2846
|
* @throws {ValidationError} If mnemonic is invalid
|
|
2605
2847
|
*/
|
|
2606
2848
|
async derivePublicKeys(network) {
|
|
2607
|
-
const generatedKeys = await deriveKeysFromMnemonicOrSeed(
|
|
2849
|
+
const generatedKeys = await deriveKeysFromMnemonicOrSeed(
|
|
2850
|
+
network,
|
|
2851
|
+
this.mnemonicOrSeed
|
|
2852
|
+
);
|
|
2608
2853
|
const { xpub, accountXpubVanilla, accountXpubColored, masterFingerprint } = generatedKeys;
|
|
2609
2854
|
return { xpub, accountXpubVanilla, accountXpubColored, masterFingerprint };
|
|
2610
2855
|
}
|
|
@@ -2626,7 +2871,7 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2626
2871
|
// ==========================================
|
|
2627
2872
|
// IWalletManager Implementation
|
|
2628
2873
|
// ==========================================
|
|
2629
|
-
async goOnline(
|
|
2874
|
+
async goOnline() {
|
|
2630
2875
|
this.ensureInitialized();
|
|
2631
2876
|
throw new Error("goOnline not implemented");
|
|
2632
2877
|
}
|
|
@@ -2778,12 +3023,21 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2778
3023
|
this.ensureInitialized();
|
|
2779
3024
|
const { backupPath, password } = params;
|
|
2780
3025
|
if (!backupPath || !password) {
|
|
2781
|
-
throw new ValidationError(
|
|
3026
|
+
throw new ValidationError(
|
|
3027
|
+
"backupPath and password are required",
|
|
3028
|
+
"createBackup"
|
|
3029
|
+
);
|
|
2782
3030
|
}
|
|
2783
3031
|
const fp = this.utexoKeys.masterFingerprint;
|
|
2784
3032
|
const { layer1TmpDir, utexoTmpDir, layer1FinalPath, utexoFinalPath } = prepareUtxoBackupDirs(backupPath, fp);
|
|
2785
|
-
const layer1Result = await this.layer1RGBWallet.createBackup({
|
|
2786
|
-
|
|
3033
|
+
const layer1Result = await this.layer1RGBWallet.createBackup({
|
|
3034
|
+
backupPath: layer1TmpDir,
|
|
3035
|
+
password
|
|
3036
|
+
});
|
|
3037
|
+
const utexoResult = await this.utexoRGBWallet.createBackup({
|
|
3038
|
+
backupPath: utexoTmpDir,
|
|
3039
|
+
password
|
|
3040
|
+
});
|
|
2787
3041
|
finalizeUtxoBackupPaths({
|
|
2788
3042
|
layer1BackupPath: layer1Result.backupPath,
|
|
2789
3043
|
utexoBackupPath: utexoResult.backupPath,
|
|
@@ -2833,7 +3087,11 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2833
3087
|
}
|
|
2834
3088
|
const serverUrl = this.options.vssServerUrl ?? DEFAULT_VSS_SERVER_URL;
|
|
2835
3089
|
const preset = this.options.network ?? "mainnet";
|
|
2836
|
-
vssConfig = await buildVssConfigFromMnemonic(
|
|
3090
|
+
vssConfig = await buildVssConfigFromMnemonic(
|
|
3091
|
+
mnemonicToUse.trim(),
|
|
3092
|
+
serverUrl,
|
|
3093
|
+
preset
|
|
3094
|
+
);
|
|
2837
3095
|
}
|
|
2838
3096
|
const { layer1, utexo } = getVssConfigs(vssConfig);
|
|
2839
3097
|
await this.layer1RGBWallet.vssBackup(layer1);
|
|
@@ -2852,11 +3110,18 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2852
3110
|
} else {
|
|
2853
3111
|
const mnemonicToUse = mnemonic ?? (typeof this.mnemonicOrSeed === "string" ? this.mnemonicOrSeed : null);
|
|
2854
3112
|
if (!mnemonicToUse) {
|
|
2855
|
-
throw new ValidationError(
|
|
3113
|
+
throw new ValidationError(
|
|
3114
|
+
"config or mnemonic required for vssBackupInfo",
|
|
3115
|
+
"config"
|
|
3116
|
+
);
|
|
2856
3117
|
}
|
|
2857
3118
|
const serverUrl = this.options.vssServerUrl ?? DEFAULT_VSS_SERVER_URL;
|
|
2858
3119
|
const preset = this.options.network ?? "mainnet";
|
|
2859
|
-
vssConfig = await buildVssConfigFromMnemonic(
|
|
3120
|
+
vssConfig = await buildVssConfigFromMnemonic(
|
|
3121
|
+
mnemonicToUse.trim(),
|
|
3122
|
+
serverUrl,
|
|
3123
|
+
preset
|
|
3124
|
+
);
|
|
2860
3125
|
}
|
|
2861
3126
|
const { utexo } = getVssConfigs(vssConfig);
|
|
2862
3127
|
return this.utexoRGBWallet.vssBackupInfo(utexo);
|
|
@@ -2874,23 +3139,26 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2874
3139
|
return this.utexoRGBWallet.verifyMessage(message, signature, accountXpub);
|
|
2875
3140
|
}
|
|
2876
3141
|
/**
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
3142
|
+
* Validates that the wallet has sufficient spendable balance for the given asset and amount.
|
|
3143
|
+
* @param assetId - Asset ID to check balance for
|
|
3144
|
+
* @param amount - Required amount (in asset units)
|
|
3145
|
+
* @throws {ValidationError} If balance is not found or insufficient
|
|
3146
|
+
*/
|
|
2882
3147
|
async validateBalance(assetId, amount) {
|
|
2883
3148
|
const assetBalance = await this.getAssetBalance(assetId);
|
|
2884
3149
|
if (!assetBalance || !assetBalance.spendable) {
|
|
2885
3150
|
throw new ValidationError("Asset balance is not found", "assetBalance");
|
|
2886
3151
|
}
|
|
2887
3152
|
if (assetBalance.spendable < amount) {
|
|
2888
|
-
throw new ValidationError(
|
|
3153
|
+
throw new ValidationError(
|
|
3154
|
+
`Insufficient balance ${assetBalance.spendable} < ${amount}`,
|
|
3155
|
+
"amount"
|
|
3156
|
+
);
|
|
2889
3157
|
}
|
|
2890
3158
|
}
|
|
2891
3159
|
/**
|
|
2892
3160
|
* Extracts invoice data and destination asset from a bridge transfer.
|
|
2893
|
-
*
|
|
3161
|
+
*
|
|
2894
3162
|
* @param bridgeTransfer - Bridge transfer response containing recipient invoice and token info
|
|
2895
3163
|
* @returns Object containing invoice string, decoded invoice data, and destination asset
|
|
2896
3164
|
* @throws {ValidationError} If destination asset is not supported
|
|
@@ -2898,9 +3166,14 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2898
3166
|
async extractInvoiceAndAsset(bridgeTransfer) {
|
|
2899
3167
|
const utexoInvoice = bridgeTransfer.recipient.address;
|
|
2900
3168
|
const invoiceData = await this.decodeRGBInvoice({ invoice: utexoInvoice });
|
|
2901
|
-
const destinationAsset = this.networkIdMap.utexo.getAssetById(
|
|
3169
|
+
const destinationAsset = this.networkIdMap.utexo.getAssetById(
|
|
3170
|
+
bridgeTransfer.recipientToken.id
|
|
3171
|
+
);
|
|
2902
3172
|
if (!destinationAsset) {
|
|
2903
|
-
throw new ValidationError(
|
|
3173
|
+
throw new ValidationError(
|
|
3174
|
+
"Destination asset is not supported",
|
|
3175
|
+
"assetId"
|
|
3176
|
+
);
|
|
2904
3177
|
}
|
|
2905
3178
|
return { utexoInvoice, invoiceData, destinationAsset };
|
|
2906
3179
|
}
|
|
@@ -2909,9 +3182,17 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2909
3182
|
*/
|
|
2910
3183
|
async onchainReceive(params) {
|
|
2911
3184
|
this.ensureInitialized();
|
|
2912
|
-
const destinationAsset = getDestinationAsset(
|
|
3185
|
+
const destinationAsset = getDestinationAsset(
|
|
3186
|
+
"mainnet",
|
|
3187
|
+
"utexo",
|
|
3188
|
+
params.assetId ?? null,
|
|
3189
|
+
this.networkIdMap
|
|
3190
|
+
);
|
|
2913
3191
|
if (!destinationAsset) {
|
|
2914
|
-
throw new ValidationError(
|
|
3192
|
+
throw new ValidationError(
|
|
3193
|
+
"Destination asset is not supported",
|
|
3194
|
+
"assetId"
|
|
3195
|
+
);
|
|
2915
3196
|
}
|
|
2916
3197
|
if (!params.amount) {
|
|
2917
3198
|
throw new ValidationError("Amount is required", "amount");
|
|
@@ -2945,7 +3226,10 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2945
3226
|
}
|
|
2946
3227
|
async onchainSendBegin(params) {
|
|
2947
3228
|
this.ensureInitialized();
|
|
2948
|
-
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3229
|
+
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3230
|
+
params.invoice,
|
|
3231
|
+
this.networkIdMap.mainnet.networkId
|
|
3232
|
+
);
|
|
2949
3233
|
if (!bridgeTransfer) {
|
|
2950
3234
|
console.log("External invoice UTEXO -> Mainnet initiated");
|
|
2951
3235
|
return this.UTEXOToMainnetRGB(params);
|
|
@@ -2953,9 +3237,14 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2953
3237
|
const utexoInvoice = bridgeTransfer.recipient.address;
|
|
2954
3238
|
const invoiceData = await this.decodeRGBInvoice({ invoice: utexoInvoice });
|
|
2955
3239
|
const bridgeAmount = bridgeTransfer.recipientAmount;
|
|
2956
|
-
const destinationAsset = this.networkIdMap.utexo.getAssetById(
|
|
3240
|
+
const destinationAsset = this.networkIdMap.utexo.getAssetById(
|
|
3241
|
+
bridgeTransfer.recipientToken.id
|
|
3242
|
+
);
|
|
2957
3243
|
if (!destinationAsset) {
|
|
2958
|
-
throw new ValidationError(
|
|
3244
|
+
throw new ValidationError(
|
|
3245
|
+
"Destination asset is not supported",
|
|
3246
|
+
"assetId"
|
|
3247
|
+
);
|
|
2959
3248
|
}
|
|
2960
3249
|
const amount = toUnitsNumber(bridgeAmount, destinationAsset.precision);
|
|
2961
3250
|
const isWitness = invoiceData.recipientId.includes("wvout:");
|
|
@@ -2976,27 +3265,46 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
2976
3265
|
}
|
|
2977
3266
|
async onchainSendEnd(params) {
|
|
2978
3267
|
this.ensureInitialized();
|
|
2979
|
-
const sendResult = await this.utexoRGBWallet.sendEnd({
|
|
3268
|
+
const sendResult = await this.utexoRGBWallet.sendEnd({
|
|
3269
|
+
signedPsbt: params.signedPsbt
|
|
3270
|
+
});
|
|
2980
3271
|
return sendResult;
|
|
2981
3272
|
}
|
|
2982
3273
|
async onchainSend(params, mnemonic) {
|
|
2983
3274
|
this.ensureInitialized();
|
|
2984
3275
|
const psbt = await this.onchainSendBegin(params);
|
|
2985
3276
|
const signed_psbt = await this.utexoRGBWallet.signPsbt(psbt, mnemonic);
|
|
2986
|
-
return await this.onchainSendEnd({
|
|
3277
|
+
return await this.onchainSendEnd({
|
|
3278
|
+
signedPsbt: signed_psbt,
|
|
3279
|
+
invoice: params.invoice
|
|
3280
|
+
});
|
|
2987
3281
|
}
|
|
2988
3282
|
async getOnchainSendStatus(invoice) {
|
|
2989
|
-
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3283
|
+
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3284
|
+
invoice,
|
|
3285
|
+
this.networkIdMap.mainnet.networkId
|
|
3286
|
+
);
|
|
2990
3287
|
if (!bridgeTransfer) {
|
|
2991
|
-
const withdrawTransfer = await this.bridge.getWithdrawTransfer(
|
|
3288
|
+
const withdrawTransfer = await this.bridge.getWithdrawTransfer(
|
|
3289
|
+
invoice,
|
|
3290
|
+
this.networkIdMap.utexo.networkId
|
|
3291
|
+
);
|
|
2992
3292
|
if (!withdrawTransfer) {
|
|
2993
3293
|
return null;
|
|
2994
3294
|
}
|
|
2995
3295
|
return withdrawTransfer.status;
|
|
2996
3296
|
}
|
|
2997
3297
|
const { invoiceData, destinationAsset } = await this.extractInvoiceAndAsset(bridgeTransfer);
|
|
2998
|
-
const
|
|
2999
|
-
const
|
|
3298
|
+
const assets = await this.utexoRGBWallet.listAssets();
|
|
3299
|
+
const walletAsset = assets.nia.find(
|
|
3300
|
+
(a) => a.assetId === destinationAsset.assetId
|
|
3301
|
+
);
|
|
3302
|
+
const transfers = await this.utexoRGBWallet.listTransfers(
|
|
3303
|
+
walletAsset?.assetId
|
|
3304
|
+
);
|
|
3305
|
+
const transfer = transfers.find(
|
|
3306
|
+
(transfer2) => transfer2.recipientId === invoiceData.recipientId
|
|
3307
|
+
);
|
|
3000
3308
|
if (transfer) {
|
|
3001
3309
|
return transfer.status;
|
|
3002
3310
|
}
|
|
@@ -3021,9 +3329,17 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
3021
3329
|
if (!asset.amount) {
|
|
3022
3330
|
throw new ValidationError("Amount is required", "amount");
|
|
3023
3331
|
}
|
|
3024
|
-
const destinationAsset = getDestinationAsset(
|
|
3332
|
+
const destinationAsset = getDestinationAsset(
|
|
3333
|
+
"mainnet",
|
|
3334
|
+
"utexo",
|
|
3335
|
+
asset.assetId,
|
|
3336
|
+
this.networkIdMap
|
|
3337
|
+
);
|
|
3025
3338
|
if (!destinationAsset) {
|
|
3026
|
-
throw new ValidationError(
|
|
3339
|
+
throw new ValidationError(
|
|
3340
|
+
"Destination asset is not supported",
|
|
3341
|
+
"assetId"
|
|
3342
|
+
);
|
|
3027
3343
|
}
|
|
3028
3344
|
const destinationInvoice = await this.utexoRGBWallet.witnessReceive({
|
|
3029
3345
|
assetId: "",
|
|
@@ -3052,7 +3368,10 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
3052
3368
|
}
|
|
3053
3369
|
async payLightningInvoiceBegin(params) {
|
|
3054
3370
|
this.ensureInitialized();
|
|
3055
|
-
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3371
|
+
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3372
|
+
params.lnInvoice,
|
|
3373
|
+
this.networkIdMap.mainnetLightning.networkId
|
|
3374
|
+
);
|
|
3056
3375
|
if (!bridgeTransfer) {
|
|
3057
3376
|
console.log("External invoice UTEXO -> Mainnet Lightning initiated");
|
|
3058
3377
|
return this.UtexoToMainnetLightning(params);
|
|
@@ -3077,70 +3396,119 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
3077
3396
|
}
|
|
3078
3397
|
async payLightningInvoiceEnd(params) {
|
|
3079
3398
|
this.ensureInitialized();
|
|
3080
|
-
const sendResult = await this.utexoRGBWallet.sendEnd({
|
|
3399
|
+
const sendResult = await this.utexoRGBWallet.sendEnd({
|
|
3400
|
+
signedPsbt: params.signedPsbt
|
|
3401
|
+
});
|
|
3081
3402
|
return sendResult;
|
|
3082
3403
|
}
|
|
3083
3404
|
async payLightningInvoice(params, mnemonic) {
|
|
3084
3405
|
this.ensureInitialized();
|
|
3085
3406
|
const psbt = await this.payLightningInvoiceBegin(params);
|
|
3086
3407
|
const signed_psbt = await this.utexoRGBWallet.signPsbt(psbt, mnemonic);
|
|
3087
|
-
return await this.payLightningInvoiceEnd({
|
|
3408
|
+
return await this.payLightningInvoiceEnd({
|
|
3409
|
+
signedPsbt: signed_psbt,
|
|
3410
|
+
lnInvoice: params.lnInvoice
|
|
3411
|
+
});
|
|
3088
3412
|
}
|
|
3089
3413
|
async getLightningSendRequest(lnInvoice) {
|
|
3090
3414
|
this.ensureInitialized();
|
|
3091
|
-
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3415
|
+
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3416
|
+
lnInvoice,
|
|
3417
|
+
this.networkIdMap.mainnetLightning.networkId
|
|
3418
|
+
);
|
|
3092
3419
|
if (!bridgeTransfer) {
|
|
3093
|
-
const withdrawTransfer = await this.bridge.getWithdrawTransfer(
|
|
3420
|
+
const withdrawTransfer = await this.bridge.getWithdrawTransfer(
|
|
3421
|
+
lnInvoice,
|
|
3422
|
+
this.networkIdMap.utexo.networkId
|
|
3423
|
+
);
|
|
3094
3424
|
if (!withdrawTransfer) {
|
|
3095
3425
|
return null;
|
|
3096
3426
|
}
|
|
3097
3427
|
return withdrawTransfer.status;
|
|
3098
3428
|
}
|
|
3099
3429
|
const { invoiceData, destinationAsset } = await this.extractInvoiceAndAsset(bridgeTransfer);
|
|
3100
|
-
const transfers = await this.utexoRGBWallet.listTransfers(
|
|
3101
|
-
|
|
3430
|
+
const transfers = await this.utexoRGBWallet.listTransfers(
|
|
3431
|
+
destinationAsset.assetId
|
|
3432
|
+
);
|
|
3433
|
+
return transfers.length > 0 ? transfers.find(
|
|
3434
|
+
(transfer) => transfer.recipientId === invoiceData.recipientId
|
|
3435
|
+
)?.status ?? null : null;
|
|
3102
3436
|
}
|
|
3103
3437
|
async getLightningReceiveRequest(lnInvoice) {
|
|
3104
3438
|
this.ensureInitialized();
|
|
3105
|
-
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3439
|
+
const bridgeTransfer = await this.bridge.getTransferByMainnetInvoice(
|
|
3440
|
+
lnInvoice,
|
|
3441
|
+
this.networkIdMap.mainnetLightning.networkId
|
|
3442
|
+
);
|
|
3106
3443
|
if (!bridgeTransfer) {
|
|
3107
|
-
const withdrawTransfer = await this.bridge.getWithdrawTransfer(
|
|
3444
|
+
const withdrawTransfer = await this.bridge.getWithdrawTransfer(
|
|
3445
|
+
lnInvoice,
|
|
3446
|
+
this.networkIdMap.utexo.networkId
|
|
3447
|
+
);
|
|
3108
3448
|
if (!withdrawTransfer) {
|
|
3109
3449
|
return null;
|
|
3110
3450
|
}
|
|
3111
3451
|
return withdrawTransfer.status;
|
|
3112
3452
|
}
|
|
3113
3453
|
const { invoiceData, destinationAsset } = await this.extractInvoiceAndAsset(bridgeTransfer);
|
|
3114
|
-
const transfers = await this.utexoRGBWallet.listTransfers(
|
|
3115
|
-
|
|
3454
|
+
const transfers = await this.utexoRGBWallet.listTransfers(
|
|
3455
|
+
destinationAsset.assetId
|
|
3456
|
+
);
|
|
3457
|
+
return transfers.length > 0 ? transfers.find(
|
|
3458
|
+
(transfer) => transfer.recipientId === invoiceData.recipientId
|
|
3459
|
+
)?.status ?? null : null;
|
|
3116
3460
|
}
|
|
3117
3461
|
async UTEXOToMainnetRGB(params) {
|
|
3118
3462
|
this.ensureInitialized();
|
|
3119
|
-
const invoiceData = await this.decodeRGBInvoice({
|
|
3463
|
+
const invoiceData = await this.decodeRGBInvoice({
|
|
3464
|
+
invoice: params.invoice
|
|
3465
|
+
});
|
|
3120
3466
|
if (!params.assetId && !invoiceData.assetId) {
|
|
3121
|
-
throw new ValidationError(
|
|
3467
|
+
throw new ValidationError(
|
|
3468
|
+
"Asset ID is required for external invoice",
|
|
3469
|
+
"assetId"
|
|
3470
|
+
);
|
|
3122
3471
|
}
|
|
3123
3472
|
const assetId = params.assetId ?? invoiceData.assetId;
|
|
3124
|
-
const utexoAsset = getDestinationAsset(
|
|
3473
|
+
const utexoAsset = getDestinationAsset(
|
|
3474
|
+
"mainnet",
|
|
3475
|
+
"utexo",
|
|
3476
|
+
assetId ?? null,
|
|
3477
|
+
this.networkIdMap
|
|
3478
|
+
);
|
|
3125
3479
|
if (!utexoAsset) {
|
|
3126
3480
|
throw new ValidationError("UTEXO asset is not supported", "assetId");
|
|
3127
3481
|
}
|
|
3128
|
-
const destinationAsset = this.networkIdMap.mainnet.getAssetById(
|
|
3482
|
+
const destinationAsset = this.networkIdMap.mainnet.getAssetById(
|
|
3483
|
+
utexoAsset?.tokenId ?? 0
|
|
3484
|
+
);
|
|
3129
3485
|
if (!destinationAsset) {
|
|
3130
|
-
throw new ValidationError(
|
|
3486
|
+
throw new ValidationError(
|
|
3487
|
+
"Destination asset is not supported",
|
|
3488
|
+
"assetId"
|
|
3489
|
+
);
|
|
3131
3490
|
}
|
|
3132
3491
|
if (!params.amount && !invoiceData.assignment.amount) {
|
|
3133
|
-
throw new ValidationError(
|
|
3492
|
+
throw new ValidationError(
|
|
3493
|
+
"Amount is required for external invoice",
|
|
3494
|
+
"amount"
|
|
3495
|
+
);
|
|
3134
3496
|
}
|
|
3135
3497
|
let amount;
|
|
3136
3498
|
if (params.amount) {
|
|
3137
3499
|
amount = params.amount;
|
|
3138
3500
|
} else if (invoiceData.assignment.amount) {
|
|
3139
|
-
amount = fromUnitsNumber(
|
|
3501
|
+
amount = fromUnitsNumber(
|
|
3502
|
+
invoiceData.assignment.amount,
|
|
3503
|
+
destinationAsset.precision
|
|
3504
|
+
);
|
|
3140
3505
|
} else {
|
|
3141
3506
|
throw new ValidationError("Amount is required", "amount");
|
|
3142
3507
|
}
|
|
3143
|
-
await this.validateBalance(
|
|
3508
|
+
await this.validateBalance(
|
|
3509
|
+
utexoAsset.assetId,
|
|
3510
|
+
toUnitsNumber(amount.toString(), utexoAsset.precision)
|
|
3511
|
+
);
|
|
3144
3512
|
const payload = {
|
|
3145
3513
|
sender: {
|
|
3146
3514
|
address: "rgb-address",
|
|
@@ -3177,22 +3545,44 @@ var UTEXOWallet = class extends UTEXOProtocol {
|
|
|
3177
3545
|
async UtexoToMainnetLightning(params) {
|
|
3178
3546
|
this.ensureInitialized();
|
|
3179
3547
|
if (!params.assetId) {
|
|
3180
|
-
throw new ValidationError(
|
|
3548
|
+
throw new ValidationError(
|
|
3549
|
+
"Asset ID is required for external invoice",
|
|
3550
|
+
"assetId"
|
|
3551
|
+
);
|
|
3181
3552
|
}
|
|
3182
3553
|
const assetId = params.assetId;
|
|
3183
|
-
const utexoAsset = getDestinationAsset(
|
|
3184
|
-
|
|
3554
|
+
const utexoAsset = getDestinationAsset(
|
|
3555
|
+
"mainnet",
|
|
3556
|
+
"utexo",
|
|
3557
|
+
assetId ?? null,
|
|
3558
|
+
this.networkIdMap
|
|
3559
|
+
);
|
|
3560
|
+
const destinationAsset = this.networkIdMap.mainnet.getAssetById(
|
|
3561
|
+
utexoAsset?.tokenId ?? 0
|
|
3562
|
+
);
|
|
3185
3563
|
if (!destinationAsset) {
|
|
3186
|
-
throw new ValidationError(
|
|
3564
|
+
throw new ValidationError(
|
|
3565
|
+
"Destination asset is not supported",
|
|
3566
|
+
"assetId"
|
|
3567
|
+
);
|
|
3187
3568
|
}
|
|
3188
3569
|
if (!utexoAsset) {
|
|
3189
|
-
throw new ValidationError(
|
|
3570
|
+
throw new ValidationError(
|
|
3571
|
+
"Destination asset is not supported",
|
|
3572
|
+
"assetId"
|
|
3573
|
+
);
|
|
3190
3574
|
}
|
|
3191
3575
|
if (!params.amount) {
|
|
3192
|
-
throw new ValidationError(
|
|
3576
|
+
throw new ValidationError(
|
|
3577
|
+
"Amount is required for external invoice",
|
|
3578
|
+
"amount"
|
|
3579
|
+
);
|
|
3193
3580
|
}
|
|
3194
3581
|
const amount = params.amount;
|
|
3195
|
-
await this.validateBalance(
|
|
3582
|
+
await this.validateBalance(
|
|
3583
|
+
utexoAsset.assetId,
|
|
3584
|
+
toUnitsNumber(amount.toString(), utexoAsset.precision)
|
|
3585
|
+
);
|
|
3196
3586
|
const bridgeOutTransfer = await this.bridge.getBridgeInSignature({
|
|
3197
3587
|
sender: {
|
|
3198
3588
|
address: "rgb-address",
|