Skip to content

Commit

Permalink
limit for tari script
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Aug 19, 2024
1 parent d4f9875 commit 7510db8
Show file tree
Hide file tree
Showing 49 changed files with 289 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ pub async fn command_runner(
&TransactionOutputVersion::get_current_version(),
&script!(PushPubKey(Box::new(
session_info.recipient_address.public_spend_key().clone()
))),
)))?,
&leader_info.output_features,
&leader_info.sender_offset_pubkey,
&leader_info.metadata_signature_ephemeral_commitment,
Expand Down
3 changes: 3 additions & 0 deletions applications/minotari_console_wallet/src/automation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use tari_common_types::types::FixedHashSizeError;
use tari_core::transactions::{tari_amount::MicroMinotariError, transaction_components::TransactionError};
use tari_crypto::signatures::SchnorrSignatureError;
use tari_key_manager::key_manager_service::KeyManagerServiceError;
use tari_script::ScriptError;
use tari_utilities::{hex::HexError, ByteArrayError};
use thiserror::Error;
use tokio::task::JoinError;
Expand Down Expand Up @@ -92,6 +93,8 @@ pub enum CommandError {
GrpcTlsError(#[from] GrpcTlsError),
#[error("Invalid signature: `{0}`")]
FailedSignature(#[from] SchnorrSignatureError),
#[error("Tari script error: {0}")]
ScriptError(#[from] ScriptError),
}

impl From<HexError> for CommandError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
.await
.map_err(|e| Status::internal(e.to_string()))?;

output = output.with_script(script![Nop]);
output = output.with_script(script![Nop].map_err(|e| Status::internal(e.to_string()))?);

let (tx_id, transaction) = output_manager
.create_send_to_self_with_output(vec![output], fee_per_gram.into(), UtxoSelectionCriteria::default())
Expand Down
1 change: 1 addition & 0 deletions applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
tari_crypto = { version = "0.20.2", default-features = false }
tari_utilities = { version = "0.7" }

tari_common = { path = "../../../common" }
tari_common_types = { path = "../../../base_layer/common_types" }
tari_script = { path = "../../../infrastructure/tari_script" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ pub fn ledger_get_one_sided_metadata_signature(

// Ensure that the serialized script produce expected results
let test_key = RistrettoPublicKey::from_secret_key(&RistrettoSecretKey::random(&mut OsRng));
let script = script!(PushPubKey(Box::new(test_key.clone())));
let script = script!(PushPubKey(Box::new(test_key.clone())))?;
let mut serialized_script = Vec::new();
script
.serialize(&mut serialized_script)
Expand Down
3 changes: 3 additions & 0 deletions applications/minotari_ledger_wallet/comms/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use serde::{Deserialize, Serialize};
use tari_crypto::tari_utilities::ByteArrayError;
use tari_script::ScriptError;
use thiserror::Error;

/// Ledger device errors.
Expand Down Expand Up @@ -50,6 +51,8 @@ pub enum LedgerDeviceError {
NotSupported,
#[error("User cancelled the transaction")]
UserCancelled,
#[error("Tari script error: {0}")]
ScriptError(#[from] ScriptError),
}

impl From<ByteArrayError> for LedgerDeviceError {
Expand Down
12 changes: 8 additions & 4 deletions applications/minotari_ledger_wallet/comms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,27 @@ mod test {
fn test_push_pub_key_serialized_byte_representation() {
let mut scripts = Vec::new();

scripts.push((script!(Nop), NOP_IDENTIFIER, "".to_string()));
scripts.push((script!(PushOne), PUSH_ONE_IDENTIFIER, "".to_string()));
scripts.push((script!(Nop).unwrap(), NOP_IDENTIFIER, "".to_string()));
scripts.push((script!(PushOne).unwrap(), PUSH_ONE_IDENTIFIER, "".to_string()));

for pub_key in [
RistrettoPublicKey::default(),
RistrettoPublicKey::from_secret_key(&RistrettoSecretKey::random(&mut OsRng)),
] {
scripts.push((
script!(PushPubKey(Box::new(pub_key.clone()))),
script!(PushPubKey(Box::new(pub_key.clone()))).unwrap(),
PUSH_PUBKEY_IDENTIFIER,
pub_key.to_hex(),
));
}

let key = RistrettoSecretKey::random(&mut OsRng);
let msg = slice_to_boxed_message(key.as_bytes());
scripts.push((script!(CheckSigVerify(msg)), CHECK_SIG_VERIFY_IDENTIFIER, key.to_hex()));
scripts.push((
script!(CheckSigVerify(msg)).unwrap(),
CHECK_SIG_VERIFY_IDENTIFIER,
key.to_hex(),
));

for (script, hex_identifier, hex_payload) in scripts {
let mut serialized = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/blocks/pre_mine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ pub async fn create_pre_mine_genesis_block_info(
Else
PushPubKey(Box::new(backup_key.clone()))
EndIf
);
).map_err(|e| e.to_string())?;
let output = WalletOutputBuilder::new(item.value, commitment_mask.key_id)
.with_features(OutputFeatures::new(
OutputFeaturesVersion::get_current_version(),
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

use blake2::Blake2b;
use digest::consts::U64;
#[cfg(feature = "base_node")]
use tari_hashing::ConfidentialOutputHashDomain;
use tari_max_size::MaxSizeVec;

use crate::consensus::DomainSeparatedConsensusHasher;

Expand Down Expand Up @@ -68,6 +66,8 @@ impl BanReason {
}
}

#[cfg(feature = "base_node")]
use tari_max_size::MaxSizeVec;
/// AuxChainHashes is a vector of limited size
#[cfg(feature = "base_node")]
pub type AuxChainHashes = MaxSizeVec<monero::Hash, 128>;
2 changes: 1 addition & 1 deletion base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl ConsensusConstants {
let features_and_scripts_size = self.transaction_weight.round_up_features_and_scripts_size(
output_features.get_serialized_size()? +
CoinBaseExtra::default().max_size() +
script![Nop].get_serialized_size()?,
script![Nop].map_err(|e| e.to_std_io_error())?.get_serialized_size()?,
);
Ok(self.transaction_weight.calculate(1, 0, 1, features_and_scripts_size))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod tests {
let network = Network::get_current_or_user_setting_or_default();

// Script is chosen because the consensus encoding impl for TariScript has 2 writes
let test_subject = script!(Nop);
let test_subject = script!(Nop).unwrap();
let mut hasher = Blake2b::<U32>::default();
TestHashDomain::add_domain_separation_tag(&mut hasher, &format!("{}.n{}", "foo", network.as_byte()));

Expand Down
6 changes: 5 additions & 1 deletion base_layer/core/src/covenants/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ mod test {
CovenantArg::Hash(FixedHash::zero()),
&from_hex("010000000000000000000000000000000000000000000000000000000000000000").unwrap(),
);
test_case(CovenantArg::TariScript(script!(Nop)), &[ARG_TARI_SCRIPT, 0x01, 0x73]);
test_case(CovenantArg::TariScript(script!(Nop).unwrap()), &[
ARG_TARI_SCRIPT,
0x01,
0x73,
]);
test_case(CovenantArg::OutputField(OutputField::Covenant), &[
ARG_OUTPUT_FIELD,
FIELD_COVENANT,
Expand Down
10 changes: 5 additions & 5 deletions base_layer/core/src/covenants/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ mod test {
sidechain_feature: Some(side_chain_features),
..Default::default()
},
script: script![Drop Nop],
script: script![Drop Nop].unwrap(),
..Default::default()
},
&key_manager,
Expand Down Expand Up @@ -455,7 +455,7 @@ mod test {
output_type: OutputType::Burn,
..Default::default()
},
script: script![Drop Nop],
script: script![Drop Nop].unwrap(),
minimum_value_promise: MicroMinotari(123456),
value: MicroMinotari(123456),
..Default::default()
Expand All @@ -466,7 +466,7 @@ mod test {
.remove(0);

assert!(!OutputField::Commitment.is_eq(&output, &Commitment::default()).unwrap());
assert!(!OutputField::Script.is_eq(&output, &script![Nop Drop]).unwrap());
assert!(!OutputField::Script.is_eq(&output, &script![Nop Drop].unwrap()).unwrap());
assert!(!OutputField::SenderOffsetPublicKey
.is_eq(&output, &PublicKey::default())
.unwrap());
Expand Down Expand Up @@ -504,7 +504,7 @@ mod test {
maturity: 42,
..Default::default()
},
script: script![Drop Nop],
script: script![Drop Nop].unwrap(),
..Default::default()
},
&key_manager,
Expand Down Expand Up @@ -584,7 +584,7 @@ mod test {
1,
UtxoTestParams {
features,
script: script![Drop Nop],
script: script![Drop Nop].unwrap(),
minimum_value_promise: MicroMinotari(123456),
value: MicroMinotari(123456),
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/covenants/filters/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod test {
#[tokio::test]
async fn it_filters_outputset_using_intersection() -> Result<(), Box<dyn std::error::Error>> {
let key_manager = create_memory_db_key_manager().unwrap();
let script = script!(CheckHeight(101));
let script = script!(CheckHeight(101)).unwrap();
let covenant = covenant!(and(field_eq(@field::features_maturity, @uint(42),), field_eq(@field::script, @script(script.clone())))).unwrap();
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(
Expand All @@ -64,7 +64,7 @@ mod test {
|outputs| {
// output satisfying maturity only
outputs[2].features.maturity = 42;
outputs[2].script = script!(CheckHeight(102));
outputs[2].script = script!(CheckHeight(102)).unwrap();
// output satisfying maturity and script
outputs[5].features.maturity = 42;
outputs[5].script = script.clone();
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/field_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod test {
#[tokio::test]
async fn it_filters_tari_script() -> Result<(), Box<dyn std::error::Error>> {
let key_manager = create_memory_db_key_manager().unwrap();
let script = script!(CheckHeight(100));
let script = script!(CheckHeight(100)).unwrap();
let covenant = covenant!(field_eq(
@field::script,
@script(script.clone())
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod test {
#[tokio::test]
async fn it_filters_compliment_of_filter() -> Result<(), Box<dyn std::error::Error>> {
let key_manager = create_memory_db_key_manager().unwrap();
let script = script!(CheckHeight(100));
let script = script!(CheckHeight(100)).unwrap();
let covenant = covenant!(not(or(field_eq(@field::features_maturity, @uint(42),), field_eq(@field::script, @script(script.clone()))))).unwrap();
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod test {
#[tokio::test]
async fn it_filters_outputset_using_union() -> Result<(), Box<dyn std::error::Error>> {
let key_manager = create_memory_db_key_manager().unwrap();
let script = script!(CheckHeight(100));
let script = script!(CheckHeight(100)).unwrap();
let covenant = covenant!(or(field_eq(@field::features_maturity, @uint(42),), field_eq(@field::script, @script(script.clone())))).unwrap();
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod test {
#[tokio::test]
async fn it_filters_outputset_using_symmetric_difference() -> Result<(), Box<dyn std::error::Error>> {
let key_manager = create_memory_db_key_manager().unwrap();
let script = script!(CheckHeight(100));
let script = script!(CheckHeight(100)).unwrap();
let covenant = covenant!(and(field_eq(@field::features_maturity, @uint(42),), field_eq(@field::script, @script(script.clone())))).unwrap();
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ mod test {
};
let dest_pk = PublicKey::from_hex("b0c1f788f137ba0cdc0b61e89ee43b80ebf5cca4136d3229561bf11eba347849").unwrap();
let sender_pk = dest_pk.clone();
let script = script!(HashSha256 PushHash(Box::new(hash)) Equal IfThen PushPubKey(Box::new(dest_pk)) Else CheckHeightVerify(100) PushPubKey(Box::new(sender_pk)) EndIf);
let script = script!(HashSha256 PushHash(Box::new(hash)) Equal IfThen PushPubKey(Box::new(dest_pk)) Else CheckHeightVerify(100) PushPubKey(Box::new(sender_pk)) EndIf).unwrap();
let covenant = covenant!(field_eq(@field::script, @script(script.clone()))).unwrap();

let decoded = Covenant::from_bytes(&mut covenant.to_bytes().as_bytes()).unwrap();
Expand Down
11 changes: 6 additions & 5 deletions base_layer/core/src/transactions/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ impl TestParams {
pub fn get_size_for_default_features_and_scripts(&self, num_outputs: usize) -> std::io::Result<usize> {
let output_features = OutputFeatures { ..Default::default() };
Ok(self.fee().weighting().round_up_features_and_scripts_size(
script![Nop].get_serialized_size()? + output_features.get_serialized_size()?,
script![Nop].map_err(|e| e.to_std_io_error())?.get_serialized_size()? +
output_features.get_serialized_size()?,
) * num_outputs)
}
}
Expand Down Expand Up @@ -232,7 +233,7 @@ impl Default for UtxoTestParams {
fn default() -> Self {
Self {
value: 10.into(),
script: script![Nop],
script: script![Nop].unwrap(),
features: OutputFeatures::default(),
input_data: None,
covenant: Covenant::default(),
Expand Down Expand Up @@ -438,7 +439,7 @@ macro_rules! txn_schema {
fee: $fee,
lock_height: $lock,
features: $features.clone(),
script: tari_script::script![Nop],
script: tari_script::script![Nop].unwrap(),
covenant: Default::default(),
input_data: None,
input_version: $input_version.clone(),
Expand Down Expand Up @@ -540,7 +541,7 @@ pub async fn create_tx(
output_count,
fee_per_gram,
&output_features,
&script![Nop],
&script![Nop].unwrap(),
&Default::default(),
key_manager,
)
Expand Down Expand Up @@ -717,7 +718,7 @@ pub async fn create_stx_protocol_internal(
.with_lock_height(schema.lock_height)
.with_fee_per_gram(schema.fee)
.with_change_data(
script!(PushPubKey(Box::new(script_public_key))),
script!(PushPubKey(Box::new(script_public_key))).unwrap(),
ExecutionStack::default(),
change.script_key_id,
change.commitment_mask_key_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async fn range_proof_verification() {
test_params_2.commitment_mask_key_id.clone(),
)
.with_features(OutputFeatures::default())
.with_script(script![Nop])
.with_script(script![Nop].unwrap())
.encrypt_data_for_recovery(&key_manager, None, PaymentId::Empty)
.await
.unwrap()
Expand Down Expand Up @@ -524,7 +524,7 @@ async fn inputs_not_malleable() {
2,
15.into(),
&Default::default(),
&script![Nop],
&script![Nop].unwrap(),
&Default::default(),
&key_manager,
)
Expand All @@ -538,7 +538,7 @@ async fn inputs_not_malleable() {
.unwrap();

let mut inputs = tx.body().inputs().clone();
inputs[0].set_script(script![Drop]).unwrap();
inputs[0].set_script(script![Drop].unwrap()).unwrap();
inputs[0].input_data = stack;
tx.body = AggregateBody::new(inputs, tx.body.outputs().clone(), tx.body().kernels().clone());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ mod test {
let (commitment_mask_key, script_key_id) = key_manager.get_next_commitment_mask_and_script_key().await.unwrap();
let value = MicroMinotari(100);
let kmob = WalletOutputBuilder::new(value, commitment_mask_key.key_id.clone());
let kmob = kmob.with_script(TariScript::new(vec![]));
let kmob = kmob.with_script(TariScript::new(vec![]).unwrap());
assert!(kmob.clone().try_build(&key_manager).await.is_err());
let sender_offset = key_manager
.get_next_key(TransactionKeyManagerBranch::SenderOffset.get_branch_key())
Expand Down Expand Up @@ -403,7 +403,7 @@ mod test {
let (commitment_mask_key, script_key) = key_manager.get_next_commitment_mask_and_script_key().await.unwrap();
let value = MicroMinotari(100);
let kmob = WalletOutputBuilder::new(value, commitment_mask_key.key_id.clone());
let kmob = kmob.with_script(TariScript::new(vec![]));
let kmob = kmob.with_script(TariScript::new(vec![]).unwrap());
let sender_offset = key_manager
.get_next_key(TransactionKeyManagerBranch::SenderOffset.get_branch_key())
.await
Expand Down
Loading

0 comments on commit 7510db8

Please sign in to comment.