bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 36쪽 중 9쪽
ucted from the caller's allowance unless the allowance was made for `type(uint256).max`.
Requirements:
* `recipient` cannot be the zero address,
* `spender` must have a balance of at least `amount`,
* the caller must have an allowance for `spender`'s balance of at least `amount`.
#### Parameters
| Name | Type | Description |
| --------- | ------- | -------------------------------------------------- |
| spender | address | The address from which the balance is transferred. |
| recipient | address | The address to which the balance is transferred. |
| amount | uint256 | The amount of balance that is transferred. |
### permit
```solidity
function permit(address owner, address spender, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external
```
An EIP2612 approval made with secp256k1 signature. Users can authorize a transfer of their balance with a signature conforming to the EIP712 standard, rather than an on-chain transaction from their address. Anyone can submit this signature on the user's behalf by calling the `permit` function, paying gas fees, and possibly performing other actions in the same transaction.
The deadline argument can be set to `type(uint256).max to create permits that effectively never expire. If the` amount`is set to`type(uint256).max`then`transferBalanceFrom`will not reduce an allowance. Beware that changing an allowance with this function brings the risk that someone may use both the old and the new allowance by unfortunate transaction ordering. Please use`increaseBalanceAllowance`and`decreaseBalanceAllowance\` to eliminate the risk.
#### Parameters
| Name | Type | Description |
| -------- | ------- | ------------------------------------------------------ |
| owner | address | The balance owner who signed the permission. |
| spender | address | The address that will be allowed to spend the balance. |
| amount | uint256 | The amount the spender is allowed to spend. |
| deadline | uint256 | The UNIX time until which the permit is valid. |
| v | uint8 | V part of the permit signature. |
| r | bytes32 | R part of the permit signature. |
| s | bytes32 | S part of the permit signature. |
### increaseBalances
```solidity
function increaseBalances(address[] recipients, uint256[] amounts) external
```
Increases balances of the provided `recipients` by the provided `amounts`. Can only be called by the Bridge.
Requirements:
* length of `recipients` and `amounts` must be the same,
* none of `recipients` addresses must point to the Bank.
#### Parameters
| Name | Type | Description |
| ---------- | ---------- | ---------------------------------------- |
| recipients | address\[] | Balance increase recipients. |
| amounts | uint256\[] | Amounts by which balances are increased. |
### increaseBalance
```solidity
function increaseBalance(address recipient, uint256 amount) external
```
Increases balance of the provided `recipient` by the provided `amount`. Can only be called by the Bridge.
Requirements:
* `recipient` address must not point to the Bank.
#### Parameters
| Name | Type | Description |
| --------- | ------- | ----------------------------------------- |
| recipient | address | Balance increase recipient. |
| amount | uint256 | Amount by which the balance is increased. |
### increaseBalanceAndCall
```solidity
function increaseBalanceAndCall(address vault, address[] recipients, uint256[] amounts) external
```
Increases the given smart contract `vault`'s balance and notifies the `vault` contract about it. Can be called only by the Bridge.
Requirements:
* `vault` must implement `IVault` interface,
* length of `recipients` and `amounts` must be the same.
#### Parameters
| Name | Type | Description |
| ---------- | ---------- | ---------------------------------------- |
| vault | address | Address of `IVault` recipient contract. |
| recipients | address\[] | Balance increase recipients. |
| amounts | uint256\[] | Amounts by which balances are increased. |
### decreaseBalance
```solidity
function decreaseBalance(uint256 amount) external
```
Decreases caller's balance by the provided `amount`. There is no way to restore the balance so do not call this function unless you really know what you are doing!
Requirements:
* The caller must have a balance of at least `amount`.
#### Parameters
| Name | Type | Description |
| ------ | ------- | --------------------------------------------- |
| amount | uint256 | The amount by which the balance is decreased. |
### DOMAIN\_SEPARATOR
```solidity
function DOMAIN_SEPARATOR() public view returns (bytes32)
```
Returns hash of EIP712 Domain struct with `TBTC Bank` as a signing domain and Bank contract as a verifying contract. Used to construct an EIP2612 signature provided to the `permit` function.
### \_increaseBalance
```solidity
function _increaseBalance(address recipient, uint256 amount) internal
```
# BitcoinTx
## BitcoinTx
Allows to reference Bitcoin raw transaction in Solidity.
See <https://developer.bitcoin.org/reference/transactions.html#raw-transaction-format>
Raw Bitcoin transaction data:
| Bytes | Name | BTC type | Description |
| ------ | -------------- | --------------------- | ------------------------- |
| 4 | version | int32\_t (LE) | TX version number |
| varies | tx\_in\_count | compactSize uint (LE) | Number of TX inputs |
| varies | tx\_in | txIn\[] | TX inputs |
| varies | tx\_out\_count | compactSize uint (LE) | Number of TX outputs |
| varies | tx\_out | txOut\[] | TX outputs |
| 4 | lock\_time | uint32\_t (LE) | Unix time or block number |
Non-coinbase transaction input (txIn):
| Bytes | Name | BTC type | Description |
| ------ | ----------------- | --------------------- | ------------------------------------------- |
| 36 | previous\_output | outpoint | The previous outpoint being spent |
| varies | script\_bytes | compactSize uint (LE) | The number of bytes in the signature script |
| varies | signature\_script | char\[] | The signature script, empty for P2WSH |
| 4 | sequence | uint32\_t (LE) | Sequence number |
The reference to transaction being spent (outpoint):
| Bytes | Name | BTC type | Description |
| ----- | ----- | -------------- | ---------------------------------------- |
| 32 | hash | char\[32] | Hash of the transaction to spend |
| 4 | index | uint32\_t (LE) | Index of the specific output from the TX |
Transaction output (txOut):
| Bytes | Name | BTC type | Description |
| ------ | ----------------- | --------------------- | ------------------------------------ |
| 8 | value | int64\_t (LE) | Number of satoshis to spend |
| 1+ | pk\_script\_bytes | compactSize uint (LE) | Number of bytes in the pubkey script |
| varies | pk\_script | char\[] | Pubkey script |
compactSize uint format:
| Value | Bytes | Format |
| --------------------------------------- | ----- | --------------------------------------------- |
| >= 0 && <= 252 | 1 | uint8\_t |
| >= 253 && <= 0xffff | 3 | 0xfd followed by the number as uint16\_t (LE) |
| >= 0x10000 && <= 0xffffffff | 5 | 0xfe followed by the number as uint32\_t (LE) |
| >= 0x100000000 && <= 0xffffffffffffffff | 9 | 0xff followed by the number as uint64\_t (LE) |
(\*) compactSize uint is often references as VarInt)
Coinbase transaction input (txIn):
| Bytes | Name | BTC type | Description |
| ------ | ---------------- | --------------------- | ------------------------------------------- |
| 32 | hash | char\[32] | A 32-byte 0x0 null (no previous\_outpoint) |
| 4 | index | uint32\_t (LE) | 0xffffffff (no previous\_outpoint) |
| varies | script\_bytes | compactSize uint (LE) | The number of bytes in the coinbase script |
| varies | height | char\[] | The block height of this block (BIP34) (\*) |
| varies | coinbase\_script | none | Arbitrary data, max 100 bytes |
| 4 | sequence | uint32\_t (LE) | Sequence number |
(\*) Uses script language: starts with a data-pushing opcode that indicates how many bytes to push to the stack followed by the block height as a little-endian unsigned integer. This script must be as short as possible, otherwise it may be rejected. The data-pushing opcode will be 0x03 and the total size four bytes until block 16,777,216 about 300 years from now.
### Info
```solidity
struct Info {
bytes4 version;
bytes inputVector;
bytes outputVector;
bytes4 locktime;
}
```
### Proof
```solidity
struct Proof {
bytes merkleProof;
uint256 txIndexInBlock;
bytes bitcoinHeaders;
}
```
### UTXO
```solidity
struct UTXO {
bytes32 txHash;
uint32 txOutputIndex;
uint64 txOutputValue;
}
```
### RSVSignature
```solidity
struct RSVSignature {
bytes32 r;
bytes32 s;
uint8 v;
}
```
### validateProof
```solidity
function validateProof(struct BridgeState.Storage self, struct BitcoinTx.Info txInfo, struct BitcoinTx.Proof proof) internal view returns (bytes32 txHash)
```
Validates the SPV proof of the Bitcoin transaction. Reverts in case the validation or proof verification fail.
#### Parameters
| Name | Type | Description |
| ------ | -------------------------- | ------------------------- |
| self | struct BridgeState.Storage | |
| txInfo | struct BitcoinTx.Info | Bitcoin transaction data. |
| proof | struct BitcoinTx.Proof | Bitcoin proof data. |
#### Return Values
| Name | Type | Description |
| ------ | ------- | -------------------------------- |
| txHash | bytes32 | Proven 32-byte transaction hash. |
### evaluateProofDifficulty
```solidity
function evaluateProofDifficulty(struct BridgeState.Storage self, bytes bitcoinHeaders) internal view
```
Evaluates the given Bitcoin proof difficulty against the actual Bitcoin chain difficulty provided by the relay oracle. Reverts in case the evaluation fails.
#### Parameters
| Name | Type | Description |
| -------------- | -------------------------- | ------------------------------------------------------------------------------------------------- |
| self | struct BridgeState.Storage | |
| bitcoinHeaders | bytes | Bitcoin headers chain being part of the SPV proof. Used to extract the observed proof difficulty. |
### extractPubKeyHash
```solidity
function extractPubKeyHash(struct BridgeState.Storage, bytes output) internal pure returns (bytes20 pubKeyHash)
```
Extracts public key hash from the provided P2PKH or P2WPKH output. Reverts if the validation fails.
Requirements:
* The output must be of P2PKH or P2WPKH type and lock the funds on a 20-byte public key hash.
#### Parameters
| Name | Type | Description |
| ------ | -------------------------- | ----------------------- |
| | struct BridgeState.Storage | |
| output | bytes | The transaction output. |
#### Return Values
| Name | Type | Description |
| ---------- | ------- | -------------------------------------------------- |
| pubKeyHash | bytes20 | 20-byte public key hash the output locks funds on. |
### makeP2PKHScript
```solidity
function makeP2PKHScript(bytes20 pubKeyHash) internal pure returns (bytes26)
```
Build the P2PKH script from the given public key hash.
The P2PKH script has the following byte format: <0x1976a914> <20-byte PKH> <0x88ac>. According to <https://en.bitcoin.it/wiki/Script#Opcodes> this translates to:
* 0x19: Byte length of the entire script
* 0x76: OP\_DUP
* 0xa9: OP\_HASH160
* 0x14: Byte length of the public key hash
* 0x88: OP\_EQUALVERIFY
* 0xac: OP\_CHECKSIG which matches the P2PKH structure as per: <https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash>
#### Parameters
| Name | Type | Description |
| ---------- | ------- | ---------------------------- |
| pubKeyHash | bytes20 | The 20-byte public key hash. |
#### Return Values
| Name | Type | Description |
| ---- | ------- | ----------------- |
| \[0] | bytes26 | The P2PKH script. |
### makeP2WPKHScript
```solidity
function makeP2WPKHScript(bytes20 pubKeyHash) internal pure returns (bytes23)
```
Build the P2WPKH script from the given public key hash.
The P2WPKH script has the following format: <0x160014> <20-byte PKH>. According to <https://en.bitcoin.it/wiki/Script#Opcodes> this translates to:
* 0x16: Byte length of the entire script
* 0x00: OP\_0
* 0x14: Byte length of the public key hash which matches the P2WPKH structure as per: <https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH>
#### Parameters
| Name | Type | Description |
| ---------- | ------- | ---------------------------- |
| pubKeyHash | bytes20 | The 20-byte public key hash. |
#### Return Values
| Name | Type | Description |
| ---- | ------- | ------------------ |
| \[0] | bytes23 | The P2WPKH script. |
# Bridge
## Bridge
Bridge manages BTC deposit and redemption flow and is increasing and decreasing balances in the Bank as a result of BTC deposit and redemption operations performed by depositors and redeemers.
Depositors send BTC funds to the most recently created off-chain ECDSA wallet of the bridge using pay-to-script-hash (P2SH) or pay-to-witness-script-hash (P2WSH) containing hashed information about the depositor’s Ethereum address. Then, the depositor reveals their Ethereum address along with their deposit blinding factor, refund public key hash and refund locktime to the Bridge on Ethereum chain. The off-chain ECDSA wallet listens for these sorts of messages and when it gets one, it checks the Bitcoin network to make sure the deposit lines up. If it does, the off-chain ECDSA wallet may decide to pick the deposit transaction for sweeping, and when the sweep operation is confirmed on the Bitcoin network, the ECDSA wallet informs the Bridge about the sweep increasing appropriate balances in the Bank.
Bridge is an upgradeable component of the Bank. The order of functionalities in this contract is: deposit, sweep, redemption, moving funds, wallet lifecycle, frauds, parameters.
### self
```solidity
struct BridgeState.Storage self
```
### DepositRevealed
```solidity
event DepositRevealed(bytes32 fundingTxHash, uint32 fundingOutputIndex, address depositor, uint64 amount, bytes8 blindingFactor, bytes20 walletPubKeyHash, bytes20 refundPubKeyHash, bytes4 refundLocktime, address vault)
```
### DepositsSwept
```solidity
event DepositsSwept(bytes20 walletPubKeyHash, bytes32 sweepTxHash)
```
### RedemptionRequested
```solidity
event RedemptionRequested(bytes20 walletPubKeyHash, bytes redeemerOutputScript, address redeemer, uint64 requestedAmount, uint64 treasuryFee, uint64 txMaxFee)
```
### RedemptionsCompleted
```solidity
event RedemptionsCompleted(bytes20 walletPubKeyHash, bytes32 redemptionTxHash)
```
### RedemptionTimedOut
```solidity
event RedemptionTimedOut(bytes20 walletPubKeyHash, bytes redeemerOutputScript)
```
### WalletMovingFunds
```solidity
event WalletMovingFunds(bytes32 ecdsaWalletID, bytes20 walletPubKeyHash)
```
### MovingFundsCommitmentSubmitted
```solidity
event MovingFundsCommitmentSubmitted(bytes20 walletPubKeyHash, bytes20[] targetWallets, address submitter)
```
### MovingFundsTimeoutReset
```solidity
event MovingFundsTimeoutReset(bytes20 walletPubKeyHash)
```
### MovingFundsCompleted
```solidity
event MovingFundsCompleted(bytes20 walletPubKeyHash, bytes32 movingFundsTxHash)
```
### MovingFundsTimedOut
```solidity
event MovingFundsTimedOut(bytes20 walletPubKeyHash)
```
### MovingFundsBelowDustReported
```solidity
event MovingFundsBelowDustReported(bytes20 walletPubKeyHash)
```
### MovedFundsSwept
```solidity
event MovedFundsSwept(bytes20 walletPubKeyHash, bytes32 sweepTxHash)
```
### MovedFundsSweepTimedOut
```solidity
event MovedFundsSweepTimedOut(bytes20 walletPubKeyHash, bytes32 movingFundsTxHash, uint32 movingFundsTxOutputIndex)
```
### NewWalletRequested
```solidity
event NewWalletRequested()
```
### NewWalletRegistered
```solidity
event NewWalletRegistered(bytes32 ecdsaWalletID, bytes20 walletPubKeyHash)
```
### WalletClosing
```solidity
event WalletClosing(bytes32 ecdsaWalletID, bytes20 walletPubKeyHash)
```
### WalletClosed
```solidity
event WalletClosed(bytes32 ecdsaWalletID, bytes20 walletPubKeyHash)
```
### WalletTerminated
```solidity
event WalletTerminated(bytes32 ecdsaWalletID, bytes20 walletPubKeyHash)
```
### FraudChallengeSubmitted
```solidity
event FraudChallengeSubmitted(bytes20 walletPubKeyHash, bytes32 sighash, uint8 v, bytes32 r, bytes32 s)
```
### FraudChallengeDefeated
```solidity
event FraudChallengeDefeated(bytes20 walletPubKeyHash, bytes32 sighash)
```
### FraudChallengeDefeatTimedOut
```solidity
event FraudChallengeDefeatTimedOut(bytes20 walletPubKeyHash, bytes32 sighash)
```
### VaultStatusUpdated
```solidity
event VaultStatusUpdated(address vault, bool isTrusted)
```
### SpvMaintainerStatusUpdated
```solidity
event SpvMaintainerStatusUpdated(address spvMaintainer, bool isTrusted)
```
### DepositParametersUpdated
```solidity
event DepositParametersUpdated(uint64 depositDustThreshold, uint64 depositTreasuryFeeDivisor, uint64 depositTxMaxFee, uint32 depositRevealAheadPeriod)
```
### RedemptionParametersUpdated
```solidity
event RedemptionParametersUpdated(uint64 redemptionDustThreshold, uint64 redemptionTreasuryFeeDivisor, uint64 redemptionTxMaxFee, uint64 redemptionTxMaxTotalFee, uint32 redemptionTimeout, uint96 redemptionTimeoutSlashingAmount, uint32 redemptionTimeoutNotifierRewardMultiplier)
```
### MovingFundsParametersUpdated
```solidity
event MovingFundsParametersUpdated(uint64 movingFundsTxMaxTotalFee, uint64 movingFundsDustThreshold, uint32 movingFundsTimeoutResetDelay, uint32 movingFundsTimeout, uint96 movingFundsTimeoutSlashingAmount, uint32 movingFundsTimeoutNotifierRewardMultiplier, uint16 movingFundsCommitmentGasOffset, uint64 movedFundsSweepTxMaxTotalFee, uint32 movedFundsSweepTimeout, uint96 movedFundsSweepTimeoutSlashingAmount, uint32 movedFundsSweepTimeoutNotifierRewardMultiplier)
```
### WalletParametersUpdated
```solidity
event WalletParametersUpdated(uint32 walletCreationPeriod, uint64 walletCreationMinBtcBalance, uint64 walletCreationMaxBtcBalance, uint64 walletClosureMinBtcBalance, uint32 walletMaxAge, uint64 walletMaxBtcTransfer, uint32 walletClosingPeriod)
```
### FraudParametersUpdated
```solidity
event FraudParametersUpdated(uint96 fraudChallengeDepositAmount, uint32 fraudChallengeDefeatTimeout, uint96 fraudSlashingAmount, uint32 fraudNotifierRewardMultiplier)
```
### TreasuryUpdated
```solidity
event TreasuryUpdated(address treasury)
```
### onlySpvMaintainer
```solidity
modifier onlySpvMaintainer()
```
### constructor
```solidity
constructor() public
```
### initialize
```solidity
function initialize(address _bank, address _relay, address _treasury, address _ecdsaWalletRegistry, address payable _reimbursementPool, uint96 _txProofDifficultyFactor) external
```
Initializes upgradable contract on deployment.
#### Parameters
| Name | Type | Description |
| ------------------------- | --------------- | ------------------------------------------------------------------------------------------------ |
| \_bank | address | Address of the Bank the Bridge belongs to. |
| \_relay | address | Address of the Bitcoin relay providing the current Bitcoin network difficulty. |
| \_treasury | address | Address where the deposit and redemption treasury fees will be sent to. |
| \_ecdsaWalletRegistry | address | Address of the ECDSA Wallet Registry contract. |
| \_reimbursementPool | address payable | Address of the Reimbursement Pool contract. |
| \_txProofDifficultyFactor | uint96 | The number of confirmations on the Bitcoin chain required to successfully evaluate an SPV proof. |
### revealDeposit
```solidity
function revealDeposit(struct BitcoinTx.Info fundingTx, struct Deposit.DepositRevealInfo reveal) external
```
Used by the depositor to reveal information about their P2(W)SH Bitcoin deposit to the Bridge on Ethereum chain. The off-chain wallet listens for revealed deposit events and may decide to include the revealed deposit in the next executed sweep. Information about the Bitcoin deposit can be revealed before or after the Bitcoin transaction with P2(W)SH deposit is mined on the Bitcoin chai프로젝트로 돌아가기