@btc-vision/wallet-sdk 1.0.2 → 1.0.5

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.
Files changed (129) hide show
  1. package/es/bundle.js +1 -1
  2. package/es/bundle.js.LICENSE.txt +2 -2
  3. package/lib/address/index.js +50 -94
  4. package/lib/bitcoin-core.js +7 -47
  5. package/lib/constants.js +1 -4
  6. package/lib/error.js +6 -10
  7. package/lib/index.js +14 -53
  8. package/lib/keyring/hd-keyring.js +40 -89
  9. package/lib/keyring/index.js +4 -20
  10. package/lib/keyring/interfaces/SimpleKeyringOptions.js +16 -31
  11. package/lib/keyring/keystone-keyring.js +142 -211
  12. package/lib/keyring/simple-keyring.js +13 -18
  13. package/lib/message/bip322-simple.js +31 -81
  14. package/lib/message/deterministic-ecdsa.js +7 -43
  15. package/lib/message/ecdsa.js +10 -17
  16. package/lib/message/index.js +3 -19
  17. package/lib/network/index.js +12 -17
  18. package/lib/runes/index.js +1 -17
  19. package/lib/runes/rund_id.js +5 -10
  20. package/lib/runes/varint.js +6 -12
  21. package/lib/transaction/index.js +3 -19
  22. package/lib/transaction/inscription-utxo.js +9 -10
  23. package/lib/transaction/transaction.js +137 -142
  24. package/lib/transaction/utxo.js +13 -18
  25. package/lib/tx-helpers/index.js +8 -18
  26. package/lib/tx-helpers/send-atomicals-ft.js +45 -59
  27. package/lib/tx-helpers/send-atomicals-nft.js +29 -43
  28. package/lib/tx-helpers/send-btc.js +53 -70
  29. package/lib/tx-helpers/send-inscription.js +32 -46
  30. package/lib/tx-helpers/send-inscriptions.js +28 -42
  31. package/lib/tx-helpers/send-runes.js +83 -100
  32. package/lib/tx-helpers/split-inscription-utxo.js +39 -53
  33. package/lib/types.js +2 -5
  34. package/lib/utils.js +16 -29
  35. package/lib/wallet/abstract-wallet.js +1 -2
  36. package/lib/wallet/estimate-wallet.js +130 -147
  37. package/lib/wallet/index.js +3 -19
  38. package/lib/wallet/local-wallet.js +134 -150
  39. package/package.json +21 -12
  40. package/lib/address/index.d.ts +0 -36
  41. package/lib/bitcoin-core.d.ts +0 -5
  42. package/lib/constants.d.ts +0 -1
  43. package/lib/error.d.ts +0 -20
  44. package/lib/index.d.ts +0 -14
  45. package/lib/keyring/hd-keyring.d.ts +0 -50
  46. package/lib/keyring/index.d.ts +0 -4
  47. package/lib/keyring/interfaces/SimpleKeyringOptions.d.ts +0 -52
  48. package/lib/keyring/keystone-keyring.d.ts +0 -82
  49. package/lib/keyring/simple-keyring.d.ts +0 -11
  50. package/lib/message/bip322-simple.d.ts +0 -19
  51. package/lib/message/deterministic-ecdsa.d.ts +0 -2
  52. package/lib/message/ecdsa.d.ts +0 -3
  53. package/lib/message/index.d.ts +0 -3
  54. package/lib/network/index.d.ts +0 -14
  55. package/lib/runes/index.d.ts +0 -1
  56. package/lib/runes/rund_id.d.ts +0 -11
  57. package/lib/runes/varint.d.ts +0 -14
  58. package/lib/src/address/index.d.ts +0 -36
  59. package/lib/src/bitcoin-core.d.ts +0 -5
  60. package/lib/src/constants.d.ts +0 -1
  61. package/lib/src/error.d.ts +0 -20
  62. package/lib/src/index.d.ts +0 -14
  63. package/lib/src/keyring/hd-keyring.d.ts +0 -50
  64. package/lib/src/keyring/index.d.ts +0 -4
  65. package/lib/src/keyring/interfaces/SimpleKeyringOptions.d.ts +0 -52
  66. package/lib/src/keyring/keystone-keyring.d.ts +0 -82
  67. package/lib/src/keyring/simple-keyring.d.ts +0 -11
  68. package/lib/src/message/bip322-simple.d.ts +0 -19
  69. package/lib/src/message/deterministic-ecdsa.d.ts +0 -2
  70. package/lib/src/message/ecdsa.d.ts +0 -3
  71. package/lib/src/message/index.d.ts +0 -3
  72. package/lib/src/network/index.d.ts +0 -14
  73. package/lib/src/runes/index.d.ts +0 -1
  74. package/lib/src/runes/rund_id.d.ts +0 -11
  75. package/lib/src/runes/varint.d.ts +0 -14
  76. package/lib/src/transaction/index.d.ts +0 -3
  77. package/lib/src/transaction/inscription-utxo.d.ts +0 -33
  78. package/lib/src/transaction/transaction.d.ts +0 -51
  79. package/lib/src/transaction/utxo.d.ts +0 -35
  80. package/lib/src/tx-helpers/index.d.ts +0 -8
  81. package/lib/src/tx-helpers/send-atomicals-ft.d.ts +0 -16
  82. package/lib/src/tx-helpers/send-atomicals-nft.d.ts +0 -14
  83. package/lib/src/tx-helpers/send-btc.d.ts +0 -28
  84. package/lib/src/tx-helpers/send-inscription.d.ts +0 -16
  85. package/lib/src/tx-helpers/send-inscriptions.d.ts +0 -14
  86. package/lib/src/tx-helpers/send-runes.d.ts +0 -19
  87. package/lib/src/tx-helpers/split-inscription-utxo.d.ts +0 -15
  88. package/lib/src/types.d.ts +0 -59
  89. package/lib/src/utils.d.ts +0 -23
  90. package/lib/src/wallet/abstract-wallet.d.ts +0 -6
  91. package/lib/src/wallet/estimate-wallet.d.ts +0 -23
  92. package/lib/src/wallet/index.d.ts +0 -3
  93. package/lib/src/wallet/local-wallet.d.ts +0 -23
  94. package/lib/test/address/address.test.d.ts +0 -1
  95. package/lib/test/keyring/hd-keyring.test.d.ts +0 -1
  96. package/lib/test/keyring/keystone-keyring.test.d.ts +0 -1
  97. package/lib/test/keyring/simple-keyring.test.d.ts +0 -1
  98. package/lib/test/message/message.test.d.ts +0 -1
  99. package/lib/test/runes/varint.test.d.ts +0 -1
  100. package/lib/test/transaction/transaction.test.d.ts +0 -1
  101. package/lib/test/transaction/utxo.test.d.ts +0 -1
  102. package/lib/test/tx-helpers/send-atomicals-ft.test.d.ts +0 -1
  103. package/lib/test/tx-helpers/send-atomicals-nft.test.d.ts +0 -1
  104. package/lib/test/tx-helpers/send-btc.test.d.ts +0 -1
  105. package/lib/test/tx-helpers/send-inscription.test.d.ts +0 -1
  106. package/lib/test/tx-helpers/send-inscriptions.test.d.ts +0 -1
  107. package/lib/test/tx-helpers/send-runes.test.d.ts +0 -1
  108. package/lib/test/tx-helpers/split-inscription-utxo.test.d.ts +0 -1
  109. package/lib/test/tx-helpers/utils.d.ts +0 -217
  110. package/lib/test/utils.d.ts +0 -4
  111. package/lib/test/wallet/local-wallet.test.d.ts +0 -1
  112. package/lib/transaction/index.d.ts +0 -3
  113. package/lib/transaction/inscription-utxo.d.ts +0 -33
  114. package/lib/transaction/transaction.d.ts +0 -51
  115. package/lib/transaction/utxo.d.ts +0 -35
  116. package/lib/tx-helpers/index.d.ts +0 -8
  117. package/lib/tx-helpers/send-atomicals-ft.d.ts +0 -16
  118. package/lib/tx-helpers/send-atomicals-nft.d.ts +0 -14
  119. package/lib/tx-helpers/send-btc.d.ts +0 -28
  120. package/lib/tx-helpers/send-inscription.d.ts +0 -16
  121. package/lib/tx-helpers/send-inscriptions.d.ts +0 -14
  122. package/lib/tx-helpers/send-runes.d.ts +0 -19
  123. package/lib/tx-helpers/split-inscription-utxo.d.ts +0 -15
  124. package/lib/types.d.ts +0 -59
  125. package/lib/utils.d.ts +0 -23
  126. package/lib/wallet/abstract-wallet.d.ts +0 -6
  127. package/lib/wallet/estimate-wallet.d.ts +0 -23
  128. package/lib/wallet/index.d.ts +0 -3
  129. package/lib/wallet/local-wallet.d.ts +0 -23
