diff --git a/chain-runtimes/monero/Cargo.lock b/chain-runtimes/monero/Cargo.lock index 4e94374c1..543e2b19e 100644 --- a/chain-runtimes/monero/Cargo.lock +++ b/chain-runtimes/monero/Cargo.lock @@ -1259,7 +1259,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "onekey-monero-runtime" -version = "0.1.0-alpha.12" +version = "0.1.0-alpha.14" dependencies = [ "curve25519-dalek", "futures-lite", diff --git a/chain-runtimes/monero/Cargo.toml b/chain-runtimes/monero/Cargo.toml index 0a896b9e4..94b4a7c19 100644 --- a/chain-runtimes/monero/Cargo.toml +++ b/chain-runtimes/monero/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/monero-runtime"] exclude = ["vendor/sqlite-wasm-vfs-0.2.0"] [workspace.package] -version = "0.1.0-alpha.12" +version = "0.1.0-alpha.14" edition = "2021" license = "Apache-2.0" rust-version = "1.85" diff --git a/chain-runtimes/monero/crates/monero-runtime/src/query.rs b/chain-runtimes/monero/crates/monero-runtime/src/query.rs index 9aa69bcfb..9e19fa191 100644 --- a/chain-runtimes/monero/crates/monero-runtime/src/query.rs +++ b/chain-runtimes/monero/crates/monero-runtime/src/query.rs @@ -3,7 +3,7 @@ //! Paging and aggregation run as indexed SQLite queries. use monero_wallet::address::Network; -use monero_wallet::WalletOutput; +use monero_wallet::{OutputWithDecoys, WalletOutput}; use rusqlite::{params, OptionalExtension}; use serde_json::{json, Value}; @@ -297,6 +297,44 @@ pub fn load_output(db: &Db, output_id: i64) -> Result { }) } +/// The ring saved for the identity's output at `global_index`, if any. A blob that no longer +/// parses counts as none, so a newly picked ring replaces it. +pub fn load_ring(db: &Db, identity_id: i64, global_index: u64) -> Result> { + let Ok(index) = i64::try_from(global_index) else { + return Ok(None); + }; + let blob: Option> = db + .conn() + .query_row( + "SELECT blob FROM rings WHERE identity_id = ?1 AND global_index = ?2", + params![identity_id, index], + |r| r.get(0), + ) + .optional() + .map_err(|e| db_err("loadRing", e))?; + Ok(blob.and_then(|blob| OutputWithDecoys::read(&mut blob.as_slice()).ok())) +} + +/// Saves newly picked rings in one commit; a first max send can pick one per spendable output. +pub fn save_rings(db: &Db, identity_id: i64, rings: &[(u64, OutputWithDecoys)]) -> Result<()> { + if rings.is_empty() { + return Ok(()); + } + let tx = db + .conn() + .unchecked_transaction() + .map_err(|e| db_err("beginSaveRings", e))?; + for (global_index, ring) in rings { + let index = i64::try_from(*global_index).map_err(|e| db_err("saveRing", e))?; + tx.execute( + "INSERT OR REPLACE INTO rings (identity_id, global_index, blob) VALUES (?1, ?2, ?3)", + params![identity_id, index, ring.serialize()], + ) + .map_err(|e| db_err("saveRing", e))?; + } + tx.commit().map_err(|e| db_err("commitSaveRings", e)) +} + /// Transaction history, paged, newest height first. pub fn history(db: &Db, identity_id: i64, limit: u32, offset: u32) -> Result { let limit = limit.clamp(1, 500); diff --git a/chain-runtimes/monero/crates/monero-runtime/src/schema.rs b/chain-runtimes/monero/crates/monero-runtime/src/schema.rs index f225d6021..89fa1337f 100644 --- a/chain-runtimes/monero/crates/monero-runtime/src/schema.rs +++ b/chain-runtimes/monero/crates/monero-runtime/src/schema.rs @@ -8,7 +8,9 @@ use rusqlite::Connection; use crate::error::{db_err, ErrorCode, Result, RuntimeError}; /// Current schema version. Once released, any schema change must add a migration -/// and bump this; before that, V1 is edited in place. +/// and bump this; before that, V1 is edited in place. A new table that older +/// runtimes can ignore is the exception: it is created on every open without a +/// bump, so a downgrade still opens the database. pub const SCHEMA_VERSION: i64 = 1; /// Reorg safety window: `blocks` keeps hashes only for this many blocks below the tip (about a day). @@ -35,12 +37,30 @@ pub fn migrate(conn: &Connection) -> Result<()> { if version < 1 { tx.execute_batch(V1).map_err(|e| db_err("migrateV1", e))?; } + // Additive and idempotent, so it runs on every open without a version bump: an older runtime + // still opens the database and simply ignores the table. + tx.execute_batch(RINGS) + .map_err(|e| db_err("migrateRings", e))?; tx.execute(&format!("PRAGMA user_version = {SCHEMA_VERSION}"), []) .map_err(|e| db_err("setUserVersion", e))?; tx.commit().map_err(|e| db_err("commitMigrate", e)) } +/// The ring picked when a send first used an output, reused by every later attempt as wallet2's +/// ringdb does: fresh decoys each time would let a node intersect them down to the real spend. +/// Keyed by the output's global index, so a rescan that re-creates the output row keeps its ring; +/// removing the account removes its rings, which hold the output's keys. +const RINGS: &str = r#" +CREATE TABLE IF NOT EXISTS rings ( + identity_id INTEGER NOT NULL REFERENCES identities(id) ON DELETE CASCADE, + global_index INTEGER NOT NULL, + -- monero-oxide `OutputWithDecoys` serialization + blob BLOB NOT NULL, + PRIMARY KEY (identity_id, global_index) +); +"#; + /// Version 1. /// /// Amounts are `*_atomic` in piconero. Total supply (1.8e19) exceeds i64, but a @@ -327,6 +347,27 @@ mod tests { assert_eq!(v, SCHEMA_VERSION); } + // A database written before the rings table existed keeps its version and gains the table. + #[test] + fn an_existing_database_gains_the_rings_table() { + let conn = Connection::open_in_memory().unwrap(); + conn.execute_batch(V1).unwrap(); + conn.execute_batch("PRAGMA user_version = 1").unwrap(); + migrate(&conn).unwrap(); + let rings: i64 = conn + .query_row( + "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='rings'", + [], + |r| r.get(0), + ) + .unwrap(); + assert_eq!(rings, 1); + let v: i64 = conn + .query_row("PRAGMA user_version", [], |r| r.get(0)) + .unwrap(); + assert_eq!(v, 1); + } + #[test] fn refuses_to_open_a_newer_database() { let conn = Connection::open_in_memory().unwrap(); @@ -347,6 +388,7 @@ mod tests { "outputs", "transactions", "possible_spends", + "rings", ] { let n: i64 = conn .query_row( diff --git a/chain-runtimes/monero/crates/monero-runtime/src/spend.rs b/chain-runtimes/monero/crates/monero-runtime/src/spend.rs index 012a6eca2..ce74aa0fe 100644 --- a/chain-runtimes/monero/crates/monero-runtime/src/spend.rs +++ b/chain-runtimes/monero/crates/monero-runtime/src/spend.rs @@ -370,45 +370,119 @@ impl } } -/// Picks a ring of decoys for one output at the current height; a stale height -/// skews the ring distribution into a fingerprint. +/// One output's ring, with the global index to save it under when it is newly picked. The ring +/// saved when a send first used the output is reused while it still stands, as wallet2's ringdb +/// does: fresh decoys on every attempt would let the node intersect them down to the real spend. +/// A new ring is picked at the current height; a stale height skews the ring distribution into a +/// fingerprint. async fn ring_for( db: &Db, decoy_source: &impl monero_wallet::interface::ProvidesDecoys, rng: &mut R, + identity_id: i64, output_id: i64, chain_tip: u64, -) -> Result { +) -> Result<(OutputWithDecoys, Option)> { + let select_error = |e: monero_wallet::interface::TransactionsError| { + RuntimeError::with( + ErrorCode::NetworkError, + json!({ "operation": "selectDecoys", "outputId": output_id }), + ) + .detail(format!("{e:?}")) + }; let output = query::load_output(db, output_id)?; - OutputWithDecoys::new(rng, decoy_source, RING_LEN, chain_tip as usize, output) + let global_index = output.index_on_blockchain(); + // An unreadable saved ring counts as none. An unanswered check keeps the ring, as wallet2 + // does: rotating it on a node's silence would let that node intersect two rings. + if let Ok(Some(saved)) = query::load_ring(db, identity_id, global_index) { + if saved_ring_stands(decoy_source, &saved, &output) + .await + .map_err(select_error)? + { + return Ok((saved, None)); + } + } + let ring = OutputWithDecoys::new(rng, decoy_source, RING_LEN, chain_tip as usize, output) .await - .map_err(|e| { - RuntimeError::with( - ErrorCode::NetworkError, - json!({ "operation": "selectDecoys", "outputId": output_id }), - ) - .detail(format!("{e:?}")) - }) + .map_err(select_error)?; + Ok((ring, Some(global_index))) } -/// Rings for a set of outputs, reusing ones already fetched. +/// Whether a saved ring can be reused: it was built for this output and the node still serves +/// every member unlocked and unchanged (a deep reorg can drop or move one). +async fn saved_ring_stands( + decoy_source: &impl monero_wallet::interface::ProvidesDecoys, + saved: &OutputWithDecoys, + output: &WalletOutput, +) -> std::result::Result { + let decoys = saved.decoys(); + if decoys.len() != usize::from(RING_LEN) { + return Ok(false); + } + // Saved data for another output would fail signing on every retry. + let real_member = [output.key(), output.commitment().commit()]; + let same_output = saved.key() == output.key() + && saved.key_offset() == output.key_offset() + && decoys.signer_ring_members() == real_member; + if !same_output { + return Ok(false); + } + let positions = decoys.positions(); + let real_position = positions.get(usize::from(decoys.signer_index())); + if real_position != Some(&output.index_on_blockchain()) { + return Ok(false); + } + let served = decoy_source + .unlocked_ringct_outputs( + &positions, + monero_wallet::interface::EvaluateUnlocked::Normal, + ) + .await?; + Ok(served + .iter() + .zip(decoys.ring()) + .all(|(now, saved)| now.as_ref() == Some(saved))) +} + +/// Rings for a set of outputs, reusing ones already fetched. Rings picked here are saved even +/// when a later output fails, since the node has already seen their requests. async fn rings_for( db: &Db, decoy_source: &impl monero_wallet::interface::ProvidesDecoys, rng: &mut R, + identity_id: i64, output_ids: &[i64], chain_tip: u64, cache: &mut HashMap, ) -> Result> { let mut inputs = Vec::with_capacity(output_ids.len()); + let mut picked = Vec::new(); + let mut failure = None; for id in output_ids { - if !cache.contains_key(id) { - let ring = ring_for(db, decoy_source, rng, *id, chain_tip).await?; - cache.insert(*id, ring); + if let Some(ring) = cache.get(id) { + inputs.push(ring.clone()); + continue; } - inputs.push(cache[id].clone()); + match ring_for(db, decoy_source, rng, identity_id, *id, chain_tip).await { + Ok((ring, save_at)) => { + if let Some(global_index) = save_at { + picked.push((global_index, ring.clone())); + } + inputs.push(ring.clone()); + cache.insert(*id, ring); + } + Err(e) => { + failure = Some(e); + break; + } + } + } + // Unsaved rings still send; the next attempt only picks new ones. + let _ = query::save_rings(db, identity_id, &picked); + match failure { + Some(e) => Err(e), + None => Ok(inputs), } - Ok(inputs) } /// Assembles a `SignableTransaction`. Change always goes to the account's primary address. @@ -476,6 +550,7 @@ pub async fn prepare( db, decoy_source, rng, + identity_id, &selection.output_ids, chain_tip, &mut cache, @@ -554,7 +629,16 @@ pub async fn prepare_max( } let mut cache: HashMap = HashMap::new(); - let inputs = rings_for(db, decoy_source, rng, &output_ids, chain_tip, &mut cache).await?; + let inputs = rings_for( + db, + decoy_source, + rng, + identity_id, + &output_ids, + chain_tip, + &mut cache, + ) + .await?; // Probe for the fee: `necessary_fee()` on success, or from `NotEnoughFunds`. let fee_atomic = match assemble( @@ -948,6 +1032,229 @@ mod tests { (db, id) } + /// A synthetic chain of 10 outputs per block where every output is unlocked. The real output + /// answers with its own key and commitment. Members in `changed` answer with other points (as + /// after a reorg), members in `locked` as locked, and a request naming a member in `missing` + /// fails as a whole, as monerod does for an index it does not have yet. + struct FakeChain { + real: (u64, [Point; 2]), + changed: std::sync::Mutex>, + locked: std::sync::Mutex>, + missing: std::sync::Mutex>, + distributions: std::sync::atomic::AtomicUsize, + } + + impl FakeChain { + const TIP: u64 = 1_000; + + fn new(real: &WalletOutput) -> Self { + Self { + real: ( + real.index_on_blockchain(), + [real.key(), real.commitment().commit()], + ), + changed: std::sync::Mutex::new(Vec::new()), + locked: std::sync::Mutex::new(Vec::new()), + missing: std::sync::Mutex::new(Vec::new()), + distributions: std::sync::atomic::AtomicUsize::new(0), + } + } + + fn point(n: u64) -> Point { + Point::from( + curve25519_dalek::Scalar::from(n) + * curve25519_dalek::constants::ED25519_BASEPOINT_POINT, + ) + } + } + + impl monero_wallet::interface::ProvidesBlockchainMeta for FakeChain { + async fn latest_block_number( + &self, + ) -> std::result::Result { + Ok(Self::TIP as usize) + } + } + + impl monero_wallet::interface::ProvidesUnvalidatedDecoys for FakeChain { + async fn ringct_output_distribution( + &self, + _range: impl Send + std::ops::RangeBounds, + ) -> std::result::Result, monero_wallet::interface::InterfaceError> { + self.distributions + .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + Ok((0..=Self::TIP).map(|block| (block + 1) * 10).collect()) + } + + async fn unlocked_ringct_outputs( + &self, + indexes: &[u64], + _evaluate_unlocked: monero_wallet::interface::EvaluateUnlocked, + ) -> std::result::Result>, monero_wallet::interface::TransactionsError> + { + let missing = self.missing.lock().unwrap().clone(); + if indexes.iter().any(|index| missing.contains(index)) { + return Err(monero_wallet::interface::InterfaceError::InvalidInterface( + "unknown output index".to_owned(), + ) + .into()); + } + let changed = self.changed.lock().unwrap().clone(); + let locked = self.locked.lock().unwrap().clone(); + Ok(indexes + .iter() + .map(|&index| { + if index == self.real.0 { + return Some(self.real.1); + } + if locked.contains(&index) { + return None; + } + let seed = if changed.contains(&index) { 1 << 40 } else { 0 }; + Some([ + Self::point(seed + 2 * index + 2), + Self::point(seed + 2 * index + 3), + ]) + }) + .collect()) + } + } + + /// One owned output at global index 7 (the fixture's position on chain). + fn db_with_one_output() -> (Db, i64, i64, WalletOutput) { + let (db, id) = db_with_identity(); + let (blob, _) = owned_output_blob(&[3; 32], 1, [1; 32]); + db.conn() + .execute( + "INSERT INTO outputs (identity_id, blob, txid, global_index, index_in_tx, + amount_atomic, height) + VALUES (?1, ?2, ?3, 7, 0, 1000, 1)", + params![id, blob, vec![1u8; 32]], + ) + .unwrap(); + let output_id = db.conn().last_insert_rowid(); + let output = WalletOutput::read(&mut blob.as_slice()).unwrap(); + (db, id, output_id, output) + } + + /// One prepare's ring for the output, through the same path a send takes. + fn pick_ring( + db: &Db, + chain: &FakeChain, + identity_id: i64, + output_id: i64, + seed: u64, + ) -> OutputWithDecoys { + let mut rng = test_support::SeededRng::new(seed); + let mut cache = HashMap::new(); + futures_lite::future::block_on(rings_for( + db, + chain, + &mut rng, + identity_id, + &[output_id], + FakeChain::TIP, + &mut cache, + )) + .unwrap() + .remove(0) + } + + fn a_decoy(ring: &OutputWithDecoys) -> u64 { + ring.decoys() + .positions() + .into_iter() + .find(|&position| position != 7) + .unwrap() + } + + fn distributions(chain: &FakeChain) -> usize { + chain + .distributions + .load(std::sync::atomic::Ordering::SeqCst) + } + + // Fresh decoys on each attempt would let the node intersect them down to the real spend. + #[test] + fn a_later_attempt_reuses_the_ring_picked_first() { + let (db, id, output_id, output) = db_with_one_output(); + let chain = FakeChain::new(&output); + let first = pick_ring(&db, &chain, id, output_id, 1); + let second = pick_ring(&db, &chain, id, output_id, 2); + assert_eq!(first.decoys().positions(), second.decoys().positions()); + assert_eq!( + distributions(&chain), + 1, + "the second attempt picked no new decoys" + ); + } + + #[test] + fn a_saved_ring_with_a_changed_or_locked_member_is_replaced() { + for lock in [false, true] { + let (db, id, output_id, output) = db_with_one_output(); + let chain = FakeChain::new(&output); + let first = pick_ring(&db, &chain, id, output_id, 1); + if lock { + chain.locked.lock().unwrap().push(a_decoy(&first)); + } else { + chain.changed.lock().unwrap().push(a_decoy(&first)); + } + + let second = pick_ring(&db, &chain, id, output_id, 2); + assert_eq!(distributions(&chain), 2, "a new ring was picked"); + let saved = query::load_ring(&db, id, 7).unwrap().unwrap(); + assert_eq!(saved.decoys().positions(), second.decoys().positions()); + } + } + + // A node that does not answer must not get a second ring to intersect with the first. + #[test] + fn an_unanswered_check_fails_and_keeps_the_saved_ring() { + let (db, id, output_id, output) = db_with_one_output(); + let chain = FakeChain::new(&output); + let first = pick_ring(&db, &chain, id, output_id, 1); + chain.missing.lock().unwrap().push(a_decoy(&first)); + + let mut rng = test_support::SeededRng::new(2); + let mut cache = HashMap::new(); + let error = futures_lite::future::block_on(rings_for( + &db, + &chain, + &mut rng, + id, + &[output_id], + FakeChain::TIP, + &mut cache, + )) + .unwrap_err(); + assert!(matches!(error.code, ErrorCode::NetworkError)); + assert_eq!(distributions(&chain), 1, "no new ring was picked"); + let saved = query::load_ring(&db, id, 7).unwrap().unwrap(); + assert_eq!(saved.decoys().positions(), first.decoys().positions()); + + // Once the node answers again, the same ring is used. + chain.missing.lock().unwrap().clear(); + let later = pick_ring(&db, &chain, id, output_id, 3); + assert_eq!(later.decoys().positions(), first.decoys().positions()); + } + + // Saved rings hold the output's keys, so they go with the account. + #[test] + fn removing_the_account_removes_its_rings() { + let (mut db, id, output_id, output) = db_with_one_output(); + let chain = FakeChain::new(&output); + pick_ring(&db, &chain, id, output_id, 1); + assert!(query::load_ring(&db, id, 7).unwrap().is_some()); + + assert!(db.remove_identity("acct-1").unwrap()); + let rings: i64 = db + .conn() + .query_row("SELECT count(*) FROM rings", [], |r| r.get(0)) + .unwrap(); + assert_eq!(rings, 0); + } + type OutputRow = (i64, Option>, Option, Option>); fn output_rows(db: &Db) -> Vec { diff --git a/chain-runtimes/zcash/Cargo.lock b/chain-runtimes/zcash/Cargo.lock index 633768ad3..c467aa1d0 100644 --- a/chain-runtimes/zcash/Cargo.lock +++ b/chain-runtimes/zcash/Cargo.lock @@ -1655,7 +1655,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "onekey-zcash-keys" -version = "0.1.0-alpha.15" +version = "0.1.0-alpha.16" dependencies = [ "bip0039", "bip32", @@ -1688,7 +1688,7 @@ dependencies = [ [[package]] name = "onekey-zcash-runtime" -version = "0.1.0-alpha.15" +version = "0.1.0-alpha.16" dependencies = [ "blake2b_simd", "console_error_panic_hook", @@ -1738,7 +1738,7 @@ dependencies = [ [[package]] name = "onekey-zcash-send-policy" -version = "0.1.0-alpha.15" +version = "0.1.0-alpha.16" dependencies = [ "serde", "serde_json", diff --git a/chain-runtimes/zcash/Cargo.toml b/chain-runtimes/zcash/Cargo.toml index b98ad00be..d1c0ba3d9 100644 --- a/chain-runtimes/zcash/Cargo.toml +++ b/chain-runtimes/zcash/Cargo.toml @@ -8,7 +8,7 @@ members = [ exclude = ["vendor/sqlite-wasm-vfs-0.2.0"] [workspace.package] -version = "0.1.0-alpha.15" +version = "0.1.0-alpha.16" edition = "2021" license = "Apache-2.0" rust-version = "1.85" diff --git a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs index 8847a4698..bd363132f 100644 --- a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs +++ b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs @@ -10,7 +10,7 @@ use sapling::bundle::Authorized as SaplingAuthorized; use serde::{Deserialize, Serialize}; use transparent::{ address::TransparentAddress, - builder::{TransparentBuilder, TransparentSigningSet}, + builder::TransparentBuilder, bundle::{OutPoint, TxOut}, keys::{AccountPrivKey, AccountPubKey, NonHardenedChildIndex, TransparentKeyScope}, }; @@ -572,11 +572,12 @@ fn build_transaction( let quote = plan.quote(); let branch = BranchId::for_height(&MAIN_NETWORK, plan.target_height); let mut builder = TransparentBuilder::empty(); - let mut signing_set = TransparentSigningSet::new(); + let account_pubkey = account_key.to_account_pubkey(); + let mut coins = Vec::with_capacity(plan.inputs.len()); + let mut pubkeys = Vec::with_capacity(plan.inputs.len()); for input in &plan.inputs { - let secret_key = derive_secret_key(account_key, input.path)?; - let pubkey = signing_set.add_key(secret_key); + let pubkey = derive_public_key(&account_pubkey, input.path)?; let derived_address = TransparentAddress::from_pubkey(&pubkey); let expected_script = derived_address.script().to_bytes(); if input.script_pub_key != expected_script { @@ -585,13 +586,16 @@ fn build_transaction( serde_json::json!({ "txid": input.txid.to_string(), "vout": input.request.vout }), )); } + let coin = TxOut::new(input.value, derived_address.script().into()); builder .add_p2pkh_input( pubkey, OutPoint::new(*input.txid.as_ref(), input.request.vout), - TxOut::new(input.value, derived_address.script().into()), + coin.clone(), ) .map_err(|e| KeysError::new(ErrorCode::TransactionBuildFailed).detail(e))?; + coins.push(coin); + pubkeys.push(pubkey); } for output in &plan.recipients { @@ -600,8 +604,7 @@ fn build_transaction( .map_err(|e| KeysError::new(ErrorCode::TransactionBuildFailed).detail(e))?; } if let Some((change_request, output, path)) = &plan.change { - let secret_key = derive_secret_key(account_key, *path)?; - let pubkey = signing_set.add_key(secret_key); + let pubkey = derive_public_key(&account_pubkey, *path)?; let derived_address = TransparentAddress::from_pubkey(&pubkey); if derived_address != require_transparent(output.address)? { return Err(KeysError::with( @@ -639,19 +642,49 @@ fn build_transaction( None, ); let txid_parts = unsigned_tx.digest(TxIdDigester); - let authorized_bundle = transparent_bundle - .apply_signatures( - |input| { - *signature_hash( - &unsigned_tx, - &SignableInput::Transparent(input), - &txid_parts, - ) - .as_ref() - }, - &signing_set, + + // Each key signs its own input and is erased right away. Upstream's `TransparentSigningSet` + // would hold every key until the end and free its list without wiping it. + let signer = secp256k1::Secp256k1::signing_only(); + let mut vin = Vec::with_capacity(plan.inputs.len()); + for (index, ((input, unsigned_input), (coin, pubkey))) in plan + .inputs + .iter() + .zip(&transparent_bundle.vin) + .zip(coins.iter().zip(&pubkeys)) + .enumerate() + { + // The fields upstream `apply_signatures` signs: SIGHASH_ALL, and for P2PKH a script code + // equal to the coin's script. + let signable = transparent::sighash::SignableInput::from_parts( + &transparent_bundle, + transparent::sighash::SighashType::ALL, + index, + coin.script_pubkey(), + coin.script_pubkey(), + input.value, ) .map_err(|e| KeysError::new(ErrorCode::TransactionBuildFailed).detail(e))?; + let sighash = signature_hash( + &unsigned_tx, + &SignableInput::Transparent(signable), + &txid_parts, + ); + let message = secp256k1::Message::from_digest(*sighash.as_ref()); + let mut secret_key = derive_secret_key(account_key, input.path)?; + let signature = signer.sign_ecdsa(&message, &secret_key); + secret_key.non_secure_erase(); + vin.push(transparent::bundle::TxIn::from_parts( + unsigned_input.prevout().clone(), + p2pkh_script_sig(&signature, pubkey)?, + unsigned_input.sequence(), + )); + } + let authorized_bundle = transparent::bundle::Bundle { + vin, + vout: transparent_bundle.vout.clone(), + authorization: transparent::bundle::Authorized, + }; let transaction = TransactionData::::from_parts_v6( branch, 0, @@ -677,6 +710,24 @@ fn build_transaction( }) } +/// The P2PKH spend script as upstream's private `construct_script_sig` builds it: the DER +/// signature followed by SIGHASH_ALL, then the compressed public key. +fn p2pkh_script_sig( + signature: &secp256k1::ecdsa::Signature, + pubkey: &secp256k1::PublicKey, +) -> Result { + let mut signature_bytes = signature.serialize_der().to_vec(); + signature_bytes.push(transparent::sighash::SIGHASH_ALL); + let push = |bytes: &[u8]| { + zcash_script::pv::push_value(bytes) + .ok_or_else(|| KeysError::new(ErrorCode::TransactionBuildFailed)) + }; + Ok( + zcash_script::script::Component(vec![push(&signature_bytes)?, push(&pubkey.serialize())?]) + .into(), + ) +} + fn calculate_fee( target_height: BlockHeight, input_count: usize, @@ -932,8 +983,8 @@ mod tests { fn address_and_script(seed: &[u8], account: u32, scope: u32, index: u32) -> (String, String) { let key = account_key(seed, account); - let secret = derive_secret_key( - &key, + let public = derive_public_key( + &key.to_account_pubkey(), DerivationPath { account, scope, @@ -941,8 +992,6 @@ mod tests { }, ) .unwrap(); - let mut signing_set = TransparentSigningSet::new(); - let public = signing_set.add_key(secret); let address = TransparentAddress::from_pubkey(&public); ( address.to_zcash_address(NetworkType::Main).to_string(), @@ -994,6 +1043,86 @@ mod tests { .to_string() } + /// The previous signing path: every key in upstream's `TransparentSigningSet`, then + /// `apply_signatures`. Kept only to prove the new path signs the same bytes. + fn build_with_signing_set(plan: TransactionPlan, account_key: &AccountPrivKey) -> String { + use transparent::builder::TransparentSigningSet; + let branch = BranchId::for_height(&MAIN_NETWORK, plan.target_height); + let mut builder = TransparentBuilder::empty(); + let mut signing_set = TransparentSigningSet::new(); + for input in &plan.inputs { + let pubkey = signing_set.add_key(derive_secret_key(account_key, input.path).unwrap()); + let script = TransparentAddress::from_pubkey(&pubkey).script(); + builder + .add_p2pkh_input( + pubkey, + OutPoint::new(*input.txid.as_ref(), input.request.vout), + TxOut::new(input.value, script.into()), + ) + .unwrap(); + } + let change = plan.change.as_ref().map(|(_, output, _)| output); + for output in plan.recipients.iter().chain(change) { + builder + .add_output(&require_transparent(output.address).unwrap(), output.value) + .unwrap(); + } + let bundle = builder.build().unwrap(); + let unsigned = TransactionData::::from_parts_v6( + branch, + 0, + plan.expiry_height, + Some(bundle.clone()), + None, + None, + None, + ); + let txid_parts = unsigned.digest(TxIdDigester); + let signed = bundle + .apply_signatures( + |input| { + *signature_hash(&unsigned, &SignableInput::Transparent(input), &txid_parts) + .as_ref() + }, + &signing_set, + ) + .unwrap(); + let tx = TransactionData::::from_parts_v6( + branch, + 0, + plan.expiry_height, + Some(signed), + None, + None, + None, + ) + .freeze() + .unwrap(); + let mut raw = Vec::new(); + tx.write(&mut raw).unwrap(); + hex::encode(raw) + } + + // Signing input by input must produce exactly what upstream's signing set produced, including + // several inputs, two inputs at one address, change, and send-max. + #[test] + fn signs_the_same_bytes_as_the_upstream_signing_set() { + let mut shared: serde_json::Value = + serde_json::from_str(&request_json(&[100_000, 100_000, 100_000], false)).unwrap(); + shared["utxos"][1]["derivationPath"] = shared["utxos"][0]["derivationPath"].clone(); + shared["utxos"][1]["scriptPubKey"] = shared["utxos"][0]["scriptPubKey"].clone(); + let key = account_key(&seed(), 0); + for request in [ + request_json(&[100_000, 200_000, 300_000, 400_000], false), + request_json(&[100_000, 200_000, 300_000], true), + shared.to_string(), + ] { + let plan = || plan_transaction(parse_request(&request).unwrap()).unwrap(); + let built = build_transaction(plan(), &key).unwrap(); + assert_eq!(built.raw_tx, build_with_signing_set(plan(), &key)); + } + } + fn shielding_request(send_max: bool) -> String { let sk = orchard::keys::SpendingKey::from_bytes([7; 32]).unwrap(); let fvk = orchard::keys::FullViewingKey::from(&sk);