Skip to content

Commit

Permalink
fix script dependance one party order
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jul 11, 2024
1 parent 2700a46 commit c9cd54c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 66 deletions.
33 changes: 18 additions & 15 deletions applications/minotari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
collections::HashMap,
convert::TryInto,
fs,
fs::File,
Expand Down Expand Up @@ -85,7 +86,7 @@ use tari_core::{
};
use tari_crypto::ristretto::{pedersen::PedersenCommitment, RistrettoSecretKey};
use tari_key_manager::key_manager_service::KeyManagerInterface;
use tari_script::{script, ExecutionStack, TariScript};
use tari_script::{script, CheckSigSchnorrSignature, ExecutionStack, TariScript};
use tari_utilities::{hex::Hex, ByteArray};
use tokio::{
sync::{broadcast, mpsc},
Expand Down Expand Up @@ -144,13 +145,13 @@ pub async fn burn_tari(

/// encumbers a n-of-m transaction
#[allow(clippy::too_many_arguments)]
#[allow(clippy::mutable_key_type)]
async fn encumber_aggregate_utxo(
mut wallet_transaction_service: TransactionServiceHandle,
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand All @@ -163,7 +164,6 @@ async fn encumber_aggregate_utxo(
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand Down Expand Up @@ -751,8 +751,7 @@ pub async fn command_runner(

println!(
"Party details created with:
1. script input signature: ({},{}),
2. wallet public spend key: {},
1. script input share: ({},{},{}),
3. wallet public spend key_id: {},
4. spend nonce key_id: {},
5. public spend nonce key: {},
Expand All @@ -761,9 +760,9 @@ pub async fn command_runner(
8. sender offset nonce key_id: {},
9. public sender offset nonce key: {},
10. public shared secret: {}",
wallet_public_spend_key,
script_input_signature.get_signature().to_hex(),
script_input_signature.get_public_nonce().to_hex(),
wallet_public_spend_key,
wallet_spend_key_id,
script_nonce_key_id,
public_script_nonce,
Expand All @@ -775,19 +774,23 @@ pub async fn command_runner(
);
},
FaucetEncumberAggregateUtxo(args) => {
#[allow(clippy::mutable_key_type)]
let mut input_shares = HashMap::new();
for share in args.script_input_shares {
let data = share.split(',').collect::<Vec<_>>();
let public_key = PublicKey::from_hex(data[0])?;
let signature = PrivateKey::from_hex(data[1])?;
let public_nonce = PublicKey::from_hex(data[2])?;
let sig = CheckSigSchnorrSignature::new(public_nonce, signature);
input_shares.insert(public_key, sig);
}

match encumber_aggregate_utxo(
transaction_service.clone(),
args.fee_per_gram,
args.output_hash,
Commitment::from_hex(&args.commitment)?,
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<_>>(),
input_shares,
args.script_signature_public_nonces
.iter()
.map(|v| v.clone().into())
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_console_wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub struct FaucetEncumberAggregateUtxoArgs {
#[clap(long)]
pub output_hash: String,
#[clap(long)]
pub script_input_shares: Vec<UniSignature>,
pub script_input_shares: Vec<String>,
#[clap(long)]
pub script_public_key_shares: Vec<UniPublicKey>,
#[clap(long)]
Expand Down
14 changes: 6 additions & 8 deletions base_layer/wallet/src/output_manager_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{fmt, fmt::Formatter, sync::Arc};
use std::{collections::HashMap, fmt, fmt::Formatter, sync::Arc};

use tari_common_types::{
tari_address::TariAddress,
transaction::TxId,
types::{Commitment, FixedHash, HashOutput, PublicKey, Signature},
types::{Commitment, FixedHash, HashOutput, PublicKey},
};
use tari_core::{
covenants::Covenant,
Expand All @@ -38,7 +38,7 @@ use tari_core::{
},
};
use tari_crypto::ristretto::pedersen::PedersenCommitment;
use tari_script::TariScript;
use tari_script::{CheckSigSchnorrSignature, TariScript};
use tari_service_framework::reply_channel::SenderService;
use tari_utilities::hex::Hex;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -66,8 +66,7 @@ pub enum OutputManagerRequest {
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand Down Expand Up @@ -759,14 +758,14 @@ impl OutputManagerHandle {
}
}

#[allow(clippy::mutable_key_type)]
pub async fn encumber_aggregate_utxo(
&mut self,
tx_id: TxId,
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand All @@ -791,7 +790,6 @@ impl OutputManagerHandle {
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand Down
39 changes: 21 additions & 18 deletions base_layer/wallet/src/output_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{convert::TryInto, fmt, sync::Arc};
use std::{collections::HashMap, convert::TryInto, fmt, sync::Arc};

use blake2::Blake2b;
use diesel::result::{DatabaseErrorKind, Error as DieselError};
Expand All @@ -32,7 +32,7 @@ use tari_common::configuration::Network;
use tari_common_types::{
tari_address::TariAddress,
transaction::TxId,
types::{BlockHash, Commitment, FixedHash, HashOutput, PrivateKey, PublicKey, Signature},
types::{BlockHash, Commitment, FixedHash, HashOutput, PrivateKey, PublicKey},
};
use tari_comms::{types::CommsDHKE, NodeIdentity};
use tari_core::{
Expand Down Expand Up @@ -252,7 +252,6 @@ where
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand All @@ -265,7 +264,6 @@ where
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand Down Expand Up @@ -1176,14 +1174,14 @@ where

/// Create a partial transaction in order to prepare output
#[allow(clippy::too_many_lines)]
#[allow(clippy::mutable_key_type)]
pub async fn encumber_aggregate_utxo(
&mut self,
tx_id: TxId,
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
mut script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand Down Expand Up @@ -1233,6 +1231,7 @@ where
.iter()
.fold(tari_common_types::types::PublicKey::default(), |acc, x| acc + x);
let encryption_private_key = public_key_to_output_encryption_key(&sum_public_keys)?;
let mut aggregated_script_public_key_shares = PublicKey::default();
// Decrypt the output secrets and create a new input as WalletOutput (unblinded)
let input = if let Ok((amount, spending_key, payment_id)) =
EncryptedData::decrypt_data(&encryption_private_key, &output.commitment, &output.encrypted_data)
Expand All @@ -1250,15 +1249,21 @@ where
.key_manager
.sign_script_message(&self.resources.wallet_identity.wallet_node_key_id, &script_challange)
.await?;
script_signatures.push(StackItem::Signature(CheckSigSchnorrSignature::new(
self_signature.get_public_nonce().clone(),
self_signature.get_signature().clone(),
)));
for signature in &script_input_shares {
script_signatures.push(StackItem::Signature(CheckSigSchnorrSignature::new(
signature.get_public_nonce().clone(),
signature.get_signature().clone(),
)));
script_input_shares.insert(
self.resources.wallet_identity.address.public_spend_key().clone(),
self_signature,
);

// the order here is important, we need to add the signatures in the same order as public keys where
// added to the script originally
for key in public_keys {
if let Some(signature) = script_input_shares.get(&key) {
script_signatures.push(StackItem::Signature(signature.clone()));
// our own key should not be added yet, it will be added with the script signing
if &key != self.resources.wallet_identity.address.public_spend_key() {
aggregated_script_public_key_shares = aggregated_script_public_key_shares + key;
}
}
}
let spending_key_id = self.resources.key_manager.import_key(spending_key).await?;
WalletOutput::new_with_rangeproof(
Expand Down Expand Up @@ -1462,9 +1467,7 @@ where
let aggregated_script_signature_public_nonces = script_signature_public_nonces
.iter()
.fold(PublicKey::default(), |acc, x| acc + x);
let aggregated_script_public_key_shares = script_public_key_shares
.iter()
.fold(PublicKey::default(), |acc, x| acc + x);

// Update the input's script signature
let (updated_input, total_script_public_key) = input
.to_transaction_input_with_multi_party_script_signature(
Expand Down
26 changes: 10 additions & 16 deletions base_layer/wallet/src/transaction_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use tari_core::{
},
};
use tari_crypto::ristretto::pedersen::PedersenCommitment;
use tari_script::CheckSigSchnorrSignature;
use tari_service_framework::reply_channel::SenderService;
use tari_utilities::hex::Hex;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -114,8 +115,7 @@ pub enum TransactionServiceRequest {
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand Down Expand Up @@ -233,7 +233,6 @@ impl fmt::Display for TransactionServiceRequest {
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand All @@ -242,24 +241,20 @@ impl fmt::Display for TransactionServiceRequest {
..
} => f.write_str(&format!(
"Creating encumber n-of-m utxo with: fee_per_gram = {}, output_hash = {}, commitment = {}, \
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 = {}",
script_input_shares = {:?},, script_signature_shares = {:?}, sender_offset_public_key_shares = {:?}, \
metadata_ephemeral_public_key_shares = {:?}, dh_shared_secret_shares = {:?}, recipient_address = {}",
fee_per_gram,
output_hash,
expected_commitment.to_hex(),
script_input_shares
.iter()
.map(|v| format!(
"(sig: {}, nonce: {})",
v.get_signature().to_hex(),
v.get_public_nonce().to_hex()
"(public_key: {}, sig: {}, nonce: {})",
v.0.to_hex(),
v.1.get_signature().to_hex(),
v.1.get_public_nonce().to_hex()
))
.collect::<Vec<String>>(),
script_public_key_shares
.iter()
.map(|v| v.to_hex())
.collect::<Vec<String>>(),
script_signature_public_nonces
.iter()
.map(|v| format!("(public nonce: {})", v.to_hex(),))
Expand Down Expand Up @@ -731,13 +726,13 @@ impl TransactionServiceHandle {
}
}

#[allow(clippy::mutable_key_type)]
pub async fn encumber_aggregate_utxo(
&mut self,
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand All @@ -751,7 +746,6 @@ impl TransactionServiceHandle {
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand Down
8 changes: 3 additions & 5 deletions base_layer/wallet/src/transaction_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ use tari_script::{
push_pubkey_script,
script,
slice_to_boxed_message,
CheckSigSchnorrSignature,
ExecutionStack,
ScriptContext,
TariScript,
Expand Down Expand Up @@ -723,7 +724,6 @@ where
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand All @@ -735,7 +735,6 @@ where
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand Down Expand Up @@ -1377,13 +1376,13 @@ where
}

/// Creates an encumbered uninitialized transaction
#[allow(clippy::mutable_key_type)]
pub async fn encumber_aggregate_tx(
&mut self,
fee_per_gram: MicroMinotari,
output_hash: String,
expected_commitment: PedersenCommitment,
script_input_shares: Vec<Signature>,
script_public_key_shares: Vec<PublicKey>,
script_input_shares: HashMap<PublicKey, CheckSigSchnorrSignature>,
script_signature_public_nonces: Vec<PublicKey>,
sender_offset_public_key_shares: Vec<PublicKey>,
metadata_ephemeral_public_key_shares: Vec<PublicKey>,
Expand All @@ -1401,7 +1400,6 @@ where
output_hash,
expected_commitment,
script_input_shares,
script_public_key_shares,
script_signature_public_nonces,
sender_offset_public_key_shares,
metadata_ephemeral_public_key_shares,
Expand Down
Loading

0 comments on commit c9cd54c

Please sign in to comment.