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

Add missing binding methods #49

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions bindings/c-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ pub extern "C" fn rgblib_get_address(wallet: &COpaqueStruct) -> CResultString {
get_address(wallet).into()
}

#[no_mangle]
pub extern "C" fn rgblib_get_asset_balance(
wallet: &COpaqueStruct,
asset_id: *const c_char,
) -> CResultString {
get_asset_balance(wallet, asset_id).into()
}

#[no_mangle]
pub extern "C" fn rgblib_get_btc_balance(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
) -> CResultString {
get_btc_balance(wallet, online).into()
}

#[no_mangle]
pub extern "C" fn rgblib_go_online(
wallet: &COpaqueStruct,
Expand Down Expand Up @@ -170,6 +186,31 @@ pub extern "C" fn rgblib_list_assets(
list_assets(wallet, filter_asset_schemas).into()
}

#[no_mangle]
pub extern "C" fn rgblib_list_transactions(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
) -> CResultString {
list_transactions(wallet, online).into()
}

#[no_mangle]
pub extern "C" fn rgblib_list_transfers(
wallet: &COpaqueStruct,
asset_id: *const c_char,
) -> CResultString {
list_transfers(wallet, asset_id).into()
}

#[no_mangle]
pub extern "C" fn rgblib_list_unspents(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
settled_only: bool,
) -> CResultString {
list_unspents(wallet, online, settled_only).into()
}

#[no_mangle]
pub extern "C" fn rgblib_new_wallet(wallet_data: *const c_char) -> CResult {
new_wallet(wallet_data).into()
Expand All @@ -185,6 +226,14 @@ pub extern "C" fn rgblib_refresh(
refresh(wallet, online, asset_id_opt, filter).into()
}

#[no_mangle]
pub extern "C" fn rgblib_restore_keys(
bitcoin_network: *const c_char,
mnemonic: *const c_char,
) -> CResultString {
restore_keys(bitcoin_network, mnemonic).into()
}

#[no_mangle]
pub extern "C" fn rgblib_send(
wallet: &COpaqueStruct,
Expand All @@ -205,6 +254,17 @@ pub extern "C" fn rgblib_send(
.into()
}

#[no_mangle]
pub extern "C" fn rgblib_send_btc(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
address: *const c_char,
amount: u64,
fee_rate: c_float,
) -> CResultString {
send_btc(wallet, online, address, amount, fee_rate).into()
}

#[no_mangle]
pub extern "C" fn rgblib_witness_receive(
wallet: &COpaqueStruct,
Expand Down
75 changes: 75 additions & 0 deletions bindings/c-ffi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ pub(crate) fn get_address(wallet: &COpaqueStruct) -> Result<String, Error> {
Ok(wallet.get_address()?)
}

pub(crate) fn get_asset_balance(
wallet: &COpaqueStruct,
asset_id: *const c_char,
) -> Result<String, Error> {
let wallet = Wallet::from_opaque(wallet)?;
let asset_id = ptr_to_string(asset_id);
let res = wallet.get_asset_balance(asset_id)?;
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn get_btc_balance(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
) -> Result<String, Error> {
let wallet = Wallet::from_opaque(wallet)?;
let online = Online::from_opaque(online)?;
let res = wallet.get_btc_balance((*online).clone())?;
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn go_online(
wallet: &COpaqueStruct,
skip_consistency_check: bool,
Expand Down Expand Up @@ -264,6 +284,37 @@ pub(crate) fn list_assets(
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn list_transactions(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
) -> Result<String, Error> {
let wallet = Wallet::from_opaque(wallet)?;
let online = Online::from_opaque(online)?;
let res = wallet.list_transactions(Some((*online).clone()))?;
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn list_transfers(
wallet: &COpaqueStruct,
asset_id: *const c_char,
) -> Result<String, Error> {
let wallet = Wallet::from_opaque(wallet)?;
let asset_id = convert_optional_string(asset_id);
let res = wallet.list_transfers(asset_id)?;
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn list_unspents(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
settled_only: bool,
) -> Result<String, Error> {
let wallet = Wallet::from_opaque(wallet)?;
let online = Online::from_opaque(online)?;
let res = wallet.list_unspents(Some((*online).clone()), settled_only)?;
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn new_wallet(wallet_data: *const c_char) -> Result<Wallet, Error> {
let wallet_data: WalletData = serde_json::from_str(&ptr_to_string(wallet_data))?;
Ok(Wallet::new(wallet_data)?)
Expand All @@ -283,6 +334,16 @@ pub(crate) fn refresh(
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn restore_keys(
bitcoin_network: *const c_char,
mnemonic: *const c_char,
) -> Result<String, Error> {
let bitcoin_network = BitcoinNetwork::from_str(&ptr_to_string(bitcoin_network))?;
let mnemonic = ptr_to_string(mnemonic);
let res = rgb_lib::restore_keys(bitcoin_network.into(), mnemonic)?;
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn send(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
Expand All @@ -305,6 +366,20 @@ pub(crate) fn send(
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn send_btc(
wallet: &COpaqueStruct,
online: &COpaqueStruct,
address: *const c_char,
amount: u64,
fee_rate: c_float,
) -> Result<String, Error> {
let wallet = Wallet::from_opaque(wallet)?;
let online = Online::from_opaque(online)?;
let address = ptr_to_string(address);
let res = wallet.send_btc((*online).clone(), address, amount, fee_rate)?;
Ok(res)
}

pub(crate) fn witness_receive(
wallet: &COpaqueStruct,
asset_id_opt: *const c_char,
Expand Down
Loading