@omnisat/lasereyes 0.0.43 → 0.0.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -461,7 +461,12 @@ var findPaymentAddress = (addresses) => {
461
461
  };
462
462
  var getBTCBalance = (address2, network) => __async(void 0, null, function* () {
463
463
  try {
464
- const utxos = yield getAddressUtxos(address2, network);
464
+ const utxos = yield getAddressUtxos(
465
+ address2,
466
+ network
467
+ );
468
+ if (!utxos)
469
+ return 0;
465
470
  return utxos.reduce((acc, utxo) => acc + utxo.value, 0);
466
471
  } catch (error) {
467
472
  console.error("Error fetching BTC balance:", error);
@@ -493,13 +498,31 @@ function estimateTxSize(taprootInputCount, nonTaprootInputCount, outputCount) {
493
498
  }
494
499
  function getAddressUtxos(address2, network) {
495
500
  return __async(this, null, function* () {
501
+ if (address2.startsWith("t")) {
502
+ console.log("starts with t");
503
+ if (network === MAINNET) {
504
+ return [];
505
+ }
506
+ if (network === FRACTAL_MAINNET) {
507
+ return [];
508
+ }
509
+ if (network === FRACTAL_TESTNET) {
510
+ return [];
511
+ }
512
+ }
496
513
  return yield import_axios.default.get(`${getMempoolSpaceUrl2(network)}/api/address/${address2}/utxo`).then((response) => response.data);
497
514
  });
498
515
  }
499
516
  function createSendBtcPsbt(address2, paymentAddress, recipientAddress, amount, paymentPublicKey, network, feeRate = 7) {
500
517
  return __async(this, null, function* () {
501
518
  const isTaprootOnly = address2 === paymentAddress;
502
- const utxos = yield getAddressUtxos(paymentAddress, network);
519
+ const utxos = yield getAddressUtxos(
520
+ paymentAddress,
521
+ network
522
+ );
523
+ if (!utxos) {
524
+ throw new Error("No UTXOs found");
525
+ }
503
526
  const sortedUtxos = utxos.sort(
504
527
  (a, b) => b.value - a.value
505
528
  );
@@ -591,7 +614,6 @@ var LaserEyesProvider = ({
591
614
  children,
592
615
  config
593
616
  }) => {
594
- var _a;
595
617
  const selfRef = (0, import_react.useRef)({
596
618
  accounts: []
597
619
  });
@@ -615,11 +637,9 @@ var LaserEyesProvider = ({
615
637
  const [hasLeather, setHasLeather] = (0, import_react.useState)(false);
616
638
  const [hasPhantom, setHasPhantom] = (0, import_react.useState)(false);
617
639
  const [hasWizz, setHasWizz] = (0, import_react.useState)(false);
618
- const [network, setNetwork] = (0, import_usehooks_ts.useLocalStorage)("network", (_a = config == null ? void 0 : config.network) != null ? _a : MAINNET, {
619
- initializeWithValue: false
620
- });
640
+ const [network, setNetwork] = (0, import_usehooks_ts.useLocalStorage)("network", (config == null ? void 0 : config.network) || MAINNET);
621
641
  (0, import_react.useEffect)(() => {
622
- if (config) {
642
+ if (config && config.network) {
623
643
  setNetwork(config.network);
624
644
  getNetwork().then((foundNetwork) => {
625
645
  try {
@@ -652,8 +672,8 @@ var LaserEyesProvider = ({
652
672
  }, []);
653
673
  (0, import_react.useEffect)(() => {
654
674
  const observer = new MutationObserver(() => {
655
- var _a2;
656
- const xverseLib = (_a2 = window == null ? void 0 : window.XverseProviders) == null ? void 0 : _a2.BitcoinProvider;
675
+ var _a;
676
+ const xverseLib = (_a = window == null ? void 0 : window.XverseProviders) == null ? void 0 : _a.BitcoinProvider;
657
677
  if (xverseLib) {
658
678
  setHasXverse(true);
659
679
  observer.disconnect();
@@ -692,10 +712,10 @@ var LaserEyesProvider = ({
692
712
  }, []);
693
713
  (0, import_react.useEffect)(() => {
694
714
  const observer = new MutationObserver(() => {
695
- var _a2, _b;
715
+ var _a, _b;
696
716
  let foundOkx;
697
717
  if (network === TESTNET || network === TESTNET4 || network === SIGNET || network === FRACTAL_TESTNET) {
698
- foundOkx = (_a2 = window == null ? void 0 : window.okxwallet) == null ? void 0 : _a2.bitcoinTestnet;
718
+ foundOkx = (_a = window == null ? void 0 : window.okxwallet) == null ? void 0 : _a.bitcoinTestnet;
699
719
  } else if (network === MAINNET || network === FRACTAL_MAINNET) {
700
720
  foundOkx = (_b = window == null ? void 0 : window.okxwallet) == null ? void 0 : _b.bitcoin;
701
721
  }
@@ -724,8 +744,8 @@ var LaserEyesProvider = ({
724
744
  }, []);
725
745
  (0, import_react.useEffect)(() => {
726
746
  const observer = new MutationObserver(() => {
727
- var _a2;
728
- const phantomLib = (_a2 = window == null ? void 0 : window.phantom) == null ? void 0 : _a2.bitcoin;
747
+ var _a;
748
+ const phantomLib = (_a = window == null ? void 0 : window.phantom) == null ? void 0 : _a.bitcoin;
729
749
  if (phantomLib && phantomLib.isPhantom) {
730
750
  setHasPhantom(true);
731
751
  observer.disconnect();
@@ -933,15 +953,23 @@ var LaserEyesProvider = ({
933
953
  throw error;
934
954
  }
935
955
  });
936
- const connectOkx = () => __async(void 0, null, function* () {
956
+ const connectOkx = (0, import_react.useCallback)(() => __async(void 0, null, function* () {
957
+ var _a;
937
958
  try {
938
959
  localStorage == null ? void 0 : localStorage.setItem(LOCAL_STORAGE_DEFAULT_WALLET, OKX);
960
+ console.log("connecting", network);
939
961
  const lib = network === TESTNET || network === TESTNET4 || network === FRACTAL_TESTNET ? window.okxwallet.bitcoinTestnet : network === SIGNET ? window.okxwallet.bitcoinSignet : window.okxwallet.bitcoin;
940
962
  const okxAccounts = yield lib.connect();
941
963
  if (!okxAccounts)
942
964
  throw new Error("No accounts found");
943
965
  setAccounts([okxAccounts]);
944
966
  setAddress(okxAccounts.address);
967
+ if (((_a = String(okxAccounts.address)) == null ? void 0 : _a.startsWith("tb")) && network !== TESTNET && network !== TESTNET4 && network !== SIGNET) {
968
+ console.log("err");
969
+ throw new Error(
970
+ `Please switch networks to ${network} in the wallet settings.`
971
+ );
972
+ }
945
973
  setPaymentAddress(okxAccounts.address);
946
974
  setPublicKey(okxAccounts.publicKey);
947
975
  setPaymentPublicKey(okxAccounts.publicKey);
@@ -954,9 +982,9 @@ var LaserEyesProvider = ({
954
982
  console.log("error", error);
955
983
  throw error;
956
984
  }
957
- });
985
+ }), [hasOkx, network]);
958
986
  const connectLeather = (0, import_react.useCallback)(() => __async(void 0, null, function* () {
959
- var _a2;
987
+ var _a;
960
988
  try {
961
989
  localStorage == null ? void 0 : localStorage.setItem(LOCAL_STORAGE_DEFAULT_WALLET, LEATHER);
962
990
  const lib = window.LeatherProvider;
@@ -974,9 +1002,9 @@ var LaserEyesProvider = ({
974
1002
  const segwitAddress = addresses.find(
975
1003
  (address3) => address3.type === P2WPKH
976
1004
  );
977
- if (((_a2 = String(taprootAddress)) == null ? void 0 : _a2.startsWith("tb")) && network !== TESTNET && network !== TESTNET4 && network !== SIGNET) {
1005
+ if (((_a = String(taprootAddress == null ? void 0 : taprootAddress.address)) == null ? void 0 : _a.startsWith("tb")) && network !== TESTNET && network !== TESTNET4 && network !== SIGNET) {
978
1006
  throw new Error(
979
- "Please switch networks to testnet in the wallet settings."
1007
+ `Please switch networks to ${network} in the wallet settings.`
980
1008
  );
981
1009
  }
982
1010
  setAccounts(leatherAccountsParsed);
@@ -1234,10 +1262,13 @@ var LaserEyesProvider = ({
1234
1262
  }
1235
1263
  });
1236
1264
  const getNetwork = (0, import_react.useCallback)(() => __async(void 0, null, function* () {
1237
- var _a2;
1265
+ var _a, _b;
1238
1266
  try {
1239
1267
  if (provider === UNISAT) {
1240
1268
  const unisatNetwork = yield library == null ? void 0 : library.getChain();
1269
+ if (!unisatNetwork) {
1270
+ return (_a = config == null ? void 0 : config.network) != null ? _a : MAINNET;
1271
+ }
1241
1272
  return getNetworkForUnisat(unisatNetwork.enum);
1242
1273
  } else if (provider === XVERSE) {
1243
1274
  if (address2.slice(0, 1) === "t") {
@@ -1300,7 +1331,7 @@ var LaserEyesProvider = ({
1300
1331
  const wizzNetwork = yield library == null ? void 0 : library.getNetwork();
1301
1332
  return getNetworkForWizz(wizzNetwork);
1302
1333
  }
1303
- return (_a2 = config == null ? void 0 : config.network) != null ? _a2 : MAINNET;
1334
+ return (_b = config == null ? void 0 : config.network) != null ? _b : MAINNET;
1304
1335
  } catch (error) {
1305
1336
  throw error;
1306
1337
  }
@@ -1354,9 +1385,6 @@ var LaserEyesProvider = ({
1354
1385
  if (!library)
1355
1386
  return;
1356
1387
  if (provider === UNISAT) {
1357
- if (network === TESTNET) {
1358
- return yield getBTCBalance(paymentAddress, network);
1359
- }
1360
1388
  return yield library.getBalance();
1361
1389
  } else if (provider === XVERSE) {
1362
1390
  return yield getBTCBalance(paymentAddress, network);
@@ -1399,7 +1427,7 @@ var LaserEyesProvider = ({
1399
1427
  }
1400
1428
  });
1401
1429
  const sendBTC = (to, amount) => __async(void 0, null, function* () {
1402
- var _a2;
1430
+ var _a;
1403
1431
  try {
1404
1432
  if (amount <= 0)
1405
1433
  throw new Error("Amount must be greater than 0");
@@ -1485,7 +1513,7 @@ var LaserEyesProvider = ({
1485
1513
  }
1486
1514
  ]
1487
1515
  });
1488
- if ((_a2 = response == null ? void 0 : response.result) == null ? void 0 : _a2.txid) {
1516
+ if ((_a = response == null ? void 0 : response.result) == null ? void 0 : _a.txid) {
1489
1517
  return response.result.txid;
1490
1518
  } else {
1491
1519
  if (response.error.code === import_sats_connect.RpcErrorCode.USER_REJECTION) {
@@ -1507,7 +1535,7 @@ var LaserEyesProvider = ({
1507
1535
  }
1508
1536
  });
1509
1537
  const signMessage = (message, toSignAddress) => __async(void 0, null, function* () {
1510
- var _a2;
1538
+ var _a;
1511
1539
  try {
1512
1540
  if (!library)
1513
1541
  return;
@@ -1565,7 +1593,7 @@ var LaserEyesProvider = ({
1565
1593
  message,
1566
1594
  paymentType
1567
1595
  });
1568
- return (_a2 = signed == null ? void 0 : signed.result) == null ? void 0 : _a2.signature;
1596
+ return (_a = signed == null ? void 0 : signed.result) == null ? void 0 : _a.signature;
1569
1597
  } else if (provider === PHANTOM) {
1570
1598
  const utf8Bytes = new TextEncoder().encode(message);
1571
1599
  const uintArray = new Uint8Array(utf8Bytes);
package/dist/index.mjs CHANGED
@@ -356,7 +356,12 @@ var findPaymentAddress = (addresses) => {
356
356
  };
357
357
  var getBTCBalance = (address2, network) => __async(void 0, null, function* () {
358
358
  try {
359
- const utxos = yield getAddressUtxos(address2, network);
359
+ const utxos = yield getAddressUtxos(
360
+ address2,
361
+ network
362
+ );
363
+ if (!utxos)
364
+ return 0;
360
365
  return utxos.reduce((acc, utxo) => acc + utxo.value, 0);
361
366
  } catch (error) {
362
367
  console.error("Error fetching BTC balance:", error);
@@ -388,13 +393,31 @@ function estimateTxSize(taprootInputCount, nonTaprootInputCount, outputCount) {
388
393
  }
389
394
  function getAddressUtxos(address2, network) {
390
395
  return __async(this, null, function* () {
396
+ if (address2.startsWith("t")) {
397
+ console.log("starts with t");
398
+ if (network === MAINNET) {
399
+ return [];
400
+ }
401
+ if (network === FRACTAL_MAINNET) {
402
+ return [];
403
+ }
404
+ if (network === FRACTAL_TESTNET) {
405
+ return [];
406
+ }
407
+ }
391
408
  return yield axios.get(`${getMempoolSpaceUrl2(network)}/api/address/${address2}/utxo`).then((response) => response.data);
392
409
  });
393
410
  }
394
411
  function createSendBtcPsbt(address2, paymentAddress, recipientAddress, amount, paymentPublicKey, network, feeRate = 7) {
395
412
  return __async(this, null, function* () {
396
413
  const isTaprootOnly = address2 === paymentAddress;
397
- const utxos = yield getAddressUtxos(paymentAddress, network);
414
+ const utxos = yield getAddressUtxos(
415
+ paymentAddress,
416
+ network
417
+ );
418
+ if (!utxos) {
419
+ throw new Error("No UTXOs found");
420
+ }
398
421
  const sortedUtxos = utxos.sort(
399
422
  (a, b) => b.value - a.value
400
423
  );
@@ -495,7 +518,6 @@ var LaserEyesProvider = ({
495
518
  children,
496
519
  config
497
520
  }) => {
498
- var _a;
499
521
  const selfRef = useRef({
500
522
  accounts: []
501
523
  });
@@ -519,11 +541,9 @@ var LaserEyesProvider = ({
519
541
  const [hasLeather, setHasLeather] = useState(false);
520
542
  const [hasPhantom, setHasPhantom] = useState(false);
521
543
  const [hasWizz, setHasWizz] = useState(false);
522
- const [network, setNetwork] = useLocalStorage("network", (_a = config == null ? void 0 : config.network) != null ? _a : MAINNET, {
523
- initializeWithValue: false
524
- });
544
+ const [network, setNetwork] = useLocalStorage("network", (config == null ? void 0 : config.network) || MAINNET);
525
545
  useEffect(() => {
526
- if (config) {
546
+ if (config && config.network) {
527
547
  setNetwork(config.network);
528
548
  getNetwork().then((foundNetwork) => {
529
549
  try {
@@ -556,8 +576,8 @@ var LaserEyesProvider = ({
556
576
  }, []);
557
577
  useEffect(() => {
558
578
  const observer = new MutationObserver(() => {
559
- var _a2;
560
- const xverseLib = (_a2 = window == null ? void 0 : window.XverseProviders) == null ? void 0 : _a2.BitcoinProvider;
579
+ var _a;
580
+ const xverseLib = (_a = window == null ? void 0 : window.XverseProviders) == null ? void 0 : _a.BitcoinProvider;
561
581
  if (xverseLib) {
562
582
  setHasXverse(true);
563
583
  observer.disconnect();
@@ -596,10 +616,10 @@ var LaserEyesProvider = ({
596
616
  }, []);
597
617
  useEffect(() => {
598
618
  const observer = new MutationObserver(() => {
599
- var _a2, _b;
619
+ var _a, _b;
600
620
  let foundOkx;
601
621
  if (network === TESTNET || network === TESTNET4 || network === SIGNET || network === FRACTAL_TESTNET) {
602
- foundOkx = (_a2 = window == null ? void 0 : window.okxwallet) == null ? void 0 : _a2.bitcoinTestnet;
622
+ foundOkx = (_a = window == null ? void 0 : window.okxwallet) == null ? void 0 : _a.bitcoinTestnet;
603
623
  } else if (network === MAINNET || network === FRACTAL_MAINNET) {
604
624
  foundOkx = (_b = window == null ? void 0 : window.okxwallet) == null ? void 0 : _b.bitcoin;
605
625
  }
@@ -628,8 +648,8 @@ var LaserEyesProvider = ({
628
648
  }, []);
629
649
  useEffect(() => {
630
650
  const observer = new MutationObserver(() => {
631
- var _a2;
632
- const phantomLib = (_a2 = window == null ? void 0 : window.phantom) == null ? void 0 : _a2.bitcoin;
651
+ var _a;
652
+ const phantomLib = (_a = window == null ? void 0 : window.phantom) == null ? void 0 : _a.bitcoin;
633
653
  if (phantomLib && phantomLib.isPhantom) {
634
654
  setHasPhantom(true);
635
655
  observer.disconnect();
@@ -837,15 +857,23 @@ var LaserEyesProvider = ({
837
857
  throw error;
838
858
  }
839
859
  });
840
- const connectOkx = () => __async(void 0, null, function* () {
860
+ const connectOkx = useCallback(() => __async(void 0, null, function* () {
861
+ var _a;
841
862
  try {
842
863
  localStorage == null ? void 0 : localStorage.setItem(LOCAL_STORAGE_DEFAULT_WALLET, OKX);
864
+ console.log("connecting", network);
843
865
  const lib = network === TESTNET || network === TESTNET4 || network === FRACTAL_TESTNET ? window.okxwallet.bitcoinTestnet : network === SIGNET ? window.okxwallet.bitcoinSignet : window.okxwallet.bitcoin;
844
866
  const okxAccounts = yield lib.connect();
845
867
  if (!okxAccounts)
846
868
  throw new Error("No accounts found");
847
869
  setAccounts([okxAccounts]);
848
870
  setAddress(okxAccounts.address);
871
+ if (((_a = String(okxAccounts.address)) == null ? void 0 : _a.startsWith("tb")) && network !== TESTNET && network !== TESTNET4 && network !== SIGNET) {
872
+ console.log("err");
873
+ throw new Error(
874
+ `Please switch networks to ${network} in the wallet settings.`
875
+ );
876
+ }
849
877
  setPaymentAddress(okxAccounts.address);
850
878
  setPublicKey(okxAccounts.publicKey);
851
879
  setPaymentPublicKey(okxAccounts.publicKey);
@@ -858,9 +886,9 @@ var LaserEyesProvider = ({
858
886
  console.log("error", error);
859
887
  throw error;
860
888
  }
861
- });
889
+ }), [hasOkx, network]);
862
890
  const connectLeather = useCallback(() => __async(void 0, null, function* () {
863
- var _a2;
891
+ var _a;
864
892
  try {
865
893
  localStorage == null ? void 0 : localStorage.setItem(LOCAL_STORAGE_DEFAULT_WALLET, LEATHER);
866
894
  const lib = window.LeatherProvider;
@@ -878,9 +906,9 @@ var LaserEyesProvider = ({
878
906
  const segwitAddress = addresses.find(
879
907
  (address3) => address3.type === P2WPKH
880
908
  );
881
- if (((_a2 = String(taprootAddress)) == null ? void 0 : _a2.startsWith("tb")) && network !== TESTNET && network !== TESTNET4 && network !== SIGNET) {
909
+ if (((_a = String(taprootAddress == null ? void 0 : taprootAddress.address)) == null ? void 0 : _a.startsWith("tb")) && network !== TESTNET && network !== TESTNET4 && network !== SIGNET) {
882
910
  throw new Error(
883
- "Please switch networks to testnet in the wallet settings."
911
+ `Please switch networks to ${network} in the wallet settings.`
884
912
  );
885
913
  }
886
914
  setAccounts(leatherAccountsParsed);
@@ -1138,10 +1166,13 @@ var LaserEyesProvider = ({
1138
1166
  }
1139
1167
  });
1140
1168
  const getNetwork = useCallback(() => __async(void 0, null, function* () {
1141
- var _a2;
1169
+ var _a, _b;
1142
1170
  try {
1143
1171
  if (provider === UNISAT) {
1144
1172
  const unisatNetwork = yield library == null ? void 0 : library.getChain();
1173
+ if (!unisatNetwork) {
1174
+ return (_a = config == null ? void 0 : config.network) != null ? _a : MAINNET;
1175
+ }
1145
1176
  return getNetworkForUnisat(unisatNetwork.enum);
1146
1177
  } else if (provider === XVERSE) {
1147
1178
  if (address2.slice(0, 1) === "t") {
@@ -1204,7 +1235,7 @@ var LaserEyesProvider = ({
1204
1235
  const wizzNetwork = yield library == null ? void 0 : library.getNetwork();
1205
1236
  return getNetworkForWizz(wizzNetwork);
1206
1237
  }
1207
- return (_a2 = config == null ? void 0 : config.network) != null ? _a2 : MAINNET;
1238
+ return (_b = config == null ? void 0 : config.network) != null ? _b : MAINNET;
1208
1239
  } catch (error) {
1209
1240
  throw error;
1210
1241
  }
@@ -1258,9 +1289,6 @@ var LaserEyesProvider = ({
1258
1289
  if (!library)
1259
1290
  return;
1260
1291
  if (provider === UNISAT) {
1261
- if (network === TESTNET) {
1262
- return yield getBTCBalance(paymentAddress, network);
1263
- }
1264
1292
  return yield library.getBalance();
1265
1293
  } else if (provider === XVERSE) {
1266
1294
  return yield getBTCBalance(paymentAddress, network);
@@ -1303,7 +1331,7 @@ var LaserEyesProvider = ({
1303
1331
  }
1304
1332
  });
1305
1333
  const sendBTC = (to, amount) => __async(void 0, null, function* () {
1306
- var _a2;
1334
+ var _a;
1307
1335
  try {
1308
1336
  if (amount <= 0)
1309
1337
  throw new Error("Amount must be greater than 0");
@@ -1389,7 +1417,7 @@ var LaserEyesProvider = ({
1389
1417
  }
1390
1418
  ]
1391
1419
  });
1392
- if ((_a2 = response == null ? void 0 : response.result) == null ? void 0 : _a2.txid) {
1420
+ if ((_a = response == null ? void 0 : response.result) == null ? void 0 : _a.txid) {
1393
1421
  return response.result.txid;
1394
1422
  } else {
1395
1423
  if (response.error.code === RpcErrorCode.USER_REJECTION) {
@@ -1411,7 +1439,7 @@ var LaserEyesProvider = ({
1411
1439
  }
1412
1440
  });
1413
1441
  const signMessage = (message, toSignAddress) => __async(void 0, null, function* () {
1414
- var _a2;
1442
+ var _a;
1415
1443
  try {
1416
1444
  if (!library)
1417
1445
  return;
@@ -1469,7 +1497,7 @@ var LaserEyesProvider = ({
1469
1497
  message,
1470
1498
  paymentType
1471
1499
  });
1472
- return (_a2 = signed == null ? void 0 : signed.result) == null ? void 0 : _a2.signature;
1500
+ return (_a = signed == null ? void 0 : signed.result) == null ? void 0 : _a.signature;
1473
1501
  } else if (provider === PHANTOM) {
1474
1502
  const utf8Bytes = new TextEncoder().encode(message);
1475
1503
  const uintArray = new Uint8Array(utf8Bytes);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnisat/lasereyes",
3
- "version": "0.0.43",
3
+ "version": "0.0.44",
4
4
  "sideEffects": false,
5
5
  "private": false,
6
6
  "license": "MIT",