@teleportdao/bitcoin 2.0.4 → 2.0.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 (78) hide show
  1. package/dist/bitcoin-interface-ordinal.d.ts +108 -108
  2. package/dist/bitcoin-interface-ordinal.js +140 -140
  3. package/dist/bitcoin-interface-teleswap.d.ts +101 -101
  4. package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
  5. package/dist/bitcoin-interface-teleswap.js +176 -165
  6. package/dist/bitcoin-interface-teleswap.js.map +1 -1
  7. package/dist/bitcoin-interface-utils.d.ts +20 -20
  8. package/dist/bitcoin-interface-utils.js +45 -45
  9. package/dist/bitcoin-interface-wallet.d.ts +28 -28
  10. package/dist/bitcoin-interface-wallet.js +125 -125
  11. package/dist/bitcoin-interface.d.ts +66 -66
  12. package/dist/bitcoin-interface.js +119 -119
  13. package/dist/bitcoin-utils.d.ts +96 -96
  14. package/dist/bitcoin-utils.js +514 -514
  15. package/dist/bitcoin-wallet-base.d.ts +111 -111
  16. package/dist/bitcoin-wallet-base.js +258 -258
  17. package/dist/helper/brc20-helper.d.ts +42 -42
  18. package/dist/helper/brc20-helper.js +127 -127
  19. package/dist/helper/index.d.ts +3 -3
  20. package/dist/helper/index.js +29 -29
  21. package/dist/helper/ordinal-helper.d.ts +12 -12
  22. package/dist/helper/ordinal-helper.js +129 -129
  23. package/dist/helper/teleswap-helper.d.ts +95 -95
  24. package/dist/helper/teleswap-helper.js +186 -186
  25. package/dist/index.d.ts +12 -12
  26. package/dist/index.js +41 -41
  27. package/dist/ordinal-wallet.d.ts +495 -495
  28. package/dist/ordinal-wallet.js +386 -386
  29. package/dist/sign/index.d.ts +1 -1
  30. package/dist/sign/index.js +8 -8
  31. package/dist/sign/sign-transaction.d.ts +12 -12
  32. package/dist/sign/sign-transaction.js +82 -82
  33. package/dist/teleswap-wallet.d.ts +45 -45
  34. package/dist/teleswap-wallet.js +68 -68
  35. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +9 -9
  36. package/dist/transaction-builder/bitcoin-transaction-builder.js +54 -54
  37. package/dist/transaction-builder/index.d.ts +3 -3
  38. package/dist/transaction-builder/index.js +19 -19
  39. package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -63
  40. package/dist/transaction-builder/ordinal-transaction-builder.js +125 -125
  41. package/dist/transaction-builder/transaction-builder.d.ts +223 -223
  42. package/dist/transaction-builder/transaction-builder.js +447 -447
  43. package/dist/type.d.ts +61 -61
  44. package/dist/type.js +2 -2
  45. package/dist/utils/networks.d.ts +5 -5
  46. package/dist/utils/networks.js +53 -53
  47. package/dist/utils/tools.d.ts +18 -18
  48. package/dist/utils/tools.js +74 -74
  49. package/package.json +4 -4
  50. package/src/bitcoin-interface-ordinal.ts +185 -185
  51. package/src/bitcoin-interface-teleswap.ts +251 -237
  52. package/src/bitcoin-interface-utils.ts +60 -60
  53. package/src/bitcoin-interface-wallet.ts +114 -114
  54. package/src/bitcoin-interface.ts +156 -156
  55. package/src/bitcoin-utils.ts +591 -591
  56. package/src/bitcoin-wallet-base.ts +344 -344
  57. package/src/helper/brc20-helper.ts +179 -179
  58. package/src/helper/ordinal-helper.ts +118 -118
  59. package/src/index.ts +15 -15
  60. package/src/ordinal-wallet.ts +659 -659
  61. package/src/sign/index.ts +1 -1
  62. package/src/sign/sign-transaction.ts +108 -108
  63. package/src/teleswap-wallet.ts +133 -133
  64. package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -26
  65. package/src/transaction-builder/index.ts +3 -3
  66. package/src/transaction-builder/ordinal-transaction-builder.ts +139 -139
  67. package/src/transaction-builder/transaction-builder.ts +690 -690
  68. package/src/type.ts +74 -74
  69. package/src/utils/networks.ts +33 -33
  70. package/src/utils/tools.ts +92 -92
  71. package/tsconfig.json +9 -9
  72. package/webpack.config.js +16 -16
  73. package/.tmp/block-parser.ts +0 -58
  74. package/.tmp/check.ts +0 -101
  75. package/.tmp/ordinal-helper.ts +0 -133
  76. package/.tmp/ordinal.ts +0 -25
  77. package/.tmp/psbt/sign-transaction.ts +0 -121
  78. package/.tmp/rbf.ts +0 -45
