@teleportdao/bitcoin 1.8.9 → 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.
Files changed (55) hide show
  1. package/.tmp/block-parser.ts +58 -0
  2. package/dist/bitcoin-interface-ordinal.d.ts +2 -2
  3. package/dist/bitcoin-interface-ordinal.d.ts.map +1 -1
  4. package/dist/bitcoin-interface-ordinal.js +1 -1
  5. package/dist/bitcoin-interface-ordinal.js.map +1 -1
  6. package/dist/bitcoin-interface-teleswap.d.ts +2 -53
  7. package/dist/bitcoin-interface-teleswap.d.ts.map +1 -1
  8. package/dist/bitcoin-interface-teleswap.js +6 -17
  9. package/dist/bitcoin-interface-teleswap.js.map +1 -1
  10. package/dist/bitcoin-interface-wallet.d.ts +29 -0
  11. package/dist/bitcoin-interface-wallet.d.ts.map +1 -0
  12. package/dist/bitcoin-interface-wallet.js +126 -0
  13. package/dist/bitcoin-interface-wallet.js.map +1 -0
  14. package/dist/bitcoin-interface.d.ts +5 -23
  15. package/dist/bitcoin-interface.d.ts.map +1 -1
  16. package/dist/bitcoin-interface.js +13 -92
  17. package/dist/bitcoin-interface.js.map +1 -1
  18. package/dist/bitcoin-wallet-base.d.ts +55 -31
  19. package/dist/bitcoin-wallet-base.d.ts.map +1 -1
  20. package/dist/bitcoin-wallet-base.js +105 -84
  21. package/dist/bitcoin-wallet-base.js.map +1 -1
  22. package/dist/ordinal-wallet.d.ts +29 -71
  23. package/dist/ordinal-wallet.d.ts.map +1 -1
  24. package/dist/ordinal-wallet.js +48 -108
  25. package/dist/ordinal-wallet.js.map +1 -1
  26. package/dist/teleswap-wallet.d.ts +10 -16
  27. package/dist/teleswap-wallet.d.ts.map +1 -1
  28. package/dist/teleswap-wallet.js +10 -30
  29. package/dist/teleswap-wallet.js.map +1 -1
  30. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +4 -11
  31. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/bitcoin-transaction-builder.js +2 -23
  33. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  34. package/dist/transaction-builder/ordinal-transaction-builder.d.ts +3 -2
  35. package/dist/transaction-builder/ordinal-transaction-builder.d.ts.map +1 -1
  36. package/dist/transaction-builder/ordinal-transaction-builder.js +1 -6
  37. package/dist/transaction-builder/ordinal-transaction-builder.js.map +1 -1
  38. package/dist/transaction-builder/transaction-builder.d.ts +3 -7
  39. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  40. package/dist/transaction-builder/transaction-builder.js +22 -49
  41. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  42. package/dist/type.d.ts +33 -18
  43. package/dist/type.d.ts.map +1 -1
  44. package/package.json +4 -4
  45. package/src/bitcoin-interface-ordinal.ts +7 -3
  46. package/src/bitcoin-interface-teleswap.ts +7 -22
  47. package/src/bitcoin-interface-wallet.ts +114 -0
  48. package/src/bitcoin-interface.ts +15 -98
  49. package/src/bitcoin-wallet-base.ts +166 -132
  50. package/src/ordinal-wallet.ts +73 -162
  51. package/src/teleswap-wallet.ts +50 -72
  52. package/src/transaction-builder/bitcoin-transaction-builder.ts +6 -24
  53. package/src/transaction-builder/ordinal-transaction-builder.ts +2 -10
  54. package/src/transaction-builder/transaction-builder.ts +34 -50
  55. package/src/type.ts +45 -19
@@ -1,44 +1,26 @@
1
1
  import * as bitcoin from "bitcoinjs-lib"
2
2
  import { BaseTransactionBuilder } from "./transaction-builder"
3
+ import { BitcoinInterfaceWallet } from "../bitcoin-interface-wallet"
3
4
  import { BitcoinInterface } from "../bitcoin-interface"
4
- import { BitcoinConnectionInfo } from "../type"
5
+ import { BitcoinInterfaceConnectionInfo } from "../type"
5
6
 
