@teleportdao/bitcoin 1.4.3 → 1.4.4

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 (44) hide show
  1. package/dist/bitcoin-base.d.ts +17 -16
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +12 -7
  4. package/dist/bitcoin-base.js.map +1 -1
  5. package/dist/bitcoin-utils.d.ts +1 -1
  6. package/dist/bitcoin-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-utils.js +3 -0
  8. package/dist/bitcoin-utils.js.map +1 -1
  9. package/dist/bundle.js +4 -0
  10. package/dist/index.d.ts +5 -5
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +14 -12
  13. package/dist/index.js.map +1 -1
  14. package/dist/sign/sign-transaction.d.ts +9 -5
  15. package/dist/sign/sign-transaction.d.ts.map +1 -1
  16. package/dist/sign/sign-transaction.js +14 -11
  17. package/dist/sign/sign-transaction.js.map +1 -1
  18. package/dist/teleport-dao-payments.d.ts +17 -6
  19. package/dist/teleport-dao-payments.d.ts.map +1 -1
  20. package/dist/teleport-dao-payments.js +4 -3
  21. package/dist/teleport-dao-payments.js.map +1 -1
  22. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +26 -11
  23. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  24. package/dist/transaction-builder/bitcoin-transaction-builder.js +36 -8
  25. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  26. package/dist/transaction-builder/transaction-builder.d.ts +148 -9
  27. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  28. package/dist/transaction-builder/transaction-builder.js +230 -30
  29. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  30. package/package.json +7 -5
  31. package/src/bitcoin-base.js +220 -219
  32. package/src/bitcoin-interface.js +350 -350
  33. package/src/bitcoin-utils.js +487 -483
  34. package/src/helper/teleport-request-helper.js +179 -179
  35. package/src/index.ts +8 -0
  36. package/src/sign/sign-transaction.ts +96 -0
  37. package/src/teleport-dao-payments.js +280 -280
  38. package/src/transaction-builder/bitcoin-transaction-builder.ts +57 -0
  39. package/src/transaction-builder/transaction-builder.ts +490 -0
  40. package/src/index.js +0 -15
  41. package/src/sign/sign-transaction.js +0 -36
  42. package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
  43. package/src/transaction-builder/transaction-builder-common.js +0 -236
  44. package/src/transaction-builder/transaction-builder.js +0 -159
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@teleportdao/bitcoin",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "description": "teleswap bitcoin package",
5
5
  "main": "dist/index.js",
6
+ "browser": "dist/bundle.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "scripts": {
8
9
  "test": "echo \"Error: no test specified\" && exit 2",
9
- "build": "tsc",
10
- "build:watch": "tsc --watch"
10
+ "build:tsc": "tsc",
11
+ "build:watch": "tsc --watch",
12
+ "build": "npm run build:tsc && browserify dist/index.js --standalone tdo/bitcoin > dist/bundle.js"
11
13
  },
12
14
  "author": "",
13
15
  "license": "ISC",
14
16
  "dependencies": {
15
- "@teleportdao/configs": "^1.4.3",
17
+ "@teleportdao/configs": "^1.4.4",
16
18
  "@teleportdao/providers": "^1.2.4",
17
19
  "axios": "^0.27.2",
18
20
  "bignumber.js": "^9.1.1",
@@ -31,5 +33,5 @@
31
33
  "publishConfig": {
32
34
  "access": "public"
33
35
  },
34
- "gitHead": "500f1fc29c154585ab7729ba19c8616001678bb4"
36
+ "gitHead": "5ef6d5dcf2c5d5ea2c4d75309df730a52f722129"
35
37
  }
