@keep-network/tbtc-v2 0.1.1-dev.4 → 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 (60) hide show
  1. package/README.adoc +12 -0
  2. package/artifacts/TBTC.json +19 -18
  3. package/artifacts/TBTCToken.json +19 -18
  4. package/artifacts/VendingMachine.json +20 -19
  5. package/artifacts/solcInputs/002940e9cc8128f6629e90620c66cba5.json +215 -0
  6. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  7. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.json +2 -2
  8. package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
  9. package/build/contracts/bank/Bank.sol/Bank.json +20 -2
  10. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +4 -0
  11. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +10 -0
  12. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  13. package/build/contracts/bridge/Bridge.sol/Bridge.json +1664 -63
  14. package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +4 -0
  15. package/build/contracts/bridge/BridgeState.sol/BridgeState.json +42 -0
  16. package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +4 -0
  17. package/build/contracts/bridge/Deposit.sol/Deposit.json +72 -0
  18. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +4 -0
  19. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.json +10 -0
  20. package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +4 -0
  21. package/build/contracts/bridge/Fraud.sol/Fraud.json +138 -0
  22. package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +4 -0
  23. package/build/contracts/bridge/IRelay.sol/IRelay.json +37 -0
  24. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +4 -0
  25. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +30 -0
  26. package/build/contracts/bridge/Redeem.sol/OutboundTx.dbg.json +4 -0
  27. package/build/contracts/bridge/Redeem.sol/OutboundTx.json +10 -0
  28. package/build/contracts/bridge/Redeem.sol/Redeem.dbg.json +4 -0
  29. package/build/contracts/bridge/Redeem.sol/Redeem.json +92 -0
  30. package/build/contracts/bridge/Sweep.sol/Sweep.dbg.json +4 -0
  31. package/build/contracts/bridge/Sweep.sol/Sweep.json +30 -0
  32. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  33. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.json +2 -2
  34. package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +4 -0
  35. package/build/contracts/bridge/Wallets.sol/Wallets.json +93 -0
  36. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  37. package/build/contracts/token/TBTC.sol/TBTC.json +2 -2
  38. package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
  39. package/build/contracts/vault/IVault.sol/IVault.json +19 -1
  40. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
  41. package/build/contracts/vault/TBTCVault.sol/TBTCVault.json +36 -18
  42. package/contracts/GovernanceUtils.sol +1 -1
  43. package/contracts/bank/Bank.sol +34 -18
  44. package/contracts/bridge/BitcoinTx.sol +241 -0
  45. package/contracts/bridge/Bridge.sol +942 -123
  46. package/contracts/bridge/BridgeState.sol +251 -0
  47. package/contracts/bridge/Deposit.sol +244 -0
  48. package/contracts/bridge/EcdsaLib.sol +30 -0
  49. package/contracts/bridge/Fraud.sol +455 -0
  50. package/contracts/bridge/IRelay.sol +28 -0
  51. package/contracts/bridge/MovingFunds.sol +278 -0
  52. package/contracts/bridge/Redeem.sol +844 -0
  53. package/contracts/bridge/Sweep.sol +509 -0
  54. package/contracts/bridge/VendingMachine.sol +1 -1
  55. package/contracts/bridge/Wallets.sol +510 -0
  56. package/contracts/token/TBTC.sol +1 -1
  57. package/contracts/vault/IVault.sol +32 -10
  58. package/contracts/vault/TBTCVault.sol +20 -2
  59. package/package.json +28 -24
  60. package/artifacts/solcInputs/d71966212a658480bad5748ad85b1396.json +0 -116
