@keep-network/tbtc-v2 0.1.1-dev.5 → 0.1.1-dev.52
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/README.adoc +12 -0
- package/artifacts/Bank.json +752 -0
- package/artifacts/Bridge.json +3307 -0
- package/artifacts/Deposit.json +117 -0
- package/artifacts/EcdsaDkgValidator.json +532 -0
- package/artifacts/EcdsaInactivity.json +156 -0
- package/artifacts/Fraud.json +154 -0
- package/artifacts/KeepRegistry.json +99 -0
- package/artifacts/KeepStake.json +286 -0
- package/artifacts/KeepToken.json +711 -0
- package/artifacts/KeepTokenStaking.json +483 -0
- package/artifacts/MovingFunds.json +160 -0
- package/artifacts/NuCypherStakingEscrow.json +256 -0
- package/artifacts/NuCypherToken.json +711 -0
- package/artifacts/RandomBeaconStub.json +141 -0
- package/artifacts/Redemption.json +161 -0
- package/artifacts/ReimbursementPool.json +509 -0
- package/artifacts/Relay.json +123 -0
- package/artifacts/SortitionPool.json +944 -0
- package/artifacts/Sweep.json +76 -0
- package/artifacts/T.json +1148 -0
- package/artifacts/TBTC.json +27 -26
- package/artifacts/TBTCToken.json +27 -26
- package/artifacts/TokenStaking.json +2288 -0
- package/artifacts/TokenholderGovernor.json +1795 -0
- package/artifacts/TokenholderTimelock.json +1058 -0
- package/artifacts/VendingMachine.json +30 -29
- package/artifacts/VendingMachineKeep.json +400 -0
- package/artifacts/VendingMachineNuCypher.json +400 -0
- package/artifacts/WalletRegistry.json +2709 -0
- package/artifacts/WalletRegistryGovernance.json +2364 -0
- package/artifacts/Wallets.json +186 -0
- package/artifacts/solcInputs/dcf2fc19e497270603c798faeb0a9ba9.json +218 -0
- package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
- package/build/contracts/GovernanceUtils.sol/GovernanceUtils.json +2 -2
- package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
- package/build/contracts/bank/Bank.sol/Bank.json +20 -2
- package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +4 -0
- package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +10 -0
- package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
- package/build/contracts/bridge/Bridge.sol/Bridge.json +2114 -77
- package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +4 -0
- package/build/contracts/bridge/BridgeState.sol/BridgeState.json +172 -0
- package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +4 -0
- package/build/contracts/bridge/Deposit.sol/Deposit.json +72 -0
- package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +4 -0
- package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.json +10 -0
- package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +4 -0
- package/build/contracts/bridge/Fraud.sol/Fraud.json +86 -0
- package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +4 -0
- package/build/contracts/bridge/IRelay.sol/IRelay.json +37 -0
- package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +4 -0
- package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +81 -0
- package/build/contracts/bridge/Redemption.sol/OutboundTx.dbg.json +4 -0
- package/build/contracts/bridge/Redemption.sol/OutboundTx.json +10 -0
- package/build/contracts/bridge/Redemption.sol/Redemption.dbg.json +4 -0
- package/build/contracts/bridge/Redemption.sol/Redemption.json +92 -0
- package/build/contracts/bridge/Sweep.sol/Sweep.dbg.json +4 -0
- package/build/contracts/bridge/Sweep.sol/Sweep.json +30 -0
- package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
- package/build/contracts/bridge/VendingMachine.sol/VendingMachine.json +2 -2
- package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +4 -0
- package/build/contracts/bridge/Wallets.sol/Wallets.json +112 -0
- package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
- package/build/contracts/token/TBTC.sol/TBTC.json +2 -2
- package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
- package/build/contracts/vault/IVault.sol/IVault.json +19 -1
- package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
- package/build/contracts/vault/TBTCVault.sol/TBTCVault.json +36 -18
- package/contracts/GovernanceUtils.sol +1 -1
- package/contracts/bank/Bank.sol +34 -18
- package/contracts/bridge/BitcoinTx.sol +234 -0
- package/contracts/bridge/Bridge.sol +1298 -126
- package/contracts/bridge/BridgeState.sol +553 -0
- package/contracts/bridge/Deposit.sol +266 -0
- package/contracts/bridge/EcdsaLib.sol +45 -0
- package/contracts/bridge/Fraud.sol +504 -0
- package/contracts/bridge/IRelay.sol +28 -0
- package/contracts/bridge/MovingFunds.sol +511 -0
- package/contracts/bridge/Redemption.sol +846 -0
- package/contracts/bridge/Sweep.sol +514 -0
- package/contracts/bridge/VendingMachine.sol +1 -1
- package/contracts/bridge/Wallets.sol +552 -0
- package/contracts/token/TBTC.sol +1 -1
- package/contracts/vault/IVault.sol +32 -10
- package/contracts/vault/TBTCVault.sol +20 -2
- package/deploy/00_resolve_relay.ts +28 -0
- package/deploy/04_deploy_bank.ts +27 -0
- package/deploy/05_deploy_bridge.ts +64 -0
- package/deploy/06_bank_update_bridge.ts +19 -0
- package/deploy/07_transfer_ownership.ts +15 -0
- package/deploy/08_transfer_governance.ts +20 -0
- package/export.json +15194 -472
- package/package.json +27 -23
- package/artifacts/solcInputs/d71966212a658480bad5748ad85b1396.json +0 -116
|
@@ -0,0 +1,846 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
// ██████████████ ▐████▌ ██████████████
|
|
4
|
+
// ██████████████ ▐████▌ ██████████████
|
|
5
|
+
// ▐████▌ ▐████▌
|
|
6
|
+
// ▐████▌ ▐████▌
|
|
7
|
+
// ██████████████ ▐████▌ ██████████████
|
|
8
|
+
// ██████████████ ▐████▌ ██████████████
|
|
9
|
+
// ▐████▌ ▐████▌
|
|
10
|
+
// ▐████▌ ▐████▌
|
|
11
|
+
// ▐████▌ ▐████▌
|
|
12
|
+
// ▐████▌ ▐████▌
|
|
13
|
+
// ▐████▌ ▐████▌
|
|
14
|
+
// ▐████▌ ▐████▌
|
|
15
|
+
|
|
16
|
+
pragma solidity ^0.8.9;
|
|
17
|
+
|
|
18
|
+
import {BTCUtils} from "@keep-network/bitcoin-spv-sol/contracts/BTCUtils.sol";
|
|
19
|
+
import {BytesLib} from "@keep-network/bitcoin-spv-sol/contracts/BytesLib.sol";
|
|
20
|
+
|
|
21
|
+
import "./BitcoinTx.sol";
|
|
22
|
+
import "./BridgeState.sol";
|
|
23
|
+
import "./Wallets.sol";
|
|
24
|
+
|
|
25
|
+
import "../bank/Bank.sol";
|
|
26
|
+
|
|
27
|
+
/// @notice Aggregates functions common to the redemption transaction proof
|
|
28
|
+
/// validation and to the moving funds transaction proof validation.
|
|
29
|
+
library OutboundTx {
|
|
30
|
+
using BTCUtils for bytes;
|
|
31
|
+
|
|
32
|
+
/// @notice Checks whether an outbound Bitcoin transaction performed from
|
|
33
|
+
/// the given wallet has an input vector that contains a single
|
|
34
|
+
/// input referring to the wallet's main UTXO. Marks that main UTXO
|
|
35
|
+
/// as correctly spent if the validation succeeds. Reverts otherwise.
|
|
36
|
+
/// There are two outbound transactions from a wallet possible: a
|
|
37
|
+
/// redemption transaction or a moving funds to another wallet
|
|
38
|
+
/// transaction.
|
|
39
|
+
/// @param walletOutboundTxInputVector Bitcoin outbound transaction's input
|
|
40
|
+
/// vector. This function assumes vector's structure is valid so it
|
|
41
|
+
/// must be validated using e.g. `BTCUtils.validateVin` function
|
|
42
|
+
/// before it is passed here
|
|
43
|
+
/// @param mainUtxo Data of the wallet's main UTXO, as currently known on
|
|
44
|
+
/// the Ethereum chain.
|
|
45
|
+
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
46
|
+
// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
47
|
+
/// performed the outbound transaction.
|
|
48
|
+
function processWalletOutboundTxInput(
|
|
49
|
+
BridgeState.Storage storage self,
|
|
50
|
+
bytes memory walletOutboundTxInputVector,
|
|
51
|
+
BitcoinTx.UTXO calldata mainUtxo,
|
|
52
|
+
bytes20 walletPubKeyHash
|
|
53
|
+
) internal {
|
|
54
|
+
// Assert that main UTXO for passed wallet exists in storage.
|
|
55
|
+
bytes32 mainUtxoHash = self
|
|
56
|
+
.registeredWallets[walletPubKeyHash]
|
|
57
|
+
.mainUtxoHash;
|
|
58
|
+
require(mainUtxoHash != bytes32(0), "No main UTXO for given wallet");
|
|
59
|
+
|
|
60
|
+
// Assert that passed main UTXO parameter is the same as in storage and
|
|
61
|
+
// can be used for further processing.
|
|
62
|
+
require(
|
|
63
|
+
keccak256(
|
|
64
|
+
abi.encodePacked(
|
|
65
|
+
mainUtxo.txHash,
|
|
66
|
+
mainUtxo.txOutputIndex,
|
|
67
|
+
mainUtxo.txOutputValue
|
|
68
|
+
)
|
|
69
|
+
) == mainUtxoHash,
|
|
70
|
+
"Invalid main UTXO data"
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// Assert that the single outbound transaction input actually
|
|
74
|
+
// refers to the wallet's main UTXO.
|
|
75
|
+
(
|
|
76
|
+
bytes32 outpointTxHash,
|
|
77
|
+
uint32 outpointIndex
|
|
78
|
+
) = parseWalletOutboundTxInput(walletOutboundTxInputVector);
|
|
79
|
+
require(
|
|
80
|
+
mainUtxo.txHash == outpointTxHash &&
|
|
81
|
+
mainUtxo.txOutputIndex == outpointIndex,
|
|
82
|
+
"Outbound transaction input must point to the wallet's main UTXO"
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// Main UTXO used as an input, mark it as spent.
|
|
86
|
+
self.spentMainUTXOs[
|
|
87
|
+
uint256(
|
|
88
|
+
keccak256(
|
|
89
|
+
abi.encodePacked(mainUtxo.txHash, mainUtxo.txOutputIndex)
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
] = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// @notice Parses the input vector of an outbound Bitcoin transaction
|
|
96
|
+
/// performed from the given wallet. It extracts the single input
|
|
97
|
+
/// then the transaction hash and output index from its outpoint.
|
|
98
|
+
/// There are two outbound transactions from a wallet possible: a
|
|
99
|
+
/// redemption transaction or a moving funds to another wallet
|
|
100
|
+
/// transaction.
|
|
101
|
+
/// @param walletOutboundTxInputVector Bitcoin outbound transaction input
|
|
102
|
+
/// vector. This function assumes vector's structure is valid so it
|
|
103
|
+
/// must be validated using e.g. `BTCUtils.validateVin` function
|
|
104
|
+
/// before it is passed here
|
|
105
|
+
/// @return outpointTxHash 32-byte hash of the Bitcoin transaction which is
|
|
106
|
+
/// pointed in the input's outpoint.
|
|
107
|
+
/// @return outpointIndex 4-byte index of the Bitcoin transaction output
|
|
108
|
+
/// which is pointed in the input's outpoint.
|
|
109
|
+
function parseWalletOutboundTxInput(
|
|
110
|
+
bytes memory walletOutboundTxInputVector
|
|
111
|
+
) internal pure returns (bytes32 outpointTxHash, uint32 outpointIndex) {
|
|
112
|
+
// To determine the total number of Bitcoin transaction inputs,
|
|
113
|
+
// we need to parse the compactSize uint (VarInt) the input vector is
|
|
114
|
+
// prepended by. That compactSize uint encodes the number of vector
|
|
115
|
+
// elements using the format presented in:
|
|
116
|
+
// https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
|
|
117
|
+
// We don't need asserting the compactSize uint is parseable since it
|
|
118
|
+
// was already checked during `validateVin` validation.
|
|
119
|
+
// See `BitcoinTx.inputVector` docs for more details.
|
|
120
|
+
(, uint256 inputsCount) = walletOutboundTxInputVector.parseVarInt();
|
|
121
|
+
require(
|
|
122
|
+
inputsCount == 1,
|
|
123
|
+
"Outbound transaction must have a single input"
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
bytes memory input = walletOutboundTxInputVector.extractInputAtIndex(0);
|
|
127
|
+
|
|
128
|
+
outpointTxHash = input.extractInputTxIdLE();
|
|
129
|
+
|
|
130
|
+
outpointIndex = BTCUtils.reverseUint32(
|
|
131
|
+
uint32(input.extractTxIndexLE())
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
// There is only one input in the transaction. Input has an outpoint
|
|
135
|
+
// field that is a reference to the transaction being spent (see
|
|
136
|
+
// `BitcoinTx` docs). The outpoint contains the hash of the transaction
|
|
137
|
+
// to spend (`outpointTxHash`) and the index of the specific output
|
|
138
|
+
// from that transaction (`outpointIndex`).
|
|
139
|
+
return (outpointTxHash, outpointIndex);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/// @title Bridge redemption
|
|
144
|
+
/// @notice The library handles the logic for redeeming Bitcoin balances from
|
|
145
|
+
/// the Bridge.
|
|
146
|
+
/// @dev To initiate a redemption, a user with a Bank balance supplies
|
|
147
|
+
/// a Bitcoin address. Then, the system calculates the redemption fee, and
|
|
148
|
+
/// releases balance to the provided Bitcoin address. Just like in case of
|
|
149
|
+
/// sweeps of revealed deposits, redemption requests are processed in
|
|
150
|
+
/// batches and require SPV proof to be submitted to the Bridge.
|
|
151
|
+
library Redemption {
|
|
152
|
+
using BridgeState for BridgeState.Storage;
|
|
153
|
+
using Wallets for BridgeState.Storage;
|
|
154
|
+
using BitcoinTx for BridgeState.Storage;
|
|
155
|
+
|
|
156
|
+
using BTCUtils for bytes;
|
|
157
|
+
using BytesLib for bytes;
|
|
158
|
+
|
|
159
|
+
/// @notice Represents a redemption request.
|
|
160
|
+
struct RedemptionRequest {
|
|
161
|
+
// ETH address of the redeemer who created the request.
|
|
162
|
+
address redeemer;
|
|
163
|
+
// Requested TBTC amount in satoshi.
|
|
164
|
+
uint64 requestedAmount;
|
|
165
|
+
// Treasury TBTC fee in satoshi at the moment of request creation.
|
|
166
|
+
uint64 treasuryFee;
|
|
167
|
+
// Transaction maximum BTC fee in satoshi at the moment of request
|
|
168
|
+
// creation.
|
|
169
|
+
uint64 txMaxFee;
|
|
170
|
+
// UNIX timestamp the request was created at.
|
|
171
|
+
uint32 requestedAt;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// @notice Represents an outcome of the redemption Bitcoin transaction
|
|
175
|
+
/// outputs processing.
|
|
176
|
+
struct RedemptionTxOutputsInfo {
|
|
177
|
+
// Total TBTC value in satoshi that should be burned by the Bridge.
|
|
178
|
+
// It includes the total amount of all BTC redeemed in the transaction
|
|
179
|
+
// and the fee paid to BTC miners for the redemption transaction.
|
|
180
|
+
uint64 totalBurnableValue;
|
|
181
|
+
// Total TBTC value in satoshi that should be transferred to
|
|
182
|
+
// the treasury. It is a sum of all treasury fees paid by all
|
|
183
|
+
// redeemers included in the redemption transaction.
|
|
184
|
+
uint64 totalTreasuryFee;
|
|
185
|
+
// Index of the change output. The change output becomes
|
|
186
|
+
// the new main wallet's UTXO.
|
|
187
|
+
uint32 changeIndex;
|
|
188
|
+
// Value in satoshi of the change output.
|
|
189
|
+
uint64 changeValue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// @notice Represents temporary information needed during the processing of
|
|
193
|
+
/// the redemption Bitcoin transaction outputs. This structure is an
|
|
194
|
+
/// internal one and should not be exported outside of the redemption
|
|
195
|
+
/// transaction processing code.
|
|
196
|
+
/// @dev Allows to mitigate "stack too deep" errors on EVM.
|
|
197
|
+
struct RedemptionTxOutputsProcessingInfo {
|
|
198
|
+
// The first output starting index in the transaction.
|
|
199
|
+
uint256 outputStartingIndex;
|
|
200
|
+
// The number of outputs in the transaction.
|
|
201
|
+
uint256 outputsCount;
|
|
202
|
+
// P2PKH script for the wallet. Needed to determine the change output.
|
|
203
|
+
bytes32 walletP2PKHScriptKeccak;
|
|
204
|
+
// P2WPKH script for the wallet. Needed to determine the change output.
|
|
205
|
+
bytes32 walletP2WPKHScriptKeccak;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
event RedemptionRequested(
|
|
209
|
+
bytes20 walletPubKeyHash,
|
|
210
|
+
bytes redeemerOutputScript,
|
|
211
|
+
address redeemer,
|
|
212
|
+
uint64 requestedAmount,
|
|
213
|
+
uint64 treasuryFee,
|
|
214
|
+
uint64 txMaxFee
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
event RedemptionsCompleted(
|
|
218
|
+
bytes20 walletPubKeyHash,
|
|
219
|
+
bytes32 redemptionTxHash
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
event RedemptionTimedOut(
|
|
223
|
+
bytes20 walletPubKeyHash,
|
|
224
|
+
bytes redeemerOutputScript
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
/// @notice Requests redemption of the given amount from the specified
|
|
228
|
+
/// wallet to the redeemer Bitcoin output script.
|
|
229
|
+
/// @param walletPubKeyHash The 20-byte wallet public key hash (computed
|
|
230
|
+
/// using Bitcoin HASH160 over the compressed ECDSA public key)
|
|
231
|
+
/// @param mainUtxo Data of the wallet's main UTXO, as currently known on
|
|
232
|
+
/// the Ethereum chain
|
|
233
|
+
/// @param redeemerOutputScript The redeemer's length-prefixed output
|
|
234
|
+
/// script (P2PKH, P2WPKH, P2SH or P2WSH) that will be used to lock
|
|
235
|
+
/// redeemed BTC
|
|
236
|
+
/// @param amount Requested amount in satoshi. This is also the TBTC amount
|
|
237
|
+
/// that is taken from redeemer's balance in the Bank upon request.
|
|
238
|
+
/// Once the request is handled, the actual amount of BTC locked
|
|
239
|
+
/// on the redeemer output script will be always lower than this value
|
|
240
|
+
/// since the treasury and Bitcoin transaction fees must be incurred.
|
|
241
|
+
/// The minimal amount satisfying the request can be computed as:
|
|
242
|
+
/// `amount - (amount / redemptionTreasuryFeeDivisor) - redemptionTxMaxFee`.
|
|
243
|
+
/// Fees values are taken at the moment of request creation.
|
|
244
|
+
/// @dev Requirements:
|
|
245
|
+
/// - Wallet behind `walletPubKeyHash` must be live
|
|
246
|
+
/// - `mainUtxo` components must point to the recent main UTXO
|
|
247
|
+
/// of the given wallet, as currently known on the Ethereum chain.
|
|
248
|
+
/// - `redeemerOutputScript` must be a proper Bitcoin script
|
|
249
|
+
/// - `redeemerOutputScript` cannot have wallet PKH as payload
|
|
250
|
+
/// - `amount` must be above or equal the `redemptionDustThreshold`
|
|
251
|
+
/// - Given `walletPubKeyHash` and `redeemerOutputScript` pair can be
|
|
252
|
+
/// used for only one pending request at the same time
|
|
253
|
+
/// - Wallet must have enough Bitcoin balance to proceed the request
|
|
254
|
+
/// - Redeemer must make an allowance in the Bank that the Bridge
|
|
255
|
+
/// contract can spend the given `amount`.
|
|
256
|
+
function requestRedemption(
|
|
257
|
+
BridgeState.Storage storage self,
|
|
258
|
+
bytes20 walletPubKeyHash,
|
|
259
|
+
BitcoinTx.UTXO calldata mainUtxo,
|
|
260
|
+
bytes calldata redeemerOutputScript,
|
|
261
|
+
uint64 amount
|
|
262
|
+
) external {
|
|
263
|
+
Wallets.Wallet storage wallet = self.registeredWallets[
|
|
264
|
+
walletPubKeyHash
|
|
265
|
+
];
|
|
266
|
+
|
|
267
|
+
require(
|
|
268
|
+
wallet.state == Wallets.WalletState.Live,
|
|
269
|
+
"Wallet must be in Live state"
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
bytes32 mainUtxoHash = wallet.mainUtxoHash;
|
|
273
|
+
require(
|
|
274
|
+
mainUtxoHash != bytes32(0),
|
|
275
|
+
"No main UTXO for the given wallet"
|
|
276
|
+
);
|
|
277
|
+
require(
|
|
278
|
+
keccak256(
|
|
279
|
+
abi.encodePacked(
|
|
280
|
+
mainUtxo.txHash,
|
|
281
|
+
mainUtxo.txOutputIndex,
|
|
282
|
+
mainUtxo.txOutputValue
|
|
283
|
+
)
|
|
284
|
+
) == mainUtxoHash,
|
|
285
|
+
"Invalid main UTXO data"
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
// TODO: Confirm if `walletPubKeyHash` should be validated by checking
|
|
289
|
+
// if it is the oldest one who can handle the request. This will
|
|
290
|
+
// be suggested by the dApp but may not be respected by users who
|
|
291
|
+
// interact directly with the contract. Do we need to enforce it
|
|
292
|
+
// here? One option is not to enforce it, to save on gas, but if
|
|
293
|
+
// we see this rule is not respected, upgrade Bridge contract to
|
|
294
|
+
// require it.
|
|
295
|
+
|
|
296
|
+
// Validate if redeemer output script is a correct standard type
|
|
297
|
+
// (P2PKH, P2WPKH, P2SH or P2WSH). This is done by building a stub
|
|
298
|
+
// output with 0 as value and using `BTCUtils.extractHash` on it. Such
|
|
299
|
+
// a function extracts the payload properly only from standard outputs
|
|
300
|
+
// so if it succeeds, we have a guarantee the redeemer output script
|
|
301
|
+
// is proper. Worth to note `extractHash` ignores the value at all
|
|
302
|
+
// so this is why we can use 0 safely. This way of validation is the
|
|
303
|
+
// same as in tBTC v1.
|
|
304
|
+
bytes memory redeemerOutputScriptPayload = abi
|
|
305
|
+
.encodePacked(bytes8(0), redeemerOutputScript)
|
|
306
|
+
.extractHash();
|
|
307
|
+
require(
|
|
308
|
+
redeemerOutputScriptPayload.length > 0,
|
|
309
|
+
"Redeemer output script must be a standard type"
|
|
310
|
+
);
|
|
311
|
+
// Check if the redeemer output script payload does not point to the
|
|
312
|
+
// wallet public key hash.
|
|
313
|
+
require(
|
|
314
|
+
keccak256(abi.encodePacked(walletPubKeyHash)) !=
|
|
315
|
+
keccak256(redeemerOutputScriptPayload),
|
|
316
|
+
"Redeemer output script must not point to the wallet PKH"
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
require(
|
|
320
|
+
amount >= self.redemptionDustThreshold,
|
|
321
|
+
"Redemption amount too small"
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
// The redemption key is built on top of the wallet public key hash
|
|
325
|
+
// and redeemer output script pair. That means there can be only one
|
|
326
|
+
// request asking for redemption from the given wallet to the given
|
|
327
|
+
// BTC script at the same time.
|
|
328
|
+
uint256 redemptionKey = uint256(
|
|
329
|
+
keccak256(abi.encodePacked(walletPubKeyHash, redeemerOutputScript))
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
// Check if given redemption key is not used by a pending redemption.
|
|
333
|
+
// There is no need to check for existence in `timedOutRedemptions`
|
|
334
|
+
// since the wallet's state is changed to other than Live after
|
|
335
|
+
// first time out is reported so making new requests is not possible.
|
|
336
|
+
// slither-disable-next-line incorrect-equality
|
|
337
|
+
require(
|
|
338
|
+
self.pendingRedemptions[redemptionKey].requestedAt == 0,
|
|
339
|
+
"There is a pending redemption request from this wallet to the same address"
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
// No need to check whether `amount - treasuryFee - txMaxFee > 0`
|
|
343
|
+
// since the `redemptionDustThreshold` should force that condition
|
|
344
|
+
// to be always true.
|
|
345
|
+
uint64 treasuryFee = self.redemptionTreasuryFeeDivisor > 0
|
|
346
|
+
? amount / self.redemptionTreasuryFeeDivisor
|
|
347
|
+
: 0;
|
|
348
|
+
uint64 txMaxFee = self.redemptionTxMaxFee;
|
|
349
|
+
|
|
350
|
+
// The main wallet UTXO's value doesn't include all pending redemptions.
|
|
351
|
+
// To determine if the requested redemption can be performed by the
|
|
352
|
+
// wallet we need to subtract the total value of all pending redemptions
|
|
353
|
+
// from that wallet's main UTXO value. Given that the treasury fee is
|
|
354
|
+
// not redeemed from the wallet, we are subtracting it.
|
|
355
|
+
wallet.pendingRedemptionsValue += amount - treasuryFee;
|
|
356
|
+
require(
|
|
357
|
+
mainUtxo.txOutputValue >= wallet.pendingRedemptionsValue,
|
|
358
|
+
"Insufficient wallet funds"
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
self.pendingRedemptions[redemptionKey] = RedemptionRequest(
|
|
362
|
+
msg.sender,
|
|
363
|
+
amount,
|
|
364
|
+
treasuryFee,
|
|
365
|
+
txMaxFee,
|
|
366
|
+
/* solhint-disable-next-line not-rely-on-time */
|
|
367
|
+
uint32(block.timestamp)
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
emit RedemptionRequested(
|
|
371
|
+
walletPubKeyHash,
|
|
372
|
+
redeemerOutputScript,
|
|
373
|
+
msg.sender,
|
|
374
|
+
amount,
|
|
375
|
+
treasuryFee,
|
|
376
|
+
txMaxFee
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
self.bank.transferBalanceFrom(msg.sender, address(this), amount);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/// @notice Used by the wallet to prove the BTC redemption transaction
|
|
383
|
+
/// and to make the necessary bookkeeping. Redemption is only
|
|
384
|
+
/// accepted if it satisfies SPV proof.
|
|
385
|
+
///
|
|
386
|
+
/// The function is performing Bank balance updates by burning
|
|
387
|
+
/// the total redeemed Bitcoin amount from Bridge balance and
|
|
388
|
+
/// transferring the treasury fee sum to the treasury address.
|
|
389
|
+
///
|
|
390
|
+
/// It is possible to prove the given redemption only one time.
|
|
391
|
+
/// @param redemptionTx Bitcoin redemption transaction data
|
|
392
|
+
/// @param redemptionProof Bitcoin redemption proof data
|
|
393
|
+
/// @param mainUtxo Data of the wallet's main UTXO, as currently known on
|
|
394
|
+
/// the Ethereum chain
|
|
395
|
+
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
396
|
+
/// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
397
|
+
/// performed the redemption transaction
|
|
398
|
+
/// @dev Requirements:
|
|
399
|
+
/// - `redemptionTx` components must match the expected structure. See
|
|
400
|
+
/// `BitcoinTx.Info` docs for reference. Their values must exactly
|
|
401
|
+
/// correspond to appropriate Bitcoin transaction fields to produce
|
|
402
|
+
/// a provable transaction hash.
|
|
403
|
+
/// - The `redemptionTx` should represent a Bitcoin transaction with
|
|
404
|
+
/// exactly 1 input that refers to the wallet's main UTXO. That
|
|
405
|
+
/// transaction should have 1..n outputs handling existing pending
|
|
406
|
+
/// redemption requests or pointing to reported timed out requests.
|
|
407
|
+
/// There can be also 1 optional output representing the
|
|
408
|
+
/// change and pointing back to the 20-byte wallet public key hash.
|
|
409
|
+
/// The change should be always present if the redeemed value sum
|
|
410
|
+
/// is lower than the total wallet's BTC balance.
|
|
411
|
+
/// - `redemptionProof` components must match the expected structure.
|
|
412
|
+
/// See `BitcoinTx.Proof` docs for reference. The `bitcoinHeaders`
|
|
413
|
+
/// field must contain a valid number of block headers, not less
|
|
414
|
+
/// than the `txProofDifficultyFactor` contract constant.
|
|
415
|
+
/// - `mainUtxo` components must point to the recent main UTXO
|
|
416
|
+
/// of the given wallet, as currently known on the Ethereum chain.
|
|
417
|
+
/// Additionally, the recent main UTXO on Ethereum must be set.
|
|
418
|
+
/// - `walletPubKeyHash` must be connected with the main UTXO used
|
|
419
|
+
/// as transaction single input.
|
|
420
|
+
/// Other remarks:
|
|
421
|
+
/// - Putting the change output as the first transaction output can
|
|
422
|
+
/// save some gas because the output processing loop begins each
|
|
423
|
+
/// iteration by checking whether the given output is the change
|
|
424
|
+
/// thus uses some gas for making the comparison. Once the change
|
|
425
|
+
/// is identified, that check is omitted in further iterations.
|
|
426
|
+
function submitRedemptionProof(
|
|
427
|
+
BridgeState.Storage storage self,
|
|
428
|
+
BitcoinTx.Info calldata redemptionTx,
|
|
429
|
+
BitcoinTx.Proof calldata redemptionProof,
|
|
430
|
+
BitcoinTx.UTXO calldata mainUtxo,
|
|
431
|
+
bytes20 walletPubKeyHash
|
|
432
|
+
) external {
|
|
433
|
+
// The actual transaction proof is performed here. After that point, we
|
|
434
|
+
// can assume the transaction happened on Bitcoin chain and has
|
|
435
|
+
// a sufficient number of confirmations as determined by
|
|
436
|
+
// `txProofDifficultyFactor` constant.
|
|
437
|
+
bytes32 redemptionTxHash = self.validateProof(
|
|
438
|
+
redemptionTx,
|
|
439
|
+
redemptionProof
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
// Process the redemption transaction input. Specifically, check if it
|
|
443
|
+
// refers to the expected wallet's main UTXO.
|
|
444
|
+
OutboundTx.processWalletOutboundTxInput(
|
|
445
|
+
self,
|
|
446
|
+
redemptionTx.inputVector,
|
|
447
|
+
mainUtxo,
|
|
448
|
+
walletPubKeyHash
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
Wallets.Wallet storage wallet = self.registeredWallets[
|
|
452
|
+
walletPubKeyHash
|
|
453
|
+
];
|
|
454
|
+
|
|
455
|
+
Wallets.WalletState walletState = wallet.state;
|
|
456
|
+
require(
|
|
457
|
+
walletState == Wallets.WalletState.Live ||
|
|
458
|
+
walletState == Wallets.WalletState.MovingFunds,
|
|
459
|
+
"Wallet must be in Live or MovingFunds state"
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
// Process redemption transaction outputs to extract some info required
|
|
463
|
+
// for further processing.
|
|
464
|
+
RedemptionTxOutputsInfo memory outputsInfo = processRedemptionTxOutputs(
|
|
465
|
+
self,
|
|
466
|
+
redemptionTx.outputVector,
|
|
467
|
+
walletPubKeyHash
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
if (outputsInfo.changeValue > 0) {
|
|
471
|
+
// If the change value is grater than zero, it means the change
|
|
472
|
+
// output exists and can be used as new wallet's main UTXO.
|
|
473
|
+
wallet.mainUtxoHash = keccak256(
|
|
474
|
+
abi.encodePacked(
|
|
475
|
+
redemptionTxHash,
|
|
476
|
+
outputsInfo.changeIndex,
|
|
477
|
+
outputsInfo.changeValue
|
|
478
|
+
)
|
|
479
|
+
);
|
|
480
|
+
} else {
|
|
481
|
+
// If the change value is zero, it means the change output doesn't
|
|
482
|
+
// exists and no funds left on the wallet. Delete the main UTXO
|
|
483
|
+
// for that wallet to represent that state in a proper way.
|
|
484
|
+
delete wallet.mainUtxoHash;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
wallet.pendingRedemptionsValue -= outputsInfo.totalBurnableValue;
|
|
488
|
+
|
|
489
|
+
emit RedemptionsCompleted(walletPubKeyHash, redemptionTxHash);
|
|
490
|
+
|
|
491
|
+
self.bank.decreaseBalance(outputsInfo.totalBurnableValue);
|
|
492
|
+
self.bank.transferBalance(self.treasury, outputsInfo.totalTreasuryFee);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/// @notice Processes the Bitcoin redemption transaction output vector.
|
|
496
|
+
/// It extracts each output and tries to identify it as a pending
|
|
497
|
+
/// redemption request, reported timed out request, or change.
|
|
498
|
+
/// Reverts if one of the outputs cannot be recognized properly.
|
|
499
|
+
/// This function also marks each request as processed by removing
|
|
500
|
+
/// them from `pendingRedemptions` mapping.
|
|
501
|
+
/// @param redemptionTxOutputVector Bitcoin redemption transaction output
|
|
502
|
+
/// vector. This function assumes vector's structure is valid so it
|
|
503
|
+
/// must be validated using e.g. `BTCUtils.validateVout` function
|
|
504
|
+
/// before it is passed here
|
|
505
|
+
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
506
|
+
// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
507
|
+
/// performed the redemption transaction.
|
|
508
|
+
/// @return info Outcomes of the processing.
|
|
509
|
+
function processRedemptionTxOutputs(
|
|
510
|
+
BridgeState.Storage storage self,
|
|
511
|
+
bytes memory redemptionTxOutputVector,
|
|
512
|
+
bytes20 walletPubKeyHash
|
|
513
|
+
) internal returns (RedemptionTxOutputsInfo memory info) {
|
|
514
|
+
// Determining the total number of redemption transaction outputs in
|
|
515
|
+
// the same way as for number of inputs. See `BitcoinTx.outputVector`
|
|
516
|
+
// docs for more details.
|
|
517
|
+
(
|
|
518
|
+
uint256 outputsCompactSizeUintLength,
|
|
519
|
+
uint256 outputsCount
|
|
520
|
+
) = redemptionTxOutputVector.parseVarInt();
|
|
521
|
+
|
|
522
|
+
// To determine the first output starting index, we must jump over
|
|
523
|
+
// the compactSize uint which prepends the output vector. One byte
|
|
524
|
+
// must be added because `BtcUtils.parseVarInt` does not include
|
|
525
|
+
// compactSize uint tag in the returned length.
|
|
526
|
+
//
|
|
527
|
+
// For >= 0 && <= 252, `BTCUtils.determineVarIntDataLengthAt`
|
|
528
|
+
// returns `0`, so we jump over one byte of compactSize uint.
|
|
529
|
+
//
|
|
530
|
+
// For >= 253 && <= 0xffff there is `0xfd` tag,
|
|
531
|
+
// `BTCUtils.determineVarIntDataLengthAt` returns `2` (no
|
|
532
|
+
// tag byte included) so we need to jump over 1+2 bytes of
|
|
533
|
+
// compactSize uint.
|
|
534
|
+
//
|
|
535
|
+
// Please refer `BTCUtils` library and compactSize uint
|
|
536
|
+
// docs in `BitcoinTx` library for more details.
|
|
537
|
+
uint256 outputStartingIndex = 1 + outputsCompactSizeUintLength;
|
|
538
|
+
|
|
539
|
+
// Calculate the keccak256 for two possible wallet's P2PKH or P2WPKH
|
|
540
|
+
// scripts that can be used to lock the change. This is done upfront to
|
|
541
|
+
// save on gas. Both scripts have a strict format defined by Bitcoin.
|
|
542
|
+
//
|
|
543
|
+
// The P2PKH script has the byte format: <0x1976a914> <20-byte PKH> <0x88ac>.
|
|
544
|
+
// According to https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
545
|
+
// - 0x19: Byte length of the entire script
|
|
546
|
+
// - 0x76: OP_DUP
|
|
547
|
+
// - 0xa9: OP_HASH160
|
|
548
|
+
// - 0x14: Byte length of the public key hash
|
|
549
|
+
// - 0x88: OP_EQUALVERIFY
|
|
550
|
+
// - 0xac: OP_CHECKSIG
|
|
551
|
+
// which matches the P2PKH structure as per:
|
|
552
|
+
// https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash
|
|
553
|
+
bytes32 walletP2PKHScriptKeccak = keccak256(
|
|
554
|
+
abi.encodePacked(hex"1976a914", walletPubKeyHash, hex"88ac")
|
|
555
|
+
);
|
|
556
|
+
// The P2WPKH script has the byte format: <0x160014> <20-byte PKH>.
|
|
557
|
+
// According to https://en.bitcoin.it/wiki/Script#Opcodes this translates to:
|
|
558
|
+
// - 0x16: Byte length of the entire script
|
|
559
|
+
// - 0x00: OP_0
|
|
560
|
+
// - 0x14: Byte length of the public key hash
|
|
561
|
+
// which matches the P2WPKH structure as per:
|
|
562
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH
|
|
563
|
+
bytes32 walletP2WPKHScriptKeccak = keccak256(
|
|
564
|
+
abi.encodePacked(hex"160014", walletPubKeyHash)
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
return
|
|
568
|
+
processRedemptionTxOutputs(
|
|
569
|
+
self,
|
|
570
|
+
redemptionTxOutputVector,
|
|
571
|
+
walletPubKeyHash,
|
|
572
|
+
RedemptionTxOutputsProcessingInfo(
|
|
573
|
+
outputStartingIndex,
|
|
574
|
+
outputsCount,
|
|
575
|
+
walletP2PKHScriptKeccak,
|
|
576
|
+
walletP2WPKHScriptKeccak
|
|
577
|
+
)
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/// @notice Processes all outputs from the redemption transaction. Tries to
|
|
582
|
+
/// identify output as a change output, pending redemption request
|
|
583
|
+
// or reported redemption. Reverts if one of the outputs cannot be
|
|
584
|
+
/// recognized properly. Marks each request as processed by removing
|
|
585
|
+
/// them from `pendingRedemptions` mapping.
|
|
586
|
+
/// @param redemptionTxOutputVector Bitcoin redemption transaction output
|
|
587
|
+
/// vector. This function assumes vector's structure is valid so it
|
|
588
|
+
/// must be validated using e.g. `BTCUtils.validateVout` function
|
|
589
|
+
/// before it is passed here
|
|
590
|
+
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
591
|
+
// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
592
|
+
/// performed the redemption transaction.
|
|
593
|
+
/// @param processInfo RedemptionTxOutputsProcessingInfo identifying output
|
|
594
|
+
/// starting index, the number of outputs and possible wallet change
|
|
595
|
+
/// P2PKH and P2WPKH scripts.
|
|
596
|
+
function processRedemptionTxOutputs(
|
|
597
|
+
BridgeState.Storage storage self,
|
|
598
|
+
bytes memory redemptionTxOutputVector,
|
|
599
|
+
bytes20 walletPubKeyHash,
|
|
600
|
+
RedemptionTxOutputsProcessingInfo memory processInfo
|
|
601
|
+
) internal returns (RedemptionTxOutputsInfo memory resultInfo) {
|
|
602
|
+
// Helper flag indicating whether there was at least one redemption
|
|
603
|
+
// output present (redemption must be either pending or reported as
|
|
604
|
+
// timed out).
|
|
605
|
+
bool redemptionPresent = false;
|
|
606
|
+
|
|
607
|
+
// Outputs processing loop.
|
|
608
|
+
for (uint256 i = 0; i < processInfo.outputsCount; i++) {
|
|
609
|
+
// TODO: Check if we can optimize gas costs by adding
|
|
610
|
+
// `extractValueAt` and `extractHashAt` in `bitcoin-spv-sol`
|
|
611
|
+
// in order to avoid allocating bytes in memory.
|
|
612
|
+
uint256 outputLength = redemptionTxOutputVector
|
|
613
|
+
.determineOutputLengthAt(processInfo.outputStartingIndex);
|
|
614
|
+
bytes memory output = redemptionTxOutputVector.slice(
|
|
615
|
+
processInfo.outputStartingIndex,
|
|
616
|
+
outputLength
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
// Extract the value from given output.
|
|
620
|
+
uint64 outputValue = output.extractValue();
|
|
621
|
+
// The output consists of an 8-byte value and a variable length
|
|
622
|
+
// script. To extract that script we slice the output starting from
|
|
623
|
+
// 9th byte until the end.
|
|
624
|
+
bytes memory outputScript = output.slice(8, output.length - 8);
|
|
625
|
+
|
|
626
|
+
if (
|
|
627
|
+
resultInfo.changeValue == 0 &&
|
|
628
|
+
(keccak256(outputScript) ==
|
|
629
|
+
processInfo.walletP2PKHScriptKeccak ||
|
|
630
|
+
keccak256(outputScript) ==
|
|
631
|
+
processInfo.walletP2WPKHScriptKeccak) &&
|
|
632
|
+
outputValue > 0
|
|
633
|
+
) {
|
|
634
|
+
// If we entered here, that means the change output with a
|
|
635
|
+
// proper non-zero value was found.
|
|
636
|
+
resultInfo.changeIndex = uint32(i);
|
|
637
|
+
resultInfo.changeValue = outputValue;
|
|
638
|
+
} else {
|
|
639
|
+
// If we entered here, that the means the given output is
|
|
640
|
+
// supposed to represent a redemption.
|
|
641
|
+
(
|
|
642
|
+
uint64 burnableValue,
|
|
643
|
+
uint64 treasuryFee
|
|
644
|
+
) = processNonChangeRedemptionTxOutput(
|
|
645
|
+
self,
|
|
646
|
+
walletPubKeyHash,
|
|
647
|
+
outputScript,
|
|
648
|
+
outputValue
|
|
649
|
+
);
|
|
650
|
+
resultInfo.totalBurnableValue += burnableValue;
|
|
651
|
+
resultInfo.totalTreasuryFee += treasuryFee;
|
|
652
|
+
redemptionPresent = true;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// Make the `outputStartingIndex` pointing to the next output by
|
|
656
|
+
// increasing it by current output's length.
|
|
657
|
+
processInfo.outputStartingIndex += outputLength;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// Protect against the cases when there is only a single change output
|
|
661
|
+
// referring back to the wallet PKH and just burning main UTXO value
|
|
662
|
+
// for transaction fees.
|
|
663
|
+
require(
|
|
664
|
+
redemptionPresent,
|
|
665
|
+
"Redemption transaction must process at least one redemption"
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/// @notice Processes a single redemption transaction output. Tries to
|
|
670
|
+
/// identify output as a pending redemption request or reported
|
|
671
|
+
/// redemption timeout. Output script passed to this function must
|
|
672
|
+
/// not be the change output. Such output needs to be identified
|
|
673
|
+
/// separately before calling this function.
|
|
674
|
+
/// Reverts if output is neither requested pending redemption nor
|
|
675
|
+
/// requested and reported timed-out redemption.
|
|
676
|
+
/// This function also marks each pending request as processed by
|
|
677
|
+
/// removing them from `pendingRedemptions` mapping.
|
|
678
|
+
/// @param walletPubKeyHash 20-byte public key hash (computed using Bitcoin
|
|
679
|
+
// HASH160 over the compressed ECDSA public key) of the wallet which
|
|
680
|
+
/// performed the redemption transaction.
|
|
681
|
+
/// @param outputScript Non-change output script to be processed
|
|
682
|
+
/// @param outputValue Value of the output being processed
|
|
683
|
+
/// @return burnableValue The value burnable as a result of processing this
|
|
684
|
+
/// single redemption output. This value needs to be summed up with
|
|
685
|
+
/// burnable values of all other outputs to evaluate total burnable
|
|
686
|
+
/// value for the entire redemption transaction. This value is 0
|
|
687
|
+
/// for a timed-out redemption request.
|
|
688
|
+
/// @return treasuryFee The treasury fee from this single redemption output.
|
|
689
|
+
/// This value needs to be summed up with treasury fees of all other
|
|
690
|
+
/// outputs to evaluate the total treasury fee for the entire
|
|
691
|
+
/// redemption transaction. This value is 0 for a timed-out
|
|
692
|
+
/// redemption request.
|
|
693
|
+
function processNonChangeRedemptionTxOutput(
|
|
694
|
+
BridgeState.Storage storage self,
|
|
695
|
+
bytes20 walletPubKeyHash,
|
|
696
|
+
bytes memory outputScript,
|
|
697
|
+
uint64 outputValue
|
|
698
|
+
) internal returns (uint64 burnableValue, uint64 treasuryFee) {
|
|
699
|
+
// This function should be called only if the given output is
|
|
700
|
+
// supposed to represent a redemption. Build the redemption key
|
|
701
|
+
// to perform that check.
|
|
702
|
+
uint256 redemptionKey = uint256(
|
|
703
|
+
keccak256(abi.encodePacked(walletPubKeyHash, outputScript))
|
|
704
|
+
);
|
|
705
|
+
|
|
706
|
+
if (self.pendingRedemptions[redemptionKey].requestedAt != 0) {
|
|
707
|
+
// If we entered here, that means the output was identified
|
|
708
|
+
// as a pending redemption request.
|
|
709
|
+
RedemptionRequest storage request = self.pendingRedemptions[
|
|
710
|
+
redemptionKey
|
|
711
|
+
];
|
|
712
|
+
// Compute the request's redeemable amount as the requested
|
|
713
|
+
// amount reduced by the treasury fee. The request's
|
|
714
|
+
// minimal amount is then the redeemable amount reduced by
|
|
715
|
+
// the maximum transaction fee.
|
|
716
|
+
uint64 redeemableAmount = request.requestedAmount -
|
|
717
|
+
request.treasuryFee;
|
|
718
|
+
// Output value must fit between the request's redeemable
|
|
719
|
+
// and minimal amounts to be deemed valid.
|
|
720
|
+
require(
|
|
721
|
+
redeemableAmount - request.txMaxFee <= outputValue &&
|
|
722
|
+
outputValue <= redeemableAmount,
|
|
723
|
+
"Output value is not within the acceptable range of the pending request"
|
|
724
|
+
);
|
|
725
|
+
// Add the redeemable amount to the total burnable value
|
|
726
|
+
// the Bridge will use to decrease its balance in the Bank.
|
|
727
|
+
burnableValue = redeemableAmount;
|
|
728
|
+
// Add the request's treasury fee to the total treasury fee
|
|
729
|
+
// value the Bridge will transfer to the treasury.
|
|
730
|
+
treasuryFee = request.treasuryFee;
|
|
731
|
+
// Request was properly handled so remove its redemption
|
|
732
|
+
// key from the mapping to make it reusable for further
|
|
733
|
+
// requests.
|
|
734
|
+
delete self.pendingRedemptions[redemptionKey];
|
|
735
|
+
} else {
|
|
736
|
+
// If we entered here, the output is not a redemption
|
|
737
|
+
// request but there is still a chance the given output is
|
|
738
|
+
// related to a reported timed out redemption request.
|
|
739
|
+
// If so, check if the output value matches the request
|
|
740
|
+
// amount to confirm this is an overdue request fulfillment
|
|
741
|
+
// then bypass this output and process the subsequent
|
|
742
|
+
// ones. That also means the wallet was already punished
|
|
743
|
+
// for the inactivity. Otherwise, just revert.
|
|
744
|
+
RedemptionRequest storage request = self.timedOutRedemptions[
|
|
745
|
+
redemptionKey
|
|
746
|
+
];
|
|
747
|
+
|
|
748
|
+
require(
|
|
749
|
+
request.requestedAt != 0,
|
|
750
|
+
"Output is a non-requested redemption"
|
|
751
|
+
);
|
|
752
|
+
|
|
753
|
+
uint64 redeemableAmount = request.requestedAmount -
|
|
754
|
+
request.treasuryFee;
|
|
755
|
+
|
|
756
|
+
require(
|
|
757
|
+
redeemableAmount - request.txMaxFee <= outputValue &&
|
|
758
|
+
outputValue <= redeemableAmount,
|
|
759
|
+
"Output value is not within the acceptable range of the timed out request"
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/// @notice Notifies that there is a pending redemption request associated
|
|
765
|
+
/// with the given wallet, that has timed out. The redemption
|
|
766
|
+
/// request is identified by the key built as
|
|
767
|
+
/// `keccak256(walletPubKeyHash | redeemerOutputScript)`.
|
|
768
|
+
/// The results of calling this function: the pending redemptions
|
|
769
|
+
/// value for the wallet will be decreased by the requested amount
|
|
770
|
+
/// (minus treasury fee), the tokens taken from the redeemer on
|
|
771
|
+
/// redemption request will be returned to the redeemer, the request
|
|
772
|
+
/// will be moved from pending redemptions to timed-out redemptions.
|
|
773
|
+
/// If the state of the wallet is `Live` or `MovingFunds`, the
|
|
774
|
+
/// wallet operators will be slashed.
|
|
775
|
+
/// Additionally, if the state of wallet is `Live`, the wallet will
|
|
776
|
+
/// be closed or marked as `MovingFunds` (depending on the presence
|
|
777
|
+
/// or absence of the wallet's main UTXO) and the wallet will no
|
|
778
|
+
/// longer be marked as the active wallet (if it was marked as such).
|
|
779
|
+
/// @param walletPubKeyHash 20-byte public key hash of the wallet
|
|
780
|
+
/// @param redeemerOutputScript The redeemer's length-prefixed output
|
|
781
|
+
/// script (P2PKH, P2WPKH, P2SH or P2WSH)
|
|
782
|
+
/// @dev Requirements:
|
|
783
|
+
/// - The redemption request identified by `walletPubKeyHash` and
|
|
784
|
+
/// `redeemerOutputScript` must exist
|
|
785
|
+
/// - The amount of time defined by `redemptionTimeout` must have
|
|
786
|
+
/// passed since the redemption was requested (the request must be
|
|
787
|
+
/// timed-out).
|
|
788
|
+
function notifyRedemptionTimeout(
|
|
789
|
+
BridgeState.Storage storage self,
|
|
790
|
+
bytes20 walletPubKeyHash,
|
|
791
|
+
bytes calldata redeemerOutputScript
|
|
792
|
+
) external {
|
|
793
|
+
uint256 redemptionKey = uint256(
|
|
794
|
+
keccak256(abi.encodePacked(walletPubKeyHash, redeemerOutputScript))
|
|
795
|
+
);
|
|
796
|
+
Redemption.RedemptionRequest memory request = self.pendingRedemptions[
|
|
797
|
+
redemptionKey
|
|
798
|
+
];
|
|
799
|
+
|
|
800
|
+
require(request.requestedAt > 0, "Redemption request does not exist");
|
|
801
|
+
require(
|
|
802
|
+
/* solhint-disable-next-line not-rely-on-time */
|
|
803
|
+
request.requestedAt + self.redemptionTimeout < block.timestamp,
|
|
804
|
+
"Redemption request has not timed out"
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
// Update the wallet's pending redemptions value
|
|
808
|
+
Wallets.Wallet storage wallet = self.registeredWallets[
|
|
809
|
+
walletPubKeyHash
|
|
810
|
+
];
|
|
811
|
+
wallet.pendingRedemptionsValue -=
|
|
812
|
+
request.requestedAmount -
|
|
813
|
+
request.treasuryFee;
|
|
814
|
+
|
|
815
|
+
require(
|
|
816
|
+
// TODO: Allow the wallets in `Closing` state when the state is added
|
|
817
|
+
wallet.state == Wallets.WalletState.Live ||
|
|
818
|
+
wallet.state == Wallets.WalletState.MovingFunds ||
|
|
819
|
+
wallet.state == Wallets.WalletState.Terminated,
|
|
820
|
+
"The wallet must be in Live, MovingFunds or Terminated state"
|
|
821
|
+
);
|
|
822
|
+
|
|
823
|
+
// It is worth noting that there is no need to check if
|
|
824
|
+
// `timedOutRedemption` mapping already contains the given redemption
|
|
825
|
+
// key. There is no possibility to re-use a key of a reported timed-out
|
|
826
|
+
// redemption because the wallet responsible for causing the timeout is
|
|
827
|
+
// moved to a state that prevents it to receive new redemption requests.
|
|
828
|
+
|
|
829
|
+
// Move the redemption from pending redemptions to timed-out redemptions
|
|
830
|
+
self.timedOutRedemptions[redemptionKey] = request;
|
|
831
|
+
delete self.pendingRedemptions[redemptionKey];
|
|
832
|
+
|
|
833
|
+
if (
|
|
834
|
+
wallet.state == Wallets.WalletState.Live ||
|
|
835
|
+
wallet.state == Wallets.WalletState.MovingFunds
|
|
836
|
+
) {
|
|
837
|
+
// Propagate timeout consequences to the wallet
|
|
838
|
+
self.notifyWalletTimedOutRedemption(walletPubKeyHash);
|
|
839
|
+
}
|
|
840
|
+
// slither-disable-next-line reentrancy-events
|
|
841
|
+
emit RedemptionTimedOut(walletPubKeyHash, redeemerOutputScript);
|
|
842
|
+
|
|
843
|
+
// Return the requested amount of tokens to the redeemer
|
|
844
|
+
self.bank.transferBalance(request.redeemer, request.requestedAmount);
|
|
845
|
+
}
|
|
846
|
+
}
|