Skip to content

Add get functions for order, delegation and pool to wasm #1929

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions wasm-wrappers/WASM-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ The current block height information is used in case a network upgrade changed t

Returns the Fungible/NFT Token ID for the given inputs of a transaction

### Function: `get_order_id`

Returns the Order ID for the given inputs of a transaction

### Function: `get_delegation_id`

Returns the Delegation ID for the given inputs of a transaction

### Function: `get_pool_id`

Returns the Pool ID for the given inputs of a transaction

### Function: `data_deposit_fee`

Returns the fee that needs to be paid by a transaction for issuing a data deposit
Expand Down
81 changes: 81 additions & 0 deletions wasm-wrappers/js-bindings-test/tests/test_misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import {
Amount,
get_token_id,
get_order_id,
get_delegation_id,
get_pool_id,
get_transaction_id,
effective_pool_balance,
make_default_account_privkey,
Expand Down Expand Up @@ -52,6 +55,9 @@ export function test_misc() {
run_one_test(test_sign_challenge);
run_one_test(test_staking_pool_spend_maturity_block_count);
run_one_test(test_get_token_id);
run_one_test(test_get_order_id);
run_one_test(test_get_delegation_id);
run_one_test(test_get_pool_id);
run_one_test(test_effective_pool_balance);
run_one_test(test_get_transaction_id);
}
Expand Down Expand Up @@ -168,6 +174,81 @@ function test_get_token_id() {
}
}

function test_get_order_id() {
try {
get_order_id(new Uint8Array(), Network.Testnet);
throw "Order Id generated without a UTXO input somehow!";
} catch (e) {
const msg = get_err_msg(e);
if (!(msg.includes("No UTXO inputs for order id creation") ||
msg.includes("No inputs for order id creation"))) {
throw e;
}
console.log("Tested no UTXO inputs for order ID successfully");
}

{
const expected_order_id =
"tordr1favkn4kqrxruqdtkjywhafeme30z8frlu85xut4euzfduplrsauqejwmlh";
const order_id = get_order_id(Uint8Array.from(INPUTS), Network.Testnet);
console.log(order_id);

if (order_id != expected_order_id) {
throw new Error("Different order id");
}
}
}

function test_get_delegation_id() {
try {
get_delegation_id(new Uint8Array(), Network.Testnet);
throw "Delegation Id generated without a UTXO input somehow!";
} catch (e) {
const msg = get_err_msg(e);
if (!(msg.includes("No UTXO inputs for delegation id creation") ||
msg.includes("No inputs for delegation id creation"))) {
throw e;
}
console.log("Tested no UTXO inputs for delegation ID successfully");
}

{
const expected_delegation_id =
"tdelg1uq9yjdlsny4txxz9vr833s2zq2h2p92weq9s4mpz7rrvcnqwztgqhg9ypf";
const delegation_id = get_delegation_id(Uint8Array.from(INPUTS), Network.Testnet);
console.log(delegation_id);

if (delegation_id != expected_delegation_id) {
throw new Error("Different delegation id");
}
}
}

function test_get_pool_id() {
try {
get_pool_id(new Uint8Array(), Network.Testnet);
throw "Pool Id generated without a UTXO input somehow!";
} catch (e) {
const msg = get_err_msg(e);
if (!(msg.includes("No UTXO inputs for pool id creation") ||
msg.includes("No inputs for pool id creation"))) {
throw e;
}
console.log("Tested no UTXO inputs for pool ID successfully");
}

{
const expected_pool_id =
"tpool10922a3v92kph0dheca07fzxjktvgcjs7lcrna0ny4tvw5t3t20squchyn5";
const pool_id = get_pool_id(Uint8Array.from(INPUTS), Network.Testnet);
console.log(pool_id);

if (pool_id != expected_pool_id) {
throw new Error("Different pool id");
}
}
}

function test_effective_pool_balance() {
{
const eff_bal = effective_pool_balance(
Expand Down
44 changes: 43 additions & 1 deletion wasm-wrappers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use common::{
classic_multisig::ClassicMultisigChallenge,
config::{Builder, BIP44_PATH},
htlc::HtlcSecret,
make_token_id,
make_delegation_id, make_order_id, make_pool_id, make_token_id,
signature::{
inputsig::{
arbitrary_message::{produce_message_challenge, ArbitraryMessageSignature},
Expand Down Expand Up @@ -550,6 +550,48 @@ pub fn get_token_id(
.to_string())
}

/// Returns the Order ID for the given inputs of a transaction
#[wasm_bindgen]
pub fn get_order_id(inputs: &[u8], network: Network) -> Result<String, Error> {
let chain_config = Builder::new(network.into()).build();

let inputs = decode_raw_array::<TxInput>(inputs).map_err(Error::InvalidInputEncoding)?;

let token_id = make_order_id(&inputs)?;

Ok(Address::new(&chain_config, token_id)
.expect("Should not fail to create address")
.to_string())
}

/// Returns the Delegation ID for the given inputs of a transaction
#[wasm_bindgen]
pub fn get_delegation_id(inputs: &[u8], network: Network) -> Result<String, Error> {
let chain_config = Builder::new(network.into()).build();

let inputs = decode_raw_array::<TxInput>(inputs).map_err(Error::InvalidInputEncoding)?;

let token_id = make_delegation_id(&inputs)?;

Ok(Address::new(&chain_config, token_id)
.expect("Should not fail to create address")
.to_string())
}

/// Returns the Pool ID for the given inputs of a transaction
#[wasm_bindgen]
pub fn get_pool_id(inputs: &[u8], network: Network) -> Result<String, Error> {
let chain_config = Builder::new(network.into()).build();

let inputs = decode_raw_array::<TxInput>(inputs).map_err(Error::InvalidInputEncoding)?;

let token_id = make_pool_id(&inputs)?;

Ok(Address::new(&chain_config, token_id)
.expect("Should not fail to create address")
.to_string())
}

/// Returns the fee that needs to be paid by a transaction for issuing a data deposit
#[wasm_bindgen]
pub fn data_deposit_fee(current_block_height: u64, network: Network) -> Amount {
Expand Down