WASM signer functions

Every function exported by the lighter-go WASM signer, with its arguments in order.

Source: wasm/main.go in elliottech/lighter-go. Build instructions: Quickstart: TypeScript + WASM.

Conventions

  • All functions are synchronous globals on globalThis.
  • Arguments are positional. Every argument is required; pass 0 for unused ones. undefined returns an error.
  • Return value is an object. Check error first:
type SignResult = {
  error?: string;
  txType: number;         // pass as tx_type to sendTx
  txInfo: string;         // JSON string; pass as tx_info to sendTx
  txHash: string;
  messageToSign?: string; // always present for ChangePubKey, Transfer, ApproveIntegrator (see below)
};
  • messageToSign is returned for every ChangePubKey, Transfer and ApproveIntegrator, but the server only requires the L1 signature in some cases (table below). When it's required, sign it with the owner's Ethereum wallet (personal_sign), then txInfo = JSON.stringify({ ...JSON.parse(txInfo), L1Sig: signature }). When it isn't, send txInfo as is and don't prompt the wallet.
  • The last two arguments of every Sign* function are always apiKeyIndex, accountIndex, and they select a client previously created with CreateClient.
  • skipNonce: 0 = strict nonce = previous + 1; 1 = allow any higher nonce. See Core concepts.

When an L1 signature is required

"Same owner" means both accounts belong to the same master account, i.e. the same master account and its sub-accounts.

TransactionL1 signature requiredNot required
ChangePubKeyAlways–
TransferSender and receiver have different owners. This includes fast withdrawals, which are a transfer to the LP account.Transfers between the same owner's accounts (e.g. master to sub-account)
ApproveIntegratorIntegrator has a different owner and at least one max fee is > 0Zero-fee approvals and revokes; approving an account with the same owner
Everything else (orders, Withdraw, …)Never; messageToSign isn't returned–
function needsL1Sig(kind: "changePubKey" | "transfer" | "approveIntegrator",
                    sameOwner: boolean, anyFeeAboveZero = false) {
  if (kind === "changePubKey") return true;
  if (kind === "transfer") return !sameOwner;
  return !sameOwner && anyFeeAboveZero;
}

Client & keys

FunctionArgumentsReturns
GenerateAPIKey–{ privateKey, publicKey }
CreateClienturl, privateKey, chainId, apiKeyIndex, accountIndex: pass "" as url{} or { error }
CheckClientapiKeyIndex, accountIndex: don't use from WASM, see below{} if key registered on-chain
CreateAuthTokendeadline (unix s, 0 = ~7h), apiKeyIndex, accountIndex{ authToken }
SignChangePubKeypubKeyHex, skipNonce, nonce, apiKeyIndex, accountIndextx + messageToSign

You can create several clients (one per apiKeyIndex/accountIndex pair) in the same WASM instance.

🚧

Do all HTTP from JavaScript

The WASM build uses Go's HTTP client, which can't open network connections from WebAssembly (Node or browser). CheckClient, and any Sign* call with nonce = -1 (which asks the signer to fetch the nonce itself), fail with dial tcp: lookup …. Always pass an explicit nonce from GET /api/v1/nextNonce. To verify a key, either:

  • compare the public_key from GET /api/v1/apikeys?account_index=…&api_key_index=… with your public key (the API omits the 0x), or
  • sign an auth token with CreateAuthToken and call GET /api/v1/accountLimits?account_index=… with it in the Authorization header: code: 200 means the key is registered.

See the TypeScript quickstart for a full example.

Orders