6
7
  export class BitcoinTransactionBuilder extends BaseTransactionBuilder {
7
- btcInterface: BitcoinInterface
8
+ btcInterface: BitcoinInterfaceWallet
8
9
  constructor(
9
- connectionInfo: BitcoinConnectionInfo,
10
10
  networkName: string,
11
11
  network = bitcoin.networks.bitcoin,
12
+ connectionInfo?: BitcoinInterfaceConnectionInfo,
12
13
  ) {
13
14
  super({
14
15
  network,
15
16
  testnet: networkName?.includes("_testnet"),
16
17
  dustLimit: 1000,
17
18
  })
18
- this.btcInterface = new BitcoinInterface(connectionInfo, networkName)
19
- }
20
-
21
- async _getUtxo(userAddress: string) {
22
- let utxos = await this.btcInterface.getUtxo(userAddress)
23
- return utxos.map((tx) => ({
24
- hash: tx.txId as string,
25
- value: +tx.value,
26
- index: +tx.index,
27
- }))
28
- }
29
-
30
- async _getFeeRate(speed: "normal" | "slow" | "fast" = "normal") {
31
- return this.btcInterface.getFeeRate(speed)
19
+ this.btcInterface = new BitcoinInterface(networkName, connectionInfo)
32
20
  }
33
21
 
22
+ // eslint-disable-next-line no-underscore-dangle
34
23
  async _getTransactionHex(transactionId: string): Promise<string> {
35
24
  return this.btcInterface.getRawTransaction(transactionId)
36
25
  }
37
-
38
- async sendTx(txHex: string): Promise<string> {
39
- let txId = await (
40
- this.btcInterface.rpcProvider || this.btcInterface.apiProvider
41
- ).sendRawTransaction(txHex)
42
- return txId
43
- }
44
26
  }
@@ -1,8 +1,8 @@
1
- import { ExtendedUtxo, Target } from "./transaction-builder"
2
1
  import * as bitcoin from "bitcoinjs-lib"
3
2
  import { LEAF_VERSION_TAPSCRIPT } from "bitcoinjs-lib/src/payments/bip341"
4
3
  import { BitcoinTransactionBuilder } from "./bitcoin-transaction-builder"
5
4
  import { getOrdinalScript, toXOnly } from "../helper/ordinal-helper"
5
+ import { ExtendedUtxo, Target } from "./transaction-builder"
6
6
 
7
7
  export class OrdinalTransactionBuilder extends BitcoinTransactionBuilder {
8
8
  async createNftPsbt({
@@ -133,15 +133,7 @@ export class OrdinalTransactionBuilder extends BitcoinTransactionBuilder {
133
133
  },
134
134
  ],
135
135
  fee: inscribeDeposit.value - ordinalAmount,
136
+ possibleTxId: this.getUnsignedPsbtTxId(psbt.toBase64()),
136
137
  }
137
138
  }
138
-
139
- // use with caution. just segwit address
140
- getUnsignedPsbtTxId(unsignedPsbt: string): string {
141
- // use with caution
142
- let psbt = bitcoin.Psbt.fromBase64(unsignedPsbt, {
143
- network: this.network,
144
- })
145
- return (psbt as any).__CACHE.__TX.getId()
146
- }
147
139
  }
@@ -161,13 +161,6 @@ export class BaseTransactionBuilder {
161
161
  this.dustLimit = dustLimit || 1 * 2 * componentBytes.bytePerInput.p2pkh
162
162
  }
163
163
 
164
- // eslint-disable-next-line no-unused-vars, class-methods-use-this
165
- async _getUtxo(userAddress: string): Promise<Utxo[]> {
166
- // The child has implemented this method.
167
- throw new Error("Do not call abstract method directly")
168
- // return utxo
169
- }
170
-
171
164
  // eslint-disable-next-line no-unused-vars, class-methods-use-this
172
165
  async _getTransactionHex(transactionId: string): Promise<string> {
173
166
  // The child has implemented this method.
@@ -202,18 +195,6 @@ export class BaseTransactionBuilder {
202
195
  }
203
196
  }
204
197
 
