@keep-network/tbtc-v2 0.1.1-dev.41 → 0.1.1-dev.42

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 (40) hide show
  1. package/artifacts/TBTC.json +3 -3
  2. package/artifacts/TBTCToken.json +3 -3
  3. package/artifacts/VendingMachine.json +10 -10
  4. package/artifacts/solcInputs/{e9b173393b9fd7287a0bfaa6d4eb4b71.json → 002940e9cc8128f6629e90620c66cba5.json} +22 -22
  5. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  6. package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
  7. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +1 -1
  8. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  9. package/build/contracts/bridge/Bridge.sol/Bridge.json +86 -140
  10. package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +1 -1
  11. package/build/contracts/bridge/BridgeState.sol/BridgeState.json +35 -3
  12. package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +1 -1
  13. package/build/contracts/bridge/Deposit.sol/Deposit.json +2 -2
  14. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +1 -1
  15. package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +1 -1
  16. package/build/contracts/bridge/Fraud.sol/Fraud.json +2 -2
  17. package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +1 -1
  18. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +1 -1
  19. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +4 -22
  20. package/build/contracts/bridge/Redeem.sol/OutboundTx.dbg.json +1 -1
  21. package/build/contracts/bridge/Redeem.sol/OutboundTx.json +2 -2
  22. package/build/contracts/bridge/Redeem.sol/Redeem.dbg.json +1 -1
  23. package/build/contracts/bridge/Redeem.sol/Redeem.json +4 -22
  24. package/build/contracts/bridge/Sweep.sol/Sweep.dbg.json +1 -1
  25. package/build/contracts/bridge/Sweep.sol/Sweep.json +2 -2
  26. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  27. package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +1 -1
  28. package/build/contracts/bridge/Wallets.sol/Wallets.json +2 -47
  29. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  30. package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
  31. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
  32. package/contracts/bridge/Bridge.sol +79 -76
  33. package/contracts/bridge/BridgeState.sol +78 -4
  34. package/contracts/bridge/Deposit.sol +1 -4
  35. package/contracts/bridge/Fraud.sol +1 -2
  36. package/contracts/bridge/MovingFunds.sol +2 -4
  37. package/contracts/bridge/Redeem.sol +6 -11
  38. package/contracts/bridge/Sweep.sol +3 -4
  39. package/contracts/bridge/Wallets.sol +40 -121
  40. package/package.json +1 -1
