@keep-network/tbtc-v2 0.1.1-dev.35 → 0.1.1-dev.38
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/TBTC.json +3 -3
- package/artifacts/TBTCToken.json +3 -3
- package/artifacts/VendingMachine.json +10 -10
- package/artifacts/solcInputs/{922339b8aca537314dc3d35162317588.json → fbc4b7a5a9816c01bbe8daca114c1439.json} +9 -3
- 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/Bridge.sol/Bridge.dbg.json +1 -1
- package/build/contracts/bridge/Bridge.sol/Bridge.json +165 -157
- package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +1 -1
- package/build/contracts/bridge/BridgeState.sol/BridgeState.json +2 -2
- 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/EcdsaLib.sol/EcdsaLib.dbg.json +1 -1
- package/build/contracts/bridge/Frauds.sol/Frauds.dbg.json +1 -1
- package/build/contracts/bridge/Frauds.sol/Frauds.json +2 -2
- package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +1 -1
- package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +4 -0
- package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +64 -0
- package/build/contracts/bridge/Redeem.sol/OutboundTx.dbg.json +4 -0
- package/build/contracts/bridge/Redeem.sol/OutboundTx.json +10 -0
- package/build/contracts/bridge/Redeem.sol/Redeem.dbg.json +4 -0
- package/build/contracts/bridge/Redeem.sol/Redeem.json +126 -0
- package/build/contracts/bridge/Sweep.sol/Sweep.dbg.json +1 -1
- package/build/contracts/bridge/Sweep.sol/Sweep.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/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/Bridge.sol +131 -821
- package/contracts/bridge/BridgeState.sol +68 -0
- package/contracts/bridge/MovingFunds.sol +280 -0
- package/contracts/bridge/Redeem.sol +849 -0
- package/package.json +1 -1
|
@@ -26,10 +26,12 @@ import "./IRelay.sol";
|
|
|
26
26
|
import "./BridgeState.sol";
|
|
27
27
|
import "./Deposit.sol";
|
|
28
28
|
import "./Sweep.sol";
|
|
29
|
+
import "./Redeem.sol";
|
|
29
30
|
import "./BitcoinTx.sol";
|
|
30
31
|
import "./EcdsaLib.sol";
|
|
31
32
|
import "./Wallets.sol";
|
|
32
33
|
import "./Frauds.sol";
|
|
34
|
+
import "./MovingFunds.sol";
|
|
33
35
|
|
|
34
36
|
import "../bank/Bank.sol";
|
|
35
37
|
|
|
@@ -53,14 +55,16 @@ import "../bank/Bank.sol";
|
|
|
53
55
|
/// balances in the Bank.
|
|
54
56
|
/// @dev Bridge is an upgradeable component of the Bank.
|
|
55
57
|
///
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
// TODO: All wallets-related operations that are currently done directly
|
|
59
|
+
// by the Bridge can be probably delegated to the Wallets library.
|
|
60
|
+
// Examples of such operations are main UTXO or pending redemptions
|
|
61
|
+
// value updates.
|
|
60
62
|
contract Bridge is Ownable, EcdsaWalletOwner {
|
|
61
63
|
using BridgeState for BridgeState.Storage;
|
|
62
64
|
using Deposit for BridgeState.Storage;
|
|
63
65
|
using Sweep for BridgeState.Storage;
|
|
66
|
+
using Redeem for BridgeState.Storage;
|
|
67
|
+
using MovingFunds for BridgeState.Storage;
|
|
64
68
|
using Frauds for Frauds.Data;
|
|
65
69
|
using Wallets for Wallets.Data;
|
|
66
70
|
|
|
@@ -68,115 +72,8 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
68
72
|
using BTCUtils for uint256;
|
|
69
73
|
using BytesLib for bytes;
|
|
70
74
|
|
|
71
|
-
/// @notice Represents a redemption request.
|
|
72
|
-
struct RedemptionRequest {
|
|
73
|
-
// ETH address of the redeemer who created the request.
|
|
74
|
-
address redeemer;
|
|
75
|
-
// Requested TBTC amount in satoshi.
|
|
76
|
-
uint64 requestedAmount;
|
|
77
|
-
// Treasury TBTC fee in satoshi at the moment of request creation.
|
|
78
|
-
uint64 treasuryFee;
|
|
79
|
-
// Transaction maximum BTC fee in satoshi at the moment of request
|
|
80
|
-
// creation.
|
|
81
|
-
uint64 txMaxFee;
|
|
82
|
-
// UNIX timestamp the request was created at.
|
|
83
|
-
uint32 requestedAt;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/// @notice Represents an outcome of the redemption Bitcoin transaction
|
|
87
|
-
/// outputs processing.
|
|
88
|
-
struct RedemptionTxOutputsInfo {
|
|
89
|
-
// Total TBTC value in satoshi that should be burned by the Bridge.
|
|
90
|
-
// It includes the total amount of all BTC redeemed in the transaction
|
|
91
|
-
// and the fee paid to BTC miners for the redemption transaction.
|
|
92
|
-
uint64 totalBurnableValue;
|
|
93
|
-
// Total TBTC value in satoshi that should be transferred to
|
|
94
|
-
// the treasury. It is a sum of all treasury fees paid by all
|
|
95
|
-
// redeemers included in the redemption transaction.
|
|
96
|
-
uint64 totalTreasuryFee;
|
|
97
|
-
// Index of the change output. The change output becomes
|
|
98
|
-
// the new main wallet's UTXO.
|
|
99
|
-
uint32 changeIndex;
|
|
100
|
-
// Value in satoshi of the change output.
|
|
101
|
-
uint64 changeValue;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
75
|
BridgeState.Storage internal self;
|
|
105
76
|
|
|
106
|
-
/// TODO: Make it governable.
|
|
107
|
-
/// @notice The minimal amount that can be requested for redemption.
|
|
108
|
-
/// Value of this parameter must take into account the value of
|
|
109
|
-
/// `redemptionTreasuryFeeDivisor` and `redemptionTxMaxFee`
|
|
110
|
-
/// parameters in order to make requests that can incur the
|
|
111
|
-
/// treasury and transaction fee and still satisfy the redeemer.
|
|
112
|
-
uint64 public redemptionDustThreshold;
|
|
113
|
-
|
|
114
|
-
/// TODO: Make it governable.
|
|
115
|
-
/// @notice Divisor used to compute the treasury fee taken from each
|
|
116
|
-
/// redemption request and transferred to the treasury upon
|
|
117
|
-
/// successful request finalization. That fee is computed as follows:
|
|
118
|
-
/// `treasuryFee = requestedAmount / redemptionTreasuryFeeDivisor`
|
|
119
|
-
/// For example, if the treasury fee needs to be 2% of each
|
|
120
|
-
/// redemption request, the `redemptionTreasuryFeeDivisor` should
|
|
121
|
-
/// be set to `50` because `1/50 = 0.02 = 2%`.
|
|
122
|
-
uint64 public redemptionTreasuryFeeDivisor;
|
|
123
|
-
|
|
124
|
-
/// TODO: Make it governable.
|
|
125
|
-
/// @notice Maximum amount of BTC transaction fee that can be incurred by
|
|
126
|
-
/// each redemption request being part of the given redemption
|
|
127
|
-
/// transaction. If the maximum BTC transaction fee is exceeded, such
|
|
128
|
-
/// transaction is considered a fraud.
|
|
129
|
-
/// @dev This is a per-redemption output max fee for the redemption transaction.
|
|
130
|
-
uint64 public redemptionTxMaxFee;
|
|
131
|
-
|
|
132
|
-
/// TODO: Make it governable.
|
|
133
|
-
/// @notice Time after which the redemption request can be reported as
|
|
134
|
-
/// timed out. It is counted from the moment when the redemption
|
|
135
|
-
/// request was created via `requestRedemption` call. Reported
|
|
136
|
-
/// timed out requests are cancelled and locked TBTC is returned
|
|
137
|
-
/// to the redeemer in full amount.
|
|
138
|
-
uint256 public redemptionTimeout;
|
|
139
|
-
|
|
140
|
-
/// TODO: Make it governable.
|
|
141
|
-
/// @notice Maximum amount of the total BTC transaction fee that is
|
|
142
|
-
/// acceptable in a single moving funds transaction.
|
|
143
|
-
/// @dev This is a TOTAL max fee for the moving funds transaction. Note that
|
|
144
|
-
/// `depositTxMaxFee` is per single deposit and `redemptionTxMaxFee`
|
|
145
|
-
/// if per single redemption. `movingFundsTxMaxTotalFee` is a total fee
|
|
146
|
-
/// for the entire transaction.
|
|
147
|
-
uint64 public movingFundsTxMaxTotalFee;
|
|
148
|
-
|
|
149
|
-
/// @notice Collection of all pending redemption requests indexed by
|
|
150
|
-
/// redemption key built as
|
|
151
|
-
/// keccak256(walletPubKeyHash | redeemerOutputScript). The
|
|
152
|
-
/// walletPubKeyHash is the 20-byte wallet's public key hash
|
|
153
|
-
/// (computed using Bitcoin HASH160 over the compressed ECDSA
|
|
154
|
-
/// public key) and redeemerOutputScript is a Bitcoin script
|
|
155
|
-
/// (P2PKH, P2WPKH, P2SH or P2WSH) that will be used to lock
|
|
156
|
-
/// redeemed BTC as requested by the redeemer. Requests are added
|
|
157
|
-
/// to this mapping by the `requestRedemption` method (duplicates
|
|
158
|
-
/// not allowed) and are removed by one of the following methods:
|
|
159
|
-
/// - `submitRedemptionProof` in case the request was handled
|
|
160
|
-
/// successfully
|
|
161
|
-
/// - `notifyRedemptionTimeout` in case the request was reported
|
|
162
|
-
/// to be timed out
|
|
163
|
-
mapping(uint256 => RedemptionRequest) public pendingRedemptions;
|
|
164
|
-
|
|
165
|
-
/// @notice Collection of all timed out redemptions requests indexed by
|
|
166
|
-
/// redemption key built as
|
|
167
|
-
/// keccak256(walletPubKeyHash | redeemerOutputScript). The
|
|
168
|
-
/// walletPubKeyHash is the 20-byte wallet's public key hash
|
|
169
|
-
/// (computed using Bitcoin HASH160 over the compressed ECDSA
|
|
170
|
-
/// public key) and redeemerOutputScript is the Bitcoin script
|
|
171
|
-
/// (P2PKH, P2WPKH, P2SH or P2WSH) that is involved in the timed
|
|
172
|
-
/// out request. Timed out requests are stored in this mapping to
|
|
173
|
-
/// avoid slashing the wallets multiple times for the same timeout.
|
|
174
|
-
/// Only one method can add to this mapping:
|
|
175
|
-
/// - `notifyRedemptionTimeout` which puts the redemption key
|
|
176
|
-
/// to this mapping basing on a timed out request stored
|
|
177
|
-
/// previously in `pendingRedemptions` mapping.
|
|
178
|
-
mapping(uint256 => RedemptionRequest) public timedOutRedemptions;
|
|
179
|
-
|
|
180
77
|
/// @notice Contains parameters related to frauds and the collection of all
|
|
181
78
|
/// submitted fraud challenges.
|
|
182
79
|
Frauds.Data internal frauds;
|
|
@@ -306,11 +203,11 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
306
203
|
self.depositDustThreshold = 1000000; // 1000000 satoshi = 0.01 BTC
|
|
307
204
|
self.depositTxMaxFee = 10000; // 10000 satoshi
|
|
308
205
|
self.depositTreasuryFeeDivisor = 2000; // 1/2000 == 5bps == 0.05% == 0.0005
|
|
309
|
-
redemptionDustThreshold = 1000000; // 1000000 satoshi = 0.01 BTC
|
|
310
|
-
redemptionTreasuryFeeDivisor = 2000; // 1/2000 == 5bps == 0.05% == 0.0005
|
|
311
|
-
redemptionTxMaxFee = 10000; // 10000 satoshi
|
|
312
|
-
redemptionTimeout = 172800; // 48 hours
|
|
313
|
-
movingFundsTxMaxTotalFee = 10000; // 10000 satoshi
|
|
206
|
+
self.redemptionDustThreshold = 1000000; // 1000000 satoshi = 0.01 BTC
|
|
207
|
+
self.redemptionTreasuryFeeDivisor = 2000; // 1/2000 == 5bps == 0.05% == 0.0005
|
|
208
|
+
self.redemptionTxMaxFee = 10000; // 10000 satoshi
|
|
209
|
+
self.redemptionTimeout = 172800; // 48 hours
|
|
210
|
+
self.movingFundsTxMaxTotalFee = 10000; // 10000 satoshi
|
|
314
211
|
|
|
315
212
|
// TODO: Revisit initial values.
|
|
316
213
|
frauds.setSlashingAmount(10000 * 1e18); // 10000 T
|
|
@@ -781,123 +678,13 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
781
678
|
bytes calldata redeemerOutputScript,
|
|
782
679
|
uint64 amount
|
|
783
680
|
) external {
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
];
|
|
787
|
-
|
|
788
|
-
require(
|
|
789
|
-
wallet.state == Wallets.WalletState.Live,
|
|
790
|
-
"Wallet must be in Live state"
|
|
791
|
-
);
|
|
792
|
-
|
|
793
|
-
bytes32 mainUtxoHash = wallet.mainUtxoHash;
|
|
794
|
-
require(
|
|
795
|
-
mainUtxoHash != bytes32(0),
|
|
796
|
-
"No main UTXO for the given wallet"
|
|
797
|
-
);
|
|
798
|
-
require(
|
|
799
|
-
keccak256(
|
|
800
|
-
abi.encodePacked(
|
|
801
|
-
mainUtxo.txHash,
|
|
802
|
-
mainUtxo.txOutputIndex,
|
|
803
|
-
mainUtxo.txOutputValue
|
|
804
|
-
)
|
|
805
|
-
) == mainUtxoHash,
|
|
806
|
-
"Invalid main UTXO data"
|
|
807
|
-
);
|
|
808
|
-
|
|
809
|
-
// TODO: Confirm if `walletPubKeyHash` should be validated by checking
|
|
810
|
-
// if it is the oldest one who can handle the request. This will
|
|
811
|
-
// be suggested by the dApp but may not be respected by users who
|
|
812
|
-
// interact directly with the contract. Do we need to enforce it
|
|
813
|
-
// here? One option is not to enforce it, to save on gas, but if
|
|
814
|
-
// we see this rule is not respected, upgrade Bridge contract to
|
|
815
|
-
// require it.
|
|
816
|
-
|
|
817
|
-
// Validate if redeemer output script is a correct standard type
|
|
818
|
-
// (P2PKH, P2WPKH, P2SH or P2WSH). This is done by building a stub
|
|
819
|
-
// output with 0 as value and using `BTCUtils.extractHash` on it. Such
|
|
820
|
-
// a function extracts the payload properly only from standard outputs
|
|
821
|
-
// so if it succeeds, we have a guarantee the redeemer output script
|
|
822
|
-
// is proper. Worth to note `extractHash` ignores the value at all
|
|
823
|
-
// so this is why we can use 0 safely. This way of validation is the
|
|
824
|
-
// same as in tBTC v1.
|
|
825
|
-
bytes memory redeemerOutputScriptPayload = abi
|
|
826
|
-
.encodePacked(bytes8(0), redeemerOutputScript)
|
|
827
|
-
.extractHash();
|
|
828
|
-
require(
|
|
829
|
-
redeemerOutputScriptPayload.length > 0,
|
|
830
|
-
"Redeemer output script must be a standard type"
|
|
831
|
-
);
|
|
832
|
-
// Check if the redeemer output script payload does not point to the
|
|
833
|
-
// wallet public key hash.
|
|
834
|
-
require(
|
|
835
|
-
keccak256(abi.encodePacked(walletPubKeyHash)) !=
|
|
836
|
-
keccak256(redeemerOutputScriptPayload),
|
|
837
|
-
"Redeemer output script must not point to the wallet PKH"
|
|
838
|
-
);
|
|
839
|
-
|
|
840
|
-
require(
|
|
841
|
-
amount >= redemptionDustThreshold,
|
|
842
|
-
"Redemption amount too small"
|
|
843
|
-
);
|
|
844
|
-
|
|
845
|
-
// The redemption key is built on top of the wallet public key hash
|
|
846
|
-
// and redeemer output script pair. That means there can be only one
|
|
847
|
-
// request asking for redemption from the given wallet to the given
|
|
848
|
-
// BTC script at the same time.
|
|
849
|
-
uint256 redemptionKey = uint256(
|
|
850
|
-
keccak256(abi.encodePacked(walletPubKeyHash, redeemerOutputScript))
|
|
851
|
-
);
|
|
852
|
-
|
|
853
|
-
// Check if given redemption key is not used by a pending redemption.
|
|
854
|
-
// There is no need to check for existence in `timedOutRedemptions`
|
|
855
|
-
// since the wallet's state is changed to other than Live after
|
|
856
|
-
// first time out is reported so making new requests is not possible.
|
|
857
|
-
// slither-disable-next-line incorrect-equality
|
|
858
|
-
require(
|
|
859
|
-
pendingRedemptions[redemptionKey].requestedAt == 0,
|
|
860
|
-
"There is a pending redemption request from this wallet to the same address"
|
|
861
|
-
);
|
|
862
|
-
|
|
863
|
-
// No need to check whether `amount - treasuryFee - txMaxFee > 0`
|
|
864
|
-
// since the `redemptionDustThreshold` should force that condition
|
|
865
|
-
// to be always true.
|
|
866
|
-
uint64 treasuryFee = redemptionTreasuryFeeDivisor > 0
|
|
867
|
-
? amount / redemptionTreasuryFeeDivisor
|
|
868
|
-
: 0;
|
|
869
|
-
uint64 txMaxFee = redemptionTxMaxFee;
|
|
870
|
-
|
|
871
|
-
// The main wallet UTXO's value doesn't include all pending redemptions.
|
|
872
|
-
// To determine if the requested redemption can be performed by the
|
|
873
|
-
// wallet we need to subtract the total value of all pending redemptions
|
|
874
|
-
// from that wallet's main UTXO value. Given that the treasury fee is
|
|
875
|
-
// not redeemed from the wallet, we are subtracting it.
|
|
876
|
-
wallet.pendingRedemptionsValue += amount - treasuryFee;
|
|
877
|
-
require(
|
|
878
|
-
mainUtxo.txOutputValue >= wallet.pendingRedemptionsValue,
|
|
879
|
-
"Insufficient wallet funds"
|
|
880
|
-
);
|
|
881
|
-
|
|
882
|
-
pendingRedemptions[redemptionKey] = RedemptionRequest(
|
|
883
|
-
msg.sender,
|
|
884
|
-
amount,
|
|
885
|
-
treasuryFee,
|
|
886
|
-
txMaxFee,
|
|
887
|
-
/* solhint-disable-next-line not-rely-on-time */
|
|
888
|
-
uint32(block.timestamp)
|
|
889
|
-
);
|
|
890
|
-
|
|
891
|
-
emit RedemptionRequested(
|
|
681
|
+
self.requestRedemption(
|
|
682
|
+
wallets,
|
|
892
683
|
walletPubKeyHash,
|
|
684
|
+
mainUtxo,
|
|
893
685
|
redeemerOutputScript,
|
|
894
|
-
|
|
895
|
-
amount,
|
|
896
|
-
treasuryFee,
|
|
897
|
-
txMaxFee
|
|
686
|
+
amount
|
|
898
687
|
);
|
|
899
|
-
|
|
900
|
-
self.bank.transferBalanceFrom(msg.sender, address(this), amount);
|
|
901
688
|
}
|
|
902
689
|
|
|
903
690
|
/// @notice Used by the wallet to prove the BTC redemption transaction
|
|
@@ -950,370 +737,13 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
950
737
|
BitcoinTx.UTXO calldata mainUtxo,
|
|
951
738
|
bytes20 walletPubKeyHash
|
|
952
739
|
) external {
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
// https://github.com/keep-network/tbtc-v2/pull/106#discussion_r801745204
|
|
956
|
-
|
|
957
|
-
// The actual transaction proof is performed here. After that point, we
|
|
958
|
-
// can assume the transaction happened on Bitcoin chain and has
|
|
959
|
-
// a sufficient number of confirmations as determined by
|
|
960
|
-
// `txProofDifficultyFactor` constant.
|
|
961
|
-
bytes32 redemptionTxHash = BitcoinTx.validateProof(
|
|
740
|
+
self.submitRedemptionProof(
|
|
741
|
+
wallets,
|
|
962
742
|
redemptionTx,
|
|
963
743
|
redemptionProof,
|
|
964
|
-
self.proofDifficultyContext()
|
|
965
|
-
);
|
|
966
|
-
|
|
967
|
-
// Process the redemption transaction input. Specifically, check if it
|
|
968
|
-
// refers to the expected wallet's main UTXO.
|
|
969
|
-
processWalletOutboundTxInput(
|
|
970
|
-
redemptionTx.inputVector,
|
|
971
744
|
mainUtxo,
|
|
972
745
|
walletPubKeyHash
|
|
973
746
|
);
|
|
974
|
-
|
|
975
|
-
Wallets.Wallet storage wallet = wallets.registeredWallets[
|
|
976
|
-
walletPubKeyHash
|
|
977
|
-
];
|
|
978
|
-
|
|
979
|
-
Wallets.WalletState walletState = wallet.state;
|
|
980
|
-
require(
|
|
981
|
-
walletState == Wallets.WalletState.Live ||
|
|
982
|
-
walletState == Wallets.WalletState.MovingFunds,
|
|
983
|
-
"Wallet must be in Live or MovingFuds state"
|
|
984
|
-
);
|
|
985
|
-
|
|
986
|
-
// Process redemption transaction outputs to extract some info required
|
|
987
|
-
// for further processing.
|
|
988
|
-
RedemptionTxOutputsInfo memory outputsInfo = processRedemptionTxOutputs(
|
|
989
|
-
redemptionTx.outputVector,
|
|
990
|
-
walletPubKeyHash
|
|
991
|
-
);
|
|
992
|
-
|
|
993
|
-
if (outputsInfo.changeValue > 0) {
|
|
994
|
-
// If the change value is grater than zero, it means the change
|
|
995
|
-
// output exists and can be used as new wallet's main UTXO.
|
|
996
|
-
wallet.mainUtxoHash = keccak256(
|
|
997
|
-
abi.encodePacked(
|
|
998
|
-
redemptionTxHash,
|
|
999
|
-
outputsInfo.changeIndex,
|
|
1000
|
-
outputsInfo.changeValue
|
|
1001
|
-
)
|
|
1002
|
-
);
|
|
1003
|
-
} else {
|
|
1004
|
-
// If the change value is zero, it means the change output doesn't
|
|
1005
|
-
// exists and no funds left on the wallet. Delete the main UTXO
|
|
1006
|
-
// for that wallet to represent that state in a proper way.
|
|
1007
|
-
delete wallet.mainUtxoHash;
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
wallet.pendingRedemptionsValue -= outputsInfo.totalBurnableValue;
|
|
1011
|
-
|
|
1012
|
-
emit RedemptionsCompleted(walletPubKeyHash, redemptionTxHash);
|
|
1013
|
-
|
|
1014
|
-
self.bank.decreaseBalance(outputsInfo.totalBurnableValue);
|
|
1015
|
-
self.bank.transferBalance(self.treasury, outputsInfo.totalTreasuryFee);
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
/// @notice Checks whether an outbound Bitcoin transaction performed from
|
|
1019
|
-
/// the given wallet has an input vector that contains a single
|
|
1020
|
-
/// input referring to the wallet's main UTXO. Marks that main UTXO
|
|
1021
|
-
/// as correctly spent if the validation succeeds. Reverts otherwise.
|
|
1022
|
-
/// There are two outbound transactions from a wallet possible: a
|
|
1023
|
-
/// redemption transaction or a moving funds to another wallet
|
|
1024
|
-
/// transaction.
|
|
1025
|
-
/// @param walletOutboundTxInputVector Bitcoin outbound transaction's input
|
|
1026
|
-
/// vector. This function assumes vector's structure is valid so it
|
|
1027
|
-
/// must be validated using e.g. `BTCUtils.validateVin` function
|
|
1028
|
-
/// before it is passed here
|
|
1029
|
-
/// @param mainUtxo Data of the wallet's main UTXO, as currently known on
|
|
1030
|
-
/// the Ethereum chain.
|
|
1031
|
-
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
1032
|
-
// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
1033
|
-
/// performed the outbound transaction.
|
|
1034
|
-
function processWalletOutboundTxInput(
|
|
1035
|
-
bytes memory walletOutboundTxInputVector,
|
|
1036
|
-
BitcoinTx.UTXO calldata mainUtxo,
|
|
1037
|
-
bytes20 walletPubKeyHash
|
|
1038
|
-
) internal {
|
|
1039
|
-
// Assert that main UTXO for passed wallet exists in storage.
|
|
1040
|
-
bytes32 mainUtxoHash = wallets
|
|
1041
|
-
.registeredWallets[walletPubKeyHash]
|
|
1042
|
-
.mainUtxoHash;
|
|
1043
|
-
require(mainUtxoHash != bytes32(0), "No main UTXO for given wallet");
|
|
1044
|
-
|
|
1045
|
-
// Assert that passed main UTXO parameter is the same as in storage and
|
|
1046
|
-
// can be used for further processing.
|
|
1047
|
-
require(
|
|
1048
|
-
keccak256(
|
|
1049
|
-
abi.encodePacked(
|
|
1050
|
-
mainUtxo.txHash,
|
|
1051
|
-
mainUtxo.txOutputIndex,
|
|
1052
|
-
mainUtxo.txOutputValue
|
|
1053
|
-
)
|
|
1054
|
-
) == mainUtxoHash,
|
|
1055
|
-
"Invalid main UTXO data"
|
|
1056
|
-
);
|
|
1057
|
-
|
|
1058
|
-
// Assert that the single outbound transaction input actually
|
|
1059
|
-
// refers to the wallet's main UTXO.
|
|
1060
|
-
(
|
|
1061
|
-
bytes32 outpointTxHash,
|
|
1062
|
-
uint32 outpointIndex
|
|
1063
|
-
) = parseWalletOutboundTxInput(walletOutboundTxInputVector);
|
|
1064
|
-
require(
|
|
1065
|
-
mainUtxo.txHash == outpointTxHash &&
|
|
1066
|
-
mainUtxo.txOutputIndex == outpointIndex,
|
|
1067
|
-
"Outbound transaction input must point to the wallet's main UTXO"
|
|
1068
|
-
);
|
|
1069
|
-
|
|
1070
|
-
// Main UTXO used as an input, mark it as spent.
|
|
1071
|
-
self.spentMainUTXOs[
|
|
1072
|
-
uint256(
|
|
1073
|
-
keccak256(
|
|
1074
|
-
abi.encodePacked(mainUtxo.txHash, mainUtxo.txOutputIndex)
|
|
1075
|
-
)
|
|
1076
|
-
)
|
|
1077
|
-
] = true;
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
/// @notice Parses the input vector of an outbound Bitcoin transaction
|
|
1081
|
-
/// performed from the given wallet. It extracts the single input
|
|
1082
|
-
/// then the transaction hash and output index from its outpoint.
|
|
1083
|
-
/// There are two outbound transactions from a wallet possible: a
|
|
1084
|
-
/// redemption transaction or a moving funds to another wallet
|
|
1085
|
-
/// transaction.
|
|
1086
|
-
/// @param walletOutboundTxInputVector Bitcoin outbound transaction input
|
|
1087
|
-
/// vector. This function assumes vector's structure is valid so it
|
|
1088
|
-
/// must be validated using e.g. `BTCUtils.validateVin` function
|
|
1089
|
-
/// before it is passed here
|
|
1090
|
-
/// @return outpointTxHash 32-byte hash of the Bitcoin transaction which is
|
|
1091
|
-
/// pointed in the input's outpoint.
|
|
1092
|
-
/// @return outpointIndex 4-byte index of the Bitcoin transaction output
|
|
1093
|
-
/// which is pointed in the input's outpoint.
|
|
1094
|
-
function parseWalletOutboundTxInput(
|
|
1095
|
-
bytes memory walletOutboundTxInputVector
|
|
1096
|
-
) internal pure returns (bytes32 outpointTxHash, uint32 outpointIndex) {
|
|
1097
|
-
// To determine the total number of Bitcoin transaction inputs,
|
|
1098
|
-
// we need to parse the compactSize uint (VarInt) the input vector is
|
|
1099
|
-
// prepended by. That compactSize uint encodes the number of vector
|
|
1100
|
-
// elements using the format presented in:
|
|
1101
|
-
// https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
|
|
1102
|
-
// We don't need asserting the compactSize uint is parseable since it
|
|
1103
|
-
// was already checked during `validateVin` validation.
|
|
1104
|
-
// See `BitcoinTx.inputVector` docs for more details.
|
|
1105
|
-
(, uint256 inputsCount) = walletOutboundTxInputVector.parseVarInt();
|
|
1106
|
-
require(
|
|
1107
|
-
inputsCount == 1,
|
|
1108
|
-
"Outbound transaction must have a single input"
|
|
1109
|
-
);
|
|
1110
|
-
|
|
1111
|
-
bytes memory input = walletOutboundTxInputVector.extractInputAtIndex(0);
|
|
1112
|
-
|
|
1113
|
-
outpointTxHash = input.extractInputTxIdLE();
|
|
1114
|
-
|
|
1115
|
-
outpointIndex = BTCUtils.reverseUint32(
|
|
1116
|
-
uint32(input.extractTxIndexLE())
|
|
1117
|
-
);
|
|
1118
|
-
|
|
1119
|
-
// There is only one input in the transaction. Input has an outpoint
|
|
1120
|
-
// field that is a reference to the transaction being spent (see
|
|
1121
|
-
// `BitcoinTx` docs). The outpoint contains the hash of the transaction
|
|
1122
|
-
// to spend (`outpointTxHash`) and the index of the specific output
|
|
1123
|
-
// from that transaction (`outpointIndex`).
|
|
1124
|
-
return (outpointTxHash, outpointIndex);
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
/// @notice Processes the Bitcoin redemption transaction output vector.
|
|
1128
|
-
/// It extracts each output and tries to identify it as a pending
|
|
1129
|
-
/// redemption request, reported timed out request, or change.
|
|
1130
|
-
/// Reverts if one of the outputs cannot be recognized properly.
|
|
1131
|
-
/// This function also marks each request as processed by removing
|
|
1132
|
-
/// them from `pendingRedemptions` mapping.
|
|
1133
|
-
/// @param redemptionTxOutputVector Bitcoin redemption transaction output
|
|
1134
|
-
/// vector. This function assumes vector's structure is valid so it
|
|
1135
|
-
/// must be validated using e.g. `BTCUtils.validateVout` function
|
|
1136
|
-
/// before it is passed here
|
|
1137
|
-
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
1138
|
-
// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
1139
|
-
/// performed the redemption transaction.
|
|
1140
|
-
/// @return info Outcomes of the processing.
|
|
1141
|
-
function processRedemptionTxOutputs(
|
|
1142
|
-
bytes memory redemptionTxOutputVector,
|
|
1143
|
-
bytes20 walletPubKeyHash
|
|
1144
|
-
) internal returns (RedemptionTxOutputsInfo memory info) {
|
|
1145
|
-
// Determining the total number of redemption transaction outputs in
|
|
1146
|
-
// the same way as for number of inputs. See `BitcoinTx.outputVector`
|
|
1147
|
-
// docs for more details.
|
|
1148
|
-
(
|
|
1149
|
-
uint256 outputsCompactSizeUintLength,
|
|
1150
|
-
uint256 outputsCount
|
|
1151
|
-
) = redemptionTxOutputVector.parseVarInt();
|
|
1152
|
-
|
|
1153
|
-
// To determine the first output starting index, we must jump over
|
|
1154
|
-
// the compactSize uint which prepends the output vector. One byte
|
|
1155
|
-
// must be added because `BtcUtils.parseVarInt` does not include
|
|
1156
|
-
// compactSize uint tag in the returned length.
|
|
1157
|
-
//
|
|
1158
|
-
// For >= 0 && <= 252, `BTCUtils.determineVarIntDataLengthAt`
|
|
1159
|
-
// returns `0`, so we jump over one byte of compactSize uint.
|
|
1160
|
-
//
|
|
1161
|
-
// For >= 253 && <= 0xffff there is `0xfd` tag,
|
|
1162
|
-
// `BTCUtils.determineVarIntDataLengthAt` returns `2` (no
|
|
1163
|
-
// tag byte included) so we need to jump over 1+2 bytes of
|
|
1164
|
-
// compactSize uint.
|
|
1165
|
-
//
|
|
1166
|
-
// Please refer `BTCUtils` library and compactSize uint
|
|
1167
|
-
// docs in `BitcoinTx` library for more details.
|
|
1168
|
-
uint256 outputStartingIndex = 1 + outputsCompactSizeUintLength;
|
|
1169
|
-
|
|
1170
|
-
// Calculate the keccak256 for two possible wallet's P2PKH or P2WPKH
|
|
1171
|
-
// scripts that can be used to lock the change. This is done upfront to
|
|
1172
|
-
// save on gas. Both scripts have a strict format defined by Bitcoin.
|
|
1173
|
-
//
|
|
1174
|
-
// The P2PKH script has the byte format: <0x1976a914> <20-byte PKH> <0x88ac>.
|
|
1175
|
-
// According to https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
1176
|
-
// - 0x19: Byte length of the entire script
|
|
1177
|
-
// - 0x76: OP_DUP
|
|
1178
|
-
// - 0xa9: OP_HASH160
|
|
1179
|
-
// - 0x14: Byte length of the public key hash
|
|
1180
|
-
// - 0x88: OP_EQUALVERIFY
|
|
1181
|
-
// - 0xac: OP_CHECKSIG
|
|
1182
|
-
// which matches the P2PKH structure as per:
|
|
1183
|
-
// https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash
|
|
1184
|
-
bytes32 walletP2PKHScriptKeccak = keccak256(
|
|
1185
|
-
abi.encodePacked(hex"1976a914", walletPubKeyHash, hex"88ac")
|
|
1186
|
-
);
|
|
1187
|
-
// The P2WPKH script has the byte format: <0x160014> <20-byte PKH>.
|
|
1188
|
-
// According to https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
1189
|
-
// - 0x16: Byte length of the entire script
|
|
1190
|
-
// - 0x00: OP_0
|
|
1191
|
-
// - 0x14: Byte length of the public key hash
|
|
1192
|
-
// which matches the P2WPKH structure as per:
|
|
1193
|
-
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH
|
|
1194
|
-
bytes32 walletP2WPKHScriptKeccak = keccak256(
|
|
1195
|
-
abi.encodePacked(hex"160014", walletPubKeyHash)
|
|
1196
|
-
);
|
|
1197
|
-
|
|
1198
|
-
// Helper variable that counts the number of processed redemption
|
|
1199
|
-
// outputs. Redemptions can be either pending or reported as timed out.
|
|
1200
|
-
// TODO: Revisit the approach with redemptions count according to
|
|
1201
|
-
// https://github.com/keep-network/tbtc-v2/pull/128#discussion_r808237765
|
|
1202
|
-
uint256 processedRedemptionsCount = 0;
|
|
1203
|
-
|
|
1204
|
-
// Outputs processing loop.
|
|
1205
|
-
for (uint256 i = 0; i < outputsCount; i++) {
|
|
1206
|
-
// TODO: Check if we can optimize gas costs by adding
|
|
1207
|
-
// `extractValueAt` and `extractHashAt` in `bitcoin-spv-sol`
|
|
1208
|
-
// in order to avoid allocating bytes in memory.
|
|
1209
|
-
uint256 outputLength = redemptionTxOutputVector
|
|
1210
|
-
.determineOutputLengthAt(outputStartingIndex);
|
|
1211
|
-
bytes memory output = redemptionTxOutputVector.slice(
|
|
1212
|
-
outputStartingIndex,
|
|
1213
|
-
outputLength
|
|
1214
|
-
);
|
|
1215
|
-
|
|
1216
|
-
// Extract the value from given output.
|
|
1217
|
-
uint64 outputValue = output.extractValue();
|
|
1218
|
-
// The output consists of an 8-byte value and a variable length
|
|
1219
|
-
// script. To extract that script we slice the output starting from
|
|
1220
|
-
// 9th byte until the end.
|
|
1221
|
-
bytes memory outputScript = output.slice(8, output.length - 8);
|
|
1222
|
-
|
|
1223
|
-
if (
|
|
1224
|
-
info.changeValue == 0 &&
|
|
1225
|
-
(keccak256(outputScript) == walletP2PKHScriptKeccak ||
|
|
1226
|
-
keccak256(outputScript) == walletP2WPKHScriptKeccak) &&
|
|
1227
|
-
outputValue > 0
|
|
1228
|
-
) {
|
|
1229
|
-
// If we entered here, that means the change output with a
|
|
1230
|
-
// proper non-zero value was found.
|
|
1231
|
-
info.changeIndex = uint32(i);
|
|
1232
|
-
info.changeValue = outputValue;
|
|
1233
|
-
} else {
|
|
1234
|
-
// If we entered here, that the means the given output is
|
|
1235
|
-
// supposed to represent a redemption. Build the redemption key
|
|
1236
|
-
// to perform that check.
|
|
1237
|
-
uint256 redemptionKey = uint256(
|
|
1238
|
-
keccak256(abi.encodePacked(walletPubKeyHash, outputScript))
|
|
1239
|
-
);
|
|
1240
|
-
|
|
1241
|
-
if (pendingRedemptions[redemptionKey].requestedAt != 0) {
|
|
1242
|
-
// If we entered here, that means the output was identified
|
|
1243
|
-
// as a pending redemption request.
|
|
1244
|
-
RedemptionRequest storage request = pendingRedemptions[
|
|
1245
|
-
redemptionKey
|
|
1246
|
-
];
|
|
1247
|
-
// Compute the request's redeemable amount as the requested
|
|
1248
|
-
// amount reduced by the treasury fee. The request's
|
|
1249
|
-
// minimal amount is then the redeemable amount reduced by
|
|
1250
|
-
// the maximum transaction fee.
|
|
1251
|
-
uint64 redeemableAmount = request.requestedAmount -
|
|
1252
|
-
request.treasuryFee;
|
|
1253
|
-
// Output value must fit between the request's redeemable
|
|
1254
|
-
// and minimal amounts to be deemed valid.
|
|
1255
|
-
require(
|
|
1256
|
-
redeemableAmount - request.txMaxFee <= outputValue &&
|
|
1257
|
-
outputValue <= redeemableAmount,
|
|
1258
|
-
"Output value is not within the acceptable range of the pending request"
|
|
1259
|
-
);
|
|
1260
|
-
// Add the redeemable amount to the total burnable value
|
|
1261
|
-
// the Bridge will use to decrease its balance in the Bank.
|
|
1262
|
-
info.totalBurnableValue += redeemableAmount;
|
|
1263
|
-
// Add the request's treasury fee to the total treasury fee
|
|
1264
|
-
// value the Bridge will transfer to the treasury.
|
|
1265
|
-
info.totalTreasuryFee += request.treasuryFee;
|
|
1266
|
-
// Request was properly handled so remove its redemption
|
|
1267
|
-
// key from the mapping to make it reusable for further
|
|
1268
|
-
// requests.
|
|
1269
|
-
delete pendingRedemptions[redemptionKey];
|
|
1270
|
-
|
|
1271
|
-
processedRedemptionsCount++;
|
|
1272
|
-
} else {
|
|
1273
|
-
// If we entered here, the output is not a redemption
|
|
1274
|
-
// request but there is still a chance the given output is
|
|
1275
|
-
// related to a reported timed out redemption request.
|
|
1276
|
-
// If so, check if the output value matches the request
|
|
1277
|
-
// amount to confirm this is an overdue request fulfillment
|
|
1278
|
-
// then bypass this output and process the subsequent
|
|
1279
|
-
// ones. That also means the wallet was already punished
|
|
1280
|
-
// for the inactivity. Otherwise, just revert.
|
|
1281
|
-
RedemptionRequest storage request = timedOutRedemptions[
|
|
1282
|
-
redemptionKey
|
|
1283
|
-
];
|
|
1284
|
-
|
|
1285
|
-
require(
|
|
1286
|
-
request.requestedAt != 0,
|
|
1287
|
-
"Output is a non-requested redemption"
|
|
1288
|
-
);
|
|
1289
|
-
|
|
1290
|
-
uint64 redeemableAmount = request.requestedAmount -
|
|
1291
|
-
request.treasuryFee;
|
|
1292
|
-
|
|
1293
|
-
require(
|
|
1294
|
-
redeemableAmount - request.txMaxFee <= outputValue &&
|
|
1295
|
-
outputValue <= redeemableAmount,
|
|
1296
|
-
"Output value is not within the acceptable range of the timed out request"
|
|
1297
|
-
);
|
|
1298
|
-
|
|
1299
|
-
processedRedemptionsCount++;
|
|
1300
|
-
}
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
|
-
// Make the `outputStartingIndex` pointing to the next output by
|
|
1304
|
-
// increasing it by current output's length.
|
|
1305
|
-
outputStartingIndex += outputLength;
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
|
-
// Protect against the cases when there is only a single change output
|
|
1309
|
-
// referring back to the wallet PKH and just burning main UTXO value
|
|
1310
|
-
// for transaction fees.
|
|
1311
|
-
require(
|
|
1312
|
-
processedRedemptionsCount > 0,
|
|
1313
|
-
"Redemption transaction must process at least one redemption"
|
|
1314
|
-
);
|
|
1315
|
-
|
|
1316
|
-
return info;
|
|
1317
747
|
}
|
|
1318
748
|
|
|
1319
749
|
/// @notice Notifies that there is a pending redemption request associated
|
|
@@ -1344,56 +774,11 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
1344
774
|
bytes20 walletPubKeyHash,
|
|
1345
775
|
bytes calldata redeemerOutputScript
|
|
1346
776
|
) external {
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
require(request.requestedAt > 0, "Redemption request does not exist");
|
|
1353
|
-
require(
|
|
1354
|
-
/* solhint-disable-next-line not-rely-on-time */
|
|
1355
|
-
request.requestedAt + redemptionTimeout < block.timestamp,
|
|
1356
|
-
"Redemption request has not timed out"
|
|
1357
|
-
);
|
|
1358
|
-
|
|
1359
|
-
// Update the wallet's pending redemptions value
|
|
1360
|
-
Wallets.Wallet storage wallet = wallets.registeredWallets[
|
|
1361
|
-
walletPubKeyHash
|
|
1362
|
-
];
|
|
1363
|
-
wallet.pendingRedemptionsValue -=
|
|
1364
|
-
request.requestedAmount -
|
|
1365
|
-
request.treasuryFee;
|
|
1366
|
-
|
|
1367
|
-
require(
|
|
1368
|
-
// TODO: Allow the wallets in `Closing` state when the state is added
|
|
1369
|
-
wallet.state == Wallets.WalletState.Live ||
|
|
1370
|
-
wallet.state == Wallets.WalletState.MovingFunds ||
|
|
1371
|
-
wallet.state == Wallets.WalletState.Terminated,
|
|
1372
|
-
"The wallet must be in Live, MovingFunds or Terminated state"
|
|
777
|
+
self.notifyRedemptionTimeout(
|
|
778
|
+
wallets,
|
|
779
|
+
walletPubKeyHash,
|
|
780
|
+
redeemerOutputScript
|
|
1373
781
|
);
|
|
1374
|
-
|
|
1375
|
-
// It is worth noting that there is no need to check if
|
|
1376
|
-
// `timedOutRedemption` mapping already contains the given redemption
|
|
1377
|
-
// key. There is no possibility to re-use a key of a reported timed-out
|
|
1378
|
-
// redemption because the wallet responsible for causing the timeout is
|
|
1379
|
-
// moved to a state that prevents it to receive new redemption requests.
|
|
1380
|
-
|
|
1381
|
-
// Move the redemption from pending redemptions to timed-out redemptions
|
|
1382
|
-
timedOutRedemptions[redemptionKey] = request;
|
|
1383
|
-
delete pendingRedemptions[redemptionKey];
|
|
1384
|
-
|
|
1385
|
-
if (
|
|
1386
|
-
wallet.state == Wallets.WalletState.Live ||
|
|
1387
|
-
wallet.state == Wallets.WalletState.MovingFunds
|
|
1388
|
-
) {
|
|
1389
|
-
// Propagate timeout consequences to the wallet
|
|
1390
|
-
wallets.notifyRedemptionTimedOut(walletPubKeyHash);
|
|
1391
|
-
}
|
|
1392
|
-
|
|
1393
|
-
emit RedemptionTimedOut(walletPubKeyHash, redeemerOutputScript);
|
|
1394
|
-
|
|
1395
|
-
// Return the requested amount of tokens to the redeemer
|
|
1396
|
-
self.bank.transferBalance(request.redeemer, request.requestedAmount);
|
|
1397
782
|
}
|
|
1398
783
|
|
|
1399
784
|
/// @notice Used by the wallet to prove the BTC moving funds transaction
|
|
@@ -1447,195 +832,16 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
1447
832
|
BitcoinTx.UTXO calldata mainUtxo,
|
|
1448
833
|
bytes20 walletPubKeyHash
|
|
1449
834
|
) external {
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
// a sufficient number of confirmations as determined by
|
|
1453
|
-
// `txProofDifficultyFactor` constant.
|
|
1454
|
-
bytes32 movingFundsTxHash = BitcoinTx.validateProof(
|
|
835
|
+
self.submitMovingFundsProof(
|
|
836
|
+
wallets,
|
|
1455
837
|
movingFundsTx,
|
|
1456
838
|
movingFundsProof,
|
|
1457
|
-
self.proofDifficultyContext()
|
|
1458
|
-
);
|
|
1459
|
-
|
|
1460
|
-
// Process the moving funds transaction input. Specifically, check if
|
|
1461
|
-
// it refers to the expected wallet's main UTXO.
|
|
1462
|
-
processWalletOutboundTxInput(
|
|
1463
|
-
movingFundsTx.inputVector,
|
|
1464
839
|
mainUtxo,
|
|
1465
840
|
walletPubKeyHash
|
|
1466
841
|
);
|
|
1467
|
-
|
|
1468
|
-
(
|
|
1469
|
-
bytes32 targetWalletsHash,
|
|
1470
|
-
uint256 outputsTotalValue
|
|
1471
|
-
) = processMovingFundsTxOutputs(movingFundsTx.outputVector);
|
|
1472
|
-
|
|
1473
|
-
require(
|
|
1474
|
-
mainUtxo.txOutputValue - outputsTotalValue <=
|
|
1475
|
-
movingFundsTxMaxTotalFee,
|
|
1476
|
-
"Transaction fee is too high"
|
|
1477
|
-
);
|
|
1478
|
-
|
|
1479
|
-
wallets.notifyFundsMoved(walletPubKeyHash, targetWalletsHash);
|
|
1480
|
-
|
|
1481
|
-
emit MovingFundsCompleted(walletPubKeyHash, movingFundsTxHash);
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
/// @notice Processes the moving funds Bitcoin transaction output vector
|
|
1485
|
-
/// and extracts information required for further processing.
|
|
1486
|
-
/// @param movingFundsTxOutputVector Bitcoin moving funds transaction output
|
|
1487
|
-
/// vector. This function assumes vector's structure is valid so it
|
|
1488
|
-
/// must be validated using e.g. `BTCUtils.validateVout` function
|
|
1489
|
-
/// before it is passed here
|
|
1490
|
-
/// @return targetWalletsHash keccak256 hash over the list of actual
|
|
1491
|
-
/// target wallets used in the transaction.
|
|
1492
|
-
/// @return outputsTotalValue Sum of all outputs values.
|
|
1493
|
-
/// @dev Requirements:
|
|
1494
|
-
/// - The `movingFundsTxOutputVector` must be parseable, i.e. must
|
|
1495
|
-
/// be validated by the caller as stated in their parameter doc.
|
|
1496
|
-
/// - Each output must refer to a 20-byte public key hash.
|
|
1497
|
-
/// - The total outputs value must be evenly divided over all outputs.
|
|
1498
|
-
function processMovingFundsTxOutputs(bytes memory movingFundsTxOutputVector)
|
|
1499
|
-
internal
|
|
1500
|
-
view
|
|
1501
|
-
returns (bytes32 targetWalletsHash, uint256 outputsTotalValue)
|
|
1502
|
-
{
|
|
1503
|
-
// Determining the total number of Bitcoin transaction outputs in
|
|
1504
|
-
// the same way as for number of inputs. See `BitcoinTx.outputVector`
|
|
1505
|
-
// docs for more details.
|
|
1506
|
-
(
|
|
1507
|
-
uint256 outputsCompactSizeUintLength,
|
|
1508
|
-
uint256 outputsCount
|
|
1509
|
-
) = movingFundsTxOutputVector.parseVarInt();
|
|
1510
|
-
|
|
1511
|
-
// To determine the first output starting index, we must jump over
|
|
1512
|
-
// the compactSize uint which prepends the output vector. One byte
|
|
1513
|
-
// must be added because `BtcUtils.parseVarInt` does not include
|
|
1514
|
-
// compactSize uint tag in the returned length.
|
|
1515
|
-
//
|
|
1516
|
-
// For >= 0 && <= 252, `BTCUtils.determineVarIntDataLengthAt`
|
|
1517
|
-
// returns `0`, so we jump over one byte of compactSize uint.
|
|
1518
|
-
//
|
|
1519
|
-
// For >= 253 && <= 0xffff there is `0xfd` tag,
|
|
1520
|
-
// `BTCUtils.determineVarIntDataLengthAt` returns `2` (no
|
|
1521
|
-
// tag byte included) so we need to jump over 1+2 bytes of
|
|
1522
|
-
// compactSize uint.
|
|
1523
|
-
//
|
|
1524
|
-
// Please refer `BTCUtils` library and compactSize uint
|
|
1525
|
-
// docs in `BitcoinTx` library for more details.
|
|
1526
|
-
uint256 outputStartingIndex = 1 + outputsCompactSizeUintLength;
|
|
1527
|
-
|
|
1528
|
-
bytes20[] memory targetWallets = new bytes20[](outputsCount);
|
|
1529
|
-
uint64[] memory outputsValues = new uint64[](outputsCount);
|
|
1530
|
-
|
|
1531
|
-
// Outputs processing loop.
|
|
1532
|
-
for (uint256 i = 0; i < outputsCount; i++) {
|
|
1533
|
-
uint256 outputLength = movingFundsTxOutputVector
|
|
1534
|
-
.determineOutputLengthAt(outputStartingIndex);
|
|
1535
|
-
|
|
1536
|
-
bytes memory output = movingFundsTxOutputVector.slice(
|
|
1537
|
-
outputStartingIndex,
|
|
1538
|
-
outputLength
|
|
1539
|
-
);
|
|
1540
|
-
|
|
1541
|
-
// Extract the output script payload.
|
|
1542
|
-
bytes memory targetWalletPubKeyHashBytes = output.extractHash();
|
|
1543
|
-
// Output script payload must refer to a known wallet public key
|
|
1544
|
-
// hash which is always 20-byte.
|
|
1545
|
-
require(
|
|
1546
|
-
targetWalletPubKeyHashBytes.length == 20,
|
|
1547
|
-
"Target wallet public key hash must have 20 bytes"
|
|
1548
|
-
);
|
|
1549
|
-
|
|
1550
|
-
bytes20 targetWalletPubKeyHash = targetWalletPubKeyHashBytes
|
|
1551
|
-
.slice20(0);
|
|
1552
|
-
|
|
1553
|
-
// The next step is making sure that the 20-byte public key hash
|
|
1554
|
-
// is actually used in the right context of a P2PKH or P2WPKH
|
|
1555
|
-
// output. To do so, we must extract the full script from the output
|
|
1556
|
-
// and compare with the expected P2PKH and P2WPKH scripts
|
|
1557
|
-
// referring to that 20-byte public key hash. The output consists
|
|
1558
|
-
// of an 8-byte value and a variable length script. To extract the
|
|
1559
|
-
// script we slice the output starting from 9th byte until the end.
|
|
1560
|
-
bytes32 outputScriptKeccak = keccak256(
|
|
1561
|
-
output.slice(8, output.length - 8)
|
|
1562
|
-
);
|
|
1563
|
-
// Build the expected P2PKH script which has the following byte
|
|
1564
|
-
// format: <0x1976a914> <20-byte PKH> <0x88ac>. According to
|
|
1565
|
-
// https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
1566
|
-
// - 0x19: Byte length of the entire script
|
|
1567
|
-
// - 0x76: OP_DUP
|
|
1568
|
-
// - 0xa9: OP_HASH160
|
|
1569
|
-
// - 0x14: Byte length of the public key hash
|
|
1570
|
-
// - 0x88: OP_EQUALVERIFY
|
|
1571
|
-
// - 0xac: OP_CHECKSIG
|
|
1572
|
-
// which matches the P2PKH structure as per:
|
|
1573
|
-
// https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash
|
|
1574
|
-
bytes32 targetWalletP2PKHScriptKeccak = keccak256(
|
|
1575
|
-
abi.encodePacked(
|
|
1576
|
-
hex"1976a914",
|
|
1577
|
-
targetWalletPubKeyHash,
|
|
1578
|
-
hex"88ac"
|
|
1579
|
-
)
|
|
1580
|
-
);
|
|
1581
|
-
// Build the expected P2WPKH script which has the following format:
|
|
1582
|
-
// <0x160014> <20-byte PKH>. According to
|
|
1583
|
-
// https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
1584
|
-
// - 0x16: Byte length of the entire script
|
|
1585
|
-
// - 0x00: OP_0
|
|
1586
|
-
// - 0x14: Byte length of the public key hash
|
|
1587
|
-
// which matches the P2WPKH structure as per:
|
|
1588
|
-
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH
|
|
1589
|
-
bytes32 targetWalletP2WPKHScriptKeccak = keccak256(
|
|
1590
|
-
abi.encodePacked(hex"160014", targetWalletPubKeyHash)
|
|
1591
|
-
);
|
|
1592
|
-
// Make sure the actual output script matches either the P2PKH
|
|
1593
|
-
// or P2WPKH format.
|
|
1594
|
-
require(
|
|
1595
|
-
outputScriptKeccak == targetWalletP2PKHScriptKeccak ||
|
|
1596
|
-
outputScriptKeccak == targetWalletP2WPKHScriptKeccak,
|
|
1597
|
-
"Output must be P2PKH or P2WPKH"
|
|
1598
|
-
);
|
|
1599
|
-
|
|
1600
|
-
// Add the wallet public key hash to the list that will be used
|
|
1601
|
-
// to build the result list hash. There is no need to check if
|
|
1602
|
-
// given output is a change here because the actual target wallet
|
|
1603
|
-
// list must be exactly the same as the pre-committed target wallet
|
|
1604
|
-
// list which is guaranteed to be valid.
|
|
1605
|
-
targetWallets[i] = targetWalletPubKeyHash;
|
|
1606
|
-
|
|
1607
|
-
// Extract the value from given output.
|
|
1608
|
-
outputsValues[i] = output.extractValue();
|
|
1609
|
-
outputsTotalValue += outputsValues[i];
|
|
1610
|
-
|
|
1611
|
-
// Make the `outputStartingIndex` pointing to the next output by
|
|
1612
|
-
// increasing it by current output's length.
|
|
1613
|
-
outputStartingIndex += outputLength;
|
|
1614
|
-
}
|
|
1615
|
-
|
|
1616
|
-
// Compute the indivisible remainder that remains after dividing the
|
|
1617
|
-
// outputs total value over all outputs evenly.
|
|
1618
|
-
uint256 outputsTotalValueRemainder = outputsTotalValue % outputsCount;
|
|
1619
|
-
// Compute the minimum allowed output value by dividing the outputs
|
|
1620
|
-
// total value (reduced by the remainder) by the number of outputs.
|
|
1621
|
-
uint256 minOutputValue = (outputsTotalValue -
|
|
1622
|
-
outputsTotalValueRemainder) / outputsCount;
|
|
1623
|
-
// Maximum possible value is the minimum value with the remainder included.
|
|
1624
|
-
uint256 maxOutputValue = minOutputValue + outputsTotalValueRemainder;
|
|
1625
|
-
|
|
1626
|
-
for (uint256 i = 0; i < outputsCount; i++) {
|
|
1627
|
-
require(
|
|
1628
|
-
minOutputValue <= outputsValues[i] &&
|
|
1629
|
-
outputsValues[i] <= maxOutputValue,
|
|
1630
|
-
"Transaction amount is not distributed evenly"
|
|
1631
|
-
);
|
|
1632
|
-
}
|
|
1633
|
-
|
|
1634
|
-
targetWalletsHash = keccak256(abi.encodePacked(targetWallets));
|
|
1635
|
-
|
|
1636
|
-
return (targetWalletsHash, outputsTotalValue);
|
|
1637
842
|
}
|
|
1638
843
|
|
|
844
|
+
/// @notice Returns the addresses of contracts Bridge is interacting with.
|
|
1639
845
|
/// @return bank Address of the Bank the Bridge belongs to.
|
|
1640
846
|
/// @return relay Address of the Bitcoin relay providing the current Bitcoin
|
|
1641
847
|
/// network difficulty.
|
|
@@ -1683,6 +889,68 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
1683
889
|
txProofDifficultyFactor = self.txProofDifficultyFactor;
|
|
1684
890
|
}
|
|
1685
891
|
|
|
892
|
+
/// @notice Returns the current values of Bridge redemption parameters.
|
|
893
|
+
/// @return redemptionDustThreshold The minimal amount that can be requested
|
|
894
|
+
/// for redemption. Value of this parameter must take into account
|
|
895
|
+
/// the value of `redemptionTreasuryFeeDivisor` and `redemptionTxMaxFee`
|
|
896
|
+
/// parameters in order to make requests that can incur the
|
|
897
|
+
/// treasury and transaction fee and still satisfy the redeemer.
|
|
898
|
+
/// @return redemptionTreasuryFeeDivisor Divisor used to compute the treasury
|
|
899
|
+
/// fee taken from each redemption request and transferred to the
|
|
900
|
+
/// treasury upon successful request finalization. That fee is
|
|
901
|
+
/// computed as follows:
|
|
902
|
+
/// `treasuryFee = requestedAmount / redemptionTreasuryFeeDivisor`
|
|
903
|
+
/// For example, if the treasury fee needs to be 2% of each
|
|
904
|
+
/// redemption request, the `redemptionTreasuryFeeDivisor` should
|
|
905
|
+
/// be set to `50` because `1/50 = 0.02 = 2%`.
|
|
906
|
+
/// @return redemptionTxMaxFee Maximum amount of BTC transaction fee that
|
|
907
|
+
/// can be incurred by each redemption request being part of the
|
|
908
|
+
/// given redemption transaction. If the maximum BTC transaction
|
|
909
|
+
/// fee is exceeded, such transaction is considered a fraud.
|
|
910
|
+
/// @return redemptionTimeout Time after which the redemption request can be
|
|
911
|
+
/// reported as timed out. It is counted from the moment when the
|
|
912
|
+
/// redemption request was created via `requestRedemption` call.
|
|
913
|
+
/// Reported timed out requests are cancelled and locked TBTC is
|
|
914
|
+
/// returned to the redeemer in full amount.
|
|
915
|
+
/// @return treasury Address where the redemption treasury fees will be
|
|
916
|
+
/// sent to. Treasury takes part in the operators rewarding process.
|
|
917
|
+
/// @return txProofDifficultyFactor The number of confirmations on the
|
|
918
|
+
/// Bitcoin chain required to successfully evaluate an SPV proof.
|
|
919
|
+
function redemptionParameters()
|
|
920
|
+
external
|
|
921
|
+
view
|
|
922
|
+
returns (
|
|
923
|
+
uint64 redemptionDustThreshold,
|
|
924
|
+
uint64 redemptionTreasuryFeeDivisor,
|
|
925
|
+
uint64 redemptionTxMaxFee,
|
|
926
|
+
uint256 redemptionTimeout,
|
|
927
|
+
address treasury,
|
|
928
|
+
uint256 txProofDifficultyFactor
|
|
929
|
+
)
|
|
930
|
+
{
|
|
931
|
+
redemptionDustThreshold = self.redemptionDustThreshold;
|
|
932
|
+
redemptionTreasuryFeeDivisor = self.redemptionTreasuryFeeDivisor;
|
|
933
|
+
redemptionTxMaxFee = self.redemptionTxMaxFee;
|
|
934
|
+
redemptionTimeout = self.redemptionTimeout;
|
|
935
|
+
treasury = self.treasury;
|
|
936
|
+
txProofDifficultyFactor = self.txProofDifficultyFactor;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/// @notice Returns the current values of Bridge moving funds between
|
|
940
|
+
/// wallets parameters.
|
|
941
|
+
/// @return movingFundsTxMaxTotalFee Maximum amount of the total BTC
|
|
942
|
+
/// transaction fee that is acceptable in a single moving funds
|
|
943
|
+
/// transaction. This is a _total_ max fee for the entire moving
|
|
944
|
+
/// funds transaction.
|
|
945
|
+
function movingFundsParameters()
|
|
946
|
+
external
|
|
947
|
+
view
|
|
948
|
+
returns (uint64 movingFundsTxMaxTotalFee)
|
|
949
|
+
{
|
|
950
|
+
// TODO: we will have more parameters here, for example moving funds timeout
|
|
951
|
+
movingFundsTxMaxTotalFee = self.movingFundsTxMaxTotalFee;
|
|
952
|
+
}
|
|
953
|
+
|
|
1686
954
|
/// @notice Indicates if the vault with the given address is trusted or not.
|
|
1687
955
|
/// Depositors can route their revealed deposits only to trusted
|
|
1688
956
|
/// vaults and have trusted vaults notified about new deposits as
|
|
@@ -1705,7 +973,6 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
1705
973
|
view
|
|
1706
974
|
returns (Deposit.Request memory)
|
|
1707
975
|
{
|
|
1708
|
-
// TODO: rename to getDeposit?
|
|
1709
976
|
return self.deposits[depositKey];
|
|
1710
977
|
}
|
|
1711
978
|
|
|
@@ -1718,4 +985,47 @@ contract Bridge is Ownable, EcdsaWalletOwner {
|
|
|
1718
985
|
function spentMainUTXOs(uint256 utxoKey) external view returns (bool) {
|
|
1719
986
|
return self.spentMainUTXOs[utxoKey];
|
|
1720
987
|
}
|
|
988
|
+
|
|
989
|
+
/// @notice Collection of all pending redemption requests indexed by
|
|
990
|
+
/// redemption key built as
|
|
991
|
+
/// keccak256(walletPubKeyHash | redeemerOutputScript). The
|
|
992
|
+
/// walletPubKeyHash is the 20-byte wallet's public key hash
|
|
993
|
+
/// (computed using Bitcoin HASH160 over the compressed ECDSA
|
|
994
|
+
/// public key) and redeemerOutputScript is a Bitcoin script
|
|
995
|
+
/// (P2PKH, P2WPKH, P2SH or P2WSH) that will be used to lock
|
|
996
|
+
/// redeemed BTC as requested by the redeemer. Requests are added
|
|
997
|
+
/// to this mapping by the `requestRedemption` method (duplicates
|
|
998
|
+
/// not allowed) and are removed by one of the following methods:
|
|
999
|
+
/// - `submitRedemptionProof` in case the request was handled
|
|
1000
|
+
/// successfully
|
|
1001
|
+
/// - `notifyRedemptionTimeout` in case the request was reported
|
|
1002
|
+
/// to be timed out
|
|
1003
|
+
function pendingRedemptions(uint256 redemptionKey)
|
|
1004
|
+
external
|
|
1005
|
+
view
|
|
1006
|
+
returns (Redeem.RedemptionRequest memory)
|
|
1007
|
+
{
|
|
1008
|
+
return self.pendingRedemptions[redemptionKey];
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/// @notice Collection of all timed out redemptions requests indexed by
|
|
1012
|
+
/// redemption key built as
|
|
1013
|
+
/// keccak256(walletPubKeyHash | redeemerOutputScript). The
|
|
1014
|
+
/// walletPubKeyHash is the 20-byte wallet's public key hash
|
|
1015
|
+
/// (computed using Bitcoin HASH160 over the compressed ECDSA
|
|
1016
|
+
/// public key) and redeemerOutputScript is the Bitcoin script
|
|
1017
|
+
/// (P2PKH, P2WPKH, P2SH or P2WSH) that is involved in the timed
|
|
1018
|
+
/// out request. Timed out requests are stored in this mapping to
|
|
1019
|
+
/// avoid slashing the wallets multiple times for the same timeout.
|
|
1020
|
+
/// Only one method can add to this mapping:
|
|
1021
|
+
/// - `notifyRedemptionTimeout` which puts the redemption key
|
|
1022
|
+
/// to this mapping basing on a timed out request stored
|
|
1023
|
+
/// previously in `pendingRedemptions` mapping.
|
|
1024
|
+
function timedOutRedemptions(uint256 redemptionKey)
|
|
1025
|
+
external
|
|
1026
|
+
view
|
|
1027
|
+
returns (Redeem.RedemptionRequest memory)
|
|
1028
|
+
{
|
|
1029
|
+
return self.timedOutRedemptions[redemptionKey];
|
|
1030
|
+
}
|
|
1721
1031
|
}
|