@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.
- package/dist/bitcoin-base.d.ts +62 -50
- package/dist/bitcoin-base.d.ts.map +1 -1
- package/dist/bitcoin-base.js +55 -28
- package/dist/bitcoin-base.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +12 -10
- package/dist/bitcoin-interface-utils.d.ts.map +1 -1
- package/dist/bitcoin-interface-utils.js +16 -10
- package/dist/bitcoin-interface-utils.js.map +1 -1
- package/dist/bitcoin-interface.d.ts +206 -50
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +42 -27
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-utils.d.ts +111 -41
- package/dist/bitcoin-utils.d.ts.map +1 -1
- package/dist/bitcoin-utils.js +215 -159
- package/dist/bitcoin-utils.js.map +1 -1
- package/dist/bundle.js +13 -4
- package/dist/index.d.ts +5 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -13
- package/dist/index.js.map +1 -1
- package/dist/teleport-dao-payments.d.ts +60 -92
- package/dist/teleport-dao-payments.d.ts.map +1 -1
- package/dist/teleport-dao-payments.js +12 -1
- package/dist/teleport-dao-payments.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +8 -4
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +3 -3
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts +63 -13
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +72 -19
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/utils/networks.d.ts +5 -35
- package/dist/utils/networks.d.ts.map +1 -1
- package/dist/utils/networks.js +26 -2
- package/dist/utils/networks.js.map +1 -1
- package/dist/utils/tools.d.ts +15 -9
- package/dist/utils/tools.d.ts.map +1 -1
- package/dist/utils/tools.js +14 -11
- package/dist/utils/tools.js.map +1 -1
- package/package.json +6 -6
- package/src/{bitcoin-base.js → bitcoin-base.ts} +97 -69
- package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
- package/src/{bitcoin-interface.js → bitcoin-interface.ts} +115 -45
- package/src/{bitcoin-utils.js → bitcoin-utils.ts} +337 -216
- package/src/index.ts +5 -8
- package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +67 -6
- package/src/transaction-builder/bitcoin-transaction-builder.ts +8 -4
- package/src/transaction-builder/transaction-builder.ts +102 -25
- package/src/utils/{networks.js → networks.ts} +33 -31
- package/src/utils/{tools.js → tools.ts} +80 -72
- package/tsconfig.json +10 -9
- package/webpack.config.js +16 -0
- package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
- package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
- package/dist/transaction-builder/transaction-builder-common.js +0 -183
- package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
|
@@ -1,17 +1,41 @@
|
|
|
1
|
-
import TransactionBuilder
|
|
2
|
-
|
|
1
|
+
import TransactionBuilder, {
|
|
2
|
+
BitcoinConnectionInfo,
|
|
3
|
+
} from "./transaction-builder/bitcoin-transaction-builder"
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
11
|
-
|
|
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 =
|
|
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 =
|
|
30
|
-
this.currentAccountType =
|
|
53
|
+
this.currentAccount = undefined
|
|
54
|
+
this.currentAccountType = undefined
|
|
31
55
|
|
|
32
|
-
this.privateKey =
|
|
33
|
-
this.publicKey =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
248
|
+
export { BitcoinBase }
|
|
@@ -1,53 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
createAddressObjectByHash,
|
|
4
|
-
createAddressObjectByAddress,
|
|
5
|
-
createAddressObjectByPublicKey,
|
|
6
|
-
createAddressObjectByScript,
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|