@keep-network/tbtc-v2 0.1.1-dev.2 → 0.1.1-dev.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/artifacts/TBTC.json +18 -18
  2. package/artifacts/TBTCToken.json +18 -18
  3. package/artifacts/VendingMachine.json +19 -19
  4. package/artifacts/solcInputs/e2b5f983e9c69369a4f41eb2f0688e3c.json +140 -0
  5. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  6. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.json +2 -2
  7. package/build/contracts/bank/Bank.sol/Bank.dbg.json +4 -0
  8. package/build/contracts/bank/Bank.sol/Bank.json +537 -0
  9. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +4 -0
  10. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +96 -0
  11. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  12. package/build/contracts/bridge/Bridge.sol/Bridge.json +767 -50
  13. package/build/contracts/bridge/Bridge.sol/IRelay.dbg.json +4 -0
  14. package/build/contracts/bridge/Bridge.sol/IRelay.json +37 -0
  15. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  16. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.json +2 -2
  17. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  18. package/build/contracts/token/TBTC.sol/TBTC.json +2 -2
  19. package/build/contracts/vault/IVault.sol/IVault.dbg.json +4 -0
  20. package/build/contracts/vault/IVault.sol/IVault.json +47 -0
  21. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +4 -0
  22. package/build/contracts/vault/TBTCVault.sol/TBTCVault.json +181 -0
  23. package/contracts/GovernanceUtils.sol +1 -1
  24. package/contracts/bank/Bank.sol +390 -0
  25. package/contracts/bridge/BitcoinTx.sol +231 -0
  26. package/contracts/bridge/Bridge.sol +1562 -104
  27. package/contracts/bridge/VendingMachine.sol +1 -1
  28. package/contracts/token/TBTC.sol +1 -1
  29. package/contracts/vault/IVault.sol +60 -0
  30. package/contracts/vault/TBTCVault.sol +146 -0
  31. package/package.json +10 -9
  32. package/artifacts/solcInputs/02b9e185d8beb23545e98201d474fc6b.json +0 -107
