@atomiqlabs/lp-lib 15.0.13 → 15.0.14

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 (163) 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 +40 -40
  5. package/dist/index.js +56 -56
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +58 -61
  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 +26 -26
  13. package/dist/prices/BinanceSwapPrice.js +92 -92
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +30 -30
  15. package/dist/prices/CoinGeckoSwapPrice.js +64 -64
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +26 -26
  19. package/dist/prices/OKXSwapPrice.js +92 -92
  20. package/dist/storage/IIntermediaryStorage.d.ts +18 -18
  21. package/dist/storage/IIntermediaryStorage.js +2 -2
  22. package/dist/storagemanager/IntermediaryStorageManager.d.ts +18 -18
  23. package/dist/storagemanager/IntermediaryStorageManager.js +104 -104
  24. package/dist/storagemanager/StorageManager.d.ts +12 -12
  25. package/dist/storagemanager/StorageManager.js +57 -57
  26. package/dist/swaps/SwapHandler.d.ts +153 -156
  27. package/dist/swaps/SwapHandler.js +157 -163
  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 +172 -172
  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 +51 -51
  39. package/dist/swaps/escrow/EscrowHandler.js +158 -158
  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 +101 -101
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +207 -207
  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 +83 -83
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +318 -318
  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 +107 -107
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +673 -675
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +32 -32
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +88 -88
  58. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +171 -171
  59. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +718 -718
  60. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +28 -28
  61. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +64 -64
  62. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +177 -177
  63. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +863 -863
  64. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  65. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  66. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +45 -45
  67. package/dist/swaps/spv_vault_swap/SpvVault.js +145 -145
  68. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +68 -68
  69. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  70. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  71. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +528 -528
  72. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +68 -68
  73. package/dist/swaps/spv_vault_swap/SpvVaults.js +454 -454
  74. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +51 -51
  75. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +650 -650
  76. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  77. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  78. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +76 -76
  79. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +493 -495
  80. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  81. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  82. package/dist/utils/BitcoinUtils.d.ts +4 -4
  83. package/dist/utils/BitcoinUtils.js +61 -61
  84. package/dist/utils/Utils.d.ts +29 -29
  85. package/dist/utils/Utils.js +88 -88
  86. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  87. package/dist/utils/paramcoders/IParamReader.js +2 -2
  88. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  89. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  90. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  91. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  92. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  93. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  94. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  95. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  97. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  98. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  99. package/dist/utils/paramcoders/server/ServerParamDecoder.js +105 -105
  100. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  101. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  102. package/dist/wallets/IBitcoinWallet.d.ts +67 -67
  103. package/dist/wallets/IBitcoinWallet.js +2 -2
  104. package/dist/wallets/ILightningWallet.d.ts +117 -117
  105. package/dist/wallets/ILightningWallet.js +37 -37
  106. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  107. package/dist/wallets/ISpvVaultSigner.js +2 -2
  108. package/dist/wallets/ISpvVaultWallet.d.ts +42 -42
  109. package/dist/wallets/ISpvVaultWallet.js +2 -2
  110. package/package.json +36 -36
  111. package/src/fees/IBtcFeeEstimator.ts +6 -6
  112. package/src/index.ts +51 -51
  113. package/src/info/InfoHandler.ts +100 -106
  114. package/src/plugins/IPlugin.ts +174 -174
  115. package/src/plugins/PluginManager.ts +354 -354
  116. package/src/prices/BinanceSwapPrice.ts +113 -113
  117. package/src/prices/CoinGeckoSwapPrice.ts +87 -87
  118. package/src/prices/ISwapPrice.ts +88 -88
  119. package/src/prices/OKXSwapPrice.ts +113 -113
  120. package/src/storage/IIntermediaryStorage.ts +19 -19
  121. package/src/storagemanager/IntermediaryStorageManager.ts +109 -109
  122. package/src/storagemanager/StorageManager.ts +68 -68
  123. package/src/swaps/SwapHandler.ts +272 -280
  124. package/src/swaps/SwapHandlerSwap.ts +141 -141
  125. package/src/swaps/assertions/AmountAssertions.ts +77 -77
  126. package/src/swaps/assertions/FromBtcAmountAssertions.ts +238 -238
  127. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  128. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  129. package/src/swaps/escrow/EscrowHandler.ts +179 -179
  130. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  131. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  132. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +283 -283
  133. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  134. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  135. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +452 -452
  136. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  137. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +855 -856
  138. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +137 -137
  139. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +890 -890
  140. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +108 -108
  141. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1112 -1112
  142. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  143. package/src/swaps/spv_vault_swap/SpvVault.ts +178 -178
  144. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +228 -228
  145. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +671 -671
  146. package/src/swaps/spv_vault_swap/SpvVaults.ts +526 -526
  147. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +747 -747
  148. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  149. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +591 -592
  150. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  151. package/src/utils/BitcoinUtils.ts +59 -59
  152. package/src/utils/Utils.ts +102 -102
  153. package/src/utils/paramcoders/IParamReader.ts +7 -7
  154. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  155. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  156. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  157. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  158. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  159. package/src/utils/paramcoders/server/ServerParamDecoder.ts +115 -115
  160. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  161. package/src/wallets/IBitcoinWallet.ts +68 -68
  162. package/src/wallets/ILightningWallet.ts +178 -178
  163. 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,102 +1,102 @@
