Skip to content

Commit

Permalink
feat: add pre-mine spend (#6442)
Browse files Browse the repository at this point in the history
Description
---
Added pre-mine spend with improved logging for fault finding

Motivation and Context
---
Pre-mine spending did not work.

How Has This Been Tested?
---
A new esmeralda genesis block that includes pre-mine with a 2-of-3
scheme and a backup spending wallet was created, utilizing 4x ledger
console wallets. Those wallets were then used to spend the 1st pre-mine
UTXO. The UTXO was successfully mined and imported into the target
wallet.

Leader's ledger console wallet console:
```
PS C:\Users\pluto\.tari-Hansie\node_01> .\minotari_console_wallet.exe --base-path . --network esmeralda --config $pwd"\config\config.toml" --log-config $pwd"\config\log4rs_console_wallet.yml" pre-mine-spend-session-info --fee-per-gram 5 --output-index 0 --recipient-address f27jozcSQjY2QfopvAeuNMDJX5z9T6vThdBRSteaanwecG1F31ez7vUUCVCihFL6UrJHLVY9EPxkRrgHJLwdPJNxtz4 --verify-unspent-outputs
Initializing logging according to "C:\\Users\\pluto\\.tari-Hansie\\node_01\\config\\log4rs_console_wallet.yml"
Minotari Console Wallet running... (Command mode started)
==============
Command Runner
==============

1. PreMineSpendSessionInfo(PreMineSpendSessionInfoArgs { fee_per_gram: MicroMinotari(5), output_index: 0, recipient_address: Dual(DualAddress { network: Esmeralda, features: TariAddressFeatures(1), public_view_key: 4c5b2793fc2504965138bebf97862af0865ff249e3b8e69b06d441dab0f70346, public_spend_key: b492d5ddf85688c452765180b6e18cc31a0a3ee401ec275d67118145d97cbd67 }), verify_unspent_outputs: true })


Concluded step 1 'pre-mine-generate-session-info'
Your session ID is:                 '8Tj5eMbr7Z3NXUs3'
Your session's output directory is: 'C:\Users\pluto\Documents\tari_pre_mine\spend\8Tj5eMbr7Z3NXUs3'
Session info saved to:              'C:\Users\pluto\Documents\tari_pre_mine\spend\8Tj5eMbr7Z3NXUs3\step_1_session_info.json'
Send 'step_1_session_info.json' to parties for step 2

Minotari Console Wallet running... (Command mode completed)

Shutting down wallet... Done.
PS C:\Users\pluto\.tari-Hansie\node_01> .\minotari_console_wallet.exe --base-path . --network esmeralda --config $pwd"\config\config.toml" --log-config $pwd"\config\log4rs_console_wallet.yml" pre-mine-spend-encumber-aggregate-utxo --session-id 8Tj5eMbr7Z3NXUs3 --input-file-names=step_2_for_leader_from_carol.json
Initializing logging according to "C:\\Users\\pluto\\.tari-Hansie\\node_01\\config\\log4rs_console_wallet.yml"
Minotari Console Wallet running... (Command mode started)
==============
Command Runner
==============

1. PreMineSpendEncumberAggregateUtxo(PreMineSpendEncumberAggregateUtxoArgs { session_id: "8Tj5eMbr7Z3NXUs3", input_file_names: ["step_2_for_leader_from_carol.json"] })


Concluded step 3 'pre-mine-encumber-aggregate-utxo'
Send 'step_3_for_parties.json' to parties for step 4

Minotari Console Wallet running... (Command mode completed)

Shutting down wallet... Done.
PS C:\Users\pluto\.tari-Hansie\node_01> .\minotari_console_wallet.exe --base-path . --network esmeralda --config $pwd"\config\config.toml" --log-config $pwd"\config\log4rs_console_wallet.yml" pre-mine-spend-aggregate-transaction --session-id 8Tj5eMbr7Z3NXUs3 --input-file-names=step_4_for_leader_from_carol.json
Initializing logging according to "C:\\Users\\pluto\\.tari-Hansie\\node_01\\config\\log4rs_console_wallet.yml"
Minotari Console Wallet running... (Command mode started)
==============
Command Runner
==============

1. PreMineSpendAggregateTransaction(PreMineSpendAggregateTransactionArgs { session_id: "8Tj5eMbr7Z3NXUs3", input_file_names: ["step_4_for_leader_from_carol.json"] })


Concluded step 5 'pre-mine-spend-aggregate-utxo'

Minotari Console Wallet running... (Command mode completed)

Shutting down wallet... Done.
PS C:\Users\pluto\.tari-Hansie\node_01>
```
Party member's ledger console wallet console:
```
PS C:\Users\pluto\.tari-Hansie\node_03> .\minotari_console_wallet.exe --base-path . --network esmeralda --config $pwd"\config\config.toml" --log-config $pwd"\config\log4rs_console_wallet.yml" pre-mine-spend-party-details --alias carol --output-index 0 --input-file "C:\Users\pluto\Documents\tari_pre_mine\spend\8Tj5eMbr7Z3NXUs3\step_1_session_info.json"
Initializing logging according to "C:\\Users\\pluto\\.tari-Hansie\\node_03\\config\\log4rs_console_wallet.yml"
Minotari Console Wallet running... (Command mode started)
==============
Command Runner
==============

1. PreMineSpendPartyDetails(PreMineSpendPartyDetailsArgs { input_file: "C:\\Users\\pluto\\Documents\\tari_pre_mine\\spend\\8Tj5eMbr7Z3NXUs3\\step_1_session_info.json", output_index: 0, alias: "carol" })


Concluded step 2 'pre-mine-create-party-details'
Your session's output directory is 'C:\Users\pluto\Documents\tari_pre_mine\spend\8Tj5eMbr7Z3NXUs3'
Send 'step_2_for_leader_from_carol.json' to leader for step 3

Minotari Console Wallet running... (Command mode completed)

Shutting down wallet... Done.
PS C:\Users\pluto\.tari-Hansie\node_03> .\minotari_console_wallet.exe --base-path . --network esmeralda --config $pwd"\config\config.toml" --log-config $pwd"\config\log4rs_console_wallet.yml" pre-mine-spend-input-output-sigs --session-id 8Tj5eMbr7Z3NXUs3
Initializing logging according to "C:\\Users\\pluto\\.tari-Hansie\\node_03\\config\\log4rs_console_wallet.yml"
Minotari Console Wallet running... (Command mode started)
==============
Command Runner
==============

1. PreMineSpendInputOutputSigs(PreMineSpendInputOutputSigArgs { session_id: "8Tj5eMbr7Z3NXUs3" })


Concluded step 4 'pre-mine-create-input-output-sigs'
Send 'step_4_for_leader_from_carol.json' to leader for step 5

Minotari Console Wallet running... (Command mode completed)

Shutting down wallet... Done.
PS C:\Users\pluto\.tari-Hansie\node_03>
```

What process can a PR reviewer use to test or verify this change?
---
Code review
System-level testing

<!-- 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 Aug 5, 2024
1 parent 900a107 commit 0018126
Show file tree
Hide file tree
Showing 10 changed files with 723 additions and 370 deletions.
163 changes: 56 additions & 107 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions applications/minotari_ledger_wallet/common/src/common_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ pub enum Instruction {
GetAppName = 0x02,
GetPublicSpendKey = 0x03,
GetPublicKey = 0x04,
GetScriptSignature = 0x05,
GetScriptSignatureDerived = 0x05,
GetScriptOffset = 0x06,
GetViewKey = 0x07,
GetDHSharedSecret = 0x08,
GetRawSchnorrSignature = 0x09,
GetScriptSchnorrSignature = 0x10,
GetOneSidedMetadataSignature = 0x11,
GetScriptSignatureManaged = 0x12,
}

impl Instruction {
Expand All @@ -96,13 +97,14 @@ impl Instruction {
0x02 => Some(Instruction::GetAppName),
0x03 => Some(Instruction::GetPublicSpendKey),
0x04 => Some(Instruction::GetPublicKey),
0x05 => Some(Instruction::GetScriptSignature),
0x05 => Some(Instruction::GetScriptSignatureDerived),
0x06 => Some(Instruction::GetScriptOffset),
0x07 => Some(Instruction::GetViewKey),
0x08 => Some(Instruction::GetDHSharedSecret),
0x09 => Some(Instruction::GetRawSchnorrSignature),
0x10 => Some(Instruction::GetScriptSchnorrSignature),
0x11 => Some(Instruction::GetOneSidedMetadataSignature),
0x12 => Some(Instruction::GetScriptSignatureManaged),
_ => None,
}
}
Expand Down Expand Up @@ -224,12 +226,14 @@ mod test {
(0x02, Instruction::GetAppName),
(0x03, Instruction::GetPublicSpendKey),
(0x04, Instruction::GetPublicKey),
(0x05, Instruction::GetScriptSignature),
(0x05, Instruction::GetScriptSignatureDerived),
(0x06, Instruction::GetScriptOffset),
(0x07, Instruction::GetViewKey),
(0x08, Instruction::GetDHSharedSecret),
(0x09, Instruction::GetRawSchnorrSignature),
(0x10, Instruction::GetScriptSchnorrSignature),
(0x11, Instruction::GetOneSidedMetadataSignature),
(0x12, Instruction::GetScriptSignatureManaged),
];

for (expected_byte, instruction) in &mappings {
Expand All @@ -250,7 +254,7 @@ mod test {
assert_eq!(instruction.as_byte(), *expected_byte);
assert_eq!(Instruction::from_byte(*expected_byte), Some(*instruction));
},
Instruction::GetScriptSignature => {
Instruction::GetScriptSignatureDerived => {
assert_eq!(instruction.as_byte(), *expected_byte);
assert_eq!(Instruction::from_byte(*expected_byte), Some(*instruction));
},
Expand Down Expand Up @@ -278,6 +282,10 @@ mod test {
assert_eq!(instruction.as_byte(), *expected_byte);
assert_eq!(Instruction::from_byte(*expected_byte), Some(*instruction));
},
Instruction::GetScriptSignatureManaged => {
assert_eq!(instruction.as_byte(), *expected_byte);
assert_eq!(Instruction::from_byte(*expected_byte), Some(*instruction));
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ ledger-transport-hid = { git = "https://github.com/Zondax/ledger-rs", rev = "20e
serde = { version = "1.0.106", features = ["derive"] }
thiserror = "1.0.26"

rand = "0.9.0-alpha.1"
rand = "0.8"
once_cell = "1.19.0"
log = "0.4.20"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

//! # Multi-party Ledger - command line example
/// This example demonstrates how to use the Ledger Nano S/X for the Tari wallet. In order to run the example, you
/// need to have the `MinoTari Wallet` application installed on your Ledger device. For that, please follow the
/// instructions in the [README](../../wallet/README.md) file.
/// With this example, you can:
/// - Detect the hardware wallet
/// - Verify that the Ledger application is installed and the version is correct
/// - TBD
///
/// -----------------------------------------------------------------------------------------------
/// Example use:
/// `cargo run --release --example ledger_demo`
/// -----------------------------------------------------------------------------------------------
use dialoguer::{theme::ColorfulTheme, Select};
use minotari_ledger_wallet_comms::{
accessor_methods::{
Expand All @@ -17,24 +29,12 @@ use minotari_ledger_wallet_comms::{
ledger_get_version,
ledger_get_view_key,
verify_ledger_application,
ScriptSignatureKey,
},
error::LedgerDeviceError,
ledger_wallet::get_transport,
};
use rand::rngs::OsRng;
/// This example demonstrates how to use the Ledger Nano S/X for the Tari wallet. In order to run the example, you
/// need to have the `MinoTari Wallet` application installed on your Ledger device. For that, please follow the
/// instructions in the [README](../../wallet/README.md) file.
/// With this example, you can:
/// - Detect the hardware wallet
/// - Verify that the Ledger application is installed and the version is correct
/// - TBD
///
/// -----------------------------------------------------------------------------------------------
/// Example use:
/// `cargo run --release --example ledger_demo`
/// -----------------------------------------------------------------------------------------------
use rand::RngCore;
use rand::{rngs::OsRng, RngCore};
use tari_common::configuration::Network;
use tari_common_types::{
key_branches::TransactionKeyManagerBranch,
Expand Down Expand Up @@ -148,51 +148,66 @@ fn main() {
println!("\ntest: GetScriptSignature");
let network = Network::LocalNet;
let version = 0u8;
let branch_key = get_random_nonce();
let value = PrivateKey::from(123456);
let spend_private_key = get_random_nonce();
let commitment = Commitment::from_public_key(&PublicKey::from_secret_key(&get_random_nonce()));
let mut script_message = [0u8; 32];
script_message.copy_from_slice(&get_random_nonce().to_vec());

match ledger_get_script_signature(
account,
network,
version,
&branch_key,
&value,
&spend_private_key,
&commitment,
script_message,
) {
Ok(signature) => println!(
"script_sig: ({},{},{},{},{})",
signature.ephemeral_commitment().to_hex(),
signature.ephemeral_pubkey().to_hex(),
signature.u_x().to_hex(),
signature.u_a().to_hex(),
signature.u_y().to_hex()
),
Err(e) => {
println!("\nError: {}\n", e);
return;
for branch_key in [
ScriptSignatureKey::Derived {
branch_key: get_random_nonce(),
},
ScriptSignatureKey::Managed {
branch: TransactionKeyManagerBranch::Spend,
index: OsRng.next_u64(),
},
] {
match ledger_get_script_signature(
account,
network,
version,
&branch_key,
&value,
&spend_private_key,
&commitment,
script_message,
) {
Ok(signature) => println!(
"script_sig: ({},{},{},{},{})",
signature.ephemeral_commitment().to_hex(),
signature.ephemeral_pubkey().to_hex(),
signature.u_x().to_hex(),
signature.u_a().to_hex(),
signature.u_y().to_hex()
),
Err(e) => {
println!("\nError: {}\n", e);
return;
},
}
}

// GetScriptOffset
println!("\ntest: GetScriptOffset");
let total_script_private_key = PrivateKey::default();
let mut derived_key_commitments = Vec::new();
let partial_script_offset = PrivateKey::default();
let mut derived_script_keys = Vec::new();
let mut script_key_indexes = Vec::new();
let mut derived_sender_offsets = Vec::new();
let mut sender_offset_indexes = Vec::new();
for _i in 0..5 {
derived_key_commitments.push(get_random_nonce());
sender_offset_indexes.push(OsRng.next_u64());
derived_script_keys.push(get_random_nonce());
script_key_indexes.push((TransactionKeyManagerBranch::Spend, OsRng.next_u64()));
derived_sender_offsets.push(get_random_nonce());
sender_offset_indexes.push((TransactionKeyManagerBranch::OneSidedSenderOffset, OsRng.next_u64()));
}

match ledger_get_script_offset(
account,
&total_script_private_key,
&derived_key_commitments,
&partial_script_offset,
&derived_script_keys,
&script_key_indexes,
&derived_sender_offsets,
&sender_offset_indexes,
) {
Ok(script_offset) => println!("script_offset: {}", script_offset.to_hex()),
Expand Down
Loading

0 comments on commit 0018126

Please sign in to comment.