Accounts and Addresses
In Rooch, there are two types of accounts: external accounts and contract accounts. External accounts are controlled by Bitcoin private keys, while contract accounts are controlled by contract code. To standardize address formats, Rooch maps various Bitcoin addresses to a unified address using a hash function. The addresses of contract accounts are generated by an ID generation algorithm within the contract.
The address generation process is illustrated as follows:
Address Format
A Rooch address is a 32-byte array that can be represented as a hexadecimal string starting with 0x
or encoded using bech32. For end-user scenarios, we recommend using bech32 encoding because it is more user-friendly and helps prevent input errors.
Here are some address examples:
Bitcoin: bc1q262qeyyhdakrje5qaux8m2a3r4z8sw8vu5mysh
Rooch bech32: rooch10lnft7hhq37vl0y97lwvkmzqt48fk76y0z88rfcu8zg6qm8qegfqx0rq2h
Rooch hex: 0x7fe695faf7047ccfbc85f7dccb6c405d4e9b7b44788e71a71c3891a06ce0ca12
Reserved Address Space
Rooch reserves address space for the Move standard library and the framework to facilitate developer use.
- Move Standard Library Address:
0x1
- MoveOS Library Address:
0x2
- RoochFramework Address:
0x3
- BitcoinMove Address:
0x4
Note: In developer scenarios, such as in Move code and Move.toml configurations, addresses need to be represented as hexadecimal strings starting with
0x
.
FAQ
Are there differences between testnet and mainnet addresses?
Bitcoin testnet and mainnet addresses are different, but once mapped to Rooch addresses, they are the same.
How do I convert a Bitcoin address to a Rooch address?
A Bitcoin address can be directly converted to a Rooch address via code without the need for a lookup. Refer to the specific SDK for your programming language. Below is a Rust example:
/// Convert the Bitcoin address to a Rooch address
pub fn to_rooch_address(&self) -> RoochAddress {
let mut hasher = Blake2b256::default();
hasher.update(&self.bytes);
let g_arr = hasher.finalize();
RoochAddress(H256(g_arr.digest))
}
How do I find the corresponding Bitcoin address from a Rooch address?
Not all Rooch addresses have corresponding Bitcoin addresses. To retrieve them, use the rooch_framework::address_mapping::resolve_bitcoin
method.
/// Resolve a Rooch address to a Bitcoin address
public fun resolve_bitcoin(rooch_address: address): Option<BitcoinAddress> {
let am = Self::borrow_rooch_to_bitcoin();
Self::resolve_bitcoin_address(am, rooch_address)
}
What type of address should be used when calling RPC?
Rooch's RPC and command-line tool address parameters support both Bitcoin addresses and Rooch addresses. Developers can choose based on their needs.
How to Create a Contract Account?
To create a contract account, call the moveos_std::account::create_account
method within the contract. This method returns an Object<Account>
. Developers can store this object in their data structure and use it to operate the contract account.
/// Create an Account Object with a generated address
public fun create_account(): Object<Account> {
let new_address = tx_context::fresh_address();
create_account_object(new_address)
}