Skip to content

Commit

Permalink
fix: claim n of m faucet (tari-project#6389)
Browse files Browse the repository at this point in the history
Description
---
Fixes n-of-m spending of faucets. 

Motivation and Context
---
Allows spending of m-of-n

How Has This Been Tested?
---
Manual

---------

Co-authored-by: Hansie Odendaal <[email protected]>
  • Loading branch information
SWvheerden and hansieodendaal authored Jul 11, 2024
1 parent c0c27a5 commit 2700a46
Show file tree
Hide file tree
Showing 18 changed files with 482 additions and 338 deletions.
242 changes: 88 additions & 154 deletions applications/minotari_console_wallet/src/automation/commands.rs

Large diffs are not rendered by default.

43 changes: 24 additions & 19 deletions applications/minotari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,11 @@ pub enum CliCommands {
GetBalance,
SendMinotari(SendMinotariArgs),
BurnMinotari(BurnMinotariArgs),
CreateKeyPair(CreateKeyPairArgs),
CreateAggregateSignatureUtxo(CreateAggregateSignatureUtxoArgs),
EncumberAggregateUtxo(EncumberAggregateUtxoArgs),
SpendAggregateUtxo(SpendAggregateUtxoArgs),
SignMessage(SignMessageArgs),
FaucetEncumberAggregateUtxo(FaucetEncumberAggregateUtxoArgs),
FaucetSpendAggregateUtxo(FaucetSpendAggregateUtxoArgs),
FaucetCreatePartyDetails(FaucetCreatePartyDetailsArgs),
CreateScriptSig(CreateScriptSigArgs),
CreateMetaSig(CreateMetaSigArgs),
FaucetCreateScriptSig(FaucetCreateScriptSigArgs),
FaucetCreateMetaSig(FaucetCreateMetaSigArgs),
SendOneSidedToStealthAddress(SendMinotariArgs),
MakeItRain(MakeItRainArgs),
CoinSplit(CoinSplitArgs),
Expand Down Expand Up @@ -169,13 +166,13 @@ pub struct BurnMinotariArgs {
}

#[derive(Debug, Args, Clone)]
pub struct CreateKeyPairArgs {
pub struct FaucetCreateKeyPairArgs {
#[clap(long)]
pub key_branch: String,
}

#[derive(Debug, Args, Clone)]
pub struct CreateAggregateSignatureUtxoArgs {
pub struct FaucetCreateAggregateSignatureUtxoArgs {
#[clap(long)]
pub amount: MicroMinotari,
#[clap(long)]
Expand All @@ -197,22 +194,24 @@ pub struct FaucetCreatePartyDetailsArgs {
#[clap(long)]
pub commitment: String,
#[clap(long)]
pub destination: TariAddress,
pub recipient_address: TariAddress,
}

#[derive(Debug, Args, Clone)]
pub struct SignMessageArgs {
pub struct FaucetSignMessageArgs {
#[clap(long)]
pub private_key_id: TariKeyId,
#[clap(long)]
pub challenge: String,
}

#[derive(Debug, Args, Clone)]
pub struct EncumberAggregateUtxoArgs {
pub struct FaucetEncumberAggregateUtxoArgs {
#[clap(long)]
pub fee_per_gram: MicroMinotari,
#[clap(long)]
pub commitment: String,
#[clap(long)]
pub output_hash: String,
#[clap(long)]
pub script_input_shares: Vec<UniSignature>,
Expand All @@ -231,7 +230,7 @@ pub struct EncumberAggregateUtxoArgs {
}

#[derive(Debug, Args, Clone)]
pub struct SpendAggregateUtxoArgs {
pub struct FaucetSpendAggregateUtxoArgs {
#[clap(long)]
pub tx_id: u64,
#[clap(long)]
Expand All @@ -243,11 +242,11 @@ pub struct SpendAggregateUtxoArgs {
}

#[derive(Debug, Args, Clone)]
pub struct CreateScriptSigArgs {
pub struct FaucetCreateScriptSigArgs {
#[clap(long)]
pub private_key_id: TariKeyId,
#[clap(long)]
pub secret_nonce: TariKeyId,
pub secret_nonce_key_id: TariKeyId,
#[clap(long)]
pub input_script: String,
#[clap(long)]
Expand All @@ -263,13 +262,13 @@ pub struct CreateScriptSigArgs {
}

#[derive(Debug, Args, Clone)]
pub struct CreateMetaSigArgs {
pub struct FaucetCreateMetaSigArgs {
#[clap(long)]
pub secret_script_key: TariKeyId,
pub secret_script_key_id: TariKeyId,
#[clap(long)]
pub secret_sender_offset_key: TariKeyId,
pub secret_sender_offset_key_id: TariKeyId,
#[clap(long)]
pub secret_nonce: TariKeyId,
pub secret_nonce_key_id: TariKeyId,
#[clap(long)]
pub ephemeral_commitment: String,
#[clap(long)]
Expand All @@ -278,6 +277,12 @@ pub struct CreateMetaSigArgs {
pub total_meta_key: UniPublicKey,
#[clap(long)]
pub commitment: String,
#[clap(long)]
pub encrypted_data: String,
#[clap(long)]
pub output_features: String,
#[clap(long)]
pub recipient_address: TariAddress,
}

#[derive(Debug, Args, Clone)]
Expand Down
79 changes: 32 additions & 47 deletions applications/minotari_console_wallet/src/wallet_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,10 @@ mod test {
burn-minotari --message Ups_these_funds_will_be_burned! 100T
create-key-pair --key-branch pie
create-aggregate-signature-utxo \
--amount 125T \
--fee-per-gram 1 \
--n 3 \
--m 2 \
--message ff \
--maturity 0 \
--public-keys=5c4f2a4b3f3f84e047333218a84fd24f581a9d7e4f23b78e3714e9d174427d61 \
--public-keys=f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665
sign-message \
--private-key-id imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--challenge f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665
encumber-aggregate-utxo \
faucet-encumber-aggregate-utxo \
--fee-per-gram 1 \
--output-hash f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665 \
--commitment f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665 \
--script-input-shares=3ddde10d0775c20fb25015546c6a8068812044e7ca4ee1057e84ec9ab6705d03,8a55d1cb503be36875d38f2dc6abac7b23445bbd7253684a1506f5ee1855cd58 \
--script-input-shares=3edf1ed103b0ac0bbad6a6de8369808d14dfdaaf294fe660646875d749a1f908,50a26c646db951720c919f59cd7a34600a7fc3ee978c64fbcce0ad184c46844c \
--script-public-key-shares=5c4f2a4b3f3f84e047333218a84fd24f581a9d7e4f23b78e3714e9d174427d61 \
Expand All @@ -529,7 +514,7 @@ mod test {
--dh-shared-secret-shares=f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665 \
--recipient-address f4LR9f6WwwcPiKJjK5ciTkU1ocNhANa3FPw1wkyVUwbuKpgiihawCXy6PFszunUWQ4Te8KVFnyWVHHwsk9x5Cg7ZQiA
spend-aggregate-utxo \
faucet-spend-aggregate-utxo \
--tx-id 12345678 \
--meta-signatures=3ddde10d0775c20fb25015546c6a8068812044e7ca4ee1057e84ec9ab6705d03,8a55d1cb503be36875d38f2dc6abac7b23445bbd7253684a1506f5ee1855cd58 \
--meta-signatures=3edf1ed103b0ac0bbad6a6de8369808d14dfdaaf294fe660646875d749a1f908,50a26c646db951720c919f59cd7a34600a7fc3ee978c64fbcce0ad184c46844c \
Expand All @@ -538,24 +523,31 @@ mod test {
--script-offset-keys=5c4f2a4b3f3f84e047333218a84fd24f581a9d7e4f23b78e3714e9d174427d61 \
--script-offset-keys=f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665
create-script-sig \
faucet-create-party-details \
--commitment f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665 \
--recipient-address f4LR9f6WwwcPiKJjK5ciTkU1ocNhANa3FPw1wkyVUwbuKpgiihawCXy6PFszunUWQ4Te8KVFnyWVHHwsk9x5Cg7ZQiA
faucet-create-script-sig \
--private-key-id imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--secret-nonce imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--secret-nonce-key-id imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--input-script ae010268593ed2d36a2d95f0ffe0f41649b97cc36fc4ef0c8ecd6bd28f9d56c76b793b08691435a5c813578f8a7f4973166dc1c6c15f37aec2a7d65b1583c8b2129364c916d5986a0c1b3dac7d6efb94bed688ba52fa8b962cf27c0446e2fea6d66a04 \
--input-stack 050857c14f72cf885aac9f08c9484cb7cb06b6cc20eab68c9bee1e8d5a85649b0a6d31c5cc49afc1e03ebbcf55c82f47e8cbc796c33e96c17a31eab027ee821f00 \
--ephemeral-commitment f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665 \
--ephemeral-pubkey 8a55d1cb503be36875d38f2dc6abac7b23445bbd7253684a1506f5ee1855cd58 \
--total-script-key 5c4f2a4b3f3f84e047333218a84fd24f581a9d7e4f23b78e3714e9d174427d61 \
--commitment 94966b4f1b5dc050df1109cf07a516ae85912c82503b1a8c1625986a569fae67
create-meta-sig \
--secret-script-key imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--secret-sender-offset-key imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--secret-nonce imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
faucet-create-meta-sig \
--secret-script-key-id imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--secret-sender-offset-key-id imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--secret-nonce-key-id imported.96159b07298a453c9f514f5307f70659c7561dd6d9ed376854c5cb573cb2e311 \
--ephemeral-commitment f6b2ca781342a3ebe30ee1643655c96f1d7c14f4d49f077695395de98ae73665 \
--ephemeral-pubkey 8a55d1cb503be36875d38f2dc6abac7b23445bbd7253684a1506f5ee1855cd58 \
--total-meta-key 5c4f2a4b3f3f84e047333218a84fd24f581a9d7e4f23b78e3714e9d174427d61 \
--commitment 94966b4f1b5dc050df1109cf07a516ae85912c82503b1a8c1625986a569fae67
--commitment 94966b4f1b5dc050df1109cf07a516ae85912c82503b1a8c1625986a569fae67 \
--encrypted-data 6a7aa2053ae187f60f27df0e10184bf93d02a84cd9548320ec7da546185fc23c6daa720974007c6106cfb0361eb9828e1af979b69fff724d2bcd0d86d5b9675ef1f65b424b22bee06e52fcaf4fd2a2ed \
--output-features 'features' \
--recipient-address f4FB7HhYCmLw4PsivjG8bAgUuxyPS6GTjFkhMWx6d9Nv4aoBESyaH5TdS1dAkSCg4qXqehpjZU9QrSUP2Ec7v4Gj8wf
coin-split --message Make_many_dust_UTXOs! --fee-per-gram 2 0.001T 499
Expand All @@ -576,13 +568,11 @@ mod test {
let mut get_balance = false;
let mut send_tari = false;
let mut burn_tari = false;
let mut create_key_pair = false;
let mut create_aggregate_signature_utxo = false;
let mut encumber_aggregate_utxo = false;
let mut spend_aggregate_utxo = false;
let mut sign_message = false;
let mut create_script_sig = false;
let mut create_meta_sig = false;
let mut faucet_encumber_aggregate_utxo = false;
let mut faucet_spend_aggregate_utxo = false;
let mut faucet_create_party_details = false;
let mut faucet_create_script_sig = false;
let mut faucet_create_meta_sig = false;
let mut make_it_rain = false;
let mut coin_split = false;
let mut discover_peer = false;
Expand All @@ -594,13 +584,11 @@ mod test {
CliCommands::GetBalance => get_balance = true,
CliCommands::SendMinotari(_) => send_tari = true,
CliCommands::BurnMinotari(_) => burn_tari = true,
CliCommands::CreateKeyPair(_) => create_key_pair = true,
CliCommands::CreateAggregateSignatureUtxo(_) => create_aggregate_signature_utxo = true,
CliCommands::EncumberAggregateUtxo(_) => encumber_aggregate_utxo = true,
CliCommands::SpendAggregateUtxo(_) => spend_aggregate_utxo = true,
CliCommands::SignMessage(_) => sign_message = true,
CliCommands::CreateScriptSig(_) => create_script_sig = true,
CliCommands::CreateMetaSig(_) => create_meta_sig = true,
CliCommands::FaucetEncumberAggregateUtxo(_) => faucet_encumber_aggregate_utxo = true,
CliCommands::FaucetSpendAggregateUtxo(_) => faucet_spend_aggregate_utxo = true,
CliCommands::FaucetCreatePartyDetails(_) => faucet_create_party_details = true,
CliCommands::FaucetCreateScriptSig(_) => faucet_create_script_sig = true,
CliCommands::FaucetCreateMetaSig(_) => faucet_create_meta_sig = true,
CliCommands::SendOneSidedToStealthAddress(_) => {},
CliCommands::MakeItRain(_) => make_it_rain = true,
CliCommands::CoinSplit(_) => coin_split = true,
Expand Down Expand Up @@ -628,20 +616,17 @@ mod test {
CliCommands::RevalidateWalletDb => {},
CliCommands::RegisterValidatorNode(_) => {},
CliCommands::CreateTlsCerts => {},
CliCommands::FaucetCreatePartyDetails(_) => {},
}
}
assert!(
get_balance &&
send_tari &&
burn_tari &&
create_key_pair &&
create_aggregate_signature_utxo &&
encumber_aggregate_utxo &&
spend_aggregate_utxo &&
sign_message &&
create_script_sig &&
create_meta_sig &&
faucet_encumber_aggregate_utxo &&
faucet_spend_aggregate_utxo &&
faucet_create_party_details &&
faucet_create_script_sig &&
faucet_create_meta_sig &&
make_it_rain &&
coin_split &&
discover_peer &&
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/transactions/aggregated_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl AggregateBody {
pub fn update_script_signature(
&mut self,
commitment: &Commitment,
script_signature: &ComAndPubSignature,
script_signature: ComAndPubSignature,
) -> Result<(), TransactionError> {
let input = self
.inputs
Expand All @@ -119,7 +119,7 @@ impl AggregateBody {
Err(_) => false,
})
.ok_or(TransactionError::OutputNotFound(commitment.to_hex()))?;
input.script_signature = script_signature.clone();
input.script_signature = script_signature;

Ok(())
}
Expand Down
39 changes: 31 additions & 8 deletions base_layer/core/src/transactions/key_manager/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use tari_key_manager::{
KeyManagerServiceError,
},
};
use tari_script::CheckSigSchnorrSignature;
use tari_utilities::{hex::Hex, ByteArray};
use tokio::sync::RwLock;

Expand Down Expand Up @@ -181,6 +182,13 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
Ok((key_id, key))
}

pub async fn get_random_key(&self) -> Result<(TariKeyId, PublicKey), KeyManagerServiceError> {
let random_private_key = PrivateKey::random(&mut OsRng);
let key_id = self.import_key(random_private_key).await?;
let public_key = self.get_public_key_at_key_id(&key_id).await?;
Ok((key_id, public_key))
}

pub async fn create_key_pair(&mut self, branch: &str) -> Result<(TariKeyId, PublicKey), KeyManagerServiceError> {
self.add_key_manager_branch(branch)?;
let (key_id, public_key) = self.get_next_key(branch).await?;
Expand Down Expand Up @@ -1148,21 +1156,22 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
&commitment,
ephemeral_commitment,
txo_version,
None,
None,
metadata_signature_message,
)
.await?;
let metadata_signature = &receiver_partial_metadata_signature + &sender_partial_metadata_signature;
Ok(metadata_signature)
}

pub async fn sign_message(
pub async fn sign_script_message(
&self,
private_key_id: &TariKeyId,
challenge: &[u8],
) -> Result<Signature, TransactionError> {
) -> Result<CheckSigSchnorrSignature, TransactionError> {
let private_key = self.get_private_key(private_key_id).await?;
let nonce = PrivateKey::random(&mut OsRng);
let signature = Signature::sign_with_nonce_and_message(&private_key, nonce, challenge)?;
let signature = CheckSigSchnorrSignature::sign(&private_key, challenge, &mut OsRng)?;

Ok(signature)
}
Expand All @@ -1171,11 +1180,11 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
&self,
private_key_id: &TariKeyId,
nonce: &TariKeyId,
challenge: &[u8],
challenge: &[u8; 64],
) -> Result<Signature, TransactionError> {
let private_key = self.get_private_key(private_key_id).await?;
let private_nonce = self.get_private_key(nonce).await?;
let signature = Signature::sign_with_nonce_and_message(&private_key, private_nonce, challenge)?;
let signature = Signature::sign_raw_uniform(&private_key, private_nonce, challenge)?;

Ok(signature)
}
Expand Down Expand Up @@ -1214,6 +1223,8 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
&commitment,
ephemeral_commitment,
txo_version,
None,
None,
metadata_signature_message,
)
.await?;
Expand Down Expand Up @@ -1262,21 +1273,33 @@ where TBackend: KeyManagerBackend<PublicKey> + 'static
Ok(metadata_signature)
}

// In the case where the sender is an aggregated signer, we need to parse in the total public key shares, this is
// done in: aggregated_sender_offset_public_keys and aggregated_ephemeral_public_keys. If there is no aggregated
// signers, this can be left as none
pub async fn get_sender_partial_metadata_signature(
&self,
ephemeral_private_nonce_id: &TariKeyId,
sender_offset_key_id: &TariKeyId,
commitment: &Commitment,
ephemeral_commitment: &Commitment,
txo_version: &TransactionOutputVersion,
aggregated_sender_offset_public_keys: Option<&PublicKey>,
aggregated_ephemeral_public_keys: Option<&PublicKey>,
metadata_signature_message: &[u8; 32],
) -> Result<ComAndPubSignature, TransactionError> {
match &self.wallet_type {
WalletType::Software => {
let ephemeral_private_key = self.get_private_key(ephemeral_private_nonce_id).await?;
let ephemeral_pubkey = PublicKey::from_secret_key(&ephemeral_private_key);
let ephemeral_pubkey = match aggregated_ephemeral_public_keys {
Some(agg) => agg.clone(),
None => PublicKey::from_secret_key(&ephemeral_private_key),
};
PublicKey::from_secret_key(&ephemeral_private_key);
let sender_offset_private_key = self.get_private_key(sender_offset_key_id).await?; // Take the index and use it to find the key from ledger
let sender_offset_public_key = PublicKey::from_secret_key(&sender_offset_private_key);
let sender_offset_public_key = match aggregated_sender_offset_public_keys {
Some(agg) => agg.clone(),
None => PublicKey::from_secret_key(&sender_offset_private_key),
};

let challenge = TransactionOutput::finalize_metadata_signature_challenge(
txo_version,
Expand Down
Loading

0 comments on commit 2700a46

Please sign in to comment.