@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.
- package/.tmp/block-parser.ts +58 -0
- package/.tmp/ordinal-helper.ts +133 -0
- package/.tmp/ordinal.ts +25 -0
- package/.tmp/rbf.ts +27 -24
- package/dist/bitcoin-interface-ordinal.d.ts +2 -2
- package/dist/bitcoin-interface-ordinal.d.ts.map +1 -1
- package/dist/bitcoin-interface-ordinal.js +1 -1
- package/dist/bitcoin-interface-ordinal.js.map +1 -1
- package/dist/bitcoin-interface-teleswap.d.ts +2 -53
- package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
- package/dist/bitcoin-interface-teleswap.js +6 -17
- package/dist/bitcoin-interface-teleswap.js.map +1 -1
- package/dist/bitcoin-interface-wallet.d.ts +29 -0
- package/dist/bitcoin-interface-wallet.d.ts.map +1 -0
- package/dist/bitcoin-interface-wallet.js +126 -0
- package/dist/bitcoin-interface-wallet.js.map +1 -0
- package/dist/bitcoin-interface.d.ts +5 -23
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +13 -92
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-wallet-base.d.ts +55 -31
- package/dist/bitcoin-wallet-base.d.ts.map +1 -1
- package/dist/bitcoin-wallet-base.js +105 -84
- package/dist/bitcoin-wallet-base.js.map +1 -1
- package/dist/ordinal-wallet.d.ts +29 -71
- package/dist/ordinal-wallet.d.ts.map +1 -1
- package/dist/ordinal-wallet.js +48 -108
- package/dist/ordinal-wallet.js.map +1 -1
- package/dist/teleswap-wallet.d.ts +10 -16
- package/dist/teleswap-wallet.d.ts.map +1 -1
- package/dist/teleswap-wallet.js +10 -30
- package/dist/teleswap-wallet.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +4 -11
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +2 -23
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts +3 -2
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/ordinal-transaction-builder.js +1 -6
- package/dist/transaction-builder/ordinal-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts +3 -7
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +22 -49
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/type.d.ts +33 -18
- package/dist/type.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/bitcoin-interface-ordinal.ts +185 -181
- package/src/bitcoin-interface-teleswap.ts +237 -252
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +114 -0
- package/src/bitcoin-interface.ts +156 -239
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +344 -310
- package/src/helper/brc20-helper.ts +179 -179
- package/src/helper/ordinal-helper.ts +118 -118
- package/src/index.ts +15 -15
- package/src/ordinal-wallet.ts +659 -748
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +133 -155
- package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -44
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +139 -147
- package/src/transaction-builder/transaction-builder.ts +690 -706
- package/src/type.ts +74 -48
- package/src/utils/networks.ts +33 -33
- package/src/utils/tools.ts +92 -92
- package/tsconfig.json +9 -9
- 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 {
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
this.
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
let
|
|
242
|
-
let
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
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
|
+
}
|