@teleportdao/bitcoin 1.4.4 → 1.4.7

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 (58) hide show
  1. package/dist/bitcoin-base.d.ts +62 -50
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +55 -28
  4. package/dist/bitcoin-base.js.map +1 -1
  5. package/dist/bitcoin-interface-utils.d.ts +12 -10
  6. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-utils.js +16 -10
  8. package/dist/bitcoin-interface-utils.js.map +1 -1
  9. package/dist/bitcoin-interface.d.ts +206 -50
  10. package/dist/bitcoin-interface.d.ts.map +1 -1
  11. package/dist/bitcoin-interface.js +42 -27
  12. package/dist/bitcoin-interface.js.map +1 -1
  13. package/dist/bitcoin-utils.d.ts +111 -41
  14. package/dist/bitcoin-utils.d.ts.map +1 -1
  15. package/dist/bitcoin-utils.js +215 -159
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -4
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +31 -13
  21. package/dist/index.js.map +1 -1
  22. package/dist/teleport-dao-payments.d.ts +60 -92
  23. package/dist/teleport-dao-payments.d.ts.map +1 -1
  24. package/dist/teleport-dao-payments.js +12 -1
  25. package/dist/teleport-dao-payments.js.map +1 -1
  26. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +8 -4
  27. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  28. package/dist/transaction-builder/bitcoin-transaction-builder.js +3 -3
  29. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  30. package/dist/transaction-builder/transaction-builder.d.ts +63 -13
  31. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/transaction-builder.js +72 -19
  33. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  34. package/dist/utils/networks.d.ts +5 -35
  35. package/dist/utils/networks.d.ts.map +1 -1
  36. package/dist/utils/networks.js +26 -2
  37. package/dist/utils/networks.js.map +1 -1
  38. package/dist/utils/tools.d.ts +15 -9
  39. package/dist/utils/tools.d.ts.map +1 -1
  40. package/dist/utils/tools.js +14 -11
  41. package/dist/utils/tools.js.map +1 -1
  42. package/package.json +6 -6
  43. package/src/{bitcoin-base.js → bitcoin-base.ts} +97 -69
  44. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  45. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +115 -45
  46. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +337 -216
  47. package/src/index.ts +5 -8
  48. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +67 -6
  49. package/src/transaction-builder/bitcoin-transaction-builder.ts +8 -4
  50. package/src/transaction-builder/transaction-builder.ts +102 -25
  51. package/src/utils/{networks.js → networks.ts} +33 -31
  52. package/src/utils/{tools.js → tools.ts} +80 -72
  53. package/tsconfig.json +10 -9
  54. package/webpack.config.js +16 -0
  55. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  56. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  57. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  58. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
@@ -1,17 +1,41 @@
1
- import TransactionBuilder from "./transaction-builder/bitcoin-transaction-builder"
2
- import BitcoinSign from "./sign/sign-transaction"
1
+ import TransactionBuilder, {
2
+ BitcoinConnectionInfo,
3
+ } from "./transaction-builder/bitcoin-transaction-builder"
3
4
 
4
- const bip39 = require("bip39")
5
- const bip32 = require("bip32")
6
- const hdWalletNetworksPath = require("@teleportdao/configs").hdWalletPath
7
- const { getPubKeyFromPrivateKeyHex } = require("./bitcoin-utils")
8
- const networks = require("./utils/networks")
5
+ import type { ExtendedUtxo, SignerInfo, Target } from "./transaction-builder/transaction-builder"
6
+ import BitcoinSign from "./sign/sign-transaction"
9
7
 
