@teleportdao/bitcoin 1.9.0 → 2.0.0
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/.tmp/block-parser.ts +58 -0
- package/.tmp/ordinal-helper.ts +133 -0
- package/.tmp/ordinal.ts +25 -0
- package/.tmp/rbf.ts +27 -24
- package/dist/bitcoin-interface-ordinal.d.ts +2 -2
- package/dist/bitcoin-interface-ordinal.d.ts.map +1 -1
- package/dist/bitcoin-interface-ordinal.js +1 -1
- package/dist/bitcoin-interface-ordinal.js.map +1 -1
- package/dist/bitcoin-interface-teleswap.d.ts +2 -53
- package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
- package/dist/bitcoin-interface-teleswap.js +6 -17
- package/dist/bitcoin-interface-teleswap.js.map +1 -1
- package/dist/bitcoin-interface-wallet.d.ts +29 -0
- package/dist/bitcoin-interface-wallet.d.ts.map +1 -0
- package/dist/bitcoin-interface-wallet.js +126 -0
- package/dist/bitcoin-interface-wallet.js.map +1 -0
- package/dist/bitcoin-interface.d.ts +5 -23
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +13 -92
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-wallet-base.d.ts +55 -31
- package/dist/bitcoin-wallet-base.d.ts.map +1 -1
- package/dist/bitcoin-wallet-base.js +105 -84
- package/dist/bitcoin-wallet-base.js.map +1 -1
- package/dist/ordinal-wallet.d.ts +29 -71
- package/dist/ordinal-wallet.d.ts.map +1 -1
- package/dist/ordinal-wallet.js +48 -108
- package/dist/ordinal-wallet.js.map +1 -1
- package/dist/teleswap-wallet.d.ts +10 -16
- package/dist/teleswap-wallet.d.ts.map +1 -1
- package/dist/teleswap-wallet.js +10 -30
- package/dist/teleswap-wallet.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +4 -11
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +2 -23
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts +3 -2
- package/dist/transaction-builder/ordinal-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/ordinal-transaction-builder.js +1 -6
- package/dist/transaction-builder/ordinal-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts +3 -7
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +22 -49
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/type.d.ts +33 -18
- package/dist/type.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/bitcoin-interface-ordinal.ts +185 -181
- package/src/bitcoin-interface-teleswap.ts +237 -252
- package/src/bitcoin-interface-utils.ts +60 -60
- package/src/bitcoin-interface-wallet.ts +114 -0
- package/src/bitcoin-interface.ts +156 -239
- package/src/bitcoin-utils.ts +591 -591
- package/src/bitcoin-wallet-base.ts +344 -310
- 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 -748
- package/src/sign/index.ts +1 -1
- package/src/sign/sign-transaction.ts +108 -108
- package/src/teleswap-wallet.ts +133 -155
- package/src/transaction-builder/bitcoin-transaction-builder.ts +26 -44
- package/src/transaction-builder/index.ts +3 -3
- package/src/transaction-builder/ordinal-transaction-builder.ts +139 -147
- package/src/transaction-builder/transaction-builder.ts +690 -706
- package/src/type.ts +74 -48
- 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/src/ordinal-wallet.ts
CHANGED
|
@@ -1,748 +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 {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (!connectionInfo.rpc
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
let
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
)
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
inscribeDeposit
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
let
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
let
|
|
376
|
-
return
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
) {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
let
|
|
414
|
-
|
|
415
|
-
let
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
(
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
await
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
staticFeeRate,
|
|
661
|
-
)
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
async wrapBrc20OnlyTransferUnsigned(
|
|
665
|
-
recipientAddress: string,
|
|
666
|
-
brcInscribeTx: {
|
|
667
|
-
hash: string
|
|
668
|
-
value: number
|
|
669
|
-
index: number
|
|
670
|
-
},
|
|
671
|
-
brc: { tick: string; amount: number | string },
|
|
672
|
-
brc20TokenId: number,
|
|
673
|
-
signer: SignerInfo,
|
|
674
|
-
lockerAddress: string,
|
|
675
|
-
exchange?: {
|
|
676
|
-
outputToken: string
|
|
677
|
-
outputAmount: string
|
|
678
|
-
},
|
|
679
|
-
ordinalSigner?: SignerInfo,
|
|
680
|
-
extendedUtxo?: ExtendedUtxo[],
|
|
681
|
-
staticFeeRate?: number,
|
|
682
|
-
{ chainId = 137, appId = exchange ? 1 : 0 } = {},
|
|
683
|
-
) {
|
|
684
|
-
let utxo: ExtendedUtxo[] =
|
|
685
|
-
extendedUtxo || (await this.btcInterface.getBTCUtxo(signer.address, signer))
|
|
686
|
-
|
|
687
|
-
const isExchange = !!exchange
|
|
688
|
-
let dataHex = generateBrc2OpReturn({
|
|
689
|
-
chainId,
|
|
690
|
-
appId,
|
|
691
|
-
brc20TokenId,
|
|
692
|
-
inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
693
|
-
recipientAddress,
|
|
694
|
-
isExchange,
|
|
695
|
-
outputToken: exchange?.outputToken,
|
|
696
|
-
outputAmount: exchange?.outputAmount,
|
|
697
|
-
})
|
|
698
|
-
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
|
|
699
|
-
let feeRate = staticFeeRate || (await this.btcInterface.getFeeRate("normal"))
|
|
700
|
-
let transferTxUnsignedInfo = await this.transferBrc20Unsigned(
|
|
701
|
-
lockerAddress,
|
|
702
|
-
brcInscribeTx,
|
|
703
|
-
signer,
|
|
704
|
-
ordinalSigner,
|
|
705
|
-
utxo,
|
|
706
|
-
feeRate,
|
|
707
|
-
[opTarget],
|
|
708
|
-
)
|
|
709
|
-
return transferTxUnsignedInfo
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
async wrapBrc20(
|
|
713
|
-
recipientAddress: string,
|
|
714
|
-
brc: { tick: string; amount: number | string },
|
|
715
|
-
brc20TokenId: number,
|
|
716
|
-
lockerAddress: string,
|
|
717
|
-
exchange?: {
|
|
718
|
-
outputToken: string
|
|
719
|
-
outputAmount: string
|
|
720
|
-
},
|
|
721
|
-
extendedUtxo?: ExtendedUtxo[],
|
|
722
|
-
staticFeeRate?: number,
|
|
723
|
-
{ chainId = 137, appId = exchange ? 1 : 0 } = {},
|
|
724
|
-
) {
|
|
725
|
-
const isExchange = !!exchange
|
|
726
|
-
|
|
727
|
-
let dataHex = generateBrc2OpReturn({
|
|
728
|
-
chainId,
|
|
729
|
-
appId,
|
|
730
|
-
brc20TokenId,
|
|
731
|
-
inputAmount: BigNumber(brc.amount).multipliedBy(1e18).toFixed(0),
|
|
732
|
-
recipientAddress,
|
|
733
|
-
isExchange,
|
|
734
|
-
outputToken: exchange?.outputToken,
|
|
735
|
-
outputAmount: exchange?.outputAmount,
|
|
736
|
-
})
|
|
737
|
-
let opTarget = this.transactionBuilder.getOpReturnTarget(dataHex)
|
|
738
|
-
return this.inscribeAndTransferBrc20(
|
|
739
|
-
lockerAddress,
|
|
740
|
-
brc,
|
|
741
|
-
[opTarget],
|
|
742
|
-
extendedUtxo,
|
|
743
|
-
staticFeeRate,
|
|
744
|
-
)
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
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
|