Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions crates/sdk/src/program/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,101 @@ impl Program {
Ok(info.control_block(&script_ver).expect("control block should exist"))
}
}

#[cfg(test)]
mod tests {
use simplicityhl::{
Arguments,
elements::{AssetId, confidential, pset::Input},
};

use super::*;

// simplicityhl/examples/cat.simf
const DUMMY_PROGRAM: &str = r#"
fn main() {
let ab: u16 = <(u8, u8)>::into((0x10, 0x01));
let c: u16 = 0x1001;
assert!(jet::eq_16(ab, c));
let ab: u8 = <(u4, u4)>::into((0b1011, 0b1101));
let c: u8 = 0b10111101;
assert!(jet::eq_8(ab, c));
}
"#;

#[derive(Clone)]
struct EmptyArguments;

impl ArgumentsTrait for EmptyArguments {
fn build_arguments(&self) -> Arguments {
Arguments::default()
}
}

fn dummy_asset_id(byte: u8) -> AssetId {
AssetId::from_slice(&[byte; 32]).unwrap()
}

fn dummy_pubkey(seed: u64) -> XOnlyPublicKey {
let mut rng = <secp256k1::rand::rngs::StdRng as secp256k1::rand::SeedableRng>::seed_from_u64(seed);
secp256k1::Keypair::new_global(&mut rng).x_only_public_key().0
}

fn dummy_program() -> Program {
Program::new(DUMMY_PROGRAM, dummy_pubkey(0), Box::new(EmptyArguments))
}

fn dummy_network() -> SimplicityNetwork {
SimplicityNetwork::default_regtest()
}

fn make_pst_with_script(script: Script) -> PartiallySignedTransaction {
let txout = TxOut {
asset: confidential::Asset::Explicit(dummy_asset_id(0xAA)),
value: confidential::Value::Explicit(1000),
script_pubkey: script,
..Default::default()
};
let input = Input {
witness_utxo: Some(txout),
..Default::default()
};

let mut pst = PartiallySignedTransaction::new_v2();

pst.add_input(input);

pst
}

#[test]
fn test_get_env_idx() {
let program = dummy_program();
let network = dummy_network();

let correct_script = program.get_script_pubkey(&network);
let wrong_script = Script::new();

let mut pst = make_pst_with_script(wrong_script);

let correct_txout = TxOut {
asset: confidential::Asset::Explicit(dummy_asset_id(0xAA)),
value: confidential::Value::Explicit(1000),
script_pubkey: correct_script,
..Default::default()
};

pst.add_input(Input {
witness_utxo: Some(correct_txout),
..Default::default()
});

// take a script with a wrong pubkey
assert!(matches!(
program.get_env(&pst, 0, &network).unwrap_err(),
ProgramError::ScriptPubkeyMismatch { .. }
));

assert!(program.get_env(&pst, 1, &network).is_ok());
}
}
175 changes: 175 additions & 0 deletions crates/sdk/src/transaction/final_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,178 @@ impl FinalTransaction {
(pst, input_secrets)
}
}