1
- import {Request, Response} from "express";
2
- import {ServerParamEncoder} from "./paramcoders/server/ServerParamEncoder";
3
-
4
- export type DefinedRuntimeError = {
5
- code: number;
6
- msg?: string;
7
- _httpStatus?: number;
8
- data?: any;
9
- };
10
-
11
- export function isDefinedRuntimeError(obj: any): obj is DefinedRuntimeError {
12
- if(obj.code!=null && typeof(obj.code)==="number") {
13
- if(obj.msg!=null && typeof(obj.msg)!=="string") return false;
14
- if(obj._httpStatus!=null && typeof(obj._httpStatus)!=="number") return false;
15
- return true;
16
- }
17
- return false;
18
- }
19
-
20
- export function expressHandlerWrapper(func: (
21
- req: Request,
22
- res: Response
23
- ) => Promise<void>) : ((
24
- req: Request,
25
- res: Response & {responseStream: ServerParamEncoder}
26
- ) => void) {
27
- return (
28
- req: Request,
29
- res: Response & {responseStream: ServerParamEncoder}
30
- ) => {
31
- (async () => {
32
- try {
33
- await func(req, res);
34
- } catch (e) {
35
- console.error(e);
36
- let statusCode = 500;
37
- const obj: {code: number, msg: string, data?: any} = {
38
- code: 0,
39
- msg: "Internal server error"
40
- };
41
- if(isDefinedRuntimeError(e)) {
42
- obj.msg = e.msg;
43
- obj.code = e.code;
44
- obj.data = e.data;
45
- statusCode = 400;
46
- if(e._httpStatus!=null) statusCode = e._httpStatus;
47
- }
48
- if(res.responseStream!=null) {
49
- if(res.responseStream.getAbortSignal().aborted) return;
50
- res.responseStream.writeParamsAndEnd(obj).catch(e => null);
51
- } else {
52
- res.status(statusCode).json(obj);
53
- }
54
- }
55
- })();
56
- }
57
- }
58
-
59
- export type LoggerType = {
60
- debug: (msg: string, ...args: any[]) => void,
61
- info: (msg: string, ...args: any[]) => void,
62
- warn: (msg: string, ...args: any[]) => void,
63
- error: (msg: string, ...args: any[]) => void
64
- };
65
-
66
- export function getLogger(prefix: string): LoggerType {
67
- return {
68
- debug: (msg, ...args) => console.debug(prefix+msg, ...args),
69
- info: (msg, ...args) => console.info(prefix+msg, ...args),
70
- warn: (msg, ...args) => console.warn(prefix+msg, ...args),
71
- error: (msg, ...args) => console.error(prefix+msg, ...args)
72
- };
73
- }
74
-
75
- export const HEX_REGEX = /[0-9a-fA-F]+/;
76
-
77
- export function serializeBN(bn: bigint | null): string | null {
78
- return bn==null ? null : bn.toString(10);
79
- }
80
-
81
- export function deserializeBN(str: string | null): bigint | null {
82
- return str==null ? null : BigInt(str);
83
- }
84
-
85
- export function bigIntSorter(a: bigint, b: bigint): -1 | 0 | 1 {
86
- if(a < b) return -1;
87
- if(a > b) return 1;
88
- return 0;
89
- }
90
-
91
- /**
92
- * Creates an abort controller that extends the responseStream's abort signal
93
- *
94
- * @param responseStream
95
- */
96
- export function getAbortController(responseStream: ServerParamEncoder): AbortController {
97
- const abortController = new AbortController();
98
- if(responseStream==null || responseStream.getAbortSignal==null) return abortController;
99
- const responseStreamAbortController = responseStream.getAbortSignal();
100
- responseStreamAbortController.addEventListener("abort", () => abortController.abort(responseStreamAbortController.reason));
101
- return abortController;
102
- }
1
+ import {Request, Response} from "express";
2
+ import {ServerParamEncoder} from "./paramcoders/server/ServerParamEncoder";
3
+
4
+ export type DefinedRuntimeError = {
5
+ code: number;
6
+ msg?: string;
7
+ _httpStatus?: number;
8
+ data?: any;
9
+ };
10
+
11
+ export function isDefinedRuntimeError(obj: any): obj is DefinedRuntimeError {
12
+ if(obj.code!=null && typeof(obj.code)==="number") {
13
+ if(obj.msg!=null && typeof(obj.msg)!=="string") return false;
14
+ if(obj._httpStatus!=null && typeof(obj._httpStatus)!=="number") return false;
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+
20
+ export function expressHandlerWrapper(func: (
21
+ req: Request,
22
+ res: Response
23
+ ) => Promise<void>) : ((
24
+ req: Request,
25
+ res: Response & {responseStream: ServerParamEncoder}
26
+ ) => void) {
27
+ return (
28
+ req: Request,
29
+ res: Response & {responseStream: ServerParamEncoder}
30
+ ) => {
31
+ (async () => {
32
+ try {
33
+ await func(req, res);
34
+ } catch (e) {
35
+ console.error(e);
36
+ let statusCode = 500;
37
+ const obj: {code: number, msg: string, data?: any} = {
38
+ code: 0,
39
+ msg: "Internal server error"
40
+ };
41
+ if(isDefinedRuntimeError(e)) {
42
+ obj.msg = e.msg;
43
+ obj.code = e.code;
44
+ obj.data = e.data;
45
+ statusCode = 400;
46
+ if(e._httpStatus!=null) statusCode = e._httpStatus;
47
+ }
48
+ if(res.responseStream!=null) {
49
+ if(res.responseStream.getAbortSignal().aborted) return;
50
+ res.responseStream.writeParamsAndEnd(obj).catch(e => null);
51
+ } else {
52
+ res.status(statusCode).json(obj);
53
+ }
54
+ }
55
+ })();
56
+ }
57
+ }
58
+
59
+ export type LoggerType = {
60
+ debug: (msg: string, ...args: any[]) => void,
61
+ info: (msg: string, ...args: any[]) => void,
62
+ warn: (msg: string, ...args: any[]) => void,
63
+ error: (msg: string, ...args: any[]) => void
64
+ };
65
+
66
+ export function getLogger(prefix: string): LoggerType {
67
+ return {
68
+ debug: (msg, ...args) => console.debug(prefix+msg, ...args),
69
+ info: (msg, ...args) => console.info(prefix+msg, ...args),
70
+ warn: (msg, ...args) => console.warn(prefix+msg, ...args),
71
+ error: (msg, ...args) => console.error(prefix+msg, ...args)
72
+ };
73
+ }
74
+
75
+ export const HEX_REGEX = /[0-9a-fA-F]+/;
76
+
77
+ export function serializeBN(bn: bigint | null): string | null {
78
+ return bn==null ? null : bn.toString(10);
79
+ }
80
+
81
+ export function deserializeBN(str: string | null): bigint | null {
82
+ return str==null ? null : BigInt(str);
83
+ }
84
+
85
+ export function bigIntSorter(a: bigint, b: bigint): -1 | 0 | 1 {
86
+ if(a < b) return -1;
87
+ if(a > b) return 1;
88
+ return 0;
89
+ }
90
+
91
+ /**
92
+ * Creates an abort controller that extends the responseStream's abort signal
93
+ *
94
+ * @param responseStream
95
+ */
96
+ export function getAbortController(responseStream: ServerParamEncoder): AbortController {
97
+ const abortController = new AbortController();
98
+ if(responseStream==null || responseStream.getAbortSignal==null) return abortController;
99
+ const responseStreamAbortController = responseStream.getAbortSignal();
100
+ responseStreamAbortController.addEventListener("abort", () => abortController.abort(responseStreamAbortController.reason));
101
+ return abortController;
102
+ }
@@ -1,8 +1,8 @@
1
- import {RequestSchema, RequestSchemaResult} from "./SchemaVerifier";
2
-
3
- export interface IParamReader {
4
-
5
- getParams<T extends RequestSchema>(schema: T): Promise<RequestSchemaResult<T>>;
6
- getExistingParamsOrNull<T extends RequestSchema>(schema: T): RequestSchemaResult<T>;
7
-
1
+ import {RequestSchema, RequestSchemaResult} from "./SchemaVerifier";
2
+
3
+ export interface IParamReader {
4
+
5
+ getParams<T extends RequestSchema>(schema: T): Promise<RequestSchemaResult<T>>;
6
+ getExistingParamsOrNull<T extends RequestSchema>(schema: T): RequestSchemaResult<T>;
7
+
8
8
  }
@@ -1,8 +1,8 @@
1
-
2
- export interface IParamWriter {
3
-
4
- end(): Promise<void>;
5
- writeParams(data: any): Promise<void>;
6
-
7
- }
8
-
1
+
2
+ export interface IParamWriter {
3
+
4
+ end(): Promise<void>;
5
+ writeParams(data: any): Promise<void>;
6
+
7
+ }
8
+
@@ -1,28 +1,28 @@
1
- import {IParamWriter} from "./IParamWriter";
2
-
3
-
4
- export class LegacyParamEncoder implements IParamWriter {
5
-
6
- private readonly writeFN: (data: Buffer) => Promise<void>;
7
- private readonly endFN: () => Promise<void>;
8
-
9
- private obj = {};
10
-
11
- constructor(write: (data: Buffer) => Promise<void>, end: () => Promise<void>) {
12
- this.writeFN = write;
13
- this.endFN = end;
14
- }
15
-
16
- writeParams(data: any): Promise<void> {
17
- for(let key in data) {
18
- if(this.obj[key]==null) this.obj[key] = data[key];
19
- }
20
- return Promise.resolve();
21
- }
22
-
23
- async end(): Promise<void> {
24
- await this.writeFN(Buffer.from(JSON.stringify(this.obj)));
25
- await this.endFN();
26
- }
27
-
1
+ import {IParamWriter} from "./IParamWriter";
2
+
3
+
4
+ export class LegacyParamEncoder implements IParamWriter {
5
+
6
+ private readonly writeFN: (data: Buffer) => Promise<void>;
7
+ private readonly endFN: () => Promise<void>;
8
+
9
+ private obj = {};
10
+
11
+ constructor(write: (data: Buffer) => Promise<void>, end: () => Promise<void>) {
12
+ this.writeFN = write;
13
+ this.endFN = end;
14
+ }
15
+
16
+ writeParams(data: any): Promise<void> {
17
+ for(let key in data) {
18
+ if(this.obj[key]==null) this.obj[key] = data[key];
19
+ }
20
+ return Promise.resolve();
21
+ }
22
+
23
+ async end(): Promise<void> {
24
+ await this.writeFN(Buffer.from(JSON.stringify(this.obj)));
25
+ await this.endFN();
26
+ }
27
+
28
28
  }