@teleportdao/bitcoin 1.7.2 → 1.7.3

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 (39) hide show
  1. package/dist/bitcoin-base.d.ts +93 -0
  2. package/dist/bitcoin-base.d.ts.map +1 -0
  3. package/dist/bitcoin-base.js +236 -0
  4. package/dist/bitcoin-base.js.map +1 -0
  5. package/dist/helper/burn-request-helper.d.ts +7 -0
  6. package/dist/helper/burn-request-helper.d.ts.map +1 -0
  7. package/dist/helper/burn-request-helper.js +26 -0
  8. package/dist/helper/burn-request-helper.js.map +1 -0
  9. package/dist/helper/teleport-request-helper.d.ts +47 -0
  10. package/dist/helper/teleport-request-helper.d.ts.map +1 -0
  11. package/dist/helper/teleport-request-helper.js +146 -0
  12. package/dist/helper/teleport-request-helper.js.map +1 -0
  13. package/dist/teleport-dao-payments.d.ts +76 -0
  14. package/dist/teleport-dao-payments.d.ts.map +1 -0
  15. package/dist/teleport-dao-payments.js +217 -0
  16. package/dist/teleport-dao-payments.js.map +1 -0
  17. package/package.json +3 -3
  18. package/src/bitcoin-interface-ordinal.ts +181 -181
  19. package/src/bitcoin-interface-teleswap.ts +255 -255
  20. package/src/bitcoin-interface-utils.ts +59 -59
  21. package/src/bitcoin-interface.ts +247 -247
  22. package/src/bitcoin-utils.ts +591 -591
  23. package/src/bitcoin-wallet-base.ts +314 -314
  24. package/src/helper/brc20-helper.ts +181 -181
  25. package/src/helper/ordinal-helper.ts +118 -118
  26. package/src/index.ts +15 -15
  27. package/src/ordinal-wallet.ts +738 -738
  28. package/src/sign/index.ts +1 -1
  29. package/src/sign/sign-transaction.ts +108 -108
  30. package/src/teleswap-wallet.ts +155 -155
  31. package/src/transaction-builder/bitcoin-transaction-builder.ts +44 -44
  32. package/src/transaction-builder/index.ts +3 -3
  33. package/src/transaction-builder/ordinal-transaction-builder.ts +147 -147
  34. package/src/transaction-builder/transaction-builder.ts +705 -705
  35. package/src/type.ts +43 -43
  36. package/src/utils/networks.ts +33 -33
  37. package/src/utils/tools.ts +89 -89
  38. package/tsconfig.json +9 -9
  39. package/webpack.config.js +16 -16
@@ -1,181 +1,181 @@
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, 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
+ }