Skip to content

Commit

Permalink
ts: Capitalize account discriminator in AccountCoder (coral-xyz#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm authored Oct 27, 2021
1 parent 5fa263f commit ec26966
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ts/src/coder/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Layout } from "buffer-layout";
import { Idl } from "../idl";
import { IdlCoder } from "./idl";
import { sha256 } from "js-sha256";
import camelcase from "camelcase";

/**
* Number of bytes of the account discriminator.
Expand Down Expand Up @@ -43,7 +44,7 @@ export class AccountsCoder<A extends string = string> {

public decode<T = any>(accountName: A, ix: Buffer): T {
// Chop off the discriminator before decoding.
const data = ix.slice(8);
const data = ix.slice(ACCOUNT_DISCRIMINATOR_SIZE);
const layout = this.accountLayouts.get(accountName);
if (!layout) {
throw new Error(`Unknown account: ${accountName}`);
Expand All @@ -57,6 +58,8 @@ export class AccountsCoder<A extends string = string> {
* @param name The name of the account to calculate the discriminator.
*/
public static accountDiscriminator(name: string): Buffer {
return Buffer.from(sha256.digest(`account:${name}`)).slice(0, 8);
return Buffer.from(
sha256.digest(`account:${camelcase(name, { pascalCase: true })}`)
).slice(0, ACCOUNT_DISCRIMINATOR_SIZE);
}
}

0 comments on commit ec26966

Please sign in to comment.