@keep-network/tbtc-v2 0.1.1-dev.6 → 0.1.1-dev.62

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 (101) hide show
  1. package/README.adoc +12 -0
  2. package/artifacts/Bank.json +752 -0
  3. package/artifacts/Bridge.json +3962 -0
  4. package/artifacts/Deposit.json +117 -0
  5. package/artifacts/DepositSweep.json +76 -0
  6. package/artifacts/EcdsaDkgValidator.json +532 -0
  7. package/artifacts/EcdsaInactivity.json +156 -0
  8. package/artifacts/Fraud.json +154 -0
  9. package/artifacts/KeepRegistry.json +99 -0
  10. package/artifacts/KeepStake.json +286 -0
  11. package/artifacts/KeepToken.json +711 -0
  12. package/artifacts/KeepTokenStaking.json +483 -0
  13. package/artifacts/MovingFunds.json +227 -0
  14. package/artifacts/NuCypherStakingEscrow.json +256 -0
  15. package/artifacts/NuCypherToken.json +711 -0
  16. package/artifacts/RandomBeaconStub.json +141 -0
  17. package/artifacts/Redemption.json +162 -0
  18. package/artifacts/ReimbursementPool.json +509 -0
  19. package/artifacts/Relay.json +123 -0
  20. package/artifacts/SortitionPool.json +944 -0
  21. package/artifacts/T.json +1148 -0
  22. package/artifacts/TBTC.json +27 -26
  23. package/artifacts/TBTCToken.json +27 -26
  24. package/artifacts/TokenStaking.json +2288 -0
  25. package/artifacts/TokenholderGovernor.json +1795 -0
  26. package/artifacts/TokenholderTimelock.json +1058 -0
  27. package/artifacts/VendingMachine.json +30 -29
  28. package/artifacts/VendingMachineKeep.json +400 -0
  29. package/artifacts/VendingMachineNuCypher.json +400 -0
  30. package/artifacts/WalletRegistry.json +2709 -0
  31. package/artifacts/WalletRegistryGovernance.json +2364 -0
  32. package/artifacts/Wallets.json +186 -0
  33. package/artifacts/solcInputs/05c98d94f96a77da7702c7818a8cadac.json +227 -0
  34. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  35. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.json +2 -2
  36. package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
  37. package/build/contracts/bank/Bank.sol/Bank.json +20 -2
  38. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +4 -0
  39. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.json +10 -0
  40. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  41. package/build/contracts/bridge/Bridge.sol/Bridge.json +2470 -132
  42. package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +4 -0
  43. package/build/contracts/bridge/BridgeState.sol/BridgeState.json +220 -0
  44. package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +4 -0
  45. package/build/contracts/bridge/Deposit.sol/Deposit.json +72 -0
  46. package/build/contracts/bridge/DepositSweep.sol/DepositSweep.dbg.json +4 -0
  47. package/build/contracts/bridge/DepositSweep.sol/DepositSweep.json +30 -0
  48. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +4 -0
  49. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.json +10 -0
  50. package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +4 -0
  51. package/build/contracts/bridge/Fraud.sol/Fraud.json +86 -0
  52. package/build/contracts/bridge/Heartbeat.sol/Heartbeat.dbg.json +4 -0
  53. package/build/contracts/bridge/Heartbeat.sol/Heartbeat.json +10 -0
  54. package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +4 -0
  55. package/build/contracts/bridge/IRelay.sol/IRelay.json +37 -0
  56. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +4 -0
  57. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.json +125 -0
  58. package/build/contracts/bridge/Redemption.sol/OutboundTx.dbg.json +4 -0
  59. package/build/contracts/bridge/Redemption.sol/OutboundTx.json +10 -0
  60. package/build/contracts/bridge/Redemption.sol/Redemption.dbg.json +4 -0
  61. package/build/contracts/bridge/Redemption.sol/Redemption.json +92 -0
  62. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  63. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.json +2 -2
  64. package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +4 -0
  65. package/build/contracts/bridge/Wallets.sol/Wallets.json +112 -0
  66. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  67. package/build/contracts/token/TBTC.sol/TBTC.json +2 -2
  68. package/build/contracts/vault/DonationVault.sol/DonationVault.dbg.json +4 -0
  69. package/build/contracts/vault/DonationVault.sol/DonationVault.json +103 -0
  70. package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
  71. package/build/contracts/vault/IVault.sol/IVault.json +19 -1
  72. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
  73. package/build/contracts/vault/TBTCVault.sol/TBTCVault.json +36 -18
  74. package/contracts/GovernanceUtils.sol +1 -1
  75. package/contracts/bank/Bank.sol +34 -18
  76. package/contracts/bridge/BitcoinTx.sol +318 -0
  77. package/contracts/bridge/Bridge.sol +1527 -247
  78. package/contracts/bridge/BridgeState.sol +698 -0
  79. package/contracts/bridge/Deposit.sol +266 -0
  80. package/contracts/bridge/DepositSweep.sol +514 -0
  81. package/contracts/bridge/EcdsaLib.sol +45 -0
  82. package/contracts/bridge/Fraud.sol +508 -0
  83. package/contracts/bridge/Heartbeat.sol +107 -0
  84. package/contracts/bridge/IRelay.sol +28 -0
  85. package/contracts/bridge/MovingFunds.sol +1034 -0
  86. package/contracts/bridge/Redemption.sol +868 -0
  87. package/contracts/bridge/VendingMachine.sol +1 -1
  88. package/contracts/bridge/Wallets.sol +550 -0
  89. package/contracts/token/TBTC.sol +1 -1
  90. package/contracts/vault/DonationVault.sol +125 -0
  91. package/contracts/vault/IVault.sol +32 -10
  92. package/contracts/vault/TBTCVault.sol +20 -2
  93. package/deploy/00_resolve_relay.ts +28 -0
  94. package/deploy/04_deploy_bank.ts +27 -0
  95. package/deploy/05_deploy_bridge.ts +67 -0
  96. package/deploy/06_bank_update_bridge.ts +19 -0
  97. package/deploy/07_transfer_ownership.ts +15 -0
  98. package/deploy/08_transfer_governance.ts +20 -0
  99. package/export.json +15711 -475
  100. package/package.json +27 -24
  101. package/artifacts/solcInputs/c4fd2c31cc58f5fe0cc586dd84a84b60.json +0 -125
@@ -0,0 +1,266 @@
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
+ /// @title Bridge deposit
26
+ /// @notice The library handles the logic for revealing Bitcoin deposits to
27
+ /// the Bridge.
28
+ /// @dev The depositor puts together a P2SH or P2WSH address to deposit the
29
+ /// funds. This script is unique to each depositor and looks like this:
30
+ ///
31
+ /// ```
32
+ /// <depositorAddress> DROP
33
+ /// <blindingFactor> DROP
34
+ /// DUP HASH160 <walletPubKeyHash> EQUAL
35
+ /// IF
36
+ /// CHECKSIG
37
+ /// ELSE
38
+ /// DUP HASH160 <refundPubkeyHash> EQUALVERIFY
39
+ /// <refundLocktime> CHECKLOCKTIMEVERIFY DROP
40
+ /// CHECKSIG
41
+ /// ENDIF
42
+ /// ```
43
+ ///
44
+ /// Since each depositor has their own Ethereum address and their own
45
+ /// secret blinding factor, each depositor’s script is unique, and the hash
46
+ /// of each depositor’s script is unique.
47
+ library Deposit {
48
+ using BTCUtils for bytes;
49
+ using BytesLib for bytes;
50
+
51
+ /// @notice Represents data which must be revealed by the depositor during
52
+ /// deposit reveal.
53
+ struct DepositRevealInfo {
54
+ // Index of the funding output belonging to the funding transaction.
55
+ uint32 fundingOutputIndex;
56
+ // Ethereum depositor address.
57
+ address depositor;
58
+ // The blinding factor as 8 bytes. Byte endianness doesn't matter
59
+ // as this factor is not interpreted as uint.
60
+ bytes8 blindingFactor;
61
+ // The compressed Bitcoin public key (33 bytes and 02 or 03 prefix)
62
+ // of the deposit's wallet hashed in the HASH160 Bitcoin opcode style.
63
+ bytes20 walletPubKeyHash;
64
+ // The compressed Bitcoin public key (33 bytes and 02 or 03 prefix)
65
+ // that can be used to make the deposit refund after the refund
66
+ // locktime passes. Hashed in the HASH160 Bitcoin opcode style.
67
+ bytes20 refundPubKeyHash;
68
+ // The refund locktime (4-byte LE). Interpreted according to locktime
69
+ // parsing rules described in:
70
+ // https://developer.bitcoin.org/devguide/transactions.html#locktime-and-sequence-number
71
+ // and used with OP_CHECKLOCKTIMEVERIFY opcode as described in:
72
+ // https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
73
+ bytes4 refundLocktime;
74
+ // Address of the Bank vault to which the deposit is routed to.
75
+ // Optional, can be 0x0. The vault must be trusted by the Bridge.
76
+ address vault;
77
+ }
78
+
79
+ /// @notice Represents tBTC deposit request data.
80
+ struct DepositRequest {
81
+ // Ethereum depositor address.
82
+ address depositor;
83
+ // Deposit amount in satoshi.
84
+ uint64 amount;
85
+ // UNIX timestamp the deposit was revealed at.
86
+ uint32 revealedAt;
87
+ // Address of the Bank vault the deposit is routed to.
88
+ // Optional, can be 0x0.
89
+ address vault;
90
+ // Treasury TBTC fee in satoshi at the moment of deposit reveal.
91
+ uint64 treasuryFee;
92
+ // UNIX timestamp the deposit was swept at. Note this is not the
93
+ // time when the deposit was swept on the Bitcoin chain but actually
94
+ // the time when the sweep proof was delivered to the Ethereum chain.
95
+ uint32 sweptAt;
96
+ }
97
+
98
+ event DepositRevealed(
99
+ bytes32 fundingTxHash,
100
+ uint32 fundingOutputIndex,
101
+ address depositor,
102
+ uint64 amount,
103
+ bytes8 blindingFactor,
104
+ bytes20 walletPubKeyHash,
105
+ bytes20 refundPubKeyHash,
106
+ bytes4 refundLocktime,
107
+ address vault
108
+ );
109
+
110
+ /// @notice Used by the depositor to reveal information about their P2(W)SH
111
+ /// Bitcoin deposit to the Bridge on Ethereum chain. The off-chain
112
+ /// wallet listens for revealed deposit events and may decide to
113
+ /// include the revealed deposit in the next executed sweep.
114
+ /// Information about the Bitcoin deposit can be revealed before or
115
+ /// after the Bitcoin transaction with P2(W)SH deposit is mined on
116
+ /// the Bitcoin chain. Worth noting, the gas cost of this function
117
+ /// scales with the number of P2(W)SH transaction inputs and
118
+ /// outputs. The deposit may be routed to one of the trusted vaults.
119
+ /// When a deposit is routed to a vault, vault gets notified when
120
+ /// the deposit gets swept and it may execute the appropriate action.
121
+ /// @param fundingTx Bitcoin funding transaction data, see `BitcoinTx.Info`
122
+ /// @param reveal Deposit reveal data, see `RevealInfo struct
123
+ /// @dev Requirements:
124
+ /// - `reveal.walletPubKeyHash` must identify a `Live` wallet
125
+ /// - `reveal.vault` must be 0x0 or point to a trusted vault
126
+ /// - `reveal.fundingOutputIndex` must point to the actual P2(W)SH
127
+ /// output of the BTC deposit transaction
128
+ /// - `reveal.depositor` must be the Ethereum address used in the
129
+ /// P2(W)SH BTC deposit transaction,
130
+ /// - `reveal.blindingFactor` must be the blinding factor used in the
131
+ /// P2(W)SH BTC deposit transaction,
132
+ /// - `reveal.walletPubKeyHash` must be the wallet pub key hash used in
133
+ /// the P2(W)SH BTC deposit transaction,
134
+ /// - `reveal.refundPubKeyHash` must be the refund pub key hash used in
135
+ /// the P2(W)SH BTC deposit transaction,
136
+ /// - `reveal.refundLocktime` must be the refund locktime used in the
137
+ /// P2(W)SH BTC deposit transaction,
138
+ /// - BTC deposit for the given `fundingTxHash`, `fundingOutputIndex`
139
+ /// can be revealed only one time.
140
+ ///
141
+ /// If any of these requirements is not met, the wallet _must_ refuse
142
+ /// to sweep the deposit and the depositor has to wait until the
143
+ /// deposit script unlocks to receive their BTC back.
144
+ function revealDeposit(
145
+ BridgeState.Storage storage self,
146
+ BitcoinTx.Info calldata fundingTx,
147
+ DepositRevealInfo calldata reveal
148
+ ) external {
149
+ require(
150
+ self.registeredWallets[reveal.walletPubKeyHash].state ==
151
+ Wallets.WalletState.Live,
152
+ "Wallet must be in Live state"
153
+ );
154
+
155
+ require(
156
+ reveal.vault == address(0) || self.isVaultTrusted[reveal.vault],
157
+ "Vault is not trusted"
158
+ );
159
+
160
+ // TODO: Should we enforce a specific locktime at contract level?
161
+
162
+ bytes memory expectedScript = abi.encodePacked(
163
+ hex"14", // Byte length of depositor Ethereum address.
164
+ reveal.depositor,
165
+ hex"75", // OP_DROP
166
+ hex"08", // Byte length of blinding factor value.
167
+ reveal.blindingFactor,
168
+ hex"75", // OP_DROP
169
+ hex"76", // OP_DUP
170
+ hex"a9", // OP_HASH160
171
+ hex"14", // Byte length of a compressed Bitcoin public key hash.
172
+ reveal.walletPubKeyHash,
173
+ hex"87", // OP_EQUAL
174
+ hex"63", // OP_IF
175
+ hex"ac", // OP_CHECKSIG
176
+ hex"67", // OP_ELSE
177
+ hex"76", // OP_DUP
178
+ hex"a9", // OP_HASH160
179
+ hex"14", // Byte length of a compressed Bitcoin public key hash.
180
+ reveal.refundPubKeyHash,
181
+ hex"88", // OP_EQUALVERIFY
182
+ hex"04", // Byte length of refund locktime value.
183
+ reveal.refundLocktime,
184
+ hex"b1", // OP_CHECKLOCKTIMEVERIFY
185
+ hex"75", // OP_DROP
186
+ hex"ac", // OP_CHECKSIG
187
+ hex"68" // OP_ENDIF
188
+ );
189
+
190
+ bytes memory fundingOutput = fundingTx
191
+ .outputVector
192
+ .extractOutputAtIndex(reveal.fundingOutputIndex);
193
+ bytes memory fundingOutputHash = fundingOutput.extractHash();
194
+
195
+ if (fundingOutputHash.length == 20) {
196
+ // A 20-byte output hash is used by P2SH. That hash is constructed
197
+ // by applying OP_HASH160 on the locking script. A 20-byte output
198
+ // hash is used as well by P2PKH and P2WPKH (OP_HASH160 on the
199
+ // public key). However, since we compare the actual output hash
200
+ // with an expected locking script hash, this check will succeed only
201
+ // for P2SH transaction type with expected script hash value. For
202
+ // P2PKH and P2WPKH, it will fail on the output hash comparison with
203
+ // the expected locking script hash.
204
+ require(
205
+ fundingOutputHash.slice20(0) == expectedScript.hash160View(),
206
+ "Wrong 20-byte script hash"
207
+ );
208
+ } else if (fundingOutputHash.length == 32) {
209
+ // A 32-byte output hash is used by P2WSH. That hash is constructed
210
+ // by applying OP_SHA256 on the locking script.
211
+ require(
212
+ fundingOutputHash.toBytes32() == sha256(expectedScript),
213
+ "Wrong 32-byte script hash"
214
+ );
215
+ } else {
216
+ revert("Wrong script hash length");
217
+ }
218
+
219
+ // Resulting TX hash is in native Bitcoin little-endian format.
220
+ bytes32 fundingTxHash = abi
221
+ .encodePacked(
222
+ fundingTx.version,
223
+ fundingTx.inputVector,
224
+ fundingTx.outputVector,
225
+ fundingTx.locktime
226
+ )
227
+ .hash256View();
228
+
229
+ DepositRequest storage deposit = self.deposits[
230
+ uint256(
231
+ keccak256(
232
+ abi.encodePacked(fundingTxHash, reveal.fundingOutputIndex)
233
+ )
234
+ )
235
+ ];
236
+ require(deposit.revealedAt == 0, "Deposit already revealed");
237
+
238
+ uint64 fundingOutputAmount = fundingOutput.extractValue();
239
+
240
+ require(
241
+ fundingOutputAmount >= self.depositDustThreshold,
242
+ "Deposit amount too small"
243
+ );
244
+
245
+ deposit.amount = fundingOutputAmount;
246
+ deposit.depositor = reveal.depositor;
247
+ /* solhint-disable-next-line not-rely-on-time */
248
+ deposit.revealedAt = uint32(block.timestamp);
249
+ deposit.vault = reveal.vault;
250
+ deposit.treasuryFee = self.depositTreasuryFeeDivisor > 0
251
+ ? fundingOutputAmount / self.depositTreasuryFeeDivisor
252
+ : 0;
253
+ // slither-disable-next-line reentrancy-events
254
+ emit DepositRevealed(
255
+ fundingTxHash,
256
+ reveal.fundingOutputIndex,
257
+ reveal.depositor,
258
+ fundingOutputAmount,
259
+ reveal.blindingFactor,
260
+ reveal.walletPubKeyHash,
261
+ reveal.refundPubKeyHash,
262
+ reveal.refundLocktime,
263
+ reveal.vault
264
+ );
265
+ }
266
+ }