FunctionArguments
SignCreateOrdermarketIndex, clientOrderIndex, baseAmount, price, isAsk, orderType, timeInForce, reduceOnly, triggerPrice, orderExpiry, integratorAccountIndex, integratorTakerFee, integratorMakerFee, selfTradeBehaviorMode, selfTradeEqualityMode, skipNonce, nonce, apiKeyIndex, accountIndex
SignModifyOrdermarketIndex, index, baseAmount, price, triggerPrice, integratorAccountIndex, integratorTakerFee, integratorMakerFee, selfTradeBehaviorMode, selfTradeEqualityMode, skipNonce, nonce, orderVersion, apiKeyIndex, accountIndex
SignCancelOrdermarketIndex, orderIndex, skipNonce, nonce, apiKeyIndex, accountIndex
SignCancelAllOrderstimeInForce, time, cancelAllMarketIndex, skipNonce, nonce, apiKeyIndex, accountIndex
SignCreateGroupedOrdersgroupingType, orders[], integratorAccountIndex, integratorTakerFee, integratorMakerFee, selfTradeBehaviorMode, selfTradeEqualityMode, skipNonce, nonce, apiKeyIndex, accountIndex

Enum values (order type, time in force, self-trade modes) are listed in Data Structures, Constants and Errors and Signing Transactions.

SignCreateGroupedOrders

orders is an array of objects:

{ MarketIndex, ClientOrderIndex, BaseAmount, Price, IsAsk, Type,
  TimeInForce, ReduceOnly, TriggerPrice, OrderExpiry }

groupingType: 1 one-triggers-the-other (OTO) · 2 one-cancels-the-other (OCO) · 3 OTOCO.

SignCancelAllOrders

  • timeInForce: 0 immediate · 1 scheduled (at time, ms) · 2 abort a scheduled cancel
  • cancelAllMarketIndex: a market ID, or 255 for all markets. Per-market cancel must be immediate.

Leverage & margin

FunctionArguments
SignUpdateLeveragemarketIndex, fraction, marginMode, skipNonce, nonce, apiKeyIndex, accountIndex – fraction = 10000 / leverage; marginMode 0 cross, 1 isolated
SignUpdateMarginmarketIndex, usdcAmount, direction, skipNonce, nonce, apiKeyIndex, accountIndex – direction 1 add, 0 remove

Funds

FunctionArguments
SignWithdrawassetIndex, routeType, amount, skipNonce, nonce, apiKeyIndex, accountIndex
SignTransfertoAccountIndex, assetIndex, fromRouteType, toRouteType, amount, usdcFee, memo, skipNonce, nonce, apiKeyIndex, accountIndex

routeType: 0 perps, 1 spot. memo: 32-byte hex string.

Accounts, pools & staking

FunctionArguments
SignCreateSubAccountskipNonce, nonce, apiKeyIndex, accountIndex
SignCreatePublicPooloperatorFee, initialTotalShares, minOperatorShareRate, skipNonce, nonce, apiKeyIndex, accountIndex
SignUpdatePublicPoolpublicPoolIndex, status, operatorFee, minOperatorShareRate, skipNonce, nonce, apiKeyIndex, accountIndex
SignMintSharespublicPoolIndex, shareAmount, skipNonce, nonce, apiKeyIndex, accountIndex
SignBurnSharespublicPoolIndex, shareAmount, skipNonce, nonce, apiKeyIndex, accountIndex
SignStakeAssetsstakingPoolIndex, shareAmount, skipNonce, nonce, apiKeyIndex, accountIndex
SignUnstakeAssetsstakingPoolIndex, shareAmount, skipNonce, nonce, apiKeyIndex, accountIndex
SignUpdateAccountConfigaccountTradingMode, skipNonce, nonce, apiKeyIndex, accountIndex
SignUpdateAccountAssetConfigassetIndex, assetMarginMode, skipNonce, nonce, apiKeyIndex, accountIndex

Integrators

FunctionArguments
SignApproveIntegratorintegratorIndex, maxPerpsTakerFee, maxPerpsMakerFee, maxSpotTakerFee, maxSpotMakerFee, approvalExpiry, skipNonce, nonce, apiKeyIndex, accountIndex

See Partner Attribution.

Other languages

The same signer ships as a C shared library (.so / .dylib / .dll) for Python, Rust, Java and C++, plus a React Native build. See the lighter-go releases.


Did this page help you?