10
- // eslint-disable-next-line import/prefer-default-export
11
- export class BitcoinBase {
8
+ import * as bip39 from "bip39"
9
+ import * as bip32 from "bip32"
10
+ import { hdWalletPath } from "@teleportdao/configs"
11
+ import { getPubKeyFromPrivateKeyHex } from "./bitcoin-utils"
12
+ import networks from "./utils/networks"
13
+ import { Network, Payment } from "bitcoinjs-lib"
14
+ import { BitcoinInterface } from "./bitcoin-interface"
15
+
16
+ class BitcoinBase {
17
+ network: Network
18
+ hdWalletPath: {
19
+ p2pkh: string
20
+ p2wpkh: string
21
+ "p2sh-p2wpkh": string
22
+ p2sh: string
23
+ p2wsh: string
24
+ "p2sh-p2wsh": string
25
+ }
26
+ transactionBuilder: TransactionBuilder
27
+ btcInterface: BitcoinInterface
28
+ signer: BitcoinSign
29
+ currentAccount?: string
30
+ currentAccountType?: string
31
+ privateKey?: Buffer
32
+ publicKey?: Buffer
33
+ publicKeys?: Buffer[]
34
+ addressObj?: Payment
35
+ bitcoinAddress: string | undefined
12
36
  constructor(
13
- networkName,
14
- connectionInfo = {
37
+ networkName: string,
38
+ connectionInfo: BitcoinConnectionInfo = {
15
39
  api: {
16
40
  enabled: true,
17
41
  provider: "BlockStream",
@@ -19,18 +43,18 @@ export class BitcoinBase {
19
43
  },
20
44
  ) {
21
45
  this.network = networks[networkName]
22
- this.hdWalletPath = hdWalletNetworksPath[networkName.replace("_testnet", "")]
46
+ this.hdWalletPath = hdWalletPath.bitcoin
23
47
 
24
48
  this.transactionBuilder = new TransactionBuilder(connectionInfo, networkName, this.network)
25
49
  this.btcInterface = this.transactionBuilder.btcInterface
26
50
 
27
51
  this.signer = new BitcoinSign(this.network)
28
52
 
29
- this.currentAccount = null
30
- this.currentAccountType = null
53
+ this.currentAccount = undefined
54
+ this.currentAccountType = undefined
31
55
 
32
- this.privateKey = null
33
- this.publicKey = null
56
+ this.privateKey = undefined
57
+ this.publicKey = undefined
34
58
  // todo multisig
35
59
  this.publicKeys = []
36
60
  }
@@ -40,14 +64,20 @@ export class BitcoinBase {
40
64
  extendedUtxo,
41
65
  changeAddress,
42
66
  feeRate,
43
- fullAmount = false,
67
+ }: {
68
+ targets: Target[]
69
+ extendedUtxo: ExtendedUtxo[]
70
+ changeAddress: string
71
+ feeRate: number
72
+ fullAmount?: boolean
44
73
  }) {
45
74
  return TransactionBuilder.helperHandleInputsAndOutputs({
46
75
  targets,
47
76
  extendedUtxo,
48
- changeAddress,
77
+ changeObject: {
78
+ address: changeAddress,
79
+ },
49
80
  feeRate,
50
- fullAmount,
51
81
  })
52
82
  }
53
83
 
@@ -57,14 +87,21 @@ export class BitcoinBase {
57
87
  changeAddress,
58
88
  feeRate,
59
89
  fullAmount = false,
90
+ }: {
91
+ targets: Target[]
92
+ extendedUtxo: ExtendedUtxo[]
93
+ changeAddress: string
94
+ feeRate: number
95
+ fullAmount?: boolean
60
96
  }) {
61
97
  try {
62
98
  TransactionBuilder.helperHandleInputsAndOutputs({
63
99
  targets,
64
100
  extendedUtxo,
65
- changeAddress,
101
+ changeObject: {
102
+ address: changeAddress,
103
+ },
66
104
  feeRate,
67
- fullAmount,
68
105
  })
69
106
  return true
70
107
  } catch (err) {
@@ -72,6 +109,7 @@ export class BitcoinBase {
72
109
  }
73
110
  }
74
111
 
112
+ // todo : not completed
75
113
  setMultiSigAccount(accountType = "p2sh") {
76
114
  /* eslint-disable no-unreachable */
77
115
  // todo : not completed
@@ -91,47 +129,48 @@ export class BitcoinBase {
91
129
  this.currentAccountType = accountType
92
130
  }
93
131
 
94
- setAccountPrivateKey(privateKeyHex) {
132
+ setAccountPrivateKey(privateKeyHex: string) {
95
133
  this.privateKey = Buffer.from(privateKeyHex, "hex")
96
134
  let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
97
135
  this.publicKey = publicKey
98
136
  }
99
137
 
100
- setAccountPublicKey(publicKeyHex) {
138
+ setAccountPublicKey(publicKeyHex: string) {
101
139
  this.publicKey = Buffer.from(publicKeyHex, "hex")
102
140
  }
103
141
 
104
- /**
105
- *
106
- * @param {*} input
107
- * @param {*} input.mnemonic
108
- * @param {*} input.mnemonicPassword
109
- * @param {*} input.index
110
- * @param {*} input.walletNumber
111
- * @param {*} input.addressType
112
- * @returns
113
- */
114
142
  setAccountPrivateKeyByMnemonic({
115
143
  mnemonic,
116
144
  mnemonicPassword = "",
117
145
  index = 0,
118
146
  walletNumber = 0,
119
147
  addressType = "p2sh-p2wpkh",
148
+ }: {
149
+ mnemonic: string
150
+ mnemonicPassword?: string
151
+ index?: number
152
+ walletNumber?: number
153
+ addressType?: string
120
154
  }) {
121
155
  if (!bip39.validateMnemonic(mnemonic)) throw new Error("invalid mnemonic")
122
156
  const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword)
123
157
  const node = bip32.fromSeed(seed)
124
158
 
125
- if (!this.hdWalletPath[addressType]) throw new Error("incorrect path or addressType")
126
-
127
- const path = `${this.hdWalletPath[addressType]}/${walletNumber}`
159
+ let basePath = this.hdWalletPath[addressType as keyof typeof this.hdWalletPath]
160
+ if (!basePath) {
161
+ throw new Error("incorrect path or addressType")
162
+ }
163
+ const path = `${basePath}/${walletNumber}`
128
164
  const account = node.derivePath(path)
129
165
  const userKeyPair = account.derive(index)
130
- this.setAccountPrivateKey(userKeyPair.privateKey.toString("hex"))
166
+ this.setAccountPrivateKey(userKeyPair.privateKey!.toString("hex"))
131
167
  return this.setAccount(addressType)
132
168
  }
133
169
 
134
170
  setAccount(accountType = "p2pkh") {
171
+ if (!this.publicKey) {
172
+ throw new Error("account not initialized")
173
+ }
135
174
  let addressObj = this.transactionBuilder.createAddressObject({
136
175
  addressType: accountType,
137
176
  publicKey: this.publicKey,
@@ -143,36 +182,25 @@ export class BitcoinBase {
143
182
  return addressObj.address
144
183
  }
145
184
 
146
- /**
147
- *
148
- * @param {Object} signerInfo
149
- * @param {*} signerInfo.address
150
- * @param {*} signerInfo.addressType
151
- * @param {*} signerInfo.publicKey
152
- * @param {*} signerInfo.derivationPath
153
- * @param {*} signerInfo.masterFingerprint
154
- * @returns
155
- */
156
- async getExtendedUtxo({ address, addressType, publicKey, derivationPath, masterFingerprint }) {
157
- return this.transactionBuilder.getExtendedUtxo({
158
- address,
159
- addressType,
160
- publicKey,
161
- derivationPath,
162
- masterFingerprint,
163
- })
185
+ async getExtendedUtxo(input: SignerInfo) {
186
+ return this.transactionBuilder.getExtendedUtxo(input)
164
187
  }
165
188
 
166
- /**
167
- *
168
- * @param {*} input
169
- * @param {*} input.receiverAddress
170
- * @param {*} input.amount
171
- * @param {*} input.fullAmount
172
- * @param {*} input.speed
173
- * @returns
174
- */
175
- async send({ receiverAddress, amount, fullAmount = false, speed = "normal" }) {
189
+ async send({
190
+ receiverAddress,
191
+ amount,
192
+ fullAmount = false,
193
+ speed = "normal",
194
+ }: {
195
+ receiverAddress: string
196
+ amount: number
197
+ fullAmount?: boolean
198
+ speed?: "normal" | "fast" | "slow"
199
+ }) {
200
+ if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
201
+ throw new Error("account not initialized")
202
+ }
203
+
176
204
  let extendedUtxo = await this.getExtendedUtxo({
177
205
  address: this.currentAccount,
178
206
  addressType: this.currentAccountType,
@@ -199,22 +227,22 @@ export class BitcoinBase {
199
227
  return txId
200
228
  }
201
229
 
202
- async sendSignedPsbt(signedPsbt) {
230
+ async sendSignedPsbt(signedPsbt: string) {
203
231
  let signedTx = this.signer.finalizePsbts([signedPsbt])
204
232
  let txId = await this.transactionBuilder.sendTx(signedTx)
205
233
  return txId
206
234
  }
207
235
 
208
- async sendSignedTx(signedTx) {
236
+ async sendSignedTx(signedTx: string) {
209
237
  let txId = await this.transactionBuilder.sendTx(signedTx)
210
238
  return txId
211
239
  }
212
240
 
213
- async sendMultiSignedPsbt(signedPsbts = []) {
241
+ async sendMultiSignedPsbt(signedPsbts: string[] = []) {
214
242
  let signedTx = this.signer.finalizePsbts(signedPsbts)
215
243
  let txId = await this.transactionBuilder.sendTx(signedTx)
216
244
  return txId
217
245
  }
218
246
  }
219
247
 
220
- // export default BitcoinBase
248
+ export { BitcoinBase }
@@ -1,53 +1,59 @@
1
- const networks = require("./utils/networks")
2
- const {
3
- createAddressObjectByHash,
4
- createAddressObjectByAddress,
5
- createAddressObjectByPublicKey,
6
- createAddressObjectByScript,
7
- } = require("./bitcoin-utils")
8
-
9
- class BitcoinInterfaceUtils {
10
- constructor(networkName) {
11
- this.testnet = networkName.includes("_testnet")
12
- this.network = networks[networkName]
13
- }
14
-
15
- convertHashToAddress(hashHex, addressType) {
16
- let addressObj = createAddressObjectByHash(
17
- { addressType, hash: Buffer.from(hashHex, "hex") },
18
- this.network,
19
- )
20
- if (!addressObj.address) throw new Error("incorrect input")
21
- return addressObj.address
22
- }
23
-
24
- convertScriptToAddress(scriptHex, addressType) {
25
- let addressObj = createAddressObjectByScript(
26
- { addressType, script: Buffer.from(scriptHex, "hex") },
27
- this.network,
28
- )
29
- if (!addressObj.address) throw new Error("incorrect input")
30
- return addressObj.address
31
- }
32
-
33
- convertAddressToScript(address) {
34
- let { addressObject, addressType } = createAddressObjectByAddress(address, this.network)
35
- return {
36
- script: addressObject.output,
37
- hash: addressObject.hash,
38
- addressType,
39
- }
40
- }
41
-
42
- convertAddressToObject(address) {
43
- let addObj = createAddressObjectByAddress(address, this.network)
44
- return addObj
45
- }
46
-
47
- createAddressObjectByPublicKey(address, addressType) {
48
- let addObj = createAddressObjectByPublicKey({ address, addressType }, this.network)
49
- return addObj
50
- }
51
- }
52
-
53
- module.exports = BitcoinInterfaceUtils
1
+ import networks from "./utils/networks"
2
+ import {
3
+ createAddressObjectByHash,
4
+ createAddressObjectByAddress,
5
+ createAddressObjectByPublicKey,
6
+ createAddressObjectByScript,
7
+ } from "./bitcoin-utils"
8
+ import type { Network } from "bitcoinjs-lib"
9
+
10
+ export class BitcoinInterfaceUtils {
11
+ testnet: boolean
12
+ network: Network
13
+ constructor(networkName: string) {
14
+ this.testnet = networkName.includes("_testnet")
15
+ this.network = networks[networkName]
16
+ }
17
+
18
+ convertHashToAddress(hashHex: string, addressType: string) {
19
+ let addressObj = createAddressObjectByHash(
20
+ { addressType, hash: Buffer.from(hashHex, "hex") },
21
+ this.network,
22
+ )
23
+ if (!addressObj.address) throw new Error("incorrect input")
24
+ return addressObj.address
25
+ }
26
+
27
+ convertScriptToAddress(scriptHex: string, addressType: string) {
28
+ let addressObj = createAddressObjectByScript(
29
+ { addressType, script: Buffer.from(scriptHex, "hex") },
30
+ this.network,
31
+ )
32
+ if (!addressObj.address) throw new Error("incorrect input")
33
+ return addressObj.address
34
+ }
35
+
36
+ convertAddressToScript(address: string) {
37
+ let { addressObject, addressType } = createAddressObjectByAddress(address, this.network)
38
+ return {
39
+ script: addressObject.output,
40
+ hash: addressObject.hash,
41
+ addressType,
42
+ }
43
+ }
44
+
45
+ convertAddressToObject(address: string) {
46
+ let addObj = createAddressObjectByAddress(address, this.network)
47
+ return addObj
48
+ }
49
+
50
+ createAddressObjectByPublicKey(publicKey: string, addressType: string) {
51
+ let addObj = createAddressObjectByPublicKey(
52
+ { publicKey: Buffer.from(publicKey, "hex"), addressType },
53
+ this.network,
54
+ )
55
+ return addObj
56
+ }
57
+ }
58
+
59
+ export default BitcoinInterfaceUtils