Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zecwallet Lite compatiblity #27

Merged
20 commits merged into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
6 changes: 3 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: clippy
run: |
cargo clippy --version
cargo clippy --all-features
cargo clippy --features "bundled-prover","local-prover"

tests:
runs-on: ubuntu-latest
Expand All @@ -58,7 +58,7 @@ jobs:
sharedKey: ${{ github.run_id }}-${{ github.run_attempt }}

- run: sudo apt-get install -y libudev-dev libusb-1.0.0-dev
- name: test --all-features
- name: test
run: |
#with --lib we only test the unit tests
cargo test --lib --all-features
cargo test --lib --features "bundled-prover","local-prover"
39 changes: 25 additions & 14 deletions ledger-zcash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,40 @@ autobenches = false
[lib]
name = "ledger_zcash"

[features]
default = ["normal-zcash"]
normal-zcash = ["zcash_primitives", "zcash-hsmbuilder/normal-zcash"]
zecwallet-compat = ["zecw_primitives", "zcash-hsmbuilder/zecwallet-compat"]

[dependencies]
arrayvec = "0.7.2"
byteorder = "1.4.3"
lazy_static = "1.4.0"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0.31"
log = "0.4.17"
zx-bip44 = "0.1.0"
cfg-if = "1.0.0"
ff = "0.12"
group = "0.12"
lazy_static = "1"
hex = "0.4.3"
jubjub = { version = "0.5.1", default-features = false }
secp256k1 = { version = "0.20", default-features = false }
group = "0.8.0"
jubjub = { version = "0.9", default-features = false }
log = "0.4.17"
rand_core = "0.6"
ripemd = "0.1"
secp256k1 = { version = "0.21", default-features = false }
sha2 = "0.9"
thiserror = "1.0.31"
zx-bip44 = "0.1.0"

serde = { version = "1.0", features = ["derive"] }

ledger-transport = "0.9.0"
ledger-zondax-generic = "0.9.1"

#zcash
zcash-hsmbuilder = { path = "../zcash-hsmbuilder", version = "0.3" }
zcash_primitives = { version = "0.5", features = ["transparent-inputs"] }
rand_core = "0.5"
arrayvec = "0.7.2"
ripemd = "0.1.1"
ff = "0.8"
zcash-hsmbuilder = { path = "../zcash-hsmbuilder", default-features = false, version = "0.3" }
zcash_primitives = { version = "0.6", features = ["transparent-inputs"], optional = true }

#zecwallet-compat
zecw_primitives = { git = "https://github.com/adityapk00/librustzcash", rev = "7183acd2fe12ebf201cae5b871166e356273c481", features = ["transparent-inputs"], optional = true, package = "zcash_primitives" }
tokio = { version = "1.6", features = ["sync"] }

[dev-dependencies]
futures = "0.3.21"
Expand Down
51 changes: 28 additions & 23 deletions ledger-zcash/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,42 @@
#![deny(unused_import_braces, unused_qualifications)]
#![deny(missing_docs)]

extern crate hex;
use std::{convert::TryFrom, path::Path, str};

use std::convert::TryFrom;
use std::path::Path;
use std::str;

use group::GroupEncoding;
use ledger_transport::{APDUCommand, APDUErrorCode, Exchange};
use ledger_zondax_generic::{
App, AppExt, AppInfo, ChunkPayloadType, DeviceInfo, LedgerAppError, Version,
};

use zcash_primitives::consensus::{self, Parameters};
use zcash_primitives::keys::OutgoingViewingKey;
use zcash_primitives::legacy::Script;
use zcash_primitives::memo::MemoBytes as Memo;
use zcash_primitives::merkle_tree::MerklePath;
use zcash_primitives::primitives::{Diversifier, Note, Nullifier, Rseed};
use zcash_primitives::primitives::{PaymentAddress, ProofGenerationKey};
use zcash_primitives::redjubjub::Signature;
use zcash_primitives::sapling::Node;
use zcash_primitives::transaction::components::{Amount, OutPoint};
use zcash_primitives::transaction::Transaction;
use zx_bip44::BIP44Path;
use crate::zcash::primitives::{
consensus::{self, Parameters},
keys::OutgoingViewingKey,
legacy::Script,
memo::MemoBytes as Memo,
merkle_tree::MerklePath,
sapling::{
redjubjub::Signature, Diversifier, Node, Note, Nullifier, PaymentAddress,
ProofGenerationKey, Rseed,
},
transaction::{
components::{Amount, OutPoint},
Transaction,
},
};

use byteorder::{LittleEndian, WriteBytesExt};
use zcash_hsmbuilder::{
data::{
HashSeed, HsmTxData, InitData, OutputBuilderInfo, ShieldedOutputData, ShieldedSpendData,
SpendBuilderInfo, TinData, ToutData, TransparentInputBuilderInfo,
TransparentOutputBuilderInfo,
},
txbuilder::TransactionMetadata,
txbuilder::SaplingMetadata,
};

use byteorder::{LittleEndian, WriteBytesExt};
use group::GroupEncoding;
use sha2::{Digest, Sha256};
use zx_bip44::BIP44Path;

use crate::builder::{Builder, BuilderError};

Expand Down Expand Up @@ -523,7 +523,7 @@ where
input: DataInput,
parameters: P,
branch: consensus::BranchId,
) -> Result<(Transaction, TransactionMetadata), LedgerAppError<E::Error>> {
) -> Result<(Transaction, SaplingMetadata), LedgerAppError<E::Error>> {
log::info!("adding transaction data to builder");
let fee = input.txfee;

Expand All @@ -535,6 +535,10 @@ where
Path::new("../params/sapling-output.params"),
);
log::info!("building the transaction");

