<p>This answer is refferenced from <a href="https://cardanoupdates.com/commits/6af4284b32841a13ebe86fdcc6f10dd1d74ddfdc" rel="nofollow noreferrer">https://cardanoupdates.com/commits/6af4284b32841a13ebe86fdcc6f10dd1d74ddfdc</a> and I have used similar code in production. I haven't tested this function but you should get the idea.</p>
<pre><code>buildSendADATransaction = async () =&gt; {
const messageData = {
    receiver_id: &quot;SJKdj34k3jjKFDKfjFUDfdjkfd&quot;,
    sender_id: &quot;jkfdsufjdk34h3Sdfjdhfduf873&quot;,
    comment: &quot;happy birthday&quot;,
    tags: [0, 264, -1024, 32],
};

const txBuilder = await this.initTransactionBuilder();
const shelleyOutputAddress = Address.from_bech32(
    this.state.addressBech32SendADA
);
const shelleyChangeAddress = Address.from_bech32(this.state.changeAddress);

txBuilder.add_output(
    TransactionOutput.new(
        shelleyOutputAddress,
        Value.new(BigNum.from_str(this.state.lovelaceToSend.toString()))
    )
);

/////////////////////////////////
// Add metadata here

txBuilder.add_json_metadatum(
    BigNum.from_str(&quot;674&quot;),
    JSON.stringify(messageData)
);
/////////////////////////////////


const txUnspentOutputs = await this.getTxUnspentOutputs();
txBuilder.add_inputs_from(txUnspentOutputs, 1);
txBuilder.add_change_if_needed(shelleyChangeAddress);
const txBody = txBuilder.build();

////////////////////////////
// Create unsigned tx with metadata included

const unsignedTransaction = txBuilder.build_tx();
/////////////////////////////

const transactionWitnessSet = TransactionWitnessSet.new();
const tx = Transaction.new(
    txBody,
    TransactionWitnessSet.from_bytes(transactionWitnessSet.to_bytes()),
    ///
    unsignedTransaction.auxiliary_data() // Include metadata

);
let txVkeyWitnesses = await this.API.signTx(
    Buffer.from(tx.to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;),
    true
);
txVkeyWitnesses = TransactionWitnessSet.from_bytes(
    Buffer.from(txVkeyWitnesses, &quot;hex&quot;)
);
transactionWitnessSet.set_vkeys(txVkeyWitnesses.vkeys());

const txBodyFinal = tx.body();
const signedTx = Transaction.new(
    txBodyFinal, 
    transactionWitnessSet,
    ///
    unsignedTransaction.auxiliary_data() // Include metadata

);

const submittedTxHash = await this.API.submitTx(
    Buffer.from(signedTx.to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;)
);
console.log(submittedTxHash);
this.setState({ submittedTxHash });};
</code></pre>

CC BY-SA 4.0
2022-10-02T12:43:25.010Z
2022-10-02T12:43:25.010Z
jsyme
<p>If you are open to integrating with wallets other than Nami Wallet,</p>
<p>Typhon Wallet DApp connector has simple, ready-to-call methods to build a transaction with metadata and/or tokens.</p>
<p>Below is transaction method that adds metadata to a transaction.</p>
<pre><code>const auxiliaryDataCbor = typhonjs.utils
  .createAuxiliaryDataCbor({
    metadata: [
      {
        label: 674,
        data: {
          msg: [&quot;This is a comment for the transaction xyz, thank you very much!&quot;],
        },
      },
    ],
  })
  .toString(&quot;hex&quot;);

const paymentTransactionResponse = await typhon.paymentTransaction({
  auxiliaryDataCbor: auxiliaryDataCbor,
  outputs: [
    {
      address:
        &quot;addr_test1qz7jw975stagnvs00wsjny6y6gpazn86yvwcm2vy02j3up7mt68vuzvz4nzgs00x0shrgywvy674v6r2zcs8fxvvq27qfjq8np&quot;,
      amount: &quot;6000000&quot;,
    },
  ],
});
</code></pre>
<p>Example at <a href="https://docs.typhonwallet.io/examples/paymentTransaction.html#sending-metadata" rel="nofollow noreferrer">https://docs.typhonwallet.io/examples/paymentTransaction.html#sending-metadata</a></p>
<p><a href="https://typhonwallet.io" rel="nofollow noreferrer">https://typhonwallet.io</a></p>

