Skip to main content

signTransaction

Callable

  • signTransaction(transaction: TypedTransaction, privateKey: string): Promise<SignTransactionResult>

  • Signs an Ethereum transaction with a given private key.


    Parameters

    • transaction: TypedTransaction

      The transaction, must be a legacy, EIP2930 or EIP 1559 transaction type

    • privateKey: string

      The private key to import. This is 32 bytes of random data.

    Returns Promise<SignTransactionResult>

    A signTransactionResult object that contains message hash, r, s, v, transaction hash and raw transaction.

    This function is not stateful here. We need network access to get the account nonce and chainId to sign the transaction. This function will rely on user to provide the full transaction to be signed. If you want to sign a partial transaction object Use Web3.eth.accounts.sign instead.

    Signing a legacy transaction

    import {signTransaction, Transaction} from 'web3-eth-accounts';

    signTransaction(new Transaction({
    to: '0x118C2E5F57FD62C2B5b46a5ae9216F4FF4011a07',
    value: '0x186A0',
    gasLimit: '0x520812',
    gasPrice: '0x09184e72a000',
    data: '',
    chainId: 1,
    nonce: 0 }),
    '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318')

    > {
    messageHash: '0x28b7b75f7ba48d588a902c1ff4d5d13cc0ca9ac0aaa39562368146923fb853bf',
    v: '0x25',
    r: '0x601b0017b0e20dd0eeda4b895fbc1a9e8968990953482214f880bae593e71b5',
    s: '0x690d984493560552e3ebdcc19a65b9c301ea9ddc82d3ab8cfde60485fd5722ce',
    rawTransaction: '0xf869808609184e72a0008352081294118c2e5f57fd62c2b5b46a5ae9216f4ff4011a07830186a08025a00601b0017b0e20dd0eeda4b895fbc1a9e8968990953482214f880bae593e71b5a0690d984493560552e3ebdcc19a65b9c301ea9ddc82d3ab8cfde60485fd5722ce',
    transactionHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'

    Signing an eip 1559 transaction

    import {signTransaction, Transaction} from 'web3-eth-accounts';

    signTransaction(new Transaction({
    to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    maxPriorityFeePerGas: '0x3B9ACA00',
    maxFeePerGas: '0xB2D05E00',
    gasLimit: '0x6A4012',
    value: '0x186A0',
    data: '',
    chainId: 1,
    nonce: 0}),
    "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")
    > {
    messageHash: '0x5744f24d5f0aff6c70487c8e85adf07d8564e50b08558788f00479611d7bae5f',
    v: '0x25',
    r: '0x78a5a6b2876c3985f90f82073d18d57ac299b608cc76a4ba697b8bb085048347',
    s: '0x9cfcb40cc7d505ed17ff2d3337b51b066648f10c6b7e746117de69b2eb6358d',
    rawTransaction: '0xf8638080836a401294f0109fc8df283027b6285cc889f5aa624eac1f55830186a08025a078a5a6b2876c3985f90f82073d18d57ac299b608cc76a4ba697b8bb085048347a009cfcb40cc7d505ed17ff2d3337b51b066648f10c6b7e746117de69b2eb6358d',
    transactionHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
    }

    Signing an eip 2930 transaction

    import {signTransaction, Transaction} from 'web3-eth-accounts';

    signTransaction(new Transaction ({
    chainId: 1,
    nonce: 0,
    gasPrice: '0x09184e72a000',
    gasLimit: '0x2710321',
    to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
    value: '0x186A0',
    data: '',
    accessList: [
    {
    address: '0x0000000000000000000000000000000000000101',
    storageKeys: [
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    '0x00000000000000000000000000000000000000000000000000000000000060a7',
    ],
    },
    ],
    }),"0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318")

    > {
    messageHash: '0xc55ea24bdb4c379550a7c9a6818ac39ca33e75bc78ddb862bd82c31cc1c7a073',
    v: '0x26',
    r: '0x27344e77871c8b2068bc998bf28e0b5f9920867a69c455b2ed0c1c150fec098e',
    s: '0x519f0130a1d662841d4a28082e9c9bb0a15e0e59bb46cfc39a52f0e285dec6b9',
    rawTransaction: '0xf86a808609184e72a000840271032194f0109fc8df283027b6285cc889f5aa624eac1f55830186a08026a027344e77871c8b2068bc998bf28e0b5f9920867a69c455b2ed0c1c150fec098ea0519f0130a1d662841d4a28082e9c9bb0a15e0e59bb46cfc39a52f0e285dec6b9',
    transactionHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
    }