Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use @noble/hashes/sha256 rather than obscure package #2580

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ts/packages/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"@coral-xyz/borsh": "^0.28.0",
"@noble/hashes": "^1.3.1",
"@solana/web3.js": "^1.68.0",
"base64-js": "^1.5.1",
"bn.js": "^5.1.2",
Expand All @@ -43,7 +44,6 @@
"cross-fetch": "^3.1.5",
"crypto-hash": "^1.3.0",
"eventemitter3": "^4.0.7",
"js-sha256": "^0.9.0",
"pako": "^2.0.3",
"snake-case": "^3.0.4",
"superstruct": "^0.15.4",
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/anchor/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
26 changes: 8 additions & 18 deletions ts/packages/anchor/src/coder/borsh/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import bs58 from "bs58";
import { Buffer } from "buffer";
import { Layout } from "buffer-layout";
import camelcase from "camelcase";
import { sha256 } from "js-sha256";
import { Idl, IdlTypeDef } from "../../idl.js";
import { IdlCoder } from "./idl.js";
import { AccountsCoder } from "../index.js";
import { accountSize } from "../common.js";

/**
* Number of bytes of the account discriminator.
*/
export const ACCOUNT_DISCRIMINATOR_SIZE = 8;
import { DISCRIMINATOR_SIZE, discriminator } from "./discriminator.js";

/**
* Encodes and decodes account objects.
Expand Down Expand Up @@ -77,7 +72,7 @@ export class BorshAccountsCoder<A extends string = string>

public decodeUnchecked<T = any>(accountName: A, ix: Buffer): T {
// Chop off the discriminator before decoding.
const data = ix.slice(ACCOUNT_DISCRIMINATOR_SIZE);
const data = ix.subarray(DISCRIMINATOR_SIZE);
const layout = this.accountLayouts.get(accountName);
if (!layout) {
throw new Error(`Unknown account: ${accountName}`);
Expand All @@ -96,9 +91,7 @@ export class BorshAccountsCoder<A extends string = string>
}

public size(idlAccount: IdlTypeDef): number {
return (
ACCOUNT_DISCRIMINATOR_SIZE + (accountSize(this.idl, idlAccount) ?? 0)
);
return DISCRIMINATOR_SIZE + (accountSize(this.idl, idlAccount) ?? 0);
}

/**
Expand All @@ -107,13 +100,10 @@ export class BorshAccountsCoder<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:${camelcase(name, {
pascalCase: true,
preserveConsecutiveUppercase: true,
})}`
)
).slice(0, ACCOUNT_DISCRIMINATOR_SIZE);
const discriminatorPreimage = `account:${camelcase(name, {
pascalCase: true,
preserveConsecutiveUppercase: true,
})}`;
return discriminator(discriminatorPreimage);
}
}
10 changes: 10 additions & 0 deletions ts/packages/anchor/src/coder/borsh/discriminator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sha256 } from "@noble/hashes/sha256";

/**
* Number of bytes in anchor discriminators
*/
export const DISCRIMINATOR_SIZE = 8;

export function discriminator(preimage: string): Buffer {
return Buffer.from(sha256(preimage).slice(0, DISCRIMINATOR_SIZE));
}
4 changes: 2 additions & 2 deletions ts/packages/anchor/src/coder/borsh/event.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Buffer } from "buffer";
import * as base64 from "base64-js";
import { Layout } from "buffer-layout";
import { sha256 } from "js-sha256";
import { Idl, IdlEvent, IdlTypeDef } from "../../idl.js";
import { Event, EventData } from "../../program/event.js";
import { IdlCoder } from "./idl.js";
import { EventCoder } from "../index.js";
import { discriminator } from "./discriminator.js";

