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 all 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"
42 changes: 27 additions & 15 deletions ledger-zcash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ledger-zcash"
description = "Library to integrate with the Ledger Zcash app"
version = "0.5.0"
version = "0.6.0"
license = "Apache-2.0"
authors = ["Zondax GmbH <hello@zondax.ch>"]
homepage = "https://github.com/Zondax/ledger-zcash-rs"
Expand All @@ -15,29 +15,41 @@ 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 }
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"] }
educe = "0.4.19"

[dev-dependencies]
futures = "0.3.21"
Expand Down
67 changes: 41 additions & 26 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 @@ -168,10 +168,13 @@ impl<E> ZcashApp<E> {

///Data needed to handle transparent input for sapling transaction
///Contains information needed for both ledger and builder
#[derive(educe::Educe)]
#[educe(Debug)]
pub struct DataTransparentInput {
///BIP44 path for transparent input key derivation
pub path: BIP44Path,
///Public key belonging to the secret key (of the BIP44 path)
#[educe(Debug(trait = "std::fmt::Display"))]
pub pk: secp256k1::PublicKey,
///UTXO of transparent input
pub prevout: OutPoint,
Expand Down Expand Up @@ -203,6 +206,7 @@ impl DataTransparentInput {
}

///Data needed to handle transparent output for sapling transaction
#[derive(Debug)]
pub struct DataTransparentOutput {
///The transparent output value
pub value: Amount,
Expand All @@ -229,7 +233,7 @@ impl DataTransparentOutput {
}

///Data needed to handle shielded spend for sapling transaction
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct DataShieldedSpend {
///ZIP32 path (last non-constant value)
pub path: u32,
Expand All @@ -243,7 +247,8 @@ pub struct DataShieldedSpend {
}

impl DataShieldedSpend {
fn address(&self) -> PaymentAddress {
/// Reetrieve the PaymentAddress that the note was paid to
pub fn address(&self) -> PaymentAddress {
PaymentAddress::from_parts(self.diversifier, self.note.pk_d)
//if we have a note then pk_d is not the identity
.expect("pk_d not identity")
Expand Down Expand Up @@ -279,8 +284,11 @@ impl DataShieldedSpend {
}

///Data needed to handle shielded output for sapling transaction
#[derive(educe::Educe)]
#[educe(Debug)]
pub struct DataShieldedOutput {
///address of shielded output
#[educe(Debug(method = "crate::zcash::payment_address_bytes_fmt"))]
pub address: PaymentAddress,
///value send to that address
pub value: Amount,
Expand Down Expand Up @@ -320,6 +328,7 @@ impl DataShieldedOutput {
}

///Data needed for sapling transaction
#[derive(Debug)]
pub struct DataInput {
///transaction fee.
/// Note: Ledger only supports fees of 10000 or 1000
Expand Down Expand Up @@ -523,7 +532,8 @@ where
input: DataInput,
parameters: P,
branch: consensus::BranchId,
) -> Result<(Transaction, TransactionMetadata), LedgerAppError<E::Error>> {
target_height: u32,
) -> Result<(Transaction, SaplingMetadata), LedgerAppError<E::Error>> {
log::info!("adding transaction data to builder");
let fee = input.txfee;

Expand All @@ -535,15 +545,20 @@ 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,
parameters,
&prover,
fee,
&mut rand_core::OsRng,
0,
target_height,
branch,
Some(tx),
)
.await
.map_err(|e| LedgerAppError::AppSpecific(0, e.to_string()))?;
Expand Down Expand Up @@ -1052,7 +1067,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 +1096,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;
Loading