@atomiqlabs/lp-lib 16.2.0 → 16.2.1

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 (167) hide show
  1. package/LICENSE +201 -201
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
  3. package/dist/fees/IBtcFeeEstimator.js +2 -2
  4. package/dist/index.d.ts +42 -42
  5. package/dist/index.js +58 -58
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +60 -60
  8. package/dist/plugins/IPlugin.d.ts +144 -144
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +113 -113
  11. package/dist/plugins/PluginManager.js +274 -274
  12. package/dist/prices/BinanceSwapPrice.d.ts +29 -29
  13. package/dist/prices/BinanceSwapPrice.js +79 -79
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +33 -33
  15. package/dist/prices/CoinGeckoSwapPrice.js +51 -51
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +29 -29
  19. package/dist/prices/OKXSwapPrice.js +79 -79
  20. package/dist/storage/IIntermediaryStorage.d.ts +18 -18
  21. package/dist/storage/IIntermediaryStorage.js +2 -2
  22. package/dist/storagemanager/IntermediaryStorageManager.d.ts +19 -19
  23. package/dist/storagemanager/IntermediaryStorageManager.js +111 -111
  24. package/dist/storagemanager/StorageManager.d.ts +13 -13
  25. package/dist/storagemanager/StorageManager.js +64 -64
  26. package/dist/swaps/SwapHandler.d.ts +171 -171
  27. package/dist/swaps/SwapHandler.js +217 -217
  28. package/dist/swaps/SwapHandlerSwap.d.ts +79 -79
  29. package/dist/swaps/SwapHandlerSwap.js +78 -78
  30. package/dist/swaps/assertions/AmountAssertions.d.ts +28 -28
  31. package/dist/swaps/assertions/AmountAssertions.js +74 -74
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +185 -185
  34. package/dist/swaps/assertions/LightningAssertions.d.ts +44 -44
  35. package/dist/swaps/assertions/LightningAssertions.js +86 -86
  36. package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -53
  37. package/dist/swaps/assertions/ToBtcAmountAssertions.js +150 -150
  38. package/dist/swaps/escrow/EscrowHandler.d.ts +50 -50
  39. package/dist/swaps/escrow/EscrowHandler.js +151 -151
  40. package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -35
  41. package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -69
  42. package/dist/swaps/escrow/FromBtcBaseSwap.d.ts +14 -14
  43. package/dist/swaps/escrow/FromBtcBaseSwap.js +32 -32
  44. package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +102 -102
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -210
  46. package/dist/swaps/escrow/ToBtcBaseSwap.d.ts +36 -36
  47. package/dist/swaps/escrow/ToBtcBaseSwap.js +67 -67
  48. package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -53
  49. package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -81
  50. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +84 -84
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +322 -322
  52. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.d.ts +21 -21
  53. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.js +50 -50
  54. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +108 -108
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +695 -695
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -33
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -91
  58. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +112 -112
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +708 -708
  60. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -55
  61. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -120
  62. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +170 -170
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +745 -745
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +28 -28
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +64 -64
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +178 -178
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +900 -899
  68. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  69. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  70. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +44 -44
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +145 -145
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +68 -68
  73. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  74. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  75. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +561 -561
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +63 -63
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +491 -491
  78. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +52 -52
  79. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +662 -662
  80. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  81. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  82. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +77 -77
  83. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +504 -504
  84. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  85. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  86. package/dist/utils/BitcoinUtils.d.ts +4 -4
  87. package/dist/utils/BitcoinUtils.js +61 -61
  88. package/dist/utils/Utils.d.ts +32 -32
  89. package/dist/utils/Utils.js +129 -129
  90. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  91. package/dist/utils/paramcoders/IParamReader.js +2 -2
  92. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  93. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  94. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  95. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  97. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  98. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  99. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  100. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  101. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  102. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  103. package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -107
  104. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  105. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  106. package/dist/wallets/IBitcoinWallet.d.ts +149 -149
  107. package/dist/wallets/IBitcoinWallet.js +97 -97
  108. package/dist/wallets/ILightningWallet.d.ts +136 -136
  109. package/dist/wallets/ILightningWallet.js +37 -37
  110. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  111. package/dist/wallets/ISpvVaultSigner.js +2 -2
  112. package/package.json +36 -36
  113. package/src/fees/IBtcFeeEstimator.ts +6 -6
  114. package/src/index.ts +53 -53
  115. package/src/info/InfoHandler.ts +103 -103
  116. package/src/plugins/IPlugin.ts +174 -174
  117. package/src/plugins/PluginManager.ts +354 -354
  118. package/src/prices/BinanceSwapPrice.ts +101 -101
  119. package/src/prices/CoinGeckoSwapPrice.ts +75 -75
  120. package/src/prices/ISwapPrice.ts +88 -88
  121. package/src/prices/OKXSwapPrice.ts +101 -101
  122. package/src/storage/IIntermediaryStorage.ts +19 -19
  123. package/src/storagemanager/IntermediaryStorageManager.ts +118 -118
  124. package/src/storagemanager/StorageManager.ts +78 -78
  125. package/src/swaps/SwapHandler.ts +323 -323
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +77 -77
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +251 -251
  129. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  130. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  131. package/src/swaps/escrow/EscrowHandler.ts +172 -172
  132. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  133. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  134. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -286
  135. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  136. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  137. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +457 -457
  138. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  139. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +873 -873
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +866 -866
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +920 -920
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +108 -108
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1150 -1149
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +178 -178
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +228 -228
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +718 -718
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +567 -567
  151. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +762 -762
  152. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  153. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +603 -603
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +59 -59
  156. package/src/utils/Utils.ts +150 -150
  157. package/src/utils/paramcoders/IParamReader.ts +7 -7
  158. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  159. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  160. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  161. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  162. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  163. package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -118
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +237 -237
  166. package/src/wallets/ILightningWallet.ts +200 -200
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
@@ -1,121 +1,121 @@
1
- import {createHash} from "crypto";
2
- import {deserializeBN, serializeBN} from "../../../utils/Utils";
3
- import {SwapHandlerSwap} from "../../SwapHandlerSwap";
4
- import {SwapHandlerType} from "../../SwapHandler";
5
-
6
- export enum FromBtcLnTrustedSwapState {
7
- REFUNDED = -2,
8
- CANCELED = -1,
9
- CREATED = 0,
10
- RECEIVED = 1,
11
- SENT = 2,
12
- CONFIRMED = 3,
13
- SETTLED = 4,
14
- }
15
-
16
- export class FromBtcLnTrustedSwap extends SwapHandlerSwap<FromBtcLnTrustedSwapState> {
17
-
18
- readonly pr: string;
19
- amount: bigint;
20
- readonly output: bigint;
21
- readonly dstAddress: string;
22
- readonly secret: string;
23
- readonly token: string;
24
-
25
- scRawTx: string;
26
-
27
- constructor(
28
- chainIdentifier: string,
29
- pr: string,
30
- inputMtokens: bigint,
31
- swapFee: bigint,
32
- swapFeeInToken: bigint,
33
- output: bigint,
34
- secret: string,
35
- dstAddress: string,
36
- token: string
37
- );
38
- constructor(obj: any);
39
-
40
- constructor(
41
- chainIdOrObj: string | any,
42
- pr?: string,
43
- inputMtokens?: bigint,
44
- swapFee?: bigint,
45
- swapFeeInToken?: bigint,
46
- output?: bigint,
47
- secret?: string,
48
- dstAddress?: string,
49
- token?: string
50
- ) {
51
- if(typeof(chainIdOrObj)==="string") {
52
- super(chainIdOrObj, swapFee, swapFeeInToken);
53
- this.state = FromBtcLnTrustedSwapState.CREATED;
54
- this.pr = pr;
55
- this.amount = (inputMtokens + 999n) / 1000n;
56
- this.output = output;
57
- this.secret = secret;
58
- this.dstAddress = dstAddress;
59
- this.token = token;
60
- } else {
61
- super(chainIdOrObj);
62
- this.pr = chainIdOrObj.pr;
63
- this.amount = deserializeBN(chainIdOrObj.amount);
64
- this.output = deserializeBN(chainIdOrObj.output);
65
- this.secret = chainIdOrObj.secret;
66
- this.dstAddress = chainIdOrObj.dstAddress;
67
- this.token = chainIdOrObj.token;
68
- this.scRawTx = chainIdOrObj.scRawTx;
69
- }
70
- this.type = SwapHandlerType.FROM_BTCLN_TRUSTED;
71
- }
72
-
73
- getToken(): string {
74
- return this.token;
75
- }
76
-
77
- getOutputAmount(): bigint {
78
- return this.output;
79
- }
80
-
81
- getSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
82
- return {inInputToken: this.swapFee, inOutputToken: this.swapFeeInToken};
83
- }
84
-
85
- getTotalInputAmount(): bigint {
86
- return this.amount;
87
- }
88
-
89
- getSequence(): bigint {
90
- return 0n;
91
- }
92
-
93
- serialize(): any {
94
- const partialSerialized = super.serialize();
95
- partialSerialized.pr = this.pr;
96
- partialSerialized.amount = serializeBN(this.amount);
97
- partialSerialized.output = serializeBN(this.output);
98
- partialSerialized.secret = this.secret;
99
- partialSerialized.dstAddress = this.dstAddress;
100
- partialSerialized.token = this.token;
101
- partialSerialized.scRawTx = this.scRawTx;
102
- return partialSerialized;
103
- }
104
-
105
- isFailed(): boolean {
106
- return this.state===FromBtcLnTrustedSwapState.CANCELED || this.state===FromBtcLnTrustedSwapState.REFUNDED;
107
- }
108
-
109
- isInitiated(): boolean {
110
- return this.state!==FromBtcLnTrustedSwapState.CREATED;
111
- }
112
-
113
- isSuccess(): boolean {
114
- return this.state===FromBtcLnTrustedSwapState.SETTLED;
115
- }
116
-
117
- getIdentifierHash(): string {
118
- return createHash("sha256").update(Buffer.from(this.secret, "hex")).digest().toString("hex");
119
- }
120
-
121
- }
1
+ import {createHash} from "crypto";
2
+ import {deserializeBN, serializeBN} from "../../../utils/Utils";
3
+ import {SwapHandlerSwap} from "../../SwapHandlerSwap";
4
+ import {SwapHandlerType} from "../../SwapHandler";
5
+
6
+ export enum FromBtcLnTrustedSwapState {
7
+ REFUNDED = -2,
8
+ CANCELED = -1,
9
+ CREATED = 0,
10
+ RECEIVED = 1,
11
+ SENT = 2,
12
+ CONFIRMED = 3,
13
+ SETTLED = 4,
14
+ }
15
+
16
+ export class FromBtcLnTrustedSwap extends SwapHandlerSwap<FromBtcLnTrustedSwapState> {
17
+
18
+ readonly pr: string;
19
+ amount: bigint;
20
+ readonly output: bigint;
21
+ readonly dstAddress: string;
22
+ readonly secret: string;
23
+ readonly token: string;
24
+
25
+ scRawTx: string;
26
+
27
+ constructor(
28
+ chainIdentifier: string,
29
+ pr: string,
30
+ inputMtokens: bigint,
31
+ swapFee: bigint,
32
+ swapFeeInToken: bigint,
33
+ output: bigint,
34
+ secret: string,
35
+ dstAddress: string,
36
+ token: string
37
+ );
38
+ constructor(obj: any);
39
+
40
+ constructor(
41
+ chainIdOrObj: string | any,
42
+ pr?: string,
43
+ inputMtokens?: bigint,
44
+ swapFee?: bigint,
45
+ swapFeeInToken?: bigint,
46
+ output?: bigint,
47
+ secret?: string,
48
+ dstAddress?: string,
49
+ token?: string
50
+ ) {
51
+ if(typeof(chainIdOrObj)==="string") {
52
+ super(chainIdOrObj, swapFee, swapFeeInToken);
53
+ this.state = FromBtcLnTrustedSwapState.CREATED;
54
+ this.pr = pr;
55
+ this.amount = (inputMtokens + 999n) / 1000n;
56
+ this.output = output;
57
+ this.secret = secret;
58
+ this.dstAddress = dstAddress;
59
+ this.token = token;
60
+ } else {
61
+ super(chainIdOrObj);
62
+ this.pr = chainIdOrObj.pr;
63
+ this.amount = deserializeBN(chainIdOrObj.amount);
64
+ this.output = deserializeBN(chainIdOrObj.output);
65
+ this.secret = chainIdOrObj.secret;
66
+ this.dstAddress = chainIdOrObj.dstAddress;
67
+ this.token = chainIdOrObj.token;
68
+ this.scRawTx = chainIdOrObj.scRawTx;
69
+ }
70
+ this.type = SwapHandlerType.FROM_BTCLN_TRUSTED;
71
+ }
72
+
73
+ getToken(): string {
74
+ return this.token;
75
+ }
76
+
77
+ getOutputAmount(): bigint {
78
+ return this.output;
79
+ }
80
+
81
+ getSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
82
+ return {inInputToken: this.swapFee, inOutputToken: this.swapFeeInToken};
83
+ }
84
+
85
+ getTotalInputAmount(): bigint {
86
+ return this.amount;
87
+ }
88
+
89
+ getSequence(): bigint {
90
+ return 0n;
91
+ }
92
+
93
+ serialize(): any {
94
+ const partialSerialized = super.serialize();
95
+ partialSerialized.pr = this.pr;
96
+ partialSerialized.amount = serializeBN(this.amount);
97
+ partialSerialized.output = serializeBN(this.output);
98
+ partialSerialized.secret = this.secret;
99
+ partialSerialized.dstAddress = this.dstAddress;
100
+ partialSerialized.token = this.token;
101
+ partialSerialized.scRawTx = this.scRawTx;
102
+ return partialSerialized;
103
+ }
104
+
105
+ isFailed(): boolean {
106
+ return this.state===FromBtcLnTrustedSwapState.CANCELED || this.state===FromBtcLnTrustedSwapState.REFUNDED;
107
+ }
108
+
109
+ isInitiated(): boolean {
110
+ return this.state!==FromBtcLnTrustedSwapState.CREATED;
111
+ }
112
+
113
+ isSuccess(): boolean {
114
+ return this.state===FromBtcLnTrustedSwapState.SETTLED;
115
+ }
116
+
117
+ getIdentifierHash(): string {
118
+ return createHash("sha256").update(Buffer.from(this.secret, "hex")).digest().toString("hex");
119
+ }
120
+
121
+ }
@@ -1,59 +1,59 @@
1
- import {TransactionInput} from "@scure/btc-signer/psbt";
2
- import {getPrevOut} from "@scure/btc-signer/utxo";
3
- import {OutScript} from "@scure/btc-signer";
4
- import {BitcoinRpc, BtcTx} from "@atomiqlabs/base";
5
- import {IBitcoinWallet} from "../wallets/IBitcoinWallet";
6
- import {getLogger} from "./Utils";
7
-
8
- const logger = getLogger("BitcoinUtils: ");
9
-
10
- function parsePushOpcode(script: Uint8Array): Uint8Array {
11
- if(script[0]===0x00) {
12
- return Uint8Array.from([]);
13
- }
14
- if(script[0]<=0x4b) {
15
- return script.slice(1, 1+script[0]);
16
- }
17
- if(script[0]===0x4c) {
18
- return script.slice(2, 2+script[1]);
19
- }
20
- if(script[0]===0x4d) {
21
- const length = Buffer.from(script.slice(1, 3)).readUInt16LE();
22
- return script.slice(3, 3+length);
23
- }
24
- if(script[0]===0x4e) {
25
- const length = Buffer.from(script.slice(1, 5)).readUInt32LE();
26
- return script.slice(5, 5+length);
27
- }
28
- if(script[0]===0x4f) {
29
- return Uint8Array.from([0x81]);
30
- }
31
- if(script[0]>=0x51 && script[0]<=0x60) {
32
- return Uint8Array.from([script[0] - 0x50]);
33
- }
34
- throw new Error("No push opcode detected");
35
- }
36
-
37
- export function isLegacyInput(input: TransactionInput): boolean {
38
- const prevOut = getPrevOut(input);
39
- const first = OutScript.decode(prevOut.script);
40
- if(first.type==="tr" || first.type==="wsh" || first.type==="wpkh") return false;
41
- if(first.type==="sh") {
42
- const redeemScript = input.redeemScript ?? parsePushOpcode(input.finalScriptSig);
43
- const second = OutScript.decode(redeemScript);
44
- if(second.type==="wsh" || second.type==="wpkh") return false;
45
- }
46
- return true;
47
- }
48
-
49
- export async function checkTransactionReplaced(txId: string, txRaw: string, bitcoin: BitcoinRpc<any>): Promise<BtcTx> {
50
- const existingTx = await bitcoin.getTransaction(txId);
51
- if(existingTx!=null) return existingTx;
52
- //Try to re-broadcast
53
- try {
54
- await bitcoin.sendRawTransaction(txRaw);
55
- } catch (e) {
56
- logger.error("checkTransactionReplaced("+txId+"): Error when trying to re-broadcast raw transaction: ", e);
57
- }
58
- return await bitcoin.getTransaction(txId);
59
- }
1
+ import {TransactionInput} from "@scure/btc-signer/psbt";
2
+ import {getPrevOut} from "@scure/btc-signer/utxo";
3
+ import {OutScript} from "@scure/btc-signer";
4
+ import {BitcoinRpc, BtcTx} from "@atomiqlabs/base";
5
+ import {IBitcoinWallet} from "../wallets/IBitcoinWallet";
6
+ import {getLogger} from "./Utils";
7
+
8
+ const logger = getLogger("BitcoinUtils: ");
9
+
10
+ function parsePushOpcode(script: Uint8Array): Uint8Array {
11
+ if(script[0]===0x00) {
12
+ return Uint8Array.from([]);
13
+ }
14
+ if(script[0]<=0x4b) {
15
+ return script.slice(1, 1+script[0]);
16
+ }
17
+ if(script[0]===0x4c) {
18
+ return script.slice(2, 2+script[1]);
19
+ }
20
+ if(script[0]===0x4d) {
21
+ const length = Buffer.from(script.slice(1, 3)).readUInt16LE();
22
+ return script.slice(3, 3+length);
23
+ }
24
+ if(script[0]===0x4e) {
25
+ const length = Buffer.from(script.slice(1, 5)).readUInt32LE();
26
+ return script.slice(5, 5+length);
27
+ }
28
+ if(script[0]===0x4f) {
29
+ return Uint8Array.from([0x81]);
30
+ }
31
+ if(script[0]>=0x51 && script[0]<=0x60) {
32
+ return Uint8Array.from([script[0] - 0x50]);
33
+ }
34
+ throw new Error("No push opcode detected");
35
+ }
36
+
37
+ export function isLegacyInput(input: TransactionInput): boolean {
38
+ const prevOut = getPrevOut(input);
39
+ const first = OutScript.decode(prevOut.script);
40
+ if(first.type==="tr" || first.type==="wsh" || first.type==="wpkh") return false;
41
+ if(first.type==="sh") {
42
+ const redeemScript = input.redeemScript ?? parsePushOpcode(input.finalScriptSig);
43
+ const second = OutScript.decode(redeemScript);
44
+ if(second.type==="wsh" || second.type==="wpkh") return false;
45
+ }
46
+ return true;
47
+ }
48
+
49
+ export async function checkTransactionReplaced(txId: string, txRaw: string, bitcoin: BitcoinRpc<any>): Promise<BtcTx> {
50
+ const existingTx = await bitcoin.getTransaction(txId);
51
+ if(existingTx!=null) return existingTx;
52
+ //Try to re-broadcast
53
+ try {
54
+ await bitcoin.sendRawTransaction(txRaw);
55
+ } catch (e) {
56
+ logger.error("checkTransactionReplaced("+txId+"): Error when trying to re-broadcast raw transaction: ", e);
57
+ }
58
+ return await bitcoin.getTransaction(txId);
59
+ }
@@ -1,150 +1,150 @@
1
- import {Request, Response} from "express";
2
- import {ServerParamEncoder} from "./paramcoders/server/ServerParamEncoder";
3
- import {createHash} from "crypto";
4
- import {Script, Transaction} from "@scure/btc-signer";
5
- import {BtcTx} from "@atomiqlabs/base";
6
-
7
- export type LoggerType = {
8
- debug: (msg: string, ...args: any[]) => void,
9
- info: (msg: string, ...args: any[]) => void,
10
- warn: (msg: string, ...args: any[]) => void,
11
- error: (msg: string, ...args: any[]) => void
12
- };
13
-
14
- export function getLogger(prefix: string | (() => string)): LoggerType {
15
- return {
16
- debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
17
- info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
18
- warn: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 1) && console.warn((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
19
- error: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 0) && console.error((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args)
20
- };
21
- }
22
-
23
- export type DefinedRuntimeError = {
24
- code: number;
25
- msg?: string;
26
- _httpStatus?: number;
27
- data?: any;
28
- };
29
-
30
- export function isDefinedRuntimeError(obj: any): obj is DefinedRuntimeError {
31
- if(obj.code!=null && typeof(obj.code)==="number") {
32
- if(obj.msg!=null && typeof(obj.msg)!=="string") return false;
33
- if(obj._httpStatus!=null && typeof(obj._httpStatus)!=="number") return false;
34
- return true;
35
- }
36
- return false;
37
- }
38
-
39
- const expressHandlerWrapperLogger = getLogger("ExpressHandlerWrapper: ");
40
-
41
- export function expressHandlerWrapper(func: (
42
- req: Request,
43
- res: Response
44
- ) => Promise<void>) : ((
45
- req: Request,
46
- res: Response & {responseStream: ServerParamEncoder}
47
- ) => void) {
48
- return (
49
- req: Request,
50
- res: Response & {responseStream: ServerParamEncoder}
51
- ) => {
52
- (async () => {
53
- try {
54
- await func(req, res);
55
- } catch (e) {
56
- expressHandlerWrapperLogger.error("Error in called function "+req.path+": ", e);
57
- let statusCode = 500;
58
- const obj: {code: number, msg: string, data?: any} = {
59
- code: 0,
60
- msg: "Internal server error"
61
- };
62
- if(isDefinedRuntimeError(e)) {
63
- obj.msg = e.msg;
64
- obj.code = e.code;
65
- obj.data = e.data;
66
- statusCode = 400;
67
- if(e._httpStatus!=null) statusCode = e._httpStatus;
68
- }
69
- if(res.responseStream!=null) {
70
- if(res.responseStream.getAbortSignal().aborted) return;
71
- res.responseStream.writeParamsAndEnd(obj).catch(e => null);
72
- } else {
73
- res.status(statusCode).json(obj);
74
- }
75
- }
76
- })();
77
- }
78
- }
79
-
80
- export const HEX_REGEX = /[0-9a-fA-F]+/;
81
-
82
- export function serializeBN(bn: bigint | null): string | null {
83
- return bn==null ? null : bn.toString(10);
84
- }
85
-
86
- export function deserializeBN(str: string | null): bigint | null {
87
- return str==null ? null : BigInt(str);
88
- }
89
-
90
- export function bigIntSorter(a: bigint, b: bigint): -1 | 0 | 1 {
91
- if(a < b) return -1;
92
- if(a > b) return 1;
93
- return 0;
94
- }
95
-
96
- /**
97
- * Creates an abort controller that extends the responseStream's abort signal
98
- *
99
- * @param responseStream
100
- */
101
- export function getAbortController(responseStream: ServerParamEncoder): AbortController {
102
- const abortController = new AbortController();
103
- if(responseStream==null || responseStream.getAbortSignal==null) return abortController;
104
- const responseStreamAbortController = responseStream.getAbortSignal();
105
- responseStreamAbortController.addEventListener("abort", () => abortController.abort(responseStreamAbortController.reason));
106
- return abortController;
107
- }
108
-
109
- export function parsePsbt(btcTx: Transaction): BtcTx {
110
- const txWithoutWitness = btcTx.toBytes(true, false);
111
- return {
112
- locktime: btcTx.lockTime,
113
- version: btcTx.version,
114
- blockhash: null,
115
- confirmations: 0,
116
- txid: createHash("sha256").update(
117
- createHash("sha256").update(
118
- txWithoutWitness
119
- ).digest()
120
- ).digest().reverse().toString("hex"),
121
- hex: Buffer.from(txWithoutWitness).toString("hex"),
122
- raw: Buffer.from(btcTx.toBytes(true, true)).toString("hex"),
123
- vsize: btcTx.isFinal ? btcTx.vsize : null,
124
-
125
- outs: Array.from({length: btcTx.outputsLength}, (_, i) => i).map((index) => {
126
- const output = btcTx.getOutput(index);
127
- return {
128
- value: Number(output.amount),
129
- n: index,
130
- scriptPubKey: {
131
- asm: Script.decode(output.script).map(val => typeof(val)==="object" ? Buffer.from(val).toString("hex") : val.toString()).join(" "),
132
- hex: Buffer.from(output.script).toString("hex")
133
- }
134
- }
135
- }),
136
- ins: Array.from({length: btcTx.inputsLength}, (_, i) => i).map(index => {
137
- const input = btcTx.getInput(index);
138
- return {
139
- txid: Buffer.from(input.txid).toString("hex"),
140
- vout: input.index,
141
- scriptSig: {
142
- asm: Script.decode(input.finalScriptSig).map(val => typeof(val)==="object" ? Buffer.from(val).toString("hex") : val.toString()).join(" "),
143
- hex: Buffer.from(input.finalScriptSig).toString("hex")
144
- },
145
- sequence: input.sequence,
146
- txinwitness: input.finalScriptWitness==null ? [] : input.finalScriptWitness.map(witness => Buffer.from(witness).toString("hex"))
147
- }
148
- })
149
- };
150
- }
1
+ import {Request, Response} from "express";
2
+ import {ServerParamEncoder} from "./paramcoders/server/ServerParamEncoder";
3
+ import {createHash} from "crypto";
4
+ import {Script, Transaction} from "@scure/btc-signer";
5
+ import {BtcTx} from "@atomiqlabs/base";
6
+
7
+ export type LoggerType = {
8
+ debug: (msg: string, ...args: any[]) => void,
9
+ info: (msg: string, ...args: any[]) => void,
10
+ warn: (msg: string, ...args: any[]) => void,
11
+ error: (msg: string, ...args: any[]) => void
12
+ };
13
+
14
+ export function getLogger(prefix: string | (() => string)): LoggerType {
15
+ return {
16
+ debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
17
+ info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
18
+ warn: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 1) && console.warn((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args),
19
+ error: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 0) && console.error((typeof(prefix)==="function" ? prefix() : prefix)+msg, ...args)
20
+ };
21
+ }
22
+
23
+ export type DefinedRuntimeError = {
24
+ code: number;
25
+ msg?: string;
26
+ _httpStatus?: number;
27
+ data?: any;
28
+ };
29
+
30
+ export function isDefinedRuntimeError(obj: any): obj is DefinedRuntimeError {
31
+ if(obj.code!=null && typeof(obj.code)==="number") {
32
+ if(obj.msg!=null && typeof(obj.msg)!=="string") return false;
33
+ if(obj._httpStatus!=null && typeof(obj._httpStatus)!=="number") return false;
34
+ return true;
35
+ }
36
+ return false;
37
+ }
38
+
39
+ const expressHandlerWrapperLogger = getLogger("ExpressHandlerWrapper: ");
40
+
41
+ export function expressHandlerWrapper(func: (
42
+ req: Request,
43
+ res: Response
44
+ ) => Promise<void>) : ((
45
+ req: Request,
46
+ res: Response & {responseStream: ServerParamEncoder}
47
+ ) => void) {
48
+ return (
49
+ req: Request,
50
+ res: Response & {responseStream: ServerParamEncoder}
51
+ ) => {
52
+ (async () => {
53
+ try {
54
+ await func(req, res);
55
+ } catch (e) {
56
+ expressHandlerWrapperLogger.error("Error in called function "+req.path+": ", e);
57
+ let statusCode = 500;
58
+ const obj: {code: number, msg: string, data?: any} = {
59
+ code: 0,
60
+ msg: "Internal server error"
61
+ };
62
+ if(isDefinedRuntimeError(e)) {
63
+ obj.msg = e.msg;
64
+ obj.code = e.code;
65
+ obj.data = e.data;
66
+ statusCode = 400;
67
+ if(e._httpStatus!=null) statusCode = e._httpStatus;
68
+ }
69
+ if(res.responseStream!=null) {
70
+ if(res.responseStream.getAbortSignal().aborted) return;
71
+ res.responseStream.writeParamsAndEnd(obj).catch(e => null);
72
+ } else {
73
+ res.status(statusCode).json(obj);
74
+ }
75
+ }
76
+ })();
77
+ }
78
+ }
79
+
80
+ export const HEX_REGEX = /[0-9a-fA-F]+/;
81
+
82
+ export function serializeBN(bn: bigint | null): string | null {
83
+ return bn==null ? null : bn.toString(10);
84
+ }
85
+
86
+ export function deserializeBN(str: string | null): bigint | null {
87
+ return str==null ? null : BigInt(str);
88
+ }
89
+
90
+ export function bigIntSorter(a: bigint, b: bigint): -1 | 0 | 1 {
91
+ if(a < b) return -1;
92
+ if(a > b) return 1;
93
+ return 0;
94
+ }
95
+
96
+ /**
97
+ * Creates an abort controller that extends the responseStream's abort signal
98
+ *
99
+ * @param responseStream
100
+ */
101
+ export function getAbortController(responseStream: ServerParamEncoder): AbortController {
102
+ const abortController = new AbortController();
103
+ if(responseStream==null || responseStream.getAbortSignal==null) return abortController;
104
+ const responseStreamAbortController = responseStream.getAbortSignal();
105
+ responseStreamAbortController.addEventListener("abort", () => abortController.abort(responseStreamAbortController.reason));
106
+ return abortController;
107
+ }
108
+
109
+ export function parsePsbt(btcTx: Transaction): BtcTx {
110
+ const txWithoutWitness = btcTx.toBytes(true, false);
111
+ return {
112
+ locktime: btcTx.lockTime,
113
+ version: btcTx.version,
114
+ blockhash: null,
115
+ confirmations: 0,
116
+ txid: createHash("sha256").update(
117
+ createHash("sha256").update(
118
+ txWithoutWitness
119
+ ).digest()
120
+ ).digest().reverse().toString("hex"),
121
+ hex: Buffer.from(txWithoutWitness).toString("hex"),
122
+ raw: Buffer.from(btcTx.toBytes(true, true)).toString("hex"),
123
+ vsize: btcTx.isFinal ? btcTx.vsize : null,
124
+
125
+ outs: Array.from({length: btcTx.outputsLength}, (_, i) => i).map((index) => {
126
+ const output = btcTx.getOutput(index);
127
+ return {
128
+ value: Number(output.amount),
129
+ n: index,
130
+ scriptPubKey: {
131
+ asm: Script.decode(output.script).map(val => typeof(val)==="object" ? Buffer.from(val).toString("hex") : val.toString()).join(" "),
132
+ hex: Buffer.from(output.script).toString("hex")
133
+ }
134
+ }
135
+ }),
136
+ ins: Array.from({length: btcTx.inputsLength}, (_, i) => i).map(index => {
137
+ const input = btcTx.getInput(index);
138
+ return {
139
+ txid: Buffer.from(input.txid).toString("hex"),
140
+ vout: input.index,
141
+ scriptSig: {
142
+ asm: Script.decode(input.finalScriptSig).map(val => typeof(val)==="object" ? Buffer.from(val).toString("hex") : val.toString()).join(" "),
143
+ hex: Buffer.from(input.finalScriptSig).toString("hex")
144
+ },
145
+ sequence: input.sequence,
146
+ txinwitness: input.finalScriptWitness==null ? [] : input.finalScriptWitness.map(witness => Buffer.from(witness).toString("hex"))
147
+ }
148
+ })
149
+ };
150
+ }