@teleportdao/bitcoin 2.2.5 → 2.3.0-alpha.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 (35) hide show
  1. package/dist/bitcoin-interface-utils.d.ts +4 -0
  2. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  3. package/dist/bitcoin-interface-utils.js +4 -0
  4. package/dist/bitcoin-interface-utils.js.map +1 -1
  5. package/dist/bitcoin-utils.d.ts +5 -1
  6. package/dist/bitcoin-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-utils.js +44 -2
  8. package/dist/bitcoin-utils.js.map +1 -1
  9. package/dist/bitcoin-wallet-base copy.d.ts +122 -0
  10. package/dist/bitcoin-wallet-base copy.d.ts.map +1 -0
  11. package/dist/bitcoin-wallet-base copy.js +279 -0
  12. package/dist/bitcoin-wallet-base copy.js.map +1 -0
  13. package/dist/bitcoin-wallet-base.d.ts +18 -8
  14. package/dist/bitcoin-wallet-base.d.ts.map +1 -1
  15. package/dist/bitcoin-wallet-base.js +33 -13
  16. package/dist/bitcoin-wallet-base.js.map +1 -1
  17. package/dist/multisig-coordinator-wallet.d.ts +2 -0
  18. package/dist/multisig-coordinator-wallet.d.ts.map +1 -0
  19. package/dist/multisig-coordinator-wallet.js +6 -0
  20. package/dist/multisig-coordinator-wallet.js.map +1 -0
  21. package/dist/sign/sign-transaction.d.ts +5 -1
  22. package/dist/sign/sign-transaction.d.ts.map +1 -1
  23. package/dist/sign/sign-transaction.js +5 -2
  24. package/dist/sign/sign-transaction.js.map +1 -1
  25. package/dist/transaction-builder/transaction-builder.d.ts +26 -1
  26. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  27. package/dist/transaction-builder/transaction-builder.js +192 -110
  28. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  29. package/package.json +3 -3
  30. package/src/bitcoin-interface-utils.ts +12 -0
  31. package/src/bitcoin-utils.ts +51 -2
  32. package/src/bitcoin-wallet-base.ts +62 -22
  33. package/src/multisig-coordinator-wallet.ts +9 -0
  34. package/src/sign/sign-transaction.ts +5 -7
  35. package/src/transaction-builder/transaction-builder.ts +264 -124
@@ -13,7 +13,7 @@ import type {
13
13
  Target,
14
14
  TargetAddress,
15
15
  } from "./transaction-builder/transaction-builder"
16
- import BitcoinSign from "./sign/sign-transaction"
16
+ import BitcoinSigner from "./sign/sign-transaction"
17
17
 
18
18
  import { getPubKeyFromPrivateKeyHex } from "./bitcoin-utils"
19
19
  import networks from "./utils/networks"
@@ -24,26 +24,39 @@ 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
+ //
27
31
  network: Network
28
32
  hdWalletPath: {
29
33
  p2pkh: string
30
34
  p2wpkh: string
31
35
  "p2sh-p2wpkh": string
36
+ // multisig
32
37
  p2sh: string
33
38
  p2wsh: string
34
39
  "p2sh-p2wsh": string
40
+ // taproot
35
41
  p2tr: string
36
42
  }
37
43
  transactionBuilder: BitcoinTransactionBuilder
38
44
  btcInterface: BitcoinInterfaceWallet
39
- signer: BitcoinSign
40
- currentAccount?: string
41
- currentAccountType?: string
45
+ signer: BitcoinSigner
46
+
47
+ currentAccount?: string // current account address
48
+ currentAccountType?: string // current account type
49
+ addressObj?: Payment
50
+
42
51
  privateKey?: Buffer
43
52
  publicKey?: Buffer
