Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: translateAddress should not rely on PublicKey constructor nam
Since anchor is used in web projects which will minify/mangle function names, checking an input object for a custom constructor name is unsafe. Moreover, checking the type of the input Address is unnecessary since `PublicKey`'s constructor handles: - `string` - `array` - object with a `{ _bn }` shape (like `PublicKey` itself) For additional safety, a unit test should be added to prevent `PublicKey` to stop supporting this unexpectedly. See `PublicKey` implementation: ``` /** * JSON object representation of PublicKey class */ export type PublicKeyData = { /** @internal */ _bn: BN; }; function isPublicKeyData(value: PublicKeyInitData): value is PublicKeyData { return (value as PublicKeyData)._bn !== undefined; } export class PublicKey extends Struct { /** @internal */ _bn: BN; /** * Create a new PublicKey object * @param value ed25519 public key as buffer or base-58 encoded string */ constructor(value: PublicKeyInitData) { super({}); if (isPublicKeyData(value)) { this._bn = value._bn; } else { if (typeof value === 'string') { // assume base 58 encoding by default const decoded = bs58.decode(value); if (decoded.length != 32) { throw new Error(`Invalid public key input`); } this._bn = new BN(decoded); } else { this._bn = new BN(value); } if (this._bn.byteLength() > 32) { throw new Error(`Invalid public key input`); } } } ```
- Loading branch information