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

Create the Program

Pre-alpha. The devnet pre-alpha is a mock network with no MPC. Read the warning in the introduction first.

This chapter sets up the program: its dependencies, its three account types, its errors, and the function that reads a dWallet’s owner.

Cargo.toml

[package]
name = "my-voting-program"
version = "0.1.0"
edition = "2024"

[dependencies]
ika-dwallet-pinocchio = { git = "https://github.com/dwallet-labs/ika-pre-alpha" }
pinocchio = { version = "0.11", features = ["cpi"] }
pinocchio-system = "0.6"
solana-address = { version = "2.6", features = ["curve25519"] }

[dev-dependencies]
mollusk-svm = "0.15"
solana-account = "4.3"
solana-instruction = "3.4"
solana-program-error = "3"
solana-pubkey = "4.1"

[features]
devnet-pre-alpha = ["ika-dwallet-pinocchio/devnet-pre-alpha"]

[lib]
crate-type = ["cdylib", "lib"]
  • ika-dwallet-pinocchio gives DWalletContext, which makes the CPI into the ika program, and ID, the ika program’s address on the network you build for.
  • The features pick that network. Build for the devnet pre-alpha with cargo build-sbf --tools-version v1.54 --arch v3 -- --features devnet-pre-alpha. The tests build with no feature. See Build for one network.
  • pinocchio-system creates the program’s own accounts.
  • solana-address with curve25519 derives PDAs when the program runs in host tests. On chain, the program uses the runtime’s own call for it.

The skeleton

#![allow(unused)]
#![no_std]

fn main() {
extern crate alloc;

use pinocchio::{
    AccountView, Address, ProgramResult,
    cpi::{Seed, Signer},
    entrypoint,
    error::ProgramError,
};
use pinocchio_system::instructions::CreateAccount;

use ika_dwallet_pinocchio::{DWalletContext, ID as IKA_PROGRAM_ID};

entrypoint!(process_instruction);
pinocchio::nostd_panic_handler!();

// Replace with your program's id before you deploy.
pub const ID: Address = Address::new_from_array([5u8; 32]);
}

Errors

Each check gets its own error code, so a test can assert the exact one.

#![allow(unused)]
fn main() {
#[repr(u32)]
pub enum VotingError {
    DWalletMismatch = 0,       // the dWallet passed is not the proposal's
    NotAMember = 1,            // the signer is not on the council
    NotDWalletOwner = 2,       // the council's creator is not the dWallet's owner
    InvalidCouncil = 3,        // the council account is not the dWallet's council
    InvalidDWalletAccount = 4, // the account is not a DWallet of the ika program
    InvalidQuorum = 5,         // the quorum is 0 or above the member count
    DuplicateMember = 6,
    ProposalClosed = 7,
    ProposalMismatch = 8,      // the proposal id does not match the account
    InvalidVoucherBump = 9,    // the voucher bump is not the canonical one
    VoucherMismatch = 10,      // the voucher account is not the proposal's voucher PDA
    InvalidBump = 11,          // a bump the client chose is not the canonical one
}

impl From<VotingError> for ProgramError {
    fn from(error: VotingError) -> Self {
        ProgramError::Custom(error as u32)
    }
}
}

The accounts

Every account starts with a two-byte header, a tag and a version, like the ika program’s own.

Council, at ["council", dwallet], 361 bytes, tag 3:

OffsetFieldSizeMeaning
2dwallet32The dWallet this council governs
34quorum4Yes votes needed
38member_count21 to 10
40bump1The council’s canonical bump
41members320Ten 32-byte keys. The first member_count are used

Proposal, at ["proposal", proposal_id], 196 bytes, tag 1:

OffsetFieldSizeMeaning
2proposal_id32The proposal’s id
34dwallet32The council’s dWallet
66message_digest32The Keccak-256 of the message to sign
98user_pubkey32The request key that will send the sign request. It becomes the voucher’s signing key
130signature_scheme2The scheme, such as 5 for EdDSA
132creator32The member who proposed
164yes_votes4
168no_votes4
172quorum4Copied from the council
176status10 open, 1 approved
177signature_voucher_bump1The bump of ["signature_voucher", proposal_id]
178bump1The proposal’s bump
179_reserved16

VoteRecord, at ["vote", proposal_id, voter], 69 bytes, tag 2:

OffsetFieldSize
2voter32
34proposal_id32
66vote1
67bump1

Dispatch

The first byte of the instruction data names the instruction:

#![allow(unused)]
fn main() {
pub fn process_instruction(
    program_id: &Address,
    accounts: &mut [AccountView],
    data: &[u8],
) -> ProgramResult {
    let (discriminator, rest) = data
        .split_first()
        .ok_or(ProgramError::InvalidInstructionData)?;

    match *discriminator {
        0 => create_proposal(program_id, accounts, rest),
        1 => cast_vote(program_id, accounts, rest),
        2 => create_council(program_id, accounts, rest),
        _ => Err(ProgramError::InvalidInstructionData),
    }
}
}

Reading a dWallet’s owner

create_council must prove that its signer owns the dWallet now. It reads the DWallet account of the ika program: tag 97 at byte 0, then owner at offset 2. First it proves the account is really the ika program’s.

#![allow(unused)]
fn main() {
const DWALLET_ACCOUNT_DISCRIMINATOR: u8 = 97;
const DWALLET_OWNER_OFFSET: usize = 2;

fn dwallet_owner(
    dwallet: &AccountView,
    dwallet_program: &AccountView,
) -> Result<[u8; 32], ProgramError> {
    // The dWallet program must be ika's own, the address this program was
    // built for: a dWallet account of any other program is anyone's word.
    if dwallet_program.address() != &IKA_PROGRAM_ID
        || !dwallet_program.executable()
        || !dwallet.owned_by(&IKA_PROGRAM_ID)
    {
        return Err(VotingError::InvalidDWalletAccount.into());
    }
    let data = dwallet.try_borrow()?;
    if data.first() != Some(&DWALLET_ACCOUNT_DISCRIMINATOR) {
        return Err(VotingError::InvalidDWalletAccount.into());
    }
    read_key(&data, DWALLET_OWNER_OFFSET).map_err(|_| VotingError::InvalidDWalletAccount.into())
}
}

The owner is compared with IKA_PROGRAM_ID, never with the program account the caller passed. A caller could otherwise pass a program of its own, with a look-alike account that names the caller as owner. Your own program can also check the account’s length, 185 bytes.

Rent

The example pays rent with a fixed formula:

#![allow(unused)]
fn main() {
fn minimum_balance(data_len: usize) -> u64 {
    (data_len as u64).saturating_add(128).saturating_mul(6960)
}
}

It equals Solana’s rent-exempt minimum at the current rent rate. Reading the Rent sysvar is the choice that stays right if the rate changes.

Next

Create the council and the proposal.