@teleportdao/bitcoin 2.0.5 → 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 (76) 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.js +176 -176
  5. package/dist/bitcoin-interface-utils.d.ts +20 -20
  6. package/dist/bitcoin-interface-utils.js +45 -45
  7. package/dist/bitcoin-interface-wallet.d.ts +28 -28
  8. package/dist/bitcoin-interface-wallet.js +125 -125
  9. package/dist/bitcoin-interface.d.ts +66 -66
  10. package/dist/bitcoin-interface.js +119 -119
  11. package/dist/bitcoin-utils.d.ts +96 -96
  12. package/dist/bitcoin-utils.js +514 -514
  13. package/dist/bitcoin-wallet-base.d.ts +111 -111
  14. package/dist/bitcoin-wallet-base.js +258 -258
  15. package/dist/helper/brc20-helper.d.ts +42 -42
  16. package/dist/helper/brc20-helper.js +127 -127
  17. package/dist/helper/index.d.ts +3 -3
  18. package/dist/helper/index.js +29 -29
  19. package/dist/helper/ordinal-helper.d.ts +12 -12
  20. package/dist/helper/ordinal-helper.js +129 -129
  21. package/dist/helper/teleswap-helper.d.ts +95 -95
  22. package/dist/helper/teleswap-helper.js +186 -186
  23. package/dist/index.d.ts +12 -12
  24. package/dist/index.js +41 -41
  25. package/dist/ordinal-wallet.d.ts +495 -495
  26. package/dist/ordinal-wallet.js +386 -386
  27. package/dist/sign/index.d.ts +1 -1
  28. package/dist/sign/index.js +8 -8
  29. package/dist/sign/sign-transaction.d.ts +12 -12
  30. package/dist/sign/sign-transaction.js +82 -82
  31. package/dist/teleswap-wallet.d.ts +45 -45
  32. package/dist/teleswap-wallet.js +68 -68
  33. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +9 -9
  34. package/dist/transaction-builder/bitcoin-transaction-builder.js +54 -54
  35. package/dist/transaction-builder/index.d.ts +3 -3
  36. package/dist/transaction-builder/index.js +19 -19
  37. package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -63
  38. package/dist/transaction-builder/ordinal-transaction-builder.js +125 -125
  39. package/dist/transaction-builder/transaction-builder.d.ts +223 -223
  40. package/dist/transaction-builder/transaction-builder.js +447 -447
  41. package/dist/type.d.ts +61 -61
  42. package/dist/type.js +2 -2
  43. package/dist/utils/networks.d.ts +5 -5
  44. package/dist/utils/networks.js +53 -53
  45. package/dist/utils/tools.d.ts +18 -18
  46. package/dist/utils/tools.js +74 -74
  47. package/package.json +4 -4
  48. package/src/bitcoin-interface-ordinal.ts +185 -185
  49. package/src/bitcoin-interface-teleswap.ts +251 -251
  50. package/src/bitcoin-interface-utils.ts +60 -60
  51. package/src/bitcoin-interface-wallet.ts +114 -114
  52. package/src/bitcoin-interface.ts +156 -156
  53. package/src/bitcoin-utils.ts +591 -591
  54. package/src/bitcoin-wallet-base.ts +344 -344
  55. package/src/helper/brc20-helper.ts +179 -179
  56. package/src/helper/ordinal-helper.ts +118 -118
  57. package/src/index.ts +15 -15
  58. package/src/ordinal-wallet.ts +659 -659
  59. package/src/sign/index.ts +1 -1
  60. package/src/sign/sign-transaction.ts +108 -108
  61. package/src/teleswap-wallet.ts +133 -133
  62. package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -26
  63. package/src/transaction-builder/index.ts +3 -3
  64. package/src/transaction-builder/ordinal-transaction-builder.ts +139 -139
  65. package/src/transaction-builder/transaction-builder.ts +690 -690
  66. package/src/type.ts +74 -74
  67. package/src/utils/networks.ts +33 -33
  68. package/src/utils/tools.ts +92 -92
  69. package/tsconfig.json +9 -9
  70. package/webpack.config.js +16 -16
  71. package/.tmp/block-parser.ts +0 -58
  72. package/.tmp/check.ts +0 -101
  73. package/.tmp/ordinal-helper.ts +0 -133
  74. package/.tmp/ordinal.ts +0 -25
  75. package/.tmp/psbt/sign-transaction.ts +0 -121
  76. package/.tmp/rbf.ts +0 -45
@@ -1,251 +1,251 @@
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
+ 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