@teleportdao/bitcoin 1.9.0 → 2.0.2

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 (70) hide show
  1. package/.tmp/block-parser.ts +58 -0
  2. package/.tmp/ordinal-helper.ts +133 -0
  3. package/.tmp/ordinal.ts +25 -0
  4. package/.tmp/rbf.ts +27 -24
  5. package/dist/bitcoin-interface-ordinal.d.ts +2 -2
  6. package/dist/bitcoin-interface-ordinal.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-ordinal.js +1 -1
  8. package/dist/bitcoin-interface-ordinal.js.map +1 -1
  9. package/dist/bitcoin-interface-teleswap.d.ts +2 -53
  10. package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
  11. package/dist/bitcoin-interface-teleswap.js +6 -17
  12. package/dist/bitcoin-interface-teleswap.js.map +1 -1
  13. package/dist/bitcoin-interface-wallet.d.ts +29 -0
  14. package/dist/bitcoin-interface-wallet.d.ts.map +1 -0
  15. package/dist/bitcoin-interface-wallet.js +126 -0
  16. package/dist/bitcoin-interface-wallet.js.map +1 -0
  17. package/dist/bitcoin-interface.d.ts +5 -23
  18. package/dist/bitcoin-interface.d.ts.map +1 -1
  19. package/dist/bitcoin-interface.js +13 -92
  20. package/dist/bitcoin-interface.js.map +1 -1
  21. package/dist/bitcoin-wallet-base.d.ts +55 -31
  22. package/dist/bitcoin-wallet-base.d.ts.map +1 -1
  23. package/dist/bitcoin-wallet-base.js +105 -84
  24. package/dist/bitcoin-wallet-base.js.map +1 -1
  25. package/dist/ordinal-wallet.d.ts +29 -71
  26. package/dist/ordinal-wallet.d.ts.map +1 -1
  27. package/dist/ordinal-wallet.js +48 -108
  28. package/dist/ordinal-wallet.js.map +1 -1
  29. package/dist/teleswap-wallet.d.ts +10 -16
  30. package/dist/teleswap-wallet.d.ts.map +1 -1
  31. package/dist/teleswap-wallet.js +10 -30
  32. package/dist/teleswap-wallet.js.map +1 -1
  33. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +4 -11
  34. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  35. package/dist/transaction-builder/bitcoin-transaction-builder.js +2 -23
  36. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  37. package/dist/transaction-builder/ordinal-transaction-builder.d.ts +3 -2
  38. package/dist/transaction-builder/ordinal-transaction-builder.d.ts.map +1 -1
  39. package/dist/transaction-builder/ordinal-transaction-builder.js +1 -6
  40. package/dist/transaction-builder/ordinal-transaction-builder.js.map +1 -1
  41. package/dist/transaction-builder/transaction-builder.d.ts +3 -7
  42. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  43. package/dist/transaction-builder/transaction-builder.js +22 -49
  44. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  45. package/dist/type.d.ts +33 -18
  46. package/dist/type.d.ts.map +1 -1
  47. package/package.json +4 -4
  48. package/src/bitcoin-interface-ordinal.ts +185 -181
  49. package/src/bitcoin-interface-teleswap.ts +237 -252
  50. package/src/bitcoin-interface-utils.ts +60 -60
  51. package/src/bitcoin-interface-wallet.ts +114 -0
  52. package/src/bitcoin-interface.ts +156 -239
  53. package/src/bitcoin-utils.ts +591 -591
  54. package/src/bitcoin-wallet-base.ts +344 -310
  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 -748
  59. package/src/sign/index.ts +1 -1
  60. package/src/sign/sign-transaction.ts +108 -108
  61. package/src/teleswap-wallet.ts +133 -155
  62. package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -44
  63. package/src/transaction-builder/index.ts +3 -3
  64. package/src/transaction-builder/ordinal-transaction-builder.ts +139 -147
  65. package/src/transaction-builder/transaction-builder.ts +690 -706
  66. package/src/type.ts +74 -48
  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
