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

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 (81) hide show
  1. package/artifacts/Bank.json +742 -0
  2. package/artifacts/Bridge.json +2914 -0
  3. package/artifacts/Deposit.json +117 -0
  4. package/artifacts/EcdsaDkgValidator.json +532 -0
  5. package/artifacts/EcdsaInactivity.json +156 -0
  6. package/artifacts/Fraud.json +153 -0
  7. package/artifacts/KeepRegistry.json +99 -0
  8. package/artifacts/KeepStake.json +286 -0
  9. package/artifacts/KeepToken.json +711 -0
  10. package/artifacts/KeepTokenStaking.json +483 -0
  11. package/artifacts/MovingFunds.json +137 -0
  12. package/artifacts/NuCypherStakingEscrow.json +256 -0
  13. package/artifacts/NuCypherToken.json +711 -0
  14. package/artifacts/RandomBeaconStub.json +141 -0
  15. package/artifacts/Redemption.json +161 -0
  16. package/artifacts/ReimbursementPool.json +509 -0
  17. package/artifacts/Relay.json +123 -0
  18. package/artifacts/SortitionPool.json +944 -0
  19. package/artifacts/Sweep.json +76 -0
  20. package/artifacts/T.json +1148 -0
  21. package/artifacts/TBTC.json +21 -21
  22. package/artifacts/TBTCToken.json +21 -21
  23. package/artifacts/TokenStaking.json +2288 -0
  24. package/artifacts/TokenholderGovernor.json +1795 -0
  25. package/artifacts/TokenholderTimelock.json +1058 -0
  26. package/artifacts/VendingMachine.json +24 -24
  27. package/artifacts/VendingMachineKeep.json +400 -0
  28. package/artifacts/VendingMachineNuCypher.json +400 -0
  29. package/artifacts/WalletRegistry.json +2709 -0
  30. package/artifacts/WalletRegistryGovernance.json +2364 -0
  31. package/artifacts/Wallets.json +186 -0
  32. package/artifacts/solcInputs/{002940e9cc8128f6629e90620c66cba5.json → 58ca6018672440f1c9c800806e096f9c.json} +22 -22
  33. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  34. package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
  35. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +1 -1
  36. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +2 -2
  37. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  38. package/build/contracts/bridge/Bridge.sol/Bridge.json +489 -173
  39. package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +1 -1
  40. package/build/contracts/bridge/BridgeState.sol/BridgeState.json +114 -2
  41. package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +1 -1
  42. package/build/contracts/bridge/Deposit.sol/Deposit.json +2 -2
  43. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +1 -1
  44. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.json +2 -2
  45. package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +1 -1
  46. package/build/contracts/bridge/Fraud.sol/Fraud.json +5 -57
  47. package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +1 -1
  48. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +1 -1
  49. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +40 -2
  50. package/build/contracts/bridge/Redemption.sol/OutboundTx.dbg.json +4 -0
  51. package/build/contracts/bridge/{Redeem.sol → Redemption.sol}/OutboundTx.json +3 -3
  52. package/build/contracts/bridge/Redemption.sol/Redemption.dbg.json +4 -0
  53. package/build/contracts/bridge/Redemption.sol/Redemption.json +92 -0
  54. package/build/contracts/bridge/Sweep.sol/Sweep.dbg.json +1 -1
  55. package/build/contracts/bridge/Sweep.sol/Sweep.json +2 -2
  56. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  57. package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +1 -1
  58. package/build/contracts/bridge/Wallets.sol/Wallets.json +21 -2
  59. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  60. package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
  61. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
  62. package/contracts/bridge/BitcoinTx.sol +19 -26
  63. package/contracts/bridge/Bridge.sol +736 -489
  64. package/contracts/bridge/BridgeState.sol +271 -24
  65. package/contracts/bridge/Deposit.sol +24 -2
  66. package/contracts/bridge/EcdsaLib.sol +15 -0
  67. package/contracts/bridge/Fraud.sol +64 -31
  68. package/contracts/bridge/MovingFunds.sol +194 -6
  69. package/contracts/bridge/{Redeem.sol → Redemption.sol} +20 -18
  70. package/contracts/bridge/Sweep.sol +13 -8
  71. package/contracts/bridge/Wallets.sol +53 -35
  72. package/deploy/00_resolve_relay.ts +28 -0
  73. package/deploy/04_deploy_bank.ts +25 -0
  74. package/deploy/05_deploy_bridge.ts +60 -0
  75. package/deploy/06_bank_update_bridge.ts +19 -0
  76. package/deploy/07_transfer_ownership.ts +17 -0
  77. package/export.json +14797 -459
  78. package/package.json +2 -2
  79. package/build/contracts/bridge/Redeem.sol/OutboundTx.dbg.json +0 -4
  80. package/build/contracts/bridge/Redeem.sol/Redeem.dbg.json +0 -4
  81. package/build/contracts/bridge/Redeem.sol/Redeem.json +0 -92
