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

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.
@@ -0,0 +1,176 @@
1
+ {
2
+ "_format": "hh-sol-artifact-1",
3
+ "contractName": "Bridge",
4
+ "sourceName": "contracts/bridge/Bridge.sol",
5
+ "abi": [
6
+ {
7
+ "anonymous": false,
8
+ "inputs": [
9
+ {
10
+ "indexed": false,
11
+ "internalType": "uint256",
12
+ "name": "depositId",
13
+ "type": "uint256"
14
+ },
15
+ {
16
+ "indexed": false,
17
+ "internalType": "bytes32",
18
+ "name": "fundingTxHash",
19
+ "type": "bytes32"
20
+ },
21
+ {
22
+ "indexed": false,
23
+ "internalType": "uint8",
24
+ "name": "fundingOutputIndex",
25
+ "type": "uint8"
26
+ },
27
+ {
28
+ "indexed": false,
29
+ "internalType": "address",
30
+ "name": "depositor",
31
+ "type": "address"
32
+ },
33
+ {
34
+ "indexed": false,
35
+ "internalType": "uint64",
36
+ "name": "blindingFactor",
37
+ "type": "uint64"
38
+ },
39
+ {
40
+ "indexed": false,
41
+ "internalType": "bytes",
42
+ "name": "refundPubKey",
43
+ "type": "bytes"
44
+ },
45
+ {
46
+ "indexed": false,
47
+ "internalType": "uint64",
48
+ "name": "amount",
49
+ "type": "uint64"
50
+ },
51
+ {
52
+ "indexed": false,
53
+ "internalType": "address",
54
+ "name": "vault",
55
+ "type": "address"
56
+ }
57
+ ],
58
+ "name": "DepositRevealed",
59
+ "type": "event"
60
+ },
61
+ {
62
+ "inputs": [
63
+ {
64
+ "internalType": "bytes32",
65
+ "name": "fundingTxHash",
66
+ "type": "bytes32"
67
+ },
68
+ {
69
+ "internalType": "uint8",
70
+ "name": "fundingOutputIndex",
71
+ "type": "uint8"
72
+ },
73
+ {
74
+ "internalType": "uint64",
75
+ "name": "blindingFactor",
76
+ "type": "uint64"
77
+ },
78
+ {
79
+ "internalType": "bytes",
80
+ "name": "refundPubKey",
81
+ "type": "bytes"
82
+ },
83
+ {
84
+ "internalType": "uint64",
85
+ "name": "amount",
86
+ "type": "uint64"
87
+ },
88
+ {
89
+ "internalType": "address",
90
+ "name": "vault",
91
+ "type": "address"
92
+ }
93
+ ],
94
+ "name": "revealDeposit",
95
+ "outputs": [],
96
+ "stateMutability": "nonpayable",
97
+ "type": "function"
98
+ },
99
+ {
100
+ "inputs": [
101
+ {
102
+ "internalType": "bytes4",
103
+ "name": "txVersion",
104
+ "type": "bytes4"
105
+ },
106
+ {
107
+ "internalType": "bytes",
108
+ "name": "txInputVector",
109
+ "type": "bytes"
110
+ },
111
+ {
112
+ "internalType": "bytes",
113
+ "name": "txOutput",
114
+ "type": "bytes"
115
+ },
116
+ {
117
+ "internalType": "bytes4",
118
+ "name": "txLocktime",
119
+ "type": "bytes4"
120
+ },
121
+ {
122
+ "internalType": "bytes",
123
+ "name": "merkleProof",
124
+ "type": "bytes"
125
+ },
126
+ {
127
+ "internalType": "uint256",
128
+ "name": "txIndexInBlock",
129
+ "type": "uint256"
130
+ },
131
+ {
132
+ "internalType": "bytes",
133
+ "name": "bitcoinHeaders",
134
+ "type": "bytes"
135
+ }
136
+ ],
137
+ "name": "sweep",
138
+ "outputs": [],
139
+ "stateMutability": "nonpayable",
140
+ "type": "function"
141
+ },
142
+ {
143
+ "inputs": [
144
+ {
145
+ "internalType": "uint256",
146
+ "name": "",
147
+ "type": "uint256"
148
+ }
149
+ ],
150
+ "name": "unswept",
151
+ "outputs": [
152
+ {
153
+ "internalType": "uint64",
154
+ "name": "amount",
155
+ "type": "uint64"
156
+ },
157
+ {
158
+ "internalType": "address",
159
+ "name": "vault",
160
+ "type": "address"
161
+ },
162
+ {
163
+ "internalType": "uint32",
164
+ "name": "revealedAt",
165
+ "type": "uint32"
166
+ }
167
+ ],
168
+ "stateMutability": "view",
169
+ "type": "function"
170
+ }
171
+ ],
172
+ "bytecode": "0x608060405234801561001057600080fd5b506109f0806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632c3e1d121461004657806342b1f1a41461006257806343c6de291461007e575b600080fd5b610060600480360381019061005b91906103df565b6100b0565b005b61007c60048036038101906100779190610486565b610238565b005b61009860048036038101906100939190610584565b610241565b6040516100a793929190610735565b60405180910390f35b60008787336040516020016100c793929190610657565b6040516020818303038152906040528051906020012060001c905060008060008381526020019081526020016000209050600081600001601c9054906101000a900463ffffffff1663ffffffff1614610155576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161014c9061068e565b60405180910390fd5b838160000160006101000a81548167ffffffffffffffff021916908367ffffffffffffffff160217905550828160000160086101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055504281600001601c6101000a81548163ffffffff021916908363ffffffff1602179055507fed7e2a3180a53ccf8540bf67f1679359557837b4c2350f3c7f1d1409eaac3739828a8a338b8b8b8b8b604051610225999897969594939291906106ae565b60405180910390a1505050505050505050565b50505050505050565b60006020528060005260406000206000915090508060000160009054906101000a900467ffffffffffffffff16908060000160089054906101000a900473ffffffffffffffffffffffffffffffffffffffff169080600001601c9054906101000a900463ffffffff16905083565b60006102c26102bd84610791565b61076c565b9050828152602081018484840111156102da57600080fd5b6102e5848285610887565b509392505050565b6000813590506102fc81610930565b92915050565b60008135905061031181610947565b92915050565b6000813590506103268161095e565b92915050565b60008083601f84011261033e57600080fd5b8235905067ffffffffffffffff81111561035757600080fd5b60208301915083600182028301111561036f57600080fd5b9250929050565b600082601f83011261038757600080fd5b81356103978482602086016102af565b91505092915050565b6000813590506103af81610975565b92915050565b6000813590506103c48161098c565b92915050565b6000813590506103d9816109a3565b92915050565b600080600080600080600060c0888a0312156103fa57600080fd5b60006104088a828b01610302565b97505060206104198a828b016103ca565b965050604061042a8a828b016103b5565b955050606088013567ffffffffffffffff81111561044757600080fd5b6104538a828b0161032c565b945094505060806104668a828b016103b5565b92505060a06104778a828b016102ed565b91505092959891949750929550565b600080600080600080600060e0888a0312156104a157600080fd5b60006104af8a828b01610317565b975050602088013567ffffffffffffffff8111156104cc57600080fd5b6104d88a828b01610376565b965050604088013567ffffffffffffffff8111156104f557600080fd5b6105018a828b01610376565b95505060606105128a828b01610317565b945050608088013567ffffffffffffffff81111561052f57600080fd5b61053b8a828b01610376565b93505060a061054c8a828b016103a0565b92505060c088013567ffffffffffffffff81111561056957600080fd5b6105758a828b01610376565b91505092959891949750929550565b60006020828403121561059657600080fd5b60006105a4848285016103a0565b91505092915050565b6105b6816107e4565b82525050565b6105c5816107f6565b82525050565b60006105d783856107c2565b93506105e4838584610887565b6105ed836108f6565b840190509392505050565b60006106056018836107d3565b915061061082610907565b602082019050919050565b6106248161084c565b82525050565b61063381610856565b82525050565b61064281610866565b82525050565b6106518161087a565b82525050565b600060608201905061066c60008301866105bc565b6106796020830185610648565b61068660408301846105ad565b949350505050565b600060208201905081810360008301526106a7816105f8565b9050919050565b6000610100820190506106c4600083018c61061b565b6106d1602083018b6105bc565b6106de604083018a610648565b6106eb60608301896105ad565b6106f86080830188610639565b81810360a083015261070b8186886105cb565b905061071a60c0830185610639565b61072760e08301846105ad565b9a9950505050505050505050565b600060608201905061074a6000830186610639565b61075760208301856105ad565b610764604083018461062a565b949350505050565b6000610776610787565b90506107828282610896565b919050565b6000604051905090565b600067ffffffffffffffff8211156107ac576107ab6108c7565b5b6107b5826108f6565b9050602081019050919050565b600082825260208201905092915050565b600082825260208201905092915050565b60006107ef8261082c565b9050919050565b6000819050919050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600063ffffffff82169050919050565b600067ffffffffffffffff82169050919050565b600060ff82169050919050565b82818337600083830152505050565b61089f826108f6565b810181811067ffffffffffffffff821117156108be576108bd6108c7565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f8301169050919050565b7f4465706f73697420616c72656164792072657665616c65640000000000000000600082015250565b610939816107e4565b811461094457600080fd5b50565b610950816107f6565b811461095b57600080fd5b50565b61096781610800565b811461097257600080fd5b50565b61097e8161084c565b811461098957600080fd5b50565b61099581610866565b81146109a057600080fd5b50565b6109ac8161087a565b81146109b757600080fd5b5056fea2646970667358221220bca337d7cb05b6b689ca49ecdd85c75dc9cdde9d5b5a54318aba5449dda2e35064736f6c63430008040033",
173
+ "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100415760003560e01c80632c3e1d121461004657806342b1f1a41461006257806343c6de291461007e575b600080fd5b610060600480360381019061005b91906103df565b6100b0565b005b61007c60048036038101906100779190610486565b610238565b005b61009860048036038101906100939190610584565b610241565b6040516100a793929190610735565b60405180910390f35b60008787336040516020016100c793929190610657565b6040516020818303038152906040528051906020012060001c905060008060008381526020019081526020016000209050600081600001601c9054906101000a900463ffffffff1663ffffffff1614610155576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161014c9061068e565b60405180910390fd5b838160000160006101000a81548167ffffffffffffffff021916908367ffffffffffffffff160217905550828160000160086101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055504281600001601c6101000a81548163ffffffff021916908363ffffffff1602179055507fed7e2a3180a53ccf8540bf67f1679359557837b4c2350f3c7f1d1409eaac3739828a8a338b8b8b8b8b604051610225999897969594939291906106ae565b60405180910390a1505050505050505050565b50505050505050565b60006020528060005260406000206000915090508060000160009054906101000a900467ffffffffffffffff16908060000160089054906101000a900473ffffffffffffffffffffffffffffffffffffffff169080600001601c9054906101000a900463ffffffff16905083565b60006102c26102bd84610791565b61076c565b9050828152602081018484840111156102da57600080fd5b6102e5848285610887565b509392505050565b6000813590506102fc81610930565b92915050565b60008135905061031181610947565b92915050565b6000813590506103268161095e565b92915050565b60008083601f84011261033e57600080fd5b8235905067ffffffffffffffff81111561035757600080fd5b60208301915083600182028301111561036f57600080fd5b9250929050565b600082601f83011261038757600080fd5b81356103978482602086016102af565b91505092915050565b6000813590506103af81610975565b92915050565b6000813590506103c48161098c565b92915050565b6000813590506103d9816109a3565b92915050565b600080600080600080600060c0888a0312156103fa57600080fd5b60006104088a828b01610302565b97505060206104198a828b016103ca565b965050604061042a8a828b016103b5565b955050606088013567ffffffffffffffff81111561044757600080fd5b6104538a828b0161032c565b945094505060806104668a828b016103b5565b92505060a06104778a828b016102ed565b91505092959891949750929550565b600080600080600080600060e0888a0312156104a157600080fd5b60006104af8a828b01610317565b975050602088013567ffffffffffffffff8111156104cc57600080fd5b6104d88a828b01610376565b965050604088013567ffffffffffffffff8111156104f557600080fd5b6105018a828b01610376565b95505060606105128a828b01610317565b945050608088013567ffffffffffffffff81111561052f57600080fd5b61053b8a828b01610376565b93505060a061054c8a828b016103a0565b92505060c088013567ffffffffffffffff81111561056957600080fd5b6105758a828b01610376565b91505092959891949750929550565b60006020828403121561059657600080fd5b60006105a4848285016103a0565b91505092915050565b6105b6816107e4565b82525050565b6105c5816107f6565b82525050565b60006105d783856107c2565b93506105e4838584610887565b6105ed836108f6565b840190509392505050565b60006106056018836107d3565b915061061082610907565b602082019050919050565b6106248161084c565b82525050565b61063381610856565b82525050565b61064281610866565b82525050565b6106518161087a565b82525050565b600060608201905061066c60008301866105bc565b6106796020830185610648565b61068660408301846105ad565b949350505050565b600060208201905081810360008301526106a7816105f8565b9050919050565b6000610100820190506106c4600083018c61061b565b6106d1602083018b6105bc565b6106de604083018a610648565b6106eb60608301896105ad565b6106f86080830188610639565b81810360a083015261070b8186886105cb565b905061071a60c0830185610639565b61072760e08301846105ad565b9a9950505050505050505050565b600060608201905061074a6000830186610639565b61075760208301856105ad565b610764604083018461062a565b949350505050565b6000610776610787565b90506107828282610896565b919050565b6000604051905090565b600067ffffffffffffffff8211156107ac576107ab6108c7565b5b6107b5826108f6565b9050602081019050919050565b600082825260208201905092915050565b600082825260208201905092915050565b60006107ef8261082c565b9050919050565b6000819050919050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600063ffffffff82169050919050565b600067ffffffffffffffff82169050919050565b600060ff82169050919050565b82818337600083830152505050565b61089f826108f6565b810181811067ffffffffffffffff821117156108be576108bd6108c7565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f8301169050919050565b7f4465706f73697420616c72656164792072657665616c65640000000000000000600082015250565b610939816107e4565b811461094457600080fd5b50565b610950816107f6565b811461095b57600080fd5b50565b61096781610800565b811461097257600080fd5b50565b61097e8161084c565b811461098957600080fd5b50565b61099581610866565b81146109a057600080fd5b50565b6109ac8161087a565b81146109b757600080fd5b5056fea2646970667358221220bca337d7cb05b6b689ca49ecdd85c75dc9cdde9d5b5a54318aba5449dda2e35064736f6c63430008040033",
174
+ "linkReferences": {},
175
+ "deployedLinkReferences": {}
176
+ }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/2f8e64c05fa8c4f2f2c708eabb3c81a3.json"
3
+ "buildInfo": "../../../build-info/5dcb7090cfa3ef21a6b6fbb4b3b3e6b4.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/2f8e64c05fa8c4f2f2c708eabb3c81a3.json"
3
+ "buildInfo": "../../../build-info/5dcb7090cfa3ef21a6b6fbb4b3b3e6b4.json"
4
4
  }
