@atomiqlabs/base 12.0.4 → 13.0.4

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 (113) hide show
  1. package/dist/btc/BitcoinNetwork.d.ts +5 -0
  2. package/dist/btc/BitcoinNetwork.js +5 -0
  3. package/dist/btc/rpc/BitcoinRpc.d.ts +168 -7
  4. package/dist/btcrelay/BtcRelay.d.ts +115 -6
  5. package/dist/btcrelay/synchronizer/RelaySynchronizer.d.ts +55 -14
  6. package/dist/btcrelay/types/BtcBlock.d.ts +32 -0
  7. package/dist/btcrelay/types/BtcHeader.d.ts +23 -0
  8. package/dist/btcrelay/types/BtcStoredHeader.d.ts +26 -0
  9. package/dist/btcrelay/utils/StatePredictorUtils.d.ts +5 -0
  10. package/dist/btcrelay/utils/StatePredictorUtils.js +5 -0
  11. package/dist/chains/ChainData.d.ts +41 -1
  12. package/dist/chains/ChainInterface.d.ts +44 -1
  13. package/dist/chains/ChainInterface.js +5 -0
  14. package/dist/chains/ChainType.d.ts +7 -1
  15. package/dist/errors/CannotInitializeATAError.d.ts +5 -0
  16. package/dist/errors/CannotInitializeATAError.js +5 -0
  17. package/dist/errors/SignatureVerificationError.d.ts +5 -0
  18. package/dist/errors/SignatureVerificationError.js +5 -0
  19. package/dist/errors/SwapDataVerificationError.d.ts +5 -0
  20. package/dist/errors/SwapDataVerificationError.js +5 -0
  21. package/dist/errors/TransactionRevertedError.d.ts +5 -0
  22. package/dist/errors/TransactionRevertedError.js +5 -0
  23. package/dist/events/ChainEvents.d.ts +40 -2
  24. package/dist/events/types/ChainEvent.d.ts +8 -0
  25. package/dist/events/types/ChainEvent.js +5 -0
  26. package/dist/events/types/spv_vault/SpvVaultClaimEvent.d.ts +29 -0
  27. package/dist/events/types/spv_vault/SpvVaultClaimEvent.js +5 -0
  28. package/dist/events/types/spv_vault/SpvVaultCloseEvent.d.ts +13 -0
  29. package/dist/events/types/spv_vault/SpvVaultCloseEvent.js +7 -0
  30. package/dist/events/types/spv_vault/SpvVaultDepositEvent.d.ts +12 -0
  31. package/dist/events/types/spv_vault/SpvVaultDepositEvent.js +5 -0
  32. package/dist/events/types/spv_vault/SpvVaultEvent.d.ts +18 -2
  33. package/dist/events/types/spv_vault/SpvVaultEvent.js +10 -0
  34. package/dist/events/types/spv_vault/SpvVaultFrontEvent.d.ts +22 -0
  35. package/dist/events/types/spv_vault/SpvVaultFrontEvent.js +5 -0
  36. package/dist/events/types/spv_vault/SpvVaultOpenEvent.d.ts +11 -0
  37. package/dist/events/types/spv_vault/SpvVaultOpenEvent.js +5 -0
  38. package/dist/events/types/swap/ClaimEvent.d.ts +8 -0
  39. package/dist/events/types/swap/ClaimEvent.js +6 -0
  40. package/dist/events/types/swap/InitializeEvent.d.ts +13 -2
  41. package/dist/events/types/swap/InitializeEvent.js +6 -0
  42. package/dist/events/types/swap/RefundEvent.d.ts +5 -0
  43. package/dist/events/types/swap/RefundEvent.js +9 -0
  44. package/dist/events/types/swap/SwapEvent.d.ts +15 -2
  45. package/dist/events/types/swap/SwapEvent.js +10 -0
  46. package/dist/lockable/Lockable.d.ts +18 -0
  47. package/dist/lockable/Lockable.js +18 -0
  48. package/dist/messaging/Messenger.d.ts +32 -0
  49. package/dist/messaging/messages/Message.d.ts +24 -1
  50. package/dist/messaging/messages/Message.js +23 -0
  51. package/dist/messaging/messages/SwapClaimWitnessMessage.d.ts +22 -4
  52. package/dist/messaging/messages/SwapClaimWitnessMessage.js +12 -0
  53. package/dist/spv_swap/SpvVaultContract.d.ts +18 -10
  54. package/dist/spv_swap/SpvVaultData.d.ts +76 -0
  55. package/dist/spv_swap/SpvVaultData.js +19 -0
  56. package/dist/spv_swap/SpvWithdrawalState.d.ts +46 -0
  57. package/dist/spv_swap/SpvWithdrawalState.js +19 -0
  58. package/dist/spv_swap/SpvWithdrawalTransactionData.d.ts +94 -3
  59. package/dist/spv_swap/SpvWithdrawalTransactionData.js +81 -4
  60. package/dist/storage/IStorageManager.d.ts +37 -0
  61. package/dist/storage/StorageObject.d.ts +8 -0
  62. package/dist/swaps/ChainSwapType.d.ts +22 -0
  63. package/dist/swaps/ChainSwapType.js +22 -0
  64. package/dist/swaps/SwapCommitState.d.ts +50 -0
  65. package/dist/swaps/SwapCommitState.js +20 -0
  66. package/dist/swaps/SwapContract.d.ts +58 -2
  67. package/dist/swaps/SwapData.d.ts +147 -6
  68. package/dist/swaps/SwapData.js +20 -0
  69. package/dist/utils/BigIntBufferUtils.d.ts +5 -0
  70. package/dist/utils/BigIntBufferUtils.js +6 -1
  71. package/package.json +5 -3
  72. package/src/btc/BitcoinNetwork.ts +5 -1
  73. package/src/btc/rpc/BitcoinRpc.ts +175 -8
  74. package/src/btcrelay/BtcRelay.ts +121 -7
  75. package/src/btcrelay/synchronizer/RelaySynchronizer.ts +51 -10
  76. package/src/btcrelay/types/BtcBlock.ts +40 -0
  77. package/src/btcrelay/types/BtcHeader.ts +31 -3
  78. package/src/btcrelay/types/BtcStoredHeader.ts +31 -0
  79. package/src/btcrelay/utils/StatePredictorUtils.ts +5 -0
  80. package/src/chains/ChainData.ts +41 -1
  81. package/src/chains/ChainInterface.ts +47 -0
  82. package/src/chains/ChainType.ts +10 -3
  83. package/src/errors/CannotInitializeATAError.ts +5 -1
  84. package/src/errors/SignatureVerificationError.ts +5 -1
  85. package/src/errors/SwapDataVerificationError.ts +5 -1
  86. package/src/errors/TransactionRevertedError.ts +5 -1
  87. package/src/events/ChainEvents.ts +44 -2
  88. package/src/events/types/ChainEvent.ts +8 -0
  89. package/src/events/types/spv_vault/SpvVaultClaimEvent.ts +29 -0
  90. package/src/events/types/spv_vault/SpvVaultCloseEvent.ts +13 -0
  91. package/src/events/types/spv_vault/SpvVaultDepositEvent.ts +13 -0
  92. package/src/events/types/spv_vault/SpvVaultEvent.ts +18 -3
  93. package/src/events/types/spv_vault/SpvVaultFrontEvent.ts +22 -0
  94. package/src/events/types/spv_vault/SpvVaultOpenEvent.ts +11 -0
  95. package/src/events/types/swap/ClaimEvent.ts +9 -2
  96. package/src/events/types/swap/InitializeEvent.ts +14 -4
  97. package/src/events/types/swap/RefundEvent.ts +6 -1
  98. package/src/events/types/swap/SwapEvent.ts +15 -3
  99. package/src/lockable/Lockable.ts +18 -2
  100. package/src/messaging/Messenger.ts +37 -0
  101. package/src/messaging/messages/Message.ts +24 -3
  102. package/src/messaging/messages/SwapClaimWitnessMessage.ts +23 -6
  103. package/src/spv_swap/SpvVaultContract.ts +19 -12
  104. package/src/spv_swap/SpvVaultData.ts +84 -0
  105. package/src/spv_swap/SpvWithdrawalState.ts +46 -1
  106. package/src/spv_swap/SpvWithdrawalTransactionData.ts +102 -7
  107. package/src/storage/IStorageManager.ts +41 -0
  108. package/src/storage/StorageObject.ts +8 -2
  109. package/src/swaps/ChainSwapType.ts +22 -0
  110. package/src/swaps/SwapCommitState.ts +50 -0
  111. package/src/swaps/SwapContract.ts +58 -2
  112. package/src/swaps/SwapData.ts +160 -7
  113. package/src/utils/BigIntBufferUtils.ts +6 -1