44
- publicKeys?: Buffer[]
45
- addressObj?: Payment
46
- bitcoinAddress: string | undefined
53
+
54
+ // multisig
55
+ multisig?: {
56
+ publicKeys: Buffer[]
57
+ numberOfSigners: number
58
+ }
59
+
47
60
  constructor(
48
61
  networkName: string,
49
62
  connectionInfo?: {
@@ -62,7 +75,7 @@ export class BitcoinBaseWallet {
62
75
  )
63
76
  this.btcInterface = this.transactionBuilder.btcInterface
64
77
 
65
- this.signer = new BitcoinSign(this.network)
78
+ this.signer = new BitcoinSigner(this.network)
66
79
 
67
80
  // initialize account
68
81
  this.currentAccount = undefined
@@ -79,38 +92,65 @@ export class BitcoinBaseWallet {
79
92
  return new BigNumber(btc).multipliedBy(1e8).toFixed(0)
80
93
  }
81
94
 
95
+ get bitcoinAddress() {
96
+ return this.currentAccount
97
+ }
98
+
82
99
  // initialize account
83
100
 
84
101
  get signerInfo() {
85
102
  return this.privateKey
86
103
  ? {
87
104
  address: this.bitcoinAddress!,
88
- publicKey: this.publicKey!.toString("hex"),
89
105
  addressType: this.currentAccountType!,
106
+ publicKey: this.publicKey!.toString("hex"),
107
+ publicKeys: this.multisig?.publicKeys.map((p) => p.toString("hex")),
108
+ numberOfSigners: this.multisig?.numberOfSigners,
90
109
  }
91
110
  : undefined
92
111
  }
93
112
 
94
- setAccountPrivateKey(privateKeyHex: string) {
113
+ setAccountPrivateKey(privateKeyHex: string, accountType = "p2wpkh") {
95
114
  this.privateKey = Buffer.from(privateKeyHex, "hex")
96
115
  let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
97
116
  this.publicKey = publicKey
98
- this.setAccountType("p2wpkh")
117
+ this.setAccountType(accountType)
99
118
  }
100
119
 
101
- setAccountType(accountType = "p2pkh") {
120
+ setAccountType(
121
+ accountType = "p2wpkh",
122
+ multisig?: {
123
+ publicKeys: string[]
124
+ numberOfSigners?: number
125
+ },
126
+ ) {
102
127
  if (!this.publicKey) {
103
128
  throw new Error("account not initialized")
104
129
  }
105
- let addressObj = this.transactionBuilder.createAddressObject({
106
- addressType: accountType,
107
- publicKey: this.publicKey,
108
- })
130
+
131
+ let addressObj
132
+ if (this.SINGLESIG_TYPES.includes(accountType)) {
133
+ addressObj = this.btcInterface.createAddressObjectByPublicKey(
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
+
109
149
  this.currentAccount = addressObj.address
110
150
  this.currentAccountType = accountType
111
151
  this.addressObj = addressObj
112
- this.bitcoinAddress = addressObj.address
113
- return addressObj.address!
152
+ if (!addressObj.address) throw new Error("address not exist")
153
+ return addressObj.address
114
154
  }
115
155
 
116
156
  setAccountPrivateKeyByMnemonic({
@@ -199,7 +239,7 @@ export class BitcoinBaseWallet {
199
239
  staticExtendedUtxo,
200
240
  )
201
241
  let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
202
- let signedTx = this.signer.finalizePsbts([signedPsbt])
242
+ let { hex: signedTx } = this.signer.finalizePsbts([signedPsbt])
203
243
  let txId = await this.sendSignedTx(signedTx)
204
244
  return txId
205
245
  }
@@ -219,7 +259,7 @@ export class BitcoinBaseWallet {
219
259
  staticExtendedUtxo,
220
260
  )
221
261
  let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
222
- let signedTx = this.signer.finalizePsbts([signedPsbt])
262
+ let { hex: signedTx } = this.signer.finalizePsbts([signedPsbt])
223
263
  let txId = await this.sendSignedTx(signedTx)
224
264
  return txId
225
265
  }
@@ -275,7 +315,7 @@ export class BitcoinBaseWallet {
275
315
  }
276
316
 
277
317
  async sendSignedPsbt(signedPsbt: string) {
278
- let signedTx = this.signer.finalizePsbts([signedPsbt])
318
+ let { hex: signedTx } = this.signer.finalizePsbts([signedPsbt])
279
319
  let txId = await this.sendSignedTx(signedTx)
280
320
  return txId
281
321
  }
@@ -288,7 +328,7 @@ export class BitcoinBaseWallet {
288
328
  }
289
329
 
290
330
  async sendMultiSignedPsbt(signedPsbts: string[] = []) {
291
- let signedTx = this.signer.finalizePsbts(signedPsbts)
331
+ let { hex: signedTx } = this.signer.finalizePsbts(signedPsbts)
292
332
  let txId = await this.btcInterface.sendRawTransaction(signedTx)
293
333
  return txId
294
334
  }
@@ -0,0 +1,9 @@
1
+ import { BitcoinBaseWallet } from "./bitcoin-wallet-base"
2
+
3
+ class BitcoinMultiSigWithCoordinatorWallet extends BitcoinBaseWallet {
4
+ // register wallet
5
+ // get wallet state
6
+ // get utxo and feeRate
7
+ // sendTx
8
+ // get tx state
9
+ }
@@ -93,15 +93,13 @@ class BitcoinLikeSignTransaction {
93
93
  const finals = psbtsBase64.map((psbtBase64) =>
94
94
  Psbt.fromBase64(psbtBase64, { network: this.network }),
95
95
  )
96
- const psbt =
97
- finals.length === 1 ? finals[0] : new Psbt({ network: this.network }).combine(...finals)
98
-
96
+ let psbt = finals[0]
97
+ for (let i = 1; i < finals.length; i += 1) {
98
+ psbt.combine(finals[i])
99
+ }
99
100
  psbt.finalizeAllInputs()
100
-
101
101
  let finalizeTx = psbt.extractTransaction()
102
- // const rawTx = finalizeTx.toBuffer()
103
- // const hex = rawTx.toString("hex")
104
- return finalizeTx.toHex()
102
+ return { hex: finalizeTx.toHex(), txId: finalizeTx.getId(), vBytes: finalizeTx.virtualSize() }
105
103
  }
106
104
  }
107
105