@somnia-chain/markets-sdk


@somnia-chain/markets-sdk / native / sessionPrivateKey

Function: sessionPrivateKey()

sessionPrivateKey(seed): `0x${string}`

Defined in: packages/sdk/src/native/session.ts:76

Derive a session's private key from its seed, exactly as the node does.

for i = 0, 1, 2, …:
  candidate = keccak256(seed ‖ uint64_le(i))
  if 0 < candidate < secp256k1_n: return candidate

The loop exists only for completeness: a 256-bit keccak output falls outside [1, n-1] with probability under 2^-128, so i = 0 returns in every case anyone will ever observe. (Because i = 0 encodes to eight zero bytes, its endianness is unobservable; the node documents little-endian and that is what this implements, so a hypothetical i > 0 would still agree.)

This exists to make one thing unmissable: whoever holds the seed holds this key, and can move the session account's funds without touching the node. Guard a seed exactly as you would guard the key it produces.

Details

  • seed: 32-byte session seed.
  • Returns: The derived secp256k1 private key.

Gotchas

  • Throws If the seed is not 32 bytes of hex.

Example (Signing with a session key)

ts
import { sessionPrivateKey } from "@somnia-chain/markets-sdk/native";
import { privateKeyToAccount } from "viem/accounts";

// Sign locally instead of letting the node sign for you.
const account = privateKeyToAccount(sessionPrivateKey(seed));

Parameters

seed

`0x${string}`

Returns

`0x${string}`