CC BY-SA 4.0
2022-03-17T06:16:31.623Z
Yeah, I saw that -- but I'd prefer to stick with CSL, any thoughts?
Teddy Pender
CC BY-SA 4.0
2022-03-15T05:47:59.943Z
2022-03-15T05:47:59.943Z
Mehul Prajapati
<p>I've forked a dApp Connector I found on Github and I'm trying to add some transaction metadata to the ADA transaction it builds. This is what I currently have:</p>
<pre><code>buildSendADATransaction = async () =&gt; {
    //metadata
    const messageData = {
      receiver_id: &quot;SJKdj34k3jjKFDKfjFUDfdjkfd&quot;,
      sender_id: &quot;jkfdsufjdk34h3Sdfjdhfduf873&quot;,
      comment: &quot;happy birthday&quot;,
      tags: [0, 264, -1024, 32],
    };
    const generalMetadata = GeneralTransactionMetadata.new();
    console.log(Buffer.from(generalMetadata.to_bytes(), &quot;hex&quot;).toString(&quot;hex&quot;));

    generalMetadata.insert(
      BigNum.from_str(&quot;674&quot;),
      encode_json_str_to_metadatum(JSON.stringify(messageData))
    );
    const _metadata = AuxiliaryData.new();
    _metadata.set_metadata(generalMetadata);

    console.log(`the metadata is ${_metadata.metadata()}`);

    // build tx
    const txBuilder = await this.initTransactionBuilder();

    const shelleyOutputAddress = Address.from_bech32(
      this.state.addressBech32SendADA
    );
    const shelleyChangeAddress = Address.from_bech32(this.state.changeAddress);

    txBuilder.add_output(
      TransactionOutput.new(
        shelleyOutputAddress,
        Value.new(BigNum.from_str(this.state.lovelaceToSend.toString()))
      )
    );

    // Find the available UTXOs in the wallet and
    // use them as Inputs
    const txUnspentOutputs = await this.getTxUnspentOutputs();
    txBuilder.add_inputs_from(txUnspentOutputs, 1);

    // calculate the min fee required and send any change to an address
    txBuilder.add_change_if_needed(shelleyChangeAddress);

    // once the transaction is ready, we build it to get the tx body without witnesses
    const txBody = txBuilder.build();
    txBody.set_auxiliary_data_hash(hash_auxiliary_data(_metadata));

    // Tx witness
    const transactionWitnessSet = TransactionWitnessSet.new();

    const tx = Transaction.new(
      txBody,
      TransactionWitnessSet.from_bytes(transactionWitnessSet.to_bytes()),
      _metadata
    );

    let txVkeyWitnesses = await this.API.signTx(
      Buffer.from(tx.to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;),
      true
    );
    txVkeyWitnesses = TransactionWitnessSet.from_bytes(
      Buffer.from(txVkeyWitnesses, &quot;hex&quot;)
    );

    transactionWitnessSet.set_vkeys(txVkeyWitnesses.vkeys());

    const txBodyFinal = tx.body();
    const signedTx = Transaction.new(
      txBodyFinal,
      transactionWitnessSet,
      _metadata
    );

    const submittedTxHash = await this.API.submitTx(
      Buffer.from(signedTx.to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;)
    );
    console.log(submittedTxHash);
    this.setState({ submittedTxHash });
  };
</code></pre>
<p>It constructs the transaction just fine with a wallet like Nami, but there's no metadata attached to that transaction. Any thoughts?</p>

CC BY-SA 4.0
2022-03-15T03:15:36.720Z
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Community
<metadata><cardano-serialization-lib><transaction-metadata>
Submitting Transaction with CSL Metadata Isn't Showing
# Submitting Transaction with CSL Metadata Isn't Showing

(Asked by: username_0 on 2022-03-13)

I've forked a dApp Connector I found on Github and I'm trying to add some transaction metadata to the ADA transaction it builds. This is what I currently have:
buildSendADATransaction = async () => {
    //metadata
    const messageData = {
      receiver_id: "SJKdj34k3jjKFDKfjFUDfdjkfd",
      sender_id: "jkfdsufjdk34h3Sdfjdhfduf873",
      comment: "happy birthday",
      tags: [0, 264, -1024, 32],
    };
    const generalMetadata = GeneralTransactionMetadata.new();
    console.log(Buffer.from(generalMetadata.to_bytes(), "hex").toString("hex"));

    generalMetadata.insert(
      BigNum.from_str("674"),
      encode_json_str_to_metadatum(JSON.stringify(messageData))
    );
    const _metadata = AuxiliaryData.new();
    _metadata.set_metadata(generalMetadata);

    console.log(`the metadata is ${_metadata.metadata()}`);

    // build tx
    const txBuilder = await this.initTransactionBuilder();

    const shelleyOutputAddress = Address.from_bech32(
      this.state.addressBech32SendADA
    );
    const shelleyChangeAddress = Address.from_bech32(this.state.changeAddress);

    txBuilder.add_output(
      TransactionOutput.new(
        shelleyOutputAddress,
        Value.new(BigNum.from_str(this.state.lovelaceToSend.toString()))
      )
    );

    // Find the available UTXOs in the wallet and
    // use them as Inputs
    const txUnspentOutputs = await this.getTxUnspentOutputs();
    txBuilder.add_inputs_from(txUnspentOutputs, 1);

    // calculate the min fee required and send any change to an address
    txBuilder.add_change_if_needed(shelleyChangeAddress);

    // once the transaction is ready, we build it to get the tx body without witnesses
    const txBody = txBuilder.build();
    txBody.set_auxiliary_data_hash(hash_auxiliary_data(_metadata));

    // Tx witness
    const transactionWitnessSet = TransactionWitnessSet.new();

    const tx = Transaction.new(
      txBody,
      TransactionWitnessSet.from_bytes(transactionWitnessSet.to_bytes()),
      _metadata
    );

    let txVkeyWitnesses = await this.API.signTx(
      Buffer.from(tx.to_bytes(), "utf8").toString("hex"),
      true
    );
    txVkeyWitnesses = TransactionWitnessSet.from_bytes(
      Buffer.from(txVkeyWitnesses, "hex")
    );

    transactionWitnessSet.set_vkeys(txVkeyWitnesses.vkeys());

    const txBodyFinal = tx.body();
    const signedTx = Transaction.new(
      txBodyFinal,
      transactionWitnessSet,
      _metadata
    );

    const submittedTxHash = await this.API.submitTx(
      Buffer.from(signedTx.to_bytes(), "utf8").toString("hex")
    );
    console.log(submittedTxHash);
    this.setState({ submittedTxHash });
  };

