Skip to content

Commit fb0adba

Browse files
authored
port reset nonce from release branch (#104)
1 parent fa716ee commit fb0adba

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

programs/gateway/src/contexts.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ pub struct UpdateAuthority<'info> {
237237
pub pda: Account<'info, Pda>,
238238
}
239239

240+
/// Instruction context for resetting the PDA nonce.
241+
#[derive(Accounts)]
242+
pub struct ResetNonce<'info> {
243+
/// The account of the signer performing the update.
244+
#[account(mut)]
245+
pub signer: Signer<'info>,
246+
247+
/// Gateway PDA.
248+
#[account(mut, seeds = [b"meta"], bump)]
249+
pub pda: Account<'info, Pda>,
250+
}
251+
240252
/// Instruction context for pausing or unpausing deposits.
241253
#[derive(Accounts)]
242254
pub struct UpdatePaused<'info> {

programs/gateway/src/instructions/admin.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{
2-
contexts::{Initialize, Unwhitelist, UpdateAuthority, UpdatePaused, UpdateTss, Whitelist},
2+
contexts::{
3+
Initialize, ResetNonce, Unwhitelist, UpdateAuthority, UpdatePaused, UpdateTss, Whitelist,
4+
},
35
state::InstructionId,
46
utils::{
57
recover_and_verify_eth_address, validate_message_hash, verify_and_update_nonce,
@@ -162,3 +164,14 @@ pub fn unwhitelist_spl_mint(
162164

163165
Ok(())
164166
}
167+
168+
// Resets the PDA authority. Caller is authority stored in PDA.
169+
pub fn reset_nonce(ctx: Context<ResetNonce>, new_nonce: u64) -> Result<()> {
170+
verify_authority(&ctx.accounts.signer.key(), &ctx.accounts.pda)?;
171+
let pda = &mut ctx.accounts.pda;
172+
pda.nonce = new_nonce;
173+
174+
msg!("PDA nonce reset: new nonce = {}", new_nonce);
175+
176+
Ok(())
177+
}

programs/gateway/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ pub mod gateway {
223223
instructions::admin::update_authority(ctx, new_authority_address)
224224
}
225225

226+
/// Resets the PDA nonce. Caller is authority stored in PDA.
227+
///
228+
/// # Arguments
229+
/// * `ctx` - The instruction context.
230+
/// * `new_nonce` - The new nonce.
231+
pub fn reset_nonce(ctx: Context<ResetNonce>, new_nonce: u64) -> Result<()> {
232+
instructions::admin::reset_nonce(ctx, new_nonce)
233+
}
234+
226235
/// Whitelists a new SPL token. Caller is TSS.
227236
/// # Arguments
228237
/// * `ctx` - The instruction context.

tests/gateway.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,6 +3149,14 @@ describe("Gateway", () => {
31493149
}
31503150
});
31513151

3152+
it("Reset nonce", async () => {
3153+
await gatewayProgram.methods.resetNonce(new anchor.BN(1000)).rpc();
3154+
const pdaAccountDataAfter = await gatewayProgram.account.pda.fetch(
3155+
pdaAccount
3156+
);
3157+
expect(pdaAccountDataAfter.nonce.toNumber()).to.equal(1000);
3158+
});
3159+
31523160
const newAuthority = anchor.web3.Keypair.generate();
31533161
it("Update authority", async () => {
31543162
await gatewayProgram.methods.updateAuthority(newAuthority.publicKey).rpc();
@@ -3163,4 +3171,14 @@ describe("Gateway", () => {
31633171
expect(err.message).to.include("SignerIsNotAuthority");
31643172
}
31653173
});
3174+
3175+
it("Reset nonce fails if wrong authority", async () => {
3176+
try {
3177+
await gatewayProgram.methods.resetNonce(new anchor.BN(1000)).rpc();
3178+
throw new Error("Expected error not thrown");
3179+
} catch (err) {
3180+
expect(err).to.be.instanceof(anchor.AnchorError);
3181+
expect(err.message).to.include("SignerIsNotAuthority");
3182+
}
3183+
});
31663184
});

0 commit comments

Comments
 (0)