Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Jun 27, 2024
1 parent a3074c5 commit 76caf29
Show file tree
Hide file tree
Showing 18 changed files with 962 additions and 405 deletions.
2 changes: 1 addition & 1 deletion applications/minotari_app_utilities/src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl From<UniPublicKey> for PublicKey {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum UniNodeId {
PublicKey(PublicKey),
NodeId(NodeId),
Expand Down
163 changes: 91 additions & 72 deletions applications/minotari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use minotari_wallet::{
WalletConfig,
WalletSqlite,
};
use rand::rngs::OsRng;
use serde::{de::DeserializeOwned, Serialize};
use sha2::Sha256;
use tari_common_types::{
Expand All @@ -71,6 +70,7 @@ use tari_core::{
encrypted_data::PaymentId,
EncryptedData,
OutputFeatures,
RangeProofType,
Transaction,
TransactionInput,
TransactionInputVersion,
Expand All @@ -81,7 +81,7 @@ use tari_core::{
},
},
};
use tari_crypto::{keys::SecretKey, ristretto::RistrettoSecretKey};
use tari_crypto::ristretto::RistrettoSecretKey;
use tari_script::{script, ExecutionStack, TariScript};
use tari_utilities::{hex::Hex, ByteArray};
use tokio::{
Expand Down Expand Up @@ -147,38 +147,50 @@ pub async fn create_aggregate_signature_utxo(
m: u8,
public_keys: Vec<PublicKey>,
message: String,
maturity: u64,
) -> Result<(TxId, FixedHash), CommandError> {
let mut msg = [0u8; 32];
msg.copy_from_slice(message.as_bytes());

wallet_transaction_service
.create_aggregate_signature_utxo(amount, fee_per_gram, n, m, public_keys, msg)
.create_aggregate_signature_utxo(amount, fee_per_gram, n, m, public_keys, msg, maturity)
.await
.map_err(CommandError::TransactionServiceError)
}

/// encumbers a n-of-m transaction
#[allow(clippy::too_many_arguments)]
async fn encumber_aggregate_utxo(
mut wallet_transaction_service: TransactionServiceHandle,
fee_per_gram: MicroMinotari,
output_hash: String,
signatures: Vec<Signature>,
total_script_pubkey: PublicKey,
total_offset_pubkey: PublicKey,
total_signature_nonce: PublicKey,
metadata_signature_nonce: PublicKey,
wallet_script_secret_key: String,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_signature_shares: Vec<Signature>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
dh_shared_secret_shares: Vec<PublicKey>,
recipient_address: TariAddress,
payment_id: PaymentId,
maturity: u64,
range_proof_type: RangeProofType,
minimum_value_promise: MicroMinotari,
) -> Result<(TxId, Transaction, PublicKey), CommandError> {
wallet_transaction_service
.encumber_aggregate_utxo(
fee_per_gram,
output_hash,
signatures,
total_script_pubkey,
total_offset_pubkey,
total_signature_nonce,
metadata_signature_nonce,
wallet_script_secret_key,
script_input_shares,
script_public_key_shares,
script_signature_shares,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
dh_shared_secret_shares,
recipient_address,
payment_id,
maturity,
range_proof_type,
minimum_value_promise,
)
.await
.map_err(CommandError::TransactionServiceError)
Expand Down Expand Up @@ -321,18 +333,6 @@ pub async fn coin_split(
Ok(tx_id)
}

pub fn sign_message(private_key: String, challenge: String) -> Result<Signature, CommandError> {
let private_key =
PrivateKey::from_hex(private_key.as_str()).map_err(|e| CommandError::InvalidArgument(e.to_string()))?;
let challenge = challenge.as_bytes();

let nonce = PrivateKey::random(&mut OsRng);
let signature = Signature::sign_with_nonce_and_message(&private_key, nonce, challenge)
.map_err(CommandError::FailedSignature)?;

Ok(signature)
}

async fn wait_for_comms(connectivity_requester: &ConnectivityRequester) -> Result<(), CommandError> {
let mut connectivity = connectivity_requester.get_event_subscription();
print!("Waiting for connectivity... ");
Expand Down Expand Up @@ -740,12 +740,12 @@ pub async fn command_runner(
}
},
CreateKeyPair(args) => match key_manager_service.create_key_pair(args.key_branch).await {
Ok((sk, pk)) => {
Ok((key_id, pk)) => {
println!(
"New key pair:
1. secret key: {},
1. key id : {},
2. public key: {}",
sk.to_hex(),
key_id,
pk.to_hex()
)
},
Expand All @@ -761,7 +761,8 @@ pub async fn command_runner(
.iter()
.map(|pk| PublicKey::from(pk.clone()))
.collect::<Vec<_>>(),
args.message,
args.message, // 1. What is the message? => commitment
args.maturity,
)
.await
{
Expand All @@ -777,45 +778,59 @@ pub async fn command_runner(
},
Err(e) => eprintln!("CreateAggregateSignatureUtxo error! {}", e),
},
SignMessage(args) => match sign_message(args.private_key, args.challenge) {
Ok(sgn) => {
println!(
"Sign message:
SignMessage(args) => {
match key_manager_service
.sign_message(&args.private_key_id, args.challenge.as_bytes())
.await
{
// 1. What is the message/challenge? => commitment
Ok(sgn) => {
println!(
"Sign message:
1. signature: {},
2. public nonce: {}",
sgn.get_signature().to_hex(),
sgn.get_public_nonce().to_hex(),
)
},
Err(e) => eprintln!("SignMessage error! {}", e),
sgn.get_signature().to_hex(),
sgn.get_public_nonce().to_hex(),
)
},
Err(e) => eprintln!("SignMessage error! {}", e),
}
},
EncumberAggregateUtxo(args) => {
let mut total_script_pub_key = PublicKey::default();
for sig in args.script_pubkeys {
total_script_pub_key = sig.into();
}
let mut total_offset_pub_key = PublicKey::default();
for sig in args.offset_pubkeys {
total_offset_pub_key = sig.into();
}
let mut total_sig_nonce = PublicKey::default();
for sig in args.script_signature_nonces {
total_sig_nonce = sig.into();
}
let mut total_meta_nonce = PublicKey::default();
for sig in args.metadata_signature_nonces {
total_meta_nonce = sig.into();
}
match encumber_aggregate_utxo(
transaction_service.clone(),
args.fee_per_gram,
args.output_hash,
args.signatures.iter().map(|sgn| sgn.clone().into()).collect::<Vec<_>>(),
total_script_pub_key,
total_offset_pub_key,
total_sig_nonce,
total_meta_nonce,
args.wallet_script_secret_key,
args.script_input_shares
.iter()
.map(|v| v.clone().into())
.collect::<Vec<_>>(),
args.script_public_key_shares
.iter()
.map(|v| v.clone().into())
.collect::<Vec<_>>(),
args.script_signature_shares
.iter()
.map(|v| v.clone().into())
.collect::<Vec<_>>(),
args.sender_offset_public_key_shares
.iter()
.map(|v| v.clone().into())
.collect::<Vec<_>>(),
args.metadata_ephemeral_public_key_shares
.iter()
.map(|v| v.clone().into())
.collect::<Vec<_>>(),
args.dh_shared_secret_shares
.iter()
.map(|v| v.clone().into())
.collect::<Vec<_>>(),
args.recipient_address,
PaymentId::from_bytes(args.payment_id.as_bytes())
.map_err(|e| CommandError::InvalidArgument(e.to_string()))?,
args.maturity,
args.range_proof_type,
args.minimum_value_promise,
)
.await
{
Expand Down Expand Up @@ -893,8 +908,6 @@ pub async fn command_runner(
}
},
CreateScriptSig(args) => {
let private_key =
PrivateKey::from_hex(&args.secret_key).map_err(|e| CommandError::InvalidArgument(e.to_string()))?;
let private_nonce = PrivateKey::from_hex(&args.secret_nonce)
.map_err(|e| CommandError::InvalidArgument(e.to_string()))?;
let script = TariScript::from_hex(&args.input_script)
Expand All @@ -916,16 +929,22 @@ pub async fn command_runner(
&args.total_script_key.into(),
&commitment,
);
// TODO: Change to `ComAndPubSignature`
let signature = Signature::sign_with_nonce_and_message(&private_key, private_nonce, challenge)
.map_err(CommandError::FailedSignature)?;
println!(
"Sign script sig:

match key_manager_service
.sign_with_nonce_and_message(&args.private_key_id, &private_nonce, challenge.as_slice())
.await
{
Ok(signature) => {
println!(
"Sign script sig:
1. signature: {},
2. public nonce: {}",
signature.get_signature().to_hex(),
signature.get_public_nonce().to_hex(),
)
signature.get_signature().to_hex(),
signature.get_public_nonce().to_hex(),
)
},
Err(e) => eprintln!("SignMessage error! {}", e),
}
},
CreateMetaSig(args) => {
let private_key = PrivateKey::from_hex(&args.secret_offset_key)
Expand Down
35 changes: 26 additions & 9 deletions applications/minotari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ use minotari_app_utilities::{
use tari_common::configuration::{ConfigOverrideProvider, Network};
use tari_common_types::tari_address::TariAddress;
use tari_comms::multiaddr::Multiaddr;
use tari_core::transactions::{tari_amount, tari_amount::MicroMinotari};
use tari_core::transactions::{
key_manager::TariKeyId,
tari_amount,
tari_amount::MicroMinotari,
transaction_components::RangeProofType,
};
use tari_key_manager::SeedWords;
use tari_utilities::{
hex::{Hex, HexError},
Expand Down Expand Up @@ -186,13 +191,15 @@ pub struct CreateAggregateSignatureUtxoArgs {
#[clap(long)]
pub message: String,
#[clap(long)]
pub maturity: u64,
#[clap(long)]
pub public_keys: Vec<UniPublicKey>,
}

#[derive(Debug, Args, Clone)]
pub struct SignMessageArgs {
#[clap(long)]
pub private_key: String,
pub private_key_id: TariKeyId,
#[clap(long)]
pub challenge: String,
}
Expand All @@ -204,17 +211,27 @@ pub struct EncumberAggregateUtxoArgs {
#[clap(long)]
pub output_hash: String,
#[clap(long)]
pub wallet_script_secret_key: String,
pub script_input_shares: Vec<UniSignature>,
#[clap(long)]
pub script_public_key_shares: Vec<UniPublicKey>,
#[clap(long)]
pub script_signature_shares: Vec<UniSignature>,
#[clap(long)]
pub sender_offset_public_key_shares: Vec<UniPublicKey>,
#[clap(long)]
pub metadata_ephemeral_public_key_shares: Vec<UniPublicKey>,
#[clap(long)]
pub dh_shared_secret_shares: Vec<UniPublicKey>,
#[clap(long)]
pub script_pubkeys: Vec<UniPublicKey>,
pub recipient_address: TariAddress,
#[clap(long)]
pub offset_pubkeys: Vec<UniPublicKey>,
pub payment_id: String,
#[clap(long)]
pub script_signature_nonces: Vec<UniPublicKey>,
pub maturity: u64,
#[clap(long)]
pub metadata_signature_nonces: Vec<UniPublicKey>,
pub range_proof_type: RangeProofType,
#[clap(long)]
pub signatures: Vec<UniSignature>,
pub minimum_value_promise: MicroMinotari,
}

#[derive(Debug, Args, Clone)]
Expand All @@ -232,7 +249,7 @@ pub struct SpendAggregateUtxoArgs {
#[derive(Debug, Args, Clone)]
pub struct CreateScriptSigArgs {
#[clap(long)]
pub secret_key: String,
pub private_key_id: TariKeyId,
#[clap(long)]
pub secret_nonce: String,
#[clap(long)]
Expand Down
Loading

0 comments on commit 76caf29

Please sign in to comment.