@@ -15,9 +15,11 @@
15
15
 
16
16
  pragma solidity ^0.8.9;
17
17
 
18
+ import {IWalletRegistry as EcdsaWalletRegistry} from "@keep-network/ecdsa/contracts/api/IWalletRegistry.sol";
19
+
18
20
  import "./IRelay.sol";
19
21
  import "./Deposit.sol";
20
- import "./Redeem.sol";
22
+ import "./Redemption.sol";
21
23
  import "./Fraud.sol";
22
24
  import "./Wallets.sol";
23
25
 
@@ -81,6 +83,11 @@ library BridgeState {
81
83
  // if per single redemption. `movingFundsTxMaxTotalFee` is a total
82
84
  // fee for the entire transaction.
83
85
  uint64 movingFundsTxMaxTotalFee;
86
+ // Time after which the moving funds process can be reported as
87
+ // timed out. It is counted from the moment when the wallet
88
+ // was requested to move their funds and switched to the MovingFunds
89
+ // state. Value in seconds.
90
+ uint32 movingFundsTimeout;
84
91
  // The minimal amount that can be requested for redemption.
85
92
  // Value of this parameter must take into account the value of
86
93
  // `redemptionTreasuryFeeDivisor` and `redemptionTxMaxFee`
@@ -123,7 +130,7 @@ library BridgeState {
123
130
  // successfully
124
131
  // - `notifyRedemptionTimeout` in case the request was reported
125
132
  // to be timed out
126
- mapping(uint256 => Redeem.RedemptionRequest) pendingRedemptions;
133
+ mapping(uint256 => Redemption.RedemptionRequest) pendingRedemptions;
127
134
  // Collection of all timed out redemptions requests indexed by
128
135
  // redemption key built as
129
136
  // `keccak256(walletPubKeyHash | redeemerOutputScript)`. The
@@ -137,7 +144,7 @@ library BridgeState {
137
144
  // - `notifyRedemptionTimeout` which puts the redemption key to this
138
145
  // mapping basing on a timed out request stored previously in
139
146
  // `pendingRedemptions` mapping.
140
- mapping(uint256 => Redeem.RedemptionRequest) timedOutRedemptions;
147
+ mapping(uint256 => Redemption.RedemptionRequest) timedOutRedemptions;
141
148
  // The amount of stake slashed from each member of a wallet for a fraud.
142
149
  uint256 fraudSlashingAmount;
143
150
  // The percentage of the notifier reward from the staking contract
@@ -174,40 +181,245 @@ library BridgeState {
174
181
  // active wallet. Can be unset to the zero value under certain
175
182
  // circumstances.
176
183
  bytes20 activeWalletPubKeyHash;
184
+ // The current number of wallets in the Live state.
185
+ uint32 liveWalletsCount;
186
+ // The maximum BTC amount in satoshi than can be transferred to a single
187
+ // target wallet during the moving funds process.
188
+ uint64 walletMaxBtcTransfer;
177
189
  // Maps the 20-byte wallet public key hash (computed using Bitcoin
178
190
  // HASH160 over the compressed ECDSA public key) to the basic wallet
179
191
  // information like state and pending redemptions value.
180
192
  mapping(bytes20 => Wallets.Wallet) registeredWallets;
181
193
  }
182
194
 
195
+ event DepositParametersUpdated(
196
+ uint64 depositDustThreshold,
197
+ uint64 depositTreasuryFeeDivisor,
198
+ uint64 depositTxMaxFee
199
+ );
200
+
201
+ event RedemptionParametersUpdated(
202
+ uint64 redemptionDustThreshold,
203
+ uint64 redemptionTreasuryFeeDivisor,
204
+ uint64 redemptionTxMaxFee,
205
+ uint256 redemptionTimeout
206
+ );
207
+
208
+ event MovingFundsParametersUpdated(
209
+ uint64 movingFundsTxMaxTotalFee,
210
+ uint32 movingFundsTimeout
211
+ );
212
+
183
213
  event WalletParametersUpdated(
184
214
  uint32 walletCreationPeriod,
185
215
  uint64 walletMinBtcBalance,
186
216
  uint64 walletMaxBtcBalance,
187
- uint32 walletMaxAge
217
+ uint32 walletMaxAge,
218
+ uint64 walletMaxBtcTransfer
219
+ );
220
+
221
+ event FraudParametersUpdated(
222
+ uint256 fraudSlashingAmount,
223
+ uint256 fraudNotifierRewardMultiplier,
224
+ uint256 fraudChallengeDefeatTimeout,
225
+ uint256 fraudChallengeDepositAmount
188
226
  );
189
227
 
228
+ /// @notice Updates parameters of deposits.
229
+ /// @param _depositDustThreshold New value of the deposit dust threshold in
230
+ /// satoshis. It is the minimal amount that can be requested to
231
+ //// deposit. Value of this parameter must take into account the value
232
+ /// of `depositTreasuryFeeDivisor` and `depositTxMaxFee` parameters
233
+ /// in order to make requests that can incur the treasury and
234
+ /// transaction fee and still satisfy the depositor
235
+ /// @param _depositTreasuryFeeDivisor New value of the treasury fee divisor.
236
+ /// It is the divisor used to compute the treasury fee taken from
237
+ /// each deposit and transferred to the treasury upon sweep proof
238
+ /// submission. That fee is computed as follows:
239
+ /// `treasuryFee = depositedAmount / depositTreasuryFeeDivisor`
240
+ /// For example, if the treasury fee needs to be 2% of each deposit,
241
+ /// the `depositTreasuryFeeDivisor` should be set to `50`
242
+ /// because `1/50 = 0.02 = 2%`
243
+ /// @param _depositTxMaxFee New value of the deposit tx max fee in satoshis.
244
+ /// It is the maximum amount of BTC transaction fee that can
245
+ /// be incurred by each swept deposit being part of the given sweep
246
+ /// transaction. If the maximum BTC transaction fee is exceeded,
247
+ /// such transaction is considered a fraud
248
+ /// @dev Requirements:
249
+ /// - Deposit dust threshold must be greater than zero
250
+ /// - Deposit treasury fee divisor must be greater than zero
251
+ /// - Deposit transaction max fee must be greater than zero
252
+ function updateDepositParameters(
253
+ Storage storage self,
254
+ uint64 _depositDustThreshold,
255
+ uint64 _depositTreasuryFeeDivisor,
256
+ uint64 _depositTxMaxFee
257
+ ) internal {
258
+ require(
259
+ _depositDustThreshold > 0,
260
+ "Deposit dust threshold must be greater than zero"
261
+ );
262
+
263
+ require(
264
+ _depositTreasuryFeeDivisor > 0,
265
+ "Deposit treasury fee divisor must be greater than zero"
266
+ );
267
+
268
+ require(
269
+ _depositTxMaxFee > 0,
270
+ "Deposit transaction max fee must be greater than zero"
271
+ );
272
+
273
+ self.depositDustThreshold = _depositDustThreshold;
274
+ self.depositTreasuryFeeDivisor = _depositTreasuryFeeDivisor;
275
+ self.depositTxMaxFee = _depositTxMaxFee;
276
+
277
+ emit DepositParametersUpdated(
278
+ _depositDustThreshold,
279
+ _depositTreasuryFeeDivisor,
280
+ _depositTxMaxFee
281
+ );
282
+ }
283
+
284
+ /// @notice Updates parameters of redemptions.
285
+ /// @param _redemptionDustThreshold New value of the redemption dust
286
+ /// threshold in satoshis. It is the minimal amount that can be
287
+ /// requested for redemption. Value of this parameter must take into
288
+ /// account the value of `redemptionTreasuryFeeDivisor` and
289
+ /// `redemptionTxMaxFee` parameters in order to make requests that
290
+ /// can incur the treasury and transaction fee and still satisfy the
291
+ /// redeemer.
292
+ /// @param _redemptionTreasuryFeeDivisor New value of the redemption
293
+ /// treasury fee divisor. It is the divisor used to compute the
294
+ /// treasury fee taken from each redemption request and transferred
295
+ /// to the treasury upon successful request finalization. That fee is
296
+ /// computed as follows:
297
+ /// `treasuryFee = requestedAmount / redemptionTreasuryFeeDivisor`
298
+ /// For example, if the treasury fee needs to be 2% of each
299
+ /// redemption request, the `redemptionTreasuryFeeDivisor` should
300
+ /// be set to `50` because `1/50 = 0.02 = 2%`.
301
+ /// @param _redemptionTxMaxFee New value of the redemption transaction max
302
+ /// fee in satoshis. It is the maximum amount of BTC transaction fee
303
+ /// that can be incurred by each redemption request being part of the
304
+ /// given redemption transaction. If the maximum BTC transaction fee
305
+ /// is exceeded, such transaction is considered a fraud.
306
+ /// This is a per-redemption output max fee for the redemption
307
+ /// transaction.
308
+ /// @param _redemptionTimeout New value of the redemption timeout in seconds.
309
+ /// It is the time after which the redemption request can be reported
310
+ /// as timed out. It is counted from the moment when the redemption
311
+ /// request was created via `requestRedemption` call. Reported timed
312
+ /// out requests are cancelled and locked TBTC is returned to the
313
+ /// redeemer in full amount.
314
+ /// @dev Requirements:
315
+ /// - Redemption dust threshold must be greater than zero
316
+ /// - Redemption treasury fee divisor must be greater than zero
317
+ /// - Redemption transaction max fee must be greater than zero
318
+ /// - Redemption timeout must be greater than zero
319
+ function updateRedemptionParameters(
320
+ Storage storage self,
321
+ uint64 _redemptionDustThreshold,
322
+ uint64 _redemptionTreasuryFeeDivisor,
323
+ uint64 _redemptionTxMaxFee,
324
+ uint256 _redemptionTimeout
325
+ ) internal {
326
+ require(
327
+ _redemptionDustThreshold > 0,
328
+ "Redemption dust threshold must be greater than zero"
329
+ );
330
+
331
+ require(
332
+ _redemptionTreasuryFeeDivisor > 0,
333
+ "Redemption treasury fee divisor must be greater than zero"
334
+ );
335
+
336
+ require(
337
+ _redemptionTxMaxFee > 0,
338
+ "Redemption transaction max fee must be greater than zero"
339
+ );
340
+
341
+ require(
342
+ _redemptionTimeout > 0,
343
+ "Redemption timeout must be greater than zero"
344
+ );
345
+
346
+ self.redemptionDustThreshold = _redemptionDustThreshold;
347
+ self.redemptionTreasuryFeeDivisor = _redemptionTreasuryFeeDivisor;
348
+ self.redemptionTxMaxFee = _redemptionTxMaxFee;
349
+ self.redemptionTimeout = _redemptionTimeout;
350
+
351
+ emit RedemptionParametersUpdated(
352
+ _redemptionDustThreshold,
353
+ _redemptionTreasuryFeeDivisor,
354
+ _redemptionTxMaxFee,
355
+ _redemptionTimeout
356
+ );
357
+ }
358
+
359
+ /// @notice Updates parameters of moving funds.
360
+ /// @param _movingFundsTxMaxTotalFee New value of the moving funds transaction
361
+ /// max total fee in satoshis. It is the maximum amount of the total
362
+ /// BTC transaction fee that is acceptable in a single moving funds
363
+ /// transaction. This is a _total_ max fee for the entire moving
364
+ /// funds transaction.
365
+ /// @param _movingFundsTimeout New value of the moving funds timeout in
366
+ /// seconds. It is the time after which the moving funds process can
367
+ /// be reported as timed out. It is counted from the moment when the
368
+ /// wallet was requested to move their funds and switched to the
369
+ /// MovingFunds state.
370
+ /// @dev Requirements:
371
+ /// - Moving funds transaction max total fee must be greater than zero
372
+ /// - Moving funds timeout must be greater than zero
373
+ function updateMovingFundsParameters(
374
+ Storage storage self,
375
+ uint64 _movingFundsTxMaxTotalFee,
376
+ uint32 _movingFundsTimeout
377
+ ) internal {
378
+ require(
379
+ _movingFundsTxMaxTotalFee > 0,
380
+ "Moving funds transaction max total fee must be greater than zero"
381
+ );
382
+
383
+ require(
384
+ _movingFundsTimeout > 0,
385
+ "Moving funds timeout must be greater than zero"
386
+ );
387
+
388
+ self.movingFundsTxMaxTotalFee = _movingFundsTxMaxTotalFee;
389
+ self.movingFundsTimeout = _movingFundsTimeout;
390
+
391
+ emit MovingFundsParametersUpdated(
392
+ _movingFundsTxMaxTotalFee,
393
+ _movingFundsTimeout
394
+ );
395
+ }
396
+
190
397
  /// @notice Updates parameters of wallets.
191
398
  /// @param _walletCreationPeriod New value of the wallet creation period in
192
399
  /// seconds, determines how frequently a new wallet creation can be
193
400
  /// requested
194
401
  /// @param _walletMinBtcBalance New value of the wallet minimum BTC balance
195
- /// in sathoshis, used to decide about wallet creation or closing
402
+ /// in satoshi, used to decide about wallet creation or closing
196
403
  /// @param _walletMaxBtcBalance New value of the wallet maximum BTC balance
197
- /// in sathoshis, used to decide about wallet creation
404
+ /// in satoshi, used to decide about wallet creation
198
405
  /// @param _walletMaxAge New value of the wallet maximum age in seconds,
199
406
  /// indicates the maximum age of a wallet in seconds, after which
200
407
  /// the wallet moving funds process can be requested
408
+ /// @param _walletMaxBtcTransfer New value of the wallet maximum BTC transfer
409
+ /// in satoshi, determines the maximum amount that can be transferred
410
+ // to a single target wallet during the moving funds process
201
411
  /// @dev Requirements:
202
412
  /// - Wallet minimum BTC balance must be greater than zero
203
413
  /// - Wallet maximum BTC balance must be greater than the wallet
204
414
  /// minimum BTC balance
415
+ /// - Wallet maximum BTC transfer must be greater than zero
205
416
  function updateWalletParameters(
206
417
  Storage storage self,
207
418
  uint32 _walletCreationPeriod,
208
419
  uint64 _walletMinBtcBalance,
209
420
  uint64 _walletMaxBtcBalance,
210
- uint32 _walletMaxAge
421
+ uint32 _walletMaxAge,
422
+ uint64 _walletMaxBtcTransfer
211
423
  ) internal {
212
424
  require(
213
425
  _walletMinBtcBalance > 0,
@@ -217,35 +429,70 @@ library BridgeState {
217
429
  _walletMaxBtcBalance > _walletMinBtcBalance,
218
430
  "Wallet maximum BTC balance must be greater than the minimum"
219
431
  );
432
+ require(
433
+ _walletMaxBtcTransfer > 0,
434
+ "Wallet maximum BTC transfer must be greater than zero"
435
+ );
220
436
 
221
437
  self.walletCreationPeriod = _walletCreationPeriod;
222
438
  self.walletMinBtcBalance = _walletMinBtcBalance;
223
439
  self.walletMaxBtcBalance = _walletMaxBtcBalance;
224
440
  self.walletMaxAge = _walletMaxAge;
441
+ self.walletMaxBtcTransfer = _walletMaxBtcTransfer;
225
442
 
226
443
  emit WalletParametersUpdated(
227
444
  _walletCreationPeriod,
228
445
  _walletMinBtcBalance,
229
446
  _walletMaxBtcBalance,
230
- _walletMaxAge
447
+ _walletMaxAge,
448
+ _walletMaxBtcTransfer
231
449
  );
232
450
  }
233
451
 
234
- // TODO: Is it the right place for this function? Should we move it to Bridge?
235
- /// @notice Determines the current Bitcoin SPV proof difficulty context.
236
- /// @return proofDifficulty Bitcoin proof difficulty context.
237
- function proofDifficultyContext(Storage storage self)
238
- internal
239
- view
240
- returns (BitcoinTx.ProofDifficulty memory proofDifficulty)
241
- {
242
- IRelay relay = self.relay;
243
- proofDifficulty.currentEpochDifficulty = relay
244
- .getCurrentEpochDifficulty();
245
- proofDifficulty.previousEpochDifficulty = relay
246
- .getPrevEpochDifficulty();
247
- proofDifficulty.difficultyFactor = self.txProofDifficultyFactor;
248
-
249
- return proofDifficulty;
452
+ /// @notice Updates parameters related to frauds.
453
+ /// @param _fraudSlashingAmount New value of the fraud slashing amount in T,
454
+ /// it is the amount slashed from each wallet member for committing
455
+ /// a fraud
456
+ /// @param _fraudNotifierRewardMultiplier New value of the fraud notifier
457
+ /// reward multiplier as percentage, it determines the percentage of
458
+ /// the notifier reward from the staking contact the notifier of
459
+ /// a fraud receives. The value must be in the range [0, 100]
460
+ /// @param _fraudChallengeDefeatTimeout New value of the challenge defeat
461
+ /// timeout in seconds, it is the amount of time the wallet has to
462
+ /// defeat a fraud challenge. The value must be greater than zero
463
+ /// @param _fraudChallengeDepositAmount New value of the fraud challenge
464
+ /// deposit amount in wei, it is the amount of ETH the party
465
+ /// challenging the wallet for fraud needs to deposit
466
+ /// @dev Requirements:
467
+ /// - Fraud notifier reward multiplier must be in the range [0, 100]
468
+ /// - Fraud challenge defeat timeout must be greater than 0
469
+ function updateFraudParameters(
470
+ Storage storage self,
471
+ uint256 _fraudSlashingAmount,
472
+ uint256 _fraudNotifierRewardMultiplier,
473
+ uint256 _fraudChallengeDefeatTimeout,
474
+ uint256 _fraudChallengeDepositAmount
475
+ ) internal {
476
+ require(
477
+ _fraudNotifierRewardMultiplier <= 100,
478
+ "Fraud notifier reward multiplier must be in the range [0, 100]"
479
+ );
480
+
481
+ require(
482
+ _fraudChallengeDefeatTimeout > 0,
483
+ "Fraud challenge defeat timeout must be greater than zero"
484
+ );
485
+
486
+ self.fraudSlashingAmount = _fraudSlashingAmount;
487
+ self.fraudNotifierRewardMultiplier = _fraudNotifierRewardMultiplier;
488
+ self.fraudChallengeDefeatTimeout = _fraudChallengeDefeatTimeout;
489
+ self.fraudChallengeDepositAmount = _fraudChallengeDepositAmount;
490
+
491
+ emit FraudParametersUpdated(
492
+ _fraudSlashingAmount,
493
+ _fraudNotifierRewardMultiplier,
494
+ _fraudChallengeDefeatTimeout,
495
+ _fraudChallengeDepositAmount
496
+ );
250
497
  }
251
498
  }
@@ -22,6 +22,28 @@ import "./BitcoinTx.sol";
22
22
  import "./BridgeState.sol";
23
23
  import "./Wallets.sol";
24
24
 
25
+ /// @title Bridge deposit
26
+ /// @notice The library handles the logic for revealing Bitcoin deposits to
27
+ /// the Bridge.
28
+ /// @dev The depositor puts together a P2SH or P2WSH address to deposit the
29
+ /// funds. This script is unique to each depositor and looks like this:
30
+ ///
31
+ /// ```
32
+ /// <depositorAddress> DROP
33
+ /// <blindingFactor> DROP
34
+ /// DUP HASH160 <walletPubKeyHash> EQUAL
35
+ /// IF
36
+ /// CHECKSIG
37
+ /// ELSE
38
+ /// DUP HASH160 <refundPubkeyHash> EQUALVERIFY
39
+ /// <refundLocktime> CHECKLOCKTIMEVERIFY DROP
40
+ /// CHECKSIG
41
+ /// ENDIF
42
+ /// ```
43
+ ///
44
+ /// Since each depositor has their own Ethereum address and their own
45
+ /// secret blinding factor, each depositor’s script is unique, and the hash
46
+ /// of each depositor’s script is unique.
25
47
  library Deposit {
26
48
  using BTCUtils for bytes;
27
49
  using BytesLib for bytes;
@@ -127,7 +149,7 @@ library Deposit {
127
149
  require(
128
150
  self.registeredWallets[reveal.walletPubKeyHash].state ==
129
151
  Wallets.WalletState.Live,
130
- "Wallet is not in Live state"
152
+ "Wallet must be in Live state"
131
153
  );
132
154
 
133
155
  require(
@@ -228,7 +250,7 @@ library Deposit {
228
250
  deposit.treasuryFee = self.depositTreasuryFeeDivisor > 0
229
251
  ? fundingOutputAmount / self.depositTreasuryFeeDivisor
230
252
  : 0;
231
-
253
+ // slither-disable-next-line reentrancy-events
232
254
  emit DepositRevealed(
233
255
  fundingTxHash,
234
256
  reveal.fundingOutputIndex,
@@ -1,3 +1,18 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+
1
16
  pragma solidity ^0.8.9;
2
17
 
3
18
  import "@keep-network/bitcoin-spv-sol/contracts/BytesLib.sol";
@@ -24,7 +24,26 @@ import "./EcdsaLib.sol";
24
24
  import "./BridgeState.sol";
25
25
  import "./Wallets.sol";
26
26
 
27
+ /// @title Bridge fraud
28
+ /// @notice The library handles the logic for challenging Bridge wallets that
29
+ /// committed fraud.
30
+ /// @dev Anyone can submit a fraud challenge indicating that a UTXO being under
31
+ /// the wallet control was unlocked by the wallet but was not used
32
+ /// according to the protocol rules. That means the wallet signed
33
+ /// a transaction input pointing to that UTXO and there is a unique
34
+ /// sighash and signature pair associated with that input.
35
+ ///
36
+ /// In order to defeat the challenge, the same wallet public key and
37
+ /// signature must be provided as were used to calculate the sighash during
38
+ /// the challenge. The wallet provides the preimage which produces sighash
39
+ /// used to generate the ECDSA signature that is the subject of the fraud
40
+ /// claim. The fraud challenge defeat attempt will only succeed if the
41
+ /// inputs in the preimage are considered honestly spent by the wallet.
42
+ /// Therefore the transaction spending the UTXO must be proven in the
43
+ /// Bridge before a challenge defeat is called.
27
44
  library Fraud {
45
+ using Wallets for BridgeState.Storage;
46
+
28
47
  using BytesLib for bytes;
29
48
  using BTCUtils for bytes;
30
49
  using BTCUtils for uint32;
@@ -41,32 +60,18 @@ library Fraud {
41
60
  bool resolved;
42
61
  }
43
62
 
44
- event FraudSlashingAmountUpdated(uint256 newFraudSlashingAmount);
45
-
46
- event FraudNotifierRewardMultiplierUpdated(
47
- uint256 newFraudNotifierRewardMultiplier
48
- );
49
-
50
- event FraudChallengeDefeatTimeoutUpdated(
51
- uint256 newFraudChallengeDefeatTimeout
52
- );
53
-
54
- event FraudChallengeDepositAmountUpdated(
55
- uint256 newFraudChallengeDepositAmount
56
- );
57
-
58
63
  event FraudChallengeSubmitted(
59
- bytes20 walletPublicKeyHash,
64
+ bytes20 walletPubKeyHash,
60
65
  bytes32 sighash,
61
66
  uint8 v,
62
67
  bytes32 r,
63
68
  bytes32 s
64
69
  );
65
70
 
66
- event FraudChallengeDefeated(bytes20 walletPublicKeyHash, bytes32 sighash);
71
+ event FraudChallengeDefeated(bytes20 walletPubKeyHash, bytes32 sighash);
67
72
 
68
73
  event FraudChallengeDefeatTimedOut(
69
- bytes20 walletPublicKeyHash,
74
+ bytes20 walletPubKeyHash,
70
75
  bytes32 sighash
71
76
  );
72
77
 
@@ -95,8 +100,8 @@ library Fraud {
95
100
  /// for reference
96
101
  /// @param signature Bitcoin signature in the R/S/V format
97
102
  /// @dev Requirements:
98
- /// - Wallet behind `walletPublicKey` must be in `Live` or `MovingFunds`
99
- /// state
103
+ /// - Wallet behind `walletPublicKey` must be in Live or MovingFunds
104
+ /// or Closing state
100
105
  /// - The challenger must send appropriate amount of ETH used as
101
106
  /// fraud challenge deposit
102
107
  /// - The signature (represented by r, s and v) must be generated by
@@ -136,8 +141,9 @@ library Fraud {
136
141
 
137
142
  require(
138
143
  wallet.state == Wallets.WalletState.Live ||
139
- wallet.state == Wallets.WalletState.MovingFunds,
140
- "Wallet is neither in Live nor MovingFunds state"
144
+ wallet.state == Wallets.WalletState.MovingFunds ||
145
+ wallet.state == Wallets.WalletState.Closing,
146
+ "Wallet must be in Live or MovingFunds or Closing state"
141
147
  );
142
148
 
143
149
  uint256 challengeKey = uint256(
@@ -152,7 +158,7 @@ library Fraud {
152
158
  /* solhint-disable-next-line not-rely-on-time */
153
159
  challenge.reportedAt = uint32(block.timestamp);
154
160
  challenge.resolved = false;
155
-
161
+ // slither-disable-next-line reentrancy-events
156
162
  emit FraudChallengeSubmitted(
157
163
  walletPubKeyHash,
158
164
  sighash,
@@ -229,7 +235,7 @@ library Fraud {
229
235
 
230
236
  // Send the ether deposited by the challenger to the treasury
231
237
  /* solhint-disable avoid-low-level-calls */
232
- // slither-disable-next-line low-level-calls
238
+ // slither-disable-next-line low-level-calls,unchecked-lowlevel,arbitrary-send
233
239
  self.treasury.call{gas: 100000, value: challenge.depositAmount}("");
234
240
  /* solhint-enable avoid-low-level-calls */
235
241
 
@@ -238,7 +244,7 @@ library Fraud {
238
244
  walletPublicKey.slice32(32)
239
245
  );
240
246
  bytes20 walletPubKeyHash = compressedWalletPublicKey.hash160View();
241
-
247
+ // slither-disable-next-line reentrancy-events
242
248
  emit FraudChallengeDefeated(walletPubKeyHash, sighash);
243
249
  }
244
250
 
@@ -261,9 +267,11 @@ library Fraud {
261
267
  /// the transaction input the signature is produced for. See BIP-143
262
268
  /// for reference
263
269
  /// @dev Requirements:
264
- /// - `walletPublicKey` and `sighash` must identify an open fraud
270
+ /// - The wallet must be in the Live or MovingFunds or Closing or
271
+ /// Terminated state
272
+ /// - The `walletPublicKey` and `sighash` must identify an open fraud
265
273
  /// challenge
266
- /// - the amount of time indicated by `challengeDefeatTimeout` must pass
274
+ /// - The amount of time indicated by `challengeDefeatTimeout` must pass
267
275
  /// after the challenge was reported
268
276
  function notifyFraudChallengeDefeatTimeout(
269
277
  BridgeState.Storage storage self,
@@ -275,11 +283,14 @@ library Fraud {
275
283
  );
276
284
 
277
285
  FraudChallenge storage challenge = self.fraudChallenges[challengeKey];
286
+
278
287
  require(challenge.reportedAt > 0, "Fraud challenge does not exist");
288
+
279
289
  require(
280
290
  !challenge.resolved,
281
291
  "Fraud challenge has already been resolved"
282
292
  );
293
+
283
294
  require(
284
295
  /* solhint-disable-next-line not-rely-on-time */
285
296
  block.timestamp >=
@@ -287,14 +298,10 @@ library Fraud {
287
298
  "Fraud challenge defeat period did not time out yet"
288
299
  );
289
300
 
290
- // TODO: Call notifyFraud from Wallets library
291
- // TODO: Reward the challenger
292
-
293
301
  challenge.resolved = true;
294
-
295
302
  // Return the ether deposited by the challenger
296
303
  /* solhint-disable avoid-low-level-calls */
297
- // slither-disable-next-line low-level-calls
304
+ // slither-disable-next-line low-level-calls,unchecked-lowlevel
298
305
  challenge.challenger.call{gas: 100000, value: challenge.depositAmount}(
299
306
  ""
300
307
  );
@@ -305,7 +312,33 @@ library Fraud {
305
312
  walletPublicKey.slice32(32)
306
313
  );
307
314
  bytes20 walletPubKeyHash = compressedWalletPublicKey.hash160View();
315
+ Wallets.WalletState walletState = self
316
+ .registeredWallets[walletPubKeyHash]
317
+ .state;
318
+
319
+ if (
320
+ walletState == Wallets.WalletState.Live ||
321
+ walletState == Wallets.WalletState.MovingFunds ||
322
+ walletState == Wallets.WalletState.Closing
323
+ ) {
324
+ self.terminateWallet(walletPubKeyHash);
325
+
326
+ // TODO: Perform slashing of the wallet operators, reward the
327
+ // challenger, and add unit tests for that.
328
+ } else if (walletState == Wallets.WalletState.Terminated) {
329
+ // This is a special case when the wallet was already terminated
330
+ // due to a previous deliberate protocol violation. In that
331
+ // case, this function should be still callable for other fraud
332
+ // challenges timeouts in order to let the challenger unlock its
333
+ // ETH deposit back. However, the wallet termination logic is
334
+ // not called and the challenger is not rewarded.
335
+ } else {
336
+ revert(
337
+ "Wallet must be in Live or MovingFunds or Closing or Terminated state"
338
+ );
339
+ }
308
340
 
341
+ // slither-disable-next-line reentrancy-events
309
342
  emit FraudChallengeDefeatTimedOut(walletPubKeyHash, sighash);
310
343
  }
311
344