Skip to content

improve(Address): Support new Address from nontraditional encoding #974

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 25 additions & 4 deletions src/utils/AddressUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,20 @@ export class EvmAddress extends Address {
}

// Constructs a new EvmAddress type.
static from(hexString: string): EvmAddress {
return new this(utils.arrayify(hexString));
static from(address: string, encoding: "base16" | "base58" = "base16"): EvmAddress {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nb. We have the option of inferring the encoding with a regex. That'd definitely be a UX improvement...not sure if we want to go there though.

if (encoding === "base16") {
return new this(utils.arrayify(address));
}

const decodedAddress = bs58.decode(address);
const padding = decodedAddress.subarray(0, 12);
const evmAddress = decodedAddress.subarray(12);

if (padding.length !== 12 || utils.stripZeros(padding).length !== 0 || evmAddress.length !== 20) {
throw new Error(`Not a valid base58-encoded EVM address: ${address}`);
}

return new this(evmAddress);
}
}

Expand All @@ -242,7 +254,16 @@ export class SvmAddress extends Address {
}

// Constructs a new SvmAddress type.
static from(bs58Address: string): SvmAddress {
return new this(bs58.decode(bs58Address));
static from(address: string, encoding: "base58" | "base16" = "base58"): SvmAddress {
if (encoding === "base58") {
return new this(bs58.decode(address));
}

const decodedAddress = utils.arrayify(address);
if (decodedAddress.length !== 32) {
throw new Error(`Not a valid base16-encoded SVM address: ${address}`);
}

return new this(decodedAddress);
}
}
54 changes: 48 additions & 6 deletions test/AddressUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EvmAddress, Address, SvmAddress, toAddressType } from "../src/utils";
import bs58 from "bs58";
import { bs58, EvmAddress, Address, SvmAddress, toAddressType } from "../src/utils";
import { CHAIN_IDs } from "../src/constants";
import { expect, ethers } from "./utils";

Expand All @@ -21,13 +20,56 @@ describe("Address Utils: Address Type", function () {
expect(ethers.utils.isHexString(svmToken.toAddress())).to.be.false;
});
it("Coerces addresses to their proper type when possible", function () {
const validEvmAddress = randomBytes(20);
const invalidEvmAddress = randomBytes(32);
const evmAddress = toAddressType(validEvmAddress, CHAIN_IDs.MAINNET);
const invalidEvmAddress = toAddressType(invalidEvmAddress, CHAIN_IDs.MAINNET);
const evmAddress = toAddressType(randomBytes(20), CHAIN_IDs.MAINNET);
expect(EvmAddress.isAddress(evmAddress)).to.be.true;

const invalidEvmAddress = toAddressType(randomBytes(32), CHAIN_IDs.MAINNET);
expect(EvmAddress.isAddress(invalidEvmAddress)).to.be.false;
expect(Address.isAddress(invalidEvmAddress)).to.be.true;
});
it("Handles base58-encoded EVM addresses", function () {
const rawAddress = ethers.utils.arrayify(randomBytes(20));

// Valid padding length
let padding = new Uint8Array(12);
let b58Address = bs58.encode([...padding, ...rawAddress]).toString();
const address = EvmAddress.from(b58Address, "base58");
expect(address.toAddress()).to.equal(ethers.utils.getAddress(ethers.utils.hexlify(rawAddress)));

// Wrong encoding
expect(() => EvmAddress.from(b58Address, "base16")).to.throw(Error, /invalid arrayify value/);

// Invalid EVM address length
[19, 21].forEach((len) => {
b58Address = bs58.encode([...padding, ...ethers.utils.arrayify(randomBytes(len))]).toString();
expect(() => EvmAddress.from(b58Address, "base58")).to.throw(Error, /Not a valid base58-encoded EVM address/);
});

// Invalid padding length.
[11, 13].forEach((len) => {
padding = new Uint8Array(len);
b58Address = bs58.encode([...padding, ...rawAddress]).toString();
expect(() => EvmAddress.from(b58Address, "base58")).to.throw(Error, /Not a valid base58-encoded EVM address/);
});
});
it("Handles base16-encoded SVM addresses", function () {
const rawAddress = randomBytes(32);
const expectedAddress = bs58.encode(ethers.utils.arrayify(rawAddress));

// Valid address
const address = SvmAddress.from(rawAddress, "base16");
expect(address.toAddress()).to.equal(expectedAddress);

// Wrong encoding
expect(() => SvmAddress.from(rawAddress, "base58")).to.throw(Error, /Non-base58 character/);

// Invalid SVM address length
[31, 33].forEach((len) => {
expect(() => SvmAddress.from(randomBytes(len), "base16")).to.throw(
Error,
/Not a valid base16-encoded SVM address/
);
});
});
});
});