@@ -1,57 +1,43 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.splitInscriptionUtxo = splitInscriptionUtxo;
13
- const constants_1 = require("../constants");
14
- const error_1 = require("../error");
15
- const transaction_1 = require("../transaction");
16
- function splitInscriptionUtxo(_a) {
17
- return __awaiter(this, arguments, void 0, function* ({ btcUtxos, assetUtxo, networkType, changeAddress, feeRate, enableRBF = true, outputValue = 546 }) {
18
- if (transaction_1.utxoHelper.hasAnyAssets(btcUtxos)) {
19
- throw new error_1.WalletUtilsError(error_1.ErrorCodes.NOT_SAFE_UTXOS);
20
- }
21
- if (transaction_1.utxoHelper.hasAtomicals([assetUtxo])) {
22
- throw new error_1.WalletUtilsError(error_1.ErrorCodes.NOT_SAFE_UTXOS);
23
- }
24
- const tx = new transaction_1.Transaction();
25
- tx.setNetworkType(networkType);
26
- tx.setFeeRate(feeRate);
27
- tx.setEnableRBF(enableRBF);
28
- tx.setChangeAddress(changeAddress);
29
- const toSignInputs = [];
30
- let lastUnit = null;
31
- let splitedCount = 0;
32
- const ordUtxo = new transaction_1.InscriptionUnspendOutput(assetUtxo, outputValue);
33
- tx.addInput(ordUtxo.utxo);
34
- toSignInputs.push({ index: 0, publicKey: ordUtxo.utxo.pubkey });
35
- for (let j = 0; j < ordUtxo.inscriptionUnits.length; j++) {
36
- const unit = ordUtxo.inscriptionUnits[j];
37
- if (unit.hasInscriptions()) {
38
- tx.addChangeOutput(unit.satoshis);
39
- lastUnit = unit;
40
- splitedCount++;
41
- continue;
42
- }
1
+ import { UTXO_DUST } from '../constants';
2
+ import { ErrorCodes, WalletUtilsError } from '../error';
3
+ import { InscriptionUnspendOutput, Transaction, utxoHelper } from '../transaction';
4
+ export async function splitInscriptionUtxo({ btcUtxos, assetUtxo, networkType, changeAddress, feeRate, enableRBF = true, outputValue = 546 }) {
5
+ if (utxoHelper.hasAnyAssets(btcUtxos)) {
6
+ throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
7
+ }
8
+ if (utxoHelper.hasAtomicals([assetUtxo])) {
9
+ throw new WalletUtilsError(ErrorCodes.NOT_SAFE_UTXOS);
10
+ }
11
+ const tx = new Transaction();
12
+ tx.setNetworkType(networkType);
13
+ tx.setFeeRate(feeRate || 1);
14
+ tx.setEnableRBF(enableRBF);
15
+ tx.setChangeAddress(changeAddress);
16
+ const toSignInputs = [];
17
+ let lastUnit = null;
18
+ let splitedCount = 0;
19
+ const ordUtxo = new InscriptionUnspendOutput(assetUtxo, outputValue);
20
+ tx.addInput(ordUtxo.utxo);
21
+ toSignInputs.push({ index: 0, publicKey: ordUtxo.utxo.pubkey });
22
+ for (let j = 0; j < ordUtxo.inscriptionUnits.length; j++) {
23
+ const unit = ordUtxo.inscriptionUnits[j];
24
+ if (unit.hasInscriptions()) {
43
25
  tx.addChangeOutput(unit.satoshis);
44
26
  lastUnit = unit;
27
+ splitedCount++;
28
+ continue;
45
29
  }
46
- if (!lastUnit.hasInscriptions()) {
47
- tx.removeChangeOutput();
48
- }
49
- if (lastUnit.satoshis < constants_1.UTXO_DUST) {
50
- lastUnit.satoshis = constants_1.UTXO_DUST;
51
- }
52
- const _toSignInputs = yield tx.addSufficientUtxosForFee(btcUtxos);
53
- toSignInputs.push(..._toSignInputs);
54
- const psbt = tx.toPsbt();
55
- return { psbt, toSignInputs, splitedCount };
56
- });
30
+ tx.addChangeOutput(unit.satoshis);
31
+ lastUnit = unit;
32
+ }
33
+ if (!lastUnit.hasInscriptions()) {
34
+ tx.removeChangeOutput();
35
+ }
36
+ if (lastUnit.satoshis < UTXO_DUST) {
37
+ lastUnit.satoshis = UTXO_DUST;
38
+ }
39
+ const _toSignInputs = await tx.addSufficientUtxosForFee(btcUtxos);
40
+ toSignInputs.push(..._toSignInputs);
41
+ const psbt = tx.toPsbt();
42
+ return { psbt, toSignInputs, splitedCount };
57
43
  }
package/lib/types.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AddressType = void 0;
4
- var AddressType;
1
+ export var AddressType;
5
2
  (function (AddressType) {
6
3
  AddressType[AddressType["P2PKH"] = 0] = "P2PKH";
7
4
  AddressType[AddressType["P2WPKH"] = 1] = "P2WPKH";
@@ -12,4 +9,4 @@ var AddressType;
12
9
  AddressType[AddressType["P2WSH"] = 6] = "P2WSH";
13
10
  AddressType[AddressType["P2SH"] = 7] = "P2SH";
14
11
  AddressType[AddressType["UNKNOWN"] = 8] = "UNKNOWN";
15
- })(AddressType || (exports.AddressType = AddressType = {}));
12
+ })(AddressType || (AddressType = {}));
package/lib/utils.js CHANGED
@@ -1,24 +1,13 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.schnorrValidator = exports.validator = exports.toXOnly = void 0;
7
- exports.tweakSigner = tweakSigner;
8
- exports.satoshisToAmount = satoshisToAmount;
9
- exports.amountToSaothis = amountToSaothis;
10
- exports.shortAddress = shortAddress;
11
- const bignumber_js_1 = __importDefault(require("bignumber.js"));
12
- const bitcoin_core_1 = require("./bitcoin-core");
13
- const toXOnly = (pubKey) => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
14
- exports.toXOnly = toXOnly;
1
+ import BigNumber from 'bignumber.js';
2
+ import { bitcoin, ecc, ECPair } from './bitcoin-core';
3
+ export const toXOnly = (pubKey) => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
15
4
  function tapTweakHash(pubKey, h) {
16
- return bitcoin_core_1.bitcoin.crypto.taggedHash('TapTweak', Buffer.concat(h ? [pubKey, h] : [pubKey]));
5
+ return bitcoin.crypto.taggedHash('TapTweak', Buffer.concat(h ? [pubKey, h] : [pubKey]));
17
6
  }
