From 5681aeae7eb3d95c6fa0491968bca945780dd4bb Mon Sep 17 00:00:00 2001 From: Luther Blissett Date: Fri, 23 Sep 2022 19:22:00 +0200 Subject: [PATCH] darkotc: Simplify transaction signing. --- bin/darkotc/src/main.rs | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/bin/darkotc/src/main.rs b/bin/darkotc/src/main.rs index 918a31ca8..631580855 100644 --- a/bin/darkotc/src/main.rs +++ b/bin/darkotc/src/main.rs @@ -493,30 +493,18 @@ async fn sign_tx(endpoint: Url, data: &str) -> Result { eprintln!("Trying to sign transaction"); let mut tx: Transaction = deserialize(&bs58::decode(data).into_vec()?)?; - let mut input_idxs = vec![]; + // We assume our input and our output are in the same index, since this + // transaction contains 2 inputs and 2 outputs, and one of each is ours, + // and one of each is the other party's. So we go on and sign the input + // index of the output index we can decrypt the note for. + let mut idx_to_sign = 0; let mut signature = schnorr::Signature::dummy(); - // Find dummy signatures to fill. We assume we're using the same - // signature everywhere. - eprintln!("Looking for dummy signatures..."); - for (i, input) in tx.inputs.iter().enumerate() { - if input.signature == schnorr::Signature::dummy() { - eprintln!("Found dummy signature in input {}", i); - input_idxs.push(i); - } - } - - if input_idxs.is_empty() { - eprintln!("Error: Did not find any dummy signatures in the transaction."); - exit(1); - } - - // Find a note to decrypt that holds our secret key. + eprintln!("Looking for an encrypted note we can decrypt..."); let mut found_secret = false; for (i, output) in tx.outputs.iter().enumerate() { // TODO: FIXME: Consider not closing the RPC on failure. - let rpc_client = RpcClient::new(endpoint.clone()).await?; - let rpc = Rpc { rpc_client }; + let rpc = Rpc { rpc_client: RpcClient::new(endpoint.clone()).await? }; let note = match rpc.decrypt_note(&output.enc_note).await { Ok(v) => v, @@ -531,6 +519,7 @@ async fn sign_tx(endpoint: Url, data: &str) -> Result { signature = try_sign_tx(¬e, &unsigned_tx_data[..])?; found_secret = true; + idx_to_sign = i; break } @@ -543,10 +532,7 @@ async fn sign_tx(endpoint: Url, data: &str) -> Result { exit(1); } - for i in input_idxs { - tx.inputs[i].signature = signature.clone(); - } - + tx.inputs[idx_to_sign].signature = signature.clone(); Ok(tx) }