@@ -1,237 +1,251 @@
1
- import { bitcoin } from "@teleportdao/providers"
2
- import { parseBlockHeader } from "./bitcoin-utils"
3
- import {
4
- checkAndParseWrapRequest,
5
- getBurnTransactionInfo,
6
- UnwrapInfo,
7
- WrapOpReturnWithType,
8
- } from "./helper/teleswap-helper"
9
- import BitcoinInterface from "./bitcoin-interface"
10
- import { BitcoinNodeConnectionInfo, Transaction } from "./type"
11
-
12
- export type ValidWrapRequest = {
13
- transaction: bitcoin.Types.ConfirmedTransaction & {
14
- address?: string
15
- addressScript?: string
16
- merkleProof?: Transaction["merkleProof"]
17
- }
18
- request: {
19
- status: boolean
20
- data: WrapOpReturnWithType
21
- value: number
22
- valueOutputIndex: number
23
- }
24
- lockerAddress: string
25
- lockerLockingScript: string
26
- }
27
-
28
- export type InValidWrapRequest = {
29
- transaction: bitcoin.Types.ConfirmedTransaction & {
30
- address?: string
31
- addressScript?: string
32
- merkleProof?: Transaction["merkleProof"]
33
- }
34
- request: {
35
- status: boolean
36
- message: string
37
- code: string
38
- }
39
- lockerAddress: string
40
- lockerLockingScript: string
41
- }
42
-
43
- export type UnwrapRequest = {
44
- burnInfo: UnwrapInfo | undefined
45
- lockerAddress: string
46
- lockerLockingScript: string
47
- }
48
-
49
- export class BitcoinInterfaceTeleswap extends BitcoinInterface {
50
- constructor(connectionInfo: BitcoinNodeConnectionInfo, networkName: string) {
51
- if (!connectionInfo.rpc) {
52
- throw new Error("rpc provider is required")
53
- }
54
- super(networkName, connectionInfo)
55
- }
56
- // relayer
57
- async getHexBlockHeaders(startBlockNumber: number, endBlockNumber: number) {
58
- const blockHeaders = []
59
- let difficulty = null
60
- let hexBlockHeaders = ""
61
-
62
- let fromBlockNumber = startBlockNumber
63
- for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
64
- let blockHeader = await this.getBlockHeaderHex(blockNumber)
65
- console.log("block", blockNumber)
66
- let parsedBlockHeader = parseBlockHeader(blockHeader)
67
- if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
68
- blockHeaders.push({
69
- hexBlockHeaders,
70
- fromBlockNumber,
71
- toBlockNumber: blockNumber - 1,
72
- difficulty,
73
- })
74
- hexBlockHeaders = blockHeader
75
- fromBlockNumber = blockNumber
76
- } else {
77
- hexBlockHeaders += blockHeader
78
- }
79
- difficulty = parsedBlockHeader.difficulty
80
- }
81
- if (hexBlockHeaders) {
82
- blockHeaders.push({
83
- hexBlockHeaders,
84
- fromBlockNumber,
85
- toBlockNumber: endBlockNumber,
86
- difficulty,
87
- })
88
- }
89
-
90
- return blockHeaders
91
- }
92
-
93
- async getWrapRequests(addresses: string[], startblockNumber: number, endBlockNumber: number) {
94
- // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
95
- let transactions = await this.getMultipleBlocksTransactions(
96
- addresses,
97
- startblockNumber,
98
- endBlockNumber,
99
- )
100
-
101
- let requests: ValidWrapRequest[] = []
102
- let invalidRequests: InValidWrapRequest[] = []
103
-
104
- for (let inputTx of transactions) {
105
- let { transaction, request, lockerAddress, lockerLockingScript } =
106
- await this.getWrapRequestByTx(inputTx, inputTx.address)
107
- if (request.status && "data" in request && request.data) {
108
- requests.push({
109
- transaction,
110
- request,
111
- lockerAddress,
112
- lockerLockingScript,
113
- })
114
- } else if (request.code !== "NO_OP_RETURN") {
115
- invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
116
- }
117
- }
118
- return { requests, invalidRequests }
119
- }
120
-
121
- async getWrapRequestByTx(
122
- inputTransaction:
123
- | { txId: string }
124
- | (bitcoin.Types.ConfirmedTransaction & {
125
- address?: string
126
- addressScript?: string
127
- merkleProof?: Transaction["merkleProof"]
128
- }),
129
- lockerAddress: string,
130
- ) {
131
- let transaction: bitcoin.Types.ConfirmedTransaction & {
132
- address?: string
133
- addressScript?: string
134
- merkleProof?: Transaction["merkleProof"]
135
- }
136
- if ("vout" in inputTransaction && "blockNumber" in inputTransaction) {
137
- transaction = inputTransaction
138
- } else {
139
- if (!inputTransaction.txId) throw new Error("txId not exist")
140
- transaction = await this.getTransaction(inputTransaction.txId)
141
- }
142
-
143
- let vout = transaction.vout
144
- let request = checkAndParseWrapRequest(vout!, lockerAddress)
145
- let lockerLockingScript: string =
146
- transaction.addressScript ||
147
- this.convertAddressToScript(lockerAddress).script!.toString("hex")
148
-
149
- return {
150
- transaction,
151
- request,
152
- lockerAddress,
153
- lockerLockingScript,
154
- }
155
- }
156
-
157
- async getLockersUnWrapTransactions(
158
- addresses: string[],
159
- startBlockNumber: number,
160
- endBlockNumber: number,
161
- ) {
162
- let transactions = await this.getMultipleBlocksTransactions(
163
- addresses,
164
- startBlockNumber,
165
- endBlockNumber,
166
- )
167
-
168
- let validTxs = []
169
- for (let transaction of transactions) {
170
- let address = transaction.address
171
- // check if its a transaction to spend btc
172
- let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
173
- if (txBurnInfo) {
174
- const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
175
- validTxs.push({
176
- transaction,
177
- burnInfo,
178
- lockerAddress,
179
- lockerLockingScript,
180
- })
181
- }
182
- }
183
- return validTxs
184
- }
185
-
186
- async getLockersUnwrapMEmpoolTransactions(
187
- addresses: string[],
188
- startBlockNumber: number,
189
- endBlockNumber: number,
190
- ) {
191
- let transactions = await this.getMultipleBlocksTransactions(
192
- addresses,
193
- startBlockNumber,
194
- endBlockNumber,
195
- )
196
-
197
- let validTxs = []
198
- for (let transaction of transactions) {
199
- let address = transaction.address
200
- // check if its a transaction to spend btc
201
- let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
202
- if (txBurnInfo) {
203
- const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
204
- validTxs.push({
205
- transaction,
206
- burnInfo,
207
- lockerAddress,
208
- lockerLockingScript,
209
- })
210
- }
211
- }
212
- return validTxs
213
- }
214
-
215
- async getTransactionUnwrapInfoByTx(
216
- transaction: {
217
- txId: any
218
- vout?: any
219
- vin?: any
220
- addressScript?: any
221
- },
222
- lockerAddress: string,
223
- ) {
224
- if (!transaction.txId) throw new Error("txId not exist")
225
- let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
226
- let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
227
- if (!burnInfo) return
228
- let lockerLockingScript: string =
229
- transaction.addressScript ||
230
- this.convertAddressToScript(lockerAddress).script!.toString("hex")
231
- return {
232
- burnInfo,
233
- lockerAddress,
234
- lockerLockingScript,
235
- } as UnwrapRequest
236
- }
237
- }
1
+ import { bitcoin } from "@teleportdao/providers"
2
+ import { teleswap } from "@teleportdao/configs"
3
+ import { parseBlockHeader } from "./bitcoin-utils"
4
+ import {
5
+ checkAndParseWrapRequest,
6
+ getBurnTransactionInfo,
7
+ UnwrapInfo,
8
+ WrapOpReturnWithType,
9
+ } from "./helper/teleswap-helper"
10
+ import BitcoinInterface from "./bitcoin-interface"
11
+ import { BitcoinNodeConnectionInfo, Transaction } from "./type"
12
+
13
+ export type ValidWrapRequest = {
14
+ transaction: bitcoin.Types.ConfirmedTransaction & {
15
+ address?: string
16
+ addressScript?: string
17
+ merkleProof?: Transaction["merkleProof"]
18
+ }
19
+ request: {
20
+ status: boolean
21
+ data: WrapOpReturnWithType
22
+ value: number
23
+ valueOutputIndex: number
24
+ }
25
+ lockerAddress: string
26
+ lockerLockingScript: string
27
+ }
28
+
29
+ export type InValidWrapRequest = {
30
+ transaction: bitcoin.Types.ConfirmedTransaction & {
31
+ address?: string
32
+ addressScript?: string
33
+ merkleProof?: Transaction["merkleProof"]
34
+ }
35
+ request: {
36
+ status: boolean
37
+ message: string
38
+ code: string
39
+ }
40
+ lockerAddress: string
41
+ lockerLockingScript: string
42
+ }
43
+
44
+ export type UnwrapRequest = {
45
+ burnInfo: UnwrapInfo | undefined
46
+ lockerAddress: string
47
+ lockerLockingScript: string
48
+ }
49
+
50
+ export class BitcoinInterfaceTeleswap extends BitcoinInterface {
51
+ constructor(connectionInfo: BitcoinNodeConnectionInfo, networkName: string) {
52
+ if (!connectionInfo.rpc) {
53
+ throw new Error("rpc provider is required")
54
+ }
55
+ super(networkName, connectionInfo)
56
+ }
57
+ // relayer
58
+ async getHexBlockHeaders(startBlockNumber: number, endBlockNumber: number) {
59
+ const blockHeaders = []
60
+ let difficulty = null
61
+ let hexBlockHeaders = ""
62
+
63
+ let fromBlockNumber = startBlockNumber
64
+ for (let blockNumber = startBlockNumber; blockNumber <= endBlockNumber; blockNumber += 1) {
65
+ let blockHeader = await this.getBlockHeaderHex(blockNumber)
66
+ console.log("block", blockNumber)
67
+ let parsedBlockHeader = parseBlockHeader(blockHeader)
68
+ if (difficulty && parsedBlockHeader.difficulty !== difficulty) {
69
+ blockHeaders.push({
70
+ hexBlockHeaders,
71
+ fromBlockNumber,
72
+ toBlockNumber: blockNumber - 1,
73
+ difficulty,
74
+ })
75
+ hexBlockHeaders = blockHeader
76
+ fromBlockNumber = blockNumber
77
+ } else {
78
+ hexBlockHeaders += blockHeader
79
+ }
80
+ difficulty = parsedBlockHeader.difficulty
81
+ }
82
+ if (hexBlockHeaders) {
83
+ blockHeaders.push({
84
+ hexBlockHeaders,
85
+ fromBlockNumber,
86
+ toBlockNumber: endBlockNumber,
87
+ difficulty,
88
+ })
89
+ }
90
+
91
+ return blockHeaders
92
+ }
93
+
94
+ async getWrapRequests(addresses: string[], startblockNumber: number, endBlockNumber: number) {
95
+ // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
96
+ let transactions = await this.getMultipleBlocksTransactions(
97
+ addresses,
98
+ startblockNumber,
99
+ endBlockNumber,
100
+ )
101
+
102
+ let requests: ValidWrapRequest[] = []
103
+ let invalidRequests: InValidWrapRequest[] = []
104
+
105
+ for (let inputTx of transactions) {
106
+ let { transaction, request, lockerAddress, lockerLockingScript } =
107
+ await this.getWrapRequestByTx(inputTx, inputTx.address)
108
+ if (request.status && "data" in request && request.data) {
109
+ if (!teleswap.requestAppId[request.data.requestType].appIds.includes(request.data.appId)) {
110
+ invalidRequests.push({
111
+ transaction,
112
+ request: {
113
+ ...request,
114
+ message: "invalid app id",
115
+ code: "INVALID_APP_ID",
116
+ },
117
+ lockerAddress,
118
+ lockerLockingScript,
119
+ })
120
+ } else {
121
+ requests.push({
122
+ transaction,
123
+ request,
124
+ lockerAddress,
125
+ lockerLockingScript,
126
+ })
127
+ }
128
+ } else if (request.code !== "NO_OP_RETURN") {
129
+ invalidRequests.push({ transaction, request, lockerAddress, lockerLockingScript })
130
+ }
131
+ }
132
+ return { requests, invalidRequests }
133
+ }
134
+
135
+ async getWrapRequestByTx(
136
+ inputTransaction:
137
+ | { txId: string }
138
+ | (bitcoin.Types.ConfirmedTransaction & {
139
+ address?: string
140
+ addressScript?: string
141
+ merkleProof?: Transaction["merkleProof"]
142
+ }),
143
+ lockerAddress: string,
144
+ ) {
145
+ let transaction: bitcoin.Types.ConfirmedTransaction & {
146
+ address?: string
147
+ addressScript?: string
148
+ merkleProof?: Transaction["merkleProof"]
149
+ }
150
+ if ("vout" in inputTransaction && "blockNumber" in inputTransaction) {
151
+ transaction = inputTransaction
152
+ } else {
153
+ if (!inputTransaction.txId) throw new Error("txId not exist")
154
+ transaction = await this.getTransaction(inputTransaction.txId)
155
+ }
156
+
157
+ let vout = transaction.vout
158
+ let request = checkAndParseWrapRequest(vout!, lockerAddress)
159
+ let lockerLockingScript: string =
160
+ transaction.addressScript ||
161
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
162
+
163
+ return {
164
+ transaction,
165
+ request,
166
+ lockerAddress,
167
+ lockerLockingScript,
168
+ }
169
+ }
170
+
171
+ async getLockersUnWrapTransactions(
172
+ addresses: string[],
173
+ startBlockNumber: number,
174
+ endBlockNumber: number,
175
+ ) {
176
+ let transactions = await this.getMultipleBlocksTransactions(
177
+ addresses,
178
+ startBlockNumber,
179
+ endBlockNumber,
180
+ )
181
+
182
+ let validTxs = []
183
+ for (let transaction of transactions) {
184
+ let address = transaction.address
185
+ // check if its a transaction to spend btc
186
+ let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
187
+ if (txBurnInfo) {
188
+ const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
189
+ validTxs.push({
190
+ transaction,
191
+ burnInfo,
192
+ lockerAddress,
193
+ lockerLockingScript,
194
+ })
195
+ }
196
+ }
197
+ return validTxs
198
+ }
199
+
200
+ async getLockersUnwrapMEmpoolTransactions(
201
+ addresses: string[],
202
+ startBlockNumber: number,
203
+ endBlockNumber: number,
204
+ ) {
205
+ let transactions = await this.getMultipleBlocksTransactions(
206
+ addresses,
207
+ startBlockNumber,
208
+ endBlockNumber,
209
+ )
210
+
211
+ let validTxs = []
212
+ for (let transaction of transactions) {
213
+ let address = transaction.address
214
+ // check if its a transaction to spend btc
215
+ let txBurnInfo = await this.getTransactionUnwrapInfoByTx(transaction, address)
216
+ if (txBurnInfo) {
217
+ const { burnInfo, lockerAddress, lockerLockingScript } = txBurnInfo
218
+ validTxs.push({
219
+ transaction,
220
+ burnInfo,
221
+ lockerAddress,
222
+ lockerLockingScript,
223
+ })
224
+ }
225
+ }
226
+ return validTxs
227
+ }
228
+
229
+ async getTransactionUnwrapInfoByTx(
230
+ transaction: {
231
+ txId: any
232
+ vout?: any
233
+ vin?: any
234
+ addressScript?: any
235
+ },
236
+ lockerAddress: string,
237
+ ) {
238
+ if (!transaction.txId) throw new Error("txId not exist")
239
+ let vin = transaction.vin || (await this.getTransaction(transaction.txId)).vin
240
+ let burnInfo = getBurnTransactionInfo(lockerAddress, vin, transaction.vout)
241
+ if (!burnInfo) return
242
+ let lockerLockingScript: string =
243
+ transaction.addressScript ||
244
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
245
+ return {
246
+ burnInfo,
247
+ lockerAddress,
248
+ lockerLockingScript,
249
+ } as UnwrapRequest
250
+ }
251
+ }
@@ -1,60 +1,60 @@
1
- import type { Network } from "bitcoinjs-lib"
2
- import networks from "./utils/networks"
3
- import {
4
- createAddressObjectByHash,
5
- createAddressObjectByAddress,
6
- createAddressObjectByPublicKey,
7
- createAddressObjectByScript,
8
- } from "./bitcoin-utils"
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
- const hash = addressType === "p2tr" ? addressObject.pubkey : addressObject.hash
39
- return {
40
- script: addressObject.output,
41
- hash,
42
- addressType,
43
- }
44
- }
45
-
46
- convertAddressToObject(address: string) {
47
- let addObj = createAddressObjectByAddress(address, this.network)
48
- return addObj
49
- }
50
-
51
- createAddressObjectByPublicKey(publicKey: string, addressType: string) {
52
- let addObj = createAddressObjectByPublicKey(
53
- { publicKey: Buffer.from(publicKey, "hex"), addressType },
54
- this.network,
55
- )
56
- return addObj
57
- }
58
- }
59
-
60
- export default BitcoinInterfaceUtils
1
+ import type { Network } from "bitcoinjs-lib"
2
+ import networks from "./utils/networks"
3
+ import {
4
+ createAddressObjectByHash,
5
+ createAddressObjectByAddress,
6
+ createAddressObjectByPublicKey,
7
+ createAddressObjectByScript,
8
+ } from "./bitcoin-utils"
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
+ const hash = addressType === "p2tr" ? addressObject.pubkey : addressObject.hash
39
+ return {
40
+ script: addressObject.output,
41
+ hash,
42
+ addressType,
43
+ }
44
+ }
45
+
46
+ convertAddressToObject(address: string) {
47
+ let addObj = createAddressObjectByAddress(address, this.network)
48
+ return addObj
49
+ }
50
+
51
+ createAddressObjectByPublicKey(publicKey: string, addressType: string) {
52
+ let addObj = createAddressObjectByPublicKey(
53
+ { publicKey: Buffer.from(publicKey, "hex"), addressType },
54
+ this.network,
55
+ )
56
+ return addObj
57
+ }
58
+ }
59
+
60
+ export default BitcoinInterfaceUtils