@@ -1,219 +1,220 @@
1
- const bip39 = require("bip39")
2
- const bip32 = require("bip32")
3
- const hdWalletNetworksPath = require("@teleportdao/configs").hdWalletPath
4
- const { getPubKeyFromPrivateKeyHex } = require("./bitcoin-utils")
5
- const networks = require("./utils/networks")
6
-
7
- const TransactionBuilder = require("./transaction-builder/bitcoin-transaction-builder")
8
- const BitcoinSign = require("./sign/sign-transaction")
9
-
10
- class BitcoinBase {
11
- constructor(
12
- networkName,
13
- connectionInfo = {
14
- api: {
15
- enabled: true,
16
- provider: "BlockStream",
17
- },
18
- },
19
- ) {
20
- this.network = networks[networkName]
21
- this.hdWalletPath = hdWalletNetworksPath[networkName.replace("_testnet", "")]
22
-
23
- this.transactionBuilder = new TransactionBuilder(connectionInfo, networkName, this.network)
24
- this.btcInterface = this.transactionBuilder.btcInterface
25
-
26
- this.signer = new BitcoinSign(this.network)
27
-
28
- this.currentAccount = null
29
- this.currentAccountType = null
30
-
31
- this.privateKey = null
32
- this.publicKey = null
33
- // todo multisig
34
- this.publicKeys = []
35
- }
36
-
37
- static createTransactionInputsAndOutputs({
38
- targets,
39
- extendedUtxo,
40
- changeAddress,
41
- feeRate,
42
- fullAmount = false,
43
- }) {
44
- return TransactionBuilder.helperHandleInputsAndOutputs({
45
- targets,
46
- extendedUtxo,
47
- changeAddress,
48
- feeRate,
49
- fullAmount,
50
- })
51
- }
52
-
53
- static checkBalanceIsSufficient({
54
- targets,
55
- extendedUtxo,
56
- changeAddress,
57
- feeRate,
58
- fullAmount = false,
59
- }) {
60
- try {
61
- TransactionBuilder.helperHandleInputsAndOutputs({
62
- targets,
63
- extendedUtxo,
64
- changeAddress,
65
- feeRate,
66
- fullAmount,
67
- })
68
- return true
69
- } catch (err) {
70
- return false
71
- }
72
- }
73
-
74
- setMultiSigAccount(accountType = "p2sh") {
75
- /* eslint-disable no-unreachable */
76
- // todo : not completed
77
- switch (accountType) {
78
- // case 'p2sh':
79
- // this.currentAccount = ''
80
- // break
81
- // case 'p2wsh':
82
- // this.currentAccount = ''
83
- // break
84
- // case 'p2sh-p2wsh':
85
- // this.currentAccount = ''
86
- // break
87
- default:
88
- throw new Error("accountType is incorrect")
89
- }
90
- this.currentAccountType = accountType
91
- }
92
-
93
- setAccountPrivateKey(privateKeyHex) {
94
- this.privateKey = Buffer.from(privateKeyHex, "hex")
95
- let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
96
- this.publicKey = publicKey
97
- }
98
-
99
- setAccountPublicKey(publicKeyHex) {
100
- this.publicKey = Buffer.from(publicKeyHex, "hex")
101
- }
102
-
103
- /**
104
- *
105
- * @param {*} input
106
- * @param {*} input.mnemonic
107
- * @param {*} input.mnemonicPassword
108
- * @param {*} input.index
109
- * @param {*} input.walletNumber
110
- * @param {*} input.addressType
111
- * @returns
112
- */
113
- setAccountPrivateKeyByMnemonic({
114
- mnemonic,
115
- mnemonicPassword = "",
116
- index = 0,
117
- walletNumber = 0,
118
- addressType = "p2sh-p2wpkh",
119
- }) {
120
- if (!bip39.validateMnemonic(mnemonic)) throw new Error("invalid mnemonic")
121
- const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword)
122
- const node = bip32.fromSeed(seed)
123
-
124
- if (!this.hdWalletPath[addressType]) throw new Error("incorrect path or addressType")
125
-
126
- const path = `${this.hdWalletPath[addressType]}/${walletNumber}`
127
- const account = node.derivePath(path)
128
- const userKeyPair = account.derive(index)
129
- this.setAccountPrivateKey(userKeyPair.privateKey.toString("hex"))
130
- return this.setAccount(addressType)
131
- }
132
-
133
- setAccount(accountType = "p2pkh") {
134
- let addressObj = this.transactionBuilder.createAddressObject({
135
- addressType: accountType,
136
- publicKey: this.publicKey,
137
- })
138
- this.currentAccount = addressObj.address
139
- this.currentAccountType = accountType
140
- this.addressObj = addressObj
141
- this.bitcoinAddress = addressObj.address
142
- return addressObj.address
143
- }
144
-
145
- /**
146
- *
147
- * @param {Object} signerInfo
148
- * @param {*} signerInfo.address
149
- * @param {*} signerInfo.addressType
150
- * @param {*} signerInfo.publicKey
151
- * @param {*} signerInfo.derivationPath
152
- * @param {*} signerInfo.masterFingerprint
153
- * @returns
154
- */
155
- async getExtendedUtxo({ address, addressType, publicKey, derivationPath, masterFingerprint }) {
156
- return this.transactionBuilder.getExtendedUtxo({
157
- address,
158
- addressType,
159
- publicKey,
160
- derivationPath,
161
- masterFingerprint,
162
- })
163
- }
164
-
165
- /**
166
- *
167
- * @param {*} input
168
- * @param {*} input.receiverAddress
169
- * @param {*} input.amount
170
- * @param {*} input.fullAmount
171
- * @param {*} input.speed
172
- * @returns
173
- */
174
- async send({ receiverAddress, amount, fullAmount = false, speed = "normal" }) {
175
- let extendedUtxo = await this.getExtendedUtxo({
176
- address: this.currentAccount,
177
- addressType: this.currentAccountType,
178
- publicKey: this.publicKey.toString("hex"),
179
- })
180
- if (amount - +amount.toFixed() !== 0)
181
- throw new Error("incorrect amount. amount should be in satoshi")
182
- let feeRate = await this.transactionBuilder._getFeeRate(speed)
183
- let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
184
- extendedUtxo,
185
- targets: [
186
- {
187
- address: receiverAddress,
188
- value: amount,
189
- },
190
- ],
191
- changeAddress: this.currentAccount,
192
- feeRate,
193
- fullAmount,
194
- })
195
- let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
196
- let signedTx = this.signer.finalizePsbts([signedPsbt])
197
- let txId = await this.transactionBuilder.sendTx(signedTx)
198
- return txId
199
- }
200
-
201
- async sendSignedPsbt(signedPsbt) {
202
- let signedTx = this.signer.finalizePsbts([signedPsbt])
203
- let txId = await this.transactionBuilder.sendTx(signedTx)
204
- return txId
205
- }
206
-
207
- async sendSignedTx(signedTx) {
208
- let txId = await this.transactionBuilder.sendTx(signedTx)
209
- return txId
210
- }
211
-
212
- async sendMultiSignedPsbt(signedPsbts = []) {
213
- let signedTx = this.signer.finalizePsbts(signedPsbts)
214
- let txId = await this.transactionBuilder.sendTx(signedTx)
215
- return txId
216
- }
217
- }
218
-
219
- module.exports = BitcoinBase
1
+ import TransactionBuilder from "./transaction-builder/bitcoin-transaction-builder"
2
+ import BitcoinSign from "./sign/sign-transaction"
3
+
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")
9
+
10
+ // eslint-disable-next-line import/prefer-default-export
11
+ export class BitcoinBase {
12
+ constructor(
13
+ networkName,
14
+ connectionInfo = {
15
+ api: {
16
+ enabled: true,
17
+ provider: "BlockStream",
18
+ },
19
+ },
20
+ ) {
21
+ this.network = networks[networkName]
22
+ this.hdWalletPath = hdWalletNetworksPath[networkName.replace("_testnet", "")]
23
+
24
+ this.transactionBuilder = new TransactionBuilder(connectionInfo, networkName, this.network)
25
+ this.btcInterface = this.transactionBuilder.btcInterface
26
+
27
+ this.signer = new BitcoinSign(this.network)
28
+
29
+ this.currentAccount = null
30
+ this.currentAccountType = null
31
+
32
+ this.privateKey = null
33
+ this.publicKey = null
34
+ // todo multisig
35
+ this.publicKeys = []
36
+ }
37
+
38
+ static createTransactionInputsAndOutputs({
39
+ targets,
40
+ extendedUtxo,
41
+ changeAddress,
42
+ feeRate,
43
+ fullAmount = false,
44
+ }) {
45
+ return TransactionBuilder.helperHandleInputsAndOutputs({
46
+ targets,
47
+ extendedUtxo,
48
+ changeAddress,
49
+ feeRate,
50
+ fullAmount,
51
+ })
52
+ }
53
+
54
+ static checkBalanceIsSufficient({
55
+ targets,
56
+ extendedUtxo,
57
+ changeAddress,
58
+ feeRate,
59
+ fullAmount = false,
60
+ }) {
61
+ try {
62
+ TransactionBuilder.helperHandleInputsAndOutputs({
63
+ targets,
64
+ extendedUtxo,
65
+ changeAddress,
66
+ feeRate,
67
+ fullAmount,
68
+ })
69
+ return true
70
+ } catch (err) {
71
+ return false
72
+ }
73
+ }
74
+
75
+ setMultiSigAccount(accountType = "p2sh") {
76
+ /* eslint-disable no-unreachable */
77
+ // todo : not completed
78
+ switch (accountType) {
79
+ // case 'p2sh':
80
+ // this.currentAccount = ''
81
+ // break
82
+ // case 'p2wsh':
83
+ // this.currentAccount = ''
84
+ // break
85
+ // case 'p2sh-p2wsh':
86
+ // this.currentAccount = ''
87
+ // break
88
+ default:
89
+ throw new Error("accountType is incorrect")
90
+ }
91
+ this.currentAccountType = accountType
92
+ }
93
+
94
+ setAccountPrivateKey(privateKeyHex) {
95
+ this.privateKey = Buffer.from(privateKeyHex, "hex")
96
+ let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
97
+ this.publicKey = publicKey
98
+ }
99
+
100
+ setAccountPublicKey(publicKeyHex) {
101
+ this.publicKey = Buffer.from(publicKeyHex, "hex")
102
+ }
103
+
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
+ setAccountPrivateKeyByMnemonic({
115
+ mnemonic,
116
+ mnemonicPassword = "",
117
+ index = 0,
118
+ walletNumber = 0,
119
+ addressType = "p2sh-p2wpkh",
120
+ }) {
121
+ if (!bip39.validateMnemonic(mnemonic)) throw new Error("invalid mnemonic")
122
+ const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword)
123
+ const node = bip32.fromSeed(seed)
124
+
125
+ if (!this.hdWalletPath[addressType]) throw new Error("incorrect path or addressType")
126
+
127
+ const path = `${this.hdWalletPath[addressType]}/${walletNumber}`
128
+ const account = node.derivePath(path)
129
+ const userKeyPair = account.derive(index)
130
+ this.setAccountPrivateKey(userKeyPair.privateKey.toString("hex"))
131
+ return this.setAccount(addressType)
132
+ }
133
+
134
+ setAccount(accountType = "p2pkh") {
135
+ let addressObj = this.transactionBuilder.createAddressObject({
136
+ addressType: accountType,
137
+ publicKey: this.publicKey,
138
+ })
139
+ this.currentAccount = addressObj.address
140
+ this.currentAccountType = accountType
141
+ this.addressObj = addressObj
142
+ this.bitcoinAddress = addressObj.address
143
+ return addressObj.address
144
+ }
145
+
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
+ })
164
+ }
165
+
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" }) {
176
+ let extendedUtxo = await this.getExtendedUtxo({
177
+ address: this.currentAccount,
178
+ addressType: this.currentAccountType,
179
+ publicKey: this.publicKey.toString("hex"),
180
+ })
181
+ if (amount - +amount.toFixed() !== 0)
182
+ throw new Error("incorrect amount. amount should be in satoshi")
183
+ let feeRate = await this.transactionBuilder._getFeeRate(speed)
184
+ let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
185
+ extendedUtxo,
186
+ targets: [
187
+ {
188
+ address: receiverAddress,
189
+ value: amount,
190
+ },
191
+ ],
192
+ changeAddress: this.currentAccount,
193
+ feeRate,
194
+ fullAmount,
195
+ })
196
+ let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
197
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
198
+ let txId = await this.transactionBuilder.sendTx(signedTx)
199
+ return txId
200
+ }
201
+
202
+ async sendSignedPsbt(signedPsbt) {
203
+ let signedTx = this.signer.finalizePsbts([signedPsbt])
204
+ let txId = await this.transactionBuilder.sendTx(signedTx)
205
+ return txId
206
+ }
207
+
208
+ async sendSignedTx(signedTx) {
209
+ let txId = await this.transactionBuilder.sendTx(signedTx)
210
+ return txId
211
+ }
212
+
213
+ async sendMultiSignedPsbt(signedPsbts = []) {
214
+ let signedTx = this.signer.finalizePsbts(signedPsbts)
215
+ let txId = await this.transactionBuilder.sendTx(signedTx)
216
+ return txId
217
+ }
218
+ }
219
+
220
+ // export default BitcoinBase