@teleportdao/bitcoin 1.4.3 → 1.4.6
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 +66 -53
- package/dist/bitcoin-base.d.ts.map +1 -1
- package/dist/bitcoin-base.js +47 -38
- 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 -156
- package/dist/bitcoin-utils.js.map +1 -1
- package/dist/bundle.js +13 -0
- package/dist/index.d.ts +5 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -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 +68 -89
- package/dist/teleport-dao-payments.d.ts.map +1 -1
- package/dist/teleport-dao-payments.js +16 -4
- package/dist/teleport-dao-payments.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +30 -11
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +37 -9
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts +198 -9
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +291 -38
- 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 +8 -6
- package/src/{bitcoin-base.js → bitcoin-base.ts} +248 -219
- package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
- package/src/{bitcoin-interface.js → bitcoin-interface.ts} +420 -350
- package/src/{bitcoin-utils.js → bitcoin-utils.ts} +608 -483
- package/src/helper/teleport-request-helper.js +179 -179
- package/src/index.ts +5 -0
- package/src/sign/sign-transaction.ts +96 -0
- package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +341 -280
- package/src/transaction-builder/bitcoin-transaction-builder.ts +61 -0
- package/src/transaction-builder/transaction-builder.ts +567 -0
- 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
- 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
|
@@ -1,219 +1,248 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
let
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
1
|
+
import TransactionBuilder, {
|
|
2
|
+
BitcoinConnectionInfo,
|
|
3
|
+
} from "./transaction-builder/bitcoin-transaction-builder"
|
|
4
|
+
|
|
5
|
+
import type { ExtendedUtxo, SignerInfo, Target } from "./transaction-builder/transaction-builder"
|
|
6
|
+
import BitcoinSign from "./sign/sign-transaction"
|
|
7
|
+
|
|
8
|
+
import bip39 from "bip39"
|
|
9
|
+
import 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
|
|
36
|
+
constructor(
|
|
37
|
+
networkName: string,
|
|
38
|
+
connectionInfo: BitcoinConnectionInfo = {
|
|
39
|
+
api: {
|
|
40
|
+
enabled: true,
|
|
41
|
+
provider: "BlockStream",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
) {
|
|
45
|
+
this.network = networks[networkName]
|
|
46
|
+
this.hdWalletPath = hdWalletPath.bitcoin
|
|
47
|
+
|
|
48
|
+
this.transactionBuilder = new TransactionBuilder(connectionInfo, networkName, this.network)
|
|
49
|
+
this.btcInterface = this.transactionBuilder.btcInterface
|
|
50
|
+
|
|
51
|
+
this.signer = new BitcoinSign(this.network)
|
|
52
|
+
|
|
53
|
+
this.currentAccount = undefined
|
|
54
|
+
this.currentAccountType = undefined
|
|
55
|
+
|
|
56
|
+
this.privateKey = undefined
|
|
57
|
+
this.publicKey = undefined
|
|
58
|
+
// todo multisig
|
|
59
|
+
this.publicKeys = []
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static createTransactionInputsAndOutputs({
|
|
63
|
+
targets,
|
|
64
|
+
extendedUtxo,
|
|
65
|
+
changeAddress,
|
|
66
|
+
feeRate,
|
|
67
|
+
}: {
|
|
68
|
+
targets: Target[]
|
|
69
|
+
extendedUtxo: ExtendedUtxo[]
|
|
70
|
+
changeAddress: string
|
|
71
|
+
feeRate: number
|
|
72
|
+
fullAmount?: boolean
|
|
73
|
+
}) {
|
|
74
|
+
return TransactionBuilder.helperHandleInputsAndOutputs({
|
|
75
|
+
targets,
|
|
76
|
+
extendedUtxo,
|
|
77
|
+
changeObject: {
|
|
78
|
+
address: changeAddress,
|
|
79
|
+
},
|
|
80
|
+
feeRate,
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static checkBalanceIsSufficient({
|
|
85
|
+
targets,
|
|
86
|
+
extendedUtxo,
|
|
87
|
+
changeAddress,
|
|
88
|
+
feeRate,
|
|
89
|
+
fullAmount = false,
|
|
90
|
+
}: {
|
|
91
|
+
targets: Target[]
|
|
92
|
+
extendedUtxo: ExtendedUtxo[]
|
|
93
|
+
changeAddress: string
|
|
94
|
+
feeRate: number
|
|
95
|
+
fullAmount?: boolean
|
|
96
|
+
}) {
|
|
97
|
+
try {
|
|
98
|
+
TransactionBuilder.helperHandleInputsAndOutputs({
|
|
99
|
+
targets,
|
|
100
|
+
extendedUtxo,
|
|
101
|
+
changeObject: {
|
|
102
|
+
address: changeAddress,
|
|
103
|
+
},
|
|
104
|
+
feeRate,
|
|
105
|
+
})
|
|
106
|
+
return true
|
|
107
|
+
} catch (err) {
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// todo : not completed
|
|
113
|
+
setMultiSigAccount(accountType = "p2sh") {
|
|
114
|
+
/* eslint-disable no-unreachable */
|
|
115
|
+
// todo : not completed
|
|
116
|
+
switch (accountType) {
|
|
117
|
+
// case 'p2sh':
|
|
118
|
+
// this.currentAccount = ''
|
|
119
|
+
// break
|
|
120
|
+
// case 'p2wsh':
|
|
121
|
+
// this.currentAccount = ''
|
|
122
|
+
// break
|
|
123
|
+
// case 'p2sh-p2wsh':
|
|
124
|
+
// this.currentAccount = ''
|
|
125
|
+
// break
|
|
126
|
+
default:
|
|
127
|
+
throw new Error("accountType is incorrect")
|
|
128
|
+
}
|
|
129
|
+
this.currentAccountType = accountType
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
setAccountPrivateKey(privateKeyHex: string) {
|
|
133
|
+
this.privateKey = Buffer.from(privateKeyHex, "hex")
|
|
134
|
+
let publicKey = getPubKeyFromPrivateKeyHex(privateKeyHex, this.network)
|
|
135
|
+
this.publicKey = publicKey
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
setAccountPublicKey(publicKeyHex: string) {
|
|
139
|
+
this.publicKey = Buffer.from(publicKeyHex, "hex")
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
setAccountPrivateKeyByMnemonic({
|
|
143
|
+
mnemonic,
|
|
144
|
+
mnemonicPassword = "",
|
|
145
|
+
index = 0,
|
|
146
|
+
walletNumber = 0,
|
|
147
|
+
addressType = "p2sh-p2wpkh",
|
|
148
|
+
}: {
|
|
149
|
+
mnemonic: string
|
|
150
|
+
mnemonicPassword?: string
|
|
151
|
+
index?: number
|
|
152
|
+
walletNumber?: number
|
|
153
|
+
addressType?: string
|
|
154
|
+
}) {
|
|
155
|
+
if (!bip39.validateMnemonic(mnemonic)) throw new Error("invalid mnemonic")
|
|
156
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic, mnemonicPassword)
|
|
157
|
+
const node = bip32.fromSeed(seed)
|
|
158
|
+
|
|
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}`
|
|
164
|
+
const account = node.derivePath(path)
|
|
165
|
+
const userKeyPair = account.derive(index)
|
|
166
|
+
this.setAccountPrivateKey(userKeyPair.privateKey!.toString("hex"))
|
|
167
|
+
return this.setAccount(addressType)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
setAccount(accountType = "p2pkh") {
|
|
171
|
+
if (!this.publicKey) {
|
|
172
|
+
throw new Error("account not initialized")
|
|
173
|
+
}
|
|
174
|
+
let addressObj = this.transactionBuilder.createAddressObject({
|
|
175
|
+
addressType: accountType,
|
|
176
|
+
publicKey: this.publicKey,
|
|
177
|
+
})
|
|
178
|
+
this.currentAccount = addressObj.address
|
|
179
|
+
this.currentAccountType = accountType
|
|
180
|
+
this.addressObj = addressObj
|
|
181
|
+
this.bitcoinAddress = addressObj.address
|
|
182
|
+
return addressObj.address
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async getExtendedUtxo(input: SignerInfo) {
|
|
186
|
+
return this.transactionBuilder.getExtendedUtxo(input)
|
|
187
|
+
}
|
|
188
|
+
|
|
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
|
+
|
|
204
|
+
let extendedUtxo = await this.getExtendedUtxo({
|
|
205
|
+
address: this.currentAccount,
|
|
206
|
+
addressType: this.currentAccountType,
|
|
207
|
+
publicKey: this.publicKey.toString("hex"),
|
|
208
|
+
})
|
|
209
|
+
if (amount - +amount.toFixed() !== 0)
|
|
210
|
+
throw new Error("incorrect amount. amount should be in satoshi")
|
|
211
|
+
let feeRate = await this.transactionBuilder._getFeeRate(speed)
|
|
212
|
+
let unsignedTx = await this.transactionBuilder.processUnsignedTransaction({
|
|
213
|
+
extendedUtxo,
|
|
214
|
+
targets: [
|
|
215
|
+
{
|
|
216
|
+
address: receiverAddress,
|
|
217
|
+
value: amount,
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
changeAddress: this.currentAccount,
|
|
221
|
+
feeRate,
|
|
222
|
+
fullAmount,
|
|
223
|
+
})
|
|
224
|
+
let signedPsbt = await this.signer.signPsbt(unsignedTx, this.privateKey)
|
|
225
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
226
|
+
let txId = await this.transactionBuilder.sendTx(signedTx)
|
|
227
|
+
return txId
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async sendSignedPsbt(signedPsbt: string) {
|
|
231
|
+
let signedTx = this.signer.finalizePsbts([signedPsbt])
|
|
232
|
+
let txId = await this.transactionBuilder.sendTx(signedTx)
|
|
233
|
+
return txId
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async sendSignedTx(signedTx: string) {
|
|
237
|
+
let txId = await this.transactionBuilder.sendTx(signedTx)
|
|
238
|
+
return txId
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async sendMultiSignedPsbt(signedPsbts: string[] = []) {
|
|
242
|
+
let signedTx = this.signer.finalizePsbts(signedPsbts)
|
|
243
|
+
let txId = await this.transactionBuilder.sendTx(signedTx)
|
|
244
|
+
return txId
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
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
|