Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitcoin Bridge Smart Contract Implementation #1

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

peter-curl
Copy link
Owner

Overview

This pull request introduces a comprehensive Bitcoin Bridge Smart Contract for the Stacks blockchain, enabling secure wrapping of Bitcoin tokens with robust security and management features.

Key Features

  • Bidirectional Bitcoin token wrapping
  • Advanced security mechanisms
  • Comprehensive error handling
  • Oracle-based transaction validation
  • Flexible bridge configuration

Implemented Functionalities

Security and Access Control

  • Owner-based administration
  • Oracle management system
  • Recipient address whitelisting
  • Bridge pausability mechanism

Transaction Management

  • Bitcoin deposit with fee calculation
  • Wrapped Bitcoin (wBTC) token minting
  • Transaction replay prevention
  • Maximum deposit limit enforcement

Detailed Changes

Smart Contract Components

  • Error constants for comprehensive scenario handling
  • Configurable bridge parameters
  • Authorized oracles tracking
  • Processed transaction tracking
  • User balance management

Key Functions

  • deposit-bitcoin: Secure Bitcoin deposit and wBTC minting
  • validate-bitcoin-transaction: Oracle-based transaction validation
  • add-oracle / remove-oracle: Dynamic oracle management
  • pause-bridge / unpause-bridge: Emergency control mechanism
  • update-bridge-fee: Flexible fee configuration

Security Considerations

  • Owner-only administrative functions
  • Input validation at multiple stages
  • Oracle-based transaction verification
  • Whitelisting mechanism
  • Duplicate transaction prevention

Error Handling

Implemented granular error codes covering:

  • Authorization failures
  • Invalid transaction amounts
  • Insufficient balances
  • Bridge paused states
  • Transaction processing issues

Potential Improvements

  • Enhanced Oracle validation
  • Multi-signature support
  • Comprehensive withdrawal mechanisms

Testing Recommendations

  • Comprehensive unit testing
  • Security audit
  • Simulated transaction scenarios
  • Edge case validation

Dependencies

  • Stacks blockchain
  • Authorized Oracle infrastructure

Checklist

  • Implement core bridge functionality
  • Add comprehensive error handling
  • Create security mechanisms
  • Conduct thorough testing
  • Perform security audit

- Implemented a smart contract for bridging Bitcoin to a wrapped token (wBTC) on the Stacks blockchain.
- Added error constants for various failure scenarios including authorization, invalid amounts, insufficient balance, paused bridge, duplicate transactions, and oracle validation failures.
- Defined storage variables for bridge configuration and state management.
- Implemented oracle management functions to add and remove authorized oracles.
- Added pausing mechanism for emergency scenarios.
- Implemented bridge fee management functionality.
- Created functions for depositing Bitcoin with oracle validation and minting wBTC.
- Added withdrawal function for burning wBTC and releasing Bitcoin.
- Included read-only functions for bridge transparency and user balance checks.
- Defined `bridge-owner` to store the principal of the bridge owner.
- Added `is-bridge-paused` to track the paused state of the bridge.
- Introduced `total-locked-bitcoin` to keep track of the total amount of Bitcoin locked in the bridge.
- Implemented `bridge-fee-percentage` to store the bridge fee percentage, initialized to 0.1%.
- Defined `authorized-oracles` map to store the principals of authorized oracles.
- Added `processed-transactions` map to track processed Bitcoin transactions using their transaction hashes.
- Defined `wrapped-bitcoin` as a fungible token to represent wrapped Bitcoin (wBTC).
- Added `user-balances` map to track the balance of wBTC for each user.
- Implemented a read-only function `is-bridge-owner` to check if the given sender is the bridge owner.
- Implemented `add-oracle` function to add a new oracle, ensuring only the bridge owner can perform this action.
- Implemented `remove-oracle` function to remove an oracle, ensuring only the bridge owner can perform this action.
- Implemented `pause-bridge` function to pause the bridge, ensuring only the bridge owner can perform this action.
- Implemented `unpause-bridge` function to unpause the bridge, ensuring only the bridge owner can perform this action.
- Implemented `update-bridge-fee` function to update the bridge fee percentage, ensuring only the bridge owner can perform this action.
- Added validation to ensure the new fee is less than 100%.
- Implemented `get-user-balance-amount` function to retrieve the balance of a user from the `user-balances` map.
- Returns the balance amount if it exists, otherwise returns 0.
- Implemented `deposit-bitcoin` function to handle Bitcoin deposits and mint wrapped Bitcoin tokens (wBTC).
- Calculates the bridge fee and net amount to be minted.
- Ensures the bridge is not paused before proceeding.
- Validates that the Bitcoin transaction has not been processed before.
- Simulates oracle validation for the Bitcoin transaction.
- Mints the net amount of wBTC to the recipient.
- Marks the Bitcoin transaction as processed.
- Updates the total locked Bitcoin amount.
- Implemented `withdraw-bitcoin` function to handle the withdrawal process by burning wrapped Bitcoin tokens (wBTC) and releasing the equivalent Bitcoin.
- Calculates the bridge fee and net amount to be released.
- Ensures the bridge is not paused before proceeding.
- Validates that the user has sufficient balance to withdraw the specified amount.
- Burns the specified amount of wBTC from the user's balance.
- Reduces the total locked Bitcoin amount accordingly.
- Updates the user's balance in the `user-balances` map.
- Implemented `validate-bitcoin-transaction` function to validate Bitcoin transactions using oracles.
- Checks if the caller is an authorized oracle.
- Placeholder for additional validation logic, such as checking the Bitcoin blockchain for transaction confirmation.
- Implemented `check-is-bridge-owner` function to verify if the transaction sender is the bridge owner.
- Ensures that only the bridge owner can perform certain actions by asserting the sender's identity.
- Implemented `get-total-locked-bitcoin` function to retrieve the total amount of Bitcoin locked in the bridge.
- Implemented `get-user-balance` function to retrieve the wBTC balance of a specified user.
- Implemented `is-oracle-authorized` function to check if a specified oracle is authorized.
- Provided an overview of the Bitcoin Bridge Smart Contract, highlighting its purpose and core functionality.
- Detailed the main features, including Bitcoin deposits, fee management, oracle-based validation, and recipient whitelisting.
- Explained advanced security mechanisms such as bridge pausability, maximum deposit limit, transaction duplicate prevention, and extensive error handling.
- Listed key contract components, including error constants and data structures.
- Described main functions for deposit functionality, oracle and access management, and bridge administration.
- Outlined security considerations, including owner-only administration functions, comprehensive input validation, oracle-based transaction verification, whitelisting mechanism, and transaction replay protection.
- Included read-only functions for bridge transparency.
- Provided error handling details with specific error codes.
- Added a usage example for depositing Bitcoin.
- Mentioned configuration options and potential improvements.
- Listed dependencies required for the contract.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant