diff --git a/programs/protocol-contracts-solana/src/lib.rs b/programs/protocol-contracts-solana/src/lib.rs index 78e5677..0c897b5 100644 --- a/programs/protocol-contracts-solana/src/lib.rs +++ b/programs/protocol-contracts-solana/src/lib.rs @@ -33,11 +33,12 @@ declare_id!("94U5AHQMKkV5txNJ17QPXWoh474PheGou6cNP2FEuL1d"); pub mod gateway { use super::*; - pub fn initialize(ctx: Context, tss_address:[u8; 20]) -> Result<()> { + pub fn initialize(ctx: Context, tss_address:[u8; 20], chain_id: u64) -> Result<()> { let initialized_pda = &mut ctx.accounts.pda; initialized_pda.nonce = 0; initialized_pda.tss_address = tss_address; initialized_pda.authority = ctx.accounts.signer.key(); + initialized_pda.chain_id = chain_id; Ok(()) } @@ -108,6 +109,7 @@ pub mod gateway { return err!(Errors::NonceMismatch); } let mut concatenated_buffer = Vec::new(); + concatenated_buffer.extend_from_slice(&pda.chain_id.to_be_bytes()); concatenated_buffer.extend_from_slice(&nonce.to_be_bytes()); concatenated_buffer.extend_from_slice(&amount.to_be_bytes()); concatenated_buffer.extend_from_slice(&ctx.accounts.to.key().to_bytes()); @@ -144,6 +146,7 @@ pub mod gateway { return err!(Errors::NonceMismatch); } let mut concatenated_buffer = Vec::new(); + concatenated_buffer.extend_from_slice(&pda.chain_id.to_be_bytes()); concatenated_buffer.extend_from_slice(&nonce.to_be_bytes()); concatenated_buffer.extend_from_slice(&amount.to_be_bytes()); concatenated_buffer.extend_from_slice(&ctx.accounts.to.key().to_bytes()); @@ -272,6 +275,7 @@ pub struct Pda { nonce: u64, // ensure that each signature can only be used once tss_address: [u8; 20], // 20 bytes address format of ethereum authority: Pubkey, + chain_id: u64, } diff --git a/tests/protocol-contracts-solana.ts b/tests/protocol-contracts-solana.ts index e28c469..9e3dfde 100644 --- a/tests/protocol-contracts-solana.ts +++ b/tests/protocol-contracts-solana.ts @@ -49,12 +49,15 @@ describe("some tests", () => { const tssAddress = Array.from(address); console.log("tss address", tssAddress); + const chain_id = 111111; + const chain_id_bn = new anchor.BN(chain_id); + it("Initializes the program", async () => { - await gatewayProgram.methods.initialize(tssAddress).rpc(); + await gatewayProgram.methods.initialize(tssAddress, chain_id_bn).rpc(); // repeated initialization should fail try { - await gatewayProgram.methods.initialize(tssAddress).rpc(); + await gatewayProgram.methods.initialize(tssAddress,chain_id_bn).rpc(); throw new Error("Expected error not thrown"); // This line will make the test fail if no error is thrown } catch (err) { expect(err).to.be.not.null; @@ -181,6 +184,7 @@ describe("some tests", () => { const amount = new anchor.BN(500_000); const nonce = pdaAccountData.nonce; const buffer = Buffer.concat([ + chain_id_bn.toArrayLike(Buffer, 'be', 8), nonce.toArrayLike(Buffer, 'be', 8), amount.toArrayLike(Buffer, 'be', 8), wallet_ata.toBuffer(), @@ -247,6 +251,7 @@ describe("some tests", () => { const amount = new anchor.BN(500000000); const to = wallet.publicKey; const buffer = Buffer.concat([ + chain_id_bn.toArrayLike(Buffer, 'be', 8), nonce.toArrayLike(Buffer, 'be', 8), amount.toArrayLike(Buffer, 'be', 8), to.toBuffer(), @@ -294,6 +299,8 @@ describe("some tests", () => { expect(err.message).to.include("SignerIsNotAuthority"); } }); + + });