@teleportdao/bitcoin 1.9.0 → 2.0.0

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 (70) hide show
  1. package/.tmp/block-parser.ts +58 -0
  2. package/.tmp/ordinal-helper.ts +133 -0
  3. package/.tmp/ordinal.ts +25 -0
  4. package/.tmp/rbf.ts +27 -24
  5. package/dist/bitcoin-interface-ordinal.d.ts +2 -2
  6. package/dist/bitcoin-interface-ordinal.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-ordinal.js +1 -1
  8. package/dist/bitcoin-interface-ordinal.js.map +1 -1
  9. package/dist/bitcoin-interface-teleswap.d.ts +2 -53
  10. package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
  11. package/dist/bitcoin-interface-teleswap.js +6 -17
  12. package/dist/bitcoin-interface-teleswap.js.map +1 -1
  13. package/dist/bitcoin-interface-wallet.d.ts +29 -0
  14. package/dist/bitcoin-interface-wallet.d.ts.map +1 -0
  15. package/dist/bitcoin-interface-wallet.js +126 -0
  16. package/dist/bitcoin-interface-wallet.js.map +1 -0
  17. package/dist/bitcoin-interface.d.ts +5 -23
  18. package/dist/bitcoin-interface.d.ts.map +1 -1
  19. package/dist/bitcoin-interface.js +13 -92
  20. package/dist/bitcoin-interface.js.map +1 -1
  21. package/dist/bitcoin-wallet-base.d.ts +55 -31
  22. package/dist/bitcoin-wallet-base.d.ts.map +1 -1
  23. package/dist/bitcoin-wallet-base.js +105 -84
  24. package/dist/bitcoin-wallet-base.js.map +1 -1
  25. package/dist/ordinal-wallet.d.ts +29 -71
  26. package/dist/ordinal-wallet.d.ts.map +1 -1
  27. package/dist/ordinal-wallet.js +48 -108
  28. package/dist/ordinal-wallet.js.map +1 -1
  29. package/dist/teleswap-wallet.d.ts +10 -16
  30. package/dist/teleswap-wallet.d.ts.map +1 -1
  31. package/dist/teleswap-wallet.js +10 -30
  32. package/dist/teleswap-wallet.js.map +1 -1
  33. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +4 -11
  34. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  35. package/dist/transaction-builder/bitcoin-transaction-builder.js +2 -23
  36. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  37. package/dist/transaction-builder/ordinal-transaction-builder.d.ts +3 -2
  38. package/dist/transaction-builder/ordinal-transaction-builder.d.ts.map +1 -1
  39. package/dist/transaction-builder/ordinal-transaction-builder.js +1 -6
  40. package/dist/transaction-builder/ordinal-transaction-builder.js.map +1 -1
  41. package/dist/transaction-builder/transaction-builder.d.ts +3 -7
  42. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  43. package/dist/transaction-builder/transaction-builder.js +22 -49
  44. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  45. package/dist/type.d.ts +33 -18
  46. package/dist/type.d.ts.map +1 -1
  47. package/package.json +4 -4
  48. package/src/bitcoin-interface-ordinal.ts +185 -181
  49. package/src/bitcoin-interface-teleswap.ts +237 -252
  50. package/src/bitcoin-interface-utils.ts +60 -60
  51. package/src/bitcoin-interface-wallet.ts +114 -0
  52. package/src/bitcoin-interface.ts +156 -239
  53. package/src/bitcoin-utils.ts +591 -591
  54. package/src/bitcoin-wallet-base.ts +344 -310
  55. package/src/helper/brc20-helper.ts +179 -179
  56. package/src/helper/ordinal-helper.ts +118 -118
  57. package/src/index.ts +15 -15
  58. package/src/ordinal-wallet.ts +659 -748
  59. package/src/sign/index.ts +1 -1
  60. package/src/sign/sign-transaction.ts +108 -108
  61. package/src/teleswap-wallet.ts +133 -155
  62. package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -44
  63. package/src/transaction-builder/index.ts +3 -3
  64. package/src/transaction-builder/ordinal-transaction-builder.ts +139 -147
  65. package/src/transaction-builder/transaction-builder.ts +690 -706
  66. package/src/type.ts +74 -48
  67. package/src/utils/networks.ts +33 -33
  68. package/src/utils/tools.ts +92 -92
  69. package/tsconfig.json +9 -9
  70. package/webpack.config.js +16 -16
