Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

setAuthority transfers control

Source: record/tests/record.rs L174

  • Given: a record initialized with alice as authority
  • When: setup: payer initializes the record
  • When: alice hands off authority to bob
  • Then: authority = 6DAhWE3pSJQrSFRNjkGQznpUi7wWnqWf3FnRyZQqxbvu ✓
  • When: bob writes to the record he now controls
  • When: alice tries to write after handing off
  • Then: alice no longer controls it ✓

Result: ✓ success — 943 CU · claims 2/2 ✓

sequenceDiagram
    participant p0 as alice
    participant p1 as Record
    p0->>p1: setAuthority
    activate p1
    p1-->>p0: ✓ 943cu
    deactivate p1

Authority

flowchart LR
    Record["Record"]:::program
    111F[("111F…")]:::state
    alice(["alice"]):::signer
    Record -->|writes| 111F
    alice -->|signs| Record
    classDef program fill:#dae8fc,stroke:#6c8ebf;
    classDef signer fill:#d5e8d4,stroke:#82b366;
    classDef state fill:#ffe6cc,stroke:#d79b00;

Ownership

flowchart LR
    Record["Record"]:::program
    111F[("111F…")]:::state
    system["system"]:::program
    alice[("alice")]:::state
    Record -->|owns| 111F
    system -->|owns| alice
    classDef program fill:#dae8fc,stroke:#6c8ebf;
    classDef signer fill:#d5e8d4,stroke:#82b366;
    classDef state fill:#ffe6cc,stroke:#d79b00;

Accounts

AccountSignerWritableOwner
111F…·Record
alicesystem
bob··system
Call tree and logs
setAuthority transfers control
alice (943cu)
└─ Record::setAuthority ✓ 943cu
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 invoke [1]
Program log: RecordInstruction::SetAuthority
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 consumed 943 of 200000 compute units
Program recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5 success

Integration

What an integrator writes: the derivations and the instruction, byte-identical to the transaction this page witnessed. Addresses are this scenario’s; supply your own.

#![allow(unused)]
fn main() {
use solana_instruction::{AccountMeta, Instruction};
use solana_pubkey::{pubkey, Pubkey};

// --- setAuthority ---
let program_id: Pubkey = pubkey!("recr1L3PCGKLbckBqMNcJhuuyU1zgo8nBhfLVsJNwr5");
let record_account: Pubkey = pubkey!("111FJo4zLAGU9nzTWa6EnbV4VAmtG4FR8kcokrtZYr"); // supply your own
let authority: Pubkey = pubkey!("2rj1EEgg32bupe12mMYnPFaKQCPPyYBhKYmDFZ8cXxpT"); // alice: this scenario's value; supply your own
let new_authority: Pubkey = pubkey!("6DAhWE3pSJQrSFRNjkGQznpUi7wWnqWf3FnRyZQqxbvu"); // bob: this scenario's value; supply your own
let ix = Instruction {
    program_id,
    accounts: vec![
        AccountMeta::new(record_account, false), // recordAccount
        AccountMeta::new_readonly(authority, true), // authority
        AccountMeta::new_readonly(new_authority, false), // newAuthority
    ],
    // discriminator ++ borsh()
    data: vec![0x02],
};
}

The same call through the Codama-generated TypeScript client (the async builder derives every PDA the resolver derived here, so only the free inputs appear):

import { address } from "@solana/kit";
import { getSetAuthorityInstructionAsync } from "your-generated-client";

const ix = await getSetAuthorityInstructionAsync({
  recordAccount: address("111FJo4zLAGU9nzTWa6EnbV4VAmtG4FR8kcokrtZYr"),
  authority: authoritySigner, // alice (a TransactionSigner)
  newAuthority: address("6DAhWE3pSJQrSFRNjkGQznpUi7wWnqWf3FnRyZQqxbvu"), // bob
});
How this page verifies itself: the test that ran it

The narrative above is this test; the page and the code cannot drift.

#![allow(unused)]
fn main() {
#[test]
fn set_authority_transfers_control() {
    let mut story = Story::load(SO, IDL);

    story.given("a record initialized with alice as authority");
    let payer = story.cast("payer");
    let alice = story.cast("alice");
    let bob = story.cast("bob");
    let record = story.stage_record(WRITABLE_START + 16);
    story
        .when(
            "setup: payer initializes the record",
            initialize()
                .record_account(record)
                .authority(alice.pubkey()),
            &[&payer],
        )
        .expect_success();

    let authority = story.track("authority", move |s| s.read_record(&record).1);
    let handoff = story
        .when(
            "alice hands off authority to bob",
            set_authority()
                .record_account(record)
                .authority(&alice)
                .new_authority(bob.pubkey()),
            &[&alice],
        )
        .expect_success();
    story.then_eq(&authority, bob.pubkey());

    story
        .when(
            "bob writes to the record he now controls",
            write()
                .record_account(record)
                .authority(&bob)
                .offset(0)
                .data(b"bob".to_vec()),
            &[&bob],
        )
        .expect_success();

    let out = story.when(
        "alice tries to write after handing off",
        write()
            .record_account(record)
            .authority(&alice)
            .offset(0)
            .data(b"back".to_vec()),
        &[&alice],
    );
    story.then_holds("alice no longer controls it", !out.success);
    emit_scenario(&story, &handoff, "setAuthority transfers control");
}
}