diff --git a/CHANGELOG.md b/CHANGELOG.md index 3da4e92c..9e0aada0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ incremented for features. ## Breaking Changes * 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)). +* 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)). * 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. ## [0.6.0] - 2021-05-23 diff --git a/lang/attribute/account/src/lib.rs b/lang/attribute/account/src/lib.rs index 3e81199e..597ea47b 100644 --- a/lang/attribute/account/src/lib.rs +++ b/lang/attribute/account/src/lib.rs @@ -57,14 +57,24 @@ pub fn account( args: proc_macro::TokenStream, input: proc_macro::TokenStream, ) -> proc_macro::TokenStream { - let namespace = args.to_string().replace("\"", ""); - let is_zero_copy = match args.into_iter().next() { - None => false, - Some(tt) => match tt { - proc_macro::TokenTree::Literal(_) => false, - _ => namespace == "zero_copy", - }, - }; + let mut namespace = "".to_string(); + let mut is_zero_copy = false; + if args.to_string().split(",").collect::>().len() > 2 { + panic!("Only two args are allowed to the account attribute.") + } + for arg in args.to_string().split(",") { + let ns = arg + .to_string() + .replace("\"", "") + .chars() + .filter(|c| !c.is_whitespace()) + .collect(); + if ns == "zero_copy" { + is_zero_copy = true; + } else { + namespace = ns; + } + } let account_strct = parse_macro_input!(input as syn::ItemStruct); let account_name = &account_strct.ident; @@ -73,7 +83,7 @@ pub fn account( // Namespace the discriminator to prevent collisions. let discriminator_preimage = { // For now, zero copy accounts can't be namespaced. - if is_zero_copy || namespace.is_empty() { + if namespace.is_empty() { format!("account:{}", account_name.to_string()) } else { format!("{}:{}", namespace, account_name.to_string()) diff --git a/lang/attribute/state/src/lib.rs b/lang/attribute/state/src/lib.rs index da98d7a3..c6387aff 100644 --- a/lang/attribute/state/src/lib.rs +++ b/lang/attribute/state/src/lib.rs @@ -71,8 +71,8 @@ pub fn state( }; let attribute = match is_zero_copy { - false => quote! {#[account]}, - true => quote! {#[account(zero_copy)]}, + false => quote! {#[account("state")]}, + true => quote! {#[account("state", zero_copy)]}, }; proc_macro::TokenStream::from(quote! { diff --git a/ts/src/coder.ts b/ts/src/coder.ts index 31631066..bbf57f2b 100644 --- a/ts/src/coder.ts +++ b/ts/src/coder.ts @@ -340,7 +340,6 @@ class IdlCoder { case "publicKey": { return borsh.publicKey(fieldName); } - // TODO: all the other types that need to be exported by the borsh package. default: { // @ts-ignore if (field.type.vec) { @@ -452,7 +451,7 @@ export async function accountDiscriminator(name: string): Promise { // Calculates unique 8 byte discriminator prepended to all anchor state accounts. export async function stateDiscriminator(name: string): Promise { // @ts-ignore - return Buffer.from(sha256.digest(`account:${name}`)).slice(0, 8); + return Buffer.from(sha256.digest(`state:${name}`)).slice(0, 8); } export function eventDiscriminator(name: string): Buffer {