ts: Capitalize account discriminator in `AccountCoder` (#931)

This commit is contained in:
Matthew Callens 2021-10-27 14:09:40 -04:00 committed by GitHub
parent 5fa263ff17
commit ec26966340
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 2 deletions

View File

@ -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.
@ -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}`);
@ -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);
}
}