@keep-network/tbtc-v2 0.1.1-dev.9 → 0.1.1-dev.92

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 (115) hide show
  1. package/README.adoc +12 -0
  2. package/artifacts/Bank.json +807 -0
  3. package/artifacts/Bridge.json +2300 -0
  4. package/artifacts/Deposit.json +117 -0
  5. package/artifacts/DepositSweep.json +77 -0
  6. package/artifacts/EcdsaDkgValidator.json +532 -0
  7. package/artifacts/EcdsaInactivity.json +156 -0
  8. package/artifacts/EcdsaSortitionPool.json +1004 -0
  9. package/artifacts/Fraud.json +164 -0
  10. package/artifacts/KeepRegistry.json +99 -0
  11. package/artifacts/KeepStake.json +286 -0
  12. package/artifacts/KeepToken.json +711 -0
  13. package/artifacts/KeepTokenStaking.json +483 -0
  14. package/artifacts/MovingFunds.json +249 -0
  15. package/artifacts/NuCypherStakingEscrow.json +256 -0
  16. package/artifacts/NuCypherToken.json +711 -0
  17. package/artifacts/RandomBeaconStub.json +141 -0
  18. package/artifacts/Redemption.json +174 -0
  19. package/artifacts/ReimbursementPool.json +509 -0
  20. package/artifacts/Relay.json +123 -0
  21. package/artifacts/T.json +1148 -0
  22. package/artifacts/TBTC.json +27 -26
  23. package/artifacts/TBTCToken.json +27 -26
  24. package/artifacts/TBTCVault.json +691 -0
  25. package/artifacts/TokenStaking.json +2288 -0
  26. package/artifacts/TokenholderGovernor.json +1795 -0
  27. package/artifacts/TokenholderTimelock.json +1058 -0
  28. package/artifacts/VendingMachine.json +31 -30
  29. package/artifacts/VendingMachineKeep.json +400 -0
  30. package/artifacts/VendingMachineNuCypher.json +400 -0
  31. package/artifacts/WalletRegistry.json +1843 -0
  32. package/artifacts/WalletRegistryGovernance.json +2754 -0
  33. package/artifacts/Wallets.json +186 -0
  34. package/artifacts/solcInputs/2522ad78efeee521d6cb3252a08f9f62.json +314 -0
  35. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  36. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.json +2 -2
  37. package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
  38. package/build/contracts/bank/Bank.sol/Bank.json +27 -4
  39. package/build/contracts/bank/IReceiveBalanceApproval.sol/IReceiveBalanceApproval.dbg.json +4 -0
  40. package/build/contracts/bank/IReceiveBalanceApproval.sol/IReceiveBalanceApproval.json +34 -0
  41. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +1 -1
  42. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +2 -2
  43. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  44. package/build/contracts/bridge/Bridge.sol/Bridge.json +2547 -196
  45. package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +4 -0
  46. package/build/contracts/bridge/BridgeState.sol/BridgeState.json +226 -0
  47. package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +4 -0
  48. package/build/contracts/bridge/Deposit.sol/Deposit.json +72 -0
  49. package/build/contracts/bridge/DepositSweep.sol/DepositSweep.dbg.json +4 -0
  50. package/build/contracts/bridge/DepositSweep.sol/DepositSweep.json +30 -0
  51. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +4 -0
  52. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.json +10 -0
  53. package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +4 -0
  54. package/build/contracts/bridge/Fraud.sol/Fraud.json +86 -0
  55. package/build/contracts/bridge/Heartbeat.sol/Heartbeat.dbg.json +4 -0
  56. package/build/contracts/bridge/Heartbeat.sol/Heartbeat.json +10 -0
  57. package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +4 -0
  58. package/build/contracts/bridge/IRelay.sol/IRelay.json +37 -0
  59. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +4 -0
  60. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +138 -0
  61. package/build/contracts/bridge/Redemption.sol/OutboundTx.dbg.json +4 -0
  62. package/build/contracts/bridge/Redemption.sol/OutboundTx.json +10 -0
  63. package/build/contracts/bridge/Redemption.sol/Redemption.dbg.json +4 -0
  64. package/build/contracts/bridge/Redemption.sol/Redemption.json +92 -0
  65. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  66. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.json +2 -2
  67. package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +4 -0
  68. package/build/contracts/bridge/Wallets.sol/Wallets.json +112 -0
  69. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  70. package/build/contracts/token/TBTC.sol/TBTC.json +2 -2
  71. package/build/contracts/vault/DonationVault.sol/DonationVault.dbg.json +4 -0
  72. package/build/contracts/vault/DonationVault.sol/DonationVault.json +108 -0
  73. package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
  74. package/build/contracts/vault/IVault.sol/IVault.json +24 -1
  75. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
  76. package/build/contracts/vault/TBTCVault.sol/TBTCVault.json +295 -9
  77. package/contracts/GovernanceUtils.sol +4 -4
  78. package/contracts/bank/Bank.sol +119 -57
  79. package/contracts/bank/IReceiveBalanceApproval.sol +45 -0
  80. package/contracts/bridge/BitcoinTx.sol +232 -10
  81. package/contracts/bridge/Bridge.sol +1692 -244
  82. package/contracts/bridge/BridgeState.sol +739 -0
  83. package/contracts/bridge/Deposit.sol +269 -0
  84. package/contracts/bridge/DepositSweep.sol +571 -0
  85. package/contracts/bridge/EcdsaLib.sol +45 -0
  86. package/contracts/bridge/Fraud.sol +604 -0
  87. package/contracts/bridge/Heartbeat.sol +112 -0
  88. package/contracts/bridge/IRelay.sol +28 -0
  89. package/contracts/bridge/MovingFunds.sol +1089 -0
  90. package/contracts/bridge/Redemption.sol +1021 -0
  91. package/contracts/bridge/VendingMachine.sol +2 -2
  92. package/contracts/bridge/Wallets.sol +553 -0
  93. package/contracts/hardhat-dependency-compiler/.hardhat-dependency-compiler +1 -0
  94. package/contracts/hardhat-dependency-compiler/@keep-network/ecdsa/contracts/WalletRegistry.sol +3 -0
  95. package/contracts/hardhat-dependency-compiler/@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol +3 -0
  96. package/contracts/hardhat-dependency-compiler/@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol +3 -0
  97. package/contracts/token/TBTC.sol +1 -1
  98. package/contracts/vault/DonationVault.sol +125 -0
  99. package/contracts/vault/IVault.sol +19 -13
  100. package/contracts/vault/TBTCVault.sol +200 -23
  101. package/deploy/00_resolve_relay.ts +28 -0
  102. package/deploy/{03_transfer_roles.ts → 03_transfer_vending_machine_roles.ts} +1 -1
  103. package/deploy/04_deploy_bank.ts +27 -0
  104. package/deploy/05_deploy_bridge.ts +80 -0
  105. package/deploy/06_deploy_tbtc_vault.ts +30 -0
  106. package/deploy/07_bank_update_bridge.ts +19 -0
  107. package/deploy/08_transfer_bank_ownership.ts +15 -0
  108. package/deploy/09_transfer_tbtc_vault_ownership.ts +15 -0
  109. package/deploy/10_transfer_bridge_governance.ts +20 -0
  110. package/deploy/11_initialize_wallet_owner.ts +18 -0
  111. package/deploy/11_transfer_proxy_admin_ownership.ts +30 -0
  112. package/deploy/12_deploy_proxy_admin_with_deputy.ts +33 -0
  113. package/export.json +15771 -443
  114. package/package.json +34 -26
  115. package/artifacts/solcInputs/58d5b3ee7688835879381470de985d6b.json +0 -128
@@ -13,14 +13,27 @@
13
13
  // ▐████▌ ▐████▌
14
14
  // ▐████▌ ▐████▌
15
15
 
16
- pragma solidity 0.8.4;
16
+ pragma solidity ^0.8.9;
17
17
 
18
- import "@openzeppelin/contracts/access/Ownable.sol";
18
+ import "@keep-network/random-beacon/contracts/Governable.sol";
19
+ import {IWalletOwner as EcdsaWalletOwner} from "@keep-network/ecdsa/contracts/api/IWalletOwner.sol";
19
20
 
20
- import {BTCUtils} from "@keep-network/bitcoin-spv-sol/contracts/BTCUtils.sol";
21
- import {BytesLib} from "@keep-network/bitcoin-spv-sol/contracts/BytesLib.sol";
21
+ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
22
+ import "@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol";
22
23
 
24
+ import "./IRelay.sol";
25
+ import "./BridgeState.sol";
26
+ import "./Deposit.sol";
27
+ import "./DepositSweep.sol";
28
+ import "./Redemption.sol";
23
29
  import "./BitcoinTx.sol";
30
+ import "./EcdsaLib.sol";
31
+ import "./Wallets.sol";
32
+ import "./Fraud.sol";
33
+ import "./MovingFunds.sol";
34
+
35
+ import "../bank/IReceiveBalanceApproval.sol";
36
+ import "../bank/Bank.sol";
24
37
 
25
38
  /// @title Bitcoin Bridge
26
39
  /// @notice Bridge manages BTC deposit and redemption flow and is increasing and
@@ -40,100 +53,245 @@ import "./BitcoinTx.sol";
40
53
  /// the sweep operation is confirmed on the Bitcoin network, the ECDSA
41
54
  /// wallet informs the Bridge about the sweep increasing appropriate
42
55
  /// balances in the Bank.
