This commit is contained in:
Hanh 2023-04-26 08:05:40 +10:00
parent c10a074fb6
commit 13d19cf143
3 changed files with 90 additions and 98 deletions

View File

@ -7,11 +7,9 @@ use group::GroupEncoding;
use hex_literal::hex;
use jubjub::{Fq, Fr};
use orchard::keys::Scope;
use crate::ledger::builder::transparent_bundle::{TransparentBuilder, TransparentInputUnAuthorized};
use crate::ledger::builder::transparent_bundle::TransparentBuilder;
use crate::ledger::transport::*;
use crate::taddr::derive_from_pubkey;
use crate::{CompactTxStreamerClient, Destination, RawTransaction, Source, TransactionPlan};
use anyhow::{anyhow, Result};
use rand::{rngs::OsRng, RngCore, SeedableRng};
@ -21,12 +19,12 @@ use secp256k1::PublicKey;
use sha2::Sha256;
use tonic::{transport::Channel, Request};
use zcash_client_backend::encoding::{
decode_transparent_address, encode_extended_full_viewing_key, encode_transparent_address,
encode_extended_full_viewing_key, encode_transparent_address,
};
use zcash_primitives::consensus::Network;
use zcash_primitives::consensus::Parameters;
use zcash_primitives::legacy::{Script, TransparentAddress};
use zcash_primitives::transaction::components::{transparent, OutPoint, TxIn, TxOut};
use zcash_primitives::legacy::TransparentAddress;
use zcash_primitives::zip32::ExtendedFullViewingKey;
use zcash_primitives::{
@ -50,8 +48,8 @@ use zcash_primitives::{
};
use zcash_proofs::{prover::LocalTxProver, sapling::SaplingProvingContext};
mod transparent_bundle;
mod orchard_bundle;
mod transparent_bundle;
struct SpendDescriptionUnAuthorized {
cv: ValueCommitment,
@ -88,10 +86,7 @@ pub async fn show_public_keys() -> Result<()> {
}
pub fn create_hasher(perso: &[u8]) -> State {
let h = Params::new()
.hash_length(32)
.personal(perso)
.to_state();
let h = Params::new().hash_length(32).personal(perso).to_state();
h
}
@ -109,8 +104,7 @@ pub async fn build_broadcast_tx(
anyhow::bail!("This ledger wallet has a different address");
}
let taddr = &tx_plan.taddr;
let _taddr = &tx_plan.taddr;
// Compute header digest
let mut h = create_hasher(b"ZTxIdHeadersHash");
@ -137,7 +131,7 @@ pub async fn build_broadcast_tx(
let nf_key = proofgen_key.to_viewing_key().nk;
let o_fvk: [u8; 96] = ledger_get_o_fvk().await?.try_into().unwrap();
let o_fvk =
let _o_fvk =
orchard::keys::FullViewingKey::from_bytes(&o_fvk).ok_or(anyhow!("Invalid Orchard FVK"))?;
assert_eq!(
@ -180,7 +174,9 @@ pub async fn build_broadcast_tx(
for sp in tx_plan.spends.iter() {
match sp.source {
Source::Transparent { txid, index } => {
transparent_builder.add_input(txid, index, sp.amount).await?;
transparent_builder
.add_input(txid, index, sp.amount)
.await?;
}
Source::Sapling {
diversifier,
@ -270,7 +266,9 @@ pub async fn build_broadcast_tx(
let mut shielded_outputs = vec![];
for output in tx_plan.outputs.iter() {
if let Destination::Transparent(raw_address) = output.destination {
transparent_builder.add_output(raw_address, output.amount).await?;
transparent_builder
.add_output(raw_address, output.amount)
.await?;
}
}
ledger_set_stage(3).await?;

View File

@ -1,13 +1,18 @@
use std::{fs::File, io::Read};
use blake2b_simd::Params;
use byteorder::{LE, WriteBytesExt};
use byteorder::{WriteBytesExt, LE};
use group::{Group, GroupEncoding};
use orchard::{
builder::{SpendInfo, InProgress, Unproven, Unauthorized as OrchardUnauthorized, SigningMetadata, SigningParts},
bundle::{Authorized, Flags, Authorization},
builder::{
InProgress, SigningMetadata, SigningParts, SpendInfo, Unauthorized as OrchardUnauthorized,
Unproven,
},
bundle::{Authorization, Authorized, Flags},
circuit::{Circuit, Instance, ProvingKey},
keys::{Diversifier, FullViewingKey, Scope, SpendValidatingKey, SpendingKey, SpendAuthorizingKey},
keys::{
Diversifier, FullViewingKey, Scope, SpendAuthorizingKey, SpendValidatingKey, SpendingKey,
},
note::{ExtractedNoteCommitment, Nullifier, RandomSeed, TransmittedNoteCiphertext},
note_encryption::OrchardNoteEncryption,
primitives::redpallas::{Signature, SpendAuth},
@ -19,16 +24,22 @@ use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha20Rng;
use ripemd::Digest;
use crate::{
connect_lightwalletd, decode_orchard_merkle_path, ledger::*, RawTransaction, TransactionPlan,
};
use anyhow::Result;
use tonic::Request;
use crate::{decode_orchard_merkle_path, TransactionPlan, RawTransaction, connect_lightwalletd, ledger::*};
use zcash_primitives::{transaction::{components::Amount, TransactionData, TxVersion, Transaction, sighash_v5, sighash::SignableInput,
txid::TxIdDigester, Unauthorized, Authorized as TxAuthorized},
consensus::{BlockHeight, BranchId}};
use hex_literal::hex;
use group::ff::Field;
use hex_literal::hex;
use nonempty::NonEmpty;
use zcash_primitives::{
consensus::{BlockHeight, BranchId},
transaction::{
components::Amount, sighash::SignableInput, sighash_v5, txid::TxIdDigester,
Authorized as TxAuthorized, Transaction, TransactionData, TxVersion, Unauthorized,
},
};
use crate::{Destination, Source};
@ -44,7 +55,7 @@ pub async fn build_orchard() -> Result<()> {
let mut prng = ChaCha20Rng::from_seed([0; 32]);
let mut rseed_rng = ChaCha20Rng::from_seed([1; 32]);
let mut alpha_rng = ChaCha20Rng::from_seed([2; 32]);
let mut sig_rng = ChaCha20Rng::from_seed([3; 32]);
let _sig_rng = ChaCha20Rng::from_seed([3; 32]);
let spending_key = hex::decode(dotenv::var("SPENDING_KEY").unwrap()).unwrap();
let spk = SpendingKey::from_bytes(spending_key.try_into().unwrap()).unwrap();
@ -117,7 +128,7 @@ pub async fn build_orchard() -> Result<()> {
})
.collect();
let zero_bsk = ValueCommitTrapdoor::zero().into_bsk();
let _zero_bsk = ValueCommitTrapdoor::zero().into_bsk();
let mut orchard_memos_hasher = Params::new()
.hash_length(32)
@ -204,9 +215,15 @@ pub async fn build_orchard() -> Result<()> {
orchard_nc_hasher.update(&enc[564..]);
orchard_nc_hasher.update(&out);
println!("d/pkd {}", hex::encode(&output.recipient.to_raw_address_bytes()));
println!(
"d/pkd {}",
hex::encode(&output.recipient.to_raw_address_bytes())
);
println!("rho {}", hex::encode(&rho.to_bytes()));
println!("amount {}", hex::encode(&output.amount.inner().to_le_bytes()));
println!(
"amount {}",
hex::encode(&output.amount.inner().to_le_bytes())
);
println!("rseed {}", hex::encode(&rseed.as_bytes()));
println!("cmx {}", hex::encode(&cmx.to_bytes()));
@ -218,7 +235,8 @@ pub async fn build_orchard() -> Result<()> {
cv_net.clone(),
SigningMetadata {
dummy_ask: None,
parts: SigningParts { ak, alpha } },
parts: SigningParts { ak, alpha },
},
);
actions.push(action);
@ -244,7 +262,9 @@ pub async fn build_orchard() -> Result<()> {
anchor,
InProgress::<Unproven, OrchardUnauthorized> {
proof: Unproven { circuits: vec![] },
sigs: OrchardUnauthorized { bsk: bsk.clone() } });
sigs: OrchardUnauthorized { bsk: bsk.clone() },
},
);
let tx_data: TransactionData<Unauthorized> = TransactionData {
version: TxVersion::Zip225,
@ -282,8 +302,7 @@ pub async fn build_orchard() -> Result<()> {
for (a, o) in bundle.actions().iter().zip(padded_outputs.iter()) {
let nf = a.nullifier().to_bytes();
let epk = a.encrypted_note().epk_bytes;
let address =
ledger_add_o_action(
let _address = ledger_add_o_action(
&nf,
o.amount.inner(),
&epk,
@ -308,9 +327,13 @@ pub async fn build_orchard() -> Result<()> {
let sig_bytes: [u8; 64] = ledger_sign_orchard().await.unwrap().try_into().unwrap();
let signature: Signature<SpendAuth> = sig_bytes.into();
let auth_action = Action::from_parts(
a.nullifier().clone(), a.rk().clone(), a.cmx().clone(),
a.encrypted_note().clone(), a.cv_net().clone(),
signature);
a.nullifier().clone(),
a.rk().clone(),
a.cmx().clone(),
a.encrypted_note().clone(),
a.cv_net().clone(),
signature,
);
auth_actions.push(auth_action);
}
let auth_actions = NonEmpty::from_slice(&auth_actions).unwrap();
@ -338,8 +361,8 @@ pub async fn build_orchard() -> Result<()> {
let mut tx_bytes = vec![];
tx.write(&mut tx_bytes).unwrap();
let orchard_memos_hash = orchard_memos_hasher.finalize();
let orchard_nc_hash = orchard_nc_hasher.finalize();
let _orchard_memos_hash = orchard_memos_hasher.finalize();
let _orchard_nc_hash = orchard_nc_hasher.finalize();
let mut client = connect_lightwalletd("https://lwdv3.zecwallet.co").await?;
let response = client

View File

@ -1,53 +1,23 @@
use blake2b_simd::Params;
use blake2b_simd::State;
use byteorder::WriteBytesExt;
use byteorder::LE;
use ff::{Field, PrimeField};
use group::GroupEncoding;
use hex_literal::hex;
use jubjub::{Fq, Fr};
use orchard::keys::Scope;
use hex_literal::hex;
use crate::ledger::transport::*;
use crate::taddr::derive_from_pubkey;
use crate::{CompactTxStreamerClient, Destination, RawTransaction, Source, TransactionPlan};
use anyhow::{anyhow, Result};
use rand::{rngs::OsRng, RngCore, SeedableRng};
use rand_chacha::ChaChaRng;
use ripemd::{Digest, Ripemd160};
use secp256k1::PublicKey;
use sha2::Sha256;
use tonic::{transport::Channel, Request};
use zcash_client_backend::encoding::{
decode_transparent_address, encode_extended_full_viewing_key, encode_transparent_address,
};
use anyhow::Result;
use ripemd::Digest;
use zcash_client_backend::encoding::decode_transparent_address;
use zcash_primitives::consensus::Network;
use zcash_primitives::consensus::Parameters;
use zcash_primitives::legacy::{Script, TransparentAddress};
use zcash_primitives::transaction::components::{transparent, OutPoint, TxIn, TxOut};
use zcash_primitives::zip32::ExtendedFullViewingKey;
use zcash_primitives::{
consensus::{BlockHeight, BranchId, MainNetwork},
constants::PROOF_GENERATION_KEY_GENERATOR,
merkle_tree::IncrementalWitness,
sapling::{
note_encryption::sapling_note_encryption,
prover::TxProver,
redjubjub::Signature,
value::{NoteValue, ValueCommitment, ValueSum},
Diversifier, Node, Note, Nullifier, PaymentAddress, Rseed,
},
transaction::{
components::{
sapling::{Authorized as SapAuthorized, Bundle},
Amount, OutputDescription, SpendDescription, GROTH_PROOF_SIZE,
},
Authorized, TransactionData, TxVersion,
},
};
use zcash_proofs::{prover::LocalTxProver, sapling::SaplingProvingContext};
use zcash_primitives::transaction::components::Amount;
use super::create_hasher;
@ -76,7 +46,9 @@ impl TransparentBuilder {
&network.b58_pubkey_address_prefix(),
&network.b58_script_address_prefix(),
&taddr_str,
).unwrap().unwrap();
)
.unwrap()
.unwrap();
let pkh = match taddr {
TransparentAddress::PublicKey(pkh) => pkh,
_ => unreachable!(),
@ -151,7 +123,7 @@ impl TransparentBuilder {
}
pub async fn sign(&mut self) -> Result<()> {
let mut vins: Vec<TxIn<transparent::Authorized>> = vec![];
let _vins: Vec<TxIn<transparent::Authorized>> = vec![];
for tin in self.vin.iter() {
let mut txin_hasher = create_hasher(b"Zcash___TxInHash");
@ -191,8 +163,7 @@ impl TransparentBuilder {
authorization: transparent::Authorized,
};
Some(transparent_bundle)
}
else {
} else {
None
}
}