Skip to content

Commit

Permalink
feat: add ledger methods for faucet spending (#6409)
Browse files Browse the repository at this point in the history
Description
---
- Added ledger methods to support faucet spending.
- Consolidated h/w ledger support into the
`minotari_ledger_wallet_comms` crate.
- Added test methods for all h/w ledger functions in the form of an
example.
- Addedd a seperate crate for common types shared between the Ledger
application and the rest of the code base.

~~**Note:** I do not think the ledger feature (`#[cfg(feature =
"ledger")]`) is nesseccary anymore. Any opinions?~~

Motivation and Context
---
Faucet spending was not enabled on ledger devices.

How Has This Been Tested?
---
Compiled the ledger project `minotari_ledger_wallet`, uploaded the
binaries to a ledger nano s device, then ran `cargo run --release
--example ledger_demo`.

```
Transport created in 103.1686ms
Transport created in 5.8315ms
Transport created in 6.0115ms
Transport created in 5.9472ms
Transport created in 5.9368ms
Transport created in 5.8717ms
Transport created in 5.9897ms
Transport created in 6.0287ms
Transport created in 5.9255ms
Transport created in 5.9359ms

Application verified in 1.293162s
Application verified in 200ns
Application verified in 200ns
Application verified in 100ns
Application verified in 200ns
Application verified in 400ns
Application verified in 10.2µs
Application verified in 200ns
Application verified in 100ns
Application verified in 100ns


test: GetAppName
app name:       minotari_ledger_wallet

test: GetVersion
version:        1.0.0-pre.16

test: GetPublicAlpha
public_alpha:   ea10ad3d125beb2c1ebfa2d295ee8e42fd4a85ad831886f8b0b408f8390a9e3c

test: GetPublicKey
public_key:     4a01ab9c27a256827c6ee85373412f80cc88040425afe6301c9b1c7773356e2a

test: GetScriptSignature
script_sig:     (3058744ade0c70a5fe914ed64695c82bc4c71477f8df46578fb3780c06c8110a,5e3fb09be68a31f397330037b6fc5b09227f1a89f74c66fe72fa34c507b90e2b,ed1a3a74f17ae19d70252a2d0ca92a0295f1f0c43565653b4f1fea9ffac32c0d,4f73a5c7cb723b12eca2c3ff57b8156ccacf9ed2790ac2b2e6c033147536e30d,7e8abeb6774fa9e63b181b54e47f09198116d02ec2c1f536c977cf8254d69a09)

test: GetScriptOffset
script_offset:  3a46a979ec9c2d94fbb55e09221fd0deb9e869dc849975e5ee163fb36bbe0709

test: GetViewKey
view_key:       158e9d986d15c40ac3f6178f5947fde0cbd4f71f7de01c00c88cfa570e648e09

test: GetDHSharedSecret
shared_secret:  e85e660bd4d55f4d57784c3592e1ed063c77b8b1aa37431c078e8a7e18c8d23a

test: GetRawSchnorrSignature
signature:      (2971d25b5920bc7a102a9d22c652fb1cf8da85ef08b8464dca6a363612040903,0a7f70764654239c51590d0f32b3b23368e355bb269d99cc151e1c261db56156)

test: GetScriptSchnorrSignature
signature:      (c1fce9daf50cc65e69440f3f4383db1f64b1fd190bdcf7824c9dce0dcd68c208,ace0f952697b2496e60deadee0d40a23c263fa0d73c66a2742767fadad61fe33)

test: Ledger app not running
✔ Exit the 'MinoTari Wallet' Ledger app and press Enter to continue.. · Ok

test: Ledger disconnected
✔ Disconnect the Ledger device and press Enter to continue.. · Ok

test: Ledger reconnected
✔ Reconnect the Ledger device (with password) and press Enter to continue.. · Ok

test: Ledger app restart
✔ Start the 'MinoTari Wallet' Ledger app and press Enter to continue.. · Ok
view_key:       158e9d986d15c40ac3f6178f5947fde0cbd4f71f7de01c00c88cfa570e648e09

Test completed successfully
```

What process can a PR reviewer use to test or verify this change?
---
- Code review.
- Compile the ledger project `minotari_ledger_wallet`, upload the
binaries to a ledger nano s device, then run `cargo run --release
--example ledger_demo`.

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
hansieodendaal authored Jul 24, 2024
1 parent 80efbf2 commit 80acbd2
Show file tree
Hide file tree
Showing 54 changed files with 2,089 additions and 603 deletions.
191 changes: 132 additions & 59 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ members = [
"applications/minotari_merge_mining_proxy",
"applications/minotari_miner",
"applications/minotari_ledger_wallet/comms",
"applications/minotari_ledger_wallet/common",
"integration_tests",
"hashing",
]
Expand Down
3 changes: 1 addition & 2 deletions applications/minotari_console_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ dirs = "5.0"
futures = { version = "^0.3.16", default-features = false, features = [
"alloc",
] }
ledger-transport-hid = { git = "https://github.com/Zondax/ledger-rs", rev = "20e2a20", optional = true }
log = { version = "0.4.8", features = ["std"] }
log4rs = { version = "1.3.0", default-features = false, features = [
"config_parsing",
Expand Down Expand Up @@ -89,7 +88,7 @@ tari_features = { path = "../../common/tari_features", version = "1.0.0-pre.18"
[features]
default = ["libtor", "ledger"]
grpc = []
ledger = ["ledger-transport-hid", "minotari_ledger_wallet_comms"]
ledger = ["minotari_ledger_wallet_comms"]
libtor = ["tari_libtor"]

[package.metadata.cargo-machete]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ pub async fn command_runner(

let mut script_signature = Signature::default();
match key_manager_service
.sign_with_nonce_and_message(
.sign_with_nonce_and_challenge(
&party_info.wallet_spend_key_id,
&party_info.script_nonce_key_id,
&challenge,
Expand Down Expand Up @@ -1007,7 +1007,7 @@ pub async fn command_runner(

let mut metadata_signature = Signature::default();
match key_manager_service
.sign_with_nonce_and_message(
.sign_with_nonce_and_challenge(
&party_info.sender_offset_key_id,
&party_info.sender_offset_nonce_key_id,
&challenge,
Expand Down
82 changes: 14 additions & 68 deletions applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,10 @@

use std::{fs, io, path::PathBuf, str::FromStr, sync::Arc, time::Instant};

#[cfg(feature = "ledger")]
use ledger_transport_hid::{hidapi::HidApi, TransportNativeHID};
use log::*;
use minotari_app_utilities::{consts, identity_management::setup_node_identity};
#[cfg(feature = "ledger")]
use minotari_ledger_wallet_comms::ledger_wallet::LedgerCommands;
#[cfg(feature = "ledger")]
use minotari_ledger_wallet_comms::{
error::LedgerDeviceError,
ledger_wallet::{get_transport, Instruction},
};
use minotari_ledger_wallet_comms::accessor_methods::{ledger_get_public_spend_key, ledger_get_view_key};
use minotari_wallet::{
error::{WalletError, WalletStorageError},
output_manager_service::storage::database::OutputManagerDatabase,
Expand Down Expand Up @@ -833,66 +826,19 @@ pub fn prompt_wallet_type(
};
if prompt(connected_hardware_msg) {
print!("Scanning for connected Ledger hardware device... ");
match get_transport() {
Ok(hid) => {
println!("Device found.");
let account = prompt_ledger_account(boot_mode).expect("An account value");
let ledger = LedgerWallet::new(account, wallet_config.network, None, None);
match ledger
.build_command(Instruction::GetPublicAlpha, vec![])
.execute_with_transport(&hid)
{
Ok(result) => {
debug!(target: LOG_TARGET, "result length: {}, data: {:?}", result.data().len(), result.data());
if result.data().len() < 33 {
debug!(target: LOG_TARGET, "result less than 33");
panic!(
"'get_public_key' insufficient data - expected 33 got {} bytes ({:?})",
result.data().len(),
result
);
}

let public_alpha = match PublicKey::from_canonical_bytes(&result.data()[1..33]) {
Ok(k) => k,
Err(e) => panic!("{}", e),
};

match ledger
.build_command(Instruction::GetViewKey, vec![])
.execute_with_transport(&hid)
{
Ok(result) => {
debug!(target: LOG_TARGET, "result length: {}, data: {:?}", result.data().len(), result.data());
if result.data().len() < 33 {
debug!(target: LOG_TARGET, "result less than 33");
panic!(
"'get_view_key' insufficient data - expected 33 got {} bytes \
({:?})",
result.data().len(),
result
);
}

let view_key = match PrivateKey::from_canonical_bytes(&result.data()[1..33])
{
Ok(k) => k,
Err(e) => panic!("{}", e),
};

let ledger = LedgerWallet::new(
account,
wallet_config.network,
Some(public_alpha),
Some(view_key),
);
Some(WalletType::Ledger(ledger))
},
Err(e) => panic!("{}", e),
}
},
Err(e) => panic!("{}", e),
}
let account = prompt_ledger_account(boot_mode).expect("An account value");
match ledger_get_public_spend_key(account) {
Ok(public_alpha) => match ledger_get_view_key(account) {
Ok(view_key) => {
let ledger = LedgerWallet::new(
account,
wallet_config.network,
Some(public_alpha),
Some(view_key),
);
Some(WalletType::Ledger(ledger))
},
Err(e) => panic!("{}", e),
},
Err(e) => panic!("{}", e),
}
Expand Down
8 changes: 8 additions & 0 deletions applications/minotari_ledger_wallet/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "minotari_ledger_wallet_common"
version = "1.0.0-pre.16"
authors = ["The Tari Development Community"]
license = "BSD-3-Clause"
edition = "2021"

[dependencies]
Loading

0 comments on commit 80acbd2

Please sign in to comment.