Skip to content

Commit

Permalink
fix: translateAddress should not rely on PublicKey constructor nam (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCryptoSheik committed Dec 15, 2021
1 parent 911620e commit a7e8007
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,10 @@ incremented for features.

## [Unreleased]

### Fixes

* ts: fix `translateAddress` which currently leads to failing browser code. Now uses `PublicKey` constructor instead of prototype chain constructor name checking which doesn't work in the presence of code minifying/mangling([1138](https://github.com/project-serum/anchor/pull/1138))

### Features

* lang: Add `programdata_address: Option<Pubkey>` field to `Program` account. Will be populated if account is a program owned by the upgradable bpf loader ([#1125](https://github.com/project-serum/anchor/pull/1125))
Expand Down
12 changes: 6 additions & 6 deletions tests/yarn.lock
Expand Up @@ -50,17 +50,17 @@
snake-case "^3.0.4"
toml "^3.0.0"

"@project-serum/anchor@^0.18.2":
version "0.18.2"
resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.18.2.tgz#0f13b5c2046446b7c24cf28763eec90febb28485"
integrity sha512-uyjiN/3Ipp+4hrZRm/hG18HzGLZyvP790LXrCsGO3IWxSl28YRhiGEpKnZycfMW94R7nxdUoE3wY67V+ZHSQBQ==
"@project-serum/anchor@^0.19.0":
version "0.19.0"
resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.19.0.tgz#79f1fbe7c3134860ccbfe458a0e09daf79644885"
integrity sha512-cs0LBmJOrL9eJ8MRNqitnzbpCT5QEzVdJmiIjfNV5YaGn1K9vISR7DtISj3Bdl3KBdLqii4CTw1mpHdi8iXUCg==
dependencies:
"@project-serum/borsh" "^0.2.2"
"@solana/web3.js" "^1.17.0"
base64-js "^1.5.1"
bn.js "^5.1.2"
bs58 "^4.0.1"
buffer-layout "^1.2.0"
buffer-layout "^1.2.2"
camelcase "^5.3.1"
crypto-hash "^1.3.0"
eventemitter3 "^4.0.7"
Expand Down Expand Up @@ -334,7 +334,7 @@ buffer-from@^1.0.0, buffer-from@^1.1.0:
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==

buffer-layout@^1.2.0:
buffer-layout@^1.2.0, buffer-layout@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/buffer-layout/-/buffer-layout-1.2.2.tgz#b9814e7c7235783085f9ca4966a0cfff112259d5"
integrity sha512-kWSuLN694+KTk8SrYvCqwP2WcgQjoRCiF5b4QDvkkz8EmgD+aWAIceGFKMIAdmF/pH+vpgNV3d3kAKorcdAmWA==
Expand Down
9 changes: 1 addition & 8 deletions ts/src/program/common.ts
Expand Up @@ -55,14 +55,7 @@ export function validateAccounts(

// Translates an address to a Pubkey.
export function translateAddress(address: Address): PublicKey {
if (typeof address === "string") {
const pk = new PublicKey(address);
return pk;
} else if (address.constructor.prototype.constructor.name === "PublicKey") {
return address;
} else {
throw new Error("Given address is not a PublicKey nor a string.");
}
return address instanceof PublicKey ? address : new PublicKey(address);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions ts/tests/program-common.spec.ts
@@ -0,0 +1,56 @@
import BN from "bn.js";
import bs58 from "bs58";
import { PublicKey } from "@solana/web3.js";

import { translateAddress } from "../src/program/common";

describe("program/common", () => {
describe("translateAddress", () => {
it("should accept a valid string address", () => {
const address = "11111111111111111111111111111111";

const func = () => translateAddress(address);
expect(func).not.toThrow();

const output = func();
expect(output).toBeInstanceOf(PublicKey);
expect(new PublicKey(address).equals(output)).toBeTruthy();
});

it("should accept a PublicKey address", () => {
const publicKey = new PublicKey("11111111111111111111111111111111");

const func = () => translateAddress(publicKey);
expect(func).not.toThrow();

const output = func();
expect(output).toBeInstanceOf(PublicKey);
expect(new PublicKey(publicKey).equals(output)).toBe(true);
});

it("should accept an object with a PublicKey shape { _bn }", () => {
const obj = ({
_bn: new BN(bs58.decode("11111111111111111111111111111111")),
} as any) as PublicKey;
const func = () => translateAddress(obj);

expect(func).not.toThrow();
const output = func();

expect(output).toBeInstanceOf(PublicKey);
expect(new PublicKey(obj).equals(output)).toBe(true);
});

it("should not accept an invalid string address", () => {
const invalid = "invalid";
const func = () => translateAddress(invalid);
expect(func).toThrow();
});

it("should not accept an invalid object", () => {
const invalid = {} as PublicKey;
const func = () => translateAddress(invalid);
expect(func).toThrow();
});
});
});

0 comments on commit a7e8007

Please sign in to comment.