@@ -6,6 +6,9 @@ import {Buffer} from "buffer";
6
6
  import {AbstractSigner, TransactionConfirmationOptions} from "../chains/ChainInterface";
7
7
  import {SwapCommitState} from "./SwapCommitState";
8
8
 
9
+ /**
10
+ * Represents intermediary (LP) reputation across different swap types
11
+ */
9
12
  export type IntermediaryReputationType = {
10
13
  [key in ChainSwapType]: {
11
14
  successVolume: bigint,
@@ -17,20 +20,46 @@ export type IntermediaryReputationType = {
17
20
  }
18
21
  };
19
22
 
23
+ /**
24
+ * Signature authorization data
25
+ */
20
26
  export type SignatureData = {
21
27
  prefix: string,
22
28
  timeout: string,
23
29
  signature: string
24
30
  };
25
31
 
32
+ /**
33
+ * Simplified bitcoin transaction data
34
+ */
26
35
  export type BitcoinTransactionData = {
36
+ /**
37
+ * Blockhash of a block which contains this transaction
38
+ */
27
39
  blockhash: string,
40
+ /**
41
+ * The number of confirmations of a block containing this transaction
42
+ */
28
43
  confirmations: number,
44
+ /**
45
+ * Transaction ID identifier of the transaction
46
+ */
29
47
  txid: string,
48
+ /**
49
+ * A hexadecimal serialized representation of the transaction with witness data stripped (legacy transaction format)
50
+ */
30
51
  hex: string,
52
+ /**
53
+ * The blockheight of a block containing this transaction
54
+ */
31
55
  height: number
32
56
  };
33
57
 
58
+ /**
59
+ * A contract for interacting with escrow manager swaps - escrow based swaps like HTLCs and PrTLCs
60
+ *
61
+ * @category Swaps
62
+ */
34
63
  export interface SwapContract<
35
64
  T extends SwapData = SwapData,
36
65
  TX = any,
@@ -40,10 +69,28 @@ export interface SwapContract<
40
69
  ChainId extends string = string
41
70
  > {
42
71
 
72
+ /**
73
+ * Chain identifier string
74
+ */
43
75
  readonly chainId: ChainId;
76
+ /**
77
+ * A timeout to be used when claiming the escrow with a secret (HTLC), recommended to wait at least this long after
78
+ * sending a transaction before considering it a failure
79
+ */
44
80
  readonly claimWithSecretTimeout: number;
81
+ /**
82
+ * A timeout to be used when claiming the escrow with bitcoin transaction data (PrTLC), recommended to wait at
83
+ * least this long after sending a transaction before considering it a failure
84
+ */
45
85
  readonly claimWithTxDataTimeout: number;
86
+ /**
87
+ * A timeout to be used when refunding the escrow, recommended to wait at
88
+ * least this long after sending a transaction before considering it a failure
89
+ */
46
90
  readonly refundTimeout: number;
91
+ /**
92
+ * Whether this chain supports sending initialization without a direct authorization by the claimer
93
+ */
47
94
  readonly supportsInitWithoutClaimer?: boolean;
48
95
 
49
96
  /**
@@ -265,7 +312,7 @@ export interface SwapContract<
265
312
  state: SwapCommitState
266
313
  }
267
314
  },
268
- latestBlockheight: number
315
+ latestBlockheight?: number
269
316
  }>;
270
317
 
271
318
  /**
@@ -410,7 +457,7 @@ export interface SwapContract<
410
457
  * @param address
411
458
  * @param token
412
459
  */
413
- getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType>;
460
+ getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType | null>;
414
461
 
415
462
  /**
416
463
  * Returns the fee in native token base units to commit (initiate) the swap
@@ -501,6 +548,15 @@ export interface SwapContract<
501
548
  */
502
549
  getClaimFeeRate(signer: string, swapData: T): Promise<string>;
503
550
 
551
+ /**
552
+ * Returns additional data to be included in the data-carrier (extra data) part of the PrTLC escrow initialization
553
+ * this provides helpers for 3rd party claimers
554
+ *
555
+ * @param outputScript A bitcoin output script required in the bitcoin transaction to claim the escrow
556
+ * @param amount An amount of bitcoin (in satoshis) required in the bitcoin transaction to claim the escrow
557
+ * @param confirmations Confirmations of the bitcoin transaction required for claiming the escrow
558
+ * @param nonce An optional nonce applied to the transaction
559
+ */
504
560
  getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer;
505
561
 
506
562
  /**
@@ -1,12 +1,25 @@
1
1
  import {ChainSwapType} from "./ChainSwapType";
2
2
  import {StorageObject} from "../storage/StorageObject";
3
3
 
4
+ /**
5
+ * Represents full escrow swap data and parameters
6
+ *
7
+ * @category Swaps
8
+ */
4
9
  export abstract class SwapData implements StorageObject {
5
10
 
11
+ /**
12
+ * A mapping of deserializers for different escrow swap data types coming from different smart chain implementations
13
+ */
6
14
  static deserializers: {
7
15
  [type: string]: new (serialized: any) => any,
8
16
  } = {};
9
17
 
18
+ /**
19
+ * Deserializer parsing the chain-specific escrow swap data from a JSON-compatible object representation
20
+ *
21
+ * @param data
22
+ */
10
23
  static deserialize<T extends SwapData>(data: any): T {
11
24
  const deserializer = SwapData.deserializers[data.type];
12
25
  if(deserializer != null) {
@@ -14,56 +27,196 @@ export abstract class SwapData implements StorageObject {
14
27
  }
15
28
  throw new Error(`No deserializer found for swap data type: ${data.type}`);
16
29
  }
30
+
31
+ /**
32
+ * Gets the offerer, which funds the escrow
33
+ */
17
34
  abstract getOfferer(): string;
35
+
36
+ /**
37
+ * Sets the offerer to fund the escrow
38
+ *
39
+ * @param newOfferer
40
+ */
18
41
  abstract setOfferer(newOfferer: string): void;
42
+
43
+ /**
44
+ * Checks whether the provided address is an offerer for this escrow
45
+ *
46
+ * @param address
47
+ */
19
48
  abstract isOfferer(address: string): boolean;
20
49
 
50
+ /**
51
+ * Gets the claim, which receives funds from the escrow
52
+ */
21
53
  abstract getClaimer(): string;
54
+
55
+ /**
56
+ * Sets the claimer which receives funds from the escrow
57
+ *
58
+ * @param newClaimer
59
+ */
22
60
  abstract setClaimer(newClaimer: string): void;
61
+
62
+ /**
63
+ * Checks whether the provided address is a claimer for this escrow
64
+ *
65
+ * @param address
66
+ */
23
67
  abstract isClaimer(address: string): boolean;
24
68
 
69
+ /**
70
+ * @inheritDoc
71
+ */
25
72
  abstract serialize(): any;
26
73
 
74
+ /**
75
+ * Returns the type of this escrow
76
+ */
27
77
  abstract getType(): ChainSwapType;
28
78
 
79
+ /**
80
+ * Gets the amount of token deposited into the vault by the offerer
81
+ */
29
82
  abstract getAmount(): bigint;
30
83
 
84
+ /**
85
+ * Gets the address of the deposited token in a vault
86
+ */
31
87
  abstract getToken(): string;
32
88
 
89
+ /**
90
+ * Checks whether the provided token address is the actual token used by this escrow
91
+ *
92
+ * @param token
93
+ */
33
94
  abstract isToken(token: string): boolean;
34
95
 
96
+ /**
97
+ * Returns the expiration time of this escrow, after the timeout an offerer is able to refund unilaterally.
98
+ * taking back funds from the escrow
99
+ */
35
100
  abstract getExpiry(): bigint;
36
101
 
102
+ /**
103
+ * Whether this escrow will pay out the claimer, when `true` it pays out normally to the claimer's address, when
104
+ * `false` it instead keeps the funds inside the contract and assigns them to the claimer's vault inside the
105
+ * contract
106
+ */
37
107
  abstract isPayOut(): boolean;
38
108
 
109
+ /**
110
+ * Whether this escrow will be funded from the offerer's wallet or from the offerer's vault inside the contract,
111
+ * when `true` it takes funds normally from the offerer's address, when `false` it pulls the funds from the
112
+ * offerer's vault inside the contract
113
+ */
39
114
  abstract isPayIn(): boolean;
40
115
 
116
+ /**
117
+ * Whether the outcome of this escrow should be used to track reputation of the claimer
118
+ */
119
+ abstract isTrackingReputation(): boolean;
120
+
121
+ /**
122
+ * Returns the claim hash of the escrow, this specifies a condition that needs to be satisfied to claim
123
+ * the funds from the escrow
124
+ */
41
125
  abstract getClaimHash(): string;
42
126
 
127
+ /**
128
+ * Returns a unique hash of this escrow
129
+ */
43
130
  abstract getEscrowHash(): string;
44
131
 
45
- abstract getSequence?(): bigint;
46
-
47
- abstract getExtraData(): string;
48
- abstract getConfirmationsHint(): number;
49
- abstract getNonceHint(): bigint;
50
- abstract getTxoHashHint(): string;
51
- getHTLCHashHint(): string {
132
+ /**
133
+ * Returns a random variable sequence of this vault, used to ensure the escrow hash is always different
134
+ */
135
+ abstract getSequence(): bigint;
136
+
137
+ /**
138
+ * Gets the extraneous data specified when creating this escrow, this provides helpers for 3rd party claimers
139
+ */
140
+ abstract getExtraData(): string | null;
141
+
142
+ /**
143
+ * Retrieves required number of confirmations as a hint from the extra data provided, or `null` if no extra
144
+ * data has been provided when creating the escrow
145
+ */
146
+ abstract getConfirmationsHint(): number | null;
147
+
148
+ /**
149
+ * Retrieves the PrTLC transaction nonce as a hint from the extra data provided, or `null` if no extra
150
+ * data has been provided when creating the escrow
151
+ */
152
+ abstract getNonceHint(): bigint | null;
153
+
154
+ /**
155
+ * Retrieves the PrTLC transaction txo (transaction output) hash as a hint from the extra data provided,
156
+ * or `null` if no extra data has been provided when creating the escrow
157
+ */
158
+ abstract getTxoHashHint(): string | null;
159
+
160
+ /**
161
+ * Retrieves a hint providing an HTLC hash from the extra data provided, or `null` if no extra data
162
+ * has been provided when creating the escrow
163
+ */
164
+ getHTLCHashHint(): string | null {
52
165
  return null;
53
166
  }
167
+
168
+ /**
169
+ * Sets the extra data with hints for this escrow
170
+ *
171
+ * @param extraData
172
+ */
54
173
  abstract setExtraData(extraData: string): void;
55
174
 
175
+ /**
176
+ * Gets the security deposit, which is an amount of native tokens deposited by the caller (initiator) on
177
+ * escrow initialization to guarantee the execution, this acts as a compensation for offerer if he has
178
+ * to refund the escrow, otherwise it is returned back to claimer after a successful claim of the escrow
179
+ */
56
180
  abstract getSecurityDeposit(): bigint;
57
181
 
182
+ /**
183
+ * Gets the claimer bounty, an amount of native tokens deposited by the caller (initiator) on escrow
184
+ * initialization, that is paid out to a caller which claims the escrow as a reward, otherwise
185
+ * returned to the claimer if escrow is refunded
186
+ */
58
187
  abstract getClaimerBounty(): bigint;
59
188
 
189
+ /**
190
+ * Returns the total deposit in native token to be deposited by the caller (initiator) on escrow initialization,
191
+ * due to how claimer bounty and security deposits work, this is basically a max(claimer bounty, security deposit),
192
+ * because either a swap is claimed (and claimer bounty is paid) or a swap is refunded (and security deposit is paid),
193
+ * never both!
194
+ */
60
195
  abstract getTotalDeposit(): bigint;
61
196
 
197
+ /**
198
+ * Returns the token used for the deposit (security deposit & claimer bounty) (usually a native token of the
199
+ * underlying smart chain)
200
+ */
62
201
  abstract getDepositToken(): string;
202
+
203
+ /**
204
+ * Checks whether a provided token is used as a deposit token (security deposit & claimer bounty) for the escrow
205
+ *
206
+ * @param token
207
+ */
63
208
  abstract isDepositToken(token: string): boolean;
64
209
 
210
+ /**
211
+ * Checks equality between 2 swap data objects
212
+ *
213
+ * @param other
214
+ */
65
215
  abstract equals(other: SwapData): boolean;
66
216
 
217
+ /**
218
+ * Checks whether the escrow contains an execution action (swap+)
219
+ */
67
220
  hasSuccessAction(): boolean {
68
221
  return false;
69
222
  }
@@ -1,10 +1,15 @@
1
1
  import {Buffer} from "buffer";
2
2
 
3
+ /**
4
+ * A helper for serializing and deserializing bigints from/to buffers
5
+ *
6
+ * @internal
7
+ */
3
8
  export const BigIntBufferUtils: {
4
9
  toBuffer: (value: bigint, endianness?: "be" | "le", length?: number) => Buffer
5
10
  fromBuffer: (value: Buffer, endianness?: "be" | "le") => bigint
6
11
  } = {
7
- toBuffer: (value: bigint, endianness: "be" | "le" = "be", length: number) => {
12
+ toBuffer: (value: bigint, endianness: "be" | "le" = "be", length: number = 32) => {
8
13
  let values: number[] = Array(length);
9
14
  for(let i=0;i<length;i++) {
10
15
  values[i] = Number(value & 0xffn);