Rust sdk example to call init and receive (update) functions

Hi,

I’m starting to create a Rust REST component that interfaces with my concordium smart contract.
I’ve checked the concordium-rust-sdk examples but I cannot find any example code that does an init and call a receive update function of a smart contract. Do you have something you can share that calls a smart contract: init, call a receive function with json string using the concordium-rust-sdk?

I may be able to get some hints from the cics2 code in the concordium-rust-sdk lib – i.e., the transfer and updateOperator (can’t find init call anywhere), but wonder if someone has a more straightforward example that can guide me? Thanks.

Hi @ulapcyber,

That sounds great!

I’ll create a small example that initializes and updates a contract, which I’ll put in the rust-sdk examples folder. I’ll share it with you tomorrow :blush:

/ Kasper

Thanks Kasper! Looking forward to it. :slight_smile:

Hi @ulapcyber

I’ve added an example that shows how to initialise and update a smart contract with the rust-sdk. You can see it here: https://github.com/Concordium/concordium-rust-sdk/blob/main/examples/init-update-contract.rs.

Depending on your needs, you might not need to pass in JSON data for the parameter. Instead, you could get the data via the URL parameters. In the example that I created, it might be something like http://example.com/update?weather=sunny.

However, if you want to pass in the JSON parameter, then you will need to do something like the following:

  1. Create a data type for the parameter and derive SerdeDeserialize and Serial (from concordium-contracts-common) for it.
  2. Get the JSON string
    • Either directly as a string,
    • or via a file, which is read to a string with fx std::fs::read_to_string
  3. Deserialize the JSON into the data type with serde_json::from_str.
  4. Serialize the data type to binary with concordium_contracts_common::to_bytes (or concordium_contracts_common::Serial::serial if you want to create the buffer yourself).
  5. Create the Parameter type with the binary data. Fx with Parameter::from(the_bytes) as I do in the example.

Let me know if you have any further questions :blush:

Have a nice day!
/ Kasper

1 Like

Great! Thank you for this @Kasper

Hi @Kasper
I managed to looked into this today. Got stuck in coming up with the keys.json. I finally figured out the format (i.e., the accountKeys) but I could not find a way to get the “signKey”. I can only get the encryptedSignKey by running “config show”. Can you please help how I can convert this to a hex encoded signKey? Is there a tool I can use? I checked the wallet-notes README.md and there is a “get_account_keys_and_randomness” that can generate the signKey, but got 404s for the example jsons (input/output). Is this for the example.c and is this the tool to use to get the signing key? I have not tried compiling but I wonder if it will work on a Mac m1?

Hi,

Here’s what you need to do to get the unencrypted account keys:

  1. Export your keys (make backup) from the mobile wallet and send the file, fx concordium-backup.concordiumwallet to your computer.
  2. Follow this guide to decrypt the exported file.
    • Essentially: download the linked tool, then run utils decrypt --in concordium-backup.concordiumwallet --out decrypted.json
    • The generated file decrypted.json will contain the data you need plus some additional data. Among other things, it contains a list of identities, and for each identity, there is a list of accounts created with that identity.
  3. Find the account you are interested in using inside decrypted.json and extract the address and accountKeys fields to a new file, fx keys.json:
..
        "accounts": [
          {
            "address": "ADDRESS_OF_INTEREST",     // Copy this field
            "encryptionSecretKey": "...",
            "credential": {
              "v": 0,
              "value": {
                "messageExpiry": 1660634928,
                "credential": {
                  "type": "initial",
                  "contents": {
                    "ipIdentity": 0,
                    "sig": "...",
                    "credentialPublicKeys": {
                      "keys": {
                        "0": {
                          "schemeId": "Ed25519",
                          "verifyKey": "..."
                        }
                      },
                      "threshold": 1
                    },
                    "regId": "...",
                    "policy": {
                      "createdAt": "202208",
                      "validTo": "202308",
                      "revealedAttributes": {
                      }
                    }
                  }
                }
              }
            },
            "accountKeys": {                   // And copy this field
              "keys": {
                "0": {
                  "keys": {
                    "0": {
                      "signKey": "...",
                      "verifyKey": "."
                    }
                  },
                  "threshold": 1
                }
              },
              "threshold": 1
            },
...

That should leave you with the exact file that you need :wink:

Let me know if you run into any problems :blush:

/ Kasper

2 Likes

Ah, auxiliary tools page! I’ve not researched enough. :frowning: I will try it out.
Thanks for the patience and the help!

worked! BTW, I used “utils decrypt” since the one I imported from mobile in the past is already encrypted. Thanks again.

1 Like

Great!

Ah, it was a small, but significant, typo. You definitely need to decrypt the mobile export :wink: (I’ve fixed the typo in my previous message).

I’m glad that I could help!

Have a pleasant afternoon.

/ Kasper

1 Like