@@ -0,0 +1,161 @@
1
+ pragma solidity 0.8.4;
2
+
3
+ /// @title BTC Bridge
4
+ /// @notice Bridge manages BTC deposit and redemption and is increasing and
5
+ /// decreasing balances in the Bank as a result of BTC deposit and
6
+ /// redemption operations.
7
+ ///
8
+ /// Depositors send BTC funds to the most-recently-created-wallet of the
9
+ /// bridge using pay-to-script-hash (P2SH) which contains hashed
10
+ /// information about the depositor’s minting Ethereum address. Then,
11
+ /// the depositor reveals their desired Ethereum minting address to the
12
+ /// Ethereum chain. The Bridge listens for these sorts of messages and
13
+ /// when it gets one, it checks the Bitcoin network to make sure the
14
+ /// funds line up. If they do, the off-chain wallet may decide to pick
15
+ /// this transaction for sweeping, and when the sweep operation is
16
+ /// confirmed on the Bitcoin network, the wallet informs the Bridge
17
+ /// about the sweep increasing appropriate balances in the Bank.
18
+ /// @dev Bridge is an upgradeable component of the Bank.
19
+ contract Bridge {
20
+ struct DepositInfo {
21
+ uint64 amount;
22
+ address vault;
23
+ uint32 revealedAt;
24
+ }
25
+
26
+ /// @notice Collection of all unswept deposits indexed by
27
+ /// keccak256(fundingTxHash | fundingOutputIndex | depositorAddress).
28
+ /// This mapping may contain valid and invalid deposits and the
29
+ /// wallet is responsible for validating them before attempting to
30
+ /// execute a sweep.
31
+ mapping(uint256 => DepositInfo) public unswept;
32
+
33
+ event DepositRevealed(
34
+ uint256 depositId,
35
+ bytes32 fundingTxHash,
36
+ uint8 fundingOutputIndex,
37
+ address depositor,
38
+ uint64 blindingFactor,
39
+ bytes refundPubKey,
40
+ uint64 amount,
41
+ address vault
42
+ );
43
+
44
+ /// @notice Used by the depositor to reveal information about their P2SH
45
+ /// Bitcoin deposit to the Bridge on Ethereum chain. The off-chain
46
+ /// wallet listens for revealed deposit events and may decide to
47
+ /// include the revealed deposit in the next executed sweep.
48
+ /// Information about the Bitcoin deposit can be revealed before or
49
+ /// after the Bitcoin transaction with P2SH deposit is mined on the
50
+ /// Bitcoin chain.
51
+ /// @param fundingTxHash The BTC transaction hash containing BTC P2SH
52
+ /// deposit funding transaction
53
+ /// @param fundingOutputIndex The index of the transaction output in the
54
+ /// funding TX with P2SH deposit, max 256
55
+ /// @param blindingFactor The blinding factor used in the BTC P2SH deposit,
56
+ /// max 2^64
57
+ /// @param refundPubKey The refund pub key used in the BTC P2SH deposit
58
+ /// @param amount The amount locked in the BTC P2SH deposit
59
+ /// @param vault Bank vault to which the swept deposit should be routed
60
+ /// @dev Requirements:
61
+ /// - `msg.sender` must be the Ethereum address used in the P2SH BTC deposit,
62
+ /// - `blindingFactor` must be the blinding factor used in the P2SH BTC deposit,
63
+ /// - `refundPubKey` must be the refund pub key used in the P2SH BTC deposit,
64
+ /// - `amount` must be the same as locked in the P2SH BTC deposit,
65
+ /// - BTC deposit for the given `fundingTxHash`, `fundingOutputIndex`
66
+ /// can be revealed by `msg.sender` only one time.
67
+ ///
68
+ /// If any of these requirements is not met, the wallet _must_ refuse
69
+ /// to sweep the deposit and the depositor has to wait until the
70
+ /// deposit script unlocks to receive their BTC back.
71
+ function revealDeposit(
72
+ bytes32 fundingTxHash,
73
+ uint8 fundingOutputIndex,
74
+ uint64 blindingFactor,
75
+ bytes calldata refundPubKey,
76
+ uint64 amount,
77
+ address vault
78
+ ) external {
79
+ uint256 depositId =
80
+ uint256(
81
+ keccak256(
82
+ abi.encode(fundingTxHash, fundingOutputIndex, msg.sender)
83
+ )
84
+ );
85
+
86
+ DepositInfo storage deposit = unswept[depositId];
87
+ require(deposit.revealedAt == 0, "Deposit already revealed");
88
+
89
+ deposit.amount = amount;
90
+ deposit.vault = vault;
91
+ /* solhint-disable-next-line not-rely-on-time */
92
+ deposit.revealedAt = uint32(block.timestamp);
93
+
94
+ emit DepositRevealed(
95
+ depositId,
96
+ fundingTxHash,
97
+ fundingOutputIndex,
98
+ msg.sender,
99
+ blindingFactor,
100
+ refundPubKey,
101
+ amount,
102
+ vault
103
+ );
104
+ }
105
+
106
+ /// @notice Used by the wallet to prove the BTC deposit sweep transaction
107
+ /// and to update Bank balances accordingly. Sweep is only accepted
108
+ /// if it satisfies SPV proof.
109
+ ///
110
+ /// The function is performing Bank balance updates by first
111
+ /// computing the Bitcoin fee for the sweep transaction. The fee is
112
+ /// divided evenly between all swept deposits. Each depositor
113
+ /// receives a balance in the bank equal to the amount they have
114
+ /// declared during the reveal transaction, minus their fee share.
115
+ ///
116
+ /// It is possible to prove the given sweep only one time.
117
+ /// @param txVersion Transaction version number (4-byte LE)
118
+ /// @param txInputVector All transaction inputs prepended by the number of
119
+ /// inputs encoded as a VarInt, max 0xFC(252) inputs
120
+ /// @param txOutput Single sweep transaction output
121
+ /// @param txLocktime Final 4 bytes of the transaction
122
+ /// @param merkleProof The merkle proof of transaction inclusion in a block
123
+ /// @param txIndexInBlock Transaction index in the block (0-indexed)
124
+ /// @param bitcoinHeaders Single bytestring of 80-byte bitcoin headers,
125
+ /// lowest height first
126
+ function sweep(
127
+ bytes4 txVersion,
128
+ bytes memory txInputVector,
129
+ bytes memory txOutput,
130
+ bytes4 txLocktime,
131
+ bytes memory merkleProof,
132
+ uint256 txIndexInBlock,
133
+ bytes memory bitcoinHeaders
134
+ ) external {
135
+ // TODO We need to read `fundingTxHash`, `fundingOutputIndex` and
136
+ // P2SH script depositor address from `txInputVector`.
137
+ // We then hash them to obtain deposit identifier and read
138
+ // DepositInfo. From DepositInfo we know what amount was declared
139
+ // by the depositor in their reveal transaction and we use that
140
+ // amount to update their Bank balance, minus fee.
141
+ //
142
+ // TODO We need to validate if the sum in the output minus the
143
+ // amount from the previous wallet balance input minus fees is
144
+ // equal to the amount by which Bank balances were increased.
145
+ //
146
+ // TODO We need to validate txOutput to see if the balance was not
147
+ // transferred away from the wallet before increasing balances in
148
+ // the bank.
149
+ //
150
+ // TODO Delete deposit from unswept mapping or mark it as swept
151
+ // depending on the gas costs. Alternativly, do not allow to
152
+ // use the same TX input vector twice. Sweep should be provable
153
+ // only one time.
154
+ }
155
+
156
+ // TODO It is possible a malicious wallet can sweep deposits that can not
157
+ // be later proved on Ethereum. For example, a deposit with
158
+ // an incorrect amount revealed. We need to provide a function for honest
159
+ // depositors, next to sweep, to prove their swept balances on Ethereum
160
+ // selectively, based on deposits they have earlier received.
161
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keep-network/tbtc-v2",
3
- "version": "0.1.1-dev.1+main.89acc9dc2012399923fc12d84df7c7c061940960",
3
+ "version": "0.1.1-dev.2+main.cd434b847d1fa33a36db2bf535bafa0dd706a781",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "artifacts/",