@@ -1,310 +1,344 @@
1
- import * as bip39 from "bip39"
2
- import { hdWalletPath } from "@teleportdao/configs"
3
- import { Network, Payment } from "bitcoinjs-lib"
4
- import BIP32Factory from "bip32"
5
- import ecc from "@bitcoinerlab/secp256k1"
6
- import BigNumber from "bignumber.js"
7
- import { BitcoinTransactionBuilder } from "./transaction-builder"
8
- import type { BitcoinConnectionInfo } from "./type"
9
-
10
- import type { ExtendedUtxo, SignerInfo, Target } from "./transaction-builder/transaction-builder"
11
- import BitcoinSign from "./sign/sign-transaction"
12
-
13
- import { getPubKeyFromPrivateKeyHex } from "./bitcoin-utils"
14
- import networks from "./utils/networks"
15
- import { BitcoinInterface } from "./bitcoin-interface"
16
-
17
- const bip32 = BIP32Factory(ecc)
18
-
19
- export class BitcoinBaseWallet {
20
- network: Network
21
- hdWalletPath: {
22
- p2pkh: string
23
- p2wpkh: string
24
- "p2sh-p2wpkh": string
25
- p2sh: string
26
- p2wsh: string
27
- "p2sh-p2wsh": string
28
- p2tr: string
29
- }
30
- transactionBuilder: BitcoinTransactionBuilder
31
- btcInterface: BitcoinInterface
32
- signer: BitcoinSign
33
- currentAccount?: string
34
- currentAccountType?: string
35
- privateKey?: Buffer
36
- publicKey?: Buffer
37
- publicKeys?: Buffer[]
38
- addressObj?: Payment
39
- bitcoinAddress: string | undefined
40
- constructor(
41
- networkName: string,
42
- connectionInfo: BitcoinConnectionInfo = {
43
- api: {
44
- provider: "BlockStream",
45
- },
46
- },
47
- ) {
48
- this.network = networks[networkName]
49
- this.hdWalletPath = hdWalletPath.bitcoin
50
-
51
- this.transactionBuilder = new BitcoinTransactionBuilder(
52
- connectionInfo,
53
- networkName,
54
- this.network,
55
- )
56
- this.btcInterface = this.transactionBuilder.btcInterface
57
-
58
- this.signer = new BitcoinSign(this.network)
59
-
60
- this.currentAccount = undefined
61
- this.currentAccountType = undefined
62
-
63
- this.privateKey = undefined
64
- this.publicKey = undefined
65
- // todo multisig
66
- this.publicKeys = []
67
- }
68
-
69
- get signerInfo() {
70
- return this.privateKey
71
- ? {
72
- address: this.bitcoinAddress!,
73
- publicKey: this.publicKey!.toString("hex"),
74
- addressType: this.currentAccountType!,
75
- }
76
- : undefined
77
- }
78
-
79
- createTransactionInputsAndOutputs({
80
- targets,
81
- extendedUtxo,
82
- changeAddress,
83
- feeRate,
84
- }: {
85
- targets: Target[]
86
- extendedUtxo: ExtendedUtxo[]
87
- changeAddress: string
88
- feeRate: number
89
- fullAmount?: boolean
90
- }) {
91
- return this.transactionBuilder.helperHandleInputsAndOutputs({
92
- targets,
93
- extendedUtxo,
94
- changeObject: {
95
- address: changeAddress,
96
- },
97
- feeRate,
98
- })
99
- }
100
-
101
- checkBalanceIsSufficient({
102
- targets,
103
- extendedUtxo,
104
- changeAddress,
105
- feeRate,
106
- fullAmount = false,
107
- }: {
108
- targets: Target[]
109
- extendedUtxo: ExtendedUtxo[]
110
- changeAddress: string
111
- feeRate: number
112
- fullAmount?: boolean
113
- }) {
114
- try {
115
- this.transactionBuilder.helperHandleInputsAndOutputs({
116
- targets,
117
- extendedUtxo,
118
- changeObject: {
119
- address: changeAddress,
120
- },
121
- feeRate,
122
- })
123
- return true
124
- } catch (err) {
125
- return false
126
- }
127
- }
128
-
129
- // todo : not completed
130
- setMultiSigAccount(accountType = "p2sh") {
131
- throw new Error("not supported yet")
132
-
133
- /* eslint-disable no-unreachable */
134
- // todo : not completed
135
- switch (accountType) {
136
- // case 'p2sh':
137
- // this.currentAccount = ''
138
- // break
139
- // case 'p2wsh':
140
- // this.currentAccount = ''
141
- // break
142
- // case 'p2sh-p2wsh':
143
- // this.currentAccount = ''
144
- // break
145
- default:
146
- throw new Error("accountType is incorrect")
147
- }
148
- this.currentAccountType = accountType
149
- }
150
-
151
- setAccountPrivateKey(privateKeyHex: string) {
152
- this.privateKey = Buffer.from(privateKeyHex, "hex")
153
- let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
154
- this.publicKey = publicKey
155
- }
156
-
157
- setAccountPrivateKeyByMnemonic({
158
- mnemonic,
159
- mnemonicPassword = "",
160
- index = 0,
161
- walletNumber = 0,
162
- addressType = "p2sh-p2wpkh",
163
- }: {
164
- mnemonic: string
165
- mnemonicPassword?: string
166
- index?: number
167
- walletNumber?: number
168
- addressType?: string
169
- }) {
170
- if (!bip39.validateMnemonic(mnemonic)) throw new Error("invalid mnemonic")
171
- const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword)
172
- const node = bip32.fromSeed(seed)
173
-
174
- let basePath = this.hdWalletPath[addressType as keyof typeof this.hdWalletPath]
175
- if (!basePath) {
176
- throw new Error("incorrect path or addressType")
177
- }
178
- const path = `${basePath}/${walletNumber}`
179
- const account = node.derivePath(path)
180
- const userKeyPair = account.derive(index)
181
- this.setAccountPrivateKey(userKeyPair.privateKey!.toString("hex"))
182
- return this.setAccount(addressType)
183
- }
184
-
185
- setAccountPublicKey(publicKeyHex: string) {
186
- this.publicKey = Buffer.from(publicKeyHex, "hex")
187
- }
188
-
189
- setAccount(accountType = "p2pkh") {
190
- if (!this.publicKey) {
191
- throw new Error("account not initialized")
192
- }
193
- let addressObj = this.transactionBuilder.createAddressObject({
194
- addressType: accountType,
195
- publicKey: this.publicKey,
196
- })
197
- this.currentAccount = addressObj.address
198
- this.currentAccountType = accountType
199
- this.addressObj = addressObj
200
- this.bitcoinAddress = addressObj.address
201
- return addressObj.address
202
- }
203
-
204
- //
205
-
206
- async getExtendedUtxo(input: SignerInfo) {
207
- return this.transactionBuilder.getExtendedUtxo(input)
208
- }
209
-
210
- async send(
211
- receiverAddress: string,
212
- amount: string | number,
213
- fullAmount = false,
214
- speed: "normal" | "slow" | "fast" = "normal",
215
- ) {
216
- if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
217
- throw new Error("account not initialized")
218
- }
219
-
220
- let extendedUtxo = await this.getExtendedUtxo({
221
- address: this.currentAccount,
222
- addressType: this.currentAccountType,
223
- publicKey: this.publicKey.toString("hex"),
224
- })
225
- if (!fullAmount && BigNumber(amount).isEqualTo(0))
226
- throw new Error("incorrect amount. amount should be in satoshi")
227
- let feeRate = await this.transactionBuilder._getFeeRate(speed)
228
- let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
229
- extendedUtxo,
230
- targets: [
231
- {
232
- address: receiverAddress,
233
- value: +amount,
234
- },
235
- ],
236
- changeAddress: this.currentAccount,
237
- feeRate,
238
- fullAmount,
239
- })
240
- let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
241
- let signedTx = this.signer.finalizePsbts([signedPsbt])
242
- let txId = await this.transactionBuilder.sendTx(signedTx)
243
- return txId
244
- }
245
-
246
- async sendSignedPsbt(signedPsbt: string) {
247
- let signedTx = this.signer.finalizePsbts([signedPsbt])
248
- let txId = await this.transactionBuilder.sendTx(signedTx)
249
- return txId
250
- }
251
-
252
- async sendSignedTx(signedTx: string) {
253
- let txId = await this.transactionBuilder.sendTx(signedTx)
254
- return txId
255
- }
256
-
257
- async sendMultiSignedPsbt(signedPsbts: string[] = []) {
258
- let signedTx = this.signer.finalizePsbts(signedPsbts)
259
- let txId = await this.transactionBuilder.sendTx(signedTx)
260
- return txId
261
- }
262
-
263
- async increaseTransactionFeeUnsignedPsbt(
264
- txId: string,
265
- signerInfos: SignerInfo[],
266
- extraExtendedUtxo: ExtendedUtxo[],
267
- changeAddress: string,
268
- staticFeeRate?: number,
269
- ) {
270
- let transaction = await this.btcInterface.apiProvider.getTransaction(txId)
271
-
272
- let extendedUtxo = transaction.vin.map((vi) => ({
273
- signerInfo: signerInfos.find((s) => s.address === vi.address)!,
274
- hash: vi.txId,
275
- value: +vi.value,
276
- index: vi.index,
277
- }))
278
-
279
- if (extendedUtxo.find((x) => !x.signerInfo?.address)) {
280
- throw new Error("signerInfo not match")
281
- }
282
-
283
- let changeIndex = transaction.vout.findIndex((vo) =>
284
- transaction.vin.find((vi) => vo.address === vi.address || vo.address === changeAddress),
285
- )
286
-
287
- const feeRate = staticFeeRate || (await this.transactionBuilder._getFeeRate("fast"))
288
-
289
- let targets = transaction.vout
290
- .filter((_, index) => index !== changeIndex)
291
- .map((vo) =>
292
- vo.address
293
- ? {
294
- address: vo.address,
295
- value: vo.value,
296
- }
297
- : {
298
- script: Buffer.from(vo.script, "hex"),
299
- value: vo.value,
300
- },
301
- )
302
- return this.transactionBuilder.processUnsignedTransaction({
303
- extendedUtxo: [...extendedUtxo, ...extraExtendedUtxo],
304
- targets,
305
- feeRate,
306
- changeAddress: changeIndex >= 0 ? transaction.vout[changeIndex].address : changeAddress,
307
- selectType: "inOrder",
308
- })
309
- }
310
- }
1
+ import * as bip39 from "bip39"
2
+ import { hdWalletPath } from "@teleportdao/configs"
3
+ import { Network, Payment } from "bitcoinjs-lib"
4
+ import BIP32Factory from "bip32"
5
+ import ecc from "@bitcoinerlab/secp256k1"
6
+ import BigNumber from "bignumber.js"
7
+ import { BitcoinTransactionBuilder } from "./transaction-builder"
8
+ import type { APIConnectionInfo, RPCConnectionInfo, UtxoConnectionInfo } from "./type"
9
+
10
+ import type {
11
+ ExtendedUtxo,
12
+ SignerInfo,
13
+ Target,
14
+ TargetAddress,
15
+ } from "./transaction-builder/transaction-builder"
16
+ import BitcoinSign from "./sign/sign-transaction"
17
+
18
+ import { getPubKeyFromPrivateKeyHex } from "./bitcoin-utils"
19
+ import networks from "./utils/networks"
20
+ import { BitcoinInterfaceWallet } from "./bitcoin-interface-wallet"
21
+ import { runWithRetries } from "./utils/tools"
22
+
23
+ const bip32 = BIP32Factory(ecc)
24
+
25
+ export type FeeRateType = "normal" | "slow" | "fast" | number
26
+ export class BitcoinBaseWallet {
27
+ network: Network
28
+ hdWalletPath: {
29
+ p2pkh: string
30
+ p2wpkh: string
31
+ "p2sh-p2wpkh": string
32
+ p2sh: string
33
+ p2wsh: string
34
+ "p2sh-p2wsh": string
35
+ p2tr: string
36
+ }
37
+ transactionBuilder: BitcoinTransactionBuilder
38
+ btcInterface: BitcoinInterfaceWallet
39
+ signer: BitcoinSign
40
+ currentAccount?: string
41
+ currentAccountType?: string
42
+ privateKey?: Buffer
43
+ publicKey?: Buffer
44
+ publicKeys?: Buffer[]
45
+ addressObj?: Payment
46
+ bitcoinAddress: string | undefined
47
+ constructor(
48
+ networkName: string,
49
+ connectionInfo?: {
50
+ utxo?: UtxoConnectionInfo
51
+ // rpc used for getRawTransaction in transaction builder if set (optional)
52
+ rpc?: RPCConnectionInfo
53
+ },
54
+ ) {
55
+ this.network = networks[networkName]
56
+ this.hdWalletPath = hdWalletPath.bitcoin
57
+
58
+ this.transactionBuilder = new BitcoinTransactionBuilder(
59
+ networkName,
60
+ this.network,
61
+ connectionInfo,
62
+ )
63
+ this.btcInterface = this.transactionBuilder.btcInterface
64
+
65
+ this.signer = new BitcoinSign(this.network)
66
+
67
+ // initialize account
68
+ this.currentAccount = undefined
69
+ this.currentAccountType = undefined
70
+ this.privateKey = undefined
71
+ this.publicKey = undefined
72
+ }
73
+
74
+ static satoshiToBTC(satoshi: number | string) {
75
+ return new BigNumber(satoshi).dividedBy(1e8).toString()
76
+ }
77
+
78
+ static btcToSatoshi(btc: number | string) {
79
+ return new BigNumber(btc).multipliedBy(1e8).toFixed(0)
80
+ }
81
+
82
+ // initialize account
83
+
84
+ get signerInfo() {
85
+ return this.privateKey
86
+ ? {
87
+ address: this.bitcoinAddress!,
88
+ publicKey: this.publicKey!.toString("hex"),
89
+ addressType: this.currentAccountType!,
90
+ }
91
+ : undefined
92
+ }
93
+
94
+ setAccountPrivateKey(privateKeyHex: string) {
95
+ this.privateKey = Buffer.from(privateKeyHex, "hex")
96
+ let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
97
+ this.publicKey = publicKey
98
+ this.setAccountType("p2wpkh")
99
+ }
100
+
101
+ setAccountType(accountType = "p2pkh") {
102
+ if (!this.publicKey) {
103
+ throw new Error("account not initialized")
104
+ }
105
+ let addressObj = this.transactionBuilder.createAddressObject({
106
+ addressType: accountType,
107
+ publicKey: this.publicKey,
108
+ })
109
+ this.currentAccount = addressObj.address
110
+ this.currentAccountType = accountType
111
+ this.addressObj = addressObj
112
+ this.bitcoinAddress = addressObj.address
113
+ return addressObj.address!
114
+ }
115
+
116
+ setAccountPrivateKeyByMnemonic({
117
+ mnemonic,
118
+ mnemonicPassword = "",
119
+ index = 0,
120
+ walletNumber = 0,
121
+ addressType = "p2sh-p2wpkh",
122
+ }: {
123
+ mnemonic: string
124
+ mnemonicPassword?: string
125
+ index?: number
126
+ walletNumber?: number
127
+ addressType?: string
128
+ }) {
129
+ if (!bip39.validateMnemonic(mnemonic)) throw new Error("invalid mnemonic")
130
+ const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword)
131
+ const node = bip32.fromSeed(seed)
132
+
133
+ let basePath = this.hdWalletPath[addressType as keyof typeof this.hdWalletPath]
134
+ if (!basePath) {
135
+ throw new Error("incorrect path or addressType")
136
+ }
137
+ const path = `${basePath}/${walletNumber}`
138
+ const account = node.derivePath(path)
139
+ const userKeyPair = account.derive(index)
140
+ this.setAccountPrivateKey(userKeyPair.privateKey!.toString("hex"))
141
+ return this.setAccountType(addressType)
142
+ }
143
+
144
+ // helper function
145
+
146
+ checkBalanceIsSufficient({
147
+ targets,
148
+ extendedUtxo,
149
+ changeAddress,
150
+ feeRate,
151
+ }: {
152
+ targets: Target[]
153
+ extendedUtxo: ExtendedUtxo[]
154
+ changeAddress: string
155
+ feeRate: number
156
+ }) {
157
+ try {
158
+ this.transactionBuilder.helperHandleInputsAndOutputs({
159
+ targets,
160
+ extendedUtxo,
161
+ changeObject: {
162
+ address: changeAddress,
163
+ },
164
+ feeRate,
165
+ })
166
+ return true
167
+ } catch (err) {
168
+ return false
169
+ }
170
+ }
171
+
172
+ async getExtendedUtxo(input: SignerInfo) {
173
+ return this.btcInterface.getExtendedUtxo(input)
174
+ }
175
+
176
+ async getFeeRate(feeRate: FeeRateType = "normal") {
177
+ if (feeRate === 0) throw new Error("feeRate should be greater than 0")
178
+ if (+feeRate > 0) return +feeRate
179
+ if (typeof feeRate === "string") return this.btcInterface.getFeeRate(feeRate)
180
+ throw new Error("incorrect feeRate")
181
+ }
182
+
183
+ // signed methods
184
+
185
+ async sendBTC(
186
+ receiverAddress: string,
187
+ amountInSatoshi: number | "all",
188
+ fee: FeeRateType = "normal",
189
+ staticExtendedUtxo?: ExtendedUtxo[],
190
+ ) {
191
+ if (!this.signerInfo! || !this.privateKey) {
192
+ throw new Error("account not initialized")
193
+ }
194
+ const unsignedTx = await this.sendBTCUnsignedTx(
195
+ receiverAddress,
196
+ amountInSatoshi,
197
+ this.signerInfo!,
198
+ fee,
199
+ staticExtendedUtxo,
200
+ )
201
+ let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
202
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
203
+ let txId = await this.sendSignedTx(signedTx)
204
+ return txId
205
+ }
206
+
207
+ async sendBTCMultipleAddress(
208
+ receivers: TargetAddress[],
209
+ fee: FeeRateType = "normal",
210
+ staticExtendedUtxo?: ExtendedUtxo[],
211
+ ) {
212
+ if (!this.signerInfo! || !this.privateKey) {
213
+ throw new Error("account not initialized")
214
+ }
215
+ const unsignedTx = await this.sendBTCToMultipleAddressUnsignedTx(
216
+ receivers,
217
+ this.signerInfo!,
218
+ fee,
219
+ staticExtendedUtxo,
220
+ )
221
+ let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
222
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
223
+ let txId = await this.sendSignedTx(signedTx)
224
+ return txId
225
+ }
226
+
227
+ // unsigned methods
228
+ async sendBTCToMultipleAddressUnsignedTx(
229
+ receivers: TargetAddress[],
230
+ signerInfo: SignerInfo,
231
+ fee: FeeRateType = "normal",
232
+ staticExtendedUtxo?: ExtendedUtxo[],
233
+ fullAmount = false,
234
+ ) {
235
+ if (!fullAmount) {
236
+ receivers.forEach((r) => {
237
+ if (BigNumber(r.value).isEqualTo(0)) throw new Error("incorrect amount")
238
+ })
239
+ } else if (receivers.length > 1) throw new Error("fullAmount only support one receiver")
240
+
241
+ let feeRate = await this.getFeeRate(fee)
242
+ let extendedUtxo = staticExtendedUtxo || (await this.getExtendedUtxo(signerInfo))
243
+
244
+ let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
245
+ extendedUtxo,
246
+ targets: receivers,
247
+ changeAddress: signerInfo.address,
248
+ feeRate,
249
+ fullAmount,
250
+ })
251
+ return unsignedTx
252
+ }
253
+
254
+ async sendBTCUnsignedTx(
255
+ receiver: string,
256
+ amountInSatoshi: number | "all",
257
+ signerInfo: SignerInfo,
258
+ fee: FeeRateType = "normal",
259
+ staticExtendedUtxo?: ExtendedUtxo[],
260
+ ) {
261
+ return this.sendBTCToMultipleAddressUnsignedTx(
262
+ [{ address: receiver, value: amountInSatoshi === "all" ? 0 : amountInSatoshi }],
263
+ signerInfo,
264
+ fee,
265
+ staticExtendedUtxo,
266
+ amountInSatoshi === "all",
267
+ )
268
+ }
269
+
270
+ // send tx
271
+
272
+ async sendSignedTx(signedTx: string) {
273
+ let txId = await this.btcInterface.sendRawTransaction(signedTx)
274
+ return txId
275
+ }
276
+
277
+ async sendSignedPsbt(signedPsbt: string) {
278
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
279
+ let txId = await this.sendSignedTx(signedTx)
280
+ return txId
281
+ }
282
+
283
+ async sendSignedPsbtWithRetry(signedPsbt: string, { maxTries = 5, retrySleep = 5000 } = {}) {
284
+ return runWithRetries(() => this.sendSignedPsbt(signedPsbt), {
285
+ retrySleep,
286
+ maxTries,
287
+ })
288
+ }
289
+
290
+ async sendMultiSignedPsbt(signedPsbts: string[] = []) {
291
+ let signedTx = this.signer.finalizePsbts(signedPsbts)
292
+ let txId = await this.btcInterface.sendRawTransaction(signedTx)
293
+ return txId
294
+ }
295
+
296
+ // increase transaction fee (beta vesrion)
297
+ async increaseTransactionFeeUnsignedPsbt(
298
+ txId: string,
299
+ signerInfos: SignerInfo[],
300
+ extraExtendedUtxo: ExtendedUtxo[],
301
+ changeAddress: string,
302
+ staticFeeRate?: number,
303
+ ) {
304
+ let transaction = await this.btcInterface.apiProvider.getTransaction(txId)
305
+
306
+ let extendedUtxo = transaction.vin.map((vi) => ({
307
+ signerInfo: signerInfos.find((s) => s.address === vi.address)!,
308
+ hash: vi.txId,
309
+ value: +vi.value,
310
+ index: vi.index,
311
+ }))
312
+
313
+ if (extendedUtxo.find((x) => !x.signerInfo?.address)) {
314
+ throw new Error("signerInfo not match")
315
+ }
316
+
317
+ let changeIndex = transaction.vout.findIndex((vo) =>
318
+ transaction.vin.find((vi) => vo.address === vi.address || vo.address === changeAddress),
319
+ )
320
+
321
+ const feeRate = staticFeeRate || (await this.btcInterface.getFeeRate("fast"))
322
+
323
+ let targets = transaction.vout
324
+ .filter((_, index) => index !== changeIndex)
325
+ .map((vo) =>
326
+ vo.address
327
+ ? {
328
+ address: vo.address,
329
+ value: vo.value,
330
+ }
331
+ : {
332
+ script: Buffer.from(vo.script, "hex"),
333
+ value: vo.value,
334
+ },
335
+ )
336
+ return this.transactionBuilder.processUnsignedTransaction({
337
+ extendedUtxo: [...extendedUtxo, ...extraExtendedUtxo],
338
+ targets,
339
+ feeRate,
340
+ changeAddress: changeIndex >= 0 ? transaction.vout[changeIndex].address : changeAddress,
341
+ selectType: "inOrder",
342
+ })
343
+ }
344
+ }