Sign Transaction

The signTransaction() method is similar to checkout, but provides a different UI to the user. This request does not connect to the Nimiq blockchain at any point, so it can be used to sign transactions offline.

Table of Contents

  1. Differences to Checkout
  2. Request
  3. Options
  4. Result

Differences to Checkout

  • It requires the request to include the sender’s address as sender.
  • It requires the request to include the transaction’s validityStartHeight.
  • The created transaction will only be returned to the caller, not sent to the network.

Request

Result: -

Please choose an address first to use as the sender:
const options = {
  appName: 'Hub API Docs',
  sender: chosenAddress,
  recipient: 'NQ07 0000 0000 0000 0000 0000 0000 0000 0000',
  value: 3.14 * 1e5, // 3.14 NIM
  validityStartHeight: 500000,
  // See more options in the table below
};

// All client requests are async and return a promise
const signedTransaction = await hubApi.signTransaction(options);

Options

(On mobile, scroll right to see the whole table.)

Option Type Required? Description
appName string yes The name of your app, should be as short as possible.
sender string yes The human-readable address of the sender.
recipient string yes The human-readable address of the recipient (your shop/app).
value number yes Value of the transaction, in Luna.
validityStartHeight number yes The transaction’s validity start height. (A transaction is only valid for 120 blocks after its validityStartHeight. Transactions with a validityStartHeight higher than the next network block height are rejected and need to be sent again later during their validity window.)
fee number no Transaction fee in luna. Default: 0
extraData string or Uint8Array no Extra data that should be sent with the transaction.
flags number no A Nimiq.Transaction.Flag, only required if the transaction should create a contract.
recipientType number no The Nimiq.Account.Type of the recipient. Only required if the transaction should create a contract.

Result

The signTransaction() method returns a SignedTransaction.

interface SignedTransaction {
    serializedTx: string;                  // HEX signed and serialized transaction
    hash: string;                          // HEX transaction hash

    raw: {
        signerPublicKey: Uint8Array;       // Serialized public key of the signer
        signature: Uint8Array;             // Serialized signature of the signer

        sender: string;                    // Human-readable address of sender
        senderType: Nimiq.Account.Type;    // 0, 1, 2 - see recipientType above

        recipient: string;                 // Human-readable address of recipient
        recipientType: Nimiq.Account.Type; // 0, 1, 2 - see above

        value: number;
        fee: number;
        validityStartHeight: number;       // Automatically determined validity
                                           // start height of the transaction
        extraData: Uint8Array;
        flags: number;
        networkId: number;
    }
}

The serializedTx can be handed to a Nimiq JSON-RPC’s sendRawTransaction method.

The raw object can be handed to the NanoApi’s relayTransaction method.