@@ -0,0 +1,510 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+
16
+ pragma solidity ^0.8.9;
17
+
18
+ import {BTCUtils} from "@keep-network/bitcoin-spv-sol/contracts/BTCUtils.sol";
19
+ import {IWalletRegistry as EcdsaWalletRegistry} from "@keep-network/ecdsa/contracts/api/IWalletRegistry.sol";
20
+ import {EcdsaDkg} from "@keep-network/ecdsa/contracts/libraries/EcdsaDkg.sol";
21
+
22
+ import "./BitcoinTx.sol";
23
+ import "./EcdsaLib.sol";
24
+ import "./BridgeState.sol";
25
+
26
+ /// @title Wallet library
27
+ /// @notice Library responsible for handling integration between Bridge
28
+ /// contract and ECDSA wallets.
29
+ library Wallets {
30
+ using BTCUtils for bytes;
31
+
32
+ /// @notice Represents wallet state:
33
+ enum WalletState {
34
+ /// @dev The wallet is unknown to the Bridge.
35
+ Unknown,
36
+ /// @dev The wallet can sweep deposits and accept redemption requests.
37
+ Live,
38
+ /// @dev The wallet was deemed unhealthy and is expected to move their
39
+ /// outstanding funds to another wallet. The wallet can still
40
+ /// fulfill their pending redemption requests although new
41
+ /// redemption requests and new deposit reveals are not accepted.
42
+ MovingFunds,
43
+ /// @dev The wallet moved or redeemed all their funds and cannot
44
+ /// perform any action.
45
+ Closed,
46
+ /// @dev The wallet committed a fraud that was reported. The wallet is
47
+ /// blocked and can not perform any actions in the Bridge.
48
+ /// Off-chain coordination with the wallet operators is needed to
49
+ /// recover funds.
50
+ Terminated
51
+ }
52
+
53
+ /// @notice Holds information about a wallet.
54
+ struct Wallet {
55
+ // Identifier of a ECDSA Wallet registered in the ECDSA Wallet Registry.
56
+ bytes32 ecdsaWalletID;
57
+ // Latest wallet's main UTXO hash computed as
58
+ // keccak256(txHash | txOutputIndex | txOutputValue). The `tx` prefix
59
+ // refers to the transaction which created that main UTXO. The `txHash`
60
+ // is `bytes32` (ordered as in Bitcoin internally), `txOutputIndex`
61
+ // an `uint32`, and `txOutputValue` an `uint64` value.
62
+ bytes32 mainUtxoHash;
63
+ // The total redeemable value of pending redemption requests targeting
64
+ // that wallet.
65
+ uint64 pendingRedemptionsValue;
66
+ // UNIX timestamp the wallet was created at.
67
+ uint32 createdAt;
68
+ // UNIX timestamp indicating the moment the wallet was requested to
69
+ // move their funds.
70
+ uint32 movingFundsRequestedAt;
71
+ // Current state of the wallet.
72
+ WalletState state;
73
+ // Moving funds target wallet commitment submitted by the wallet. It
74
+ // is built by applying the keccak256 hash over the list of 20-byte
75
+ // public key hashes of the target wallets.
76
+ bytes32 movingFundsTargetWalletsCommitmentHash;
77
+ }
78
+
79
+ event NewWalletRequested();
80
+
81
+ event NewWalletRegistered(
82
+ bytes32 indexed ecdsaWalletID,
83
+ bytes20 indexed walletPubKeyHash
84
+ );
85
+
86
+ event WalletMovingFunds(
87
+ bytes32 indexed ecdsaWalletID,
88
+ bytes20 indexed walletPubKeyHash
89
+ );
90
+
91
+ event WalletClosed(
92
+ bytes32 indexed ecdsaWalletID,
93
+ bytes20 indexed walletPubKeyHash
94
+ );
95
+
96
+ event WalletTerminated(
97
+ bytes32 indexed ecdsaWalletID,
98
+ bytes20 indexed walletPubKeyHash
99
+ );
100
+
101
+ /// @notice Requests creation of a new wallet. This function just
102
+ /// forms a request and the creation process is performed
103
+ /// asynchronously. Outcome of that process should be delivered
104
+ /// using `registerNewWallet` function.
105
+ /// @param activeWalletMainUtxo Data of the active wallet's main UTXO, as
106
+ /// currently known on the Ethereum chain.
107
+ /// @dev Requirements:
108
+ /// - `activeWalletMainUtxo` components must point to the recent main
109
+ /// UTXO of the given active wallet, as currently known on the
110
+ /// Ethereum chain. If there is no active wallet at the moment, or
111
+ /// the active wallet has no main UTXO, this parameter can be
112
+ /// empty as it is ignored.
113
+ /// - Wallet creation must not be in progress
114
+ /// - If the active wallet is set, one of the following
115
+ /// conditions must be true:
116
+ /// - The active wallet BTC balance is above the minimum threshold
117
+ /// and the active wallet is old enough, i.e. the creation period
118
+ /// was elapsed since its creation time
119
+ /// - The active wallet BTC balance is above the maximum threshold
120
+ function requestNewWallet(
121
+ BridgeState.Storage storage self,
122
+ BitcoinTx.UTXO calldata activeWalletMainUtxo
123
+ ) external {
124
+ require(
125
+ self.ecdsaWalletRegistry.getWalletCreationState() ==
126
+ EcdsaDkg.State.IDLE,
127
+ "Wallet creation already in progress"
128
+ );
129
+
130
+ bytes20 activeWalletPubKeyHash = self.activeWalletPubKeyHash;
131
+
132
+ // If the active wallet is set, fetch this wallet's details from
133
+ // storage to perform conditions check. The `registerNewWallet`
134
+ // function guarantees an active wallet is always one of the
135
+ // registered ones.
136
+ if (activeWalletPubKeyHash != bytes20(0)) {
137
+ uint64 activeWalletBtcBalance = getWalletBtcBalance(
138
+ self,
139
+ activeWalletPubKeyHash,
140
+ activeWalletMainUtxo
141
+ );
142
+ uint32 activeWalletCreatedAt = self
143
+ .registeredWallets[activeWalletPubKeyHash]
144
+ .createdAt;
145
+ /* solhint-disable-next-line not-rely-on-time */
146
+ bool activeWalletOldEnough = block.timestamp >=
147
+ activeWalletCreatedAt + self.walletCreationPeriod;
148
+
149
+ require(
150
+ (activeWalletOldEnough &&
151
+ activeWalletBtcBalance >= self.walletMinBtcBalance) ||
152
+ activeWalletBtcBalance >= self.walletMaxBtcBalance,
153
+ "Wallet creation conditions are not met"
154
+ );
155
+ }
156
+
157
+ emit NewWalletRequested();
158
+
159
+ self.ecdsaWalletRegistry.requestNewWallet();
160
+ }
161
+
162
+ /// @notice Gets BTC balance for given the wallet.
163
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
164
+ /// @param walletMainUtxo Data of the wallet's main UTXO, as currently
165
+ /// known on the Ethereum chain.
166
+ /// @return walletBtcBalance Current BTC balance for the given wallet.
167
+ /// @dev Requirements:
168
+ /// - `walletMainUtxo` components must point to the recent main UTXO
169
+ /// of the given wallet, as currently known on the Ethereum chain.
170
+ /// If the wallet has no main UTXO, this parameter can be empty as it
171
+ /// is ignored.
172
+ function getWalletBtcBalance(
173
+ BridgeState.Storage storage self,
174
+ bytes20 walletPubKeyHash,
175
+ BitcoinTx.UTXO calldata walletMainUtxo
176
+ ) internal view returns (uint64 walletBtcBalance) {
177
+ bytes32 walletMainUtxoHash = self
178
+ .registeredWallets[walletPubKeyHash]
179
+ .mainUtxoHash;
180
+
181
+ // If the wallet has a main UTXO hash set, cross-check it with the
182
+ // provided plain-text parameter and get the transaction output value
183
+ // as BTC balance. Otherwise, the BTC balance is just zero.
184
+ if (walletMainUtxoHash != bytes32(0)) {
185
+ require(
186
+ keccak256(
187
+ abi.encodePacked(
188
+ walletMainUtxo.txHash,
189
+ walletMainUtxo.txOutputIndex,
190
+ walletMainUtxo.txOutputValue
191
+ )
192
+ ) == walletMainUtxoHash,
193
+ "Invalid wallet main UTXO data"
194
+ );
195
+
196
+ walletBtcBalance = walletMainUtxo.txOutputValue;
197
+ }
198
+
199
+ return walletBtcBalance;
200
+ }
201
+
202
+ /// @notice Registers a new wallet. This function should be called
203
+ /// after the wallet creation process initiated using
204
+ /// `requestNewWallet` completes and brings the outcomes.
205
+ /// @param ecdsaWalletID Wallet's unique identifier.
206
+ /// @param publicKeyX Wallet's public key's X coordinate.
207
+ /// @param publicKeyY Wallet's public key's Y coordinate.
208
+ /// @dev Requirements:
209
+ /// - The only caller authorized to call this function is `registry`
210
+ /// - Given wallet data must not belong to an already registered wallet
211
+ function registerNewWallet(
212
+ BridgeState.Storage storage self,
213
+ bytes32 ecdsaWalletID,
214
+ bytes32 publicKeyX,
215
+ bytes32 publicKeyY
216
+ ) external {
217
+ require(
218
+ msg.sender == address(self.ecdsaWalletRegistry),
219
+ "Caller is not the ECDSA Wallet Registry"
220
+ );
221
+
222
+ // Compress wallet's public key and calculate Bitcoin's hash160 of it.
223
+ bytes20 walletPubKeyHash = bytes20(
224
+ EcdsaLib.compressPublicKey(publicKeyX, publicKeyY).hash160View()
225
+ );
226
+
227
+ Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
228
+ require(
229
+ wallet.state == WalletState.Unknown,
230
+ "ECDSA wallet has been already registered"
231
+ );
232
+ wallet.ecdsaWalletID = ecdsaWalletID;
233
+ wallet.state = WalletState.Live;
234
+ /* solhint-disable-next-line not-rely-on-time */
235
+ wallet.createdAt = uint32(block.timestamp);
236
+
237
+ // Set the freshly created wallet as the new active wallet.
238
+ self.activeWalletPubKeyHash = walletPubKeyHash;
239
+
240
+ emit NewWalletRegistered(ecdsaWalletID, walletPubKeyHash);
241
+ }
242
+
243
+ /// @notice Handles a notification about a wallet heartbeat failure and
244
+ /// triggers the wallet moving funds process.
245
+ /// @param publicKeyX Wallet's public key's X coordinate.
246
+ /// @param publicKeyY Wallet's public key's Y coordinate.
247
+ /// @dev Requirements:
248
+ /// - The only caller authorized to call this function is `registry`
249
+ /// - Wallet must be in Live state
250
+ function notifyWalletHeartbeatFailed(
251
+ BridgeState.Storage storage self,
252
+ bytes32 publicKeyX,
253
+ bytes32 publicKeyY
254
+ ) external {
255
+ require(
256
+ msg.sender == address(self.ecdsaWalletRegistry),
257
+ "Caller is not the ECDSA Wallet Registry"
258
+ );
259
+
260
+ // Compress wallet's public key and calculate Bitcoin's hash160 of it.
261
+ bytes20 walletPubKeyHash = bytes20(
262
+ EcdsaLib.compressPublicKey(publicKeyX, publicKeyY).hash160View()
263
+ );
264
+
265
+ require(
266
+ self.registeredWallets[walletPubKeyHash].state == WalletState.Live,
267
+ "ECDSA wallet must be in Live state"
268
+ );
269
+
270
+ moveFunds(self, walletPubKeyHash);
271
+ }
272
+
273
+ /// @notice Handles a notification about a wallet redemption timeout
274
+ /// and requests slashing of the wallet operators. Triggers the
275
+ /// wallet moving funds process only if the wallet is still in the
276
+ /// Live state. That means multiple action timeouts can be reported
277
+ /// for the same wallet but only the first report requests the
278
+ /// wallet to move their funds.
279
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
280
+ /// @dev Requirements:
281
+ /// - The wallet must be in the `Live` or `MovingFunds` state
282
+ function notifyWalletTimedOutRedemption(
283
+ BridgeState.Storage storage self,
284
+ bytes20 walletPubKeyHash
285
+ ) internal {
286
+ WalletState walletState = self
287
+ .registeredWallets[walletPubKeyHash]
288
+ .state;
289
+
290
+ require(
291
+ walletState == WalletState.Live ||
292
+ walletState == WalletState.MovingFunds,
293
+ "ECDSA wallet must be in Live or MovingFunds state"
294
+ );
295
+
296
+ if (walletState == WalletState.Live) {
297
+ moveFunds(self, walletPubKeyHash);
298
+ }
299
+
300
+ // TODO: Perform slashing of wallet operators and transfer some of the
301
+ // slashed tokens to the caller of this function.
302
+ }
303
+
304
+ /// @notice Notifies that the wallet is either old enough or has too few
305
+ /// satoshis left and qualifies to be closed.
306
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
307
+ /// @param walletMainUtxo Data of the wallet's main UTXO, as currently
308
+ /// known on the Ethereum chain.
309
+ /// @dev Requirements:
310
+ /// - Wallet must not be set as the current active wallet
311
+ /// - Wallet must exceed the wallet maximum age OR the wallet BTC
312
+ /// balance must be lesser than the minimum threshold. If the latter
313
+ /// case is true, the `walletMainUtxo` components must point to the
314
+ /// recent main UTXO of the given wallet, as currently known on the
315
+ /// Ethereum chain. If the wallet has no main UTXO, this parameter
316
+ /// can be empty as it is ignored since the wallet balance is
317
+ /// assumed to be zero.
318
+ /// - Wallet must be in Live state
319
+ function notifyCloseableWallet(
320
+ BridgeState.Storage storage self,
321
+ bytes20 walletPubKeyHash,
322
+ BitcoinTx.UTXO calldata walletMainUtxo
323
+ ) external {
324
+ require(
325
+ self.activeWalletPubKeyHash != walletPubKeyHash,
326
+ "Active wallet cannot be considered closeable"
327
+ );
328
+
329
+ Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
330
+ require(
331
+ wallet.state == WalletState.Live,
332
+ "ECDSA wallet must be in Live state"
333
+ );
334
+
335
+ /* solhint-disable-next-line not-rely-on-time */
336
+ bool walletOldEnough = block.timestamp >=
337
+ wallet.createdAt + self.walletMaxAge;
338
+
339
+ require(
340
+ walletOldEnough ||
341
+ getWalletBtcBalance(self, walletPubKeyHash, walletMainUtxo) <
342
+ self.walletMinBtcBalance,
343
+ "Wallet needs to be old enough or have too few satoshis"
344
+ );
345
+
346
+ moveFunds(self, walletPubKeyHash);
347
+ }
348
+
349
+ /// @notice Requests a wallet to move their funds. If the wallet balance
350
+ /// is zero, the wallet is closed immediately and the ECDSA
351
+ /// registry is notified about this fact. If the move funds
352
+ /// request refers to the current active wallet, such a wallet
353
+ /// is no longer considered active and the active wallet slot
354
+ /// is unset allowing to trigger a new wallet creation immediately.
355
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
356
+ /// @dev Requirements:
357
+ /// - The caller must make sure that the wallet is in the Live state
358
+ function moveFunds(
359
+ BridgeState.Storage storage self,
360
+ bytes20 walletPubKeyHash
361
+ ) internal {
362
+ Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
363
+
364
+ if (wallet.mainUtxoHash == bytes32(0)) {
365
+ // If the wallet has no main UTXO, that means its BTC balance
366
+ // is zero and it should be closed immediately.
367
+ closeWallet(self, walletPubKeyHash);
368
+ } else {
369
+ // Otherwise, initialize the moving funds process.
370
+ wallet.state = WalletState.MovingFunds;
371
+ /* solhint-disable-next-line not-rely-on-time */
372
+ wallet.movingFundsRequestedAt = uint32(block.timestamp);
373
+
374
+ emit WalletMovingFunds(wallet.ecdsaWalletID, walletPubKeyHash);
375
+ }
376
+
377
+ if (self.activeWalletPubKeyHash == walletPubKeyHash) {
378
+ // If the move funds request refers to the current active wallet,
379
+ // unset the active wallet and make the wallet creation process
380
+ // possible in order to get a new healthy active wallet.
381
+ delete self.activeWalletPubKeyHash;
382
+ }
383
+ }
384
+
385
+ /// @notice Closes the given wallet and notifies the ECDSA registry
386
+ /// about this fact.
387
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
388
+ /// @dev Requirements:
389
+ /// - The caller must make sure that the wallet is in the
390
+ /// Live or MovingFunds state.
391
+ function closeWallet(
392
+ BridgeState.Storage storage self,
393
+ bytes20 walletPubKeyHash
394
+ ) internal {
395
+ Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
396
+
397
+ wallet.state = WalletState.Closed;
398
+
399
+ emit WalletClosed(wallet.ecdsaWalletID, walletPubKeyHash);
400
+
401
+ self.ecdsaWalletRegistry.closeWallet(wallet.ecdsaWalletID);
402
+ }
403
+
404
+ /// @notice Reports about a fraud committed by the given wallet. This
405
+ /// function performs slashing and wallet termination in reaction
406
+ /// to a proven fraud and it should only be called when the fraud
407
+ /// was confirmed.
408
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
409
+ /// @dev Requirements:
410
+ /// - Wallet must be in Live or MovingFunds state
411
+ function notifyWalletFraud(
412
+ BridgeState.Storage storage self,
413
+ bytes20 walletPubKeyHash
414
+ ) internal {
415
+ WalletState walletState = self
416
+ .registeredWallets[walletPubKeyHash]
417
+ .state;
418
+
419
+ require(
420
+ walletState == WalletState.Live ||
421
+ walletState == WalletState.MovingFunds,
422
+ "ECDSA wallet must be in Live or MovingFunds state"
423
+ );
424
+
425
+ terminateWallet(self, walletPubKeyHash);
426
+
427
+ // TODO: Perform slashing of wallet operators and add unit tests for that.
428
+ }
429
+
430
+ /// @notice Terminates the given wallet and notifies the ECDSA registry
431
+ /// about this fact. If the wallet termination refers to the current
432
+ /// active wallet, such a wallet is no longer considered active and
433
+ /// the active wallet slot is unset allowing to trigger a new wallet
434
+ /// creation immediately.
435
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
436
+ /// @dev Requirements:
437
+ /// - The caller must make sure that the wallet is in the
438
+ /// Live or MovingFunds state.
439
+ function terminateWallet(
440
+ BridgeState.Storage storage self,
441
+ bytes20 walletPubKeyHash
442
+ ) internal {
443
+ Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
444
+
445
+ wallet.state = WalletState.Terminated;
446
+
447
+ emit WalletTerminated(wallet.ecdsaWalletID, walletPubKeyHash);
448
+
449
+ if (self.activeWalletPubKeyHash == walletPubKeyHash) {
450
+ // If termination refers to the current active wallet,
451
+ // unset the active wallet and make the wallet creation process
452
+ // possible in order to get a new healthy active wallet.
453
+ delete self.activeWalletPubKeyHash;
454
+ }
455
+
456
+ self.ecdsaWalletRegistry.closeWallet(wallet.ecdsaWalletID);
457
+ }
458
+
459
+ /// @notice Notifies that the wallet completed the moving funds process
460
+ /// successfully. Checks if the funds were moved to the expected
461
+ /// target wallets. Closes the source wallet if everything went
462
+ /// good and reverts otherwise.
463
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
464
+ /// @param targetWalletsHash 32-byte keccak256 hash over the list of
465
+ /// 20-byte public key hashes of the target wallets actually used
466
+ /// within the moving funds transactions.
467
+ /// @dev Requirements:
468
+ /// - The caller must make sure the moving funds transaction actually
469
+ /// happened on Bitcoin chain and fits the protocol requirements.
470
+ /// - The source wallet must be in the MovingFunds state
471
+ /// - The target wallets commitment must be submitted by the source
472
+ /// wallet.
473
+ /// - The actual target wallets used in the moving funds transaction
474
+ /// must be exactly the same as the target wallets commitment.
475
+ function notifyWalletFundsMoved(
476
+ BridgeState.Storage storage self,
477
+ bytes20 walletPubKeyHash,
478
+ bytes32 targetWalletsHash
479
+ ) internal {
480
+ Wallet storage wallet = self.registeredWallets[walletPubKeyHash];
481
+ // Check that the wallet is in the MovingFunds state but don't check
482
+ // if the moving funds timeout is exceeded. That should give a
483
+ // possibility to move funds in case when timeout was hit but was
484
+ // not reported yet.
485
+ require(
486
+ wallet.state == WalletState.MovingFunds,
487
+ "ECDSA wallet must be in MovingFunds state"
488
+ );
489
+
490
+ bytes32 targetWalletsCommitmentHash = wallet
491
+ .movingFundsTargetWalletsCommitmentHash;
492
+
493
+ require(
494
+ targetWalletsCommitmentHash != bytes32(0),
495
+ "Target wallets commitment not submitted yet"
496
+ );
497
+
498
+ // Make sure that the target wallets where funds were moved to are
499
+ // exactly the same as the ones the source wallet committed to.
500
+ require(
501
+ targetWalletsCommitmentHash == targetWalletsHash,
502
+ "Target wallets don't correspond to the commitment"
503
+ );
504
+
505
+ // If funds were moved, the wallet has no longer a main UTXO.
506
+ delete wallet.mainUtxoHash;
507
+
508
+ closeWallet(self, walletPubKeyHash);
509
+ }
510
+ }
@@ -1,6 +1,6 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
 