@@ -0,0 +1,231 @@
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 {ValidateSPV} from "@keep-network/bitcoin-spv-sol/contracts/ValidateSPV.sol";
20
+
21
+ /// @title Bitcoin transaction
22
+ /// @notice Allows to reference Bitcoin raw transaction in Solidity.
23
+ /// @dev See https://developer.bitcoin.org/reference/transactions.html#raw-transaction-format
24
+ ///
25
+ /// Raw Bitcon transaction data:
26
+ ///
27
+ /// | Bytes | Name | BTC type | Description |
28
+ /// |--------|--------------|------------------------|---------------------------|
29
+ /// | 4 | version | int32_t (LE) | TX version number |
30
+ /// | varies | tx_in_count | compactSize uint (LE) | Number of TX inputs |
31
+ /// | varies | tx_in | txIn[] | TX inputs |
32
+ /// | varies | tx_out count | compactSize uint (LE) | Number of TX outputs |
33
+ /// | varies | tx_out | txOut[] | TX outputs |
34
+ /// | 4 | lock_time | uint32_t (LE) | Unix time or block number |
35
+ ///
36
+ //
37
+ /// Non-coinbase transaction input (txIn):
38
+ ///
39
+ /// | Bytes | Name | BTC type | Description |
40
+ /// |--------|------------------|------------------------|---------------------------------------------|
41
+ /// | 36 | previous_output | outpoint | The previous outpoint being spent |
42
+ /// | varies | script bytes | compactSize uint (LE) | The number of bytes in the signature script |
43
+ /// | varies | signature script | char[] | The signature script, empty for P2WSH |
44
+ /// | 4 | sequence | uint32_t (LE) | Sequence number |
45
+ ///
46
+ ///
47
+ /// The reference to transaction being spent (outpoint):
48
+ ///
49
+ /// | Bytes | Name | BTC type | Description |
50
+ /// |-------|-------|---------------|------------------------------------------|
51
+ /// | 32 | hash | char[32] | Hash of the transaction to spend |
52
+ /// | 4 | index | uint32_t (LE) | Index of the specific output from the TX |
53
+ ///
54
+ ///
55
+ /// Transaction output (txOut):
56
+ ///
57
+ /// | Bytes | Name | BTC type | Description |
58
+ /// |--------|-----------------|-----------------------|--------------------------------------|
59
+ /// | 8 | value | int64_t (LE) | Number of satoshis to spend |
60
+ /// | 1+ | pk_script_bytes | compactSize uint (LE) | Number of bytes in the pubkey script |
61
+ /// | varies | pk_script | char[] | Pubkey script |
62
+ ///
63
+ /// compactSize uint format:
64
+ ///
65
+ /// | Value | Bytes | Format |
66
+ /// |-----------------------------------------|-------|----------------------------------------------|
67
+ /// | >= 0 && <= 252 | 1 | uint8_t |
68
+ /// | >= 253 && <= 0xffff | 3 | 0xfd followed by the number as uint16_t (LE) |
69
+ /// | >= 0x10000 && <= 0xffffffff | 5 | 0xfe followed by the number as uint32_t (LE) |
70
+ /// | >= 0x100000000 && <= 0xffffffffffffffff | 9 | 0xff followed by the number as uint64_t (LE) |
71
+ ///
72
+ /// (*) compactSize uint is often references as VarInt)
73
+ ///
74
+ library BitcoinTx {
75
+ using BTCUtils for bytes;
76
+ using BTCUtils for uint256;
77
+ using ValidateSPV for bytes;
78
+ using ValidateSPV for bytes32;
79
+
80
+ /// @notice Represents Bitcoin transaction data.
81
+ struct Info {
82
+ /// @notice Bitcoin transaction version
83
+ /// @dev `version` from raw Bitcon transaction data.
84
+ /// Encoded as 4-bytes signed integer, little endian.
85
+ bytes4 version;
86
+ /// @notice All Bitcoin transaction inputs, prepended by the number of
87
+ /// transaction inputs.
88
+ /// @dev `tx_in_count | tx_in` from raw Bitcon transaction data.
89
+ ///
90
+ /// The number of transaction inputs encoded as compactSize
91
+ /// unsigned integer, little-endian.
92
+ ///
93
+ /// Note that some popular block explorers reverse the order of
94
+ /// bytes from `outpoint`'s `hash` and display it as big-endian.
95
+ /// Solidity code of Bridge expects hashes in little-endian, just
96
+ /// like they are represented in a raw Bitcoin transaction.
97
+ bytes inputVector;
98
+ /// @notice All Bitcoin transaction outputs prepended by the number of
99
+ /// transaction outputs.
100
+ /// @dev `tx_out_count | tx_out` from raw Bitcoin transaction data.
101
+ ///
102
+ /// The number of transaction outputs encoded as a compactSize
103
+ /// unsigned integer, little-endian.
104
+ bytes outputVector;
105
+ /// @notice Bitcoin transaction locktime.
106
+ ///
107
+ /// @dev `lock_time` from raw Bitcoin transaction data.
108
+ /// Encoded as 4-bytes unsigned integer, little endian.
109
+ bytes4 locktime;
110
+ }
111
+
112
+ /// @notice Represents data needed to perform a Bitcoin SPV proof.
113
+ struct Proof {
114
+ /// @notice The merkle proof of transaction inclusion in a block.
115
+ bytes merkleProof;
116
+ /// @notice Transaction index in the block (0-indexed).
117
+ uint256 txIndexInBlock;
118
+ /// @notice Single byte-string of 80-byte bitcoin headers,
119
+ /// lowest height first.
120
+ bytes bitcoinHeaders;
121
+ }
122
+
123
+ /// @notice Determines the difficulty context for a Bitcoin SPV proof.
124
+ struct ProofDifficulty {
125
+ /// @notice Difficulty of the current epoch.
126
+ uint256 currentEpochDifficulty;
127
+ /// @notice Difficulty of the previous epoch.
128
+ uint256 previousEpochDifficulty;
129
+ /// @notice The number of confirmations on the Bitcoin chain required
130
+ /// to successfully evaluate an SPV proof.
131
+ uint256 difficultyFactor;
132
+ }
133
+
134
+ /// @notice Represents info about an unspent transaction output.
135
+ struct UTXO {
136
+ /// @notice Hash of the transaction the output belongs to.
137
+ /// @dev Byte order corresponds to the Bitcoin internal byte order.
138
+ bytes32 txHash;
139
+ /// @notice Index of the transaction output (0-indexed).
140
+ uint32 txOutputIndex;
141
+ /// @notice Value of the transaction output.
142
+ uint64 txOutputValue;
143
+ }
144
+
145
+ /// @notice Validates the SPV proof of the Bitcoin transaction.
146
+ /// Reverts in case the validation or proof verification fail.
147
+ /// @param txInfo Bitcoin transaction data
148
+ /// @param proof Bitcoin proof data
149
+ /// @param proofDifficulty Bitcoin proof difficulty context.
150
+ /// @return txHash Proven 32-byte transaction hash.
151
+ function validateProof(
152
+ Info calldata txInfo,
153
+ Proof calldata proof,
154
+ ProofDifficulty calldata proofDifficulty
155
+ ) external view returns (bytes32 txHash) {
156
+ require(
157
+ txInfo.inputVector.validateVin(),
158
+ "Invalid input vector provided"
159
+ );
160
+ require(
161
+ txInfo.outputVector.validateVout(),
162
+ "Invalid output vector provided"
163
+ );
164
+
165
+ txHash = abi
166
+ .encodePacked(
167
+ txInfo.version,
168
+ txInfo.inputVector,
169
+ txInfo.outputVector,
170
+ txInfo.locktime
171
+ )
172
+ .hash256View();
173
+
174
+ require(
175
+ txHash.prove(
176
+ proof.bitcoinHeaders.extractMerkleRootLE(),
177
+ proof.merkleProof,
178
+ proof.txIndexInBlock
179
+ ),
180
+ "Tx merkle proof is not valid for provided header and tx hash"
181
+ );
182
+
183
+ evaluateProofDifficulty(proof.bitcoinHeaders, proofDifficulty);
184
+
185
+ return txHash;
186
+ }
187
+
188
+ /// @notice Evaluates the given Bitcoin proof difficulty against the actual
189
+ /// Bitcoin chain difficulty provided by the relay oracle.
190
+ /// Reverts in case the evaluation fails.
191
+ /// @param bitcoinHeaders Bitcoin headers chain being part of the SPV
192
+ /// proof. Used to extract the observed proof difficulty
193
+ /// @param proofDifficulty Bitcoin proof difficulty context.
194
+ function evaluateProofDifficulty(
195
+ bytes memory bitcoinHeaders,
196
+ ProofDifficulty calldata proofDifficulty
197
+ ) internal view {
198
+ uint256 requestedDiff = 0;
199
+ uint256 firstHeaderDiff = bitcoinHeaders
200
+ .extractTarget()
201
+ .calculateDifficulty();
202
+
203
+ if (firstHeaderDiff == proofDifficulty.currentEpochDifficulty) {
204
+ requestedDiff = proofDifficulty.currentEpochDifficulty;
205
+ } else if (firstHeaderDiff == proofDifficulty.previousEpochDifficulty) {
206
+ requestedDiff = proofDifficulty.previousEpochDifficulty;
207
+ } else {
208
+ revert("Not at current or previous difficulty");
209
+ }
210
+
211
+ uint256 observedDiff = bitcoinHeaders.validateHeaderChain();
212
+
213
+ require(
214
+ observedDiff != ValidateSPV.getErrBadLength(),
215
+ "Invalid length of the headers chain"
216
+ );
217
+ require(
218
+ observedDiff != ValidateSPV.getErrInvalidChain(),
219
+ "Invalid headers chain"
220
+ );
221
+ require(
222
+ observedDiff != ValidateSPV.getErrLowWork(),
223
+ "Insufficient work in a header"
224
+ );
225
+
226
+ require(
227
+ observedDiff >= requestedDiff * proofDifficulty.difficultyFactor,
228
+ "Insufficient accumulated difficulty in header chain"
229
+ );
230
+ }
231
+ }