Skip to main content

Broadcast Transaction to Sui Network

Sends a fully constructed transaction (including the Sui message and its signature) to a Sui Network node via HTTP.

note

⚠️ This operation REQUIRES the microcontroller to support HTTP (HTTP POST) and to be connected to the internet in order to communicate with a Sui API node. Therefore, the microcontroller must either natively include these capabilities or be connected to a module or device that provides them.

note

All ESP32 variants provide full compatibility for this operation when using Arduino IDE.

tip

⚠️ If your microcontroller includes an HTTP stack and internet connectivity, you are responsible for implementing the device-specific code required to perform the HTTP POST request using the payload generated by microsui-lib. In the final example, you will see exactly where this code should be placed.

ESP32 Fully supported example - (adaptation for ArduinoIDE)

note

⚠️ ATTENTION: This code only works for ESP32 by the moment!

This chunk of code is fully functional in Arduino IDE for ESP32 boards and nothing needs to be edited or added (except for the initial constants).

// 0) Constants
// --- WiFi credentials (example) ---
const char* wifi_ssid = "myWiFiSSID";
const char* wifi_pass = "myWiFiPass";
// --- Sui essentials for this example ---
const char* rpc_url = "https://fullnode.testnet.sui.io:443/"; // Sui RPC URL (testnet in this example)
const char* sui_private_key_bech32 = "suiprivkey1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq509duq";
const char* message_string = "48656c6c6f2053756921"; // which is "Hello Sui!", but in this case must be bytes from a real transaction

// 1) Connect WiFi
MicroSuiWiFi wifi = WiFi_connect(wifi_ssid, wifi_pass);

// 2) Generate RPC client
MicroSuiClient client = SuiClient_newClient(rpc_url);
Serial.print(F(" DONE! - Created Client with RPC URL: ")); Serial.println(rpc_url);

// 3) Load keypair from Bech32 secret key
MicroSuiEd25519 keypair = SuiKeypair_fromSecretKey(sui_private_key_bech32);
Serial.print(F(" DONE! - Created KeyPair - userAddress: ")); Serial.println(keypair.toSuiAddress(&keypair));

// 4) Load prebuilt TxBytes (generated externally)
MicroSuiTransaction tx = SuiTransaction_setPrebuiltTxBytes(message_string);
Serial.println(F(" DONE! - Created a Sui Transaction object with prebuilt TxBytes"));

// 5) Sign + Execute (simplest way: client handles the signature internally)
SuiTransactionBlockResponse res1 = client.signAndExecuteTransaction(&client, keypair, tx);
Serial.print(F(" DONE! - Signed Transaction sended to Sui Network!!"));

Serial.println(F("\n\n --- RPC Response - Transaction Result ---"));
if (res1.digest) { Serial.print(F(" Digest (Tx ID): ")); Serial.println(res1.digest); }
if (res1.checkpoint) { Serial.print(F(" Checkpoint: ")); Serial.println(res1.checkpoint); }
if (res1.confirmedLocalExecution) { Serial.print(F(" ConfirmedLocalExecution: ")); Serial.println(res1.confirmedLocalExecution); }

Serial.print(F(" BalanceChanges count: "));
Serial.println(res1.balanceChanges_len);

// Cleaning memory after transaction
tx.clear(&tx); // Clean transaction (important to avoid memory leaks)
wifi.disconnect(&wifi); // Disconnect WiFi

Any microcontroller with HTTP stack and internet connection - (pure C code)

This chunk of code requires add your own HTTP POST implementation after the generation of the payload string.

// Message and Signature previously generated
const char* sui_message = "0000020008c0320a030000000000202e3d52393c9035afd1ef38abd7fce2dad71f0e276b522fb274f4e14d1df974720202000101000001010300000000010100d79a4c7a655aa80cf92069bbac9666705f1d7181ff9c2d59efbc7e6ec4c3379d0115bc7e3113dfaebc7bdb30676e56e6ff651365235b74a202cbb1e73f57eaeb78600fd0140000000020c00463b22a32bebcb028b264d61bfa963c3f6a82bd7487c52bec8b2bf0c0e373d79a4c7a655aa80cf92069bbac9666705f1d7181ff9c2d59efbc7e6ec4c3379de80300000000000040ab3c000000000000";
const char* sui_signature = "004131446a8541b3c408e18054f2e175f1b04a93f6ab1bbf38d1234a3b6ed65adf508e9a03555269222536e0ecdbf4d496de7212c5e2f9c4dec2251226b7fcf8036309ede0a480229c12b578364ca13bc36cbf61786c47aa75f323127462b32405";
// RPC NODE
const char* host = "fullnode.testnet.sui.io";
const char* path = "/";
int port = 443;

// Convert the Sui Signature from hex to bytes
uint8_t signature_bytes[132];
hex_to_bytes(sui_signature, signature_bytes, sizeof(signature_bytes));

// Convert the Sui Message from hex to bytes
size_t message_bytes_len = strlen(sui_message) / 2; // 2 hex chars = 1 byte
uint8_t message_bytes[message_bytes_len];
hex_to_bytes(sui_message, message_bytes, message_bytes_len);

// Prepare the JSON string for executing the transaction block
char* json = microsui_prepare_executeTransactionBlock(signature_bytes, message_bytes, message_bytes_len);
if (!json) {
printf("Failed to generate JSON.\n");
return -1;
}
printf("\t JSON prepared to send via RPC by method `sui_executetransactionblock`:\n %s\n\n", json);

// Send the JSON via HTTP POST request
printf("\t Sending HTTP POST request to %s:%d%s\n", host, port, path);

//// YOU HAVE TO IMPLEMENT HERE YOUR OWN HTTP POST implementation (According to your microcontroller)
//
// char* response = your_microcontroller_http_post(host, path, port, json);
//
////

if (response != NULL) {
printf("HTTP POST success!\n");
printf("\n\t Sui Network RPC Response:\n%s\n", response);
} else {
printf("\n HTTP POST failed\n");
}

free(json); // Free the JSON string after use (very important to avoid memory leaks)
free(response); // Free the response string after use (very important to avoid memory leaks)
tip

MicroSui is open-source, and new platform implementations are welcome via PRs in the microsui-lib repository.