Skip to content

Commit 81f81ec

Browse files
lang, ts: Namespace state discriminator (otter-sec#320)
1 parent 9a7cfd5 commit 81f81ec

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ incremented for features.
2020
## Breaking Changes
2121

2222
* lang: `#[account(associated)]` now requires `init` to be provided to create an associated account. If not provided, then the address will be assumed to exist, and a constraint will be added to ensure its correctness ([#318](https://github.com/project-serum/anchor/pull/318)).
23+
* lang, ts: Change account discriminator pre-image of the `#[state]` account discriminator to be namespaced by "state:". This change should only be noticed by library maintainers ([#320](https://github.com/project-serum/anchor/pull/320)).
2324
* lang, ts: Change domain delimiters for the pre-image of the instruciton sighash to be a single colon `:` to be consistent with accounts. This change should only be noticed by library maintainers.
2425

2526
## [0.6.0] - 2021-05-23

lang/attribute/account/src/lib.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,24 @@ pub fn account(
5757
args: proc_macro::TokenStream,
5858
input: proc_macro::TokenStream,
5959
) -> proc_macro::TokenStream {
60-
let namespace = args.to_string().replace("\"", "");
61-
let is_zero_copy = match args.into_iter().next() {
62-
None => false,
63-
Some(tt) => match tt {
64-
proc_macro::TokenTree::Literal(_) => false,
65-
_ => namespace == "zero_copy",
66-
},
67-
};
60+
let mut namespace = "".to_string();
61+
let mut is_zero_copy = false;
62+
if args.to_string().split(",").collect::<Vec<_>>().len() > 2 {
63+
panic!("Only two args are allowed to the account attribute.")
64+
}
65+
for arg in args.to_string().split(",") {
66+
let ns = arg
67+
.to_string()
68+
.replace("\"", "")
69+
.chars()
70+
.filter(|c| !c.is_whitespace())
71+
.collect();
72+
if ns == "zero_copy" {
73+
is_zero_copy = true;
74+
} else {
75+
namespace = ns;
76+
}
77+
}
6878

6979
let account_strct = parse_macro_input!(input as syn::ItemStruct);
7080
let account_name = &account_strct.ident;
@@ -73,7 +83,7 @@ pub fn account(
7383
// Namespace the discriminator to prevent collisions.
7484
let discriminator_preimage = {
7585
// For now, zero copy accounts can't be namespaced.
76-
if is_zero_copy || namespace.is_empty() {
86+
if namespace.is_empty() {
7787
format!("account:{}", account_name.to_string())
7888
} else {
7989
format!("{}:{}", namespace, account_name.to_string())

lang/attribute/state/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ pub fn state(
7171
};
7272

7373
let attribute = match is_zero_copy {
74-
false => quote! {#[account]},
75-
true => quote! {#[account(zero_copy)]},
74+
false => quote! {#[account("state")]},
75+
true => quote! {#[account("state", zero_copy)]},
7676
};
7777

7878
proc_macro::TokenStream::from(quote! {

ts/src/coder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ class IdlCoder {
340340
case "publicKey": {
341341
return borsh.publicKey(fieldName);
342342
}
343-
// TODO: all the other types that need to be exported by the borsh package.
344343
default: {
345344
// @ts-ignore
346345
if (field.type.vec) {
@@ -452,7 +451,7 @@ export async function accountDiscriminator(name: string): Promise<Buffer> {
452451
// Calculates unique 8 byte discriminator prepended to all anchor state accounts.
453452
export async function stateDiscriminator(name: string): Promise<Buffer> {
454453
// @ts-ignore
455-
return Buffer.from(sha256.digest(`account:${name}`)).slice(0, 8);
454+
return Buffer.from(sha256.digest(`state:${name}`)).slice(0, 8);
456455
}
457456

458457
export function eventDiscriminator(name: string): Buffer {

0 commit comments

Comments
 (0)