Skip to content

Commit

Permalink
Merge pull request #53 from Pearl-Browser/backup-wallet-bindings
Browse files Browse the repository at this point in the history
Add bindings to backup & restore wallet
  • Loading branch information
zoedberg authored Dec 12, 2024
2 parents f04ff8d + d2d1790 commit 203e41e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bindings/c-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ pub extern "C" fn free_wallet(obj: COpaqueStruct) {
}
}

#[no_mangle]
pub extern "C" fn rgblib_backup(
wallet: &COpaqueStruct,
backup_path: *const c_char,
password: *const c_char,
) -> CResult {
backup(wallet, backup_path, password).into()
}

#[no_mangle]
pub extern "C" fn rgblib_backup_info(wallet: &COpaqueStruct) -> CResultString {
backup_info(wallet).into()
}

#[no_mangle]
pub extern "C" fn rgblib_blind_receive(
wallet: &COpaqueStruct,
Expand Down Expand Up @@ -244,6 +258,15 @@ pub extern "C" fn rgblib_refresh(
refresh(wallet, online, asset_id_opt, filter, skip_sync).into()
}

#[no_mangle]
pub extern "C" fn rgblib_restore_backup(
backup_path: *const c_char,
password: *const c_char,
target_dir: *const c_char,
) -> CResult {
restore_backup(backup_path, password, target_dir).into()
}

#[no_mangle]
pub extern "C" fn rgblib_restore_keys(
bitcoin_network: *const c_char,
Expand Down
30 changes: 30 additions & 0 deletions bindings/c-ffi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ fn string_to_ptr(other: String) -> *mut c_char {
cstr.into_raw()
}

pub(crate) fn backup(
wallet: &COpaqueStruct,
backup_path: *const c_char,
password: *const c_char,
) -> Result<(), Error> {
let wallet = Wallet::from_opaque(wallet)?;
let backup_path = ptr_to_string(backup_path);
let password = ptr_to_string(password);
wallet.backup(&backup_path, &password)?;
Ok(())
}

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

pub(crate) fn blind_receive(
wallet: &COpaqueStruct,
asset_id_opt: *const c_char,
Expand Down Expand Up @@ -404,6 +422,18 @@ pub(crate) fn refresh(
Ok(serde_json::to_string(&res)?)
}

pub(crate) fn restore_backup(
backup_path: *const c_char,
password: *const c_char,
target_dir: *const c_char,
) -> Result<(), Error> {
let backup_path = ptr_to_string(backup_path);
let password = ptr_to_string(password);
let target_dir = ptr_to_string(target_dir);
rgb_lib::restore_backup(&backup_path, &password, &target_dir)?;
Ok(())
}

pub(crate) fn restore_keys(
bitcoin_network: *const c_char,
mnemonic: *const c_char,
Expand Down

0 comments on commit 203e41e

Please sign in to comment.