Skip to main content

Keypair

MicroSui Object-style for handling Ed25519 keypairs in MicroSui.

Provides a small OO-like API (struct + function pointers) to work with Ed25519 keypairs in the context of the Sui blockchain.
This module provides core cryptographic utilities for creating and managing Sui-compatible keypairs and signatures.

Keypair capabilities: Generation of random Keypairs from fresh entropy, Initialize a keypair from a Bech32-encoded secret key string, Securely sign transaction messages using Ed25519, Retrieve the secret key (Bech32), public key (raw bytes), and derive the Sui-formatted address.

IMPORTANT NOTE:
Compatibility & Support: This class is fully supported and production-stable (as of this documentation release) on any microcontroller and operating system.
⚠️ Functions returning strings or buffers use static internal storage. Results will be overwritten by subsequent calls and are not thread-safe.

Inspired by the Mysten Labs TypeScript SDK, adapted for embedded C.


File Location

Defined in microsui-lib/src/Keypair.c


Indexs

Constructors

Methods


Constructors

SuiKeypair_generate

SuiKeypair_generate(uint8_t seed_extra): MicroSuiEd25519

Generate a new Ed25519 keypair for Sui.

Creates a MicroSuiEd25519 instance with a random secret key derived from the current system time and an extra seed value. Function pointers for signing, retrieving keys, and deriving the Sui address are also assigned.

Parameters

NameTypeDescription
seed_extrauint8_tExtra seed mixed with current time to initialize PRNG

Returns

Example usage

uint8_t seed_extra = 234584;    // In embedded systems, it's recommended to read the
// internal temperature sensor of the microcontroller,
// which most microcontrollers have.

MicroSuiEd25519 keypair = SuiKeypair_generate(seed_extra);

SuiKeypair_fromSecretKey

SuiKeypair_fromSecretKey(const char *sk): MicroSuiEd25519

Initialize a MicroSuiEd25519 keypair from a Bech32 secret key string.

Decodes the provided Bech32 private key string and stores it in the new created struct.

Parameters

NameTypeDescription
skconst char *Bech32-encoded secret key string

Returns

Example usage

const char* sui_secret_key_bech32 = "suiprivkey1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq509duq";

MicroSuiEd25519 keypair = SuiKeypair_fromSecretKey(sui_secret_key_bech32);

Methods

signTransaction

signTransaction(MicroSuiEd25519 *self, const char *msg): SuiSignature

Sign a transaction and execute it via the Sui RPC.

Sign a transaction message with the Ed25519 private key.
Converts a hex-encoded message string to bytes, signs it with Ed25519, and encodes the signature into Base64 format.

Parameters

NameTypeDescription
selfMicroSuiEd25519 *Pointer to MicroSuiEd25519 instance
msgconst char *Hex-encoded transaction message string

Returns

Example usage

Given:

  • A valid secret key in bech32 format sui_secret_key_bech32: const char * type.
  • A valid Message to Sign string (in hexadecimal format) messageToSign: const char * type.

We can generate the signature as following:

const char* sui_secret_key_bech32 = "suiprivkey1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq509duq";
const char* messageToSign = "0000020008c0d8a700202e3d52393c9035afd1ef38abd7fce2dad71f0e276b522fb274f4e14d1d";

MicroSuiEd25519 keypair = SuiKeypair_fromSecretKey(sui_secret_key_bech32);

SuiSignature sig = keypair.signTransaction(&keypair, messageToSign);

if (sig.signature != NULL)
// Is a valid Signature

toSuiAddress

toSuiAddress(MicroSuiEd25519 *self): const char *

Derive the Sui address from the Ed25519 public key.

Computes the public key from the secret key, encodes it into a Sui address, and formats it as a hex string with the "0x" prefix.
The returned string is stored in a static buffer and will be overwritten by subsequent calls.

Parameters

NameTypeDescription
selfMicroSuiEd25519 *Pointer to MicroSuiEd25519 instance

Returns

  • const char * string.

Example usage

Given:

  • A valid secret key in bech32 format sui_secret_key_bech32: const char * type.
const char* sui_secret_key_bech32 = "suiprivkey1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq509duq";

MicroSuiEd25519 keypair = SuiKeypair_fromSecretKey(sui_secret_key_bech32);
const char* suiAddress = keypair.toSuiAddress(&keypair);

if (suiAddress != NULL)
// Sui Address is valid

getPublicKey

getPublicKey(MicroSuiEd25519 *self): const uint8_t *

Get the public key derived from the private key.

Derives the 32-byte Ed25519 public key corresponding to the internal secret key.
The returned buffer is static and will be overwritten by subsequent calls.

Parameters

NameTypeDescription
selfMicroSuiEd25519 *Pointer to MicroSuiEd25519 instance

Returns

  • const uint8_t * string.

Example usage

  • A valid secret key in bech32 format sui_secret_key_bech32: const char * type.
const char* sui_secret_key_bech32 = "suiprivkey1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq509duq";

MicroSuiEd25519 keypair = SuiKeypair_fromSecretKey(sui_secret_key_bech32);
const uint8_t* publicKey = keypair.getPublicKey(&keypair);

if (publicKey != NULL)
// Public Key is valid

getSecretKey

getSecretKey(MicroSuiEd25519 *self): const char *

Get the secret key in Bech32 string format.

Encodes the internal 32-byte secret key into a Bech32-encoded string.
The returned string is stored in a static buffer and will be overwritten by subsequent calls.

Parameters

NameTypeDescription
selfMicroSuiEd25519 *Pointer to MicroSuiEd25519 instance

Returns

  • const char * string.

Example usage

Given:

  • A valid secret key in bech32 format sui_secret_key_bech32: const char * type.
const char* sui_secret_key_bech32 = "suiprivkey1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq509duq";

MicroSuiEd25519 keypair = SuiKeypair_fromSecretKey(sui_secret_key_bech32);
const char* secretKey = keypair.getSecretKey(&keypair);

if (secretKey != NULL)
// Secret Key is valid