@@ -47,13 +47,12 @@ library OutboundTx {
47
47
  /// performed the outbound transaction.
48
48
  function processWalletOutboundTxInput(
49
49
  BridgeState.Storage storage self,
50
- Wallets.Data storage wallets,
51
50
  bytes memory walletOutboundTxInputVector,
52
51
  BitcoinTx.UTXO calldata mainUtxo,
53
52
  bytes20 walletPubKeyHash
54
53
  ) internal {
55
54
  // Assert that main UTXO for passed wallet exists in storage.
56
- bytes32 mainUtxoHash = wallets
55
+ bytes32 mainUtxoHash = self
57
56
  .registeredWallets[walletPubKeyHash]
58
57
  .mainUtxoHash;
59
58
  require(mainUtxoHash != bytes32(0), "No main UTXO for given wallet");
@@ -144,7 +143,7 @@ library OutboundTx {
144
143
  // TODO: Rename to Redemption. All library names are nouns.
145
144
  library Redeem {
146
145
  using BridgeState for BridgeState.Storage;
147
- using Wallets for Wallets.Data;
146
+ using Wallets for BridgeState.Storage;
148
147
 
149
148
  using BTCUtils for bytes;
150
149
  using BytesLib for bytes;
@@ -248,13 +247,12 @@ library Redeem {
248
247
  /// contract can spend the given `amount`.
249
248
  function requestRedemption(
250
249
  BridgeState.Storage storage self,
251
- Wallets.Data storage wallets,
252
250
  bytes20 walletPubKeyHash,
253
251
  BitcoinTx.UTXO calldata mainUtxo,
254
252
  bytes calldata redeemerOutputScript,
255
253
  uint64 amount
256
254
  ) external {
257
- Wallets.Wallet storage wallet = wallets.registeredWallets[
255
+ Wallets.Wallet storage wallet = self.registeredWallets[
258
256
  walletPubKeyHash
259
257
  ];
260
258
 
@@ -419,7 +417,6 @@ library Redeem {
419
417
  /// is identified, that check is omitted in further iterations.
420
418
  function submitRedemptionProof(
421
419
  BridgeState.Storage storage self,
422
- Wallets.Data storage wallets,
423
420
  BitcoinTx.Info calldata redemptionTx,
424
421
  BitcoinTx.Proof calldata redemptionProof,
425
422
  BitcoinTx.UTXO calldata mainUtxo,
@@ -443,13 +440,12 @@ library Redeem {
443
440
  // refers to the expected wallet's main UTXO.
444
441
  OutboundTx.processWalletOutboundTxInput(
445
442
  self,
446
- wallets,
447
443
  redemptionTx.inputVector,
448
444
  mainUtxo,
449
445
  walletPubKeyHash
450
446
  );
451
447
 
452
- Wallets.Wallet storage wallet = wallets.registeredWallets[
448
+ Wallets.Wallet storage wallet = self.registeredWallets[
453
449
  walletPubKeyHash
454
450
  ];
455
451
 
@@ -789,7 +785,6 @@ library Redeem {
789
785
  /// timed-out).
790
786
  function notifyRedemptionTimeout(
791
787
  BridgeState.Storage storage self,
792
- Wallets.Data storage wallets,
793
788
  bytes20 walletPubKeyHash,
794
789
  bytes calldata redeemerOutputScript
795
790
  ) external {
@@ -808,7 +803,7 @@ library Redeem {
808
803
  );
809
804
 
810
805
  // Update the wallet's pending redemptions value
811
- Wallets.Wallet storage wallet = wallets.registeredWallets[
806
+ Wallets.Wallet storage wallet = self.registeredWallets[
812
807
  walletPubKeyHash
813
808
  ];
814
809
  wallet.pendingRedemptionsValue -=
@@ -838,7 +833,7 @@ library Redeem {
838
833
  wallet.state == Wallets.WalletState.MovingFunds
839
834
  ) {
840
835
  // Propagate timeout consequences to the wallet
841
- wallets.notifyRedemptionTimedOut(walletPubKeyHash);
836
+ self.notifyWalletTimedOutRedemption(walletPubKeyHash);
842
837
  }
843
838
 
844
839
  emit RedemptionTimedOut(walletPubKeyHash, redeemerOutputScript);
@@ -94,7 +94,6 @@ library Sweep {
94
94
  /// If there is no main UTXO, this parameter is ignored.
95
95
  function submitSweepProof(
96
96
  BridgeState.Storage storage self,
97
- Wallets.Data storage wallets,
98
97
  BitcoinTx.Info calldata sweepTx,
99
98
  BitcoinTx.Proof calldata sweepProof,
100
99
  BitcoinTx.UTXO calldata mainUtxo
@@ -122,7 +121,7 @@ library Sweep {
122
121
  (
123
122
  Wallets.Wallet storage wallet,
124
123
  BitcoinTx.UTXO memory resolvedMainUtxo
125
- ) = resolveSweepingWallet(wallets, walletPubKeyHash, mainUtxo);
124
+ ) = resolveSweepingWallet(self, walletPubKeyHash, mainUtxo);
126
125
 
127
126
  // Process sweep transaction inputs and extract all information needed
128
127
  // to perform deposit bookkeeping.
@@ -208,7 +207,7 @@ library Sweep {
208
207
  /// - If the main UTXO of the sweeping wallet exists in the storage,
209
208
  /// the passed `mainUTXO` parameter must be equal to the stored one.
210
209
  function resolveSweepingWallet(
211
- Wallets.Data storage wallets,
210
+ BridgeState.Storage storage self,
212
211
  bytes20 walletPubKeyHash,
213
212
  BitcoinTx.UTXO calldata mainUtxo
214
213
  )
@@ -218,7 +217,7 @@ library Sweep {
218
217
  BitcoinTx.UTXO memory resolvedMainUtxo
219
218
  )
220
219
  {
221
- wallet = wallets.registeredWallets[walletPubKeyHash];
220
+ wallet = self.registeredWallets[walletPubKeyHash];
222
221
 
223
222
  Wallets.WalletState walletState = wallet.state;
224
223
  require(
@@ -21,6 +21,7 @@ import {EcdsaDkg} from "@keep-network/ecdsa/contracts/libraries/EcdsaDkg.sol";
21
21
 
22
22
  import "./BitcoinTx.sol";
23
23
  import "./EcdsaLib.sol";
24
+ import "./BridgeState.sol";
24
25
 
25
26
  /// @title Wallet library
26
27
  /// @notice Library responsible for handling integration between Bridge
@@ -28,32 +29,6 @@ import "./EcdsaLib.sol";
28
29
  library Wallets {
29
30
  using BTCUtils for bytes;
30
31
 
31
- /// @notice Struct that groups the state managed by the library.
32
- struct Data {
33
- // ECDSA Wallet Registry contract handle.
34
- EcdsaWalletRegistry registry;
35
- // Determines how frequently a new wallet creation can be requested.
36
- // Value in seconds.
37
- uint32 creationPeriod;
38
- // The minimum BTC threshold in satoshi that is used to decide about
39
- // wallet creation or closing.
40
- uint64 minBtcBalance;
41
- // The maximum BTC threshold in satoshi that is used to decide about
42
- // wallet creation.
43
- uint64 maxBtcBalance;
44
- // The maximum age of a wallet in seconds, after which the wallet
45
- // moving funds process can be requested.
46
- uint32 maxAge;
47
- // 20-byte wallet public key hash being reference to the currently
48
- // active wallet. Can be unset to the zero value under certain
49
- // circumstances.
50
- bytes20 activeWalletPubKeyHash;
51
- // Maps the 20-byte wallet public key hash (computed using Bitcoin
52
- // HASH160 over the compressed ECDSA public key) to the basic wallet
53
- // information like state and pending redemptions value.
54
- mapping(bytes20 => Wallet) registeredWallets;
55
- }
56
-
57
32
  /// @notice Represents wallet state:
58
33
  enum WalletState {
59
34
  /// @dev The wallet is unknown to the Bridge.
@@ -101,15 +76,6 @@ library Wallets {
101
76
  bytes32 movingFundsTargetWalletsCommitmentHash;
102
77
  }
103
78
 
104
- event WalletCreationPeriodUpdated(uint32 newCreationPeriod);
105
-
106
- event WalletBtcBalanceRangeUpdated(
107
- uint64 newMinBtcBalance,
108
- uint64 newMaxBtcBalance
109
- );
110
-
111
- event WalletMaxAgeUpdated(uint32 newMaxAge);
112
-
113
79
  event NewWalletRequested();
114
80
 
115
81
  event NewWalletRegistered(
@@ -132,64 +98,6 @@ library Wallets {
132
98
  bytes20 indexed walletPubKeyHash
133
99
  );
134
100
 
135
- /// @notice Initializes state invariants.
136
- /// @param registry ECDSA Wallet Registry reference
137
- /// @dev Requirements:
138
- /// - ECDSA Wallet Registry address must not be initialized
139
- function init(Data storage self, address registry) external {
140
- require(
141
- registry != address(0),
142
- "ECDSA Wallet Registry address cannot be zero"
143
- );
144
- require(
145
- address(self.registry) == address(0),
146
- "ECDSA Wallet Registry address already set"
147
- );
148
-
149
- self.registry = EcdsaWalletRegistry(registry);
150
- }
151
-
152
- /// @notice Sets the wallet creation period.
153
- /// @param creationPeriod New value of the wallet creation period
154
- function setCreationPeriod(Data storage self, uint32 creationPeriod)
155
- external
156
- {
157
- self.creationPeriod = creationPeriod;
158
-
159
- emit WalletCreationPeriodUpdated(creationPeriod);
160
- }
161
-
162
- /// @notice Sets the minimum and maximum BTC balance parameters.
163
- /// @param minBtcBalance New value of the minimum BTC balance
164
- /// @param maxBtcBalance New value of the maximum BTC balance
165
- /// @dev Requirements:
166
- /// - Minimum BTC balance must be greater than zero
167
- /// - Maximum BTC balance must be greater than minimum BTC balance
168
- function setBtcBalanceRange(
169
- Data storage self,
170
- uint64 minBtcBalance,
171
- uint64 maxBtcBalance
172
- ) external {
173
- require(minBtcBalance > 0, "Minimum must be greater than zero");
174
- require(
175
- maxBtcBalance > minBtcBalance,
176
- "Maximum must be greater than the minimum"
177
- );
178
-
179
- self.minBtcBalance = minBtcBalance;
180
- self.maxBtcBalance = maxBtcBalance;
181
-
182
- emit WalletBtcBalanceRangeUpdated(minBtcBalance, maxBtcBalance);
183
- }
184
-
185
- /// @notice Sets the wallet maximum age.
186
- /// @param maxAge New value of the wallet maximum age
187
- function setMaxAge(Data storage self, uint32 maxAge) external {
188
- self.maxAge = maxAge;
189
-
190
- emit WalletMaxAgeUpdated(maxAge);
191
- }
192
-
193
101
  /// @notice Requests creation of a new wallet. This function just
194
102
  /// forms a request and the creation process is performed
195
103
  /// asynchronously. Outcome of that process should be delivered
@@ -210,11 +118,12 @@ library Wallets {
210
118
  /// was elapsed since its creation time
211
119
  /// - The active wallet BTC balance is above the maximum threshold
212
120
  function requestNewWallet(
213
- Data storage self,
121
+ BridgeState.Storage storage self,
214
122
  BitcoinTx.UTXO calldata activeWalletMainUtxo
215
123
  ) external {
216
124
  require(
217
- self.registry.getWalletCreationState() == EcdsaDkg.State.IDLE,
125
+ self.ecdsaWalletRegistry.getWalletCreationState() ==
126
+ EcdsaDkg.State.IDLE,
218
127
  "Wallet creation already in progress"
219
128
  );
220
129
 
@@ -235,19 +144,19 @@ library Wallets {
235
144
  .createdAt;
236
145
  /* solhint-disable-next-line not-rely-on-time */
237
146
  bool activeWalletOldEnough = block.timestamp >=
238
- activeWalletCreatedAt + self.creationPeriod;
147
+ activeWalletCreatedAt + self.walletCreationPeriod;
239
148
 
240
149
  require(
241
150
  (activeWalletOldEnough &&
242
- activeWalletBtcBalance >= self.minBtcBalance) ||
243
- activeWalletBtcBalance >= self.maxBtcBalance,
151
+ activeWalletBtcBalance >= self.walletMinBtcBalance) ||
152
+ activeWalletBtcBalance >= self.walletMaxBtcBalance,
244
153
  "Wallet creation conditions are not met"
245
154
  );
246
155
  }
247
156
 
248
157
  emit NewWalletRequested();
249
158
 
250
- self.registry.requestNewWallet();
159
+ self.ecdsaWalletRegistry.requestNewWallet();
251
160
  }
252
161
 
253
162
  /// @notice Gets BTC balance for given the wallet.
@@ -261,7 +170,7 @@ library Wallets {
261
170
  /// If the wallet has no main UTXO, this parameter can be empty as it
262
171
  /// is ignored.
263
172
  function getWalletBtcBalance(
264
- Data storage self,
173
+ BridgeState.Storage storage self,
265
174
  bytes20 walletPubKeyHash,
266
175
  BitcoinTx.UTXO calldata walletMainUtxo
267
176
  ) internal view returns (uint64 walletBtcBalance) {
@@ -300,13 +209,13 @@ library Wallets {
300
209
  /// - The only caller authorized to call this function is `registry`
301
210
  /// - Given wallet data must not belong to an already registered wallet
302
211
  function registerNewWallet(
303
- Data storage self,
212
+ BridgeState.Storage storage self,
304
213
  bytes32 ecdsaWalletID,
305
214
  bytes32 publicKeyX,
306
215
  bytes32 publicKeyY
307
216
  ) external {
308
217
  require(
309
- msg.sender == address(self.registry),
218
+ msg.sender == address(self.ecdsaWalletRegistry),
310
219
  "Caller is not the ECDSA Wallet Registry"
311
220
  );
312
221
 
@@ -339,12 +248,12 @@ library Wallets {
339
248
  /// - The only caller authorized to call this function is `registry`
340
249
  /// - Wallet must be in Live state
341
250
  function notifyWalletHeartbeatFailed(
342
- Data storage self,
251
+ BridgeState.Storage storage self,
343
252
  bytes32 publicKeyX,
344
253
  bytes32 publicKeyY
345
254
  ) external {
346
255
  require(
347
- msg.sender == address(self.registry),
256
+ msg.sender == address(self.ecdsaWalletRegistry),
348
257
  "Caller is not the ECDSA Wallet Registry"
349
258
  );
350
259
 
@@ -370,10 +279,10 @@ library Wallets {
370
279
  /// @param walletPubKeyHash 20-byte public key hash of the wallet
371
280
  /// @dev Requirements:
372
281
  /// - The wallet must be in the `Live` or `MovingFunds` state
373
- function notifyRedemptionTimedOut(
374
- Data storage self,
282
+ function notifyWalletTimedOutRedemption(
283
+ BridgeState.Storage storage self,
375
284
  bytes20 walletPubKeyHash
376
- ) external {
285
+ ) internal {
377
286
  WalletState walletState = self
378
287
  .registeredWallets[walletPubKeyHash]
379
288
  .state;
@@ -408,7 +317,7 @@ library Wallets {
408
317
  /// assumed to be zero.
409
318
  /// - Wallet must be in Live state
410
319
  function notifyCloseableWallet(
411
- Data storage self,
320
+ BridgeState.Storage storage self,
412
321
  bytes20 walletPubKeyHash,
413
322
  BitcoinTx.UTXO calldata walletMainUtxo
414
323
  ) external {
@@ -425,12 +334,12 @@ library Wallets {
425
334
 
426
335
  /* solhint-disable-next-line not-rely-on-time */
427
336
  bool walletOldEnough = block.timestamp >=
428
- wallet.createdAt + self.maxAge;
337
+ wallet.createdAt + self.walletMaxAge;
429
338
 
430
339
  require(
431
340
  walletOldEnough ||
432
341
  getWalletBtcBalance(self, walletPubKeyHash, walletMainUtxo) <
433
- self.minBtcBalance,
342
+ self.walletMinBtcBalance,
434
343
  "Wallet needs to be old enough or have too few satoshis"
435
344
  );
436
345
 
@@ -446,7 +355,10 @@ library Wallets {
446
355
  /// @param walletPubKeyHash 20-byte public key hash of the wallet
447
356
  /// @dev Requirements:
448
357
  /// - The caller must make sure that the wallet is in the Live state
449
- function moveFunds(Data storage self, bytes20 walletPubKeyHash) internal {
358
+ function moveFunds(
359
+ BridgeState.Storage storage self,
360
+ bytes20 walletPubKeyHash
361
+ ) internal {
450
362
  Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
451
363
 
452
364
  if (wallet.mainUtxoHash == bytes32(0)) {
@@ -476,14 +388,17 @@ library Wallets {
476
388
  /// @dev Requirements:
477
389
  /// - The caller must make sure that the wallet is in the
478
390
  /// Live or MovingFunds state.
479
- function closeWallet(Data storage self, bytes20 walletPubKeyHash) internal {
391
+ function closeWallet(
392
+ BridgeState.Storage storage self,
393
+ bytes20 walletPubKeyHash
394
+ ) internal {
480
395
  Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
481
396
 
482
397
  wallet.state = WalletState.Closed;
483
398
 
484
399
  emit WalletClosed(wallet.ecdsaWalletID, walletPubKeyHash);
485
400
 
486
- self.registry.closeWallet(wallet.ecdsaWalletID);
401
+ self.ecdsaWalletRegistry.closeWallet(wallet.ecdsaWalletID);
487
402
  }
488
403
 
489
404
  /// @notice Reports about a fraud committed by the given wallet. This
@@ -493,7 +408,10 @@ library Wallets {
493
408
  /// @param walletPubKeyHash 20-byte public key hash of the wallet
494
409
  /// @dev Requirements:
495
410
  /// - Wallet must be in Live or MovingFunds state
496
- function notifyFraud(Data storage self, bytes20 walletPubKeyHash) external {
411
+ function notifyWalletFraud(
412
+ BridgeState.Storage storage self,
413
+ bytes20 walletPubKeyHash
414
+ ) internal {
497
415
  WalletState walletState = self
498
416
  .registeredWallets[walletPubKeyHash]
499
417
  .state;
@@ -518,9 +436,10 @@ library Wallets {
518
436
  /// @dev Requirements:
519
437
  /// - The caller must make sure that the wallet is in the
520
438
  /// Live or MovingFunds state.
521
- function terminateWallet(Data storage self, bytes20 walletPubKeyHash)
522
- internal
523
- {
439
+ function terminateWallet(
440
+ BridgeState.Storage storage self,
441
+ bytes20 walletPubKeyHash
442
+ ) internal {
524
443
  Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
525
444
 
526
445
  wallet.state = WalletState.Terminated;
@@ -534,7 +453,7 @@ library Wallets {
534
453
  delete self.activeWalletPubKeyHash;
535
454
  }
536
455
 
537
- self.registry.closeWallet(wallet.ecdsaWalletID);
456
+ self.ecdsaWalletRegistry.closeWallet(wallet.ecdsaWalletID);
538
457
  }
539
458
 
540
459
  /// @notice Notifies that the wallet completed the moving funds process
@@ -553,11 +472,11 @@ library Wallets {
553
472
  /// wallet.
554
473
  /// - The actual target wallets used in the moving funds transaction
555
474
  /// must be exactly the same as the target wallets commitment.
556
- function notifyFundsMoved(
557
- Data storage self,
475
+ function notifyWalletFundsMoved(
476
+ BridgeState.Storage storage self,
558
477
  bytes20 walletPubKeyHash,
559
478
  bytes32 targetWalletsHash
560
- ) external {
479
+ ) internal {
561
480
  Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
562
481
  // Check that the wallet is in the MovingFunds state but don't check
563
482
  // if the moving funds timeout is exceeded. That should give a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keep-network/tbtc-v2",
3
- "version": "0.1.1-dev.41+main.4a1165e2e997e16ed16872972456ac724f86b2f3",
3
+ "version": "0.1.1-dev.42+main.3ae39c48899311b3e2494f79a238d8ec48dc5f54",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "artifacts/",