43
- /// @dev Bridge is an upgradeable component of the Bank.
44
- contract Bridge is Ownable {
45
- using BTCUtils for bytes;
46
- using BytesLib for bytes;
47
-
48
- /// @notice Represents data which must be revealed by the depositor during
49
- /// deposit reveal.
50
- struct RevealInfo {
51
- // Index of the funding output belonging to the funding transaction.
52
- uint8 fundingOutputIndex;
53
- // Ethereum depositor address.
54
- address depositor;
55
- // The blinding factor as 8 bytes. Byte endianness doesn't matter
56
- // as this factor is not interpreted as uint.
57
- bytes8 blindingFactor;
58
- // The compressed Bitcoin public key (33 bytes and 02 or 03 prefix)
59
- // of the deposit's wallet hashed in the HASH160 Bitcoin opcode style.
60
- bytes20 walletPubKeyHash;
61
- // The compressed Bitcoin public key (33 bytes and 02 or 03 prefix)
62
- // that can be used to make the deposit refund after the refund
63
- // locktime passes. Hashed in the HASH160 Bitcoin opcode style.
64
- bytes20 refundPubKeyHash;
65
- // The refund locktime (4-byte LE). Interpreted according to locktime
66
- // parsing rules described in:
67
- // https://developer.bitcoin.org/devguide/transactions.html#locktime-and-sequence-number
68
- // and used with OP_CHECKLOCKTIMEVERIFY opcode as described in:
69
- // https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
70
- bytes4 refundLocktime;
71
- // Address of the Bank vault to which the deposit is routed to.
72
- // Optional, can be 0x0. The vault must be trusted by the Bridge.
73
- address vault;
74
- }
75
-
76
- /// @notice Represents tBTC deposit data.
77
- struct DepositInfo {
78
- // Ethereum depositor address.
79
- address depositor;
80
- // Deposit amount in satoshi (8-byte LE). For example:
81
- // 0.0001 BTC = 10000 satoshi = 0x1027000000000000
82
- bytes8 amount;
83
- // UNIX timestamp the deposit was revealed at.
84
- uint32 revealedAt;
85
- // Address of the Bank vault the deposit is routed to.
86
- // Optional, can be 0x0.
87
- address vault;
88
- }
89
-
90
- /// @notice Indicates if the vault with the given address is trusted or not.
91
- /// Depositors can route their revealed deposits only to trusted
92
- /// vaults and have trusted vaults notified about new deposits as
93
- /// soon as these deposits get swept. Vaults not trusted by the
94
- /// Bridge can still be used by Bank balance owners on their own
95
- /// responsibility - anyone can approve their Bank balance to any
96
- /// address.
97
- mapping(address => bool) public isVaultTrusted;
56
+ /// @dev Bridge is an upgradeable component of the Bank. The order of
57
+ /// functionalities in this contract is: deposit, sweep, redemption,
58
+ /// moving funds, wallet lifecycle, frauds, parameters.
59
+ contract Bridge is
60
+ Governable,
61
+ EcdsaWalletOwner,
62
+ Initializable,
63
+ IReceiveBalanceApproval
64
+ {
65
+ using BridgeState for BridgeState.Storage;
66
+ using Deposit for BridgeState.Storage;
67
+ using DepositSweep for BridgeState.Storage;
68
+ using Redemption for BridgeState.Storage;
69
+ using MovingFunds for BridgeState.Storage;
70
+ using Wallets for BridgeState.Storage;
71
+ using Fraud for BridgeState.Storage;
98
72
 
99
- /// @notice Collection of all unswept deposits indexed by
100
- /// keccak256(fundingTxHash | fundingOutputIndex).
101
- /// The fundingTxHash is LE bytes32 and fundingOutputIndex an uint8.
102
- /// This mapping may contain valid and invalid deposits and the
103
- /// wallet is responsible for validating them before attempting to
104
- /// execute a sweep.
105
- ///
106
- /// TODO: Explore the possibility of storing just a hash of DepositInfo.
107
- mapping(uint256 => DepositInfo) public unswept;
73
+ BridgeState.Storage internal self;
108
74
 
109
75
  event DepositRevealed(
110
76
  bytes32 fundingTxHash,
111
- uint8 fundingOutputIndex,
112
- address depositor,
77
+ uint32 fundingOutputIndex,
78
+ address indexed depositor,
79
+ uint64 amount,
113
80
  bytes8 blindingFactor,
114
- bytes20 walletPubKeyHash,
81
+ bytes20 indexed walletPubKeyHash,
115
82
  bytes20 refundPubKeyHash,
116
83
  bytes4 refundLocktime,
117
84
  address vault
118
85
  );
119
86
 
87
+ event DepositsSwept(bytes20 walletPubKeyHash, bytes32 sweepTxHash);
88
+
89
+ event RedemptionRequested(
90
+ bytes20 indexed walletPubKeyHash,
91
+ bytes redeemerOutputScript,
92
+ address indexed redeemer,
93
+ uint64 requestedAmount,
94
+ uint64 treasuryFee,
95
+ uint64 txMaxFee
96
+ );
97
+
98
+ event RedemptionsCompleted(
99
+ bytes20 indexed walletPubKeyHash,
100
+ bytes32 redemptionTxHash
101
+ );
102
+
103
+ event RedemptionTimedOut(
104
+ bytes20 indexed walletPubKeyHash,
105
+ bytes redeemerOutputScript
106
+ );
107
+
108
+ event WalletMovingFunds(
109
+ bytes32 indexed ecdsaWalletID,
110
+ bytes20 indexed walletPubKeyHash
111
+ );
112
+
113
+ event MovingFundsCommitmentSubmitted(
114
+ bytes20 indexed walletPubKeyHash,
115
+ bytes20[] targetWallets,
116
+ address submitter
117
+ );
118
+
119
+ event MovingFundsTimeoutReset(bytes20 indexed walletPubKeyHash);
120
+
121
+ event MovingFundsCompleted(
122
+ bytes20 indexed walletPubKeyHash,
123
+ bytes32 movingFundsTxHash
124
+ );
125
+
126
+ event MovingFundsTimedOut(bytes20 indexed walletPubKeyHash);
127
+
128
+ event MovingFundsBelowDustReported(bytes20 indexed walletPubKeyHash);
129
+
130
+ event MovedFundsSwept(
131
+ bytes20 indexed walletPubKeyHash,
132
+ bytes32 sweepTxHash
133
+ );
134
+
135
+ event MovedFundsSweepTimedOut(
136
+ bytes20 indexed walletPubKeyHash,
137
+ bytes32 movingFundsTxHash,
138
+ uint32 movingFundsTxOutputIndex
139
+ );
140
+
141
+ event NewWalletRequested();
142
+
143
+ event NewWalletRegistered(
144
+ bytes32 indexed ecdsaWalletID,
145
+ bytes20 indexed walletPubKeyHash
146
+ );
147
+
148
+ event WalletClosing(
149
+ bytes32 indexed ecdsaWalletID,
150
+ bytes20 indexed walletPubKeyHash
151
+ );
152
+
153
+ event WalletClosed(
154
+ bytes32 indexed ecdsaWalletID,
155
+ bytes20 indexed walletPubKeyHash
156
+ );
157
+
158
+ event WalletTerminated(
159
+ bytes32 indexed ecdsaWalletID,
160
+ bytes20 indexed walletPubKeyHash
161
+ );
162
+
163
+ event FraudChallengeSubmitted(
164
+ bytes20 indexed walletPubKeyHash,
165
+ bytes32 sighash,
166
+ uint8 v,
167
+ bytes32 r,
168
+ bytes32 s
169
+ );
170
+
171
+ event FraudChallengeDefeated(
172
+ bytes20 indexed walletPubKeyHash,
173
+ bytes32 sighash
174
+ );
175
+
176
+ event FraudChallengeDefeatTimedOut(
177
+ bytes20 indexed walletPubKeyHash,
178
+ bytes32 sighash
179
+ );
180
+
120
181
  event VaultStatusUpdated(address indexed vault, bool isTrusted);
121
182
 
122
- /// @notice Allows the Governance to mark the given vault address as trusted
123
- /// or no longer trusted. Vaults are not trusted by default.
124
- /// Trusted vault must meet the following criteria:
125
- /// - `IVault.onBalanceIncreased` must have a known, low gas cost.
126
- /// - `IVault.onBalanceIncreased` must never revert.
127
- /// @dev Without restricting reveal only to trusted vaults, malicious
128
- /// vaults not meeting the criteria would be able to nuke sweep proof
129
- /// transactions executed by ECDSA wallet with deposits routed to
130
- /// them.
131
- /// @param vault The address of the vault
132
- /// @param isTrusted flag indicating whether the vault is trusted or not
133
- /// @dev Can only be called by the Governance.
134
- function setVaultStatus(address vault, bool isTrusted) external onlyOwner {
135
- isVaultTrusted[vault] = isTrusted;
136
- emit VaultStatusUpdated(vault, isTrusted);
183
+ event DepositParametersUpdated(
184
+ uint64 depositDustThreshold,
185
+ uint64 depositTreasuryFeeDivisor,
186
+ uint64 depositTxMaxFee
187
+ );
188
+
189
+ event RedemptionParametersUpdated(
190
+ uint64 redemptionDustThreshold,
191
+ uint64 redemptionTreasuryFeeDivisor,
192
+ uint64 redemptionTxMaxFee,
193
+ uint256 redemptionTimeout,
194
+ uint96 redemptionTimeoutSlashingAmount,
195
+ uint256 redemptionTimeoutNotifierRewardMultiplier
196
+ );
197
+
198
+ event MovingFundsParametersUpdated(
199
+ uint64 movingFundsTxMaxTotalFee,
200
+ uint64 movingFundsDustThreshold,
201
+ uint32 movingFundsTimeoutResetDelay,
202
+ uint32 movingFundsTimeout,
203
+ uint96 movingFundsTimeoutSlashingAmount,
204
+ uint256 movingFundsTimeoutNotifierRewardMultiplier,
205
+ uint64 movedFundsSweepTxMaxTotalFee,
206
+ uint32 movedFundsSweepTimeout,
207
+ uint96 movedFundsSweepTimeoutSlashingAmount,
208
+ uint256 movedFundsSweepTimeoutNotifierRewardMultiplier
209
+ );
210
+
211
+ event WalletParametersUpdated(
212
+ uint32 walletCreationPeriod,
213
+ uint64 walletCreationMinBtcBalance,
214
+ uint64 walletCreationMaxBtcBalance,
215
+ uint64 walletClosureMinBtcBalance,
216
+ uint32 walletMaxAge,
217
+ uint64 walletMaxBtcTransfer,
218
+ uint32 walletClosingPeriod
219
+ );
220
+
221
+ event FraudParametersUpdated(
222
+ uint256 fraudChallengeDepositAmount,
223
+ uint256 fraudChallengeDefeatTimeout,
224
+ uint96 fraudSlashingAmount,
225
+ uint256 fraudNotifierRewardMultiplier
226
+ );
227
+
228
+ /// @dev Initializes upgradable contract on deployment.
229
+ /// @param _bank Address of the Bank the Bridge belongs to.
230
+ /// @param _relay Address of the Bitcoin relay providing the current Bitcoin
231
+ /// network difficulty.
232
+ /// @param _treasury Address where the deposit and redemption treasury fees
233
+ /// will be sent to.
234
+ /// @param _ecdsaWalletRegistry Address of the ECDSA Wallet Registry contract.
235
+ /// @param _txProofDifficultyFactor The number of confirmations on the Bitcoin
236
+ /// chain required to successfully evaluate an SPV proof.
237
+ function initialize(
238
+ address _bank,
239
+ address _relay,
240
+ address _treasury,
241
+ address _ecdsaWalletRegistry,
242
+ uint256 _txProofDifficultyFactor
243
+ ) external initializer {
244
+ require(_bank != address(0), "Bank address cannot be zero");
245
+ self.bank = Bank(_bank);
246
+
247
+ require(_relay != address(0), "Relay address cannot be zero");
248
+ self.relay = IRelay(_relay);
249
+
250
+ require(
251
+ _ecdsaWalletRegistry != address(0),
252
+ "ECDSA Wallet Registry address cannot be zero"
253
+ );
254
+ self.ecdsaWalletRegistry = EcdsaWalletRegistry(_ecdsaWalletRegistry);
255
+
256
+ require(_treasury != address(0), "Treasury address cannot be zero");
257
+ self.treasury = _treasury;
258
+
259
+ self.txProofDifficultyFactor = _txProofDifficultyFactor;
260
+
261
+ // TODO: Revisit initial values.
262
+ // https://github.com/keep-network/tbtc-v2/issues/258
263
+ self.depositDustThreshold = 1000000; // 1000000 satoshi = 0.01 BTC
264
+ self.depositTxMaxFee = 10000; // 10000 satoshi
265
+ self.depositTreasuryFeeDivisor = 2000; // 1/2000 == 5bps == 0.05% == 0.0005
266
+ self.redemptionDustThreshold = 1000000; // 1000000 satoshi = 0.01 BTC
267
+ self.redemptionTreasuryFeeDivisor = 2000; // 1/2000 == 5bps == 0.05% == 0.0005
268
+ self.redemptionTxMaxFee = 10000; // 10000 satoshi
269
+ self.redemptionTimeout = 172800; // 48 hours
270
+ self.redemptionTimeoutSlashingAmount = 10000 * 1e18; // 10000 T
271
+ self.redemptionTimeoutNotifierRewardMultiplier = 100; // 100%
272
+ self.movingFundsTxMaxTotalFee = 10000; // 10000 satoshi
273
+ self.movingFundsDustThreshold = 20000; // 20000 satoshi
274
+ self.movingFundsTimeoutResetDelay = 6 days;
275
+ self.movingFundsTimeout = 7 days;
276
+ self.movingFundsTimeoutSlashingAmount = 10000 * 1e18; // 10000 T
277
+ self.movingFundsTimeoutNotifierRewardMultiplier = 100; //100%
278
+ self.movedFundsSweepTxMaxTotalFee = 10000; // 10000 satoshi
279
+ self.movedFundsSweepTimeout = 7 days;
280
+ self.movedFundsSweepTimeoutSlashingAmount = 10000 * 1e18; // 10000 T
281
+ self.movedFundsSweepTimeoutNotifierRewardMultiplier = 100; //100%
282
+ self.fraudChallengeDepositAmount = 2 ether;
283
+ self.fraudChallengeDefeatTimeout = 7 days;
284
+ self.fraudSlashingAmount = 10000 * 1e18; // 10000 T
285
+ self.fraudNotifierRewardMultiplier = 100; // 100%
286
+ self.walletCreationPeriod = 1 weeks;
287
+ self.walletCreationMinBtcBalance = 1e8; // 1 BTC
288
+ self.walletCreationMaxBtcBalance = 100e8; // 100 BTC
289
+ self.walletClosureMinBtcBalance = 5 * 1e7; // 0.5 BTC
290
+ self.walletMaxAge = 26 weeks; // ~6 months
291
+ self.walletMaxBtcTransfer = 10e8; // 10 BTC
292
+ self.walletClosingPeriod = 40 days;
293
+
294
+ _transferGovernance(msg.sender);
137
295
  }
138
296
 
139
297
  /// @notice Used by the depositor to reveal information about their P2(W)SH
@@ -147,12 +305,13 @@ contract Bridge is Ownable {
147
305
  /// outputs. The deposit may be routed to one of the trusted vaults.
148
306
  /// When a deposit is routed to a vault, vault gets notified when
149
307
  /// the deposit gets swept and it may execute the appropriate action.
150
- /// @param fundingTx Bitcoin funding transaction data, see `BitcoinTx.Info`
151
- /// @param reveal Deposit reveal data, see `RevealInfo struct
308
+ /// @param fundingTx Bitcoin funding transaction data, see `BitcoinTx.Info`.
309
+ /// @param reveal Deposit reveal data, see `RevealInfo struct.
152
310
  /// @dev Requirements:
153
- /// - `reveal.vault` must be 0x0 or point to a trusted vault
311
+ /// - `reveal.walletPubKeyHash` must identify a `Live` wallet,
312
+ /// - `reveal.vault` must be 0x0 or point to a trusted vault,
154
313
  /// - `reveal.fundingOutputIndex` must point to the actual P2(W)SH
155
- /// output of the BTC deposit transaction
314
+ /// output of the BTC deposit transaction,
156
315
  /// - `reveal.depositor` must be the Ethereum address used in the
157
316
  /// P2(W)SH BTC deposit transaction,
158
317
  /// - `reveal.blindingFactor` must be the blinding factor used in the
@@ -171,127 +330,9 @@ contract Bridge is Ownable {
171
330
  /// deposit script unlocks to receive their BTC back.
172
331
  function revealDeposit(
173
332
  BitcoinTx.Info calldata fundingTx,
174
- RevealInfo calldata reveal
333
+ Deposit.DepositRevealInfo calldata reveal
175
334
  ) external {
176
- require(
177
- reveal.vault == address(0) || isVaultTrusted[reveal.vault],
178
- "Vault is not trusted"
179
- );
180
-
181
- bytes memory expectedScript =
182
- abi.encodePacked(
183
- hex"14", // Byte length of depositor Ethereum address.
184
- reveal.depositor,
185
- hex"75", // OP_DROP
186
- hex"08", // Byte length of blinding factor value.
187
- reveal.blindingFactor,
188
- hex"75", // OP_DROP
189
- hex"76", // OP_DUP
190
- hex"a9", // OP_HASH160
191
- hex"14", // Byte length of a compressed Bitcoin public key hash.
192
- reveal.walletPubKeyHash,
193
- hex"87", // OP_EQUAL
194
- hex"63", // OP_IF
195
- hex"ac", // OP_CHECKSIG
196
- hex"67", // OP_ELSE
197
- hex"76", // OP_DUP
198
- hex"a9", // OP_HASH160
199
- hex"14", // Byte length of a compressed Bitcoin public key hash.
200
- reveal.refundPubKeyHash,
201
- hex"88", // OP_EQUALVERIFY
202
- hex"04", // Byte length of refund locktime value.
203
- reveal.refundLocktime,
204
- hex"b1", // OP_CHECKLOCKTIMEVERIFY
205
- hex"75", // OP_DROP
206
- hex"ac", // OP_CHECKSIG
207
- hex"68" // OP_ENDIF
208
- );
209
-
210
- bytes memory fundingOutput =
211
- fundingTx.outputVector.extractOutputAtIndex(
212
- reveal.fundingOutputIndex
213
- );
214
- bytes memory fundingOutputHash = fundingOutput.extractHash();
215
-
216
- if (fundingOutputHash.length == 20) {
217
- // A 20-byte output hash is used by P2SH. That hash is constructed
218
- // by applying OP_HASH160 on the locking script. A 20-byte output
219
- // hash is used as well by P2PKH and P2WPKH (OP_HASH160 on the
220
- // public key). However, since we compare the actual output hash
221
- // with an expected locking script hash, this check will succeed only
222
- // for P2SH transaction type with expected script hash value. For
223
- // P2PKH and P2WPKH, it will fail on the output hash comparison with
224
- // the expected locking script hash.
225
- require(
226
- keccak256(fundingOutputHash) ==
227
- keccak256(expectedScript.hash160()),
228
- "Wrong 20-byte script hash"
229
- );
230
- } else if (fundingOutputHash.length == 32) {
231
- // A 32-byte output hash is used by P2WSH. That hash is constructed
232
- // by applying OP_SHA256 on the locking script.
233
- require(
234
- fundingOutputHash.toBytes32() == sha256(expectedScript),
235
- "Wrong 32-byte script hash"
236
- );
237
- } else {
238
- revert("Wrong script hash length");
239
- }
240
-
241
- // Resulting TX hash is in native Bitcoin little-endian format.
242
- bytes32 fundingTxHash =
243
- abi
244
- .encodePacked(
245
- fundingTx
246
- .version,
247
- fundingTx
248
- .inputVector,
249
- fundingTx
250
- .outputVector,
251
- fundingTx
252
- .locktime
253
- )
254
- .hash256();
255
-
256
- DepositInfo storage deposit =
257
- unswept[
258
- uint256(
259
- keccak256(
260
- abi.encodePacked(
261
- fundingTxHash,
262
- reveal.fundingOutputIndex
263
- )
264
- )
265
- )
266
- ];
267
- require(deposit.revealedAt == 0, "Deposit already revealed");
268
-
269
- bytes8 fundingOutputAmount;
270
- /* solhint-disable-next-line no-inline-assembly */
271
- assembly {
272
- // First 8 bytes (little-endian) of the funding output represents
273
- // its value. To take the value, we need to jump over the first
274
- // word determining the array length, load the array, and trim it
275
- // by putting it to a bytes8.
276
- fundingOutputAmount := mload(add(fundingOutput, 32))
277
- }
278
-
279
- deposit.amount = fundingOutputAmount;
280
- deposit.depositor = reveal.depositor;
281
- /* solhint-disable-next-line not-rely-on-time */
282
- deposit.revealedAt = uint32(block.timestamp);
283
- deposit.vault = reveal.vault;
284
-
285
- emit DepositRevealed(
286
- fundingTxHash,
287
- reveal.fundingOutputIndex,
288
- reveal.depositor,
289
- reveal.blindingFactor,
290
- reveal.walletPubKeyHash,
291
- reveal.refundPubKeyHash,
292
- reveal.refundLocktime,
293
- reveal.vault
294
- );
335
+ self.revealDeposit(fundingTx, reveal);
295
336
  }
296
337
 
297
338
  /// @notice Used by the wallet to prove the BTC deposit sweep transaction
@@ -306,40 +347,1447 @@ contract Bridge is Ownable {
306
347
  ///
307
348
  /// It is possible to prove the given sweep only one time.
308
349
  /// @param sweepTx Bitcoin sweep transaction data.
309
- /// @param merkleProof The merkle proof of transaction inclusion in a block.
310
- /// @param txIndexInBlock Transaction index in the block (0-indexed).
311
- /// @param bitcoinHeaders Single bytestring of 80-byte bitcoin headers,
312
- /// lowest height first.
313
- function sweep(
350
+ /// @param sweepProof Bitcoin sweep proof data.
351
+ /// @param mainUtxo Data of the wallet's main UTXO, as currently known on
352
+ /// the Ethereum chain. If no main UTXO exists for the given wallet,
353
+ /// this parameter is ignored.
354
+ /// @param vault Optional address of the vault where all swept deposits
355
+ /// should be routed to. All deposits swept as part of the transaction
356
+ /// must have their `vault` parameters set to the same address.
357
+ /// If this parameter is set to an address of a trusted vault, swept
358
+ /// deposits are routed to that vault.
359
+ /// If this parameter is set to the zero address or to an address
360
+ /// of a non-trusted vault, swept deposits are not routed to a
361
+ /// vault but depositors' balances are increased in the Bank
362
+ /// individually.
363
+ /// @dev Requirements:
364
+ /// - `sweepTx` components must match the expected structure. See
365
+ /// `BitcoinTx.Info` docs for reference. Their values must exactly
366
+ /// correspond to appropriate Bitcoin transaction fields to produce
367
+ /// a provable transaction hash,
368
+ /// - The `sweepTx` should represent a Bitcoin transaction with 1..n
369
+ /// inputs. If the wallet has no main UTXO, all n inputs should
370
+ /// correspond to P2(W)SH revealed deposits UTXOs. If the wallet has
371
+ /// an existing main UTXO, one of the n inputs must point to that
372
+ /// main UTXO and remaining n-1 inputs should correspond to P2(W)SH
373
+ /// revealed deposits UTXOs. That transaction must have only
374
+ /// one P2(W)PKH output locking funds on the 20-byte wallet public
375
+ /// key hash,
376
+ /// - All revealed deposits that are swept by `sweepTx` must have
377
+ /// their `vault` parameters set to the same address as the address
378
+ /// passed in the `vault` function parameter,
379
+ /// - `sweepProof` components must match the expected structure. See
380
+ /// `BitcoinTx.Proof` docs for reference. The `bitcoinHeaders`
381
+ /// field must contain a valid number of block headers, not less
382
+ /// than the `txProofDifficultyFactor` contract constant,
383
+ /// - `mainUtxo` components must point to the recent main UTXO
384
+ /// of the given wallet, as currently known on the Ethereum chain.
385
+ /// If there is no main UTXO, this parameter is ignored.
386
+ function submitDepositSweepProof(
314
387
  BitcoinTx.Info calldata sweepTx,
315
- bytes memory merkleProof,
316
- uint256 txIndexInBlock,
317
- bytes memory bitcoinHeaders
388
+ BitcoinTx.Proof calldata sweepProof,
389
+ BitcoinTx.UTXO calldata mainUtxo,
390
+ address vault
391
+ ) external {
392
+ self.submitDepositSweepProof(sweepTx, sweepProof, mainUtxo, vault);
393
+ }
394
+
395
+ /// @notice Requests redemption of the given amount from the specified
396
+ /// wallet to the redeemer Bitcoin output script. Handles the
397
+ /// simplest case in which the redeemer's balance is decreased in
398
+ /// the Bank.
399
+ /// @param walletPubKeyHash The 20-byte wallet public key hash (computed
400
+ /// using Bitcoin HASH160 over the compressed ECDSA public key).
401
+ /// @param mainUtxo Data of the wallet's main UTXO, as currently known on
402
+ /// the Ethereum chain.
403
+ /// @param redeemerOutputScript The redeemer's length-prefixed output
404
+ /// script (P2PKH, P2WPKH, P2SH or P2WSH) that will be used to lock
405
+ /// redeemed BTC.
406
+ /// @param amount Requested amount in satoshi. This is also the Bank balance
407
+ /// that is taken from the `balanceOwner` upon request.
408
+ /// Once the request is handled, the actual amount of BTC locked
409
+ /// on the redeemer output script will be always lower than this value
410
+ /// since the treasury and Bitcoin transaction fees must be incurred.
411
+ /// The minimal amount satisfying the request can be computed as:
412
+ /// `amount - (amount / redemptionTreasuryFeeDivisor) - redemptionTxMaxFee`.
413
+ /// Fees values are taken at the moment of request creation.
414
+ /// @dev Requirements:
415
+ /// - Wallet behind `walletPubKeyHash` must be live,
416
+ /// - `mainUtxo` components must point to the recent main UTXO
417
+ /// of the given wallet, as currently known on the Ethereum chain,
418
+ /// - `redeemerOutputScript` must be a proper Bitcoin script,
419
+ /// - `redeemerOutputScript` cannot have wallet PKH as payload,
420
+ /// - `amount` must be above or equal the `redemptionDustThreshold`,
421
+ /// - Given `walletPubKeyHash` and `redeemerOutputScript` pair can be
422
+ /// used for only one pending request at the same time,
423
+ /// - Wallet must have enough Bitcoin balance to process the request,
424
+ /// - Redeemer must make an allowance in the Bank that the Bridge
425
+ /// contract can spend the given `amount`.
426
+ function requestRedemption(
427
+ bytes20 walletPubKeyHash,
428
+ BitcoinTx.UTXO calldata mainUtxo,
429
+ bytes calldata redeemerOutputScript,
430
+ uint64 amount
431
+ ) external {
432
+ self.requestRedemption(
433
+ walletPubKeyHash,
434
+ mainUtxo,
435
+ msg.sender,
436
+ redeemerOutputScript,
437
+ amount
438
+ );
439
+ }
440
+
441
+ /// @notice Requests redemption of the given amount from the specified
442
+ /// wallet to the redeemer Bitcoin output script. Used by
443
+ /// `Bank.approveBalanceAndCall`. Can handle more complex cases
444
+ /// where balance owner may be someone else than the redeemer.
445
+ /// For example, vault redeeming its balance for some depositor.
446
+ /// @param balanceOwner The address of the Bank balance owner whose balance
447
+ /// is getting redeemed.
448
+ /// @param amount Requested amount in satoshi. This is also the Bank balance
449
+ /// that is taken from the `balanceOwner` upon request.
450
+ /// Once the request is handled, the actual amount of BTC locked
451
+ /// on the redeemer output script will be always lower than this value
452
+ /// since the treasury and Bitcoin transaction fees must be incurred.
453
+ /// The minimal amount satisfying the request can be computed as:
454
+ /// `amount - (amount / redemptionTreasuryFeeDivisor) - redemptionTxMaxFee`.
455
+ /// Fees values are taken at the moment of request creation.
456
+ /// @param redemptionData ABI-encoded redemption data:
457
+ /// [
458
+ /// address redeemer,
459
+ /// bytes20 walletPubKeyHash,
460
+ /// bytes32 mainUtxoTxHash,
461
+ /// uint32 mainUtxoTxOutputIndex,
462
+ /// uint64 mainUtxoTxOutputValue,
463
+ /// bytes redeemerOutputScript
464
+ /// ]
465
+ ///
466
+ /// - redeemer: The Ethereum address of the redeemer who will be able
467
+ /// to claim Bank balance if anything goes wrong during the redemption.
468
+ /// In the most basic case, when someone redeems their balance
469
+ /// from the Bank, `balanceOwner` is the same as `redemeer`.
470
+ /// However, when a Vault is redeeming part of its balance for some
471
+ /// redeemer address (for example, someone who has earlier deposited
472
+ /// into that Vault), `balanceOwner` is the Vault, and `redemeer` is
473
+ /// the address for which the vault is redeeming its balance to,
474
+ /// - walletPubKeyHash: The 20-byte wallet public key hash (computed
475
+ /// using Bitcoin HASH160 over the compressed ECDSA public key),
476
+ /// - mainUtxoTxHash: Data of the wallet's main UTXO TX hash, as
477
+ /// currently known on the Ethereum chain,
478
+ /// - mainUtxoTxOutputIndex: Data of the wallet's main UTXO output
479
+ /// index, as currently known on Ethereum chain,
480
+ /// - mainUtxoTxOutputValue: Data of the wallet's main UTXO output
481
+ /// value, as currently known on Ethereum chain,
482
+ /// - redeemerOutputScript The redeemer's length-prefixed output
483
+ /// script (P2PKH, P2WPKH, P2SH or P2WSH) that will be used to lock
484
+ /// redeemed BTC.
485
+ /// @dev Requirements:
486
+ /// - The caller must be the Bank,
487
+ /// - Wallet behind `walletPubKeyHash` must be live,
488
+ /// - `mainUtxo` components must point to the recent main UTXO
489
+ /// of the given wallet, as currently known on the Ethereum chain,
490
+ /// - `redeemerOutputScript` must be a proper Bitcoin script,
491
+ /// - `redeemerOutputScript` cannot have wallet PKH as payload,
492
+ /// - `amount` must be above or equal the `redemptionDustThreshold`,
493
+ /// - Given `walletPubKeyHash` and `redeemerOutputScript` pair can be
494
+ /// used for only one pending request at the same time,
495
+ /// - Wallet must have enough Bitcoin balance to process the request.
496
+ ///
497
+ /// Note on upgradeability:
498
+ /// Bridge is an upgradeable contract deployed behind
499
+ /// a TransparentUpgradeableProxy. Accepting redemption data as bytes
500
+ /// provides great flexibility. The Bridge is just like any other
501
+ /// contract with a balance approved in the Bank and can be upgraded
502
+ /// to another version without being bound to a particular interface
503
+ /// forever. This flexibility comes with the cost - developers
504
+ /// integrating their vaults and dApps with `Bridge` using
505
+ /// `approveBalanceAndCall` need to pay extra attention to
506
+ /// `redemptionData` and adjust the code in case the expected structure
507
+ /// of `redemptionData` changes.
508
+ function receiveBalanceApproval(
509
+ address balanceOwner,
510
+ uint256 amount,
511
+ bytes calldata redemptionData
512
+ ) external override {
513
+ require(msg.sender == address(self.bank), "Caller is not the bank");
514
+
515
+ self.requestRedemption(
516
+ balanceOwner,
517
+ SafeCastUpgradeable.toUint64(amount),
518
+ redemptionData
519
+ );
520
+ }
521
+
522
+ /// @notice Used by the wallet to prove the BTC redemption transaction
523
+ /// and to make the necessary bookkeeping. Redemption is only
524
+ /// accepted if it satisfies SPV proof.
525
+ ///
526
+ /// The function is performing Bank balance updates by burning
527
+ /// the total redeemed Bitcoin amount from Bridge balance and
528
+ /// transferring the treasury fee sum to the treasury address.
529
+ ///
530
+ /// It is possible to prove the given redemption only one time.
531
+ /// @param redemptionTx Bitcoin redemption transaction data.
532
+ /// @param redemptionProof Bitcoin redemption proof data.
533
+ /// @param mainUtxo Data of the wallet's main UTXO, as currently known on
534
+ /// the Ethereum chain.
535
+ /// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
536
+ /// HASH160 over the compressed ECDSA public key) of the wallet which
537
+ /// performed the redemption transaction.
538
+ /// @dev Requirements:
539
+ /// - `redemptionTx` components must match the expected structure. See
540
+ /// `BitcoinTx.Info` docs for reference. Their values must exactly
541
+ /// correspond to appropriate Bitcoin transaction fields to produce
542
+ /// a provable transaction hash,
543
+ /// - The `redemptionTx` should represent a Bitcoin transaction with
544
+ /// exactly 1 input that refers to the wallet's main UTXO. That
545
+ /// transaction should have 1..n outputs handling existing pending
546
+ /// redemption requests or pointing to reported timed out requests.
547
+ /// There can be also 1 optional output representing the
548
+ /// change and pointing back to the 20-byte wallet public key hash.
549
+ /// The change should be always present if the redeemed value sum
550
+ /// is lower than the total wallet's BTC balance,
551
+ /// - `redemptionProof` components must match the expected structure.
552
+ /// See `BitcoinTx.Proof` docs for reference. The `bitcoinHeaders`
553
+ /// field must contain a valid number of block headers, not less
554
+ /// than the `txProofDifficultyFactor` contract constant,
555
+ /// - `mainUtxo` components must point to the recent main UTXO
556
+ /// of the given wallet, as currently known on the Ethereum chain.
557
+ /// Additionally, the recent main UTXO on Ethereum must be set,
558
+ /// - `walletPubKeyHash` must be connected with the main UTXO used
559
+ /// as transaction single input.
560
+ /// Other remarks:
561
+ /// - Putting the change output as the first transaction output can
562
+ /// save some gas because the output processing loop begins each
563
+ /// iteration by checking whether the given output is the change
564
+ /// thus uses some gas for making the comparison. Once the change
565
+ /// is identified, that check is omitted in further iterations.
566
+ function submitRedemptionProof(
567
+ BitcoinTx.Info calldata redemptionTx,
568
+ BitcoinTx.Proof calldata redemptionProof,
569
+ BitcoinTx.UTXO calldata mainUtxo,
570
+ bytes20 walletPubKeyHash
571
+ ) external {
572
+ self.submitRedemptionProof(
573
+ redemptionTx,
574
+ redemptionProof,
575
+ mainUtxo,
576
+ walletPubKeyHash
577
+ );
578
+ }
579
+
580
+ /// @notice Notifies that there is a pending redemption request associated
581
+ /// with the given wallet, that has timed out. The redemption
582
+ /// request is identified by the key built as
583
+ /// `keccak256(walletPubKeyHash | redeemerOutputScript)`.
584
+ /// The results of calling this function:
585
+ /// - The pending redemptions value for the wallet will be decreased
586
+ /// by the requested amount (minus treasury fee),
587
+ /// - The tokens taken from the redeemer on redemption request will
588
+ /// be returned to the redeemer,
589
+ /// - The request will be moved from pending redemptions to
590
+ /// timed-out redemptions,
591
+ /// - If the state of the wallet is `Live` or `MovingFunds`, the
592
+ /// wallet operators will be slashed and the notifier will be
593
+ /// rewarded,
594
+ /// - If the state of wallet is `Live`, the wallet will be closed or
595
+ /// marked as `MovingFunds` (depending on the presence or absence
596
+ /// of the wallet's main UTXO) and the wallet will no longer be
597
+ /// marked as the active wallet (if it was marked as such).
598
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet.
599
+ /// @param walletMembersIDs Identifiers of the wallet signing group members.
600
+ /// @param redeemerOutputScript The redeemer's length-prefixed output
601
+ /// script (P2PKH, P2WPKH, P2SH or P2WSH).
602
+ /// @dev Requirements:
603
+ /// - The wallet must be in the Live or MovingFunds or Terminated state,
604
+ /// - The redemption request identified by `walletPubKeyHash` and
605
+ /// `redeemerOutputScript` must exist,
606
+ /// - The expression `keccak256(abi.encode(walletMembersIDs))` must
607
+ /// be exactly the same as the hash stored under `membersIdsHash`
608
+ /// for the given `walletID`. Those IDs are not directly stored
609
+ /// in the contract for gas efficiency purposes but they can be
610
+ /// read from appropriate `DkgResultSubmitted` and `DkgResultApproved`
611
+ /// events of the `WalletRegistry` contract,
612
+ /// - The amount of time defined by `redemptionTimeout` must have
613
+ /// passed since the redemption was requested (the request must be
614
+ /// timed-out).
615
+ function notifyRedemptionTimeout(
616
+ bytes20 walletPubKeyHash,
617
+ uint32[] calldata walletMembersIDs,
618
+ bytes calldata redeemerOutputScript
619
+ ) external {
620
+ self.notifyRedemptionTimeout(
621
+ walletPubKeyHash,
622
+ walletMembersIDs,
623
+ redeemerOutputScript
624
+ );
625
+ }
626
+
627
+ /// @notice Submits the moving funds target wallets commitment.
628
+ /// Once all requirements are met, that function registers the
629
+ /// target wallets commitment and opens the way for moving funds
630
+ /// proof submission.
631
+ /// @param walletPubKeyHash 20-byte public key hash of the source wallet.
632
+ /// @param walletMainUtxo Data of the source wallet's main UTXO, as
633
+ /// currently known on the Ethereum chain.
634
+ /// @param walletMembersIDs Identifiers of the source wallet signing group
635
+ /// members.
636
+ /// @param walletMemberIndex Position of the caller in the source wallet
637
+ /// signing group members list.
638
+ /// @param targetWallets List of 20-byte public key hashes of the target
639
+ /// wallets that the source wallet commits to move the funds to.
640
+ /// @dev Requirements:
641
+ /// - The source wallet must be in the MovingFunds state,
642
+ /// - The source wallet must not have pending redemption requests,
643
+ /// - The source wallet must not have pending moved funds sweep requests,
644
+ /// - The source wallet must not have submitted its commitment already,
645
+ /// - The expression `keccak256(abi.encode(walletMembersIDs))` must
646
+ /// be exactly the same as the hash stored under `membersIdsHash`
647
+ /// for the given source wallet in the ECDSA registry. Those IDs are
648
+ /// not directly stored in the contract for gas efficiency purposes
649
+ /// but they can be read from appropriate `DkgResultSubmitted`
650
+ /// and `DkgResultApproved` events,
651
+ /// - The `walletMemberIndex` must be in range [1, walletMembersIDs.length],
652
+ /// - The caller must be the member of the source wallet signing group
653
+ /// at the position indicated by `walletMemberIndex` parameter,
654
+ /// - The `walletMainUtxo` components must point to the recent main
655
+ /// UTXO of the source wallet, as currently known on the Ethereum
656
+ /// chain,
657
+ /// - Source wallet BTC balance must be greater than zero,
658
+ /// - At least one Live wallet must exist in the system,
659
+ /// - Submitted target wallets count must match the expected count
660
+ /// `N = min(liveWalletsCount, ceil(walletBtcBalance / walletMaxBtcTransfer))`
661
+ /// where `N > 0`,
662
+ /// - Each target wallet must be not equal to the source wallet,
663
+ /// - Each target wallet must follow the expected order i.e. all
664
+ /// target wallets 20-byte public key hashes represented as numbers
665
+ /// must form a strictly increasing sequence without duplicates,
666
+ /// - Each target wallet must be in Live state.
667
+ function submitMovingFundsCommitment(
668
+ bytes20 walletPubKeyHash,
669
+ BitcoinTx.UTXO calldata walletMainUtxo,
670
+ uint32[] calldata walletMembersIDs,
671
+ uint256 walletMemberIndex,
672
+ bytes20[] calldata targetWallets
673
+ ) external {
674
+ self.submitMovingFundsCommitment(
675
+ walletPubKeyHash,
676
+ walletMainUtxo,
677
+ walletMembersIDs,
678
+ walletMemberIndex,
679
+ targetWallets
680
+ );
681
+ }
682
+
683
+ /// @notice Resets the moving funds timeout for the given wallet if the
684
+ /// target wallet commitment cannot be submitted due to a lack
685
+ /// of live wallets in the system.
686
+ /// @param walletPubKeyHash 20-byte public key hash of the moving funds wallet.
687
+ /// @dev Requirements:
688
+ /// - The wallet must be in the MovingFunds state,
689
+ /// - The target wallets commitment must not be already submitted for
690
+ /// the given moving funds wallet,
691
+ /// - Live wallets count must be zero,
692
+ /// - The moving funds timeout reset delay must be elapsed.
693
+ function resetMovingFundsTimeout(bytes20 walletPubKeyHash) external {
694
+ self.resetMovingFundsTimeout(walletPubKeyHash);
695
+ }
696
+
697
+ /// @notice Used by the wallet to prove the BTC moving funds transaction
698
+ /// and to make the necessary state changes. Moving funds is only
699
+ /// accepted if it satisfies SPV proof.
700
+ ///
701
+ /// The function validates the moving funds transaction structure
702
+ /// by checking if it actually spends the main UTXO of the declared
703
+ /// wallet and locks the value on the pre-committed target wallets
704
+ /// using a reasonable transaction fee. If all preconditions are
705
+ /// met, this functions closes the source wallet.
706
+ ///
707
+ /// It is possible to prove the given moving funds transaction only
708
+ /// one time.
709
+ /// @param movingFundsTx Bitcoin moving funds transaction data.
710
+ /// @param movingFundsProof Bitcoin moving funds proof data.
711
+ /// @param mainUtxo Data of the wallet's main UTXO, as currently known on
712
+ /// the Ethereum chain.
713
+ /// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
714
+ /// HASH160 over the compressed ECDSA public key) of the wallet
715
+ /// which performed the moving funds transaction.
716
+ /// @dev Requirements:
717
+ /// - `movingFundsTx` components must match the expected structure. See
718
+ /// `BitcoinTx.Info` docs for reference. Their values must exactly
719
+ /// correspond to appropriate Bitcoin transaction fields to produce
720
+ /// a provable transaction hash,
721
+ /// - The `movingFundsTx` should represent a Bitcoin transaction with
722
+ /// exactly 1 input that refers to the wallet's main UTXO. That
723
+ /// transaction should have 1..n outputs corresponding to the
724
+ /// pre-committed target wallets. Outputs must be ordered in the
725
+ /// same way as their corresponding target wallets are ordered
726
+ /// within the target wallets commitment,
727
+ /// - `movingFundsProof` components must match the expected structure.
728
+ /// See `BitcoinTx.Proof` docs for reference. The `bitcoinHeaders`
729
+ /// field must contain a valid number of block headers, not less
730
+ /// than the `txProofDifficultyFactor` contract constant,
731
+ /// - `mainUtxo` components must point to the recent main UTXO
732
+ /// of the given wallet, as currently known on the Ethereum chain.
733
+ /// Additionally, the recent main UTXO on Ethereum must be set,
734
+ /// - `walletPubKeyHash` must be connected with the main UTXO used
735
+ /// as transaction single input,
736
+ /// - The wallet that `walletPubKeyHash` points to must be in the
737
+ /// MovingFunds state,
738
+ /// - The target wallets commitment must be submitted by the wallet
739
+ /// that `walletPubKeyHash` points to,
740
+ /// - The total Bitcoin transaction fee must be lesser or equal
741
+ /// to `movingFundsTxMaxTotalFee` governable parameter.
742
+ function submitMovingFundsProof(
743
+ BitcoinTx.Info calldata movingFundsTx,
744
+ BitcoinTx.Proof calldata movingFundsProof,
745
+ BitcoinTx.UTXO calldata mainUtxo,
746
+ bytes20 walletPubKeyHash
747
+ ) external {
748
+ self.submitMovingFundsProof(
749
+ movingFundsTx,
750
+ movingFundsProof,
751
+ mainUtxo,
752
+ walletPubKeyHash
753
+ );
754
+ }
755
+
756
+ /// @notice Notifies about a timed out moving funds process. Terminates
757
+ /// the wallet and slashes signing group members as a result.
758
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet.
759
+ /// @param walletMembersIDs Identifiers of the wallet signing group members.
760
+ /// @dev Requirements:
761
+ /// - The wallet must be in the MovingFunds state,
762
+ /// - The moving funds timeout must be actually exceeded,
763
+ /// - The expression `keccak256(abi.encode(walletMembersIDs))` must
764
+ /// be exactly the same as the hash stored under `membersIdsHash`
765
+ /// for the given `walletID`. Those IDs are not directly stored
766
+ /// in the contract for gas efficiency purposes but they can be
767
+ /// read from appropriate `DkgResultSubmitted` and `DkgResultApproved`
768
+ /// events of the `WalletRegistry` contract.
769
+ function notifyMovingFundsTimeout(
770
+ bytes20 walletPubKeyHash,
771
+ uint32[] calldata walletMembersIDs
318
772
  ) external {
319
- // TODO We need to read `fundingTxHash`, `fundingOutputIndex` from
320
- // `sweepTx.inputVector`. We then hash them to obtain deposit
321
- // identifier and read DepositInfo. From DepositInfo we know what
322
- // amount was inferred during deposit reveal transaction and we
323
- // use that amount to update their Bank balance, minus fee.
324
- //
325
- // TODO We need to validate if the sum in the output minus the
326
- // amount from the previous wallet balance input minus fees is
327
- // equal to the amount by which Bank balances were increased.
328
- //
329
- // TODO We need to validate `sweepTx.outputVector` to see if the balance
330
- // was not transferred away from the wallet before increasing
331
- // balances in the bank.
332
- //
333
- // TODO Delete deposit from unswept mapping or mark it as swept
334
- // depending on the gas costs. Alternatively, do not allow to
335
- // use the same TX input vector twice. Sweep should be provable
336
- // only one time.
337
- }
338
-
339
- // TODO It is possible a malicious wallet can sweep deposits that can not
340
- // be later proved on Ethereum. For example, a deposit with
341
- // an incorrect amount revealed. We need to provide a function for honest
342
- // depositors, next to sweep, to prove their swept balances on Ethereum
343
- // selectively, based on deposits they have earlier received.
344
- // (UPDATE PR #90: Is it still the case since amounts are inferred?)
773
+ self.notifyMovingFundsTimeout(walletPubKeyHash, walletMembersIDs);
774
+ }
775
+
776
+ /// @notice Notifies about a moving funds wallet whose BTC balance is
777
+ /// below the moving funds dust threshold. Ends the moving funds
778
+ /// process and begins wallet closing immediately.
779
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet
780
+ /// @param mainUtxo Data of the wallet's main UTXO, as currently known
781
+ /// on the Ethereum chain.
782
+ /// @dev Requirements:
783
+ /// - The wallet must be in the MovingFunds state,
784
+ /// - The `mainUtxo` components must point to the recent main UTXO
785
+ /// of the given wallet, as currently known on the Ethereum chain.
786
+ /// If the wallet has no main UTXO, this parameter can be empty as it
787
+ /// is ignored,
788
+ /// - The wallet BTC balance must be below the moving funds threshold.
789
+ function notifyMovingFundsBelowDust(
790
+ bytes20 walletPubKeyHash,
791
+ BitcoinTx.UTXO calldata mainUtxo
792
+ ) external {
793
+ self.notifyMovingFundsBelowDust(walletPubKeyHash, mainUtxo);
794
+ }
795
+
796
+ /// @notice Used by the wallet to prove the BTC moved funds sweep
797
+ /// transaction and to make the necessary state changes. Moved
798
+ /// funds sweep is only accepted if it satisfies SPV proof.
799
+ ///
800
+ /// The function validates the sweep transaction structure by
801
+ /// checking if it actually spends the moved funds UTXO and the
802
+ /// sweeping wallet's main UTXO (optionally), and if it locks the
803
+ /// value on the sweeping wallet's 20-byte public key hash using a
804
+ /// reasonable transaction fee. If all preconditions are
805
+ /// met, this function updates the sweeping wallet main UTXO, thus
806
+ /// their BTC balance.
807
+ ///
808
+ /// It is possible to prove the given sweep transaction only
809
+ /// one time.
810
+ /// @param sweepTx Bitcoin sweep funds transaction data.
811
+ /// @param sweepProof Bitcoin sweep funds proof data.
812
+ /// @param mainUtxo Data of the sweeping wallet's main UTXO, as currently
813
+ /// known on the Ethereum chain.
814
+ /// @dev Requirements:
815
+ /// - `sweepTx` components must match the expected structure. See
816
+ /// `BitcoinTx.Info` docs for reference. Their values must exactly
817
+ /// correspond to appropriate Bitcoin transaction fields to produce
818
+ /// a provable transaction hash,
819
+ /// - The `sweepTx` should represent a Bitcoin transaction with
820
+ /// the first input pointing to a moved funds sweep request targeted
821
+ /// to the wallet, and optionally, the second input pointing to the
822
+ /// wallet's main UTXO, if the sweeping wallet has a main UTXO set.
823
+ /// There should be only one output locking funds on the sweeping
824
+ /// wallet 20-byte public key hash,
825
+ /// - `sweepProof` components must match the expected structure.
826
+ /// See `BitcoinTx.Proof` docs for reference. The `bitcoinHeaders`
827
+ /// field must contain a valid number of block headers, not less
828
+ /// than the `txProofDifficultyFactor` contract constant,
829
+ /// - `mainUtxo` components must point to the recent main UTXO
830
+ /// of the sweeping wallet, as currently known on the Ethereum chain.
831
+ /// If there is no main UTXO, this parameter is ignored,
832
+ /// - The sweeping wallet must be in the Live or MovingFunds state,
833
+ /// - The total Bitcoin transaction fee must be lesser or equal
834
+ /// to `movedFundsSweepTxMaxTotalFee` governable parameter.
835
+ function submitMovedFundsSweepProof(
836
+ BitcoinTx.Info calldata sweepTx,
837
+ BitcoinTx.Proof calldata sweepProof,
838
+ BitcoinTx.UTXO calldata mainUtxo
839
+ ) external {
840
+ self.submitMovedFundsSweepProof(sweepTx, sweepProof, mainUtxo);
841
+ }
842
+
843
+ /// @notice Notifies about a timed out moved funds sweep process. If the
844
+ /// wallet is not terminated yet, that function terminates
845
+ /// the wallet and slashes signing group members as a result.
846
+ /// Marks the given sweep request as TimedOut.
847
+ /// @param movingFundsTxHash 32-byte hash of the moving funds transaction
848
+ /// that caused the sweep request to be created.
849
+ /// @param movingFundsTxOutputIndex Index of the moving funds transaction
850
+ /// output that is subject of the sweep request.
851
+ /// @param walletMembersIDs Identifiers of the wallet signing group members.
852
+ /// @dev Requirements:
853
+ /// - The moved funds sweep request must be in the Pending state,
854
+ /// - The moved funds sweep timeout must be actually exceeded,
855
+ /// - The wallet must be either in the Live or MovingFunds or
856
+ /// Terminated state,
857
+ /// - The expression `keccak256(abi.encode(walletMembersIDs))` must
858
+ /// be exactly the same as the hash stored under `membersIdsHash`
859
+ /// for the given `walletID`. Those IDs are not directly stored
860
+ /// in the contract for gas efficiency purposes but they can be
861
+ /// read from appropriate `DkgResultSubmitted` and `DkgResultApproved`
862
+ /// events of the `WalletRegistry` contract.
863
+ function notifyMovedFundsSweepTimeout(
864
+ bytes32 movingFundsTxHash,
865
+ uint32 movingFundsTxOutputIndex,
866
+ uint32[] calldata walletMembersIDs
867
+ ) external {
868
+ self.notifyMovedFundsSweepTimeout(
869
+ movingFundsTxHash,
870
+ movingFundsTxOutputIndex,
871
+ walletMembersIDs
872
+ );
873
+ }
874
+
875
+ /// @notice Requests creation of a new wallet. This function just
876
+ /// forms a request and the creation process is performed
877
+ /// asynchronously. Once a wallet is created, the ECDSA Wallet
878
+ /// Registry will notify this contract by calling the
879
+ /// `__ecdsaWalletCreatedCallback` function.
880
+ /// @param activeWalletMainUtxo Data of the active wallet's main UTXO, as
881
+ /// currently known on the Ethereum chain.
882
+ /// @dev Requirements:
883
+ /// - `activeWalletMainUtxo` components must point to the recent main
884
+ /// UTXO of the given active wallet, as currently known on the
885
+ /// Ethereum chain. If there is no active wallet at the moment, or
886
+ /// the active wallet has no main UTXO, this parameter can be
887
+ /// empty as it is ignored,
888
+ /// - Wallet creation must not be in progress,
889
+ /// - If the active wallet is set, one of the following
890
+ /// conditions must be true:
891
+ /// - The active wallet BTC balance is above the minimum threshold
892
+ /// and the active wallet is old enough, i.e. the creation period
893
+ /// was elapsed since its creation time,
894
+ /// - The active wallet BTC balance is above the maximum threshold.
895
+ function requestNewWallet(BitcoinTx.UTXO calldata activeWalletMainUtxo)
896
+ external
897
+ {
898
+ self.requestNewWallet(activeWalletMainUtxo);
899
+ }
900
+
901
+ /// @notice A callback function that is called by the ECDSA Wallet Registry
902
+ /// once a new ECDSA wallet is created.
903
+ /// @param ecdsaWalletID Wallet's unique identifier.
904
+ /// @param publicKeyX Wallet's public key's X coordinate.
905
+ /// @param publicKeyY Wallet's public key's Y coordinate.
906
+ /// @dev Requirements:
907
+ /// - The only caller authorized to call this function is `registry`,
908
+ /// - Given wallet data must not belong to an already registered wallet.
909
+ function __ecdsaWalletCreatedCallback(
910
+ bytes32 ecdsaWalletID,
911
+ bytes32 publicKeyX,
912
+ bytes32 publicKeyY
913
+ ) external override {
914
+ self.registerNewWallet(ecdsaWalletID, publicKeyX, publicKeyY);
915
+ }
916
+
917
+ /// @notice A callback function that is called by the ECDSA Wallet Registry
918
+ /// once a wallet heartbeat failure is detected.
919
+ /// @param publicKeyX Wallet's public key's X coordinate.
920
+ /// @param publicKeyY Wallet's public key's Y coordinate.
921
+ /// @dev Requirements:
922
+ /// - The only caller authorized to call this function is `registry`,
923
+ /// - Wallet must be in Live state.
924
+ function __ecdsaWalletHeartbeatFailedCallback(
925
+ bytes32,
926
+ bytes32 publicKeyX,
927
+ bytes32 publicKeyY
928
+ ) external override {
929
+ self.notifyWalletHeartbeatFailed(publicKeyX, publicKeyY);
930
+ }
931
+
932
+ /// @notice Notifies that the wallet is either old enough or has too few
933
+ /// satoshi left and qualifies to be closed.
934
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet.
935
+ /// @param walletMainUtxo Data of the wallet's main UTXO, as currently
936
+ /// known on the Ethereum chain.
937
+ /// @dev Requirements:
938
+ /// - Wallet must not be set as the current active wallet,
939
+ /// - Wallet must exceed the wallet maximum age OR the wallet BTC
940
+ /// balance must be lesser than the minimum threshold. If the latter
941
+ /// case is true, the `walletMainUtxo` components must point to the
942
+ /// recent main UTXO of the given wallet, as currently known on the
943
+ /// Ethereum chain. If the wallet has no main UTXO, this parameter
944
+ /// can be empty as it is ignored since the wallet balance is
945
+ /// assumed to be zero,
946
+ /// - Wallet must be in Live state.
947
+ function notifyCloseableWallet(
948
+ bytes20 walletPubKeyHash,
949
+ BitcoinTx.UTXO calldata walletMainUtxo
950
+ ) external {
951
+ self.notifyCloseableWallet(walletPubKeyHash, walletMainUtxo);
952
+ }
953
+
954
+ /// @notice Notifies about the end of the closing period for the given wallet.
955
+ /// Closes the wallet ultimately and notifies the ECDSA registry
956
+ /// about this fact.
957
+ /// @param walletPubKeyHash 20-byte public key hash of the wallet.
958
+ /// @dev Requirements:
959
+ /// - The wallet must be in the Closing state,
960
+ /// - The wallet closing period must have elapsed.
961
+ function notifyWalletClosingPeriodElapsed(bytes20 walletPubKeyHash)
962
+ external
963
+ {
964
+ self.notifyWalletClosingPeriodElapsed(walletPubKeyHash);
965
+ }
966
+
967
+ /// @notice Submits a fraud challenge indicating that a UTXO being under
968
+ /// wallet control was unlocked by the wallet but was not used
969
+ /// according to the protocol rules. That means the wallet signed
970
+ /// a transaction input pointing to that UTXO and there is a unique
971
+ /// sighash and signature pair associated with that input. This
972
+ /// function uses those parameters to create a fraud accusation that
973
+ /// proves a given transaction input unlocking the given UTXO was
974
+ /// actually signed by the wallet. This function cannot determine
975
+ /// whether the transaction was actually broadcast and the input was
976
+ /// consumed in a fraudulent way so it just opens a challenge period
977
+ /// during which the wallet can defeat the challenge by submitting
978
+ /// proof of a transaction that consumes the given input according
979
+ /// to protocol rules. To prevent spurious allegations, the caller
980
+ /// must deposit ETH that is returned back upon justified fraud
981
+ /// challenge or confiscated otherwise.
982
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
983
+ /// and unprefixed format (64 bytes).
984
+ /// @param preimageSha256 The hash that was generated by applying SHA-256
985
+ /// one time over the preimage used during input signing. The preimage
986
+ /// is a serialized subset of the transaction and its structure
987
+ /// depends on the transaction input (see BIP-143 for reference).
988
+ /// Notice that applying SHA-256 over the `preimageSha256` results
989
+ /// in `sighash`. The path from `preimage` to `sighash` looks like
990
+ /// this:
991
+ /// preimage -> (SHA-256) -> preimageSha256 -> (SHA-256) -> sighash.
992
+ /// @param signature Bitcoin signature in the R/S/V format.
993
+ /// @dev Requirements:
994
+ /// - Wallet behind `walletPublicKey` must be in Live or MovingFunds
995
+ /// or Closing state,
996
+ /// - The challenger must send appropriate amount of ETH used as
997
+ /// fraud challenge deposit,
998
+ /// - The signature (represented by r, s and v) must be generated by
999
+ /// the wallet behind `walletPubKey` during signing of `sighash`
1000
+ /// which was calculated from `preimageSha256`,
1001
+ /// - Wallet can be challenged for the given signature only once.
1002
+ function submitFraudChallenge(
1003
+ bytes calldata walletPublicKey,
1004
+ bytes memory preimageSha256,
1005
+ BitcoinTx.RSVSignature calldata signature
1006
+ ) external payable {
1007
+ self.submitFraudChallenge(walletPublicKey, preimageSha256, signature);
1008
+ }
1009
+
1010
+ /// @notice Allows to defeat a pending fraud challenge against a wallet if
1011
+ /// the transaction that spends the UTXO follows the protocol rules.
1012
+ /// In order to defeat the challenge the same `walletPublicKey` and
1013
+ /// signature (represented by `r`, `s` and `v`) must be provided as
1014
+ /// were used to calculate the sighash during input signing.
1015
+ /// The fraud challenge defeat attempt will only succeed if the
1016
+ /// inputs in the preimage are considered honestly spent by the
1017
+ /// wallet. Therefore the transaction spending the UTXO must be
1018
+ /// proven in the Bridge before a challenge defeat is called.
1019
+ /// If successfully defeated, the fraud challenge is marked as
1020
+ /// resolved and the amount of ether deposited by the challenger is
1021
+ /// sent to the treasury.
1022
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
1023
+ /// and unprefixed format (64 bytes).
1024
+ /// @param preimage The preimage which produces sighash used to generate the
1025
+ /// ECDSA signature that is the subject of the fraud claim. It is a
1026
+ /// serialized subset of the transaction. The exact subset used as
1027
+ /// the preimage depends on the transaction input the signature is
1028
+ /// produced for. See BIP-143 for reference.
1029
+ /// @param witness Flag indicating whether the preimage was produced for a
1030
+ /// witness input. True for witness, false for non-witness input.
1031
+ /// @dev Requirements:
1032
+ /// - `walletPublicKey` and `sighash` calculated as `hash256(preimage)`
1033
+ /// must identify an open fraud challenge,
1034
+ /// - the preimage must be a valid preimage of a transaction generated
1035
+ /// according to the protocol rules and already proved in the Bridge,
1036
+ /// - before a defeat attempt is made the transaction that spends the
1037
+ /// given UTXO must be proven in the Bridge.
1038
+ function defeatFraudChallenge(
1039
+ bytes calldata walletPublicKey,
1040
+ bytes calldata preimage,
1041
+ bool witness
1042
+ ) external {
1043
+ self.defeatFraudChallenge(walletPublicKey, preimage, witness);
1044
+ }
1045
+
1046
+ /// @notice Allows to defeat a pending fraud challenge against a wallet by
1047
+ /// proving the sighash and signature were produced for an off-chain
1048
+ /// wallet heartbeat message following a strict format.
1049
+ /// In order to defeat the challenge the same `walletPublicKey` and
1050
+ /// signature (represented by `r`, `s` and `v`) must be provided as
1051
+ /// were used to calculate the sighash during heartbeat message
1052
+ /// signing. The fraud challenge defeat attempt will only succeed if
1053
+ /// the signed message follows a strict format required for
1054
+ /// heartbeat messages. If successfully defeated, the fraud
1055
+ /// challenge is marked as resolved and the amount of ether
1056
+ /// deposited by the challenger is sent to the treasury.
1057
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
1058
+ /// and unprefixed format (64 bytes).
1059
+ /// @param heartbeatMessage Off-chain heartbeat message meeting the heartbeat
1060
+ /// message format requirements which produces sighash used to
1061
+ /// generate the ECDSA signature that is the subject of the fraud
1062
+ /// claim.
1063
+ /// @dev Requirements:
1064
+ /// - `walletPublicKey` and `sighash` calculated as
1065
+ /// `hash256(heartbeatMessage)` must identify an open fraud challenge,
1066
+ /// - `heartbeatMessage` must follow a strict format of heartbeat
1067
+ /// messages.
1068
+ function defeatFraudChallengeWithHeartbeat(
1069
+ bytes calldata walletPublicKey,
1070
+ bytes calldata heartbeatMessage
1071
+ ) external {
1072
+ self.defeatFraudChallengeWithHeartbeat(
1073
+ walletPublicKey,
1074
+ heartbeatMessage
1075
+ );
1076
+ }
1077
+
1078
+ /// @notice Notifies about defeat timeout for the given fraud challenge.
1079
+ /// Can be called only if there was a fraud challenge identified by
1080
+ /// the provided `walletPublicKey` and `sighash` and it was not
1081
+ /// defeated on time. The amount of time that needs to pass after
1082
+ /// a fraud challenge is reported is indicated by the
1083
+ /// `challengeDefeatTimeout`. After a successful fraud challenge
1084
+ /// defeat timeout notification the fraud challenge is marked as
1085
+ /// resolved, the stake of each operator is slashed, the ether
1086
+ /// deposited is returned to the challenger and the challenger is
1087
+ /// rewarded.
1088
+ /// @param walletPublicKey The public key of the wallet in the uncompressed
1089
+ /// and unprefixed format (64 bytes).
1090
+ /// @param walletMembersIDs Identifiers of the wallet signing group members.
1091
+ /// @param preimageSha256 The hash that was generated by applying SHA-256
1092
+ /// one time over the preimage used during input signing. The preimage
1093
+ /// is a serialized subset of the transaction and its structure
1094
+ /// depends on the transaction input (see BIP-143 for reference).
1095
+ /// Notice that applying SHA-256 over the `preimageSha256` results
1096
+ /// in `sighash`. The path from `preimage` to `sighash` looks like
1097
+ /// this:
1098
+ /// preimage -> (SHA-256) -> preimageSha256 -> (SHA-256) -> sighash.
1099
+ /// @dev Requirements:
1100
+ /// - The wallet must be in the Live or MovingFunds or Closing or
1101
+ /// Terminated state,
1102
+ /// - The `walletPublicKey` and `sighash` calculated from
1103
+ /// `preimageSha256` must identify an open fraud challenge,
1104
+ /// - The expression `keccak256(abi.encode(walletMembersIDs))` must
1105
+ /// be exactly the same as the hash stored under `membersIdsHash`
1106
+ /// for the given `walletID`. Those IDs are not directly stored
1107
+ /// in the contract for gas efficiency purposes but they can be
1108
+ /// read from appropriate `DkgResultSubmitted` and `DkgResultApproved`
1109
+ /// events of the `WalletRegistry` contract,
1110
+ /// - The amount of time indicated by `challengeDefeatTimeout` must pass
1111
+ /// after the challenge was reported.
1112
+ function notifyFraudChallengeDefeatTimeout(
1113
+ bytes calldata walletPublicKey,
1114
+ uint32[] calldata walletMembersIDs,
1115
+ bytes memory preimageSha256
1116
+ ) external {
1117
+ self.notifyFraudChallengeDefeatTimeout(
1118
+ walletPublicKey,
1119
+ walletMembersIDs,
1120
+ preimageSha256
1121
+ );
1122
+ }
1123
+
1124
+ /// @notice Allows the Governance to mark the given vault address as trusted
1125
+ /// or no longer trusted. Vaults are not trusted by default.
1126
+ /// Trusted vault must meet the following criteria:
1127
+ /// - `IVault.receiveBalanceIncrease` must have a known, low gas
1128
+ /// cost,
1129
+ /// - `IVault.receiveBalanceIncrease` must never revert.
1130
+ /// @dev Without restricting reveal only to trusted vaults, malicious
1131
+ /// vaults not meeting the criteria would be able to nuke sweep proof
1132
+ /// transactions executed by ECDSA wallet with deposits routed to
1133
+ /// them.
1134
+ /// @param vault The address of the vault.
1135
+ /// @param isTrusted flag indicating whether the vault is trusted or not.
1136
+ /// @dev Can only be called by the Governance.
1137
+ function setVaultStatus(address vault, bool isTrusted)
1138
+ external
1139
+ onlyGovernance
1140
+ {
1141
+ self.isVaultTrusted[vault] = isTrusted;
1142
+ emit VaultStatusUpdated(vault, isTrusted);
1143
+ }
1144
+
1145
+ /// @notice Updates parameters of deposits.
1146
+ /// @param depositDustThreshold New value of the deposit dust threshold in
1147
+ /// satoshis. It is the minimal amount that can be requested to
1148
+ //// deposit. Value of this parameter must take into account the value
1149
+ /// of `depositTreasuryFeeDivisor` and `depositTxMaxFee` parameters
1150
+ /// in order to make requests that can incur the treasury and
1151
+ /// transaction fee and still satisfy the depositor.
1152
+ /// @param depositTreasuryFeeDivisor New value of the treasury fee divisor.
1153
+ /// It is the divisor used to compute the treasury fee taken from
1154
+ /// each deposit and transferred to the treasury upon sweep proof
1155
+ /// submission. That fee is computed as follows:
1156
+ /// `treasuryFee = depositedAmount / depositTreasuryFeeDivisor`
1157
+ /// For example, if the treasury fee needs to be 2% of each deposit,
1158
+ /// the `depositTreasuryFeeDivisor` should be set to `50`
1159
+ /// because `1/50 = 0.02 = 2%`.
1160
+ /// @param depositTxMaxFee New value of the deposit tx max fee in satoshis.
1161
+ /// It is the maximum amount of BTC transaction fee that can
1162
+ /// be incurred by each swept deposit being part of the given sweep
1163
+ /// transaction. If the maximum BTC transaction fee is exceeded,
1164
+ /// such transaction is considered a fraud.
1165
+ /// @dev Requirements:
1166
+ /// - Deposit dust threshold must be greater than zero,
1167
+ /// - Deposit treasury fee divisor must be greater than zero,
1168
+ /// - Deposit transaction max fee must be greater than zero.
1169
+ function updateDepositParameters(
1170
+ uint64 depositDustThreshold,
1171
+ uint64 depositTreasuryFeeDivisor,
1172
+ uint64 depositTxMaxFee
1173
+ ) external onlyGovernance {
1174
+ self.updateDepositParameters(
1175
+ depositDustThreshold,
1176
+ depositTreasuryFeeDivisor,
1177
+ depositTxMaxFee
1178
+ );
1179
+ }
1180
+
1181
+ /// @notice Updates parameters of redemptions.
1182
+ /// @param redemptionDustThreshold New value of the redemption dust
1183
+ /// threshold in satoshis. It is the minimal amount that can be
1184
+ /// requested for redemption. Value of this parameter must take into
1185
+ /// account the value of `redemptionTreasuryFeeDivisor` and
1186
+ /// `redemptionTxMaxFee` parameters in order to make requests that
1187
+ /// can incur the treasury and transaction fee and still satisfy the
1188
+ /// redeemer.
1189
+ /// @param redemptionTreasuryFeeDivisor New value of the redemption
1190
+ /// treasury fee divisor. It is the divisor used to compute the
1191
+ /// treasury fee taken from each redemption request and transferred
1192
+ /// to the treasury upon successful request finalization. That fee is
1193
+ /// computed as follows:
1194
+ /// `treasuryFee = requestedAmount / redemptionTreasuryFeeDivisor`
1195
+ /// For example, if the treasury fee needs to be 2% of each
1196
+ /// redemption request, the `redemptionTreasuryFeeDivisor` should
1197
+ /// be set to `50` because `1/50 = 0.02 = 2%`.
1198
+ /// @param redemptionTxMaxFee New value of the redemption transaction max
1199
+ /// fee in satoshis. It is the maximum amount of BTC transaction fee
1200
+ /// that can be incurred by each redemption request being part of the
1201
+ /// given redemption transaction. If the maximum BTC transaction fee
1202
+ /// is exceeded, such transaction is considered a fraud.
1203
+ /// This is a per-redemption output max fee for the redemption
1204
+ /// transaction.
1205
+ /// @param redemptionTimeout New value of the redemption timeout in seconds.
1206
+ /// It is the time after which the redemption request can be reported
1207
+ /// as timed out. It is counted from the moment when the redemption
1208
+ /// request was created via `requestRedemption` call. Reported timed
1209
+ /// out requests are cancelled and locked balance is returned to the
1210
+ /// redeemer in full amount.
1211
+ /// @param redemptionTimeoutSlashingAmount New value of the redemption
1212
+ /// timeout slashing amount in T, it is the amount slashed from each
1213
+ /// wallet member for redemption timeout.
1214
+ /// @param redemptionTimeoutNotifierRewardMultiplier New value of the
1215
+ /// redemption timeout notifier reward multiplier as percentage,
1216
+ /// it determines the percentage of the notifier reward from the
1217
+ /// staking contact the notifier of a redemption timeout receives.
1218
+ /// The value must be in the range [0, 100].
1219
+ /// @dev Requirements:
1220
+ /// - Redemption dust threshold must be greater than moving funds dust
1221
+ /// threshold,
1222
+ /// - Redemption treasury fee divisor must be greater than zero,
1223
+ /// - Redemption transaction max fee must be greater than zero,
1224
+ /// - Redemption timeout must be greater than zero,
1225
+ /// - Redemption timeout notifier reward multiplier must be in the
1226
+ /// range [0, 100].
1227
+ function updateRedemptionParameters(
1228
+ uint64 redemptionDustThreshold,
1229
+ uint64 redemptionTreasuryFeeDivisor,
1230
+ uint64 redemptionTxMaxFee,
1231
+ uint256 redemptionTimeout,
1232
+ uint96 redemptionTimeoutSlashingAmount,
1233
+ uint256 redemptionTimeoutNotifierRewardMultiplier
1234
+ ) external onlyGovernance {
1235
+ self.updateRedemptionParameters(
1236
+ redemptionDustThreshold,
1237
+ redemptionTreasuryFeeDivisor,
1238
+ redemptionTxMaxFee,
1239
+ redemptionTimeout,
1240
+ redemptionTimeoutSlashingAmount,
1241
+ redemptionTimeoutNotifierRewardMultiplier
1242
+ );
1243
+ }
1244
+
1245
+ /// @notice Updates parameters of moving funds.
1246
+ /// @param movingFundsTxMaxTotalFee New value of the moving funds transaction
1247
+ /// max total fee in satoshis. It is the maximum amount of the total
1248
+ /// BTC transaction fee that is acceptable in a single moving funds
1249
+ /// transaction. This is a _total_ max fee for the entire moving
1250
+ /// funds transaction.
1251
+ /// @param movingFundsDustThreshold New value of the moving funds dust
1252
+ /// threshold. It is the minimal satoshi amount that makes sense to
1253
+ /// be transferred during the moving funds process. Moving funds
1254
+ /// wallets having their BTC balance below that value can begin
1255
+ /// closing immediately as transferring such a low value may not be
1256
+ /// possible due to BTC network fees.
1257
+ /// @param movingFundsTimeoutResetDelay New value of the moving funds
1258
+ /// timeout reset delay in seconds. It is the time after which the
1259
+ /// moving funds timeout can be reset in case the target wallet
1260
+ /// commitment cannot be submitted due to a lack of live wallets
1261
+ /// in the system. It is counted from the moment when the wallet
1262
+ /// was requested to move their funds and switched to the MovingFunds
1263
+ /// state or from the moment the timeout was reset the last time.
1264
+ /// @param movingFundsTimeout New value of the moving funds timeout in
1265
+ /// seconds. It is the time after which the moving funds process can
1266
+ /// be reported as timed out. It is counted from the moment when the
1267
+ /// wallet was requested to move their funds and switched to the
1268
+ /// MovingFunds state.
1269
+ /// @param movingFundsTimeoutSlashingAmount New value of the moving funds
1270
+ /// timeout slashing amount in T, it is the amount slashed from each
1271
+ /// wallet member for moving funds timeout.
1272
+ /// @param movingFundsTimeoutNotifierRewardMultiplier New value of the
1273
+ /// moving funds timeout notifier reward multiplier as percentage,
1274
+ /// it determines the percentage of the notifier reward from the
1275
+ /// staking contact the notifier of a moving funds timeout receives.
1276
+ /// The value must be in the range [0, 100].
1277
+ /// @param movedFundsSweepTxMaxTotalFee New value of the moved funds sweep
1278
+ /// transaction max total fee in satoshis. It is the maximum amount
1279
+ /// of the total BTC transaction fee that is acceptable in a single
1280
+ /// moved funds sweep transaction. This is a _total_ max fee for the
1281
+ /// entire moved funds sweep transaction.
1282
+ /// @param movedFundsSweepTimeout New value of the moved funds sweep
1283
+ /// timeout in seconds. It is the time after which the moved funds
1284
+ /// sweep process can be reported as timed out. It is counted from
1285
+ /// the moment when the wallet was requested to sweep the received
1286
+ /// funds.
1287
+ /// @param movedFundsSweepTimeoutSlashingAmount New value of the moved
1288
+ /// funds sweep timeout slashing amount in T, it is the amount
1289
+ /// slashed from each wallet member for moved funds sweep timeout.
1290
+ /// @param movedFundsSweepTimeoutNotifierRewardMultiplier New value of
1291
+ /// the moved funds sweep timeout notifier reward multiplier as
1292
+ /// percentage, it determines the percentage of the notifier reward
1293
+ /// from the staking contact the notifier of a moved funds sweep
1294
+ /// timeout receives. The value must be in the range [0, 100].
1295
+ /// @dev Requirements:
1296
+ /// - Moving funds transaction max total fee must be greater than zero,
1297
+ /// - Moving funds dust threshold must be greater than zero and lower
1298
+ /// than the redemption dust threshold,
1299
+ /// - Moving funds timeout reset delay must be greater than zero,
1300
+ /// - Moving funds timeout must be greater than the moving funds
1301
+ /// timeout reset delay,
1302
+ /// - Moving funds timeout notifier reward multiplier must be in the
1303
+ /// range [0, 100],
1304
+ /// - Moved funds sweep transaction max total fee must be greater than zero,
1305
+ /// - Moved funds sweep timeout must be greater than zero,
1306
+ /// - Moved funds sweep timeout notifier reward multiplier must be in the
1307
+ /// range [0, 100].
1308
+ function updateMovingFundsParameters(
1309
+ uint64 movingFundsTxMaxTotalFee,
1310
+ uint64 movingFundsDustThreshold,
1311
+ uint32 movingFundsTimeoutResetDelay,
1312
+ uint32 movingFundsTimeout,
1313
+ uint96 movingFundsTimeoutSlashingAmount,
1314
+ uint256 movingFundsTimeoutNotifierRewardMultiplier,
1315
+ uint64 movedFundsSweepTxMaxTotalFee,
1316
+ uint32 movedFundsSweepTimeout,
1317
+ uint96 movedFundsSweepTimeoutSlashingAmount,
1318
+ uint256 movedFundsSweepTimeoutNotifierRewardMultiplier
1319
+ ) external onlyGovernance {
1320
+ self.updateMovingFundsParameters(
1321
+ movingFundsTxMaxTotalFee,
1322
+ movingFundsDustThreshold,
1323
+ movingFundsTimeoutResetDelay,
1324
+ movingFundsTimeout,
1325
+ movingFundsTimeoutSlashingAmount,
1326
+ movingFundsTimeoutNotifierRewardMultiplier,
1327
+ movedFundsSweepTxMaxTotalFee,
1328
+ movedFundsSweepTimeout,
1329
+ movedFundsSweepTimeoutSlashingAmount,
1330
+ movedFundsSweepTimeoutNotifierRewardMultiplier
1331
+ );
1332
+ }
1333
+
1334
+ /// @notice Updates parameters of wallets.
1335
+ /// @param walletCreationPeriod New value of the wallet creation period in
1336
+ /// seconds, determines how frequently a new wallet creation can be
1337
+ /// requested.
1338
+ /// @param walletCreationMinBtcBalance New value of the wallet minimum BTC
1339
+ /// balance in satoshi, used to decide about wallet creation.
1340
+ /// @param walletCreationMaxBtcBalance New value of the wallet maximum BTC
1341
+ /// balance in satoshi, used to decide about wallet creation.
1342
+ /// @param walletClosureMinBtcBalance New value of the wallet minimum BTC
1343
+ /// balance in satoshi, used to decide about wallet closure.
1344
+ /// @param walletMaxAge New value of the wallet maximum age in seconds,
1345
+ /// indicates the maximum age of a wallet in seconds, after which
1346
+ /// the wallet moving funds process can be requested.
1347
+ /// @param walletMaxBtcTransfer New value of the wallet maximum BTC transfer
1348
+ /// in satoshi, determines the maximum amount that can be transferred
1349
+ // to a single target wallet during the moving funds process.
1350
+ /// @param walletClosingPeriod New value of the wallet closing period in
1351
+ /// seconds, determines the length of the wallet closing period,
1352
+ // i.e. the period when the wallet remains in the Closing state
1353
+ // and can be subject of deposit fraud challenges.
1354
+ /// @dev Requirements:
1355
+ /// - Wallet minimum BTC balance must be greater than zero,
1356
+ /// - Wallet maximum BTC balance must be greater than the wallet
1357
+ /// minimum BTC balance,
1358
+ /// - Wallet maximum BTC transfer must be greater than zero,
1359
+ /// - Wallet closing period must be greater than zero.
1360
+ function updateWalletParameters(
1361
+ uint32 walletCreationPeriod,
1362
+ uint64 walletCreationMinBtcBalance,
1363
+ uint64 walletCreationMaxBtcBalance,
1364
+ uint64 walletClosureMinBtcBalance,
1365
+ uint32 walletMaxAge,
1366
+ uint64 walletMaxBtcTransfer,
1367
+ uint32 walletClosingPeriod
1368
+ ) external onlyGovernance {
1369
+ self.updateWalletParameters(
1370
+ walletCreationPeriod,
1371
+ walletCreationMinBtcBalance,
1372
+ walletCreationMaxBtcBalance,
1373
+ walletClosureMinBtcBalance,
1374
+ walletMaxAge,
1375
+ walletMaxBtcTransfer,
1376
+ walletClosingPeriod
1377
+ );
1378
+ }
1379
+
1380
+ /// @notice Updates parameters related to frauds.
1381
+ /// @param fraudChallengeDepositAmount New value of the fraud challenge
1382
+ /// deposit amount in wei, it is the amount of ETH the party
1383
+ /// challenging the wallet for fraud needs to deposit.
1384
+ /// @param fraudChallengeDefeatTimeout New value of the challenge defeat
1385
+ /// timeout in seconds, it is the amount of time the wallet has to
1386
+ /// defeat a fraud challenge. The value must be greater than zero.
1387
+ /// @param fraudSlashingAmount New value of the fraud slashing amount in T,
1388
+ /// it is the amount slashed from each wallet member for committing
1389
+ /// a fraud.
1390
+ /// @param fraudNotifierRewardMultiplier New value of the fraud notifier
1391
+ /// reward multiplier as percentage, it determines the percentage of
1392
+ /// the notifier reward from the staking contact the notifier of
1393
+ /// a fraud receives. The value must be in the range [0, 100].
1394
+ /// @dev Requirements:
1395
+ /// - Fraud challenge defeat timeout must be greater than 0,
1396
+ /// - Fraud notifier reward multiplier must be in the range [0, 100].
1397
+ function updateFraudParameters(
1398
+ uint256 fraudChallengeDepositAmount,
1399
+ uint256 fraudChallengeDefeatTimeout,
1400
+ uint96 fraudSlashingAmount,
1401
+ uint256 fraudNotifierRewardMultiplier
1402
+ ) external onlyGovernance {
1403
+ self.updateFraudParameters(
1404
+ fraudChallengeDepositAmount,
1405
+ fraudChallengeDefeatTimeout,
1406
+ fraudSlashingAmount,
1407
+ fraudNotifierRewardMultiplier
1408
+ );
1409
+ }
1410
+
1411
+ /// @notice Collection of all revealed deposits indexed by
1412
+ /// keccak256(fundingTxHash | fundingOutputIndex).
1413
+ /// The fundingTxHash is bytes32 (ordered as in Bitcoin internally)
1414
+ /// and fundingOutputIndex an uint32. This mapping may contain valid
1415
+ /// and invalid deposits and the wallet is responsible for
1416
+ /// validating them before attempting to execute a sweep.
1417
+ function deposits(uint256 depositKey)
1418
+ external
1419
+ view
1420
+ returns (Deposit.DepositRequest memory)
1421
+ {
1422
+ return self.deposits[depositKey];
1423
+ }
1424
+
1425
+ /// @notice Collection of all pending redemption requests indexed by
1426
+ /// redemption key built as
1427
+ /// keccak256(walletPubKeyHash | redeemerOutputScript). The
1428
+ /// walletPubKeyHash is the 20-byte wallet's public key hash
1429
+ /// (computed using Bitcoin HASH160 over the compressed ECDSA
1430
+ /// public key) and redeemerOutputScript is a Bitcoin script
1431
+ /// (P2PKH, P2WPKH, P2SH or P2WSH) that will be used to lock
1432
+ /// redeemed BTC as requested by the redeemer. Requests are added
1433
+ /// to this mapping by the `requestRedemption` method (duplicates
1434
+ /// not allowed) and are removed by one of the following methods:
1435
+ /// - `submitRedemptionProof` in case the request was handled
1436
+ /// successfully,
1437
+ /// - `notifyRedemptionTimeout` in case the request was reported
1438
+ /// to be timed out.
1439
+ function pendingRedemptions(uint256 redemptionKey)
1440
+ external
1441
+ view
1442
+ returns (Redemption.RedemptionRequest memory)
1443
+ {
1444
+ return self.pendingRedemptions[redemptionKey];
1445
+ }
1446
+
1447
+ /// @notice Collection of all timed out redemptions requests indexed by
1448
+ /// redemption key built as
1449
+ /// keccak256(walletPubKeyHash | redeemerOutputScript). The
1450
+ /// walletPubKeyHash is the 20-byte wallet's public key hash
1451
+ /// (computed using Bitcoin HASH160 over the compressed ECDSA
1452
+ /// public key) and redeemerOutputScript is the Bitcoin script
1453
+ /// (P2PKH, P2WPKH, P2SH or P2WSH) that is involved in the timed
1454
+ /// out request. Timed out requests are stored in this mapping to
1455
+ /// avoid slashing the wallets multiple times for the same timeout.
1456
+ /// Only one method can add to this mapping:
1457
+ /// - `notifyRedemptionTimeout` which puts the redemption key
1458
+ /// to this mapping basing on a timed out request stored
1459
+ /// previously in `pendingRedemptions` mapping.
1460
+ function timedOutRedemptions(uint256 redemptionKey)
1461
+ external
1462
+ view
1463
+ returns (Redemption.RedemptionRequest memory)
1464
+ {
1465
+ return self.timedOutRedemptions[redemptionKey];
1466
+ }
1467
+
1468
+ /// @notice Collection of main UTXOs that are honestly spent indexed by
1469
+ /// keccak256(fundingTxHash | fundingOutputIndex). The fundingTxHash
1470
+ /// is bytes32 (ordered as in Bitcoin internally) and
1471
+ /// fundingOutputIndex an uint32. A main UTXO is considered honestly
1472
+ /// spent if it was used as an input of a transaction that have been
1473
+ /// proven in the Bridge.
1474
+ function spentMainUTXOs(uint256 utxoKey) external view returns (bool) {
1475
+ return self.spentMainUTXOs[utxoKey];
1476
+ }
1477
+
1478
+ /// @notice Gets details about a registered wallet.
1479
+ /// @param walletPubKeyHash The 20-byte wallet public key hash (computed
1480
+ /// using Bitcoin HASH160 over the compressed ECDSA public key).
1481
+ /// @return Wallet details.
1482
+ function wallets(bytes20 walletPubKeyHash)
1483
+ external
1484
+ view
1485
+ returns (Wallets.Wallet memory)
1486
+ {
1487
+ return self.registeredWallets[walletPubKeyHash];
1488
+ }
1489
+
1490
+ /// @notice Gets the public key hash of the active wallet.
1491
+ /// @return The 20-byte public key hash (computed using Bitcoin HASH160
1492
+ /// over the compressed ECDSA public key) of the active wallet.
1493
+ /// Returns bytes20(0) if there is no active wallet at the moment.
1494
+ function activeWalletPubKeyHash() external view returns (bytes20) {
1495
+ return self.activeWalletPubKeyHash;
1496
+ }
1497
+
1498
+ /// @notice Gets the live wallets count.
1499
+ /// @return The current count of wallets being in the Live state.
1500
+ function liveWalletsCount() external view returns (uint32) {
1501
+ return self.liveWalletsCount;
1502
+ }
1503
+
1504
+ /// @notice Returns the fraud challenge identified by the given key built
1505
+ /// as keccak256(walletPublicKey|sighash).
1506
+ function fraudChallenges(uint256 challengeKey)
1507
+ external
1508
+ view
1509
+ returns (Fraud.FraudChallenge memory)
1510
+ {
1511
+ return self.fraudChallenges[challengeKey];
1512
+ }
1513
+
1514
+ /// @notice Collection of all moved funds sweep requests indexed by
1515
+ /// `keccak256(movingFundsTxHash | movingFundsOutputIndex)`.
1516
+ /// The `movingFundsTxHash` is `bytes32` (ordered as in Bitcoin
1517
+ /// internally) and `movingFundsOutputIndex` an `uint32`. Each entry
1518
+ /// is actually an UTXO representing the moved funds and is supposed
1519
+ /// to be swept with the current main UTXO of the recipient wallet.
1520
+ /// @param requestKey Request key built as
1521
+ /// `keccak256(movingFundsTxHash | movingFundsOutputIndex)`.
1522
+ /// @return Details of the moved funds sweep request.
1523
+ function movedFundsSweepRequests(uint256 requestKey)
1524
+ external
1525
+ view
1526
+ returns (MovingFunds.MovedFundsSweepRequest memory)
1527
+ {
1528
+ return self.movedFundsSweepRequests[requestKey];
1529
+ }
1530
+
1531
+ /// @notice Indicates if the vault with the given address is trusted or not.
1532
+ /// Depositors can route their revealed deposits only to trusted
1533
+ /// vaults and have trusted vaults notified about new deposits as
1534
+ /// soon as these deposits get swept. Vaults not trusted by the
1535
+ /// Bridge can still be used by Bank balance owners on their own
1536
+ /// responsibility - anyone can approve their Bank balance to any
1537
+ /// address.
1538
+ function isVaultTrusted(address vault) external view returns (bool) {
1539
+ return self.isVaultTrusted[vault];
1540
+ }
1541
+
1542
+ /// @notice Returns the current values of Bridge deposit parameters.
1543
+ /// @return depositDustThreshold The minimal amount that can be requested
1544
+ /// to deposit. Value of this parameter must take into account the
1545
+ /// value of `depositTreasuryFeeDivisor` and `depositTxMaxFee`
1546
+ /// parameters in order to make requests that can incur the
1547
+ /// treasury and transaction fee and still satisfy the depositor.
1548
+ /// @return depositTreasuryFeeDivisor Divisor used to compute the treasury
1549
+ /// fee taken from each deposit and transferred to the treasury upon
1550
+ /// sweep proof submission. That fee is computed as follows:
1551
+ /// `treasuryFee = depositedAmount / depositTreasuryFeeDivisor`
1552
+ /// For example, if the treasury fee needs to be 2% of each deposit,
1553
+ /// the `depositTreasuryFeeDivisor` should be set to `50`
1554
+ /// because `1/50 = 0.02 = 2%`.
1555
+ /// @return depositTxMaxFee Maximum amount of BTC transaction fee that can
1556
+ /// be incurred by each swept deposit being part of the given sweep
1557
+ /// transaction. If the maximum BTC transaction fee is exceeded,
1558
+ /// such transaction is considered a fraud.
1559
+ function depositParameters()
1560
+ external
1561
+ view
1562
+ returns (
1563
+ uint64 depositDustThreshold,
1564
+ uint64 depositTreasuryFeeDivisor,
1565
+ uint64 depositTxMaxFee
1566
+ )
1567
+ {
1568
+ depositDustThreshold = self.depositDustThreshold;
1569
+ depositTreasuryFeeDivisor = self.depositTreasuryFeeDivisor;
1570
+ depositTxMaxFee = self.depositTxMaxFee;
1571
+ }
1572
+
1573
+ /// @notice Returns the current values of Bridge redemption parameters.
1574
+ /// @return redemptionDustThreshold The minimal amount that can be requested
1575
+ /// for redemption. Value of this parameter must take into account
1576
+ /// the value of `redemptionTreasuryFeeDivisor` and `redemptionTxMaxFee`
1577
+ /// parameters in order to make requests that can incur the
1578
+ /// treasury and transaction fee and still satisfy the redeemer.
1579
+ /// @return redemptionTreasuryFeeDivisor Divisor used to compute the treasury
1580
+ /// fee taken from each redemption request and transferred to the
1581
+ /// treasury upon successful request finalization. That fee is
1582
+ /// computed as follows:
1583
+ /// `treasuryFee = requestedAmount / redemptionTreasuryFeeDivisor`
1584
+ /// For example, if the treasury fee needs to be 2% of each
1585
+ /// redemption request, the `redemptionTreasuryFeeDivisor` should
1586
+ /// be set to `50` because `1/50 = 0.02 = 2%`.
1587
+ /// @return redemptionTxMaxFee Maximum amount of BTC transaction fee that
1588
+ /// can be incurred by each redemption request being part of the
1589
+ /// given redemption transaction. If the maximum BTC transaction
1590
+ /// fee is exceeded, such transaction is considered a fraud.
1591
+ /// This is a per-redemption output max fee for the redemption
1592
+ /// transaction.
1593
+ /// @return redemptionTimeout Time after which the redemption request can be
1594
+ /// reported as timed out. It is counted from the moment when the
1595
+ /// redemption request was created via `requestRedemption` call.
1596
+ /// Reported timed out requests are cancelled and locked balance is
1597
+ /// returned to the redeemer in full amount.
1598
+ /// @return redemptionTimeoutSlashingAmount The amount of stake slashed
1599
+ /// from each member of a wallet for a redemption timeout.
1600
+ /// @return redemptionTimeoutNotifierRewardMultiplier The percentage of the
1601
+ /// notifier reward from the staking contract the notifier of a
1602
+ /// redemption timeout receives. The value is in the range [0, 100].
1603
+ function redemptionParameters()
1604
+ external
1605
+ view
1606
+ returns (
1607
+ uint64 redemptionDustThreshold,
1608
+ uint64 redemptionTreasuryFeeDivisor,
1609
+ uint64 redemptionTxMaxFee,
1610
+ uint256 redemptionTimeout,
1611
+ uint96 redemptionTimeoutSlashingAmount,
1612
+ uint256 redemptionTimeoutNotifierRewardMultiplier
1613
+ )
1614
+ {
1615
+ redemptionDustThreshold = self.redemptionDustThreshold;
1616
+ redemptionTreasuryFeeDivisor = self.redemptionTreasuryFeeDivisor;
1617
+ redemptionTxMaxFee = self.redemptionTxMaxFee;
1618
+ redemptionTimeout = self.redemptionTimeout;
1619
+ redemptionTimeoutSlashingAmount = self.redemptionTimeoutSlashingAmount;
1620
+ redemptionTimeoutNotifierRewardMultiplier = self
1621
+ .redemptionTimeoutNotifierRewardMultiplier;
1622
+ }
1623
+
1624
+ /// @notice Returns the current values of Bridge moving funds between
1625
+ /// wallets parameters.
1626
+ /// @return movingFundsTxMaxTotalFee Maximum amount of the total BTC
1627
+ /// transaction fee that is acceptable in a single moving funds
1628
+ /// transaction. This is a _total_ max fee for the entire moving
1629
+ /// funds transaction.
1630
+ /// @return movingFundsDustThreshold The minimal satoshi amount that makes
1631
+ /// sense to be transferred during the moving funds process. Moving
1632
+ /// funds wallets having their BTC balance below that value can
1633
+ /// begin closing immediately as transferring such a low value may
1634
+ /// not be possible due to BTC network fees.
1635
+ /// @return movingFundsTimeoutResetDelay Time after which the moving funds
1636
+ /// timeout can be reset in case the target wallet commitment
1637
+ /// cannot be submitted due to a lack of live wallets in the system.
1638
+ /// It is counted from the moment when the wallet was requested to
1639
+ /// move their funds and switched to the MovingFunds state or from
1640
+ /// the moment the timeout was reset the last time. Value in seconds
1641
+ /// This value should be lower than the value of the
1642
+ /// `movingFundsTimeout`.
1643
+ /// @return movingFundsTimeout Time after which the moving funds process
1644
+ /// can be reported as timed out. It is counted from the moment
1645
+ /// when the wallet was requested to move their funds and switched
1646
+ /// to the MovingFunds state. Value in seconds.
1647
+ /// @return movingFundsTimeoutSlashingAmount The amount of stake slashed
1648
+ /// from each member of a wallet for a moving funds timeout.
1649
+ /// @return movingFundsTimeoutNotifierRewardMultiplier The percentage of the
1650
+ /// notifier reward from the staking contract the notifier of a
1651
+ /// moving funds timeout receives. The value is in the range [0, 100].
1652
+ /// @return movedFundsSweepTxMaxTotalFee Maximum amount of the total BTC
1653
+ /// transaction fee that is acceptable in a single moved funds
1654
+ /// sweep transaction. This is a _total_ max fee for the entire
1655
+ /// moved funds sweep transaction.
1656
+ /// @return movedFundsSweepTimeout Time after which the moved funds sweep
1657
+ /// process can be reported as timed out. It is counted from the
1658
+ /// moment when the wallet was requested to sweep the received funds.
1659
+ /// Value in seconds.
1660
+ /// @return movedFundsSweepTimeoutSlashingAmount The amount of stake slashed
1661
+ /// from each member of a wallet for a moved funds sweep timeout.
1662
+ /// @return movedFundsSweepTimeoutNotifierRewardMultiplier The percentage
1663
+ /// of the notifier reward from the staking contract the notifier
1664
+ /// of a moved funds sweep timeout receives. The value is in the
1665
+ /// range [0, 100].
1666
+ function movingFundsParameters()
1667
+ external
1668
+ view
1669
+ returns (
1670
+ uint64 movingFundsTxMaxTotalFee,
1671
+ uint64 movingFundsDustThreshold,
1672
+ uint32 movingFundsTimeoutResetDelay,
1673
+ uint32 movingFundsTimeout,
1674
+ uint96 movingFundsTimeoutSlashingAmount,
1675
+ uint256 movingFundsTimeoutNotifierRewardMultiplier,
1676
+ uint64 movedFundsSweepTxMaxTotalFee,
1677
+ uint32 movedFundsSweepTimeout,
1678
+ uint96 movedFundsSweepTimeoutSlashingAmount,
1679
+ uint256 movedFundsSweepTimeoutNotifierRewardMultiplier
1680
+ )
1681
+ {
1682
+ movingFundsTxMaxTotalFee = self.movingFundsTxMaxTotalFee;
1683
+ movingFundsDustThreshold = self.movingFundsDustThreshold;
1684
+ movingFundsTimeoutResetDelay = self.movingFundsTimeoutResetDelay;
1685
+ movingFundsTimeout = self.movingFundsTimeout;
1686
+ movingFundsTimeoutSlashingAmount = self
1687
+ .movingFundsTimeoutSlashingAmount;
1688
+ movingFundsTimeoutNotifierRewardMultiplier = self
1689
+ .movingFundsTimeoutNotifierRewardMultiplier;
1690
+ movedFundsSweepTxMaxTotalFee = self.movedFundsSweepTxMaxTotalFee;
1691
+ movedFundsSweepTimeout = self.movedFundsSweepTimeout;
1692
+ movedFundsSweepTimeoutSlashingAmount = self
1693
+ .movedFundsSweepTimeoutSlashingAmount;
1694
+ movedFundsSweepTimeoutNotifierRewardMultiplier = self
1695
+ .movedFundsSweepTimeoutNotifierRewardMultiplier;
1696
+ }
1697
+
1698
+ /// @return walletCreationPeriod Determines how frequently a new wallet
1699
+ /// creation can be requested. Value in seconds.
1700
+ /// @return walletCreationMinBtcBalance The minimum BTC threshold in satoshi
1701
+ /// that is used to decide about wallet creation.
1702
+ /// @return walletCreationMaxBtcBalance The maximum BTC threshold in satoshi
1703
+ /// that is used to decide about wallet creation.
1704
+ /// @return walletClosureMinBtcBalance The minimum BTC threshold in satoshi
1705
+ /// that is used to decide about wallet closure.
1706
+ /// @return walletMaxAge The maximum age of a wallet in seconds, after which
1707
+ /// the wallet moving funds process can be requested.
1708
+ /// @return walletMaxBtcTransfer The maximum BTC amount in satoshi than
1709
+ /// can be transferred to a single target wallet during the moving
1710
+ /// funds process.
1711
+ /// @return walletClosingPeriod Determines the length of the wallet closing
1712
+ /// period, i.e. the period when the wallet remains in the Closing
1713
+ /// state and can be subject of deposit fraud challenges. Value
1714
+ /// in seconds.
1715
+ function walletParameters()
1716
+ external
1717
+ view
1718
+ returns (
1719
+ uint32 walletCreationPeriod,
1720
+ uint64 walletCreationMinBtcBalance,
1721
+ uint64 walletCreationMaxBtcBalance,
1722
+ uint64 walletClosureMinBtcBalance,
1723
+ uint32 walletMaxAge,
1724
+ uint64 walletMaxBtcTransfer,
1725
+ uint32 walletClosingPeriod
1726
+ )
1727
+ {
1728
+ walletCreationPeriod = self.walletCreationPeriod;
1729
+ walletCreationMinBtcBalance = self.walletCreationMinBtcBalance;
1730
+ walletCreationMaxBtcBalance = self.walletCreationMaxBtcBalance;
1731
+ walletClosureMinBtcBalance = self.walletClosureMinBtcBalance;
1732
+ walletMaxAge = self.walletMaxAge;
1733
+ walletMaxBtcTransfer = self.walletMaxBtcTransfer;
1734
+ walletClosingPeriod = self.walletClosingPeriod;
1735
+ }
1736
+
1737
+ /// @notice Returns the current values of Bridge fraud parameters.
1738
+ /// @return fraudChallengeDepositAmount The amount of ETH in wei the party
1739
+ /// challenging the wallet for fraud needs to deposit.
1740
+ /// @return fraudChallengeDefeatTimeout The amount of time the wallet has to
1741
+ /// defeat a fraud challenge.
1742
+ /// @return fraudSlashingAmount The amount slashed from each wallet member
1743
+ /// for committing a fraud.
1744
+ /// @return fraudNotifierRewardMultiplier The percentage of the notifier
1745
+ /// reward from the staking contract the notifier of a fraud
1746
+ /// receives. The value is in the range [0, 100].
1747
+ function fraudParameters()
1748
+ external
1749
+ view
1750
+ returns (
1751
+ uint256 fraudChallengeDepositAmount,
1752
+ uint256 fraudChallengeDefeatTimeout,
1753
+ uint96 fraudSlashingAmount,
1754
+ uint256 fraudNotifierRewardMultiplier
1755
+ )
1756
+ {
1757
+ fraudChallengeDepositAmount = self.fraudChallengeDepositAmount;
1758
+ fraudChallengeDefeatTimeout = self.fraudChallengeDefeatTimeout;
1759
+ fraudSlashingAmount = self.fraudSlashingAmount;
1760
+ fraudNotifierRewardMultiplier = self.fraudNotifierRewardMultiplier;
1761
+ }
1762
+
1763
+ /// @notice Returns the addresses of contracts Bridge is interacting with.
1764
+ /// @return bank Address of the Bank the Bridge belongs to.
1765
+ /// @return relay Address of the Bitcoin relay providing the current Bitcoin
1766
+ /// network difficulty.
1767
+ /// @return ecdsaWalletRegistry Address of the ECDSA Wallet Registry.
1768
+ function contractReferences()
1769
+ external
1770
+ view
1771
+ returns (
1772
+ Bank bank,
1773
+ IRelay relay,
1774
+ EcdsaWalletRegistry ecdsaWalletRegistry
1775
+ )
1776
+ {
1777
+ bank = self.bank;
1778
+ relay = self.relay;
1779
+ ecdsaWalletRegistry = self.ecdsaWalletRegistry;
1780
+ }
1781
+
1782
+ /// @notice Address where the deposit treasury fees will be sent to.
1783
+ /// Treasury takes part in the operators rewarding process.
1784
+ function treasury() external view returns (address) {
1785
+ return self.treasury;
1786
+ }
1787
+
1788
+ /// @notice The number of confirmations on the Bitcoin chain required to
1789
+ /// successfully evaluate an SPV proof.
1790
+ function txProofDifficultyFactor() external view returns (uint256) {
1791
+ return self.txProofDifficultyFactor;
1792
+ }
345
1793
  }