@keep-network/tbtc-v2 0.1.1-dev.25 → 0.1.1-dev.26

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.
@@ -25,6 +25,7 @@ import "../bank/Bank.sol";
25
25
  import "./BitcoinTx.sol";
26
26
  import "./EcdsaLib.sol";
27
27
  import "./Wallets.sol";
28
+ import "./Frauds.sol";
28
29
 
29
30
  /// @title Interface for the Bitcoin relay
30
31
  /// @notice Contains only the methods needed by tBTC v2. The Bitcoin relay
@@ -61,6 +62,7 @@ contract Bridge is Ownable, EcdsaWalletOwner {
61
62
  using BTCUtils for bytes;
62
63
  using BTCUtils for uint256;
63
64
  using BytesLib for bytes;
65
+ using Frauds for Frauds.Data;
64
66
  using Wallets for Wallets.Data;
65
67
 
66
68
  /// @notice Represents data which must be revealed by the depositor during
@@ -261,6 +263,16 @@ contract Bridge is Ownable, EcdsaWalletOwner {
261
263
  /// validating them before attempting to execute a sweep.
262
264
  mapping(uint256 => DepositRequest) public deposits;
263
265
 
266
+ //TODO: Remember to update this map when implementing transferring funds
267
+ // between wallets (insert the main UTXO that was used as the input).
268
+ /// @notice Collection of main UTXOs that are honestly spent indexed by
269
+ /// keccak256(fundingTxHash | fundingOutputIndex). The fundingTxHash
270
+ /// is bytes32 (ordered as in Bitcoin internally) and
271
+ /// fundingOutputIndex an uint32. A main UTXO is considered honestly
272
+ /// spent if it was used as an input of a transaction that have been
273
+ /// proven in the Bridge.
274
+ mapping(uint256 => bool) public spentMainUTXOs;
275
+
264
276
  /// @notice Collection of all pending redemption requests indexed by
265
277
  /// redemption key built as
266
278
  /// keccak256(walletPubKeyHash | redeemerOutputScript). The
@@ -295,6 +307,10 @@ contract Bridge is Ownable, EcdsaWalletOwner {
295
307
  // slither-disable-next-line uninitialized-state
296
308
  mapping(uint256 => RedemptionRequest) public timedOutRedemptions;
297
309
 
310
+ /// @notice Contains parameters related to frauds and the collection of all
311
+ /// submitted fraud challenges.
312
+ Frauds.Data internal frauds;
313
+
298
314
  /// @notice State related with wallets.
299
315
  Wallets.Data internal wallets;
300
316
 
@@ -331,6 +347,20 @@ contract Bridge is Ownable, EcdsaWalletOwner {
331
347
 
332
348
  event VaultStatusUpdated(address indexed vault, bool isTrusted);
333
349
 
350
+ event FraudSlashingAmountUpdated(uint256 newFraudSlashingAmount);
351
+
352
+ event FraudNotifierRewardMultiplierUpdated(
353
+ uint256 newFraudNotifierRewardMultiplier
354
+ );
355
+
356
+ event FraudChallengeDefeatTimeoutUpdated(
357
+ uint256 newFraudChallengeDefeatTimeout
358
+ );
359
+
360
+ event FraudChallengeDepositAmountUpdated(
361
+ uint256 newFraudChallengeDepositAmount
362
+ );
363
+
334
364
  event DepositRevealed(
335
365
  bytes32 fundingTxHash,
336
366
  uint32 fundingOutputIndex,
@@ -359,6 +389,21 @@ contract Bridge is Ownable, EcdsaWalletOwner {
359
389
  bytes32 redemptionTxHash
360
390
  );
361
391
 
392
+ event FraudChallengeSubmitted(
393
+ bytes20 walletPublicKeyHash,
394
+ bytes32 sighash,
395
+ uint8 v,
396
+ bytes32 r,
397
+ bytes32 s
398
+ );
399
+
400
+ event FraudChallengeDefeated(bytes20 walletPublicKeyHash, bytes32 sighash);
401
+
402
+ event FraudChallengeDefeatTimedOut(
403
+ bytes20 walletPublicKeyHash,
404
+ bytes32 sighash
405
+ );
406
+
362
407
  constructor(
363
408
  address _bank,
364
409
  address _relay,
@@ -385,6 +430,10 @@ contract Bridge is Ownable, EcdsaWalletOwner {
385
430
  redemptionTreasuryFeeDivisor = 2000; // 1/2000 == 5bps == 0.05% == 0.0005
386
431
  redemptionTxMaxFee = 1000; // 1000 satoshi
387
432
  redemptionTimeout = 172800; // 48 hours
433
+ frauds.setSlashingAmount(10000 * 1e18); // 10000 T
434
+ frauds.setNotifierRewardMultiplier(100); // 100%
435
+ frauds.setChallengeDefeatTimeout(7 days);
436
+ frauds.setChallengeDepositAmount(2 ether);
388
437
 
389
438
  // TODO: Revisit initial values.
390
439
  wallets.init(_ecdsaWalletRegistry);
@@ -1033,6 +1082,15 @@ contract Bridge is Ownable, EcdsaWalletOwner {
1033
1082
  // the expected main UTXO.
1034
1083
  info.inputsTotalValue += mainUtxo.txOutputValue;
1035
1084
  mainUtxoFound = true;
1085
+
1086
+ // Main UTXO used as an input, mark it as spent.
1087
+ spentMainUTXOs[
1088
+ uint256(
1089
+ keccak256(
1090
+ abi.encodePacked(outpointTxHash, outpointIndex)
1091
+ )
1092
+ )
1093
+ ] = true;
1036
1094
  } else {
1037
1095
  revert("Unknown input type");
1038
1096
  }
@@ -1398,6 +1456,187 @@ contract Bridge is Ownable, EcdsaWalletOwner {
1398
1456
  bank.transferBalance(treasury, outputsInfo.totalTreasuryFee);
1399
1457
  }
1400
1458
 
1459
+ /// @notice Submits a fraud challenge indicating that a UTXO being under
1460
+ /// wallet control was unlocked by the wallet but was not used
1461
+ /// according to the protocol rules. That means the wallet signed
1462
+ /// a transaction input pointing to that UTXO and there is a unique
1463
+ /// sighash and signature pair associated with that input. This
1464
+ /// function uses those parameters to create a fraud accusation that
1465
+ /// proves a given transaction input unlocking the given UTXO was
1466
+ /// actually signed by the wallet. This function cannot determine
1467
+ /// whether the transaction was actually broadcast and the input was
1468
+ /// consumed in a fraudulent way so it just opens a challenge period
1469
+ /// during which the wallet can defeat the challenge by submitting
1470
+ /// proof of a transaction that consumes the given input according
1471
+ /// to protocol rules. To prevent spurious allegations, the caller
1472
+ /// must deposit ETH that is returned back upon justified fraud
1473
+ /// challenge or confiscated otherwise.
1474
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
1475
+ /// and unprefixed format (64 bytes)
1476
+ /// @param sighash The hash that was used to produce the ECDSA signature
1477
+ /// that is the subject of the fraud claim. This hash is constructed
1478
+ /// by applying double SHA-256 over a serialized subset of the
1479
+ /// transaction. The exact subset used as hash preimage depends on
1480
+ /// the transaction input the signature is produced for. See BIP-143
1481
+ /// for reference
1482
+ /// @param signature Bitcoin signature in the R/S/V format
1483
+ /// @dev Requirements:
1484
+ /// - Wallet behind `walletPubKey` must be in `Live` or `MovingFunds`
1485
+ /// state
1486
+ /// - The challenger must send appropriate amount of ETH used as
1487
+ /// fraud challenge deposit
1488
+ /// - The signature (represented by r, s and v) must be generated by
1489
+ /// the wallet behind `walletPubKey` during signing of `sighash`
1490
+ /// - Wallet can be challenged for the given signature only once
1491
+ /// TODO: Consider using wallet public key in the X/Y form to avoid slicing.
1492
+ function submitFraudChallenge(
1493
+ bytes calldata walletPublicKey,
1494
+ bytes32 sighash,
1495
+ BitcoinTx.RSVSignature calldata signature
1496
+ ) external payable {
1497
+ bytes memory compressedWalletPublicKey = EcdsaLib.compressPublicKey(
1498
+ walletPublicKey.slice32(0),
1499
+ walletPublicKey.slice32(32)
1500
+ );
1501
+ bytes20 walletPubKeyHash = compressedWalletPublicKey.hash160View();
1502
+
1503
+ Wallets.Wallet storage wallet = wallets.registeredWallets[
1504
+ walletPubKeyHash
1505
+ ];
1506
+
1507
+ require(
1508
+ wallet.state == Wallets.WalletState.Live ||
1509
+ wallet.state == Wallets.WalletState.MovingFunds,
1510
+ "Wallet is neither in Live nor MovingFunds state"
1511
+ );
1512
+
1513
+ frauds.submitFraudChallenge(
1514
+ walletPublicKey,
1515
+ walletPubKeyHash,
1516
+ sighash,
1517
+ signature
1518
+ );
1519
+ }
1520
+
1521
+ /// @notice Allows to defeat a pending fraud challenge against a wallet if
1522
+ /// the transaction that spends the UTXO follows the protocol rules.
1523
+ /// In order to defeat the challenge the same `walletPublicKey` and
1524
+ /// signature (represented by `r`, `s` and `v`) must be provided as
1525
+ /// were used in the fraud challenge. Additionally a preimage must
1526
+ /// be provided which was used to calculate the sighash during input
1527
+ /// signing. The fraud challenge defeat attempt will only succeed if
1528
+ /// the inputs in the preimage are considered honestly spent by the
1529
+ /// wallet. Therefore the transaction spending the UTXO must be
1530
+ /// proven in the Bridge before a challenge defeat is called.
1531
+ /// If successfully defeated, the fraud challenge is marked as
1532
+ /// resolved and the amount of ether deposited by the challenger is
1533
+ /// sent to the treasury.
1534
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
1535
+ /// and unprefixed format (64 bytes)
1536
+ /// @param preimage The preimage which produces sighash used to generate the
1537
+ /// ECDSA signature that is the subject of the fraud claim. It is a
1538
+ /// serialized subset of the transaction. The exact subset used as
1539
+ /// the preimage depends on the transaction input the signature is
1540
+ /// produced for. See BIP-143 for reference
1541
+ /// @param witness Flag indicating whether the preimage was produced for a
1542
+ /// witness input. True for witness, false for non-witness input
1543
+ /// @dev Requirements:
1544
+ /// - `walletPublicKey` and `sighash` calculated as `hash256(preimage)`
1545
+ /// must identify an open fraud challenge
1546
+ /// - the preimage must be a valid preimage of a transaction generated
1547
+ /// according to the protocol rules and already proved in the Bridge
1548
+ /// - before a defeat attempt is made the transaction that spends the
1549
+ /// given UTXO must be proven in the Bridge
1550
+ /// TODO: Consider using wallet public key in the X/Y form to avoid slicing.
1551
+ function defeatFraudChallenge(
1552
+ bytes calldata walletPublicKey,
1553
+ bytes calldata preimage,
1554
+ bool witness
1555
+ ) external {
1556
+ uint256 utxoKey = frauds.unwrapChallenge(
1557
+ walletPublicKey,
1558
+ preimage,
1559
+ witness
1560
+ );
1561
+
1562
+ // Check that the UTXO key identifies a correctly spent UTXO.
1563
+ require(
1564
+ deposits[utxoKey].sweptAt > 0 || spentMainUTXOs[utxoKey],
1565
+ "Spent UTXO not found among correctly spent UTXOs"
1566
+ );
1567
+
1568
+ frauds.defeatChallenge(walletPublicKey, preimage, treasury);
1569
+ }
1570
+
1571
+ /// @notice Notifies about defeat timeout for the given fraud challenge.
1572
+ /// Can be called only if there was a fraud challenge identified by
1573
+ /// the provided `walletPublicKey` and `sighash` and it was not
1574
+ /// defeated on time. The amount of time that needs to pass after
1575
+ /// a fraud challenge is reported is indicated by the
1576
+ /// `challengeDefeatTimeout`. After a successful fraud challenge
1577
+ /// defeat timeout notification the fraud challenge is marked as
1578
+ /// resolved, the stake of each operator is slashed, the ether
1579
+ /// deposited is returned to the challenger and the challenger is
1580
+ /// rewarded.
1581
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
1582
+ /// and unprefixed format (64 bytes)
1583
+ /// @param sighash The hash that was used to produce the ECDSA signature
1584
+ /// that is the subject of the fraud claim. This hash is constructed
1585
+ /// by applying double SHA-256 over a serialized subset of the
1586
+ /// transaction. The exact subset used as hash preimage depends on
1587
+ /// the transaction input the signature is produced for. See BIP-143
1588
+ /// for reference
1589
+ /// @dev Requirements:
1590
+ /// - `walletPublicKey`and `sighash` must identify an open fraud
1591
+ /// challenge
1592
+ /// - the amount of time indicated by `challengeDefeatTimeout` must
1593
+ /// pass after the challenge was reported
1594
+ /// TODO: Consider using wallet public key in the X/Y form to avoid slicing.
1595
+ function notifyFraudChallengeDefeatTimeout(
1596
+ bytes calldata walletPublicKey,
1597
+ bytes32 sighash
1598
+ ) external {
1599
+ frauds.notifyFraudChallengeDefeatTimeout(walletPublicKey, sighash);
1600
+ }
1601
+
1602
+ /// @notice Returns parameters used by the `Frauds` library.
1603
+ /// @return slashingAmount Value of the slashing amount
1604
+ /// @return notifierRewardMultiplier Value of the notifier reward multiplier
1605
+ /// @return challengeDefeatTimeout Value of the challenge defeat timeout
1606
+ /// @return challengeDepositAmount Value of the challenge deposit amount
1607
+ function getFraudParameters()
1608
+ external
1609
+ view
1610
+ returns (
1611
+ uint256 slashingAmount,
1612
+ uint256 notifierRewardMultiplier,
1613
+ uint256 challengeDefeatTimeout,
1614
+ uint256 challengeDepositAmount
1615
+ )
1616
+ {
1617
+ slashingAmount = frauds.slashingAmount;
1618
+ notifierRewardMultiplier = frauds.notifierRewardMultiplier;
1619
+ challengeDefeatTimeout = frauds.challengeDefeatTimeout;
1620
+ challengeDepositAmount = frauds.challengeDepositAmount;
1621
+
1622
+ return (
1623
+ slashingAmount,
1624
+ notifierRewardMultiplier,
1625
+ challengeDefeatTimeout,
1626
+ challengeDepositAmount
1627
+ );
1628
+ }
1629
+
1630
+ /// @notice Returns the fraud challenge identified by the given key built
1631
+ /// as keccak256(walletPublicKey|sighash|v|r|s).
1632
+ function fraudChallenges(uint256 challengeKey)
1633
+ external
1634
+ view
1635
+ returns (Frauds.FraudChallenge memory)
1636
+ {
1637
+ return frauds.challenges[challengeKey];
1638
+ }
1639
+
1401
1640
  /// @notice Validates whether the redemption Bitcoin transaction input
1402
1641
  /// vector contains a single input referring to the wallet's main
1403
1642
  /// UTXO. Reverts in case the validation fails.
@@ -1414,7 +1653,7 @@ contract Bridge is Ownable, EcdsaWalletOwner {
1414
1653
  bytes memory redemptionTxInputVector,
1415
1654
  BitcoinTx.UTXO calldata mainUtxo,
1416
1655
  bytes20 walletPubKeyHash
1417
- ) internal view {
1656
+ ) internal {
1418
1657
  // Assert that main UTXO for passed wallet exists in storage.
1419
1658
  bytes32 mainUtxoHash = wallets
1420
1659
  .registeredWallets[walletPubKeyHash]
@@ -1445,6 +1684,15 @@ contract Bridge is Ownable, EcdsaWalletOwner {
1445
1684
  mainUtxo.txOutputIndex == redemptionTxOutpointIndex,
1446
1685
  "Redemption transaction input must point to the wallet's main UTXO"
1447
1686
  );
1687
+
1688
+ // Main UTXO used as an input, mark it as spent.
1689
+ spentMainUTXOs[
1690
+ uint256(
1691
+ keccak256(
1692
+ abi.encodePacked(mainUtxo.txHash, mainUtxo.txOutputIndex)
1693
+ )
1694
+ )
1695
+ ] = true;
1448
1696
  }
1449
1697
 
1450
1698
  /// @notice Processes the Bitcoin redemption transaction input vector. It