Skip to content

Commit ef70885

Browse files
committed
solana: update inbox item PDA derivation
the chain id and message are now hashed together
1 parent 0fc8194 commit ef70885

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

solana/programs/example-native-token-transfers/src/instructions/redeem.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ pub struct Redeem<'info> {
5656
space = 8 + InboxItem::INIT_SPACE,
5757
seeds = [
5858
InboxItem::SEED_PREFIX,
59-
endpoint_message.from_chain.id.to_be_bytes().as_ref(),
60-
endpoint_message.message.manager_payload.keccak256().as_ref(),
59+
endpoint_message.message.manager_payload.keccak256(
60+
endpoint_message.from_chain
61+
).as_ref(),
6162
],
6263
bump,
6364
)]

solana/programs/example-native-token-transfers/src/messages.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ pub struct ManagerMessage<A: Space> {
1717
}
1818

1919
impl<A: Space + AnchorSerialize + fmt::Debug + TypePrefixedPayload> ManagerMessage<A> {
20-
pub fn keccak256(&self) -> Hash {
21-
let payload = TypePrefixedPayload::to_vec_payload(self);
22-
solana_program::keccak::hash(&payload)
20+
pub fn keccak256(&self, chain_id: ChainId) -> Hash {
21+
let mut bytes: Vec<u8> = Vec::new();
22+
bytes.extend_from_slice(&chain_id.id.to_be_bytes());
23+
bytes.extend_from_slice(&TypePrefixedPayload::to_vec_payload(self));
24+
solana_program::keccak::hash(&bytes)
2325
}
2426
}
2527

solana/programs/example-native-token-transfers/tests/sdk/accounts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ impl NTT {
101101
manager_message: ManagerMessage<NativeTokenTransfer>,
102102
) -> Pubkey {
103103
let mut hasher = Keccak256::new();
104+
hasher.update(&chain.to_be_bytes());
104105
hasher.update(&TypePrefixedPayload::to_vec_payload(&manager_message));
105106

106107
let (inbox_item, _) = Pubkey::find_program_address(
107108
&[
108109
InboxItem::SEED_PREFIX,
109-
&chain.to_be_bytes(),
110110
&hasher.finalize(),
111111
],
112112
&self.program,

solana/ts/sdk/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ export class NTT {
9494
const chainId = coalesceChainId(chain)
9595
const serialized = ManagerMessage.serialize(managerMessage, NativeTokenTransfer.serialize)
9696
const hasher = new Keccak(256)
97+
hasher.update(new BN(chainId).toBuffer('be', 2))
9798
hasher.update(serialized)
9899
const hash = hasher.digest('hex')
99100
return this.derive_pda(
100101
[
101102
Buffer.from('inbox_item'),
102-
new BN(chainId).toBuffer('be', 2),
103103
Buffer.from(hash, 'hex')
104104
])
105105
}

0 commit comments

Comments
 (0)