It constructs the transaction just fine with a wallet like Nami, but there's no metadata attached to that transaction. Any thoughts?

**Tags:** metadata, cardano-serialization-lib, transaction-metadata
> **Comments:**
> **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  

---

## 2 answers

### Answer by username_2 on 2022-10-02. Score: 0

This answer is refferenced from https://cardanoupdates.com/commits/6af4284b32841a13ebe86fdcc6f10dd1d74ddfdc and I have used similar code in production. I haven't tested this function but you should get the idea.
buildSendADATransaction = async () => {
const messageData = {
    receiver_id: "SJKdj34k3jjKFDKfjFUDfdjkfd",
    sender_id: "jkfdsufjdk34h3Sdfjdhfduf873",
    comment: "happy birthday",
    tags: [0, 264, -1024, 32],
};

const txBuilder = await this.initTransactionBuilder();
const shelleyOutputAddress = Address.from_bech32(
    this.state.addressBech32SendADA
);
const shelleyChangeAddress = Address.from_bech32(this.state.changeAddress);

txBuilder.add_output(
    TransactionOutput.new(
        shelleyOutputAddress,
        Value.new(BigNum.from_str(this.state.lovelaceToSend.toString()))
    )
);

/////////////////////////////////
// Add metadata here

txBuilder.add_json_metadatum(
    BigNum.from_str("674"),
    JSON.stringify(messageData)
);
/////////////////////////////////


const txUnspentOutputs = await this.getTxUnspentOutputs();
txBuilder.add_inputs_from(txUnspentOutputs, 1);
txBuilder.add_change_if_needed(shelleyChangeAddress);
const txBody = txBuilder.build();

////////////////////////////
// Create unsigned tx with metadata included

const unsignedTransaction = txBuilder.build_tx();
/////////////////////////////

const transactionWitnessSet = TransactionWitnessSet.new();
const tx = Transaction.new(
    txBody,
    TransactionWitnessSet.from_bytes(transactionWitnessSet.to_bytes()),
    ///
    unsignedTransaction.auxiliary_data() // Include metadata

);
let txVkeyWitnesses = await this.API.signTx(
    Buffer.from(tx.to_bytes(), "utf8").toString("hex"),
    true
);
txVkeyWitnesses = TransactionWitnessSet.from_bytes(
    Buffer.from(txVkeyWitnesses, "hex")
);
transactionWitnessSet.set_vkeys(txVkeyWitnesses.vkeys());

const txBodyFinal = tx.body();
const signedTx = Transaction.new(
    txBodyFinal, 
    transactionWitnessSet,
    ///
    unsignedTransaction.auxiliary_data() // Include metadata

);

const submittedTxHash = await this.API.submitTx(
    Buffer.from(signedTx.to_bytes(), "utf8").toString("hex")
);
console.log(submittedTxHash);
this.setState({ submittedTxHash });};


---

### Answer by username_3 on 2022-03-15. Score: 0

If you are open to integrating with wallets other than Nami Wallet,
Typhon Wallet DApp connector has simple, ready-to-call methods to build a transaction with metadata and/or tokens.
Below is transaction method that adds metadata to a transaction.
const auxiliaryDataCbor = typhonjs.utils
  .createAuxiliaryDataCbor({
    metadata: [
      {
        label: 674,
        data: {
          msg: ["This is a comment for the transaction xyz, thank you very much!"],
        },
      },
    ],
  })
  .toString("hex");

const paymentTransactionResponse = await typhon.paymentTransaction({
  auxiliaryDataCbor: auxiliaryDataCbor,
  outputs: [
    {
      address:
        "addr_test1qz7jw975stagnvs00wsjny6y6gpazn86yvwcm2vy02j3up7mt68vuzvz4nzgs00x0shrgywvy674v6r2zcs8fxvvq27qfjq8np",
      amount: "6000000",
    },
  ],
});

Example at https://docs.typhonwallet.io/examples/paymentTransaction.html#sending-metadata
https://typhonwallet.io

> **Comments:**
> **username_0**: Yeah, I saw that -- but I'd prefer to stick with CSL, any thoughts?  

---

