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

ts: remove duplicate functions #2357

Merged
merged 4 commits into from Jan 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Breaking

- lang: Remove `state` and `interface` attributes ([#2285](https://github.com/coral-xyz/anchor/pull/2285)).
- ts: Remove `createProgramAddressSync`, `findProgramAddressSync` (now available in `@solana/web3.js`) and update `associatedAddress` to be synchronous ([#2357](https://github.com/coral-xyz/anchor/pull/2357)).

## [0.26.0] - 2022-12-15

Expand Down
7 changes: 3 additions & 4 deletions tests/bpf-upgradeable-state/tests/bpf-upgradable-state.ts
@@ -1,6 +1,5 @@
import * as anchor from "@coral-xyz/anchor";
import { AnchorError, Program } from "@coral-xyz/anchor";
import { findProgramAddressSync } from "@coral-xyz/anchor/dist/cjs/utils/pubkey";
import { PublicKey } from "@solana/web3.js";
import { assert } from "chai";
import { BpfUpgradeableState } from "../target/types/bpf_upgradeable_state";
Expand All @@ -12,7 +11,7 @@ describe("bpf_upgradeable_state", () => {

const program = anchor.workspace
.BpfUpgradeableState as Program<BpfUpgradeableState>;
const programDataAddress = findProgramAddressSync(
const programDataAddress = PublicKey.findProgramAddressSync(
[program.programId.toBytes()],
new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
)[0];
Expand Down Expand Up @@ -147,7 +146,7 @@ describe("bpf_upgradeable_state", () => {
const secondProgramAddress = new PublicKey(
"Fkv67TwmbakfZw2PoW57wYPbqNexAH6vuxpyT8vmrc3B"
);
const secondProgramProgramDataAddress = findProgramAddressSync(
const secondProgramProgramDataAddress = PublicKey.findProgramAddressSync(
[secondProgramAddress.toBytes()],
new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
)[0];
Expand Down Expand Up @@ -176,7 +175,7 @@ describe("bpf_upgradeable_state", () => {
const secondProgramAddress = new PublicKey(
"Fkv67TwmbakfZw2PoW57wYPbqNexAH6vuxpyT8vmrc3B"
);
const secondProgramProgramDataAddress = findProgramAddressSync(
const secondProgramProgramDataAddress = PublicKey.findProgramAddressSync(
[secondProgramAddress.toBytes()],
new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
)[0];
Expand Down
5 changes: 2 additions & 3 deletions tests/pda-derivation/tests/typescript.spec.ts
@@ -1,7 +1,6 @@
import * as anchor from "@coral-xyz/anchor";
import BN from "bn.js";
import { Keypair } from "@solana/web3.js";
import { findProgramAddressSync } from "@coral-xyz/anchor/dist/cjs/utils/pubkey";
import { Keypair, PublicKey } from "@solana/web3.js";
import { Program } from "@coral-xyz/anchor";
import { PdaDerivation } from "../target/types/pda_derivation";
import { expect } from "chai";
Expand Down Expand Up @@ -43,7 +42,7 @@ describe("typescript", () => {
const MY_SEED_U8 = 1;
const MY_SEED_U32 = 2;
const MY_SEED_U64 = 3;
const expectedPDAKey = findProgramAddressSync(
const expectedPDAKey = PublicKey.findProgramAddressSync(
[
Buffer.from([seedA]),
encode("another-seed"),
Expand Down
69 changes: 4 additions & 65 deletions ts/packages/anchor/src/utils/pubkey.ts
@@ -1,8 +1,7 @@
import { Buffer } from "buffer";
import BN from "bn.js";
import { sha256 as sha256Sync } from "js-sha256";
import { PublicKey } from "@solana/web3.js";
import { Address, translateAddress } from "../program/common.js";
import { sha256 as sha256Sync } from "js-sha256";

// Sync version of web3.PublicKey.createWithSeed.
export function createWithSeedSync(
Expand All @@ -19,75 +18,15 @@ export function createWithSeedSync(
return new PublicKey(Buffer.from(hash));
}

// Sync version of web3.PublicKey.createProgramAddress.
export function createProgramAddressSync(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey
): PublicKey {
const MAX_SEED_LENGTH = 32;

let buffer = Buffer.alloc(0);
seeds.forEach(function (seed) {
if (seed.length > MAX_SEED_LENGTH) {
throw new TypeError(`Max seed length exceeded`);
}
buffer = Buffer.concat([buffer, toBuffer(seed)]);
});
buffer = Buffer.concat([
buffer,
programId.toBuffer(),
Buffer.from("ProgramDerivedAddress"),
]);
let hash = sha256Sync(new Uint8Array(buffer));
let publicKeyBytes = new BN(hash, 16).toArray(undefined, 32);
if (PublicKey.isOnCurve(new Uint8Array(publicKeyBytes))) {
throw new Error(`Invalid seeds, address must fall off the curve`);
}
return new PublicKey(publicKeyBytes);
}

// Sync version of web3.PublicKey.findProgramAddress.
export function findProgramAddressSync(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey
): [PublicKey, number] {
let nonce = 255;
let address: PublicKey | undefined;
while (nonce != 0) {
try {
const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
address = createProgramAddressSync(seedsWithNonce, programId);
} catch (err) {
if (err instanceof TypeError) {
throw err;
}
nonce--;
continue;
}
return [address, nonce];
}
throw new Error(`Unable to find a viable program address nonce`);
}

const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {
if (arr instanceof Buffer) {
return arr;
} else if (arr instanceof Uint8Array) {
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);
} else {
return Buffer.from(arr);
}
};

export async function associated(
export function associated(
programId: Address,
...args: Array<Address | Buffer>
): Promise<PublicKey> {
): PublicKey {
let seeds = [Buffer.from([97, 110, 99, 104, 111, 114])]; // b"anchor".
args.forEach((arg) => {
seeds.push(arg instanceof Buffer ? arg : translateAddress(arg).toBuffer());
});
const [assoc] = await PublicKey.findProgramAddress(
const [assoc] = PublicKey.findProgramAddressSync(
seeds,
translateAddress(programId)
);
Expand Down
6 changes: 1 addition & 5 deletions ts/packages/anchor/src/utils/rpc.ts
@@ -1,5 +1,4 @@
import { Buffer } from "buffer";
import assert from "assert";
import {
AccountInfo,
AccountMeta,
Expand All @@ -17,22 +16,19 @@ import {
} from "@solana/web3.js";
import { chunks } from "../utils/common.js";
import { Address, translateAddress } from "../program/common.js";
import Provider, { getProvider, Wallet } from "../provider.js";
import Provider, { getProvider } from "../provider.js";
import {
type as pick,
number,
string,
array,
boolean,
literal,
record,
union,
optional,
nullable,
coerce,
instance,
create,
tuple,
unknown,
any,
Struct,
Expand Down
12 changes: 5 additions & 7 deletions ts/packages/anchor/src/utils/token.ts
Expand Up @@ -7,17 +7,15 @@ export const ASSOCIATED_PROGRAM_ID = new PublicKey(
"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
);

export async function associatedAddress({
export function associatedAddress({
mint,
owner,
}: {
mint: PublicKey;
owner: PublicKey;
}): Promise<PublicKey> {
return (
await PublicKey.findProgramAddress(
[owner.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
ASSOCIATED_PROGRAM_ID
)
}): PublicKey {
return PublicKey.findProgramAddressSync(
[owner.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
ASSOCIATED_PROGRAM_ID
)[0];
}