18
7
  /**
19
8
  * Transform raw private key to taproot address private key
20
9
  */
21
- function tweakSigner(signer, opts = {}) {
10
+ export function tweakSigner(signer, opts = {}) {
22
11
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
12
  // @ts-ignore
24
13
  let privateKey = signer.privateKey;
@@ -26,43 +15,41 @@ function tweakSigner(signer, opts = {}) {
26
15
  throw new Error('Private key is required for tweaking signer!');
27
16
  }
28
17
  if (signer.publicKey[0] === 3) {
29
- privateKey = bitcoin_core_1.ecc.privateNegate(privateKey);
18
+ privateKey = ecc.privateNegate(privateKey);
30
19
  }
31
- const tweakedPrivateKey = bitcoin_core_1.ecc.privateAdd(privateKey, tapTweakHash((0, exports.toXOnly)(signer.publicKey), opts.tweakHash));
20
+ const tweakedPrivateKey = ecc.privateAdd(privateKey, tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash));
32
21
  if (!tweakedPrivateKey) {
33
22
  throw new Error('Invalid tweaked private key!');
34
23
  }
35
- return bitcoin_core_1.ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
24
+ return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
36
25
  network: opts.network
37
26
  });
38
27
  }
39
28
  /**
40
29
  * ECDSA signature validator
41
30
  */