export class BorshEventCoder implements EventCoder {
/**
Expand Down Expand Up @@ -78,5 +78,5 @@ export class BorshEventCoder implements EventCoder {
}

export function eventDiscriminator(name: string): Buffer {
return Buffer.from(sha256.digest(`event:${name}`)).slice(0, 8);
return discriminator(`event:${name}`);
}
3 changes: 2 additions & 1 deletion ts/packages/anchor/src/coder/borsh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { BorshTypesCoder } from "./types.js";
import { Coder } from "../index.js";

export { BorshInstructionCoder } from "./instruction.js";
export { BorshAccountsCoder, ACCOUNT_DISCRIMINATOR_SIZE } from "./accounts.js";
export { BorshAccountsCoder } from "./accounts.js";
export { DISCRIMINATOR_SIZE } from "./discriminator.js";
export { BorshEventCoder, eventDiscriminator } from "./event.js";

/**
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/anchor/src/coder/borsh/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Buffer } from "buffer";
import { Layout } from "buffer-layout";
import camelCase from "camelcase";
import { snakeCase } from "snake-case";
import { sha256 } from "js-sha256";
import * as borsh from "@coral-xyz/borsh";
import { AccountMeta, PublicKey } from "@solana/web3.js";
import {
Expand All @@ -21,6 +20,7 @@ import {
} from "../../idl.js";
import { IdlCoder } from "./idl.js";
import { InstructionCoder } from "../index.js";
import { sha256 } from "@noble/hashes/sha256";

/**
* Namespace for global instruction function signatures (i.e. functions
Expand Down Expand Up @@ -352,5 +352,5 @@ function sentenceCase(field: string): string {
function sighash(nameSpace: string, ixName: string): Buffer {
let name = snakeCase(ixName);
let preimage = `${nameSpace}:${name}`;
return Buffer.from(sha256.digest(preimage)).slice(0, 8);
return Buffer.from(sha256(preimage).slice(0, 8));
}
5 changes: 2 additions & 3 deletions ts/packages/anchor/src/utils/pubkey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from "buffer";
import { PublicKey } from "@solana/web3.js";
import { Address, translateAddress } from "../program/common.js";
import { sha256 as sha256Sync } from "js-sha256";
import { sha256 } from "@noble/hashes/sha256";

// Sync version of web3.PublicKey.createWithSeed.
export function createWithSeedSync(
Expand All @@ -14,8 +14,7 @@ export function createWithSeedSync(
Buffer.from(seed),
programId.toBuffer(),
]);
const hash = sha256Sync.digest(buffer);
return new PublicKey(Buffer.from(hash));
return new PublicKey(sha256(buffer));
}

export function associated(
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/anchor/src/utils/sha256.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sha256 } from "js-sha256";
import { sha256 } from "@noble/hashes/sha256";

export function hash(data: string): string {
return sha256(data);
return new TextDecoder().decode(sha256(data));
}
12 changes: 4 additions & 8 deletions ts/packages/anchor/tests/coder-accounts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as assert from "assert";
import { createNodeArray } from "typescript";
import { BorshCoder } from "../src";
import { sha256 } from "js-sha256";
import { ACCOUNT_DISCRIMINATOR_SIZE } from "../src/coder/borsh/accounts";
import { DISCRIMINATOR_SIZE } from "../src/coder/borsh/discriminator";
import { sha256 } from "@noble/hashes/sha256";

describe("coder.accounts", () => {
test("Can encode and decode user-defined accounts, including those with consecutive capital letters", () => {
Expand Down Expand Up @@ -40,11 +39,8 @@ describe("coder.accounts", () => {
coder.accounts.encode("MemberDAO", memberDAO).then((encoded) => {
// start of encoded account = account discriminator
assert.deepEqual(
encoded.subarray(0, ACCOUNT_DISCRIMINATOR_SIZE),
Buffer.from(sha256.digest("account:MemberDAO")).subarray(
0,
ACCOUNT_DISCRIMINATOR_SIZE
)
encoded.subarray(0, DISCRIMINATOR_SIZE),
Buffer.from(sha256("account:MemberDAO").slice(0, DISCRIMINATOR_SIZE))
);
assert.deepEqual(coder.accounts.decode("MemberDAO", encoded), memberDAO);
});
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-associated-token-account/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-binary-option/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-binary-oracle-pair/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-feature-proposal/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-governance/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-memo/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-name-service/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-record/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-stake-pool/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-stateless-asks/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-token-lending/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-token-swap/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/spl-token/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default {
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"@noble/hashes/sha256",
"pako",
"toml",
],
Expand Down
10 changes: 5 additions & 5 deletions ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,11 @@
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.3.tgz#360afc77610e0a61f3417e497dcf36862e4f8111"
integrity sha512-CE0FCR57H2acVI5UOzIGSSIYxZ6v/HOhDR0Ro9VLyhnzLwx0o8W1mmgaqlEUx4049qJDlIBRztv5k+MM8vbO3A==

"@noble/hashes@^1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==

"@noble/secp256k1@^1.6.3":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.7.0.tgz#d15357f7c227e751d90aa06b05a0e5cf993ba8c1"
Expand Down Expand Up @@ -3253,11 +3258,6 @@ [email protected]:
import-local "^3.0.2"
jest-cli "^27.3.1"

js-sha256@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/js-sha256/-/js-sha256-0.9.0.tgz#0b89ac166583e91ef9123644bd3c5334ce9d0966"
integrity sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down
Loading