Skip to content

Commit

Permalink
add chain id to the TSS ECDSA signature message_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Jul 2, 2024
1 parent 2733d07 commit 21cfcad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ declare_id!("94U5AHQMKkV5txNJ17QPXWoh474PheGou6cNP2FEuL1d");
pub mod gateway {
use super::*;

pub fn initialize(ctx: Context<Initialize>, tss_address:[u8; 20]) -> Result<()> {
pub fn initialize(ctx: Context<Initialize>, 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(())
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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,
}


Expand Down
11 changes: 9 additions & 2 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -294,6 +299,8 @@ describe("some tests", () => {
expect(err.message).to.include("SignerIsNotAuthority");
}
});


});


0 comments on commit 21cfcad

Please sign in to comment.