-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authority timelock #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "program-authority-timelock" | ||
version = "1.0.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "program_authority_timelock" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = "0.26.0" | ||
|
||
[dev-dependencies] | ||
solana-program-test = "=1.14.7" | ||
solana-sdk = "=1.14.7" | ||
tokio = "1.14.1" | ||
bincode = "1.3.3" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#![deny(warnings)] | ||
#![allow(clippy::result_large_err)] | ||
|
||
use anchor_lang::{ | ||
prelude::*, | ||
solana_program::{ | ||
bpf_loader_upgradeable, | ||
program::{ | ||
invoke, | ||
invoke_signed, | ||
}, | ||
}, | ||
}; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
declare_id!("escMHe7kSqPcDHx4HU44rAHhgdTLBZkUrU39aN8kMcL"); | ||
const ONE_YEAR: i64 = 365 * 24 * 60 * 60; | ||
|
||
#[program] | ||
pub mod program_authority_timelock { | ||
use super::*; | ||
|
||
pub fn commit(ctx: Context<Commit>, timestamp: i64) -> Result<()> { | ||
let current_authority = &ctx.accounts.current_authority; | ||
let escrow_authority = &ctx.accounts.escrow_authority; | ||
let program_account = &ctx.accounts.program_account; | ||
|
||
invoke( | ||
&bpf_loader_upgradeable::set_upgrade_authority( | ||
&program_account.key(), | ||
¤t_authority.key(), | ||
Some(&escrow_authority.key()), | ||
), | ||
&ctx.accounts.to_account_infos(), | ||
)?; | ||
|
||
// Check that the timelock is no longer than 1 year | ||
if Clock::get()?.unix_timestamp.saturating_add(ONE_YEAR) < timestamp { | ||
return Err(ErrorCode::TimestampTooLate.into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn transfer(ctx: Context<Transfer>, timestamp: i64) -> Result<()> { | ||
let new_authority = &ctx.accounts.new_authority; | ||
let escrow_authority = &ctx.accounts.escrow_authority; | ||
let program_account = &ctx.accounts.program_account; | ||
|
||
invoke_signed( | ||
&bpf_loader_upgradeable::set_upgrade_authority( | ||
&program_account.key(), | ||
&escrow_authority.key(), | ||
Some(&new_authority.key()), | ||
), | ||
&ctx.accounts.to_account_infos(), | ||
&[&[ | ||
new_authority.key().as_ref(), | ||
timestamp.to_be_bytes().as_ref(), | ||
&[*ctx.bumps.get("escrow_authority").unwrap()], | ||
]], | ||
)?; | ||
|
||
if Clock::get()?.unix_timestamp < timestamp { | ||
return Err(ErrorCode::TimestampTooEarly.into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
#[instruction(timestamp : i64)] | ||
pub struct Commit<'info> { | ||
pub current_authority: Signer<'info>, | ||
/// CHECK: Unchecked new authority, can be a native wallet or a PDA of another program | ||
pub new_authority: AccountInfo<'info>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have to be super careful about setting this key correctly right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (i'm not sure if there's anything better we can do here, since it's not like we can sign as the governance authority anyway) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I have a strategy |
||
#[account(seeds = [new_authority.key().as_ref(), timestamp.to_be_bytes().as_ref()], bump)] | ||
pub escrow_authority: SystemAccount<'info>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let me make sure i follow this correctly: escrow_authority holds one or more authorities on behalf of new_authority.key(), but all of those authorities can only be claimed after i.e., this logic works even if multiple programs are transferred to the same new_authority at the same timestamp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes exactly, you send any number of authorities to the PDA seeded by |
||
#[account(executable, constraint = matches!(program_account.as_ref(), UpgradeableLoaderState::Program{..}))] | ||
pub program_account: Account<'info, UpgradeableLoaderState>, | ||
#[account(mut, seeds = [program_account.key().as_ref()], bump, seeds::program = bpf_upgradable_loader.key())] | ||
pub program_data: Account<'info, ProgramData>, | ||
pub bpf_upgradable_loader: Program<'info, BpfUpgradableLoader>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
#[instruction(timestamp : i64)] | ||
pub struct Transfer<'info> { | ||
/// CHECK: Unchecked new authority, can be a native wallet or a PDA of another program | ||
pub new_authority: AccountInfo<'info>, | ||
#[account(seeds = [new_authority.key().as_ref(), timestamp.to_be_bytes().as_ref()], bump)] | ||
pub escrow_authority: SystemAccount<'info>, | ||
#[account(executable, constraint = matches!(program_account.as_ref(), UpgradeableLoaderState::Program{..}))] | ||
pub program_account: Account<'info, UpgradeableLoaderState>, | ||
#[account(mut, seeds = [program_account.key().as_ref()], bump, seeds::program = bpf_upgradable_loader.key())] | ||
pub program_data: Account<'info, ProgramData>, | ||
pub bpf_upgradable_loader: Program<'info, BpfUpgradableLoader>, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct BpfUpgradableLoader {} | ||
|
||
impl Id for BpfUpgradableLoader { | ||
fn id() -> Pubkey { | ||
bpf_loader_upgradeable::id() | ||
} | ||
} | ||
|
||
#[error_code] | ||
#[derive(PartialEq, Eq)] | ||
pub enum ErrorCode { | ||
#[msg("Timestamp too early")] | ||
TimestampTooEarly, | ||
#[msg("Timestamp too late")] | ||
TimestampTooLate, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod simulator; | ||
mod test; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
smart