Skip to content

Commit

Permalink
add trezor-client generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Jun 20, 2024
1 parent 904a84c commit e1f4596
Show file tree
Hide file tree
Showing 61 changed files with 102,393 additions and 60 deletions.
55 changes: 44 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ members = [
"wallet/wallet-rpc-daemon", # Wallet RPC daemon binary.
"wallet/wallet-rpc-lib", # Wallet RPC definitions library.
"wallet/wallet-test-node", # Node for wallet testing as a library.
"wallet/trezor-client", # Auto generated trezor client library from trezor firmware repo
"wasm-wrappers", # WASM wrappers for various components.
"wasm-wrappers/wasm-doc-gen", # WASM wrappers documentation generator.
]
Expand Down
2 changes: 1 addition & 1 deletion wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ utils-networking = { path = "../utils/networking" }
utxo = { path = "../utxo" }
wallet-storage = { path = "./storage" }
wallet-types = { path = "./types" }
trezor-client = { path = "../../trezor-firmware/rust/trezor-client", features = ["bitcoin", "mintlayer"], optional = true }
trezor-client = { path = "./trezor-client", features = ["bitcoin", "mintlayer"], optional = true }

bip39 = { workspace = true, default-features = false, features = ["std", "zeroize"] }
hex.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions wallet/src/key_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod with_purpose;
pub use account_key_chain::AccountKeyChainImpl;
use common::chain::classic_multisig::ClassicMultisigChallenge;
use crypto::key::hdkd::u31::U31;
use crypto::key::PublicKey;
use crypto::vrf::VRFKeyKind;
pub use master_key_chain::MasterKeyChain;

Expand Down Expand Up @@ -112,6 +113,15 @@ pub enum FoundPubKey {
Standalone(AccountPublicKey),
}

impl FoundPubKey {
pub fn into_public_key(self) -> PublicKey {
match self {
Self::Hierarchy(xpub) => xpub.into_public_key(),
Self::Standalone(acc_pk) => acc_pk.into_item_id(),
}
}
}

pub trait AccountKeyChains {
fn find_public_key(&self, destination: &Destination) -> Option<FoundPubKey>;

Expand Down
70 changes: 46 additions & 24 deletions wallet/src/signer/trezor_signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use common::{
inputsig::{
arbitrary_message::ArbitraryMessageSignature,
authorize_pubkey_spend::AuthorizedPublicKeySpend,
authorize_pubkeyhash_spend::AuthorizedPublicKeyHashSpend,
standard_signature::StandardInputSignature, InputWitness,
},
sighash::{sighashtype::SigHashType, signature_hash},
Expand All @@ -38,6 +39,7 @@ use crypto::key::{
use itertools::Itertools;
use randomness::make_true_rng;
use serialization::Encode;
#[allow(clippy::all)]
use trezor_client::{
protos::{MintlayerTransferTxOutput, MintlayerUtxoTxInput},
Trezor,
Expand Down Expand Up @@ -112,8 +114,32 @@ impl TrezorSigner {
Some(InputWitness::NoSignature(None)),
SignatureStatus::FullySigned,
)),
Destination::PublicKey(_) | Destination::PublicKeyHash(_) => {
Destination::PublicKeyHash(_) => {
if let Some(signature) = signature {
eprintln!("some signature pkh");
let pk =
key_chain.find_public_key(destination).expect("found").into_public_key();
eprintln!("pk {:?}", pk.encode());
let mut signature = signature.clone();
signature.insert(0, 0);
let sig = Signature::from_data(signature)?;
let sig = AuthorizedPublicKeyHashSpend::new(pk, sig);
let sig = InputWitness::Standard(StandardInputSignature::new(
sighash_type,
sig.encode(),
));

Ok((Some(sig), SignatureStatus::FullySigned))
} else {
eprintln!("empty signature");
Ok((None, SignatureStatus::NotSigned))
}
}
Destination::PublicKey(_) => {
if let Some(signature) = signature {
eprintln!("some signature pk");
let mut signature = signature.clone();
signature.insert(0, 0);
let sig = Signature::from_data(signature)?;
let sig = AuthorizedPublicKeySpend::new(sig);
let sig = InputWitness::Standard(StandardInputSignature::new(
Expand All @@ -123,6 +149,7 @@ impl TrezorSigner {

Ok((Some(sig), SignatureStatus::FullySigned))
} else {
eprintln!("empty signature");
Ok((None, SignatureStatus::NotSigned))
}
}
Expand All @@ -145,26 +172,21 @@ impl TrezorSigner {
.tx()
.outputs()
.iter()
.map(|out| {
match out {
TxOutput::Transfer(value, dest) => {
match value {
OutputValue::Coin(amount) => {
let mut out_req = MintlayerTransferTxOutput::new();
// TODO: change to u128 in trezor
out_req.set_amount(amount.into_atoms() as u64);
out_req.set_address(
Address::new(&self.chain_config, dest.clone())
.expect("addressable")
.into_string(),
);
out_req
}
_ => unimplemented!("support transfer of tokens in trezor"),
}
.map(|out| match out {
TxOutput::Transfer(value, dest) => match value {
OutputValue::Coin(amount) => {
let mut out_req = MintlayerTransferTxOutput::new();
out_req.set_amount(amount.into_atoms().to_be_bytes().to_vec());
out_req.set_address(
Address::new(&self.chain_config, dest.clone())
.expect("addressable")
.into_string(),
);
out_req
}
_ => unimplemented!("support other output types in trezor"),
}
_ => unimplemented!("support transfer of tokens in trezor"),
},
_ => unimplemented!("support other output types in trezor"),
})
.collect();
outputs
Expand Down Expand Up @@ -238,11 +260,13 @@ impl Signer for TrezorSigner {
(Some(destination), Some(sig)) => {
let sighash_type =
SigHashType::try_from(SigHashType::ALL).expect("Should not fail");
eprintln!("making sig for {i}");
let (sig, status) =
self.make_signature(sig, destination, sighash_type, key_chain)?;
Ok((sig, SignatureStatus::NotSigned, status))
}
(Some(_) | None, None) | (None, Some(_)) => {
eprintln!("no signature!");
Ok((None, SignatureStatus::NotSigned, SignatureStatus::NotSigned))
}
},
Expand Down Expand Up @@ -293,8 +317,7 @@ fn to_trezor_input_msgs(ptx: &PartiallySignedTransaction) -> Vec<MintlayerUtxoTx
inp_req.set_prev_index(outpoint.output_index());
match value {
OutputValue::Coin(amount) => {
// TODO:
inp_req.set_amount(amount.into_atoms() as u64);
inp_req.set_amount(amount.into_atoms().to_be_bytes().to_vec());
}
OutputValue::TokenV1(_, _) | OutputValue::TokenV0(_) => {
unimplemented!("support for tokens")
Expand Down Expand Up @@ -328,8 +351,7 @@ fn to_trezor_utxo_msgs(
let mut out_req = MintlayerTransferTxOutput::new();
match value {
OutputValue::Coin(amount) => {
// TODO: change to u128 in trezor
out_req.set_amount(amount.into_atoms() as u64);
out_req.set_amount(amount.into_atoms().to_be_bytes().to_vec());
}
OutputValue::TokenV0(_) | OutputValue::TokenV1(_, _) => {
unimplemented!("support tokens in trezor")
Expand Down
Loading

0 comments on commit e1f4596

Please sign in to comment.