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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
- Extended functions for retrieving information from `OP_RETURN`.
- Conversion and bit functions for `u2` and `u4`.
- Functions for asserting script hashes.

### Changed
- Bumped Simplex to 0.2.0.
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ simf/lib
│ │ │ └── Functions for asserting `Amount` in inputs and outputs.
│ │ ├── asset_id.simf
│ │ │ └── Functions for asserting `AssetId` in inputs and outputs.
│ │ └── asset_id_with_amount.simf
│ │ └── Functions for asserting `Amount` and `AssetId` in inputs and outputs.
│ │ ├── asset_id_with_amount.simf
│ │ │ └── Functions for asserting `Amount` and `AssetId` in inputs and outputs.
│ │ └── script_hash.simf
│ │ └── Functions for asserting hash of the scriptPubKey in inputs and outputs.
│ ├── getters
│ │ ├── amount.simf
│ │ │ └── Functions for retrieving `Amount` in inputs and outputs.
Expand Down
2 changes: 1 addition & 1 deletion WISHLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Q. Aren't the first three directly implemented by jets? Do we just need to `unwr
Input/output by index.

* Get script hash (implemented: `utxo::getters::script_hash`)
* Check that the script hash is equal to the passed value
* Check that the script hash is equal to the passed value (implemented: `utxo::asserts::script_hash`)
* Check that the input script hash equals the output script hash
* Same as prior function, but also check that the values are equal to the passed value

Expand Down
21 changes: 21 additions & 0 deletions docs/stdlib.json
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,27 @@
"output_type": "Confidential1",
"description": "Returns the confidential input asset_id at the current_index.\n\n## Panics\nPanics if asset_id is explicit."
},
{
"simplicityhl_name": "assert_script_hash",
"section": "Script hash asserts",
"input_type": "u32, bool, u256",
"output_type": "()",
"description": "Asserts that the SHA256 hash of the scriptPubKey at the specified input or output index equals the expected `u256` value.\n\n## Panics\nThe assertion fails."
},
{
"simplicityhl_name": "assert_input_script_hash",
"section": "Script hash asserts",
"input_type": "u32, u256",
"output_type": "()",
"description": "Asserts that the SHA256 hash of the scriptPubKey of the input at the given index equals the expected `u256` value.\n\n## Panics\nThe assertion fails."
},
{
"simplicityhl_name": "assert_output_script_hash",
"section": "Script hash asserts",
"input_type": "u32, u256",
"output_type": "()",
"description": "Asserts that the SHA256 hash of the scriptPubKey of the output at the given index equals the expected `u256` value.\n\n## Panics\nThe assertion fails."
},
{
"simplicityhl_name": "get_script_hash",
"section": "Script hash getters",
Expand Down
30 changes: 30 additions & 0 deletions simf/lib/utxo/asserts/script_hash.simf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::lib::utxo::getters::script_hash::{
get_script_hash,
get_input_script_hash,
get_output_script_hash
};
use crate::lib::asserts::assert_eq_256;

/// Asserts that the SHA256 hash of the scriptPubKey at the specified input or output equals the expected u256 value
///
/// Warning: using this incurs overhead, and should be used only when the input/output selection
/// needs to be determined at runtime; otherwise, use the input- or output-specific assert directly.
pub fn assert_script_hash(index: u32, is_input_index: bool, expected: u256) {
let script_hash: u256 = get_script_hash(index, is_input_index);

assert_eq_256(script_hash, expected);
}

/// Asserts that the SHA256 hash of the scriptPubKey of the input at the given index equals the expected u256 value
pub fn assert_input_script_hash(index: u32, expected: u256) {
let script_hash: u256 = get_input_script_hash(index);

assert_eq_256(script_hash, expected);
}

/// Asserts that the SHA256 hash of the scriptPubKey of the output at the given index equals the expected u256 value
pub fn assert_output_script_hash(index: u32, expected: u256) {
let script_hash: u256 = get_output_script_hash(index);

assert_eq_256(script_hash, expected);
}
37 changes: 37 additions & 0 deletions simf/tests/utxo/asserts/script_hash.simf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::lib::utxo::asserts::script_hash::{
assert_script_hash,
assert_input_script_hash,
assert_output_script_hash
};

use crate::tests::support::dispatch::is_selected;

fn main() {
let fn_idx: u8 = witness::FUNCTION_INDEX;

let index: u32 = witness::INDEX;
let is_input_index: bool = witness::IS_INPUT_INDEX;

let expected_hash: u256 = witness::EXPECTED;

match is_selected(0, fn_idx) {
true => {
assert_script_hash(index, is_input_index, expected_hash);
},
false => (),
};

match is_selected(1, fn_idx) {
true => {
assert_input_script_hash(index, expected_hash);
},
false => (),
};

match is_selected(2, fn_idx) {
true => {
assert_output_script_hash(index, expected_hash);
},
false => (),
};
}
1 change: 1 addition & 0 deletions tests/stdlib/utxo/asserts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod amount;
mod asset_id;
mod asset_id_with_amount;
mod script_hash;
Loading
Loading