205
- async getExtendedUtxo(signerInfo: SignerInfo) {
206
- let utxo = await this._getUtxo(signerInfo.address)
207
- const extendedUtxo = utxo.map((input) => ({
208
- ...input,
209
- signerInfo,
210
- }))
211
- if (!extendedUtxo || extendedUtxo.length === 0) {
212
- return []
213
- }
214
- return extendedUtxo
215
- }
216
-
217
198
  calculateTxSize(
218
199
  inputTypes: string[],
219
200
  outputs: {
@@ -454,8 +435,7 @@ export class BaseTransactionBuilder {
454
435
  let inputs: (BitcoinJSInputInfo & {
455
436
  signerInfo: SignerInfo
456
437
  })[] = baseInputs
457
- let transactionId: string | null = null
458
- let transactionHex: string | null = null
438
+ const transactionHex: { [key: string]: string } = {}
459
439
  for (let i in inputs) {
460
440
  let { address, publicKey, derivationPath, masterFingerprint, addressType } =
461
441
  inputs[i].signerInfo
@@ -474,14 +454,10 @@ export class BaseTransactionBuilder {
474
454
  ]
475
455
  }
476
456
  if (addressType === "p2pkh") {
477
- // add p2pkh data
478
- if (transactionHex && transactionId === inputs[i].hash) {
479
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
480
- } else {
481
- transactionHex = await this._getTransactionHex(inputs[i].hash)
482
- transactionId = inputs[i].hash
483
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
484
- }
457
+ const txHex =
458
+ transactionHex[inputs[i].hash] || (await this._getTransactionHex(inputs[i].hash))
459
+ transactionHex[inputs[i].hash] = txHex
460
+ inputs[i].nonWitnessUtxo = Buffer.from(txHex, "hex")
485
461
  } else if (addressType === "p2wpkh") {
486
462
  // add p2wpkh data
487
463
  if (!addressObject.output) throw new Error("invalid signer info")
@@ -491,13 +467,10 @@ export class BaseTransactionBuilder {
491
467
  }
492
468
 
493
469
  if (inputs[i].signerInfo.includeHex) {
494
- if (transactionHex && transactionId === inputs[i].hash) {
495
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
496
- } else {
497
- transactionHex = await this._getTransactionHex(inputs[i].hash)
498
- transactionId = inputs[i].hash
499
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
500
- }
470
+ const txHex =
471
+ transactionHex[inputs[i].hash] || (await this._getTransactionHex(inputs[i].hash))
472
+ transactionHex[inputs[i].hash] = txHex
473
+ inputs[i].nonWitnessUtxo = Buffer.from(txHex, "hex")
501
474
  }
502
475
  } else if (addressType === "p2sh-p2wpkh") {
503
476
  // add p2sh-p2wpkh data
@@ -510,13 +483,10 @@ export class BaseTransactionBuilder {
510
483
  inputs[i].redeemScript = addressObject.redeem.output
511
484
 
512
485
  if (inputs[i].signerInfo.includeHex) {
513
- if (transactionHex && transactionId === inputs[i].hash) {
514
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
515
- } else {
516
- transactionHex = await this._getTransactionHex(inputs[i].hash)
517
- transactionId = inputs[i].hash
518
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
519
- }
486
+ const txHex =
487
+ transactionHex[inputs[i].hash] || (await this._getTransactionHex(inputs[i].hash))
488
+ transactionHex[inputs[i].hash] = txHex
489
+ inputs[i].nonWitnessUtxo = Buffer.from(txHex, "hex")
520
490
  }
521
491
  } else if (addressType === "p2tr") {
522
492
  if (!addressObject.output) throw new Error("invalid signer info")
@@ -528,13 +498,10 @@ export class BaseTransactionBuilder {
528
498
  inputs[i].tapInternalKey = addressObject.internalPubkey
529
499
 
530
500
  if (inputs[i].signerInfo.includeHex) {
531
- if (transactionHex && transactionId === inputs[i].hash) {
532
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
533
- } else {
534
- transactionHex = await this._getTransactionHex(inputs[i].hash)
535
- transactionId = inputs[i].hash
536
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
537
- }
501
+ const txHex =
502
+ transactionHex[inputs[i].hash] || (await this._getTransactionHex(inputs[i].hash))
503
+ transactionHex[inputs[i].hash] = txHex
504
+ inputs[i].nonWitnessUtxo = Buffer.from(txHex, "hex")
538
505
  }
539
506
  }
540
507
  }
@@ -649,6 +616,13 @@ export class BaseTransactionBuilder {
649
616
  }
650
617
 
651
618
  const unsignedPsbtBaseText = newPsbt.toBase64()
619
+
620
+ const safeAddressTypeForPossibleTxId = ["p2wpkh", "p2tr"]
621
+ const isPossibleTxId = inputs.reduce(
622
+ (a, b) => a && safeAddressTypeForPossibleTxId.includes(b.signerInfo.addressType),
623
+ true,
624
+ )
625
+
652
626
  return {
653
627
  unsignedTransaction: unsignedPsbtBaseText,
654
628
  outputs,
@@ -660,6 +634,7 @@ export class BaseTransactionBuilder {
660
634
  })),
661
635
  fee,
662
636
  change,
637
+ possibleTxId: isPossibleTxId ? this.getUnsignedPsbtTxId(unsignedPsbtBaseText) : undefined,
663
638
  }
664
639
  }
665
640
 
@@ -703,4 +678,13 @@ export class BaseTransactionBuilder {
703
678
 
704
679
  return unsignedTransaction
705
680
  }
681
+
682
+ // use with caution. just segwit address
683
+ getUnsignedPsbtTxId(unsignedPsbt: string): string {
684
+ // use with caution
685
+ let psbt = bitcoin.Psbt.fromBase64(unsignedPsbt, {
686
+ network: this.network,
687
+ })
688
+ return (psbt as any).__CACHE.__TX.getId()
689
+ }
706
690
  }
