Browse Source

Fix parsing of electrum error response

fix-olivia-event-id
Thomas Eizinger 3 years ago
parent
commit
fdd50e889e
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 53
      daemon/src/wallet.rs

53
daemon/src/wallet.rs

@ -1,5 +1,5 @@
use crate::model::WalletInfo; use crate::model::WalletInfo;
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, Context, Result};
use bdk::bitcoin::util::bip32::ExtendedPrivKey; use bdk::bitcoin::util::bip32::ExtendedPrivKey;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction; use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Amount, PublicKey, Transaction, Txid}; use bdk::bitcoin::{Amount, PublicKey, Transaction, Txid};
@ -124,27 +124,22 @@ impl Wallet {
} }
} }
fn parse_rpc_protocol_error_code(error_value: &Value) -> anyhow::Result<i64> { fn parse_rpc_protocol_error_code(error_value: &Value) -> Result<i64> {
let json_map = match error_value { let json = error_value
serde_json::Value::Object(map) => map, .as_str()
_ => bail!("Json error is not json object "), .context("Not a string")?
}; .split_terminator("RPC error: ")
.nth(1)
let error_code_value = match json_map.get("code") { .context("Unknown error code format")?;
Some(val) => val,
None => bail!("No error code field"), let error = serde_json::from_str::<RpcError>(json).context("Error has unexpected format")?;
};
Ok(error.code)
let error_code_number = match error_code_value { }
serde_json::Value::Number(num) => num,
_ => bail!("Error code is not a number"), #[derive(serde::Deserialize)]
}; struct RpcError {
code: i64,
if let Some(int) = error_code_number.as_i64() {
Ok(int)
} else {
bail!("Error code is not an unsigned integer")
}
} }
/// Bitcoin error codes: https://github.com/bitcoin/bitcoin/blob/97d3500601c1d28642347d014a6de1e38f53ae4e/src/rpc/protocol.h#L23 /// Bitcoin error codes: https://github.com/bitcoin/bitcoin/blob/97d3500601c1d28642347d014a6de1e38f53ae4e/src/rpc/protocol.h#L23
@ -160,3 +155,17 @@ impl From<RpcErrorCode> for i64 {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_error_response() {
let response = serde_json::Value::String(r#"sendrawtransaction RPC error: {"code":-27,"message":"Transaction already in block chain"}"#.to_owned());
let code = parse_rpc_protocol_error_code(&response).unwrap();
assert_eq!(code, -27);
}
}

Loading…
Cancel
Save