@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.
- package/dist/bitcoin-interface-ordinal.d.ts +108 -108
- package/dist/bitcoin-interface-ordinal.js +140 -140
- package/dist/bitcoin-interface-teleswap.d.ts +101 -101
- package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
- package/dist/bitcoin-interface-teleswap.js +176 -165
- package/dist/bitcoin-interface-teleswap.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +20 -20
- package/dist/bitcoin-interface-utils.js +45 -45
- package/dist/bitcoin-interface-wallet.d.ts +28 -28
- package/dist/bitcoin-interface-wallet.js +125 -125
- package/dist/bitcoin-interface.d.ts +66 -66
- package/dist/bitcoin-interface.js +119 -119
- package/dist/bitcoin-utils.d.ts +96 -96
- package/dist/bitcoin-utils.js +514 -514
- package/dist/bitcoin-wallet-base.d.ts +111 -111
- package/dist/bitcoin-wallet-base.js +258 -258
- package/dist/helper/brc20-helper.d.ts +42 -42
- package/dist/helper/brc20-helper.js +127 -127
- package/dist/helper/index.d.ts +3 -3
- package/dist/helper/index.js +29 -29
- package/dist/helper/ordinal-helper.d.ts +12 -12
- package/dist/helper/ordinal-helper.js +129 -129
- package/dist/helper/teleswap-helper.d.ts +95 -95
- package/dist/helper/teleswap-helper.js +186 -186
- package/dist/index.d.ts +12 -12
- package/dist/index.js +41 -41
- package/dist/ordinal-wallet.d.ts +495 -495
- package/dist/ordinal-wallet.js +386 -386
- package/dist/sign/index.d.ts +1 -1
- package/dist/sign/index.js +8 -8
- package/dist/sign/sign-transaction.d.ts +12 -12
- package/dist/sign/sign-transaction.js +82 -82
- package/dist/teleswap-wallet.d.ts +45 -45
- package/dist/teleswap-wallet.js +68 -68
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +9 -9
- package/dist/transaction-builder/bitcoin-transaction-builder.js +54 -54
- package/dist/transaction-builder/index.d.ts +3 -3
- package/dist/transaction-builder/index.js +19 -19
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts +63 -63
- package/dist/transaction-builder/ordinal-transaction-builder.js +125 -125
- package/dist/transaction-builder/transaction-builder.d.ts +223 -223
- package/dist/transaction-builder/transaction-builder.js +447 -447
- package/dist/type.d.ts +61 -61
- package/dist/type.js +2 -2
- package/dist/utils/networks.d.ts +5 -5
- package/dist/utils/networks.js +53 -53
- package/dist/utils/tools.d.ts +18 -18
- package/dist/utils/tools.js +74 -74
- package/package.json +4 -4
- package/src/bitcoin-interface-ordinal.ts +185 -185
- package/src/bitcoin-interface-teleswap.ts +251 -237
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +114 -114
- package/src/bitcoin-interface.ts +156 -156
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +344 -344
- package/src/helper/brc20-helper.ts +179 -179
- package/src/helper/ordinal-helper.ts +118 -118
- package/src/index.ts +15 -15
- package/src/ordinal-wallet.ts +659 -659
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +133 -133
- package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -26
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +139 -139
- package/src/transaction-builder/transaction-builder.ts +690 -690
- package/src/type.ts +74 -74
- package/src/utils/networks.ts +33 -33
- package/src/utils/tools.ts +92 -92
- package/tsconfig.json +9 -9
- package/webpack.config.js +16 -16
- package/.tmp/block-parser.ts +0 -58
- package/.tmp/check.ts +0 -101
- package/.tmp/ordinal-helper.ts +0 -133
- package/.tmp/ordinal.ts +0 -25
- package/.tmp/psbt/sign-transaction.ts +0 -121
- package/.tmp/rbf.ts +0 -45
|
@@ -1,185 +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, 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
|
-
}
|
|
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
|
+
}
|