@btc-vision/wallet-sdk 1.0.0 → 1.0.4

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 +4 -4
  3. package/lib/address/index.js +50 -85
  4. package/lib/bitcoin-core.js +7 -37
  5. package/lib/constants.js +1 -4
  6. package/lib/error.js +6 -10
  7. package/lib/index.js +14 -43
  8. package/lib/keyring/hd-keyring.js +40 -79
  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 -201
  12. package/lib/keyring/simple-keyring.js +13 -18
  13. package/lib/message/bip322-simple.js +31 -71
  14. package/lib/message/deterministic-ecdsa.js +19 -40
  15. package/lib/message/ecdsa.js +10 -18
  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 +8 -13
  20. package/lib/runes/varint.js +6 -12
  21. package/lib/transaction/index.js +3 -19
  22. package/lib/transaction/inscription-utxo.js +45 -38
  23. package/lib/transaction/transaction.js +134 -139
  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 -60
  27. package/lib/tx-helpers/send-atomicals-nft.js +29 -44
  28. package/lib/tx-helpers/send-btc.js +53 -71
  29. package/lib/tx-helpers/send-inscription.js +32 -47
  30. package/lib/tx-helpers/send-inscriptions.js +28 -43
  31. package/lib/tx-helpers/send-runes.js +83 -97
  32. package/lib/tx-helpers/split-inscription-utxo.js +39 -54
  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 +103 -95
  40. package/lib/address/index.d.ts +0 -37
  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 -53
  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 -15
  58. package/lib/src/address/index.d.ts +0 -37
  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 -53
  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 -15
  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 -52
  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 -52
  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
package/lib/utils.js CHANGED
@@ -1,20 +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.shortAddress = exports.amountToSaothis = exports.satoshisToAmount = exports.schnorrValidator = exports.validator = exports.tweakSigner = exports.toXOnly = void 0;
7
- const bignumber_js_1 = __importDefault(require("bignumber.js"));
8
- const bitcoin_core_1 = require("./bitcoin-core");
9
- const toXOnly = (pubKey) => (pubKey.length === 32 ? pubKey : pubKey.slice(1, 33));
10
- 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));
11
4
  function tapTweakHash(pubKey, h) {
12
- 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]));
13
6
  }
14
7
  /**
15
8
  * Transform raw private key to taproot address private key
16
9
  */
17
- function tweakSigner(signer, opts = {}) {
10
+ export function tweakSigner(signer, opts = {}) {
18
11
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
19
12
  // @ts-ignore
20
13
  let privateKey = signer.privateKey;
@@ -22,50 +15,44 @@ function tweakSigner(signer, opts = {}) {
22
15
  throw new Error('Private key is required for tweaking signer!');
23
16
  }
24
17
  if (signer.publicKey[0] === 3) {
25
- privateKey = bitcoin_core_1.ecc.privateNegate(privateKey);
18
+ privateKey = ecc.privateNegate(privateKey);
26
19
  }
27
- 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));
28
21
  if (!tweakedPrivateKey) {
29
22
  throw new Error('Invalid tweaked private key!');
30
23
  }
31
- return bitcoin_core_1.ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
24
+ return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
32
25
  network: opts.network
33
26
  });
34
27
  }
35
- exports.tweakSigner = tweakSigner;
36
28
  /**
37
29
  * ECDSA signature validator
38
30
  */
39
- const validator = (pubkey, msghash, signature) => bitcoin_core_1.ECPair.fromPublicKey(pubkey).verify(msghash, signature);
40
- exports.validator = validator;
31
+ export const validator = (pubkey, msghash, signature) => ECPair.fromPublicKey(pubkey).verify(msghash, signature);
41
32
  /**
42
33
  * Schnorr signature validator
43
34
  */
44
- const schnorrValidator = (pubkey, msghash, signature) => {
45
- 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);
46
37
  };
47
- exports.schnorrValidator = schnorrValidator;
48
38
  /**
49
39
  * Transform satoshis to btc format
50
40
  */
51
- function satoshisToAmount(val) {
52
- const num = new bignumber_js_1.default(val);
41
+ export function satoshisToAmount(val) {
42
+ const num = new BigNumber(val);
53
43
  return num.dividedBy(100000000).toFixed(8);
54
44
  }
55
- exports.satoshisToAmount = satoshisToAmount;
56
45
  /**
57
46
  * Transform btc format to satoshis
58
47
  */
59
- function amountToSaothis(val) {
60
- const num = new bignumber_js_1.default(val);
48
+ export function amountToSaothis(val) {
49
+ const num = new BigNumber(val);
61
50
  return num.multipliedBy(100000000).toNumber();
62
51
  }
63
- exports.amountToSaothis = amountToSaothis;
64
- function shortAddress(address, len = 5) {
52
+ export function shortAddress(address, len = 5) {
65
53
  if (!address)
66
54
  return '';
67
55
  if (address.length <= len * 2)
68
56
  return address;
69
57
  return address.slice(0, len) + '...' + address.slice(address.length - len);
70
58
  }
71
- exports.shortAddress = shortAddress;
@@ -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';