Send Transaction

This section guides you through the process of obtaining an access token, using it to get a signed transaction from the Vault, and broadcasting that transaction to the blockchain with viem.

This page requires Entity and Policy stores configured. If you don't have them, see theQuick start section first.

import { Action, getAddress, resourceId, toHex } from '@narval/armory-sdk'
import { SignTransaction } from '@narval/policy-engine-shared'
import { v4 as uuid } from 'uuid'
import { createPublicClient, http } from 'viem'
import { sepolia } from 'viem/chains'

const run = async () => {
  // ...
  
  const signTransaction: SignTransaction = {
    action: Action.SIGN_TRANSACTION,
    nonce: uuid(),
    resourceId: resourceId(accounts[0].address),
    transactionRequest: {
      from: getAddress(accounts[0].address),
      to: '0x7099797...0d17dc79c8',
      chainId: 137,
      value: toHex(1000000000000000000n),
      nonce: 192,
      type: '0',
      gas: 21000n,
      gasPrice: 1000000000n
    }
  }

  const signTransactionAccessToken = await authClient.requestAccessToken(signTransaction)

  const { signature } = await vaultClient.sign({
    data: signTransaction, // Authorized request
    accessToken: signTransactionAccessToken // Token authorizing the request
  })

  console.log('Signature:', signature)

  const client = createPublicClient({
    chain: sepolia,
    transport: http()
  })

  // IMPORTANT: This call will fail because the `wallet` does not have sufficient
  // funds.
  const hash = await client.sendRawTransaction({
    serializedTransaction: signature
  })

  console.log('Tx hash:', hash)
}

run()

Last updated