Skip to content

Commit

Permalink
fix: Don't add MT pubkey in derive_address_seed
Browse files Browse the repository at this point in the history
It's not a correct place for it to be applied. The distinction between
`derive_address_seed` and `derive_address` should be:

- `derive_address_seed` takes provided seeds (defined by the developer)
  and hashes them together with the program ID. This operation is done
  only in the third-party program.
- `derive_add` takes the address seed (result of `address_address_seed`)
  and hashes it together with the address Merkle tree public key. This
  is done both in the third-party program and in light-system-program.
  light-system-program does that as a check whether the correct Merkle
  tree is used.
  • Loading branch information
vadorovsky committed Sep 27, 2024
1 parent 202d411 commit e9befae
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 15 deletions.
6 changes: 1 addition & 5 deletions examples/name-service/programs/name-service/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ async fn test_name_service() {
address_queue_pubkey: env.address_merkle_tree_queue_pubkey,
};

let address_seed = derive_address_seed(
&[b"name-service", name.as_bytes()],
&name_service::ID,
&address_merkle_context,
);
let address_seed = derive_address_seed(&[b"name-service", name.as_bytes()], &name_service::ID);
let address = derive_address(&address_seed, &address_merkle_context);

let address_merkle_context =
Expand Down
1 change: 0 additions & 1 deletion macros/light-sdk-macros/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ pub(crate) fn process_light_accounts(input: ItemStruct) -> Result<TokenStream> {
let address_seed = ::light_sdk::address::derive_address_seed(
&#seeds,
&crate::ID,
&unpacked_address_merkle_context,
);
});
set_address_seed_calls.push(quote! {
Expand Down
11 changes: 2 additions & 9 deletions sdk/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,12 @@ pub fn pack_new_address_params(
/// &address_merkle_context,
/// );
/// ```
pub fn derive_address_seed(
seeds: &[&[u8]],
program_id: &Pubkey,
address_merkle_context: &AddressMerkleContext,
) -> [u8; 32] {
let mut inputs = Vec::with_capacity(seeds.len() + 2);
pub fn derive_address_seed(seeds: &[&[u8]], program_id: &Pubkey) -> [u8; 32] {
let mut inputs = Vec::with_capacity(seeds.len() + 1);

let program_id = program_id.to_bytes();
inputs.push(program_id.as_slice());

let merkle_tree_pubkey = address_merkle_context.address_merkle_tree_pubkey.to_bytes();
inputs.push(merkle_tree_pubkey.as_slice());

inputs.extend(seeds);

let address = hashv_to_bn254_field_size_be(inputs.as_slice());
Expand Down

0 comments on commit e9befae

Please sign in to comment.