Skip to content
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
479 changes: 299 additions & 180 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ zip = { version = "2.2.0", default-features = false, features = ["time", "zstd"]
[dev-dependencies]
dircmp = "0.2.0"
electrum-client = "0.20.0"
http = "1.4.0"
lazy_static = { version = "1.5.0", default-features = false }
lightning = { version = "0.2.0", path = "./rust-lightning/lightning", features = ["_rln_test_hooks"] }
once_cell = "1.20.0"
reqwest = { version = "0.12", default-features = false, features = ["json", "multipart", "native-tls", "stream"] }
serial_test = "3.1.1"
Expand All @@ -69,6 +71,7 @@ tracing-test = "0.2.5"
[patch.crates-io]
lightning = { path = "./rust-lightning/lightning" }
lightning-background-processor = { path = "./rust-lightning/lightning-background-processor"}
rgb-lib = { git = "https://github.com/RGB-Tools/rgb-lib", branch = "master" }

[lints.rust.unexpected_cfgs]
level = "allow"
Expand Down
3 changes: 2 additions & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3014,7 +3014,8 @@ components:
- RgbSend
- Drain
- CreateUtxos
- User
- SendBtc
- Incoming
Transfer:
type: object
required:
Expand Down
38 changes: 19 additions & 19 deletions src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub(crate) fn do_backup(
if backup_file.exists() {
Err(APIError::InvalidBackupPath)?;
}
let tmp_base_path = _get_parent_path(backup_file)?;
let files = _get_backup_paths(&tmp_base_path)?;
let tmp_base_path = get_parent_path(backup_file)?;
let files = get_backup_paths(&tmp_base_path)?;
let salt: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(BACKUP_KEY_LENGTH)
Expand All @@ -69,18 +69,18 @@ pub(crate) fn do_backup(

// create zip archive of wallet data
tracing::debug!("\nzipping {:?} to {:?}", &wallet_dir, &files.zip);
_zip_dir(wallet_dir, &files.zip)?;
zip_dir(wallet_dir, &files.zip)?;

// encrypt the backup file
tracing::debug!("\nencrypting {:?} to {:?}", &files.zip, &files.encrypted);
_encrypt_file(&files.zip, &files.encrypted, password, &salt, &nonce)?;
encrypt_file(&files.zip, &files.encrypted, password, &salt, &nonce)?;

// add backup nonce + salt + version to final zip file
write(files.nonce, nonce)?;
write(files.salt, salt)?;
write(files.version, BACKUP_VERSION.to_string())?;
tracing::debug!("\nzipping {:?} to {:?}", &files.tempdir, &backup_file);
_zip_dir(files.tempdir.path(), backup_file)?;
zip_dir(files.tempdir.path(), backup_file)?;

tracing::info!("backup completed");
Ok(())
Expand All @@ -95,13 +95,13 @@ pub(crate) fn restore_backup(
// setup
tracing::info!("starting restore...");
let backup_file = PathBuf::from(backup_path);
let tmp_base_path = _get_parent_path(&backup_file)?;
let files = _get_backup_paths(&tmp_base_path)?;
let tmp_base_path = get_parent_path(&backup_file)?;
let files = get_backup_paths(&tmp_base_path)?;
let target_dir_path = PathBuf::from(&target_dir);

// unpack given zip file and retrieve backup data
tracing::info!("unzipping {:?}", backup_file);
_unzip(&backup_file, &PathBuf::from(files.tempdir.path()))?;
unzip(&backup_file, &PathBuf::from(files.tempdir.path()))?;
let nonce = read_to_string(files.nonce)?;
tracing::debug!("using retrieved nonce: {}", &nonce);
let salt = read_to_string(files.salt)?;
Expand All @@ -118,15 +118,15 @@ pub(crate) fn restore_backup(

// decrypt backup and restore files
tracing::info!("decrypting {:?} to {:?}", files.encrypted, files.zip);
_decrypt_file(&files.encrypted, &files.zip, password, &salt, &nonce)?;
decrypt_file(&files.encrypted, &files.zip, password, &salt, &nonce)?;
tracing::info!("unzipping {:?} to {:?}", &files.zip, &target_dir_path);
_unzip(&files.zip, &target_dir_path)?;
unzip(&files.zip, &target_dir_path)?;

tracing::info!("restore completed");
Ok(())
}

fn _get_backup_paths(tmp_base_path: &Path) -> Result<BackupPaths, APIError> {
fn get_backup_paths(tmp_base_path: &Path) -> Result<BackupPaths, APIError> {
create_dir_all(tmp_base_path)?;
let tempdir = tempfile::tempdir_in(tmp_base_path)?;
let encrypted = tempdir.path().join("backup.enc");
Expand All @@ -144,15 +144,15 @@ fn _get_backup_paths(tmp_base_path: &Path) -> Result<BackupPaths, APIError> {
})
}

fn _get_parent_path(file: &Path) -> Result<PathBuf, APIError> {
fn get_parent_path(file: &Path) -> Result<PathBuf, APIError> {
if let Some(parent) = file.parent() {
Ok(parent.to_path_buf())
} else {
Err(APIError::Unexpected(s!("Failed to get parent path")))
}
}

fn _zip_dir(path_in: &Path, path_out: &Path) -> Result<(), APIError> {
fn zip_dir(path_in: &Path, path_out: &Path) -> Result<(), APIError> {
// setup
let writer = File::create(path_out)?;
let mut zip = zip::ZipWriter::new(writer);
Expand Down Expand Up @@ -207,7 +207,7 @@ fn _zip_dir(path_in: &Path, path_out: &Path) -> Result<(), APIError> {
Ok(())
}

fn _unzip(zip_path: &PathBuf, path_out: &Path) -> Result<(), APIError> {
fn unzip(zip_path: &PathBuf, path_out: &Path) -> Result<(), APIError> {
// setup
let file =
File::open(zip_path).map_err(|e| APIError::Unexpected(format!("Failed to unzip: {e}")))?;
Expand Down Expand Up @@ -246,7 +246,7 @@ fn _unzip(zip_path: &PathBuf, path_out: &Path) -> Result<(), APIError> {
Ok(())
}

fn _get_cypher_secrets(
fn get_cypher_secrets(
password: &str,
salt_str: &str,
nonce_str: &str,
Expand Down Expand Up @@ -275,14 +275,14 @@ fn _get_cypher_secrets(
Ok(CypherSecrets { key, nonce })
}

fn _encrypt_file(
fn encrypt_file(
path_cleartext: &PathBuf,
path_encrypted: &PathBuf,
password: &str,
salt_str: &str,
nonce_str: &str,
) -> Result<(), APIError> {
let cypher_secrets = _get_cypher_secrets(password, salt_str, nonce_str)?;
let cypher_secrets = get_cypher_secrets(password, salt_str, nonce_str)?;

// - XChacha20Poly1305 is fast, requires no special hardware and supports stream operation
// - stream mode required as files to encrypt may be big, so avoiding a memory buffer
Expand Down Expand Up @@ -318,14 +318,14 @@ fn _encrypt_file(
Ok(())
}

fn _decrypt_file(
fn decrypt_file(
path_encrypted: &PathBuf,
path_cleartext: &PathBuf,
password: &str,
salt_str: &str,
nonce_str: &str,
) -> Result<(), APIError> {
let cypher_secrets = _get_cypher_secrets(password, salt_str, nonce_str)?;
let cypher_secrets = get_cypher_secrets(password, salt_str, nonce_str)?;

// setup
let aead = XChaCha20Poly1305::new(&cypher_secrets.key);
Expand Down
6 changes: 1 addition & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub enum APIError {
#[error("Insufficient capacity to cover the commitment transaction fees ({0} sat)")]
InsufficientCapacity(u64),

#[error("Not enough funds, get an address and send {0} sats there")]
#[error("Not enough funds, missing {0} sats")]
InsufficientFunds(u64),

#[error("Invalid address: {0}")]
Expand Down Expand Up @@ -260,9 +260,6 @@ pub enum APIError {
#[error("No valid transport endpoint found")]
NoValidTransportEndpoint,

#[error("Cannot perform this operation while an open channel operation is in progress")]
OpenChannelInProgress,

#[error("Output below the dust limit")]
OutputBelowDustLimit,

Expand Down Expand Up @@ -517,7 +514,6 @@ impl IntoResponse for APIError {
| APIError::NoAvailableUtxos
| APIError::NoRoute
| APIError::NotInitialized
| APIError::OpenChannelInProgress
| APIError::PaymentNotFound(_)
| APIError::RecipientIDAlreadyUsed
| APIError::SwapNotFound(_)
Expand Down
Loading
Loading