Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jinglescode/f925ee4659d5b3ab0a17eb426eb5f89a to your computer and use it in GitHub Desktop.
Save jinglescode/f925ee4659d5b3ab0a17eb426eb5f89a to your computer and use it in GitHub Desktop.

About transaction building with JSON

The goal is to pass a JSON object to the Rust code, which will build a transaction and return the serialized transaction in hex format. This is used between Mesh and the Rust code.

Instead of build transactions directly with cardano-serialization-lib, we build the whisky package that wraps the cardano-serialization-lib package. Doing so, this allow us to create useful functions that can be used in the Rust code base.

Then, we create a JSON parser in Rust that can parse JSON objects and build transactions with the whisky package.

Example

Example of a txBody in JSON format:

{
  inputs: [
    {
      type: "PubKey",
      txIn: {
        txHash:
          "1662c4b349907e4d92e0995fd9dcdc9a4489f7dff4f5cce6b4b3901de479308c",
        txIndex: 14,
        amount: [
          {
            unit: "lovelace",
            quantity: "774643176",
          },
        ],
        address:
          "addr_test1qq0yavv5uve45rwvfaw96qynrqt8ckpmkwcg08vlwxxdncxk82f5wz75mzaesmqzl79xqsmedwgucwtuav5str6untqqmykcpn",
      },
    },
  ],
  outputs: [
    {
      address:
        "addr_test1qq0yavv5uve45rwvfaw96qynrqt8ckpmkwcg08vlwxxdncxk82f5wz75mzaesmqzl79xqsmedwgucwtuav5str6untqqmykcpn",
      amount: [
        {
          unit: "lovelace",
          quantity: "1231231",
        },
      ],
    },
  ],
  extraInputs: [
    {
      input: {
        outputIndex: 14,
        txHash:
          "1662c4b349907e4d92e0995fd9dcdc9a4489f7dff4f5cce6b4b3901de479308c",
      },
      output: {
        address:
          "addr_test1qq0yavv5uve45rwvfaw96qynrqt8ckpmkwcg08vlwxxdncxk82f5wz75mzaesmqzl79xqsmedwgucwtuav5str6untqqmykcpn",
        amount: [
          {
            unit: "lovelace",
            quantity: "774643176",
          },
        ],
      },
    },
    {
      input: {
        outputIndex: 54,
        txHash:
          "43cdd76f2b74d31e56813276b695b64fe91daac195ac37ac3b4a4b44b405f3bf",
      },
      output: {
        address:
          "addr_test1qq0yavv5uve45rwvfaw96qynrqt8ckpmkwcg08vlwxxdncxk82f5wz75mzaesmqzl79xqsmedwgucwtuav5str6untqqmykcpn",
        amount: [
          {
            unit: "lovelace",
            quantity: "390216112",
          },
        ],
      },
    },
  ],
  selectionThreshold: 5000000,
  collaterals: [],
  requiredSignatures: [],
  referenceInputs: [],
  mints: [],
  changeAddress:
    "addr_test1qq0yavv5uve45rwvfaw96qynrqt8ckpmkwcg08vlwxxdncxk82f5wz75mzaesmqzl79xqsmedwgucwtuav5str6untqqmykcpn",
  metadata: [],
  validityRange: {},
  certificates: [],
  withdrawals: [],
  signingKey: [],
}

Together with the protocol params in JSON format, is then passed to the js_serialize_tx_body function in the sidan-csl-rs package (the WASM of the core logic of whisky package).

In Rust, parse the JSON, build the transaction and return the serialized transaction in hex format:

const txHex = serializer.js_serialize_tx_body(txBodyJson, params);
// txHex = 84a300818258201662c4b349907e4d92e0995fd9dcdc9a4489f7dff4f5cce6b4b3901de479308c0e0182825839001e4eb194e3335a0dcc4f5c5d009318167c583bb3b0879d9f718cd9e0d63a93470bd4d8bb986c02ff8a6043796b91cc397ceb29058f5c9ac01a0012c97f825839001e4eb194e3335a0dcc4f5c5d009318167c583bb3b0879d9f718cd9e0d63a93470bd4d8bb986c02ff8a6043796b91cc397ceb29058f5c9ac01a2e16c3f4021a00029075a0f5f6

About Mesh v2

Mesh v2 focused on modularizing the serialization layers and provide various developer facing packages:

  • @meshsdk/core (typescript):
    • for all JS frontend frameworks
    • rely on cardano-sdk and stricahq packages to parse CBOR and build transactions.
  • @meshsdk/core-csl (webassembly):
    • for JS backends
    • wraps the sidan-csl-rs package which is a WASM of whisky
    • provide useful functions to build transactions and parse CBOR objects in Rust
    • exposes the functions to JavaScript, building transactions via JSON objects
  • whisky (Rust):
    • wraps the cardano-serialization-lib package
    • provide useful functions to build transactions and parse CBOR objects in Rust
    • think of Mesh written in Rust
    • we can test CSL completeness, and build helpers that CSL is missing

All 3 packages have very similar functions. With Mesh providing multiple wallets and aceess to community projects service providers, while the Rust package is focused on building transactions and parsing CBOR objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment