Skip to content

Commit ddc293a

Browse files
lang: Namespaceable account discriminators (otter-sec#128)
1 parent 17b4cc0 commit ddc293a

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ incremented for features.
1616
* cli: Specify test files to run ([#118](https://github.com/project-serum/anchor/pull/118)).
1717
* lang: Allow overriding the `#[state]` account's size ([#121](https://github.com/project-serum/anchor/pull/121)).
1818
* lang, client, ts: Add event emission and subscriptions ([#89](https://github.com/project-serum/anchor/pull/89)).
19+
* lang/account: Allow namespacing account discriminators ([#128](https://github.com/project-serum/anchor/pull/128)).
1920

2021
## Breaking Changes
2122

2223
* client: Replace url str with `Cluster` struct when constructing clients ([#89](https://github.com/project-serum/anchor/pull/89)).
24+
* lang: Changes the account discriminator of `IdlAccount` to be namespaced by `"internal"` ([#128](https://github.com/project-serum/anchor/pull/128)).
2325

2426
## [0.3.0] - 2021-03-12
2527

lang/attribute/account/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,24 @@ use syn::parse_macro_input;
1919
/// and the account deserialization will exit with an error.
2020
#[proc_macro_attribute]
2121
pub fn account(
22-
_args: proc_macro::TokenStream,
22+
args: proc_macro::TokenStream,
2323
input: proc_macro::TokenStream,
2424
) -> proc_macro::TokenStream {
25+
let namespace = args.to_string().replace("\"", "");
26+
2527
let account_strct = parse_macro_input!(input as syn::ItemStruct);
2628
let account_name = &account_strct.ident;
2729

2830
let discriminator: proc_macro2::TokenStream = {
2931
// Namespace the discriminator to prevent collisions.
30-
let discriminator_preimage = format!("account:{}", account_name.to_string());
32+
let discriminator_preimage = {
33+
if namespace.is_empty() {
34+
format!("account:{}", account_name.to_string())
35+
} else {
36+
format!("{}:{}", namespace, account_name.to_string())
37+
}
38+
};
39+
3140
let mut discriminator = [0u8; 8];
3241
discriminator.copy_from_slice(
3342
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8],

lang/src/idl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub struct IdlSetBuffer<'info> {
8181
//
8282
// Note: we use the same account for the "write buffer", similar to the
8383
// bpf upgradeable loader's mechanism.
84-
#[account]
84+
#[account("internal")]
8585
#[derive(Debug)]
8686
pub struct IdlAccount {
8787
// Address that can modify the IDL.

0 commit comments

Comments
 (0)