@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,659 +1,659 @@
1
- import BigNumber from "bignumber.js"
2
- import { bitcoin as bitcoinProviders } from "@teleportdao/providers"
3
- import { OrdinalTransactionBuilder } from "./transaction-builder/ordinal-transaction-builder"
4
- import BitcoinSign from "./sign/sign-transaction"
5
- //
6
- import { BitcoinInterfaceOrdinal } from "./bitcoin-interface-ordinal"
7
- import { generateBrc2OpReturn } from "./helper/brc20-helper"
8
- import { sleep } from "./utils/tools"
9
- import { ChangeTarget, ExtendedUtxo, SignerInfo, Target } from "./transaction-builder"
10
- import { BitcoinBaseWallet, FeeRateType } from "./bitcoin-wallet-base"
11
- import { BTCTokenConnectionInfo, RPCConnectionInfo, UtxoConnectionInfo } from "./type"
12
-
13
- class OrdinalWallet extends BitcoinBaseWallet {
14
- unisat: bitcoinProviders.UniSat
15
- transactionBuilder: OrdinalTransactionBuilder
16
- btcInterface: BitcoinInterfaceOrdinal
17
- signer: BitcoinSign
18
- constructor(
19
- networkName: string,
20
- uniSatToken: string,
21
- connectionInfo: {
22
- // btcToken: BTCTokenConnectionInfo
23
- rpc: RPCConnectionInfo
24
- utxo?: UtxoConnectionInfo
25
- // rpc used for getRawTransaction in transaction builder if set (optional)
26
- },
27
- ) {
28
- if (!connectionInfo.rpc) {
29
- // because api providers usually don't support sending custom psbt include p2tr address so we need rpc provider
30
- throw new Error("rpc is required")
31
- }
32
- super(networkName, connectionInfo)
33
-
34
- this.transactionBuilder = new OrdinalTransactionBuilder(
35
- networkName,
36
- this.network,
37
- connectionInfo,
38
- )
39
- this.signer = new BitcoinSign(this.network)
40
- this.btcInterface = new BitcoinInterfaceOrdinal(connectionInfo, networkName, uniSatToken)
41
- this.unisat = this.btcInterface.unisat
42
- }
43
-
44
- static deployBRC20Data(tickName: string, max: number | string, limit: number | string) {
45
- let data = {
46
- p: "brc-20",
47
- op: "deploy",
48
- tick: tickName,
49
- max: `${max}`,
50
- lim: `${limit}`,
51
- }
52
- return {
53
- buffer: Buffer.from(JSON.stringify(data), "utf8"),
54
- type: "text/plain",
55
- // type: "application/json",
56
- }
57
- }
58
-
59
- static mintBRC20Data(tickName: string, amount: string | number) {
60
- if (BigNumber(amount).isLessThanOrEqualTo(0)) throw new Error("amount should be greater than 0")
61
- let data = {
62
- p: "brc-20",
63
- op: "mint",
64
- tick: tickName,
65
- amt: `${amount}`,
66
- }
67
- return {
68
- buffer: Buffer.from(JSON.stringify(data), "utf8"),
69
- type: "text/plain",
70
- // type: "application/json",
71
- }
72
- }
73
-
74
- static transferBRC20Data(tickName: string, amount: string | number) {
75
- if (BigNumber(amount).isLessThanOrEqualTo(0)) throw new Error("amount should be greater than 0")
76
- let data = {
77
- p: "brc-20",
78
- op: "transfer",
79
- tick: tickName,
80
- amt: `${amount}`,
81
- }
82
- return {
83
- buffer: Buffer.from(JSON.stringify(data), "utf8"),
84
- type: "text/plain",
85
- // type: "application/json",
86
- }
87
- }
88
-
89
- async inscribeOrdinalDepositUnsigned(
90
- file: {
91
- buffer: Buffer
92
- type: string
93
- },
94
- signer: SignerInfo,
95
- ordinalSigner?: SignerInfo,
96
- fee: FeeRateType = "normal",
97
- extendedUtxo?: ExtendedUtxo[],
98
- ) {
99
- const ordinalSignerPublicKey = ordinalSigner?.publicKey || signer.publicKey
100
- const publicKey = Buffer.from(ordinalSignerPublicKey, "hex")
101
- let transferOrdinal = this.transactionBuilder.createOrdinalAddress(file, publicKey)
102
- const leafScript = transferOrdinal.redeem.output
103
- const { ordinalAddress } = transferOrdinal
104
-
105
- let feeRate = await this.getFeeRate(fee)
106
- let extraFee = +(((400 + leafScript.length) / 4) * feeRate * 1.5).toFixed(0)
107
- let ordinalAmount = 600
108
-
109
- let utxo1: ExtendedUtxo[] =
110
- extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
111
-
112
- let inscribeDepositUnsignedInfo = await this.sendBTCUnsignedTx(
113
- ordinalAddress,
114
- ordinalAmount + extraFee,
115
- signer,
116
- feeRate,
117
- utxo1,
118
- )
119
-
120
- return {
121
- inscribeDepositUnsignedInfo,
122
- transferOrdinal,
123
- }
124
- }
125
-
126
- async inscribeOrdinalUnsigned(
127
- file: {
128
- buffer: Buffer
129
- type: string
130
- },
131
- signer: SignerInfo,
132
- ordinalSigner?: SignerInfo,
133
- fee: FeeRateType = "normal",
134
- extendedUtxo?: ExtendedUtxo[],
135
- ) {
136
- const receiverAddress = ordinalSigner?.address || signer.address
137
- const { inscribeDepositUnsignedInfo, transferOrdinal } =
138
- await this.inscribeOrdinalDepositUnsigned(file, signer, ordinalSigner, fee, extendedUtxo)
139
- const { ordinalAddress } = transferOrdinal
140
- let ordinalAmount = 600
141
-
142
- if (!inscribeDepositUnsignedInfo.possibleTxId)
143
- throw new Error(
144
- "inscribeDepositUnsignedInfo.possibleTxId is required. inscriber address type is not p2tr",
145
- )
146
-
147
- let inscribeDeposit: {
148
- hash: string
149
- value: number
150
- index: number
151
- } = {
152
- hash: inscribeDepositUnsignedInfo.possibleTxId,
153
- value: inscribeDepositUnsignedInfo.outputs[0].value,
154
- index: 0,
155
- }
156
- let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(
157
- transferOrdinal,
158
- inscribeDeposit,
159
- receiverAddress,
160
- ordinalAmount,
161
- )
162
- return {
163
- inscribeDepositUnsignedInfo,
164
- inscribeUnsignedInfo,
165
- inscribeAddress: ordinalAddress,
166
- receiverAddress,
167
- }
168
- }
169
-
170
- async inscribeOrdinal(
171
- file: {
172
- buffer: Buffer
173
- type: string
174
- },
175
- fee: FeeRateType = "normal",
176
- extendedUtxo?: ExtendedUtxo[],
177
- ordinalReceiverAddress?: string,
178
- ) {
179
- if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
180
- throw new Error("account not initialized")
181
- }
182
- const receiverAddress = ordinalReceiverAddress || this.bitcoinAddress!
183
-
184
- const { inscribeDepositUnsignedInfo, transferOrdinal } =
185
- await this.inscribeOrdinalDepositUnsigned(
186
- file,
187
- this.signerInfo!,
188
- undefined,
189
- fee,
190
- extendedUtxo,
191
- )
192
- let ordinalUtxo = await this.btcInterface.getBTCUtxo(
193
- transferOrdinal.ordinalAddress,
194
- this.signerInfo!,
195
- )
196
-
197
- let inscribeDeposit: {
198
- inputs: {
199
- hash: string
200
- value: number
201
- index: number
202
- signerInfo: SignerInfo
203
- }[]
204
- hash: string
205
- value: number
206
- index: number
207
- change?: ChangeTarget
208
- changeIndex?: number
209
- }
210
-
211
- if (ordinalUtxo.length > 1) {
212
- throw new Error("multiple deposit found for this ordinal address")
213
- }
214
-
215
- if (
216
- ordinalUtxo.length === 0 ||
217
- ordinalUtxo[0].value <
218
- inscribeDepositUnsignedInfo.outputs[0].value -
219
- inscribeDepositUnsignedInfo.outputs[0].value * 0.15
220
- ) {
221
- const signedPsbt1 = await this.signer.signPsbt(inscribeDepositUnsignedInfo, this.privateKey!)
222
- console.log("inscribe deposit tx ...")
223
- const inscribeDepositTxId = await this.sendSignedPsbt(signedPsbt1)
224
- console.log(`inscribe deposit txId : ${inscribeDepositTxId}`)
225
-
226
- inscribeDeposit = {
227
- hash: inscribeDepositTxId,
228
- value: inscribeDepositUnsignedInfo.outputs[0].value,
229
- index: 0,
230
- inputs: inscribeDepositUnsignedInfo.inputs,
231
- change: inscribeDepositUnsignedInfo.change,
232
- changeIndex: inscribeDepositUnsignedInfo.change
233
- ? inscribeDepositUnsignedInfo.outputs.length
234
- : undefined,
235
- }
236
- } else {
237
- inscribeDeposit = {
238
- hash: ordinalUtxo[0].hash,
239
- value: ordinalUtxo[0].value,
240
- index: ordinalUtxo[0].index,
241
- inputs: [],
242
- }
243
- console.log("no need to deposit", inscribeDeposit)
244
- }
245
-
246
- const ordinalAmount = 600
247
- let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(
248
- transferOrdinal,
249
- inscribeDeposit,
250
- receiverAddress,
251
- ordinalAmount,
252
- )
253
-
254
- const signedPsbt2 = await this.signer.signPsbt(
255
- inscribeUnsignedInfo,
256
- this.privateKey!,
257
- undefined,
258
- false,
259
- )
260
-
261
- console.log("inscribeTxId ...")
262
- await sleep(10 * 1000)
263
- let inscribeTxId = await this.sendSignedPsbtWithRetry(signedPsbt2)
264
- console.log("inscribeTxId", inscribeTxId)
265
- return {
266
- inscribeTx: { hash: inscribeTxId, index: 0, value: ordinalAmount },
267
- inscribeDepositTx: inscribeDeposit,
268
- inscribeAddress: transferOrdinal.ordinalAddress,
269
- }
270
- }
271
-
272
- async deployBrc20(
273
- brc: { tick: string; max: number; limit: number },
274
- fee: FeeRateType = "normal",
275
- extendedUtxo?: ExtendedUtxo[],
276
- ) {
277
- let file = OrdinalWallet.deployBRC20Data(brc.tick, brc.max, brc.limit)
278
- return this.inscribeOrdinal(file, fee, extendedUtxo)
279
- }
280
-
281
- async mintBrc20(
282
- brc: { tick: string; amount: number | string },
283
- fee: FeeRateType = "normal",
284
- extendedUtxo?: ExtendedUtxo[],
285
- ) {
286
- let file = OrdinalWallet.mintBRC20Data(brc.tick, brc.amount)
287
- return this.inscribeOrdinal(file, fee, extendedUtxo)
288
- }
289
-
290
- async inscribeBrc20Unsigned(
291
- brc: { tick: string; amount: number | string },
292
- signer: SignerInfo,
293
- ordinalSigner?: SignerInfo,
294
- fee: FeeRateType = "normal",
295
- extendedUtxo?: ExtendedUtxo[],
296
- ) {
297
- let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount)
298
- return this.inscribeOrdinalUnsigned(file, signer, ordinalSigner, fee, extendedUtxo)
299
- }
300
-
301
- async inscribeBrc20(
302
- brc: { tick: string; amount: number | string },
303
- fee: FeeRateType = "normal",
304
- extendedUtxo?: ExtendedUtxo[],
305
- ) {
306
- let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount)
307
- return this.inscribeOrdinal(file, fee, extendedUtxo)
308
- }
309
-
310
- async transferBrc20Unsigned(
311
- receiver: string,
312
- brcInscribeUtxo: {
313
- hash: string
314
- value: number
315
- index: number
316
- },
317
- signer: SignerInfo,
318
- ordinalSigner?: SignerInfo,
319
- fee: FeeRateType = "normal",
320
- extendedUtxo?: ExtendedUtxo[],
321
- otherTargets?: Target[],
322
- ) {
323
- const ordinalSignerInfo = ordinalSigner || signer
324
- let utxo = extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
325
- let feeRate = await this.getFeeRate(fee)
326
- let unsignedTx = await this.transactionBuilder.createNftPsbt({
327
- extendedUtxo: utxo,
328
- nftExtendedUtxo: {
329
- hash: brcInscribeUtxo.hash,
330
- index: 0,
331
- value: brcInscribeUtxo.value,
332
- signerInfo: ordinalSignerInfo,
333
- },
334
- feeRate,
335
- receiverAddress: receiver,
336
- changeAddress: signer.address,
337
- otherTargets,
338
- })
339
- return {
340
- ...unsignedTx,
341
- possibleTxId: this.transactionBuilder.getUnsignedPsbtTxId(unsignedTx.unsignedTransaction),
342
- }
343
- }
344
-
345
- async transferBrc20(
346
- receiver: string,
347
- brcInscribeUtxo: {
348
- hash: string
349
- value: number
350
- index: number
351
- },
352
- fee: FeeRateType = "normal",
353
- extendedUtxo: ExtendedUtxo[],
354
- otherTargets?: Target[],
355
- ) {
356
- if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
357
- throw new Error("account not initialized")
358
- }
359
- let feeRate = await this.getFeeRate(fee)
360
- let unsignedTx = await this.transactionBuilder.createNftPsbt({
361
- extendedUtxo,
362
- nftExtendedUtxo: {
363
- hash: brcInscribeUtxo.hash,
364
- index: 0,
365
- value: brcInscribeUtxo.value,
366
- signerInfo: this.signerInfo!,
367
- },
368
- feeRate,
369
- receiverAddress: receiver,
370
- changeAddress: this.bitcoinAddress!,
371
- otherTargets,
372
- })
373
-
374
- let signedTx = await this.signer.signPsbt(unsignedTx, this.privateKey!)
375
- let finalTransferTxId = await this.sendSignedPsbtWithRetry(signedTx)
376
- return {
377
- hash: finalTransferTxId,
378
- index: 0,
379
- value: brcInscribeUtxo.value,
380
- inputs: unsignedTx.inputs,
381
- change: unsignedTx.change,
382
- changeIndex: unsignedTx.change ? unsignedTx.outputs.length : undefined,
383
- }
384
- }
385
-
386
- async inscribeAndTransferBrc20Unsigned(
387
- receiver: string,
388
- brc: { tick: string; amount: number | string },
389
- signer: SignerInfo,
390
- ordinalSigner?: SignerInfo,
391
- otherTargets?: Target[],
392
- fee: FeeRateType = "normal",
393
- extendedUtxo?: ExtendedUtxo[],
394
- ) {
395
- const ordinalSignerInfo = ordinalSigner || signer
396
- // check all fee before process transaction
397
- let brc20Balance = await this.unisat.getBrc20AddressBalanceForTicker(signer.address, brc.tick)
398
-
399
- if (BigNumber(brc20Balance.transferableBalance).gte(brc.amount)) {
400
- let transferrableInscription = brc20Balance.transferableInscriptions.find(
401
- (insc) => insc.data.tick === brc.tick && BigNumber(insc.data.amt).isEqualTo(brc.amount),
402
- )
403
-
404
- console.log("transferrableInscription", transferrableInscription)
405
- }
406
-
407
- if (BigNumber(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
408
- throw new Error("insufficient balance")
409
- }
410
-
411
- let feeRate = await this.getFeeRate(fee)
412
-
413
- let utxo1: ExtendedUtxo[] =
414
- extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
415
- let utxo2: ExtendedUtxo[] = []
416
- const { inscribeAddress, inscribeDepositUnsignedInfo, inscribeUnsignedInfo } =
417
- await this.inscribeBrc20Unsigned(brc, signer, ordinalSignerInfo, feeRate, utxo1)
418
-
419
- utxo2 = utxo1.filter(
420
- (u) =>
421
- inscribeDepositUnsignedInfo.inputs.findIndex(
422
- (i) => i.hash === u.hash && i.index === u.index,
423
- ) === -1,
424
- )
425
-
426
- if (inscribeDepositUnsignedInfo.change) {
427
- if (!inscribeDepositUnsignedInfo.possibleTxId) {
428
- throw new Error("inscribeDepositUnsignedInfo.possibleTxId is required")
429
- }
430
- utxo2.push({
431
- hash: inscribeDepositUnsignedInfo.possibleTxId,
432
- index: inscribeDepositUnsignedInfo.outputs.length,
433
- value: inscribeDepositUnsignedInfo.change.value,
434
- signerInfo: signer,
435
- })
436
- }
437
- let transferTxUnsignedInfo = await this.transferBrc20Unsigned(
438
- receiver,
439
- {
440
- hash: inscribeUnsignedInfo.possibleTxId,
441
- value: inscribeUnsignedInfo.outputs[0].value,
442
- index: 0,
443
- },
444
- signer,
445
- ordinalSignerInfo,
446
- feeRate,
447
- utxo2,
448
- otherTargets,
449
- )
450
- return {
451
- inscribeDepositUnsignedInfo,
452
- inscribeUnsignedInfo,
453
- transferTxUnsignedInfo,
454
- inscribeAddress,
455
- }
456
- }
457
-
458
- async inscribeAndTransferBrc20(
459
- receiver: string,
460
- brc: { tick: string; amount: number | string },
461
- otherTargets?: Target[],
462
- fee: FeeRateType = "normal",
463
- extendedUtxo?: ExtendedUtxo[],
464
- ) {
465
- // check all fee before process transaction
466
- let brc20Balance = await this.unisat.getBrc20AddressBalanceForTicker(
467
- this.bitcoinAddress!,
468
- brc.tick,
469
- )
470
-
471
- if (BigNumber(brc20Balance.transferableBalance).gte(brc.amount)) {
472
- let transferrableInscription = brc20Balance.transferableInscriptions.find(
473
- (insc) => insc.data.tick === brc.tick && BigNumber(insc.data.amt).isEqualTo(brc.amount),
474
- )
475
- console.log("transferrableInscription", transferrableInscription)
476
-
477
- if (transferrableInscription) {
478
- let ins = await this.btcInterface.unisat.getInscriptionInfo(
479
- transferrableInscription.inscriptionId,
480
- )
481
-
482
- if (ins) {
483
- let transferTx = await this.transferBrc20(
484
- receiver,
485
- {
486
- hash: ins.utxo.txid!,
487
- index: ins.utxo.vout!,
488
- value: ins.utxo.satoshi!,
489
- },
490
- await this.getFeeRate(fee),
491
- extendedUtxo ||
492
- (await this.btcInterface.getBTCUtxo(this.bitcoinAddress!, this.signerInfo!)),
493
- otherTargets,
494
- )
495
- return {
496
- transferTx,
497
- }
498
- }
499
- }
500
- }
501
-
502
- if (BigNumber(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
503
- throw new Error(`insufficient balance ${brc20Balance.availableBalanceSafe} ${brc.amount}`)
504
- }
505
-
506
- let feeRate = await this.getFeeRate(fee)
507
-
508
- let utxo1: ExtendedUtxo[] =
509
- extendedUtxo || (await this.btcInterface.getBTCUtxo(this.bitcoinAddress!, this.signerInfo!))
510
- let utxo2: ExtendedUtxo[] = []
511
- const { inscribeAddress, inscribeDepositTx, inscribeTx } = await this.inscribeBrc20(
512
- brc,
513
- feeRate,
514
- utxo1,
515
- )
516
-
517
- utxo2 = utxo1.filter(
518
- (u) =>
519
- inscribeDepositTx.inputs.findIndex((i) => i.hash === u.hash && i.index === u.index) === -1,
520
- )
521
-
522
- if (inscribeDepositTx.change) {
523
- utxo2.push({
524
- hash: inscribeDepositTx.hash,
525
- index: inscribeDepositTx.changeIndex!,
526
- value: inscribeDepositTx.change.value,
527
- signerInfo: this.signerInfo!,
528
- })
529
- }
530
- await sleep(10 * 1000)
531
- let transferTx = await this.transferBrc20(receiver, inscribeTx, feeRate, utxo2, otherTargets)
532
- return {
533
- inscribeTx,
534
- inscribeAddress,
535
- inscribeDepositTx,
536
- transferTx,
537
- }
538
- }
539
-
540
- async wrapBrc20Unsigned(
541
- recipientAddress: string,
542
- brc: { tick: string; amount: number | string },
543
- brc20TokenId: number,
544
- signer: SignerInfo,
545
- lockerAddress: string,
546
- exchange?: {
547
- outputToken: string
548
- outputAmount: string
549
- },
550
- ordinalSigner?: SignerInfo,
551
- fee: FeeRateType = "normal",
552
- extendedUtxo?: ExtendedUtxo[],
553
-
554
- { chainId = 137, appId = exchange ? 1 : 0 } = {},
555
- ) {
556
- const isExchange = !!exchange
557
- let dataHex = generateBrc2OpReturn({
558
- chainId,
559
- appId,
560
- brc20TokenId,
561
- inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
562
- recipientAddress,
563
- isExchange,
564
- outputToken: exchange?.outputToken,
565
- outputAmount: exchange?.outputAmount,
566
- })
567
- let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
568
- return this.inscribeAndTransferBrc20Unsigned(
569
- lockerAddress,
570
- brc,
571
- signer,
572
- ordinalSigner,
573
- [opTarget],
574
- fee,
575
- extendedUtxo,
576
- )
577
- }
578
-
579
- async wrapBrc20OnlyTransferUnsigned(
580
- recipientAddress: string,
581
- brcInscribeTx: {
582
- hash: string
583
- value: number
584
- index: number
585
- },
586
- brc: { tick: string; amount: number | string },
587
- brc20TokenId: number,
588
- signer: SignerInfo,
589
- lockerAddress: string,
590
- exchange?: {
591
- outputToken: string
592
- outputAmount: string
593
- },
594
- ordinalSigner?: SignerInfo,
595
- fee: FeeRateType = "normal",
596
- extendedUtxo?: ExtendedUtxo[],
597
-
598
- { chainId = 137, appId = exchange ? 1 : 0 } = {},
599
- ) {
600
- let utxo: ExtendedUtxo[] =
601
- extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
602
-
603
- const isExchange = !!exchange
604
- let dataHex = generateBrc2OpReturn({
605
- chainId,
606
- appId,
607
- brc20TokenId,
608
- inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
609
- recipientAddress,
610
- isExchange,
611
- outputToken: exchange?.outputToken,
612
- outputAmount: exchange?.outputAmount,
613
- })
614
- let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
615
- let feeRate = await this.getFeeRate(fee)
616
- let transferTxUnsignedInfo = await this.transferBrc20Unsigned(
617
- lockerAddress,
618
- brcInscribeTx,
619
- signer,
620
- ordinalSigner,
621
- feeRate,
622
- utxo,
623
- [opTarget],
624
- )
625
- return transferTxUnsignedInfo
626
- }
627
-
628
- async wrapBrc20(
629
- recipientAddress: string,
630
- brc: { tick: string; amount: number | string },
631
- brc20TokenId: number,
632
- lockerAddress: string,
633
- exchange?: {
634
- outputToken: string
635
- outputAmount: string
636
- },
637
- fee: FeeRateType = "normal",
638
- extendedUtxo?: ExtendedUtxo[],
639
-
640
- { chainId = 137, appId = exchange ? 1 : 0 } = {},
641
- ) {
642
- const isExchange = !!exchange
643
-
644
- let dataHex = generateBrc2OpReturn({
645
- chainId,
646
- appId,
647
- brc20TokenId,
648
- inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
649
- recipientAddress,
650
- isExchange,
651
- outputToken: exchange?.outputToken,
652
- outputAmount: exchange?.outputAmount,
653
- })
654
- let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
655
- return this.inscribeAndTransferBrc20(lockerAddress, brc, [opTarget], fee, extendedUtxo)
656
- }
657
- }
658
-
659
- export default OrdinalWallet
1
+ import BigNumber from "bignumber.js"
2
+ import { bitcoin as bitcoinProviders } from "@teleportdao/providers"
3
+ import { OrdinalTransactionBuilder } from "./transaction-builder/ordinal-transaction-builder"
4
+ import BitcoinSign from "./sign/sign-transaction"
5
+ //
6
+ import { BitcoinInterfaceOrdinal } from "./bitcoin-interface-ordinal"
7
+ import { generateBrc2OpReturn } from "./helper/brc20-helper"
8
+ import { sleep } from "./utils/tools"
9
+ import { ChangeTarget, ExtendedUtxo, SignerInfo, Target } from "./transaction-builder"
10
+ import { BitcoinBaseWallet, FeeRateType } from "./bitcoin-wallet-base"
11
+ import { BTCTokenConnectionInfo, RPCConnectionInfo, UtxoConnectionInfo } from "./type"
12
+
13
+ class OrdinalWallet extends BitcoinBaseWallet {
14
+ unisat: bitcoinProviders.UniSat
15
+ transactionBuilder: OrdinalTransactionBuilder
16
+ btcInterface: BitcoinInterfaceOrdinal
17
+ signer: BitcoinSign
18
+ constructor(
19
+ networkName: string,
20
+ uniSatToken: string,
21
+ connectionInfo: {
22
+ // btcToken: BTCTokenConnectionInfo
23
+ rpc: RPCConnectionInfo
24
+ utxo?: UtxoConnectionInfo
25
+ // rpc used for getRawTransaction in transaction builder if set (optional)
26
+ },
27
+ ) {
28
+ if (!connectionInfo.rpc) {
29
+ // because api providers usually don't support sending custom psbt include p2tr address so we need rpc provider
30
+ throw new Error("rpc is required")
31
+ }
32
+ super(networkName, connectionInfo)
33
+
34
+ this.transactionBuilder = new OrdinalTransactionBuilder(
35
+ networkName,
36
+ this.network,
37
+ connectionInfo,
38
+ )
39
+ this.signer = new BitcoinSign(this.network)
40
+ this.btcInterface = new BitcoinInterfaceOrdinal(connectionInfo, networkName, uniSatToken)
41
+ this.unisat = this.btcInterface.unisat
42
+ }
43
+
44
+ static deployBRC20Data(tickName: string, max: number | string, limit: number | string) {
45
+ let data = {
46
+ p: "brc-20",
47
+ op: "deploy",
48
+ tick: tickName,
49
+ max: `${max}`,
50
+ lim: `${limit}`,
51
+ }
52
+ return {
53
+ buffer: Buffer.from(JSON.stringify(data), "utf8"),
54
+ type: "text/plain",
55
+ // type: "application/json",
56
+ }
57
+ }
58
+
59
+ static mintBRC20Data(tickName: string, amount: string | number) {
60
+ if (BigNumber(amount).isLessThanOrEqualTo(0)) throw new Error("amount should be greater than 0")
61
+ let data = {
62
+ p: "brc-20",
63
+ op: "mint",
64
+ tick: tickName,
65
+ amt: `${amount}`,
66
+ }
67
+ return {
68
+ buffer: Buffer.from(JSON.stringify(data), "utf8"),
69
+ type: "text/plain",
70
+ // type: "application/json",
71
+ }
72
+ }
73
+
74
+ static transferBRC20Data(tickName: string, amount: string | number) {
75
+ if (BigNumber(amount).isLessThanOrEqualTo(0)) throw new Error("amount should be greater than 0")
76
+ let data = {
77
+ p: "brc-20",
78
+ op: "transfer",
79
+ tick: tickName,
80
+ amt: `${amount}`,
81
+ }
82
+ return {
83
+ buffer: Buffer.from(JSON.stringify(data), "utf8"),
84
+ type: "text/plain",
85
+ // type: "application/json",
86
+ }
87
+ }
88
+
89
+ async inscribeOrdinalDepositUnsigned(
90
+ file: {
91
+ buffer: Buffer
92
+ type: string
93
+ },
94
+ signer: SignerInfo,
95
+ ordinalSigner?: SignerInfo,
96
+ fee: FeeRateType = "normal",
97
+ extendedUtxo?: ExtendedUtxo[],
98
+ ) {
99
+ const ordinalSignerPublicKey = ordinalSigner?.publicKey || signer.publicKey
100
+ const publicKey = Buffer.from(ordinalSignerPublicKey, "hex")
101
+ let transferOrdinal = this.transactionBuilder.createOrdinalAddress(file, publicKey)
102
+ const leafScript = transferOrdinal.redeem.output
103
+ const { ordinalAddress } = transferOrdinal
104
+
105
+ let feeRate = await this.getFeeRate(fee)
106
+ let extraFee = +(((400 + leafScript.length) / 4) * feeRate * 1.5).toFixed(0)
107
+ let ordinalAmount = 600
108
+
109
+ let utxo1: ExtendedUtxo[] =
110
+ extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
111
+
112
+ let inscribeDepositUnsignedInfo = await this.sendBTCUnsignedTx(
113
+ ordinalAddress,
114
+ ordinalAmount + extraFee,
115
+ signer,
116
+ feeRate,
117
+ utxo1,
118
+ )
119
+
120
+ return {
121
+ inscribeDepositUnsignedInfo,
122
+ transferOrdinal,
123
+ }
124
+ }
125
+
126
+ async inscribeOrdinalUnsigned(
127
+ file: {
128
+ buffer: Buffer
129
+ type: string
130
+ },
131
+ signer: SignerInfo,
132
+ ordinalSigner?: SignerInfo,
133
+ fee: FeeRateType = "normal",
134
+ extendedUtxo?: ExtendedUtxo[],
135
+ ) {
136
+ const receiverAddress = ordinalSigner?.address || signer.address
137
+ const { inscribeDepositUnsignedInfo, transferOrdinal } =
138
+ await this.inscribeOrdinalDepositUnsigned(file, signer, ordinalSigner, fee, extendedUtxo)
139
+ const { ordinalAddress } = transferOrdinal
140
+ let ordinalAmount = 600
141
+
142
+ if (!inscribeDepositUnsignedInfo.possibleTxId)
143
+ throw new Error(
144
+ "inscribeDepositUnsignedInfo.possibleTxId is required. inscriber address type is not p2tr",
145
+ )
146
+
147
+ let inscribeDeposit: {
148
+ hash: string
149
+ value: number
150
+ index: number
151
+ } = {
152
+ hash: inscribeDepositUnsignedInfo.possibleTxId,
153
+ value: inscribeDepositUnsignedInfo.outputs[0].value,
154
+ index: 0,
155
+ }
156
+ let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(
157
+ transferOrdinal,
158
+ inscribeDeposit,
159
+ receiverAddress,
160
+ ordinalAmount,
161
+ )
162
+ return {
163
+ inscribeDepositUnsignedInfo,
164
+ inscribeUnsignedInfo,
165
+ inscribeAddress: ordinalAddress,
166
+ receiverAddress,
167
+ }
168
+ }
169
+
170
+ async inscribeOrdinal(
171
+ file: {
172
+ buffer: Buffer
173
+ type: string
174
+ },
175
+ fee: FeeRateType = "normal",
176
+ extendedUtxo?: ExtendedUtxo[],
177
+ ordinalReceiverAddress?: string,
178
+ ) {
179
+ if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
180
+ throw new Error("account not initialized")
181
+ }
182
+ const receiverAddress = ordinalReceiverAddress || this.bitcoinAddress!
183
+
184
+ const { inscribeDepositUnsignedInfo, transferOrdinal } =
185
+ await this.inscribeOrdinalDepositUnsigned(
186
+ file,
187
+ this.signerInfo!,
188
+ undefined,
189
+ fee,
190
+ extendedUtxo,
191
+ )
192
+ let ordinalUtxo = await this.btcInterface.getBTCUtxo(
193
+ transferOrdinal.ordinalAddress,
194
+ this.signerInfo!,
195
+ )
196
+
197
+ let inscribeDeposit: {
198
+ inputs: {
199
+ hash: string
200
+ value: number
201
+ index: number
202
+ signerInfo: SignerInfo
203
+ }[]
204
+ hash: string
205
+ value: number
206
+ index: number
207
+ change?: ChangeTarget
208
+ changeIndex?: number
209
+ }
210
+
211
+ if (ordinalUtxo.length > 1) {
212
+ throw new Error("multiple deposit found for this ordinal address")
213
+ }
214
+
215
+ if (
216
+ ordinalUtxo.length === 0 ||
217
+ ordinalUtxo[0].value <
218
+ inscribeDepositUnsignedInfo.outputs[0].value -
219
+ inscribeDepositUnsignedInfo.outputs[0].value * 0.15
220
+ ) {
221
+ const signedPsbt1 = await this.signer.signPsbt(inscribeDepositUnsignedInfo, this.privateKey!)
222
+ console.log("inscribe deposit tx ...")
223
+ const inscribeDepositTxId = await this.sendSignedPsbt(signedPsbt1)
224
+ console.log(`inscribe deposit txId : ${inscribeDepositTxId}`)
225
+
226
+ inscribeDeposit = {
227
+ hash: inscribeDepositTxId,
228
+ value: inscribeDepositUnsignedInfo.outputs[0].value,
229
+ index: 0,
230
+ inputs: inscribeDepositUnsignedInfo.inputs,
231
+ change: inscribeDepositUnsignedInfo.change,
232
+ changeIndex: inscribeDepositUnsignedInfo.change
233
+ ? inscribeDepositUnsignedInfo.outputs.length
234
+ : undefined,
235
+ }
236
+ } else {
237
+ inscribeDeposit = {
238
+ hash: ordinalUtxo[0].hash,
239
+ value: ordinalUtxo[0].value,
240
+ index: ordinalUtxo[0].index,
241
+ inputs: [],
242
+ }
243
+ console.log("no need to deposit", inscribeDeposit)
244
+ }
245
+
246
+ const ordinalAmount = 600
247
+ let inscribeUnsignedInfo = this.transactionBuilder.createInscribeUnsignedTx(
248
+ transferOrdinal,
249
+ inscribeDeposit,
250
+ receiverAddress,
251
+ ordinalAmount,
252
+ )
253
+
254
+ const signedPsbt2 = await this.signer.signPsbt(
255
+ inscribeUnsignedInfo,
256
+ this.privateKey!,
257
+ undefined,
258
+ false,
259
+ )
260
+
261
+ console.log("inscribeTxId ...")
262
+ await sleep(10 * 1000)
263
+ let inscribeTxId = await this.sendSignedPsbtWithRetry(signedPsbt2)
264
+ console.log("inscribeTxId", inscribeTxId)
265
+ return {
266
+ inscribeTx: { hash: inscribeTxId, index: 0, value: ordinalAmount },
267
+ inscribeDepositTx: inscribeDeposit,
268
+ inscribeAddress: transferOrdinal.ordinalAddress,
269
+ }
270
+ }
271
+
272
+ async deployBrc20(
273
+ brc: { tick: string; max: number; limit: number },
274
+ fee: FeeRateType = "normal",
275
+ extendedUtxo?: ExtendedUtxo[],
276
+ ) {
277
+ let file = OrdinalWallet.deployBRC20Data(brc.tick, brc.max, brc.limit)
278
+ return this.inscribeOrdinal(file, fee, extendedUtxo)
279
+ }
280
+
281
+ async mintBrc20(
282
+ brc: { tick: string; amount: number | string },
283
+ fee: FeeRateType = "normal",
284
+ extendedUtxo?: ExtendedUtxo[],
285
+ ) {
286
+ let file = OrdinalWallet.mintBRC20Data(brc.tick, brc.amount)
287
+ return this.inscribeOrdinal(file, fee, extendedUtxo)
288
+ }
289
+
290
+ async inscribeBrc20Unsigned(
291
+ brc: { tick: string; amount: number | string },
292
+ signer: SignerInfo,
293
+ ordinalSigner?: SignerInfo,
294
+ fee: FeeRateType = "normal",
295
+ extendedUtxo?: ExtendedUtxo[],
296
+ ) {
297
+ let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount)
298
+ return this.inscribeOrdinalUnsigned(file, signer, ordinalSigner, fee, extendedUtxo)
299
+ }
300
+
301
+ async inscribeBrc20(
302
+ brc: { tick: string; amount: number | string },
303
+ fee: FeeRateType = "normal",
304
+ extendedUtxo?: ExtendedUtxo[],
305
+ ) {
306
+ let file = OrdinalWallet.transferBRC20Data(brc.tick, brc.amount)
307
+ return this.inscribeOrdinal(file, fee, extendedUtxo)
308
+ }
309
+
310
+ async transferBrc20Unsigned(
311
+ receiver: string,
312
+ brcInscribeUtxo: {
313
+ hash: string
314
+ value: number
315
+ index: number
316
+ },
317
+ signer: SignerInfo,
318
+ ordinalSigner?: SignerInfo,
319
+ fee: FeeRateType = "normal",
320
+ extendedUtxo?: ExtendedUtxo[],
321
+ otherTargets?: Target[],
322
+ ) {
323
+ const ordinalSignerInfo = ordinalSigner || signer
324
+ let utxo = extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
325
+ let feeRate = await this.getFeeRate(fee)
326
+ let unsignedTx = await this.transactionBuilder.createNftPsbt({
327
+ extendedUtxo: utxo,
328
+ nftExtendedUtxo: {
329
+ hash: brcInscribeUtxo.hash,
330
+ index: 0,
331
+ value: brcInscribeUtxo.value,
332
+ signerInfo: ordinalSignerInfo,
333
+ },
334
+ feeRate,
335
+ receiverAddress: receiver,
336
+ changeAddress: signer.address,
337
+ otherTargets,
338
+ })
339
+ return {
340
+ ...unsignedTx,
341
+ possibleTxId: this.transactionBuilder.getUnsignedPsbtTxId(unsignedTx.unsignedTransaction),
342
+ }
343
+ }
344
+
345
+ async transferBrc20(
346
+ receiver: string,
347
+ brcInscribeUtxo: {
348
+ hash: string
349
+ value: number
350
+ index: number
351
+ },
352
+ fee: FeeRateType = "normal",
353
+ extendedUtxo: ExtendedUtxo[],
354
+ otherTargets?: Target[],
355
+ ) {
356
+ if (!this.currentAccount || !this.currentAccountType || !this.publicKey || !this.privateKey) {
357
+ throw new Error("account not initialized")
358
+ }
359
+ let feeRate = await this.getFeeRate(fee)
360
+ let unsignedTx = await this.transactionBuilder.createNftPsbt({
361
+ extendedUtxo,
362
+ nftExtendedUtxo: {
363
+ hash: brcInscribeUtxo.hash,
364
+ index: 0,
365
+ value: brcInscribeUtxo.value,
366
+ signerInfo: this.signerInfo!,
367
+ },
368
+ feeRate,
369
+ receiverAddress: receiver,
370
+ changeAddress: this.bitcoinAddress!,
371
+ otherTargets,
372
+ })
373
+
374
+ let signedTx = await this.signer.signPsbt(unsignedTx, this.privateKey!)
375
+ let finalTransferTxId = await this.sendSignedPsbtWithRetry(signedTx)
376
+ return {
377
+ hash: finalTransferTxId,
378
+ index: 0,
379
+ value: brcInscribeUtxo.value,
380
+ inputs: unsignedTx.inputs,
381
+ change: unsignedTx.change,
382
+ changeIndex: unsignedTx.change ? unsignedTx.outputs.length : undefined,
383
+ }
384
+ }
385
+
386
+ async inscribeAndTransferBrc20Unsigned(
387
+ receiver: string,
388
+ brc: { tick: string; amount: number | string },
389
+ signer: SignerInfo,
390
+ ordinalSigner?: SignerInfo,
391
+ otherTargets?: Target[],
392
+ fee: FeeRateType = "normal",
393
+ extendedUtxo?: ExtendedUtxo[],
394
+ ) {
395
+ const ordinalSignerInfo = ordinalSigner || signer
396
+ // check all fee before process transaction
397
+ let brc20Balance = await this.unisat.getBrc20AddressBalanceForTicker(signer.address, brc.tick)
398
+
399
+ if (BigNumber(brc20Balance.transferableBalance).gte(brc.amount)) {
400
+ let transferrableInscription = brc20Balance.transferableInscriptions.find(
401
+ (insc) => insc.data.tick === brc.tick && BigNumber(insc.data.amt).isEqualTo(brc.amount),
402
+ )
403
+
404
+ console.log("transferrableInscription", transferrableInscription)
405
+ }
406
+
407
+ if (BigNumber(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
408
+ throw new Error("insufficient balance")
409
+ }
410
+
411
+ let feeRate = await this.getFeeRate(fee)
412
+
413
+ let utxo1: ExtendedUtxo[] =
414
+ extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
415
+ let utxo2: ExtendedUtxo[] = []
416
+ const { inscribeAddress, inscribeDepositUnsignedInfo, inscribeUnsignedInfo } =
417
+ await this.inscribeBrc20Unsigned(brc, signer, ordinalSignerInfo, feeRate, utxo1)
418
+
419
+ utxo2 = utxo1.filter(
420
+ (u) =>
421
+ inscribeDepositUnsignedInfo.inputs.findIndex(
422
+ (i) => i.hash === u.hash && i.index === u.index,
423
+ ) === -1,
424
+ )
425
+
426
+ if (inscribeDepositUnsignedInfo.change) {
427
+ if (!inscribeDepositUnsignedInfo.possibleTxId) {
428
+ throw new Error("inscribeDepositUnsignedInfo.possibleTxId is required")
429
+ }
430
+ utxo2.push({
431
+ hash: inscribeDepositUnsignedInfo.possibleTxId,
432
+ index: inscribeDepositUnsignedInfo.outputs.length,
433
+ value: inscribeDepositUnsignedInfo.change.value,
434
+ signerInfo: signer,
435
+ })
436
+ }
437
+ let transferTxUnsignedInfo = await this.transferBrc20Unsigned(
438
+ receiver,
439
+ {
440
+ hash: inscribeUnsignedInfo.possibleTxId,
441
+ value: inscribeUnsignedInfo.outputs[0].value,
442
+ index: 0,
443
+ },
444
+ signer,
445
+ ordinalSignerInfo,
446
+ feeRate,
447
+ utxo2,
448
+ otherTargets,
449
+ )
450
+ return {
451
+ inscribeDepositUnsignedInfo,
452
+ inscribeUnsignedInfo,
453
+ transferTxUnsignedInfo,
454
+ inscribeAddress,
455
+ }
456
+ }
457
+
458
+ async inscribeAndTransferBrc20(
459
+ receiver: string,
460
+ brc: { tick: string; amount: number | string },
461
+ otherTargets?: Target[],
462
+ fee: FeeRateType = "normal",
463
+ extendedUtxo?: ExtendedUtxo[],
464
+ ) {
465
+ // check all fee before process transaction
466
+ let brc20Balance = await this.unisat.getBrc20AddressBalanceForTicker(
467
+ this.bitcoinAddress!,
468
+ brc.tick,
469
+ )
470
+
471
+ if (BigNumber(brc20Balance.transferableBalance).gte(brc.amount)) {
472
+ let transferrableInscription = brc20Balance.transferableInscriptions.find(
473
+ (insc) => insc.data.tick === brc.tick && BigNumber(insc.data.amt).isEqualTo(brc.amount),
474
+ )
475
+ console.log("transferrableInscription", transferrableInscription)
476
+
477
+ if (transferrableInscription) {
478
+ let ins = await this.btcInterface.unisat.getInscriptionInfo(
479
+ transferrableInscription.inscriptionId,
480
+ )
481
+
482
+ if (ins) {
483
+ let transferTx = await this.transferBrc20(
484
+ receiver,
485
+ {
486
+ hash: ins.utxo.txid!,
487
+ index: ins.utxo.vout!,
488
+ value: ins.utxo.satoshi!,
489
+ },
490
+ await this.getFeeRate(fee),
491
+ extendedUtxo ||
492
+ (await this.btcInterface.getBTCUtxo(this.bitcoinAddress!, this.signerInfo!)),
493
+ otherTargets,
494
+ )
495
+ return {
496
+ transferTx,
497
+ }
498
+ }
499
+ }
500
+ }
501
+
502
+ if (BigNumber(brc20Balance.availableBalanceSafe).isLessThan(brc.amount)) {
503
+ throw new Error(`insufficient balance ${brc20Balance.availableBalanceSafe} ${brc.amount}`)
504
+ }
505
+
506
+ let feeRate = await this.getFeeRate(fee)
507
+
508
+ let utxo1: ExtendedUtxo[] =
509
+ extendedUtxo || (await this.btcInterface.getBTCUtxo(this.bitcoinAddress!, this.signerInfo!))
510
+ let utxo2: ExtendedUtxo[] = []
511
+ const { inscribeAddress, inscribeDepositTx, inscribeTx } = await this.inscribeBrc20(
512
+ brc,
513
+ feeRate,
514
+ utxo1,
515
+ )
516
+
517
+ utxo2 = utxo1.filter(
518
+ (u) =>
519
+ inscribeDepositTx.inputs.findIndex((i) => i.hash === u.hash && i.index === u.index) === -1,
520
+ )
521
+
522
+ if (inscribeDepositTx.change) {
523
+ utxo2.push({
524
+ hash: inscribeDepositTx.hash,
525
+ index: inscribeDepositTx.changeIndex!,
526
+ value: inscribeDepositTx.change.value,
527
+ signerInfo: this.signerInfo!,
528
+ })
529
+ }
530
+ await sleep(10 * 1000)
531
+ let transferTx = await this.transferBrc20(receiver, inscribeTx, feeRate, utxo2, otherTargets)
532
+ return {
533
+ inscribeTx,
534
+ inscribeAddress,
535
+ inscribeDepositTx,
536
+ transferTx,
537
+ }
538
+ }
539
+
540
+ async wrapBrc20Unsigned(
541
+ recipientAddress: string,
542
+ brc: { tick: string; amount: number | string },
543
+ brc20TokenId: number,
544
+ signer: SignerInfo,
545
+ lockerAddress: string,
546
+ exchange?: {
547
+ outputToken: string
548
+ outputAmount: string
549
+ },
550
+ ordinalSigner?: SignerInfo,
551
+ fee: FeeRateType = "normal",
552
+ extendedUtxo?: ExtendedUtxo[],
553
+
554
+ { chainId = 137, appId = exchange ? 1 : 0 } = {},
555
+ ) {
556
+ const isExchange = !!exchange
557
+ let dataHex = generateBrc2OpReturn({
558
+ chainId,
559
+ appId,
560
+ brc20TokenId,
561
+ inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
562
+ recipientAddress,
563
+ isExchange,
564
+ outputToken: exchange?.outputToken,
565
+ outputAmount: exchange?.outputAmount,
566
+ })
567
+ let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
568
+ return this.inscribeAndTransferBrc20Unsigned(
569
+ lockerAddress,
570
+ brc,
571
+ signer,
572
+ ordinalSigner,
573
+ [opTarget],
574
+ fee,
575
+ extendedUtxo,
576
+ )
577
+ }
578
+
579
+ async wrapBrc20OnlyTransferUnsigned(
580
+ recipientAddress: string,
581
+ brcInscribeTx: {
582
+ hash: string
583
+ value: number
584
+ index: number
585
+ },
586
+ brc: { tick: string; amount: number | string },
587
+ brc20TokenId: number,
588
+ signer: SignerInfo,
589
+ lockerAddress: string,
590
+ exchange?: {
591
+ outputToken: string
592
+ outputAmount: string
593
+ },
594
+ ordinalSigner?: SignerInfo,
595
+ fee: FeeRateType = "normal",
596
+ extendedUtxo?: ExtendedUtxo[],
597
+
598
+ { chainId = 137, appId = exchange ? 1 : 0 } = {},
599
+ ) {
600
+ let utxo: ExtendedUtxo[] =
601
+ extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
602
+
603
+ const isExchange = !!exchange
604
+ let dataHex = generateBrc2OpReturn({
605
+ chainId,
606
+ appId,
607
+ brc20TokenId,
608
+ inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
609
+ recipientAddress,
610
+ isExchange,
611
+ outputToken: exchange?.outputToken,
612
+ outputAmount: exchange?.outputAmount,
613
+ })
614
+ let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
615
+ let feeRate = await this.getFeeRate(fee)
616
+ let transferTxUnsignedInfo = await this.transferBrc20Unsigned(
617
+ lockerAddress,
618
+ brcInscribeTx,
619
+ signer,
620
+ ordinalSigner,
621
+ feeRate,
622
+ utxo,
623
+ [opTarget],
624
+ )
625
+ return transferTxUnsignedInfo
626
+ }
627
+
628
+ async wrapBrc20(
629
+ recipientAddress: string,
630
+ brc: { tick: string; amount: number | string },
631
+ brc20TokenId: number,
632
+ lockerAddress: string,
633
+ exchange?: {
634
+ outputToken: string
635
+ outputAmount: string
636
+ },
637
+ fee: FeeRateType = "normal",
638
+ extendedUtxo?: ExtendedUtxo[],
639
+
640
+ { chainId = 137, appId = exchange ? 1 : 0 } = {},
641
+ ) {
642
+ const isExchange = !!exchange
643
+
644
+ let dataHex = generateBrc2OpReturn({
645
+ chainId,
646
+ appId,
647
+ brc20TokenId,
648
+ inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
649
+ recipientAddress,
650
+ isExchange,
651
+ outputToken: exchange?.outputToken,
652
+ outputAmount: exchange?.outputAmount,
653
+ })
654
+ let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
655
+ return this.inscribeAndTransferBrc20(lockerAddress, brc, [opTarget], fee, extendedUtxo)
656
+ }
657
+ }
658
+
659
+ export default OrdinalWallet