DOCS / TUTORIAL: PROGRAM KEYS

Three ways to use program-controlled keys.

A Solana program can authorize the same derived key in different ways: read-only rules, a one-time governance decision, or a payment allowance.

This tutorial uses the released @pairkey/sdk. The example controller is a starting point; your program must validate the actual request payload and application accounts for each rule.

Connect a program authority

npm install @pairkey/sdk
import * as web3 from '@solana/web3.js';
import {PairKeyClient} from '@pairkey/sdk';

const pairkey = new PairKeyClient({web3, storage: localStorage});
await pairkey.connect({
  source: 'program',
  wallet,
  program: controllerProgramId,
  mode: 'Onchain',
  policyScope: 'namespace',
  namespace: 'org/acme/treasury',
});
const operating = await pairkey.key({chain: 'solana', path: [0]});
const reserve = await pairkey.key({chain: 'solana', path: [1]});

The two paths yield separate keys under one namespace. The program’s authority PDA approves requests; it does not store a destination private key. See the program quickstart for policy setup and the runnable browser example.

1. Read-only treasury rules

Use Offchain mode when an immutable program can decide from current state without consuming anything. For example, read a treasury configuration account and accept only an allowed destination, path and exact transfer payload. Both signers evaluate pairkey_approve. The application then calls signTransaction or signAndSend for a supported destination.

// Reconnect in Offchain mode for immutable, read-only program rules.
await pairkey.connect({
  source: 'program', wallet, program: controllerProgramId,
  mode: 'Offchain', namespace: 'org/acme/treasury',
});
const signed = await pairkey.signTransaction({
  chain: 'solana', path: [0], transaction: treasuryTransaction,
});
// Submit signed.signedTransaction with your Solana connection.

The transaction must list the derived key as a required signer. Read-only evaluation is suitable only when the program's code is immutable and its decision does not need an atomic state change.

2. Governance execution

Use Onchain mode for a passed proposal that can execute once. Your controller checks the proposal ID, quorum, timelock and exact destination payload, marks the proposal used, and invokes the gateway approval CPI in the same Solana transaction. After finality, the SDK retrieves a signature for that approved request.

await pairkey.connect({
  source: 'program', wallet, program: controllerProgramId,
  mode: 'Onchain', namespace: 'org/acme/treasury',
});
const signed = await pairkey.signTransaction({
  chain: 'solana', path: [1], transaction: proposalTransaction,
});
saveJobId(signed.jobId);

The example controller only demonstrates an allowance. Add proposal-state checks to your own program; a generic “proposal passed” message does not enforce what gets signed. A later destination failure does not automatically undo a consumed proposal.

Source preview: EIP-712 and selectable EVM chain IDs are included in the SDK but require a compatible service release; this example does not run against the current public deployment. For an EVM governor that accepts EIP-712 votes, the same program authority can approve a vote signature on its selected chain. Bind the governor contract, proposal ID, support choice and domain to the proposal state before consuming it:

const vote = await pairkey.signTypedData({
  chainId: governorChainId,
  path: [2],
  typedData: {
    types: {
      EIP712Domain: [
        {name: 'name', type: 'string'},
        {name: 'chainId', type: 'uint256'},
        {name: 'verifyingContract', type: 'address'}
      ],
      Vote: [{name: 'proposalId', type: 'uint256'}, {name: 'support', type: 'bool'}]
    },
    primaryType: 'Vote',
    domain: {name: governorName, chainId: governorChainId, verifyingContract: governorAddress},
    message: {proposalId: String(proposalId), support: true}
  }
});
console.log(vote.evmSignatureHex);

This returns a signature for the governor's own verification flow; it does not cast a vote or execute a transaction. Confirm the governor's exact EIP-712 schema and nonce or replay rules before using the signature.

3. Capped payments

Use a separate payment namespace and path for each budget or payee group. The program checks recipient, amount, currency and remaining allowance in the typed transaction payload, decrements the allowance, then commits the exact approval. The client stores the job ID and recovers the original signed bytes after an uncertain result.

await pairkey.connect({
  source: 'program', wallet, program: controllerProgramId,
  mode: 'Onchain', namespace: 'org/acme/payments',
});
const payment = await pairkey.signAndSend({
  chain: 'solana', path: [0], transaction: paymentTransaction,
  connection,
});
if (payment.status === 'unknown') {
  await pairkey.recoverAndSend(payment.jobId, {
    chain: 'solana', connection, rebroadcast: true,
  });
}

Payment recipients and amounts must be parsed by the program from the canonical transaction, not inferred from a label supplied by the caller. The program integration guide explains the approval CPI and recovery model.