@teleportdao/bitcoin 2.3.0-alpha.0 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bitcoin-interface-utils.d.ts +0 -4
- package/dist/bitcoin-interface-utils.d.ts.map +1 -1
- package/dist/bitcoin-interface-utils.js +0 -4
- package/dist/bitcoin-interface-utils.js.map +1 -1
- package/dist/bitcoin-utils.d.ts +1 -5
- package/dist/bitcoin-utils.d.ts.map +1 -1
- package/dist/bitcoin-utils.js +2 -44
- package/dist/bitcoin-utils.js.map +1 -1
- package/dist/bitcoin-wallet-base.d.ts +8 -18
- package/dist/bitcoin-wallet-base.d.ts.map +1 -1
- package/dist/bitcoin-wallet-base.js +13 -33
- package/dist/bitcoin-wallet-base.js.map +1 -1
- package/dist/multisig-wallet-helper.d.ts +46 -0
- package/dist/multisig-wallet-helper.d.ts.map +1 -0
- package/dist/multisig-wallet-helper.js +203 -0
- package/dist/multisig-wallet-helper.js.map +1 -0
- package/dist/multisig-wallet.d.ts +20 -0
- package/dist/multisig-wallet.d.ts.map +1 -0
- package/dist/multisig-wallet.js +119 -0
- package/dist/multisig-wallet.js.map +1 -0
- package/dist/sign/sign-transaction.d.ts +1 -5
- package/dist/sign/sign-transaction.d.ts.map +1 -1
- package/dist/sign/sign-transaction.js +2 -5
- package/dist/sign/sign-transaction.js.map +1 -1
- package/dist/teleswap-wallet.d.ts +2 -2
- package/dist/teleswap-wallet.d.ts.map +1 -1
- package/dist/teleswap-wallet.js +6 -4
- package/dist/teleswap-wallet.js.map +1 -1
- package/dist/transaction-builder/coin-select.d.ts +87 -0
- package/dist/transaction-builder/coin-select.d.ts.map +1 -0
- package/dist/transaction-builder/coin-select.js +359 -0
- package/dist/transaction-builder/coin-select.js.map +1 -0
- package/dist/transaction-builder/transaction-builder.d.ts +6 -28
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +115 -195
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/package.json +4 -4
- package/src/bitcoin-interface-utils.ts +0 -12
- package/src/bitcoin-utils.ts +2 -51
- package/src/bitcoin-wallet-base.ts +22 -62
- package/src/sign/sign-transaction.ts +7 -5
- package/src/teleswap-wallet.ts +6 -1
- package/src/transaction-builder/transaction-builder.ts +131 -266
- package/src/multisig-coordinator-wallet.ts +0 -9
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
Target,
|
|
14
14
|
TargetAddress,
|
|
15
15
|
} from "./transaction-builder/transaction-builder"
|
|
16
|
-
import
|
|
16
|
+
import BitcoinSign from "./sign/sign-transaction"
|
|
17
17
|
|
|
18
18
|
import { getPubKeyFromPrivateKeyHex } from "./bitcoin-utils"
|
|
19
19
|
import networks from "./utils/networks"
|
|
@@ -24,39 +24,26 @@ const bip32 = BIP32Factory(ecc)
|
|
|
24
24
|
|
|
25
25
|
export type FeeRateType = "normal" | "slow" | "fast" | number
|
|
26
26
|
export class BitcoinBaseWallet {
|
|
27
|
-
SINGLESIG_TYPES = ["p2pkh", "p2wpkh", "p2sh-p2wpkh", "p2tr"]
|
|
28
|
-
MULTISIG_TYPES = ["p2sh", "p2wsh", "p2sh-p2wsh"]
|
|
29
|
-
|
|
30
|
-
//
|
|
31
27
|
network: Network
|
|
32
28
|
hdWalletPath: {
|
|
33
29
|
p2pkh: string
|
|
34
30
|
p2wpkh: string
|
|
35
31
|
"p2sh-p2wpkh": string
|
|
36
|
-
// multisig
|
|
37
32
|
p2sh: string
|
|
38
33
|
p2wsh: string
|
|
39
34
|
"p2sh-p2wsh": string
|
|
40
|
-
// taproot
|
|
41
35
|
p2tr: string
|
|
42
36
|
}
|
|
43
37
|
transactionBuilder: BitcoinTransactionBuilder
|
|
44
38
|
btcInterface: BitcoinInterfaceWallet
|
|
45
|
-
signer:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
currentAccountType?: string // current account type
|
|
49
|
-
addressObj?: Payment
|
|
50
|
-
|
|
39
|
+
signer: BitcoinSign
|
|
40
|
+
currentAccount?: string
|
|
41
|
+
currentAccountType?: string
|
|
51
42
|
privateKey?: Buffer
|
|
52
43
|
publicKey?: Buffer
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
publicKeys: Buffer[]
|
|
57
|
-
numberOfSigners: number
|
|
58
|
-
}
|
|
59
|
-
|
|
44
|
+
publicKeys?: Buffer[]
|
|
45
|
+
addressObj?: Payment
|
|
46
|
+
bitcoinAddress: string | undefined
|
|
60
47
|
constructor(
|
|
61
48
|
networkName: string,
|
|
62
49
|
connectionInfo?: {
|
|
@@ -75,7 +62,7 @@ export class BitcoinBaseWallet {
|
|
|
75
62
|
)
|
|
76
63
|
this.btcInterface = this.transactionBuilder.btcInterface
|
|
77
64
|
|
|
78
|
-
this.signer = new
|
|
65
|
+
this.signer = new BitcoinSign(this.network)
|
|
79
66
|
|
|
80
67
|
// initialize account
|
|
81
68
|
this.currentAccount = undefined
|
|
@@ -92,65 +79,38 @@ export class BitcoinBaseWallet {
|
|
|
92
79
|
return new BigNumber(btc).multipliedBy(1e8).toFixed(0)
|
|
93
80
|
}
|
|
94
81
|
|
|
95
|
-
get bitcoinAddress() {
|
|
96
|
-
return this.currentAccount
|
|
97
|
-
}
|
|
98
|
-
|
|
99
82
|
// initialize account
|
|
100
83
|
|
|
101
84
|
get signerInfo() {
|
|
102
85
|
return this.privateKey
|
|
103
86
|
? {
|
|
104
87
|
address: this.bitcoinAddress!,
|
|
105
|
-
addressType: this.currentAccountType!,
|
|
106
88
|
publicKey: this.publicKey!.toString("hex"),
|
|
107
|
-
|
|
108
|
-
numberOfSigners: this.multisig?.numberOfSigners,
|
|
89
|
+
addressType: this.currentAccountType!,
|
|
109
90
|
}
|
|
110
91
|
: undefined
|
|
111
92
|
}
|
|
112
93
|
|
|
113
|
-
setAccountPrivateKey(privateKeyHex: string
|
|
94
|
+
setAccountPrivateKey(privateKeyHex: string) {
|
|
114
95
|
this.privateKey = Buffer.from(privateKeyHex, "hex")
|
|
115
96
|
let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
|
|
116
97
|
this.publicKey = publicKey
|
|
117
|
-
this.setAccountType(
|
|
98
|
+
this.setAccountType("p2wpkh")
|
|
118
99
|
}
|
|
119
100
|
|
|
120
|
-
setAccountType(
|
|
121
|
-
accountType = "p2wpkh",
|
|
122
|
-
multisig?: {
|
|
123
|
-
publicKeys: string[]
|
|
124
|
-
numberOfSigners?: number
|
|
125
|
-
},
|
|
126
|
-
) {
|
|
101
|
+
setAccountType(accountType = "p2pkh") {
|
|
127
102
|
if (!this.publicKey) {
|
|
128
103
|
throw new Error("account not initialized")
|
|
129
104
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
this.publicKey.toString("hex"),
|
|
135
|
-
accountType,
|
|
136
|
-
)
|
|
137
|
-
} else {
|
|
138
|
-
if (!multisig) {
|
|
139
|
-
throw new Error("multisig is required")
|
|
140
|
-
}
|
|
141
|
-
const m = {
|
|
142
|
-
publicKeys: multisig.publicKeys.map((p) => Buffer.from(p, "hex")),
|
|
143
|
-
numberOfSigners: multisig.numberOfSigners || multisig.publicKeys.length,
|
|
144
|
-
}
|
|
145
|
-
addressObj = this.btcInterface.createMultisigAddressObjectByPublicKeys(m, accountType)
|
|
146
|
-
this.multisig = m
|
|
147
|
-
}
|
|
148
|
-
|
|
105
|
+
let addressObj = this.transactionBuilder.createAddressObject({
|
|
106
|
+
addressType: accountType,
|
|
107
|
+
publicKey: this.publicKey,
|
|
108
|
+
})
|
|
149
109
|
this.currentAccount = addressObj.address
|
|
150
110
|
this.currentAccountType = accountType
|
|
151
111
|
this.addressObj = addressObj
|
|
152
|
-
|
|
153
|
-
return addressObj.address
|
|
112
|
+
this.bitcoinAddress = addressObj.address
|
|
113
|
+
return addressObj.address!
|
|
154
114
|
}
|
|
155
115
|
|
|
156
116
|
setAccountPrivateKeyByMnemonic({
|
|
@@ -239,7 +199,7 @@ export class BitcoinBaseWallet {
|
|
|
239
199
|
staticExtendedUtxo,
|
|
240
200
|
)
|
|
241
201
|
let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
|
|
242
|
-
let
|
|
202
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
243
203
|
let txId = await this.sendSignedTx(signedTx)
|
|
244
204
|
return txId
|
|
245
205
|
}
|
|
@@ -259,7 +219,7 @@ export class BitcoinBaseWallet {
|
|
|
259
219
|
staticExtendedUtxo,
|
|
260
220
|
)
|
|
261
221
|
let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
|
|
262
|
-
let
|
|
222
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
263
223
|
let txId = await this.sendSignedTx(signedTx)
|
|
264
224
|
return txId
|
|
265
225
|
}
|
|
@@ -315,7 +275,7 @@ export class BitcoinBaseWallet {
|
|
|
315
275
|
}
|
|
316
276
|
|
|
317
277
|
async sendSignedPsbt(signedPsbt: string) {
|
|
318
|
-
let
|
|
278
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
319
279
|
let txId = await this.sendSignedTx(signedTx)
|
|
320
280
|
return txId
|
|
321
281
|
}
|
|
@@ -328,7 +288,7 @@ export class BitcoinBaseWallet {
|
|
|
328
288
|
}
|
|
329
289
|
|
|
330
290
|
async sendMultiSignedPsbt(signedPsbts: string[] = []) {
|
|
331
|
-
let
|
|
291
|
+
let signedTx = this.signer.finalizePsbts(signedPsbts)
|
|
332
292
|
let txId = await this.btcInterface.sendRawTransaction(signedTx)
|
|
333
293
|
return txId
|
|
334
294
|
}
|
|
@@ -93,13 +93,15 @@ class BitcoinLikeSignTransaction {
|
|
|
93
93
|
const finals = psbtsBase64.map((psbtBase64) =>
|
|
94
94
|
Psbt.fromBase64(psbtBase64, { network: this.network }),
|
|
95
95
|
)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
96
|
+
const psbt =
|
|
97
|
+
finals.length === 1 ? finals[0] : new Psbt({ network: this.network }).combine(...finals)
|
|
98
|
+
|
|
100
99
|
psbt.finalizeAllInputs()
|
|
100
|
+
|
|
101
101
|
let finalizeTx = psbt.extractTransaction()
|
|
102
|
-
|
|
102
|
+
// const rawTx = finalizeTx.toBuffer()
|
|
103
|
+
// const hex = rawTx.toString("hex")
|
|
104
|
+
return finalizeTx.toHex()
|
|
103
105
|
}
|
|
104
106
|
}
|
|
105
107
|
|
package/src/teleswap-wallet.ts
CHANGED
|
@@ -43,6 +43,7 @@ export class TeleswapWallet extends BitcoinBaseWallet {
|
|
|
43
43
|
appId = exchange
|
|
44
44
|
? teleswap.requestAppId.WrapAndSwap.default
|
|
45
45
|
: teleswap.requestAppId.Wrap.default,
|
|
46
|
+
speed = false,
|
|
46
47
|
fee: FeeRateType = "normal",
|
|
47
48
|
thirdPartyId?: number,
|
|
48
49
|
staticExtendedUtxo?: ExtendedUtxo[],
|
|
@@ -61,6 +62,7 @@ export class TeleswapWallet extends BitcoinBaseWallet {
|
|
|
61
62
|
this.signerInfo,
|
|
62
63
|
exchange,
|
|
63
64
|
appId,
|
|
65
|
+
speed,
|
|
64
66
|
fee,
|
|
65
67
|
thirdPartyId,
|
|
66
68
|
staticExtendedUtxo,
|
|
@@ -88,6 +90,7 @@ export class TeleswapWallet extends BitcoinBaseWallet {
|
|
|
88
90
|
appId = exchange
|
|
89
91
|
? teleswap.requestAppId.WrapAndSwap.default
|
|
90
92
|
: teleswap.requestAppId.Wrap.default,
|
|
93
|
+
speed = false,
|
|
91
94
|
fee: FeeRateType = "normal",
|
|
92
95
|
thirdPartyId?: number,
|
|
93
96
|
staticExtendedUtxo?: ExtendedUtxo[],
|
|
@@ -99,7 +102,7 @@ export class TeleswapWallet extends BitcoinBaseWallet {
|
|
|
99
102
|
appId,
|
|
100
103
|
recipientAddress,
|
|
101
104
|
networkFee,
|
|
102
|
-
speed
|
|
105
|
+
speed,
|
|
103
106
|
isExchange: !!exchange,
|
|
104
107
|
outputAmount: exchange?.outputAmount,
|
|
105
108
|
outputToken: exchange?.outputToken,
|
|
@@ -119,12 +122,14 @@ export class TeleswapWallet extends BitcoinBaseWallet {
|
|
|
119
122
|
this.transactionBuilder.getOpReturnTarget(dataHex),
|
|
120
123
|
]
|
|
121
124
|
|
|
125
|
+
const sequenceNumber = speed === true ? this.transactionBuilder.NO_RBF_SEQUENCE : undefined
|
|
122
126
|
const unsignedTx = this.transactionBuilder.processUnsignedTransaction({
|
|
123
127
|
extendedUtxo,
|
|
124
128
|
feeRate,
|
|
125
129
|
targets,
|
|
126
130
|
changeAddress: fullAmount ? lockerAddress : changeAddress || signer.address,
|
|
127
131
|
fullAmount,
|
|
132
|
+
sequenceNumber,
|
|
128
133
|
})
|
|
129
134
|
|
|
130
135
|
return unsignedTx
|