#[cfg(test)]
mod tests {
use bitcoin_hashes::Hash;

use simplicityhl::elements::{OutPoint, Script, TxOut, Txid};

use crate::transaction::UTXO;

use super::*;

fn dummy_asset_id(byte: u8) -> AssetId {
AssetId::from_slice(&[byte; 32]).unwrap()
}

fn dummy_txid(byte: u8) -> Txid {
Txid::from_slice(&[byte; 32]).unwrap()
}

fn explicit_utxo(txid_byte: u8, vout: u32, amount: u64, asset: AssetId) -> UTXO {
UTXO {
outpoint: OutPoint::new(dummy_txid(txid_byte), vout),
txout: TxOut::new_fee(amount, asset),
secrets: None,
}
}

fn confidential_utxo(txid_byte: u8, vout: u32, asset: AssetId, value: u64) -> UTXO {
UTXO {
outpoint: OutPoint::new(dummy_txid(txid_byte), vout),
txout: TxOut::default(),
secrets: Some(TxOutSecrets::new(
asset,
AssetBlindingFactor::zero(),
value,
ValueBlindingFactor::zero(),
)),
}
}

// Manually construct PST and check extract_pst correctness based on it
#[test]
fn extract_pst_single_explicit_input_single_output() {
let policy = dummy_asset_id(0xAA);

let utxo = explicit_utxo(0x01, 0, 5000, policy);
let partial_input = PartialInput::new(utxo);
let partial_output = PartialOutput::new(Script::new(), 4000, policy);

let mut ft = FinalTransaction::new();
ft.add_input(partial_input.clone(), RequiredSignature::None);
ft.add_output(partial_output.clone());

let mut expected_pst = PartiallySignedTransaction::new_v2();
expected_pst.add_input(partial_input.to_input());
expected_pst.add_output(partial_output.to_output());

let expected_secrets: HashMap<usize, TxOutSecrets> = HashMap::from([(
0,
TxOutSecrets::new(policy, AssetBlindingFactor::zero(), 5000, ValueBlindingFactor::zero()),
)]);

let (pst, secrets) = ft.extract_pst();

assert_eq!(pst, expected_pst);
assert_eq!(secrets, expected_secrets);
}

#[test]
fn extract_pst_single_confidential_input() {
let policy = dummy_asset_id(0xAA);

let utxo = confidential_utxo(0x01, 0, policy, 3000);
let partial_input = PartialInput::new(utxo);
let partial_output = PartialOutput::new(Script::new(), 2000, policy);

let mut ft = FinalTransaction::new();
ft.add_input(partial_input.clone(), RequiredSignature::None);
ft.add_output(partial_output.clone());

let mut expected_pst = PartiallySignedTransaction::new_v2();
expected_pst.add_input(partial_input.to_input());
expected_pst.add_output(partial_output.to_output());

let expected_secrets = HashMap::from([(
0,
TxOutSecrets::new(policy, AssetBlindingFactor::zero(), 3000, ValueBlindingFactor::zero()),
)]);

let (pst, secrets) = ft.extract_pst();

assert_eq!(pst, expected_pst);
assert_eq!(secrets, expected_secrets);
}

#[test]
fn extract_pst_mixed_inputs_multiple_outputs() {
let policy = dummy_asset_id(0xAA);
let other = dummy_asset_id(0xBB);

let explicit_utxo = explicit_utxo(0x01, 0, 5000, policy);
let conf_utxo = confidential_utxo(0x02, 1, other, 1000);

let explicit_partial = PartialInput::new(explicit_utxo);
let conf_partial = PartialInput::new(conf_utxo);

let output_a = PartialOutput::new(Script::new(), 3000, policy);
let output_b = PartialOutput::new(Script::new(), 800, other);

let mut ft = FinalTransaction::new();
ft.add_input(explicit_partial.clone(), RequiredSignature::None);
ft.add_input(conf_partial.clone(), RequiredSignature::None);
ft.add_output(output_a.clone());
ft.add_output(output_b.clone());

let mut expected_pst = PartiallySignedTransaction::new_v2();
expected_pst.add_input(explicit_partial.to_input());
expected_pst.add_input(conf_partial.to_input());
expected_pst.add_output(output_a.to_output());
expected_pst.add_output(output_b.to_output());

let expected_secrets = HashMap::from([
(
0,
TxOutSecrets::new(policy, AssetBlindingFactor::zero(), 5000, ValueBlindingFactor::zero()),
),
(
1,
TxOutSecrets::new(other, AssetBlindingFactor::zero(), 1000, ValueBlindingFactor::zero()),
),
]);

let (pst, secrets) = ft.extract_pst();

assert_eq!(pst, expected_pst);
assert_eq!(secrets, expected_secrets);
}

#[test]
fn extract_pst_with_issuance_input() {
let policy = dummy_asset_id(0xAA);
let entropy = [0x42u8; 32];
let issuance_amount = 1_000_000u64;

let utxo = explicit_utxo(0x01, 0, 5000, policy);
let partial_input = PartialInput::new(utxo);
let issuance = IssuanceInput::new(issuance_amount, entropy);
let partial_output = PartialOutput::new(Script::new(), 4000, policy);

let mut ft = FinalTransaction::new();
ft.add_issuance_input(partial_input.clone(), issuance.clone(), RequiredSignature::None);
ft.add_output(partial_output.clone());

// build expected pst, merge partial_input and issuance manually
let mut expected_pst = PartiallySignedTransaction::new_v2();
let mut expected_input = partial_input.to_input();
let issuance_input = issuance.to_input();
expected_input.issuance_value_amount = issuance_input.issuance_value_amount;
expected_input.issuance_asset_entropy = issuance_input.issuance_asset_entropy;
expected_input.issuance_inflation_keys = issuance_input.issuance_inflation_keys;
expected_input.blinded_issuance = issuance_input.blinded_issuance;
expected_pst.add_input(expected_input);
expected_pst.add_output(partial_output.to_output());

let expected_secrets = HashMap::from([(
0,
TxOutSecrets::new(policy, AssetBlindingFactor::zero(), 5000, ValueBlindingFactor::zero()),
)]);

let (pst, secrets) = ft.extract_pst();

assert_eq!(pst, expected_pst);
assert_eq!(secrets, expected_secrets);
}
}
12 changes: 6 additions & 6 deletions examples/basic/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading