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
4 changes: 4 additions & 0 deletions wasm-wrappers/WASM-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ Also, the function assumes that the input UTXOs are not HTLC.
Given inputs as bytes, outputs as bytes, and flags settings, this function returns
the transaction that contains them all, as bytes.

### Function: `decode_transaction_to_js`

Decodes an unsigned transaction from its binary encoding into a JavaScript object.

### Function: `decode_signed_transaction_to_js`

Decodes a signed transaction from its binary encoding into a JavaScript object.
Expand Down
2 changes: 2 additions & 0 deletions wasm-wrappers/js-bindings-test/tests/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { test_orders } from "./test_orders.js";
import { test_signed_transaction_intent } from "./test_signed_transaction_intent.js";
import { test_transaction_and_witness_encoding } from "./test_transaction_and_witness_encoding.js";
import { test_partially_signed_transaction_encoding } from "./test_partially_signed_transaction_encoding.js";
import { test_decode_transaction_to_js } from "./test_decode_transaction_to_js.js";

/** @public */
export function run_all_tests() {
Expand All @@ -40,6 +41,7 @@ export function run_all_tests() {
run_one_test(test_signed_transaction_intent);
run_one_test(test_transaction_and_witness_encoding);
run_one_test(test_partially_signed_transaction_encoding);
run_one_test(test_decode_transaction_to_js);

console.log("All tests passed");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) 2021-2025 RBB S.r.l
// opensource@mintlayer.org
// SPDX-License-Identifier: MIT
// Licensed under the MIT License;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {
encode_transaction,
decode_transaction_to_js,
Network,
} from "../../pkg/wasm_wrappers.js";

import { assert_eq_vals } from "./utils.js";
import { INPUTS } from "./test_encode_other_inputs.js";
import { OUTPUTS } from "./test_encode_other_outputs.js";

export function test_decode_transaction_to_js() {
const tx = encode_transaction(
Uint8Array.from(INPUTS),
Uint8Array.from(OUTPUTS),
BigInt(0),
);

const decoded_tx = decode_transaction_to_js(tx, Network.Testnet);

const expected_decoded_tx = {
V1: {
version: 1,
flags: 0,
inputs: [
{
Utxo: {
id: {
Transaction:
"0000000000000000000000000000000000000000000000000000000000000000",
},
index: 1,
},
},
{
Account: {
nonce: 1,
account: {
DelegationBalance: [
"tdelg1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnu8zn4",
{ atoms: "1" },
],
},
},
},
],
outputs: [
{
LockThenTransfer: [
{ Coin: { atoms: "100" } },
"tmt1q9dn5m4svn8sds3fcy09kpxrefnu75xekgr5wa3n",
{ type: "UntilHeight", content: 100 },
],
},
{
CreateStakePool: [
"tpool1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqza035u",
{
pledge: { atoms: "40000" },
staker:
"tmt1q9dn5m4svn8sds3fcy09kpxrefnu75xekgr5wa3n",
vrf_public_key:
"006cf5ea61aa09f79ea964547bebb7931d8876cb1892383cd902c62085fff0547b",
decommission_key:
"tmt1q9dn5m4svn8sds3fcy09kpxrefnu75xekgr5wa3n",
margin_ratio_per_thousand: "10%",
cost_per_block: { atoms: "0" },
},
],
},
],
},
};

assert_eq_vals(
JSON.stringify(decoded_tx),
JSON.stringify(expected_decoded_tx),
);

console.log("unsigned transaction decoding ok");
}
14 changes: 14 additions & 0 deletions wasm-wrappers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,20 @@ pub fn encode_transaction(inputs: &[u8], outputs: &[u8], flags: u64) -> Result<V
Ok(tx.encode())
}

/// Decodes an unsigned transaction from its binary encoding into a JavaScript object.
#[wasm_bindgen]
pub fn decode_transaction_to_js(transaction: &[u8], network: Network) -> Result<JsValue, Error> {
let chain_config = Builder::new(network.into()).build();

let tx = Transaction::decode_all(&mut &transaction[..])
.map_err(Error::InvalidTransactionEncoding)?;

let str = JsonEncoded::new(&tx).to_string();
let str = dehexify_all_addresses(&chain_config, &str);

js_sys::JSON::parse(&str).map_err(Error::JsonParseError)
}

/// Decodes a signed transaction from its binary encoding into a JavaScript object.
#[wasm_bindgen]
pub fn decode_signed_transaction_to_js(
Expand Down
Loading