Skip to content

Commit

Permalink
ts: remove duplicate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen committed Jan 9, 2023
1 parent 27bb695 commit b5f88f1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 92 deletions.
83 changes: 3 additions & 80 deletions ts/packages/anchor/src/utils/pubkey.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,16 @@
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";

// Sync version of web3.PublicKey.createWithSeed.
export function createWithSeedSync(
fromPublicKey: PublicKey,
seed: string,
programId: PublicKey
): PublicKey {
const buffer = Buffer.concat([
fromPublicKey.toBuffer(),
Buffer.from(seed),
programId.toBuffer(),
]);
const hash = sha256Sync.digest(buffer);
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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];
}

0 comments on commit b5f88f1

Please sign in to comment.