Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding find input by nft function #77

Merged
merged 1 commit into from
Feb 9, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# v0.x.y

- Added `input_by_nft` to the find submodule.

# v0.4.5

- Added the `Wallets` type and `to_vks` to the wallet submodule.
Expand Down
26 changes: 26 additions & 0 deletions lib/assist/find.ak
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,29 @@ test cant_find_output_datum_by_nft2() fail {
expect outcome: ByteArray = output_datum_by_nft(outputs, #"fade", #"fade")
outcome == fake_tx.test_datum
}

/// Find the first occurance of an inline datum on an output with a value
/// that contains a specific nft.
pub fn input_by_nft(inputs: List<Input>, pid: PolicyId, tkn: AssetName) -> Input {
when inputs is {
[input, ..rest] ->
if values.prove_exact_nft(pid, tkn, input.output.value) {
input
} else {
input_by_nft(rest, pid, tkn)
}
[] -> fail @"No Input Found In Inputs"
}
}

test cant_find_input_by_nft() fail {
let inputs: List<Input> = fake_tx.test_bad_inputs()
let input: Input = input_by_nft(inputs, #"acab", #"beef")
input == fake_tx.test_one_lovelace_input()
}

test can_find_input_by_nft() {
let inputs: List<Input> = fake_tx.test_bad_inputs()
let input: Input = input_by_nft(inputs, #"", #"")
input == fake_tx.test_one_lovelace_input()
}
20 changes: 20 additions & 0 deletions lib/assist/maths.ak
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use aiken/bytearray
use aiken/list
use aiken/math

pub const large_prime: Int =
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab

/// Calculate `n` to the power of `e` modulo `q` using the exponentiation by
/// squaring method. At each multiplication a modulo is calculated, allowing
/// very large `n` and `e` values.
Expand Down Expand Up @@ -50,6 +53,13 @@ test powmod_54_123_1() {
powmod(54, 123, 1) == 0
}

test powmod_very_large() {
let q =
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab

powmod(2, 31565052264044690, q) == 2372285326153189929528332103442483377705667947278803297593729812983050665252776619156384486056106192189240939340047
}

/// Convert a integer `n` into some base `q`. This method
/// should scale with any integer and any logical base.
///
Expand Down Expand Up @@ -89,6 +99,16 @@ test to_base_256() {
base_q(78237623, 256) == [4, 169, 207, 183]
}

test to_base_large() {
base_q(powmod(2, 15383577435643450949, large_prime), 15) == [
12, 9, 14, 13, 5, 2, 11, 13, 5, 13, 10, 8, 5, 11, 10, 4, 7, 5, 11, 0, 10, 8,
8, 8, 11, 6, 13, 3, 4, 14, 11, 10, 0, 12, 10, 11, 10, 0, 11, 8, 2, 3, 8, 13,
0, 1, 5, 13, 0, 8, 13, 3, 10, 6, 4, 12, 14, 3, 7, 1, 11, 3, 7, 3, 14, 6, 8,
4, 14, 3, 2, 7, 10, 14, 13, 10, 6, 8, 1, 7, 6, 13, 11, 10, 9, 12, 2, 13, 4,
5, 14, 8, 5, 13, 3, 12, 12,
]
}

/// Convert a hexadecimal bytearray into its base 10 representation. This
/// only works with even length bytearrays so arbitrary numbers in hexadecimal
/// form will not in general work.
Expand Down
17 changes: 16 additions & 1 deletion lib/assist/tests/fake_tx.ak
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ pub fn test_bad_input() -> Input {
input
}

pub fn test_one_lovelace_input() -> Input {
Input {
output_reference: OutputReference {
transaction_id: TransactionId { hash: #"" },
output_index: 1,
},
output: Output {
address: addresses.create_address(#"face", #""),
value: value.from_lovelace(1),
datum: NoDatum,
reference_script: None,
},
}
}

pub fn test_bad_inputs() -> List<Input> {
[
Input {
Expand Down Expand Up @@ -163,7 +178,7 @@ pub fn test_bad_inputs() -> List<Input> {
},
output: Output {
address: addresses.create_address(#"face", #""),
value: value.from_lovelace(5),
value: value.from_lovelace(1),
datum: NoDatum,
reference_script: None,
},
Expand Down
Loading