package/src/type.ts CHANGED
@@ -24,25 +24,51 @@ export type Transaction = {
24
24
  addressScript: string
25
25
  }
26
26
 
27
- export type BitcoinConnectionInfo = {
28
- api: {
29
- token?: string
30
- // provider: "BlockStream" | "MempoolSpace"
31
- provider: any
32
- }
33
- rpc?: {
34
- enabled?: boolean
35
- url: string
36
- headers?: {
37
- [key: string]: string
38
- }
39
- auth?: {
40
- username: string
41
- password: string
42
- }
27
+ export type UtxoConnectionInfo = {
28
+ token?: string
29
+ provider: "BlockStream" | "MempoolSpace" | "NowNodes" | "Unisat" | "TeleportDao"
30
+ }
31
+
32
+ export type BTCTokenConnectionInfo = {
33
+ token?: string
34
+ provider: "Unisat" | "TeleportDao"
35
+ }
36
+
37
+ export type APIConnectionInfo = {
38
+ provider: "BlockStream" | "MempoolSpace"
39
+ }
40
+
41
+ export type RPCConnectionInfo = {
42
+ url: string
43
+ headers?: {
44
+ [key: string]: string
43
45
  }
44
- utxo?: {
45
- token?: string
46
- provider: "BlockStream" | "NowNodes" | "MempoolSpace" | "Unisat"
46
+ auth?: {
47
+ username: string
48
+ password: string
47
49
  }
48
50
  }
51
+
52
+ export type BitcoinWalletConnectionInfo = {
53
+ utxo?: UtxoConnectionInfo
54
+ // rpc used for getRawTransaction in transaction builder if set (optional)
55
+ rpc?: RPCConnectionInfo
56
+ }
57
+ export type BitcoinWalletInterfaceConnectionInfo = {
58
+ api?: APIConnectionInfo
59
+ // if utxo is not provided, it will use the same provider as the api
60
+ // rpc used for getRawTransaction in transaction builder if set (optional)
61
+ utxo?: UtxoConnectionInfo
62
+ }
63
+
64
+ export type BitcoinInterfaceConnectionInfo = BitcoinWalletInterfaceConnectionInfo & {
65
+ rpc?: RPCConnectionInfo
66
+ }
67
+
68
+ export type BitcoinNodeConnectionInfo = BitcoinWalletInterfaceConnectionInfo & {
69
+ rpc: RPCConnectionInfo
70
+ }
71
+
72
+ export type BitcoinTokenNodeConnectionInfo = BitcoinNodeConnectionInfo & {
73
+ // btcToken: BTCTokenConnectionInfo
74
+ }