// Set up a channel to recieve updates on the progress of building the transaction.
let (tx, _) = tokio::sync::mpsc::channel(10);

let txdata = builder
.build(
self,
Expand All @@ -544,6 +548,7 @@ where
&mut rand_core::OsRng,
0,
branch,
Some(tx),
)
.await
.map_err(|e| LedgerAppError::AppSpecific(0, e.to_string()))?;
Expand Down Expand Up @@ -1052,7 +1057,7 @@ where
///Get a transparent signature from the ledger
pub async fn get_transparent_signature(
&self,
) -> Result<secp256k1::Signature, LedgerAppError<E::Error>> {
) -> Result<secp256k1::ecdsa::Signature, LedgerAppError<E::Error>> {
let command = APDUCommand {
cla: Self::CLA,
ins: INS_EXTRACT_TRANSSIG,
Expand Down Expand Up @@ -1081,7 +1086,7 @@ where

log::info!("Received response {}", response_data.len());

secp256k1::Signature::from_compact(&response_data[0..SIG_SIZE])
secp256k1::ecdsa::Signature::from_compact(&response_data[0..SIG_SIZE])
.map_err(|_| LedgerAppError::InvalidSignature)
}

Expand Down
2 changes: 2 additions & 0 deletions ledger-zcash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub use ledger_zondax_generic::LedgerAppError;
mod app;
pub use app::*;

pub(crate) mod zcash;

/// Ergonomic transaction builder
#[path = "./txbuilder.rs"]
pub mod builder;
36 changes: 23 additions & 13 deletions ledger-zcash/src/txbuilder.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::convert::TryFrom;

use arrayvec::ArrayVec;
use rand_core::{CryptoRng, RngCore};
use zcash_hsmbuilder::{txbuilder::TransactionMetadata, txprover::HsmTxProver};
use zcash_primitives::{
use crate::zcash::primitives::{
consensus::{self, Parameters},
keys::OutgoingViewingKey,
legacy::TransparentAddress,
memo::MemoBytes,
merkle_tree::MerklePath,
primitives::{Diversifier, Note, PaymentAddress},
sapling::Node,
sapling::{Diversifier, Node, Note, PaymentAddress},
transaction::{
components::{Amount, OutPoint, TxOut},
Transaction,
},
};
use zcash_hsmbuilder::{txbuilder::SaplingMetadata, txprover::HsmTxProver};

use arrayvec::ArrayVec;
use rand_core::{CryptoRng, RngCore};
use tokio::sync::mpsc;
use zx_bip44::BIP44Path;

use crate::{
Expand Down Expand Up @@ -72,6 +73,7 @@ pub struct Builder {
transaprent_outputs: ArrayVec<DataTransparentOutput, 5>,
sapling_spends: ArrayVec<DataShieldedSpend, 5>,
sapling_outputs: ArrayVec<DataShieldedOutput, 5>,
change_address: Option<(OutgoingViewingKey, PaymentAddress)>,
}

impl TryFrom<DataInput> for Builder {
Expand Down Expand Up @@ -344,6 +346,14 @@ impl Builder {
}
Ok(self)
}

/// Sets the Sapling address to which any change will be sent.
///
/// By default, change is sent to the Sapling address corresponding to the first note
/// being spent (i.e. the first call to [`Builder::add_sapling_spend`]).
pub fn send_change_to(&mut self, ovk: OutgoingViewingKey, to: PaymentAddress) {
self.change_address = Some((ovk, to))
}
}

impl Builder {
Expand Down Expand Up @@ -375,7 +385,8 @@ impl Builder {
rng: &mut R,
height: u32,
branch: consensus::BranchId,
) -> Result<(Transaction, TransactionMetadata), BuilderError>
progress_notifier: Option<mpsc::Sender<usize>>,
) -> Result<(Transaction, SaplingMetadata), BuilderError>
where
R: RngCore + CryptoRng,
TX: HsmTxProver + Send + Sync,
Expand Down Expand Up @@ -473,7 +484,7 @@ impl Builder {

// building finished, time to have the ledger sign everything
let ledger_data = hsmbuilder
.build(branch, prover)
.build_with_progress_notifier(branch, prover, progress_notifier)
.map_err(|_| BuilderError::FailedToBuildTx)?;

let _signed_hash = app
Expand Down Expand Up @@ -502,13 +513,12 @@ impl Builder {
}

//apply them in the builder
hsmbuilder
.add_signatures_transparant(tsigs, branch)
.map_err(|_| BuilderError::UnableToApplyTransparentSigs)?;
hsmbuilder
let hsmbuilder = hsmbuilder
.add_signatures_spend(zsigs)
.map_err(|_| BuilderError::UnableToApplySaplingSigs)?;

let hsmbuilder = hsmbuilder
.add_signatures_transparent(tsigs)
.map_err(|_| BuilderError::UnableToApplyTransparentSigs)?;
hsmbuilder
.finalize()
.map_err(|_| BuilderError::FinalizationError)
Expand Down
15 changes: 15 additions & 0 deletions ledger-zcash/src/zcash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Wrapper over zcash crates and zecwallet fork of the crates
//!
//! Use this instead of importing things from zcash crates directly!!

cfg_if::cfg_if! {
if #[cfg(all(feature = "zecwallet-compat", feature = "normal-zcash"))] {
compile_error!("Only one feature should be enabled between 'zecwallet-compat' and 'normal-zcash'!");
} else if #[cfg(feature = "zecwallet-compat")] {
pub use zecw_primitives as primitives;
} else if #[cfg(feature = "normal-zcash")] {
pub use zcash_primitives as primitives;
} else {
compile_error!("One feature should be enabled between 'zecwallet-compat' and 'normal-zcash'!");
}
}
Loading