Skip to content

Commit 203e41e

Browse files
authored
Merge pull request #53 from Pearl-Browser/backup-wallet-bindings
Add bindings to backup & restore wallet
2 parents f04ff8d + d2d1790 commit 203e41e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

bindings/c-ffi/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ pub extern "C" fn free_wallet(obj: COpaqueStruct) {
5454
}
5555
}
5656

57+
#[no_mangle]
58+
pub extern "C" fn rgblib_backup(
59+
wallet: &COpaqueStruct,
60+
backup_path: *const c_char,
61+
password: *const c_char,
62+
) -> CResult {
63+
backup(wallet, backup_path, password).into()
64+
}
65+
66+
#[no_mangle]
67+
pub extern "C" fn rgblib_backup_info(wallet: &COpaqueStruct) -> CResultString {
68+
backup_info(wallet).into()
69+
}
70+
5771
#[no_mangle]
5872
pub extern "C" fn rgblib_blind_receive(
5973
wallet: &COpaqueStruct,
@@ -244,6 +258,15 @@ pub extern "C" fn rgblib_refresh(
244258
refresh(wallet, online, asset_id_opt, filter, skip_sync).into()
245259
}
246260

261+
#[no_mangle]
262+
pub extern "C" fn rgblib_restore_backup(
263+
backup_path: *const c_char,
264+
password: *const c_char,
265+
target_dir: *const c_char,
266+
) -> CResult {
267+
restore_backup(backup_path, password, target_dir).into()
268+
}
269+
247270
#[no_mangle]
248271
pub extern "C" fn rgblib_restore_keys(
249272
bitcoin_network: *const c_char,

bindings/c-ffi/src/utils.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ fn string_to_ptr(other: String) -> *mut c_char {
166166
cstr.into_raw()
167167
}
168168

169+
pub(crate) fn backup(
170+
wallet: &COpaqueStruct,
171+
backup_path: *const c_char,
172+
password: *const c_char,
173+
) -> Result<(), Error> {
174+
let wallet = Wallet::from_opaque(wallet)?;
175+
let backup_path = ptr_to_string(backup_path);
176+
let password = ptr_to_string(password);
177+
wallet.backup(&backup_path, &password)?;
178+
Ok(())
179+
}
180+
181+
pub(crate) fn backup_info(wallet: &COpaqueStruct) -> Result<String, Error> {
182+
let wallet = Wallet::from_opaque(wallet)?;
183+
let res = wallet.backup_info()?;
184+
Ok(serde_json::to_string(&res)?)
185+
}
186+
169187
pub(crate) fn blind_receive(
170188
wallet: &COpaqueStruct,
171189
asset_id_opt: *const c_char,
@@ -404,6 +422,18 @@ pub(crate) fn refresh(
404422
Ok(serde_json::to_string(&res)?)
405423
}
406424

425+
pub(crate) fn restore_backup(
426+
backup_path: *const c_char,
427+
password: *const c_char,
428+
target_dir: *const c_char,
429+
) -> Result<(), Error> {
430+
let backup_path = ptr_to_string(backup_path);
431+
let password = ptr_to_string(password);
432+
let target_dir = ptr_to_string(target_dir);
433+
rgb_lib::restore_backup(&backup_path, &password, &target_dir)?;
434+
Ok(())
435+
}
436+
407437
pub(crate) fn restore_keys(
408438
bitcoin_network: *const c_char,
409439
mnemonic: *const c_char,

0 commit comments

Comments
 (0)