@teleportdao/bitcoin 1.4.0 → 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.
- package/dist/bitcoin-base.d.ts +17 -16
- package/dist/bitcoin-base.d.ts.map +1 -1
- package/dist/bitcoin-base.js +12 -7
- package/dist/bitcoin-base.js.map +1 -1
- package/dist/bitcoin-utils.d.ts +1 -1
- package/dist/bitcoin-utils.d.ts.map +1 -1
- package/dist/bitcoin-utils.js +3 -0
- package/dist/bitcoin-utils.js.map +1 -1
- package/dist/bundle.js +4 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -12
- package/dist/index.js.map +1 -1
- package/dist/sign/sign-transaction.d.ts +9 -5
- package/dist/sign/sign-transaction.d.ts.map +1 -1
- package/dist/sign/sign-transaction.js +14 -11
- package/dist/sign/sign-transaction.js.map +1 -1
- package/dist/teleport-dao-payments.d.ts +17 -6
- package/dist/teleport-dao-payments.d.ts.map +1 -1
- package/dist/teleport-dao-payments.js +4 -3
- package/dist/teleport-dao-payments.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +26 -11
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +36 -8
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts +148 -9
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +230 -30
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/package.json +7 -5
- package/src/bitcoin-base.js +220 -219
- package/src/bitcoin-utils.js +487 -483
- package/src/helper/teleport-request-helper.js +179 -179
- package/src/index.ts +8 -0
- package/src/sign/sign-transaction.ts +96 -0
- package/src/teleport-dao-payments.js +280 -280
- package/src/transaction-builder/bitcoin-transaction-builder.ts +57 -0
- package/src/transaction-builder/transaction-builder.ts +490 -0
- package/src/index.js +0 -15
- package/src/sign/sign-transaction.js +0 -36
- package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
- package/src/transaction-builder/transaction-builder-common.js +0 -236
- 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
|
+
"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.
|
|
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": "
|
|
36
|
+
"gitHead": "5ef6d5dcf2c5d5ea2c4d75309df730a52f722129"
|
|
35
37
|
}
|
package/src/bitcoin-base.js
CHANGED
|
@@ -1,219 +1,220 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
* @param {*} input
|
|
107
|
-
* @param {*} input.
|
|
108
|
-
* @param {*} input.
|
|
109
|
-
* @param {*} input.
|
|
110
|
-
* @param {*} input.
|
|
111
|
-
* @
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this.
|
|
140
|
-
this.
|
|
141
|
-
this.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
*
|
|
148
|
-
* @param {
|
|
149
|
-
* @param {*} signerInfo.
|
|
150
|
-
* @param {*} signerInfo.
|
|
151
|
-
* @param {*} signerInfo.
|
|
152
|
-
* @param {*} signerInfo.
|
|
153
|
-
* @
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*
|
|
168
|
-
* @param {*} input
|
|
169
|
-
* @param {*} input.
|
|
170
|
-
* @param {*} input.
|
|
171
|
-
* @param {*} input.
|
|
172
|
-
* @
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
let
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
let
|
|
197
|
-
let
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
let
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
let
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
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
|