@keep-network/tbtc-v2 0.1.1-dev.54 → 0.1.1-dev.57
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.
- package/artifacts/Bank.json +3 -3
- package/artifacts/Bridge.json +581 -234
- package/artifacts/Deposit.json +9 -9
- package/artifacts/DepositSweep.json +76 -0
- package/artifacts/EcdsaDkgValidator.json +1 -1
- package/artifacts/EcdsaInactivity.json +1 -1
- package/artifacts/Fraud.json +9 -9
- package/artifacts/KeepRegistry.json +1 -1
- package/artifacts/KeepStake.json +2 -2
- package/artifacts/KeepToken.json +2 -2
- package/artifacts/KeepTokenStaking.json +1 -1
- package/artifacts/MovingFunds.json +41 -11
- package/artifacts/NuCypherStakingEscrow.json +1 -1
- package/artifacts/NuCypherToken.json +2 -2
- package/artifacts/RandomBeaconStub.json +1 -1
- package/artifacts/Redemption.json +9 -9
- package/artifacts/ReimbursementPool.json +2 -2
- package/artifacts/Relay.json +9 -9
- package/artifacts/SortitionPool.json +2 -2
- package/artifacts/T.json +2 -2
- package/artifacts/TBTC.json +3 -3
- package/artifacts/TBTCToken.json +3 -3
- package/artifacts/TokenStaking.json +1 -1
- package/artifacts/TokenholderGovernor.json +9 -9
- package/artifacts/TokenholderTimelock.json +8 -8
- package/artifacts/VendingMachine.json +10 -10
- package/artifacts/VendingMachineKeep.json +1 -1
- package/artifacts/VendingMachineNuCypher.json +1 -1
- package/artifacts/WalletRegistry.json +2 -2
- package/artifacts/WalletRegistryGovernance.json +2 -2
- package/artifacts/Wallets.json +9 -9
- package/artifacts/solcInputs/{799c7b289d9b6d9cfc600d2fc23c7b64.json → d1140ac40baa1ba6f0d558afe0fa6d54.json} +10 -10
- package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
- package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
- package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +1 -1
- package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +2 -2
- package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
- package/build/contracts/bridge/Bridge.sol/Bridge.json +296 -128
- package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +1 -1
- package/build/contracts/bridge/BridgeState.sol/BridgeState.json +17 -11
- package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +1 -1
- package/build/contracts/bridge/Deposit.sol/Deposit.json +2 -2
- package/build/contracts/bridge/DepositSweep.sol/DepositSweep.dbg.json +4 -0
- package/build/contracts/bridge/DepositSweep.sol/DepositSweep.json +30 -0
- package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +1 -1
- package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +1 -1
- package/build/contracts/bridge/Fraud.sol/Fraud.json +2 -2
- package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +1 -1
- package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +1 -1
- package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +21 -2
- package/build/contracts/bridge/Redemption.sol/OutboundTx.dbg.json +1 -1
- package/build/contracts/bridge/Redemption.sol/OutboundTx.json +2 -2
- package/build/contracts/bridge/Redemption.sol/Redemption.dbg.json +1 -1
- package/build/contracts/bridge/Redemption.sol/Redemption.json +2 -2
- package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
- package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +1 -1
- package/build/contracts/bridge/Wallets.sol/Wallets.json +2 -2
- package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
- package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
- package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
- package/contracts/bridge/BitcoinTx.sol +69 -0
- package/contracts/bridge/Bridge.sol +132 -49
- package/contracts/bridge/BridgeState.sol +77 -46
- package/contracts/bridge/{Sweep.sol → DepositSweep.sol} +30 -18
- package/contracts/bridge/MovingFunds.sol +480 -76
- package/contracts/bridge/Wallets.sol +2 -0
- package/deploy/05_deploy_bridge.ts +5 -2
- package/export.json +348 -81
- package/package.json +1 -1
- package/artifacts/Sweep.json +0 -76
- package/build/contracts/bridge/Sweep.sol/Sweep.dbg.json +0 -4
- package/build/contracts/bridge/Sweep.sol/Sweep.json +0 -30
|
@@ -31,6 +31,9 @@ import "./Wallets.sol";
|
|
|
31
31
|
/// funds to other wallets in the Bridge. The wallet needs to commit to
|
|
32
32
|
/// which other Live wallets it is moving the funds to and then, provide an
|
|
33
33
|
/// SPV proof of moving funds to the previously committed wallets.
|
|
34
|
+
/// Once the proof is submitted, all target wallets are supposed to
|
|
35
|
+
/// sweep the received UTXOs with their own main UTXOs in order to
|
|
36
|
+
/// update their BTC balances.
|
|
34
37
|
library MovingFunds {
|
|
35
38
|
using BridgeState for BridgeState.Storage;
|
|
36
39
|
using Wallets for BridgeState.Storage;
|
|
@@ -39,6 +42,38 @@ library MovingFunds {
|
|
|
39
42
|
using BTCUtils for bytes;
|
|
40
43
|
using BytesLib for bytes;
|
|
41
44
|
|
|
45
|
+
/// @notice Represents temporary information needed during the processing
|
|
46
|
+
/// of the moving funds Bitcoin transaction outputs. This structure
|
|
47
|
+
/// is an internal one and should not be exported outside of the
|
|
48
|
+
/// moving funds transaction processing code.
|
|
49
|
+
/// @dev Allows to mitigate "stack too deep" errors on EVM.
|
|
50
|
+
struct MovingFundsTxOutputsProcessingInfo {
|
|
51
|
+
// 32-byte hash of the moving funds Bitcoin transaction.
|
|
52
|
+
bytes32 movingFundsTxHash;
|
|
53
|
+
// Output vector of the moving funds Bitcoin transaction. It is
|
|
54
|
+
// assumed the vector's structure is valid so it must be validated
|
|
55
|
+
// using e.g. `BTCUtils.validateVout` function before being used
|
|
56
|
+
// during the processing. The validation is usually done as part
|
|
57
|
+
// of the `BitcoinTx.validateProof` call that checks the SPV proof.
|
|
58
|
+
bytes movingFundsTxOutputVector;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// @notice Represents a moved funds sweep request. The request is
|
|
62
|
+
/// registered in `submitMovingFundsProof` where we know funds
|
|
63
|
+
/// have been moved to the target wallet and the only step left is
|
|
64
|
+
/// to have the target wallet sweep them.
|
|
65
|
+
struct MovedFundsSweepRequest {
|
|
66
|
+
// 20-byte public key hash of the wallet supposed to sweep the UTXO
|
|
67
|
+
// representing the received funds with their own main UTXO
|
|
68
|
+
bytes20 walletPubKeyHash;
|
|
69
|
+
// Value of the received funds.
|
|
70
|
+
uint64 value;
|
|
71
|
+
// UNIX timestamp the request was created at.
|
|
72
|
+
uint32 createdAt;
|
|
73
|
+
// UNIX timestamp the funds were swept at.
|
|
74
|
+
uint32 sweptAt;
|
|
75
|
+
}
|
|
76
|
+
|
|
42
77
|
event MovingFundsCommitmentSubmitted(
|
|
43
78
|
bytes20 walletPubKeyHash,
|
|
44
79
|
bytes20[] targetWallets,
|
|
@@ -54,6 +89,8 @@ library MovingFunds {
|
|
|
54
89
|
|
|
55
90
|
event MovingFundsBelowDustReported(bytes20 walletPubKeyHash);
|
|
56
91
|
|
|
92
|
+
event MovedFundsSwept(bytes20 walletPubKeyHash, bytes32 sweepTxHash);
|
|
93
|
+
|
|
57
94
|
/// @notice Submits the moving funds target wallets commitment.
|
|
58
95
|
/// Once all requirements are met, that function registers the
|
|
59
96
|
/// target wallets commitment and opens the way for moving funds
|
|
@@ -70,6 +107,7 @@ library MovingFunds {
|
|
|
70
107
|
/// @dev Requirements:
|
|
71
108
|
/// - The source wallet must be in the MovingFunds state
|
|
72
109
|
/// - The source wallet must not have pending redemption requests
|
|
110
|
+
/// - The source wallet must not have pending moved funds sweep requests
|
|
73
111
|
/// - The source wallet must not have submitted its commitment already
|
|
74
112
|
/// - The expression `keccak256(abi.encode(walletMembersIDs))` must
|
|
75
113
|
/// be exactly the same as the hash stored under `membersIdsHash`
|
|
@@ -115,6 +153,11 @@ library MovingFunds {
|
|
|
115
153
|
"Source wallet must handle all pending redemptions first"
|
|
116
154
|
);
|
|
117
155
|
|
|
156
|
+
require(
|
|
157
|
+
wallet.pendingMovedFundsSweepRequestsCount == 0,
|
|
158
|
+
"Source wallet must handle all pending moved funds sweep requests first"
|
|
159
|
+
);
|
|
160
|
+
|
|
118
161
|
require(
|
|
119
162
|
wallet.movingFundsTargetWalletsCommitmentHash == bytes32(0),
|
|
120
163
|
"Target wallets commitment already submitted"
|
|
@@ -263,7 +306,13 @@ library MovingFunds {
|
|
|
263
306
|
(
|
|
264
307
|
bytes32 targetWalletsHash,
|
|
265
308
|
uint256 outputsTotalValue
|
|
266
|
-
) = processMovingFundsTxOutputs(
|
|
309
|
+
) = processMovingFundsTxOutputs(
|
|
310
|
+
self,
|
|
311
|
+
MovingFundsTxOutputsProcessingInfo(
|
|
312
|
+
movingFundsTxHash,
|
|
313
|
+
movingFundsTx.outputVector
|
|
314
|
+
)
|
|
315
|
+
);
|
|
267
316
|
|
|
268
317
|
require(
|
|
269
318
|
mainUtxo.txOutputValue - outputsTotalValue <=
|
|
@@ -278,10 +327,8 @@ library MovingFunds {
|
|
|
278
327
|
|
|
279
328
|
/// @notice Processes the moving funds Bitcoin transaction output vector
|
|
280
329
|
/// and extracts information required for further processing.
|
|
281
|
-
/// @param
|
|
282
|
-
///
|
|
283
|
-
/// must be validated using e.g. `BTCUtils.validateVout` function
|
|
284
|
-
/// before it is passed here
|
|
330
|
+
/// @param processInfo Processing info containing the moving funds tx
|
|
331
|
+
/// hash and output vector.
|
|
285
332
|
/// @return targetWalletsHash keccak256 hash over the list of actual
|
|
286
333
|
/// target wallets used in the transaction.
|
|
287
334
|
/// @return outputsTotalValue Sum of all outputs values.
|
|
@@ -290,18 +337,17 @@ library MovingFunds {
|
|
|
290
337
|
/// be validated by the caller as stated in their parameter doc.
|
|
291
338
|
/// - Each output must refer to a 20-byte public key hash.
|
|
292
339
|
/// - The total outputs value must be evenly divided over all outputs.
|
|
293
|
-
function processMovingFundsTxOutputs(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
{
|
|
340
|
+
function processMovingFundsTxOutputs(
|
|
341
|
+
BridgeState.Storage storage self,
|
|
342
|
+
MovingFundsTxOutputsProcessingInfo memory processInfo
|
|
343
|
+
) internal returns (bytes32 targetWalletsHash, uint256 outputsTotalValue) {
|
|
298
344
|
// Determining the total number of Bitcoin transaction outputs in
|
|
299
345
|
// the same way as for number of inputs. See `BitcoinTx.outputVector`
|
|
300
346
|
// docs for more details.
|
|
301
347
|
(
|
|
302
348
|
uint256 outputsCompactSizeUintLength,
|
|
303
349
|
uint256 outputsCount
|
|
304
|
-
) = movingFundsTxOutputVector.parseVarInt();
|
|
350
|
+
) = processInfo.movingFundsTxOutputVector.parseVarInt();
|
|
305
351
|
|
|
306
352
|
// To determine the first output starting index, we must jump over
|
|
307
353
|
// the compactSize uint which prepends the output vector. One byte
|
|
@@ -323,85 +369,64 @@ library MovingFunds {
|
|
|
323
369
|
bytes20[] memory targetWallets = new bytes20[](outputsCount);
|
|
324
370
|
uint64[] memory outputsValues = new uint64[](outputsCount);
|
|
325
371
|
|
|
326
|
-
// Outputs processing loop.
|
|
327
|
-
|
|
328
|
-
|
|
372
|
+
// Outputs processing loop. Note that the `outputIndex` must be
|
|
373
|
+
// `uint32` to build proper `movedFundsSweepRequests` keys.
|
|
374
|
+
for (
|
|
375
|
+
uint32 outputIndex = 0;
|
|
376
|
+
outputIndex < outputsCount;
|
|
377
|
+
outputIndex++
|
|
378
|
+
) {
|
|
379
|
+
uint256 outputLength = processInfo
|
|
380
|
+
.movingFundsTxOutputVector
|
|
329
381
|
.determineOutputLengthAt(outputStartingIndex);
|
|
330
382
|
|
|
331
|
-
bytes memory output = movingFundsTxOutputVector.slice(
|
|
383
|
+
bytes memory output = processInfo.movingFundsTxOutputVector.slice(
|
|
332
384
|
outputStartingIndex,
|
|
333
385
|
outputLength
|
|
334
386
|
);
|
|
335
387
|
|
|
336
|
-
|
|
337
|
-
bytes memory targetWalletPubKeyHashBytes = output.extractHash();
|
|
338
|
-
// Output script payload must refer to a known wallet public key
|
|
339
|
-
// hash which is always 20-byte.
|
|
340
|
-
require(
|
|
341
|
-
targetWalletPubKeyHashBytes.length == 20,
|
|
342
|
-
"Target wallet public key hash must have 20 bytes"
|
|
343
|
-
);
|
|
344
|
-
|
|
345
|
-
bytes20 targetWalletPubKeyHash = targetWalletPubKeyHashBytes
|
|
346
|
-
.slice20(0);
|
|
347
|
-
|
|
348
|
-
// The next step is making sure that the 20-byte public key hash
|
|
349
|
-
// is actually used in the right context of a P2PKH or P2WPKH
|
|
350
|
-
// output. To do so, we must extract the full script from the output
|
|
351
|
-
// and compare with the expected P2PKH and P2WPKH scripts
|
|
352
|
-
// referring to that 20-byte public key hash. The output consists
|
|
353
|
-
// of an 8-byte value and a variable length script. To extract the
|
|
354
|
-
// script we slice the output starting from 9th byte until the end.
|
|
355
|
-
bytes32 outputScriptKeccak = keccak256(
|
|
356
|
-
output.slice(8, output.length - 8)
|
|
357
|
-
);
|
|
358
|
-
// Build the expected P2PKH script which has the following byte
|
|
359
|
-
// format: <0x1976a914> <20-byte PKH> <0x88ac>. According to
|
|
360
|
-
// https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
361
|
-
// - 0x19: Byte length of the entire script
|
|
362
|
-
// - 0x76: OP_DUP
|
|
363
|
-
// - 0xa9: OP_HASH160
|
|
364
|
-
// - 0x14: Byte length of the public key hash
|
|
365
|
-
// - 0x88: OP_EQUALVERIFY
|
|
366
|
-
// - 0xac: OP_CHECKSIG
|
|
367
|
-
// which matches the P2PKH structure as per:
|
|
368
|
-
// https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash
|
|
369
|
-
bytes32 targetWalletP2PKHScriptKeccak = keccak256(
|
|
370
|
-
abi.encodePacked(
|
|
371
|
-
hex"1976a914",
|
|
372
|
-
targetWalletPubKeyHash,
|
|
373
|
-
hex"88ac"
|
|
374
|
-
)
|
|
375
|
-
);
|
|
376
|
-
// Build the expected P2WPKH script which has the following format:
|
|
377
|
-
// <0x160014> <20-byte PKH>. According to
|
|
378
|
-
// https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
379
|
-
// - 0x16: Byte length of the entire script
|
|
380
|
-
// - 0x00: OP_0
|
|
381
|
-
// - 0x14: Byte length of the public key hash
|
|
382
|
-
// which matches the P2WPKH structure as per:
|
|
383
|
-
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH
|
|
384
|
-
bytes32 targetWalletP2WPKHScriptKeccak = keccak256(
|
|
385
|
-
abi.encodePacked(hex"160014", targetWalletPubKeyHash)
|
|
386
|
-
);
|
|
387
|
-
// Make sure the actual output script matches either the P2PKH
|
|
388
|
-
// or P2WPKH format.
|
|
389
|
-
require(
|
|
390
|
-
outputScriptKeccak == targetWalletP2PKHScriptKeccak ||
|
|
391
|
-
outputScriptKeccak == targetWalletP2WPKHScriptKeccak,
|
|
392
|
-
"Output must be P2PKH or P2WPKH"
|
|
393
|
-
);
|
|
388
|
+
bytes20 targetWalletPubKeyHash = self.extractPubKeyHash(output);
|
|
394
389
|
|
|
395
390
|
// Add the wallet public key hash to the list that will be used
|
|
396
391
|
// to build the result list hash. There is no need to check if
|
|
397
392
|
// given output is a change here because the actual target wallet
|
|
398
393
|
// list must be exactly the same as the pre-committed target wallet
|
|
399
394
|
// list which is guaranteed to be valid.
|
|
400
|
-
targetWallets[
|
|
395
|
+
targetWallets[outputIndex] = targetWalletPubKeyHash;
|
|
401
396
|
|
|
402
397
|
// Extract the value from given output.
|
|
403
|
-
outputsValues[
|
|
404
|
-
outputsTotalValue += outputsValues[
|
|
398
|
+
outputsValues[outputIndex] = output.extractValue();
|
|
399
|
+
outputsTotalValue += outputsValues[outputIndex];
|
|
400
|
+
|
|
401
|
+
// Register a moved funds sweep request that must be handled
|
|
402
|
+
// by the target wallet. The target wallet must sweep the
|
|
403
|
+
// received funds with their own main UTXO in order to update
|
|
404
|
+
// their BTC balance. Worth noting there is no need to check
|
|
405
|
+
// if the sweep request already exists in the system because
|
|
406
|
+
// the moving funds wallet is moved to the Closing state after
|
|
407
|
+
// submitting the moving funds proof so there is no possibility
|
|
408
|
+
// to submit the proof again and register the sweep request twice.
|
|
409
|
+
self.movedFundsSweepRequests[
|
|
410
|
+
uint256(
|
|
411
|
+
keccak256(
|
|
412
|
+
abi.encodePacked(
|
|
413
|
+
processInfo.movingFundsTxHash,
|
|
414
|
+
outputIndex
|
|
415
|
+
)
|
|
416
|
+
)
|
|
417
|
+
)
|
|
418
|
+
] = MovedFundsSweepRequest(
|
|
419
|
+
targetWalletPubKeyHash,
|
|
420
|
+
outputsValues[outputIndex],
|
|
421
|
+
/* solhint-disable-next-line not-rely-on-time */
|
|
422
|
+
uint32(block.timestamp),
|
|
423
|
+
0
|
|
424
|
+
);
|
|
425
|
+
// We added a new moved funds sweep request for the target wallet
|
|
426
|
+
// so we must increment their request counter.
|
|
427
|
+
self
|
|
428
|
+
.registeredWallets[targetWalletPubKeyHash]
|
|
429
|
+
.pendingMovedFundsSweepRequestsCount++;
|
|
405
430
|
|
|
406
431
|
// Make the `outputStartingIndex` pointing to the next output by
|
|
407
432
|
// increasing it by current output's length.
|
|
@@ -521,4 +546,383 @@ library MovingFunds {
|
|
|
521
546
|
// slither-disable-next-line reentrancy-events
|
|
522
547
|
emit MovingFundsBelowDustReported(walletPubKeyHash);
|
|
523
548
|
}
|
|
549
|
+
|
|
550
|
+
/// @notice Used by the wallet to prove the BTC moved funds sweep
|
|
551
|
+
/// transaction and to make the necessary state changes. Moved
|
|
552
|
+
/// funds sweep is only accepted if it satisfies SPV proof.
|
|
553
|
+
///
|
|
554
|
+
/// The function validates the sweep transaction structure by
|
|
555
|
+
/// checking if it actually spends the moved funds UTXO and the
|
|
556
|
+
/// sweeping wallet's main UTXO (optionally), and if it locks the
|
|
557
|
+
/// value on the sweeping wallet's 20-byte public key hash using a
|
|
558
|
+
/// reasonable transaction fee. If all preconditions are
|
|
559
|
+
/// met, this function updates the sweeping wallet main UTXO, thus
|
|
560
|
+
/// their BTC balance.
|
|
561
|
+
///
|
|
562
|
+
/// It is possible to prove the given sweep transaction only
|
|
563
|
+
/// one time.
|
|
564
|
+
/// @param sweepTx Bitcoin sweep funds transaction data
|
|
565
|
+
/// @param sweepProof Bitcoin sweep funds proof data
|
|
566
|
+
/// @param mainUtxo Data of the sweeping wallet's main UTXO, as currently
|
|
567
|
+
/// known on the Ethereum chain
|
|
568
|
+
/// @dev Requirements:
|
|
569
|
+
/// - `sweepTx` components must match the expected structure. See
|
|
570
|
+
/// `BitcoinTx.Info` docs for reference. Their values must exactly
|
|
571
|
+
/// correspond to appropriate Bitcoin transaction fields to produce
|
|
572
|
+
/// a provable transaction hash.
|
|
573
|
+
/// - The `sweepTx` should represent a Bitcoin transaction with
|
|
574
|
+
/// the first input pointing to a wallet's sweep request and,
|
|
575
|
+
/// optionally, the second input pointing to the wallet's main UTXO,
|
|
576
|
+
/// if the sweeping wallet has a main UTXO set. There should be only
|
|
577
|
+
/// one output locking funds on the sweeping wallet 20-byte public
|
|
578
|
+
/// key hash.
|
|
579
|
+
/// - `sweepProof` components must match the expected structure.
|
|
580
|
+
/// See `BitcoinTx.Proof` docs for reference. The `bitcoinHeaders`
|
|
581
|
+
/// field must contain a valid number of block headers, not less
|
|
582
|
+
/// than the `txProofDifficultyFactor` contract constant.
|
|
583
|
+
/// - `mainUtxo` components must point to the recent main UTXO
|
|
584
|
+
/// of the sweeping wallet, as currently known on the Ethereum chain.
|
|
585
|
+
/// If there is no main UTXO, this parameter is ignored.
|
|
586
|
+
/// - The sweeping wallet must be in the Live or MovingFunds state.
|
|
587
|
+
/// - The total Bitcoin transaction fee must be lesser or equal
|
|
588
|
+
/// to `movedFundsSweepTxMaxTotalFee` governable parameter.
|
|
589
|
+
function submitMovedFundsSweepProof(
|
|
590
|
+
BridgeState.Storage storage self,
|
|
591
|
+
BitcoinTx.Info calldata sweepTx,
|
|
592
|
+
BitcoinTx.Proof calldata sweepProof,
|
|
593
|
+
BitcoinTx.UTXO calldata mainUtxo
|
|
594
|
+
) external {
|
|
595
|
+
// The actual transaction proof is performed here. After that point, we
|
|
596
|
+
// can assume the transaction happened on Bitcoin chain and has
|
|
597
|
+
// a sufficient number of confirmations as determined by
|
|
598
|
+
// `txProofDifficultyFactor` constant.
|
|
599
|
+
bytes32 sweepTxHash = self.validateProof(sweepTx, sweepProof);
|
|
600
|
+
|
|
601
|
+
(
|
|
602
|
+
bytes20 walletPubKeyHash,
|
|
603
|
+
uint64 sweepTxOutputValue
|
|
604
|
+
) = processMovedFundsSweepTxOutput(self, sweepTx.outputVector);
|
|
605
|
+
|
|
606
|
+
(
|
|
607
|
+
Wallets.Wallet storage wallet,
|
|
608
|
+
BitcoinTx.UTXO memory resolvedMainUtxo
|
|
609
|
+
) = resolveMovedFundsSweepingWallet(self, walletPubKeyHash, mainUtxo);
|
|
610
|
+
|
|
611
|
+
uint256 sweepTxInputsTotalValue = processMovedFundsSweepTxInputs(
|
|
612
|
+
self,
|
|
613
|
+
sweepTx.inputVector,
|
|
614
|
+
resolvedMainUtxo,
|
|
615
|
+
walletPubKeyHash
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
require(
|
|
619
|
+
sweepTxInputsTotalValue - sweepTxOutputValue <=
|
|
620
|
+
self.movedFundsSweepTxMaxTotalFee,
|
|
621
|
+
"Transaction fee is too high"
|
|
622
|
+
);
|
|
623
|
+
|
|
624
|
+
// Use the sweep transaction output as the new sweeping wallet's main UTXO.
|
|
625
|
+
// Transaction output index is always 0 as sweep transaction always
|
|
626
|
+
// contains only one output.
|
|
627
|
+
wallet.mainUtxoHash = keccak256(
|
|
628
|
+
abi.encodePacked(sweepTxHash, uint32(0), sweepTxOutputValue)
|
|
629
|
+
);
|
|
630
|
+
|
|
631
|
+
// slither-disable-next-line reentrancy-events
|
|
632
|
+
emit MovedFundsSwept(walletPubKeyHash, sweepTxHash);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/// @notice Processes the Bitcoin moved funds sweep transaction output vector
|
|
636
|
+
/// by extracting the single output and using it to gain additional
|
|
637
|
+
/// information required for further processing (e.g. value and
|
|
638
|
+
/// wallet public key hash).
|
|
639
|
+
/// @param sweepTxOutputVector Bitcoin moved funds sweep transaction output
|
|
640
|
+
/// vector.
|
|
641
|
+
/// This function assumes vector's structure is valid so it must be
|
|
642
|
+
/// validated using e.g. `BTCUtils.validateVout` function before
|
|
643
|
+
/// it is passed here
|
|
644
|
+
/// @return walletPubKeyHash 20-byte wallet public key hash.
|
|
645
|
+
/// @return value 8-byte moved funds sweep transaction output value.
|
|
646
|
+
/// @dev Requirements:
|
|
647
|
+
/// - Output vector must contain only one output
|
|
648
|
+
/// - The single output must be of P2PKH or P2WPKH type and lock the
|
|
649
|
+
/// funds on a 20-byte public key hash
|
|
650
|
+
function processMovedFundsSweepTxOutput(
|
|
651
|
+
BridgeState.Storage storage self,
|
|
652
|
+
bytes memory sweepTxOutputVector
|
|
653
|
+
) internal view returns (bytes20 walletPubKeyHash, uint64 value) {
|
|
654
|
+
// To determine the total number of sweep transaction outputs, we need to
|
|
655
|
+
// parse the compactSize uint (VarInt) the output vector is prepended by.
|
|
656
|
+
// That compactSize uint encodes the number of vector elements using the
|
|
657
|
+
// format presented in:
|
|
658
|
+
// https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
|
|
659
|
+
// We don't need asserting the compactSize uint is parseable since it
|
|
660
|
+
// was already checked during `validateVout` validation performed as
|
|
661
|
+
// part of the `BitcoinTx.validateProof` call.
|
|
662
|
+
// See `BitcoinTx.outputVector` docs for more details.
|
|
663
|
+
(, uint256 outputsCount) = sweepTxOutputVector.parseVarInt();
|
|
664
|
+
require(
|
|
665
|
+
outputsCount == 1,
|
|
666
|
+
"Moved funds sweep transaction must have a single output"
|
|
667
|
+
);
|
|
668
|
+
|
|
669
|
+
bytes memory output = sweepTxOutputVector.extractOutputAtIndex(0);
|
|
670
|
+
walletPubKeyHash = self.extractPubKeyHash(output);
|
|
671
|
+
value = output.extractValue();
|
|
672
|
+
|
|
673
|
+
return (walletPubKeyHash, value);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/// @notice Resolves sweeping wallet based on the provided wallet public key
|
|
677
|
+
/// hash. Validates the wallet state and current main UTXO, as
|
|
678
|
+
/// currently known on the Ethereum chain.
|
|
679
|
+
/// @param walletPubKeyHash public key hash of the wallet proving the sweep
|
|
680
|
+
/// Bitcoin transaction.
|
|
681
|
+
/// @param mainUtxo Data of the wallet's main UTXO, as currently known on
|
|
682
|
+
/// the Ethereum chain. If no main UTXO exists for the given wallet,
|
|
683
|
+
/// this parameter is ignored
|
|
684
|
+
/// @return wallet Data of the sweeping wallet.
|
|
685
|
+
/// @return resolvedMainUtxo The actual main UTXO of the sweeping wallet
|
|
686
|
+
/// resolved by cross-checking the `mainUtxo` parameter with
|
|
687
|
+
/// the chain state. If the validation went well, this is the
|
|
688
|
+
/// plain-text main UTXO corresponding to the `wallet.mainUtxoHash`.
|
|
689
|
+
/// @dev Requirements:
|
|
690
|
+
/// - Sweeping wallet must be either in Live or MovingFunds state.
|
|
691
|
+
/// - If the main UTXO of the sweeping wallet exists in the storage,
|
|
692
|
+
/// the passed `mainUTXO` parameter must be equal to the stored one.
|
|
693
|
+
function resolveMovedFundsSweepingWallet(
|
|
694
|
+
BridgeState.Storage storage self,
|
|
695
|
+
bytes20 walletPubKeyHash,
|
|
696
|
+
BitcoinTx.UTXO calldata mainUtxo
|
|
697
|
+
)
|
|
698
|
+
internal
|
|
699
|
+
view
|
|
700
|
+
returns (
|
|
701
|
+
Wallets.Wallet storage wallet,
|
|
702
|
+
BitcoinTx.UTXO memory resolvedMainUtxo
|
|
703
|
+
)
|
|
704
|
+
{
|
|
705
|
+
wallet = self.registeredWallets[walletPubKeyHash];
|
|
706
|
+
|
|
707
|
+
Wallets.WalletState walletState = wallet.state;
|
|
708
|
+
require(
|
|
709
|
+
walletState == Wallets.WalletState.Live ||
|
|
710
|
+
walletState == Wallets.WalletState.MovingFunds,
|
|
711
|
+
"Wallet must be in Live or MovingFunds state"
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
// Check if the main UTXO for given wallet exists. If so, validate
|
|
715
|
+
// passed main UTXO data against the stored hash and use them for
|
|
716
|
+
// further processing. If no main UTXO exists, use empty data.
|
|
717
|
+
resolvedMainUtxo = BitcoinTx.UTXO(bytes32(0), 0, 0);
|
|
718
|
+
bytes32 mainUtxoHash = wallet.mainUtxoHash;
|
|
719
|
+
if (mainUtxoHash != bytes32(0)) {
|
|
720
|
+
require(
|
|
721
|
+
keccak256(
|
|
722
|
+
abi.encodePacked(
|
|
723
|
+
mainUtxo.txHash,
|
|
724
|
+
mainUtxo.txOutputIndex,
|
|
725
|
+
mainUtxo.txOutputValue
|
|
726
|
+
)
|
|
727
|
+
) == mainUtxoHash,
|
|
728
|
+
"Invalid main UTXO data"
|
|
729
|
+
);
|
|
730
|
+
resolvedMainUtxo = mainUtxo;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/// @notice Processes the Bitcoin moved funds sweep transaction input vector.
|
|
735
|
+
/// It extracts the first input and tries to match it with one of
|
|
736
|
+
/// the moved funds sweep requests targeting the sweeping wallet.
|
|
737
|
+
/// If the sweep request is found and not yet processed, this
|
|
738
|
+
/// function marks it as processed. If the sweeping wallet has a
|
|
739
|
+
/// main UTXO, this function extracts the second input, makes sure
|
|
740
|
+
/// it refers to the wallet main UTXO, and marks that main UTXO as
|
|
741
|
+
/// correctly spent.
|
|
742
|
+
/// @param sweepTxInputVector Bitcoin moved funds sweep transaction input vector.
|
|
743
|
+
/// This function assumes vector's structure is valid so it must be
|
|
744
|
+
/// validated using e.g. `BTCUtils.validateVin` function before
|
|
745
|
+
/// it is passed here
|
|
746
|
+
/// @param mainUtxo Data of the sweeping wallet's main UTXO. If no main UTXO
|
|
747
|
+
/// exists for the given the wallet, this parameter's fields should
|
|
748
|
+
/// be zeroed to bypass the main UTXO validation
|
|
749
|
+
/// @param walletPubKeyHash 20-byte public key hash of the sweeping wallet
|
|
750
|
+
/// @return inputsTotalValue Total inputs value sum.
|
|
751
|
+
/// @dev Requirements:
|
|
752
|
+
/// - The input vector must consist of one mandatory and one optional
|
|
753
|
+
/// input.
|
|
754
|
+
/// - The mandatory input must be the first input in the vector
|
|
755
|
+
/// - The mandatory input must point to a known moved funds sweep
|
|
756
|
+
/// request that is not processed yet and is targeted to the sweeping
|
|
757
|
+
/// wallet
|
|
758
|
+
/// - The optional output must be the second input in the vector
|
|
759
|
+
/// - The optional input is required if the sweeping wallet has a
|
|
760
|
+
/// main UTXO (i.e. the `mainUtxo` is not zeroed). In that case,
|
|
761
|
+
/// that input must point the the sweeping wallet main UTXO.
|
|
762
|
+
function processMovedFundsSweepTxInputs(
|
|
763
|
+
BridgeState.Storage storage self,
|
|
764
|
+
bytes memory sweepTxInputVector,
|
|
765
|
+
BitcoinTx.UTXO memory mainUtxo,
|
|
766
|
+
bytes20 walletPubKeyHash
|
|
767
|
+
) internal returns (uint256 inputsTotalValue) {
|
|
768
|
+
// To determine the total number of Bitcoin transaction inputs,
|
|
769
|
+
// we need to parse the compactSize uint (VarInt) the input vector is
|
|
770
|
+
// prepended by. That compactSize uint encodes the number of vector
|
|
771
|
+
// elements using the format presented in:
|
|
772
|
+
// https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
|
|
773
|
+
// We don't need asserting the compactSize uint is parseable since it
|
|
774
|
+
// was already checked during `validateVin` validation performed as
|
|
775
|
+
// part of the `BitcoinTx.validateProof` call.
|
|
776
|
+
// See `BitcoinTx.inputVector` docs for more details.
|
|
777
|
+
(
|
|
778
|
+
uint256 inputsCompactSizeUintLength,
|
|
779
|
+
uint256 inputsCount
|
|
780
|
+
) = sweepTxInputVector.parseVarInt();
|
|
781
|
+
|
|
782
|
+
// To determine the first input starting index, we must jump over
|
|
783
|
+
// the compactSize uint which prepends the input vector. One byte
|
|
784
|
+
// must be added because `BtcUtils.parseVarInt` does not include
|
|
785
|
+
// compactSize uint tag in the returned length.
|
|
786
|
+
//
|
|
787
|
+
// For >= 0 && <= 252, `BTCUtils.determineVarIntDataLengthAt`
|
|
788
|
+
// returns `0`, so we jump over one byte of compactSize uint.
|
|
789
|
+
//
|
|
790
|
+
// For >= 253 && <= 0xffff there is `0xfd` tag,
|
|
791
|
+
// `BTCUtils.determineVarIntDataLengthAt` returns `2` (no
|
|
792
|
+
// tag byte included) so we need to jump over 1+2 bytes of
|
|
793
|
+
// compactSize uint.
|
|
794
|
+
//
|
|
795
|
+
// Please refer `BTCUtils` library and compactSize uint
|
|
796
|
+
// docs in `BitcoinTx` library for more details.
|
|
797
|
+
uint256 inputStartingIndex = 1 + inputsCompactSizeUintLength;
|
|
798
|
+
|
|
799
|
+
// We always expect the first input to be the swept UTXO. Additionally,
|
|
800
|
+
// if the sweeping wallet has a main UTXO, that main UTXO should be
|
|
801
|
+
// pointed by the second input.
|
|
802
|
+
require(
|
|
803
|
+
inputsCount == (mainUtxo.txHash != bytes32(0) ? 2 : 1),
|
|
804
|
+
"Moved funds sweep transaction must have a proper inputs count"
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
// Parse the first input and extract its outpoint tx hash and index.
|
|
808
|
+
(
|
|
809
|
+
bytes32 firstInputOutpointTxHash,
|
|
810
|
+
uint32 firstInputOutpointIndex,
|
|
811
|
+
uint256 firstInputLength
|
|
812
|
+
) = parseMovedFundsSweepTxInputAt(
|
|
813
|
+
sweepTxInputVector,
|
|
814
|
+
inputStartingIndex
|
|
815
|
+
);
|
|
816
|
+
|
|
817
|
+
// Build the request key and fetch the corresponding moved funds sweep
|
|
818
|
+
// request from contract storage.
|
|
819
|
+
MovedFundsSweepRequest storage sweepRequest = self
|
|
820
|
+
.movedFundsSweepRequests[
|
|
821
|
+
uint256(
|
|
822
|
+
keccak256(
|
|
823
|
+
abi.encodePacked(
|
|
824
|
+
firstInputOutpointTxHash,
|
|
825
|
+
firstInputOutpointIndex
|
|
826
|
+
)
|
|
827
|
+
)
|
|
828
|
+
)
|
|
829
|
+
];
|
|
830
|
+
|
|
831
|
+
// The sweep request must exist, must be not processed yet, and must
|
|
832
|
+
// belong to the sweeping wallet.
|
|
833
|
+
require(sweepRequest.createdAt != 0, "Sweep request does not exist");
|
|
834
|
+
require(sweepRequest.sweptAt == 0, "Sweep request already processed");
|
|
835
|
+
// We must check if the wallet extracted from the moved funds sweep
|
|
836
|
+
// transaction output is truly the owner of the sweep request connected
|
|
837
|
+
// with the swept UTXO. This is needed to prevent a case when a wallet
|
|
838
|
+
// handles its own sweep request but locks the funds on another
|
|
839
|
+
// wallet public key hash.
|
|
840
|
+
require(
|
|
841
|
+
sweepRequest.walletPubKeyHash == walletPubKeyHash,
|
|
842
|
+
"Sweep request belongs to another wallet"
|
|
843
|
+
);
|
|
844
|
+
// If the validation passed, the sweep request must be marked as
|
|
845
|
+
// processed and its value should be counted into the total inputs
|
|
846
|
+
// value sum.
|
|
847
|
+
/* solhint-disable-next-line not-rely-on-time */
|
|
848
|
+
sweepRequest.sweptAt = uint32(block.timestamp);
|
|
849
|
+
inputsTotalValue += sweepRequest.value;
|
|
850
|
+
|
|
851
|
+
self
|
|
852
|
+
.registeredWallets[walletPubKeyHash]
|
|
853
|
+
.pendingMovedFundsSweepRequestsCount--;
|
|
854
|
+
|
|
855
|
+
// If the main UTXO for the sweeping wallet exists, it must be processed.
|
|
856
|
+
if (mainUtxo.txHash != bytes32(0)) {
|
|
857
|
+
// The second input is supposed to point to that sweeping wallet
|
|
858
|
+
// main UTXO. We need to parse that input.
|
|
859
|
+
(
|
|
860
|
+
bytes32 secondInputOutpointTxHash,
|
|
861
|
+
uint32 secondInputOutpointIndex,
|
|
862
|
+
|
|
863
|
+
) = parseMovedFundsSweepTxInputAt(
|
|
864
|
+
sweepTxInputVector,
|
|
865
|
+
inputStartingIndex + firstInputLength
|
|
866
|
+
);
|
|
867
|
+
// Make sure the second input refers to the sweeping wallet main UTXO.
|
|
868
|
+
require(
|
|
869
|
+
mainUtxo.txHash == secondInputOutpointTxHash &&
|
|
870
|
+
mainUtxo.txOutputIndex == secondInputOutpointIndex,
|
|
871
|
+
"Second input must point to the wallet's main UTXO"
|
|
872
|
+
);
|
|
873
|
+
|
|
874
|
+
// If the validation passed, count the main UTXO value into the
|
|
875
|
+
// total inputs value sum.
|
|
876
|
+
inputsTotalValue += mainUtxo.txOutputValue;
|
|
877
|
+
|
|
878
|
+
// Main UTXO used as an input, mark it as spent. This is needed
|
|
879
|
+
// to defend against fraud challenges referring to this main UTXO.
|
|
880
|
+
self.spentMainUTXOs[
|
|
881
|
+
uint256(
|
|
882
|
+
keccak256(
|
|
883
|
+
abi.encodePacked(
|
|
884
|
+
secondInputOutpointTxHash,
|
|
885
|
+
secondInputOutpointIndex
|
|
886
|
+
)
|
|
887
|
+
)
|
|
888
|
+
)
|
|
889
|
+
] = true;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
return inputsTotalValue;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/// @notice Parses a Bitcoin transaction input starting at the given index.
|
|
896
|
+
/// @param inputVector Bitcoin transaction input vector
|
|
897
|
+
/// @param inputStartingIndex Index the given input starts at
|
|
898
|
+
/// @return outpointTxHash 32-byte hash of the Bitcoin transaction which is
|
|
899
|
+
/// pointed in the given input's outpoint.
|
|
900
|
+
/// @return outpointIndex 4-byte index of the Bitcoin transaction output
|
|
901
|
+
/// which is pointed in the given input's outpoint.
|
|
902
|
+
/// @return inputLength Byte length of the given input.
|
|
903
|
+
/// @dev This function assumes vector's structure is valid so it must be
|
|
904
|
+
/// validated using e.g. `BTCUtils.validateVin` function before it
|
|
905
|
+
/// is passed here.
|
|
906
|
+
function parseMovedFundsSweepTxInputAt(
|
|
907
|
+
bytes memory inputVector,
|
|
908
|
+
uint256 inputStartingIndex
|
|
909
|
+
)
|
|
910
|
+
internal
|
|
911
|
+
pure
|
|
912
|
+
returns (
|
|
913
|
+
bytes32 outpointTxHash,
|
|
914
|
+
uint32 outpointIndex,
|
|
915
|
+
uint256 inputLength
|
|
916
|
+
)
|
|
917
|
+
{
|
|
918
|
+
outpointTxHash = inputVector.extractInputTxIdLeAt(inputStartingIndex);
|
|
919
|
+
|
|
920
|
+
outpointIndex = BTCUtils.reverseUint32(
|
|
921
|
+
uint32(inputVector.extractTxIndexLeAt(inputStartingIndex))
|
|
922
|
+
);
|
|
923
|
+
|
|
924
|
+
inputLength = inputVector.determineInputLengthAt(inputStartingIndex);
|
|
925
|
+
|
|
926
|
+
return (outpointTxHash, outpointIndex, inputLength);
|
|
927
|
+
}
|
|
524
928
|
}
|
|
@@ -78,6 +78,8 @@ library Wallets {
|
|
|
78
78
|
// UNIX timestamp indicating the moment the wallet's closing period
|
|
79
79
|
// started.
|
|
80
80
|
uint32 closingStartedAt;
|
|
81
|
+
// Total count of pending moved funds sweep requests targeting this wallet.
|
|
82
|
+
uint32 pendingMovedFundsSweepRequestsCount;
|
|
81
83
|
// Current state of the wallet.
|
|
82
84
|
WalletState state;
|
|
83
85
|
// Moving funds target wallet commitment submitted by the wallet. It
|
|
@@ -19,7 +19,10 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
|
|
|
19
19
|
deployments.getNetworkName() === "hardhat" ? 1 : 6
|
|
20
20
|
|
|
21
21
|
const Deposit = await deploy("Deposit", { from: deployer, log: true })
|
|
22
|
-
const
|
|
22
|
+
const DepositSweep = await deploy("DepositSweep", {
|
|
23
|
+
from: deployer,
|
|
24
|
+
log: true,
|
|
25
|
+
})
|
|
23
26
|
const Redemption = await deploy("Redemption", { from: deployer, log: true })
|
|
24
27
|
const Wallets = await deploy("Wallets", { from: deployer, log: true })
|
|
25
28
|
const Fraud = await deploy("Fraud", { from: deployer, log: true })
|
|
@@ -41,7 +44,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
|
|
|
41
44
|
],
|
|
42
45
|
libraries: {
|
|
43
46
|
Deposit: Deposit.address,
|
|
44
|
-
|
|
47
|
+
DepositSweep: DepositSweep.address,
|
|
45
48
|
Redemption: Redemption.address,
|
|
46
49
|
Wallets: Wallets.address,
|
|
47
50
|
Fraud: Fraud.address,
|