3
- pragma solidity 0.8.4;
3
+ pragma solidity ^0.8.9;
4
4
 
5
5
  import "@thesis/solidity-contracts/contracts/token/ERC20WithPermit.sol";
6
6
  import "@thesis/solidity-contracts/contracts/token/MisfundRecovery.sol";
@@ -13,25 +13,47 @@
13
13
  // ▐████▌ ▐████▌
14
14
  // ▐████▌ ▐████▌
15
15
 
16
- pragma solidity 0.8.4;
16
+ pragma solidity ^0.8.9;
17
17
 
18
18
  /// @title Bank Vault interface
19
19
  /// @notice `IVault` is an interface for a smart contract consuming Bank
20
- /// balances allowing the smart contract to receive Bank balances right
21
- /// after sweeping the deposit by the Bridge. This method allows the
22
- /// depositor to route their deposit revealed to the Bridge to the
23
- /// particular smart contract in the same transaction the deposit is
24
- /// revealed. This way, the depositor does not have to execute
25
- /// additional transaction after the deposit gets swept by the Bridge.
20
+ /// balances of other contracts or externally owned accounts (EOA).
26
21
  interface IVault {
22
+ /// @notice Called by the Bank in `approveBalanceAndCall` function after
23
+ /// the balance `owner` approved `amount` of their balance in the
24
+ /// Bank for the vault. This way, the depositor can approve balance
25
+ /// and call the vault to use the approved balance in a single
26
+ /// transaction.
27
+ /// @param owner Address of the Bank balance owner who approved their
28
+ /// balance to be used by the vault
29
+ /// @param amount The amount of the Bank balance approved by the owner
30
+ /// to be used by the vault
31
+ // @dev The implementation must ensure this function can only be called
32
+ /// by the Bank. The Bank does _not_ guarantee that the `amount`
33
+ /// approved by the `owner` currently exists on their balance. That is,
34
+ /// the `owner` could approve more balance than they currently have.
35
+ /// This works the same as `Bank.approve` function. The vault must
36
+ /// ensure the actual balance is checked before performing any action
37
+ /// based on it.
38
+ function receiveBalanceApproval(address owner, uint256 amount) external;
39
+
27
40
  /// @notice Called by the Bank in `increaseBalanceAndCall` function after
28
- /// increasing the balance in the Bank for the vault.
41
+ /// increasing the balance in the Bank for the vault. It happens in
42
+ /// the same transaction in which deposits were swept by the Bridge.
43
+ /// This allows the depositor to route their deposit revealed to the
44
+ /// Bridge to the particular smart contract (vault) in the same
45
+ /// transaction in which the deposit is revealed. This way, the
46
+ /// depositor does not have to execute additional transaction after
47
+ /// the deposit gets swept by the Bridge to approve and transfer
48
+ /// their balance to the vault.
29
49
  /// @param depositors Addresses of depositors whose deposits have been swept
30
50
  /// @param depositedAmounts Amounts deposited by individual depositors and
31
51
  /// swept
32
52
  /// @dev The implementation must ensure this function can only be called
33
- /// by the Bank.
34
- function onBalanceIncreased(
53
+ /// by the Bank. The Bank guarantees that the vault's balance was
54
+ /// increased by the sum of all deposited amounts before this function
55
+ /// is called, in the same transaction.
56
+ function receiveBalanceIncrease(
35
57
  address[] calldata depositors,
36
58
  uint256[] calldata depositedAmounts
37
59
  ) external;
@@ -13,7 +13,7 @@
13
13
  // ▐████▌ ▐████▌
14
14
  // ▐████▌ ▐████▌
15
15
 
16
- pragma solidity 0.8.4;
16
+ pragma solidity ^0.8.9;
17
17
 
18
18
  import "./IVault.sol";
19
19
  import "../bank/Bank.sol";
@@ -70,13 +70,31 @@ contract TBTCVault is IVault {
70
70
  bank.transferBalanceFrom(minter, address(this), amount);
71
71
  }
72
72
 
73
+ /// @notice Transfers the given `amount` of the Bank balance from the caller
74
+ /// to TBTC Vault and mints `amount` of TBTC to the caller.
75
+ /// @dev Can only be called by the Bank via `approveBalanceAndCall`.
76
+ /// @param owner The owner who approved their Bank balance
77
+ /// @param amount Amount of TBTC to mint
78
+ function receiveBalanceApproval(address owner, uint256 amount)
79
+ external
80
+ override
81
+ onlyBank
82
+ {
83
+ require(
84
+ bank.balanceOf(owner) >= amount,
85
+ "Amount exceeds balance in the bank"
86
+ );
87
+ _mint(owner, amount);
88
+ bank.transferBalanceFrom(owner, address(this), amount);
89
+ }
90
+
73
91
  /// @notice Mints the same amount of TBTC as the deposited amount for each
74
92
  /// depositor in the array. Can only be called by the Bank after the
75
93
  /// Bridge swept deposits and Bank increased balance for the
76
94
  /// vault.
77
95
  /// @dev Fails if `depositors` array is empty. Expects the length of
78
96
  /// `depositors` and `depositedAmounts` is the same.
79
- function onBalanceIncreased(
97
+ function receiveBalanceIncrease(
80
98
  address[] calldata depositors,
81
99
  uint256[] calldata depositedAmounts
82
100
  ) external override onlyBank {