# Hash Mismatch for Cardano Serialization Library

**URL:** <https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896>\
**Category:** Cardano Integration\
**Created:** [27 April 2024 02:13 UTC](https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896 "2024-04-27T02:13:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahat](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/rahat/32/84546_2.png) [@Rahat](https://forum.cardano.org/u/Rahat)\
**Post date:** [27 April 2024 02:13 UTC](https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896/1 "2024-04-27T02:13:37Z")

</div>

I have made transaction through Nami wallet here’s transaction id : `612caae48b8123606a0fcbd206dc4deb7d2f434a130e49df0664d23cce45c4a2`

I have created same transaction using CSL but I am getting different Hash  
CODE:

```bash
import * as WASM_lib from "@emurgo/cardano-serialization-lib-nodejs"

const fromHex = (hex) => Buffer.from(hex, "hex");
const toHex = (bytes) => Buffer.from(bytes).toString("hex");

const DESTINATION_ADDRESS = () =>
  WASM_lib.Address.from_bech32(
    "addr_test1qpfvrr5a8q2eefzcdqgy8cxmpj3ey3yjl969t49swq2u4wrh937lw6eeuspp3rame2793up6l70p8qlzfc2gmx2n02yqx88wg3"
  );

async function calculateHash() {
  const fee = 168493;
  const amountToTransfer = 8000000; //8ADA

  const input = WASM_lib.TransactionInput.new(
    WASM_lib.TransactionHash.from_hex("517329b3566f5331009c02d8e4ece9f9bd27f1c1d097d526c22c339ae8da5d6a"), 1
    // WASM_lib.TransactionHash.from_bytes(fromHex("517329b3566f5331009c02d8e4ece9f9bd27f1c1d097d526c22c339ae8da5d6a")) , 1
  );

  var output = WASM_lib.TransactionOutput.new(
    DESTINATION_ADDRESS(),
    WASM_lib.Value.new(WASM_lib.BigNum.from_str(amountToTransfer.toString()))
  )

  var outputOwner = WASM_lib.TransactionOutput.new(
    WASM_lib.Address.from_bech32("addr_test1qqesdq5l24eqvxjh3fdv3f9h9dcmjlvtjney94achzlehzwvdqakkxjr65st4rz9fm5e3j69260nf0uu8wxgja5tp4zq7ryx22"),
    WASM_lib.Value.new(WASM_lib.BigNum.from_str((9981663014).toString()))
  )

  const transactionWitnessSet = WASM_lib.TransactionWitnessSet.new();
  const transactionInputs = WASM_lib.TransactionInputs.new();
  transactionInputs.add(input);
  const transactionOutputs = WASM_lib.TransactionOutputs.new();
  transactionOutputs.add(output);
  transactionOutputs.add(outputOwner);

  const txBody = WASM_lib.TransactionBody.new(
    transactionInputs,
    transactionOutputs,
    WASM_lib.BigNum.from_str(fee.toString())
  );

  // txBody.set_required_signers(requiredSigners);
  const transaction = WASM_lib.Transaction.new(
    WASM_lib.TransactionBody.from_bytes(txBody.to_bytes()),
    WASM_lib.TransactionWitnessSet.from_bytes(
      transactionWitnessSet.to_bytes()
    )
  );
  console.log(transaction.to_json())

  const serializedTx = toHex(transaction.to_bytes());

  const txVkeyWitnesses = await signTx(serializedTx);

}

const signTx = async (tx,) => {
  const rawTx = WASM_lib.Transaction.from_bytes(Buffer.from(tx, 'hex'));
  const txHash = WASM_lib.hash_transaction(rawTx.body());

  console.log("txHash in hex",txHash.to_hex())
  return txHash;
};

calculateHash()

```

## Transaction Hash and Transaction body for the code

`ac848e46a992b07a6b86219e51e8235975a58667ddb2657ca1c02d4bb49a94de`

```bash
{
  "body": {
    "inputs": [
      {
        "transaction_id": "517329b3566f5331009c02d8e4ece9f9bd27f1c1d097d526c22c339ae8da5d6a",
        "index": 1
      }
    ],
    "outputs": [
      {
        "address": "addr_test1qpfvrr5a8q2eefzcdqgy8cxmpj3ey3yjl969t49swq2u4wrh937lw6eeuspp3rame2793up6l70p8qlzfc2gmx2n02yqx88wg3",
        "amount": {
          "coin": "8000000",
          "multiasset": null
        },
        "plutus_data": null,
        "script_ref": null
      },
      {
        "address": "addr_test1qqesdq5l24eqvxjh3fdv3f9h9dcmjlvtjney94achzlehzwvdqakkxjr65st4rz9fm5e3j69260nf0uu8wxgja5tp4zq7ryx22",
        "amount": {
          "coin": "9981663014",
          "multiasset": null
        },
        "plutus_data": null,
        "script_ref": null
      }
    ],
    "fee": "168493",
    "ttl": null,
    "certs": null,
    "withdrawals": null,
    "update": null,
    "auxiliary_data_hash": null,
    "validity_start_interval": null,
    "mint": null,
    "script_data_hash": null,
    "collateral": null,
    "required_signers": null,
    "network_id": null,
    "collateral_return": null,
    "total_collateral": null,
    "reference_inputs": null
  },
  "witness_set": {
    "vkeys": null,
    "native_scripts": null,
    "bootstraps": null,
    "plutus_scripts": null,
    "plutus_data": null,
    "redeemers": null
  },
  "is_valid": true,
  "auxiliary_data": null
}

```

## Here’s the UTXO and transaction details:

```bash
{
    "hash": "612caae48b8123606a0fcbd206dc4deb7d2f434a130e49df0664d23cce45c4a2",
    "inputs": [
        {
            "address": "addr_test1qqesdq5l24eqvxjh3fdv3f9h9dcmjlvtjney94achzlehzwvdqakkxjr65st4rz9fm5e3j69260nf0uu8wxgja5tp4zq7ryx22",
            "amount": [
                {
                    "unit": "lovelace",
                    "quantity": "9989831507"
                }
            ],
            "tx_hash": "517329b3566f5331009c02d8e4ece9f9bd27f1c1d097d526c22c339ae8da5d6a",
            "output_index": 1,
            "data_hash": null,
            "inline_datum": null,
            "reference_script_hash": null,
            "collateral": false,
            "reference": false
        }
    ],
    "outputs": [
        {
            "address": "addr_test1qpfvrr5a8q2eefzcdqgy8cxmpj3ey3yjl969t49swq2u4wrh937lw6eeuspp3rame2793up6l70p8qlzfc2gmx2n02yqx88wg3",
            "amount": [
                {
                    "unit": "lovelace",
                    "quantity": "8000000"
                }
            ],
            "output_index": 0,
            "data_hash": null,
            "inline_datum": null,
            "collateral": false,
            "reference_script_hash": null
        },
        {
            "address": "addr_test1qqesdq5l24eqvxjh3fdv3f9h9dcmjlvtjney94achzlehzwvdqakkxjr65st4rz9fm5e3j69260nf0uu8wxgja5tp4zq7ryx22",
            "amount": [
                {
                    "unit": "lovelace",
                    "quantity": "9981663014"
                }
            ],
            "output_index": 1,
            "data_hash": null,
            "inline_datum": null,
            "collateral": false,
            "reference_script_hash": null
        }
    ]
}

```

```bash

{
    "hash": "612caae48b8123606a0fcbd206dc4deb7d2f434a130e49df0664d23cce45c4a2",
    "block": "4c138f17296c3456620c76d16d173424603653ec4253a51bca6259615598f6c7",
    "block_height": 2177495,
    "block_time": 1714179642,
    "slot": 58496442,
    "index": 3,
    "output_amount": [
        {
            "unit": "lovelace",
            "quantity": "9989663014"
        }
    ],
    "fees": "168493",
    "deposit": "0",
    "size": 293,
    "invalid_before": null,
    "invalid_hereafter": "58517927",
    "utxo_count": 3,
    "withdrawal_count": 0,
    "mir_cert_count": 0,
    "delegation_count": 0,
    "stake_cert_count": 0,
    "pool_update_count": 0,
    "pool_retire_count": 0,
    "asset_mint_or_burn_count": 0,
    "redeemer_count": 0,
    "valid_contract": true
}

```

---

<div class="post-metadata">

**Author:** ![HeptaSean](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/heptasean/32/82306_2.png) [@HeptaSean](https://forum.cardano.org/u/HeptaSean)\
**Post date:** [27 April 2024 11:53 UTC](https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896/3 "2024-04-27T11:53:02Z")

</div>

Looks like the one on-chain has a TTL/`invalid_hereafter` set and the one you build does not. That might be the difference leading to a different hash.

---

<div class="post-metadata">

**Author:** ![Rahat](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/rahat/32/84546_2.png) [@Rahat](https://forum.cardano.org/u/Rahat)\
**Post date:** [27 April 2024 12:18 UTC](https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896/4 "2024-04-27T12:18:47Z")

</div>

```bash
const txBuilder = WASM_lib.TransactionBuilder.new(txBuilderCfg);
txBuilder.set_ttl(410021); 

  const txBody = WASM_lib.TransactionBody.new(
    transactionInputs,
    transactionOutputs,
    WASM_lib.BigNum.from_str(fee.toString()),
    txBuilder.set_ttl(58517927)
    
  );

```

but the ttl was null  
can you show me correct way to add ttl I used method showed in this example: (example)[[generating-transactions | Cardano Developer Portal](https://developers.cardano.org/docs/get-started/cardano-serialization-lib/generating-transactions)]

but I am using slight different approach to create transaction and I do not find set\_ttl function for this approach

THANKS FOR THE REPLY

---

<div class="post-metadata">

**Author:** ![HeptaSean](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/heptasean/32/82306_2.png) [@HeptaSean](https://forum.cardano.org/u/HeptaSean)\
**Post date:** [27 April 2024 12:33 UTC](https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896/5 "2024-04-27T12:33:23Z")

</div>

According to [https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/pkg/cardano\_serialization\_lib.js.flow#L8086-L8089](https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/pkg/cardano_serialization_lib.js.flow#L8086-L8089) and [https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/pkg/cardano\_serialization\_lib.js.flow#L8248-L8278](https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/pkg/cardano_serialization_lib.js.flow#L8248-L8278), the `TransactionBody` object has a `set_ttl` method (and `new` is deprecated).

So, the code should be something like:

```javascript
const txBody = WASM_lib.TransactionBody.new_tx_body(
    transactionInputs,
    transactionOutputs,
    WASM_lib.BigNum.from_str(fee.toString())
)
txBody.set_ttl(WASM_lib.BigNum.from_str('58517927'))

```

(Didn’t test, 'cause not using CSL daily and don’t have it set up right now.)

---

<div class="post-metadata">

**Author:** ![Rahat](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.cardano.org/rahat/32/84546_2.png) [@Rahat](https://forum.cardano.org/u/Rahat)\
**Post date:** [27 April 2024 12:44 UTC](https://forum.cardano.org/t/hash-mismatch-for-cardano-serialization-library/130896/6 "2024-04-27T12:44:04Z")

</div>

> [@HeptaSean](#):
>
> `txBody.set_ttl(WASM_lib.BigNum.from_str('58517927'))`

this worked

#### thanks for the help