@@ -1,181 +1,185 @@
1
- import BigNumber from "bignumber.js"
2
- import { bitcoin as bitcoinProviders } from "@teleportdao/providers"
3
- import { BitcoinInterface } from "./bitcoin-interface"
4
- import { Transaction, BitcoinConnectionInfo } from "./type"
5
- import type { SignerInfo } from "./transaction-builder"
6
-
7
- import { sleep } from "./utils/tools"
8
- import { checkAndParseBrc20Request } from "./helper/brc20-helper"
9
-
10
- export class BitcoinInterfaceOrdinal extends BitcoinInterface {
11
- unisat: bitcoinProviders.UniSat
12
- constructor(connectionInfo: BitcoinConnectionInfo, networkName: string, uniSatToken: string) {
13
- super(connectionInfo, networkName)
14
- this.unisat = new bitcoinProviders.UniSat(
15
- {
16
- token: uniSatToken,
17
- },
18
- networkName.includes("testnet"),
19
- )
20
- }
21
- async getInscriptionWithRetry(
22
- inscriptionId: string,
23
- { numberOfRetry = 5, sleepTime = 20000 } = {},
24
- ) {
25
- let count = 0
26
- while (count <= numberOfRetry) {
27
- try {
28
- let inscription = await this.unisat.getInscriptionInfo(inscriptionId)
29
- if (inscription?.brc20) {
30
- return inscription
31
- }
32
- } catch (e: any) {
33
- console.log(e.message)
34
- }
35
- await sleep(sleepTime + count * 5000)
36
- count += 1
37
- }
38
- return null
39
- }
40
-
41
- async getTransactionBrc20TransferInfo(transaction: {
42
- txId: string
43
- vout: {
44
- address: string | null
45
- script: string
46
- value: number
47
- }[]
48
- vin: {
49
- txId: string
50
- index: number
51
- address?: string | null
52
- script?: string | null
53
- value?: number | null
54
- }[]
55
- }) {
56
- let txBrc20s: {
57
- txId: string
58
- receiver: string
59
- index: number
60
- brc20: {
61
- op: string
62
- tick: string
63
- lim: string
64
- amt: string
65
- decimal: string
66
- }
67
- inscription: bitcoinProviders.UnisatInscription
68
- }[] = []
69
- for (let x = 0; x < transaction.vout.length - 1; x += 1) {
70
- if (!transaction.vout[x].address) {
71
- return []
72
- }
73
- let inscription = await this.unisat.getInscriptionInfo(
74
- `${transaction.vin[x].txId}i${transaction.vin[x].index}`,
75
- )
76
-
77
- const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
78
- if (brc20 && inscription) {
79
- txBrc20s.push({
80
- txId: transaction.txId,
81
- receiver: transaction.vout[x].address!,
82
- index: x,
83
- inscription,
84
- brc20,
85
- })
86
- } else {
87
- return []
88
- }
89
- }
90
-
91
- return txBrc20s
92
- }
93
-
94
- async convertToBrc20WrapRequest(transaction: Transaction, lockerAddress: string) {
95
- let { vout } = transaction
96
- let request = checkAndParseBrc20Request(vout, lockerAddress)
97
- let lockerLockingScript: string =
98
- transaction.addressScript ||
99
- this.convertAddressToScript(lockerAddress).script!.toString("hex")
100
-
101
- return {
102
- transaction,
103
- request,
104
- lockerAddress,
105
- lockerLockingScript,
106
- }
107
- }
108
-
109
- async getBrc20WrapRequests(
110
- addresses: string[],
111
- startblockNumber: number,
112
- endBlockNumber: number,
113
- ) {
114
- // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
115
- let transactions: Transaction[] = await this.getMultipleBlocksTransactions(
116
- addresses,
117
- startblockNumber,
118
- endBlockNumber || (await this.getLatestBlockNumber()),
119
- )
120
-
121
- let requests = []
122
- let invalidRequests = []
123
-
124
- // eslint-disable-next-line no-restricted-syntax
125
- for (let inputTx of transactions) {
126
- let data = await this.convertToBrc20WrapRequest(inputTx, inputTx.address)
127
- if (data.request.status) {
128
- let inscription = await this.getInscriptionWithRetry(
129
- `${inputTx.vin[0].txId}i${inputTx.vin[0].index}`,
130
- )
131
- const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
132
-
133
- if (!brc20) {
134
- invalidRequests.push({
135
- ...data,
136
- message: `invalid brc20 -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}`,
137
- })
138
- } else if (
139
- !BigNumber(brc20.amt)
140
- .multipliedBy(1e18)
141
- .isEqualTo(data.request.data?.inputAmount || 0)
142
- ) {
143
- invalidRequests.push({
144
- ...data,
145
- message: `invalid brc20 amount -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}:${data.request.data?.inputAmount}`,
146
- })
147
- } else {
148
- requests.push({ ...data, brc20 })
149
- }
150
- // check brc20 info
151
- // this.unisat.getBrc20TickerTxHistory(data.request.data!.brc20!.ticker, data.transaction.txId)
152
- // ignore normal transaction
153
- } else if (data.request.code !== "NO_OP_RETURN") {
154
- invalidRequests.push(data)
155
- }
156
- }
157
- return { requests, invalidRequests }
158
- }
159
-
160
- async getBTCUtxo(address: string, signerInfo: SignerInfo) {
161
- return bitcoinProviders.UniSat.convertToNormalUtxo(
162
- (await this.unisat.getBTCUtxo(address))?.utxo || [],
163
- ).map(({ txId, index, value }) => ({
164
- hash: txId,
165
- index,
166
- value,
167
- signerInfo,
168
- }))
169
- }
170
-
171
- async getInscriptionUtxo(address: string, signerInfo: SignerInfo) {
172
- return bitcoinProviders.UniSat.convertToNormalUtxo(
173
- (await this.unisat.getInscriptionUtxo(address))?.utxo || [],
174
- ).map(({ txId, index, value }) => ({
175
- hash: txId,
176
- index,
177
- value,
178
- signerInfo,
179
- }))
180
- }
181
- }
1
+ import BigNumber from "bignumber.js"
2
+ import { bitcoin as bitcoinProviders } from "@teleportdao/providers"
3
+ import { BitcoinInterface } from "./bitcoin-interface"
4
+ import { Transaction, BitcoinTokenNodeConnectionInfo } from "./type"
5
+ import type { SignerInfo } from "./transaction-builder"
6
+
7
+ import { sleep } from "./utils/tools"
8
+ import { checkAndParseBrc20Request } from "./helper/brc20-helper"
9
+
10
+ export class BitcoinInterfaceOrdinal extends BitcoinInterface {
11
+ unisat: bitcoinProviders.UniSat
12
+ constructor(
13
+ connectionInfo: BitcoinTokenNodeConnectionInfo,
14
+ networkName: string,
15
+ uniSatToken: string,
16
+ ) {
17
+ super(networkName, connectionInfo)
18
+ this.unisat = new bitcoinProviders.UniSat(
19
+ {
20
+ token: uniSatToken,
21
+ },
22
+ networkName.includes("testnet"),
23
+ )
24
+ }
25
+ async getInscriptionWithRetry(
26
+ inscriptionId: string,
27
+ { numberOfRetry = 5, sleepTime = 20000 } = {},
28
+ ) {
29
+ let count = 0
30
+ while (count <= numberOfRetry) {
31
+ try {
32
+ let inscription = await this.unisat.getInscriptionInfo(inscriptionId)
33
+ if (inscription?.brc20) {
34
+ return inscription
35
+ }
36
+ } catch (e: any) {
37
+ console.log(e.message)
38
+ }
39
+ await sleep(sleepTime + count * 5000)
40
+ count += 1
41
+ }
42
+ return null
43
+ }
44
+
45
+ async getTransactionBrc20TransferInfo(transaction: {
46
+ txId: string
47
+ vout: {
48
+ address: string | null
49
+ script: string
50
+ value: number
51
+ }[]
52
+ vin: {
53
+ txId: string
54
+ index: number
55
+ address?: string | null
56
+ script?: string | null
57
+ value?: number | null
58
+ }[]
59
+ }) {
60
+ let txBrc20s: {
61
+ txId: string
62
+ receiver: string
63
+ index: number
64
+ brc20: {
65
+ op: string
66
+ tick: string
67
+ lim: string
68
+ amt: string
69
+ decimal: string
70
+ }
71
+ inscription: bitcoinProviders.UnisatInscription
72
+ }[] = []
73
+ for (let x = 0; x < transaction.vout.length - 1; x += 1) {
74
+ if (!transaction.vout[x].address) {
75
+ return []
76
+ }
77
+ let inscription = await this.unisat.getInscriptionInfo(
78
+ `${transaction.vin[x].txId}i${transaction.vin[x].index}`,
79
+ )
80
+
81
+ const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
82
+ if (brc20 && inscription) {
83
+ txBrc20s.push({
84
+ txId: transaction.txId,
85
+ receiver: transaction.vout[x].address!,
86
+ index: x,
87
+ inscription,
88
+ brc20,
89
+ })
90
+ } else {
91
+ return []
92
+ }
93
+ }
94
+
95
+ return txBrc20s
96
+ }
97
+
98
+ async convertToBrc20WrapRequest(transaction: Transaction, lockerAddress: string) {
99
+ let { vout } = transaction
100
+ let request = checkAndParseBrc20Request(vout, lockerAddress)
101
+ let lockerLockingScript: string =
102
+ transaction.addressScript ||
103
+ this.convertAddressToScript(lockerAddress).script!.toString("hex")
104
+
105
+ return {
106
+ transaction,
107
+ request,
108
+ lockerAddress,
109
+ lockerLockingScript,
110
+ }
111
+ }
112
+
113
+ async getBrc20WrapRequests(
114
+ addresses: string[],
115
+ startblockNumber: number,
116
+ endBlockNumber: number,
117
+ ) {
118
+ // transaction in StartBlock is not returned --> (startblockNumber,endBlockNumber]
119
+ let transactions: Transaction[] = await this.getMultipleBlocksTransactions(
120
+ addresses,
121
+ startblockNumber,
122
+ endBlockNumber || (await this.getLatestBlockNumber()),
123
+ )
124
+
125
+ let requests = []
126
+ let invalidRequests = []
127
+
128
+ // eslint-disable-next-line no-restricted-syntax
129
+ for (let inputTx of transactions) {
130
+ let data = await this.convertToBrc20WrapRequest(inputTx, inputTx.address)
131
+ if (data.request.status) {
132
+ let inscription = await this.getInscriptionWithRetry(
133
+ `${inputTx.vin[0].txId}i${inputTx.vin[0].index}`,
134
+ )
135
+ const brc20 = inscription?.brc20?.op === "transfer" ? inscription.brc20 : undefined
136
+
137
+ if (!brc20) {
138
+ invalidRequests.push({
139
+ ...data,
140
+ message: `invalid brc20 -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}`,
141
+ })
142
+ } else if (
143
+ !BigNumber(brc20.amt)
144
+ .multipliedBy(1e18)
145
+ .isEqualTo(data.request.data?.inputAmount || 0)
146
+ ) {
147
+ invalidRequests.push({
148
+ ...data,
149
+ message: `invalid brc20 amount -> ${inscription?.brc20?.op}:${inscription?.brc20?.amt}:${data.request.data?.inputAmount}`,
150
+ })
151
+ } else {
152
+ requests.push({ ...data, brc20 })
153
+ }
154
+ // check brc20 info
155
+ // this.unisat.getBrc20TickerTxHistory(data.request.data!.brc20!.ticker, data.transaction.txId)
156
+ // ignore normal transaction
157
+ } else if (data.request.code !== "NO_OP_RETURN") {
158
+ invalidRequests.push(data)
159
+ }
160
+ }
161
+ return { requests, invalidRequests }
162
+ }
163
+
164
+ async getBTCUtxo(address: string, signerInfo: SignerInfo) {
165
+ return bitcoinProviders.UniSat.convertToNormalUtxo(
166
+ (await this.unisat.getBTCUtxo(address))?.utxo || [],
167
+ ).map(({ txId, index, value }) => ({
168
+ hash: txId,
169
+ index,
170
+ value,
171
+ signerInfo,
172
+ }))
173
+ }
174
+
175
+ async getInscriptionUtxo(address: string, signerInfo: SignerInfo) {
176
+ return bitcoinProviders.UniSat.convertToNormalUtxo(
177
+ (await this.unisat.getInscriptionUtxo(address))?.utxo || [],
178
+ ).map(({ txId, index, value }) => ({
179
+ hash: txId,
180
+ index,
181
+ value,
182
+ signerInfo,
183
+ }))
184
+ }
185
+ }