diff --git a/wasm-wrappers/WASM-API.md b/wasm-wrappers/WASM-API.md index 96263505f..b1d784b9a 100644 --- a/wasm-wrappers/WASM-API.md +++ b/wasm-wrappers/WASM-API.md @@ -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. diff --git a/wasm-wrappers/js-bindings-test/tests/main.ts b/wasm-wrappers/js-bindings-test/tests/main.ts index 4297c7625..1577b8dd3 100644 --- a/wasm-wrappers/js-bindings-test/tests/main.ts +++ b/wasm-wrappers/js-bindings-test/tests/main.ts @@ -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() { @@ -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"); } diff --git a/wasm-wrappers/js-bindings-test/tests/test_decode_transaction_to_js.ts b/wasm-wrappers/js-bindings-test/tests/test_decode_transaction_to_js.ts new file mode 100644 index 000000000..421cdc5b5 --- /dev/null +++ b/wasm-wrappers/js-bindings-test/tests/test_decode_transaction_to_js.ts @@ -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"); +} \ No newline at end of file diff --git a/wasm-wrappers/src/lib.rs b/wasm-wrappers/src/lib.rs index 86503acb8..6a3d01153 100644 --- a/wasm-wrappers/src/lib.rs +++ b/wasm-wrappers/src/lib.rs @@ -694,6 +694,20 @@ pub fn encode_transaction(inputs: &[u8], outputs: &[u8], flags: u64) -> Result Result { + 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(