Skip to content

Commit

Permalink
Add missing binding methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tmalahie committed Oct 7, 2024
1 parent c02320d commit 94857e3
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
60 changes: 60 additions & 0 deletions bindings/c-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,35 @@ pub extern "C" fn rgblib_generate_keys(bitcoin_network: *const c_char) -> CResul
generate_keys(bitcoin_network).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_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 +194,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 Down Expand Up @@ -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_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 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 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

0 comments on commit 94857e3

Please sign in to comment.