42
- const validator = (pubkey, msghash, signature) => bitcoin_core_1.ECPair.fromPublicKey(pubkey).verify(msghash, signature);
43
- exports.validator = validator;
31
+ export const validator = (pubkey, msghash, signature) => ECPair.fromPublicKey(pubkey).verify(msghash, signature);
44
32
  /**
45
33
  * Schnorr signature validator
46
34
  */
47
- const schnorrValidator = (pubkey, msghash, signature) => {
48
- return bitcoin_core_1.ECPair.fromPublicKey(pubkey).verifySchnorr(msghash, signature);
35
+ export const schnorrValidator = (pubkey, msghash, signature) => {
36
+ return ECPair.fromPublicKey(pubkey).verifySchnorr(msghash, signature);
49
37
  };
50
- exports.schnorrValidator = schnorrValidator;
51
38
  /**
52
39
  * Transform satoshis to btc format
53
40
  */
54
- function satoshisToAmount(val) {
55
- const num = new bignumber_js_1.default(val);
41
+ export function satoshisToAmount(val) {
42
+ const num = new BigNumber(val);
56
43
  return num.dividedBy(100000000).toFixed(8);
57
44
  }
58
45
  /**
59
46
  * Transform btc format to satoshis
60
47
  */
61
- function amountToSaothis(val) {
62
- const num = new bignumber_js_1.default(val);
48
+ export function amountToSaothis(val) {
49
+ const num = new BigNumber(val);
63
50
  return num.multipliedBy(100000000).toNumber();
64
51
  }
65
- function shortAddress(address, len = 5) {
52
+ export function shortAddress(address, len = 5) {
66
53
  if (!address)
67
54
  return '';
68
55
  if (address.length <= len * 2)
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,174 +1,157 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.EstimateWallet = void 0;
13
- const address_1 = require("../address");
14
- const bitcoin_core_1 = require("../bitcoin-core");
15
- const keyring_1 = require("../keyring");
16
- const message_1 = require("../message");
17
- const network_1 = require("../network");
18
- const types_1 = require("../types");
19
- const utils_1 = require("../utils");
1
+ import { publicKeyToAddress, scriptPkToAddress } from '../address';
2
+ import { bitcoin, ECPair } from '../bitcoin-core';
3
+ import { SimpleKeyring } from '../keyring';
4
+ import { signMessageOfBIP322Simple } from '../message';
5
+ import { NetworkType, toPsbtNetwork } from '../network';
6
+ import { AddressType } from '../types';
7
+ import { toXOnly } from '../utils';
20
8
  /**
21
9
  * EstimateWallet is a wallet that can be used to estimate the size of a transaction.
22
10
  */
23
- class EstimateWallet {
24
- constructor(wif, networkType = network_1.NetworkType.MAINNET, addressType = types_1.AddressType.P2WPKH) {
25
- const network = (0, network_1.toPsbtNetwork)(networkType);
26
- const keyPair = bitcoin_core_1.ECPair.fromWIF(wif, network);
27
- this.keyring = new keyring_1.SimpleKeyring({
11
+ export class EstimateWallet {
12
+ keyring;
13
+ address;
14
+ pubkey;
15
+ network;
16
+ networkType;
17
+ addressType;
18
+ constructor(wif, networkType = NetworkType.MAINNET, addressType = AddressType.P2WPKH) {
19
+ const network = toPsbtNetwork(networkType);
20
+ const keyPair = ECPair.fromWIF(wif, network);
21
+ this.keyring = new SimpleKeyring({
28
22
  privateKeys: [keyPair.privateKey.toString('hex')],
29
23
  network: network
30
24
  });
31
25
  this.keyring.addAccounts(1);
32
26
  this.pubkey = keyPair.publicKey.toString('hex');
33
- this.address = (0, address_1.publicKeyToAddress)(this.pubkey, addressType, networkType);
27
+ this.address = publicKeyToAddress(this.pubkey, addressType, networkType);
34
28
  this.network = network;
35
29
  this.networkType = networkType;
36
30
  this.addressType = addressType;
37
31
  }
38
- static fromRandom(addressType = types_1.AddressType.P2WPKH, networkType = network_1.NetworkType.MAINNET) {
39
- const network = (0, network_1.toPsbtNetwork)(networkType);
40
- const ecpair = bitcoin_core_1.ECPair.makeRandom({ network });
32
+ static fromRandom(addressType = AddressType.P2WPKH, networkType = NetworkType.MAINNET) {
33
+ const network = toPsbtNetwork(networkType);
34
+ const ecpair = ECPair.makeRandom({ network });
41
35
  const wallet = new EstimateWallet(ecpair.toWIF(), networkType, addressType);
42
36
  return wallet;
43
37
  }
44
38
  getNetworkType() {
45
39
  return this.networkType;
46
40
  }
47
- signPsbt(psbt, opts) {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- const _opts = opts || {
50
- autoFinalized: true,
51
- toSignInputs: []
52
- };
53
- let _inputs = yield this.formatOptionsToSignInputs(psbt, opts);
54
- if (_inputs.length == 0) {
55
- throw new Error('no input to sign');
56
- }
57
- psbt.data.inputs.forEach((v) => {
58
- var _a;
59
- const isNotSigned = !(v.finalScriptSig || v.finalScriptWitness);
60
- const isP2TR = this.addressType === types_1.AddressType.P2TR || this.addressType === types_1.AddressType.M44_P2TR;
61
- const lostInternalPubkey = !v.tapInternalKey;
62
- // Special measures taken for compatibility with certain applications.
63
- if (isNotSigned && isP2TR && lostInternalPubkey) {
64
- const tapInternalKey = (0, utils_1.toXOnly)(Buffer.from(this.pubkey, 'hex'));
65
- const { output } = bitcoin_core_1.bitcoin.payments.p2tr({
66
- internalPubkey: tapInternalKey,
67
- network: (0, network_1.toPsbtNetwork)(this.networkType)
68
- });
69
- if (((_a = v.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script.toString('hex')) == (output === null || output === void 0 ? void 0 : output.toString('hex'))) {
70
- v.tapInternalKey = tapInternalKey;
71
- }
72
- }
73
- });
74
- psbt = yield this.keyring.signTransaction(psbt, _inputs);
75
- if (_opts.autoFinalized) {
76
- _inputs.forEach((v) => {
77
- // psbt.validateSignaturesOfInput(v.index, validator);
78
- psbt.finalizeInput(v.index);
41
+ async signPsbt(psbt, opts) {
42
+ const _opts = opts || {
43
+ autoFinalized: true,
44
+ toSignInputs: []
45
+ };
46
+ let _inputs = await this.formatOptionsToSignInputs(psbt, opts);
47
+ if (_inputs.length == 0) {
48
+ throw new Error('no input to sign');
49
+ }
50
+ psbt.data.inputs.forEach((v) => {
51
+ const isNotSigned = !(v.finalScriptSig || v.finalScriptWitness);
52
+ const isP2TR = this.addressType === AddressType.P2TR || this.addressType === AddressType.M44_P2TR;
53
+ const lostInternalPubkey = !v.tapInternalKey;
54
+ // Special measures taken for compatibility with certain applications.
55
+ if (isNotSigned && isP2TR && lostInternalPubkey) {
56
+ const tapInternalKey = toXOnly(Buffer.from(this.pubkey, 'hex'));
57
+ const { output } = bitcoin.payments.p2tr({
58
+ internalPubkey: tapInternalKey,
59
+ network: toPsbtNetwork(this.networkType)
79
60
  });
61
+ if (v.witnessUtxo?.script.toString('hex') == output?.toString('hex')) {
62
+ v.tapInternalKey = tapInternalKey;
63
+ }
80
64
  }
81
- return psbt;
82
65
  });
66
+ psbt = await this.keyring.signTransaction(psbt, _inputs);
67
+ if (_opts.autoFinalized) {
68
+ _inputs.forEach((v) => {
69
+ // psbt.validateSignaturesOfInput(v.index, validator);
70
+ psbt.finalizeInput(v.index);
71
+ });
72
+ }
73
+ return psbt;
83
74
  }
84
- getPublicKey() {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- const pubkeys = yield this.keyring.getAccounts();
87
- return pubkeys[0];
88
- });
75
+ async getPublicKey() {
76
+ const pubkeys = await this.keyring.getAccounts();
77
+ return pubkeys[0];
89
78
  }
90
- signMessage(text, type) {
91
- return __awaiter(this, void 0, void 0, function* () {
92
- if (type === 'bip322-simple') {
93
- return yield (0, message_1.signMessageOfBIP322Simple)({
94
- message: text,
95
- address: this.address,
96
- networkType: this.networkType,
97
- wallet: this
98
- });
99
- }
100
- else {
101
- const pubkey = yield this.getPublicKey();
102
- return yield this.keyring.signMessage(pubkey, text);
103
- }
104
- });
79
+ async signMessage(text, type) {
80
+ if (type === 'bip322-simple') {
81
+ return await signMessageOfBIP322Simple({
82
+ message: text,
83
+ address: this.address,
84
+ networkType: this.networkType,
85
+ wallet: this
86
+ });
87
+ }
88
+ else {
89
+ const pubkey = await this.getPublicKey();
90
+ return await this.keyring.signMessage(pubkey, text);
91
+ }
105
92
  }
106
- formatOptionsToSignInputs(_psbt, options) {
107
- return __awaiter(this, void 0, void 0, function* () {
108
- const accountAddress = this.address;
109
- const accountPubkey = yield this.getPublicKey();
110
- let toSignInputs = [];
111
- if (options && options.toSignInputs) {
112
- // We expect userToSignInputs objects to be similar to ToSignInput interface,
113
- // but we allow address to be specified in addition to publicKey for convenience.
114
- toSignInputs = options.toSignInputs.map((input) => {
115
- var _a;
116
- const index = Number(input.index);
117
- if (isNaN(index))
118
- throw new Error('invalid index in toSignInput');
119
- if (!input.address && !input.publicKey) {
120
- throw new Error('no address or public key in toSignInput');
121
- }
122
- if (input.address &&
123
- input.address != accountAddress) {
124
- throw new Error('invalid address in toSignInput');
125
- }
126
- if (input.publicKey &&
127
- input.publicKey != accountPubkey) {
128
- throw new Error('invalid public key in toSignInput');
129
- }
130
- const sighashTypes = (_a = input.sighashTypes) === null || _a === void 0 ? void 0 : _a.map(Number);
131
- if (sighashTypes === null || sighashTypes === void 0 ? void 0 : sighashTypes.some(isNaN))
132
- throw new Error('invalid sighash type in toSignInput');
133
- return {
134
- index,
135
- publicKey: accountPubkey,
136
- sighashTypes,
137
- disableTweakSigner: input.disableTweakSigner
138
- };
139
- });
140
- }
141
- else {
142
- const networkType = this.getNetworkType();
143
- const psbtNetwork = (0, network_1.toPsbtNetwork)(networkType);
144
- const psbt = typeof _psbt === 'string'
145
- ? bitcoin_core_1.bitcoin.Psbt.fromHex(_psbt, { network: psbtNetwork })
146
- : _psbt;
147
- psbt.data.inputs.forEach((v, index) => {
148
- let script = null;
149
- if (v.witnessUtxo) {
150
- script = v.witnessUtxo.script;
151
- }
152
- else if (v.nonWitnessUtxo) {
153
- const tx = bitcoin_core_1.bitcoin.Transaction.fromBuffer(v.nonWitnessUtxo);
154
- const output = tx.outs[psbt.txInputs[index].index];
155
- script = output.script;
156
- }
157
- const isSigned = v.finalScriptSig || v.finalScriptWitness;
158
- if (script && !isSigned) {
159
- const address = (0, address_1.scriptPkToAddress)(script, this.networkType);
160
- if (accountAddress === address) {
161
- toSignInputs.push({
162
- index,
163
- publicKey: accountPubkey,
164
- sighashTypes: v.sighashType ? [v.sighashType] : undefined
165
- });
166
- }
93
+ async formatOptionsToSignInputs(_psbt, options) {
94
+ const accountAddress = this.address;
95
+ const accountPubkey = await this.getPublicKey();
96
+ let toSignInputs = [];
97
+ if (options && options.toSignInputs) {
98
+ // We expect userToSignInputs objects to be similar to ToSignInput interface,
99
+ // but we allow address to be specified in addition to publicKey for convenience.
100
+ toSignInputs = options.toSignInputs.map((input) => {
101
+ const index = Number(input.index);
102
+ if (isNaN(index))
103
+ throw new Error('invalid index in toSignInput');
104
+ if (!input.address && !input.publicKey) {
105
+ throw new Error('no address or public key in toSignInput');
106
+ }
107
+ if (input.address &&
108
+ input.address != accountAddress) {
109
+ throw new Error('invalid address in toSignInput');
110
+ }
111
+ if (input.publicKey &&
112
+ input.publicKey != accountPubkey) {
113
+ throw new Error('invalid public key in toSignInput');
114
+ }
115
+ const sighashTypes = input.sighashTypes?.map(Number);
116
+ if (sighashTypes?.some(isNaN))
117
+ throw new Error('invalid sighash type in toSignInput');
118
+ return {
119
+ index,
120
+ publicKey: accountPubkey,
121
+ sighashTypes,
122
+ disableTweakSigner: input.disableTweakSigner
123
+ };
124
+ });
125
+ }
126
+ else {
127
+ const networkType = this.getNetworkType();
128
+ const psbtNetwork = toPsbtNetwork(networkType);
129
+ const psbt = typeof _psbt === 'string'
130
+ ? bitcoin.Psbt.fromHex(_psbt, { network: psbtNetwork })
131
+ : _psbt;
132
+ psbt.data.inputs.forEach((v, index) => {
133
+ let script = null;
134
+ if (v.witnessUtxo) {
135
+ script = v.witnessUtxo.script;
136
+ }
137
+ else if (v.nonWitnessUtxo) {
138
+ const tx = bitcoin.Transaction.fromBuffer(v.nonWitnessUtxo);
139
+ const output = tx.outs[psbt.txInputs[index].index];
140
+ script = output.script;
141
+ }
142
+ const isSigned = v.finalScriptSig || v.finalScriptWitness;
143
+ if (script && !isSigned) {
144
+ const address = scriptPkToAddress(script, this.networkType);
145
+ if (accountAddress === address) {
146
+ toSignInputs.push({
147
+ index,
148
+ publicKey: accountPubkey,
149
+ sighashTypes: v.sighashType ? [v.sighashType] : undefined
150
+ });
167
151
  }
168
- });
169
- }
170
- return toSignInputs;
171
- });
152
+ }
153
+ });
154
+ }
155
+ return toSignInputs;
172
156
  }
173
157
  }
174
- exports.EstimateWallet = EstimateWallet;
@@ -1,19 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./abstract-wallet"), exports);
18
- __exportStar(require("./estimate-wallet"), exports);
19
- __exportStar(require("./local-wallet"), exports);
1
+ export * from './abstract-wallet';
2
+ export * from './estimate-wallet';
3
+ export * from './local-wallet';