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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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);
}
}
57 changes: 52 additions & 5 deletions test/AddressUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EvmAddress, Address, SvmAddress, toAddressType } from "../src/utils";
import bs58 from "bs58";
import { CHAIN_IDs } from "../src/constants";
import { bs58 } from "../src/utils";

Check failure on line 3 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

'../src/utils' import is duplicated
import { expect, ethers } from "./utils";

describe("Address Utils: Address Type", function () {
Expand All @@ -21,13 +21,60 @@
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();

Check warning on line 36 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
let address = EvmAddress.from(b58Address, "base58");

Check failure on line 37 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

'address' is never reassigned. Use 'const' instead
expect(address.toAddress()).to.equal(ethers.utils.getAddress(ethers.utils.hexlify(rawAddress)));

// Wrong encoding
expect(() => EvmAddress.from(b58Address, "base16"))

Check warning on line 41 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎········`
.to.throw(Error, /invalid arrayify value/);

// Invalid EVM address length
[19, 21].forEach((len) => {
b58Address = bs58.encode(

Check warning on line 46 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎··········[·...padding,·...ethers.utils.arrayify(randomBytes(len))]⏎········` with `[...padding,·...ethers.utils.arrayify(randomBytes(len))]`
[ ...padding, ...ethers.utils.arrayify(randomBytes(len))]
).toString();
expect(() => EvmAddress.from(b58Address, "base58"))

Check warning on line 49 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎··········`
.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();

Check warning on line 56 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
expect(() => EvmAddress.from(b58Address, "base58"))

Check warning on line 57 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎··········`
.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
let address = SvmAddress.from(rawAddress, "base16");

Check failure on line 66 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

'address' is never reassigned. Use 'const' instead
expect(address.toAddress()).to.equal(expectedAddress);

// Wrong encoding
expect(() => SvmAddress.from(rawAddress, "base58"))

Check warning on line 70 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎········`
.to.throw(Error, /Non-base58 character/);

// Invalid SVM address length
[31, 33].forEach((len) => {
expect(() => SvmAddress.from(randomBytes(len), "base16"))

Check warning on line 75 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `.to.throw(`
.to.throw(Error, /Not a valid base16-encoded SVM address/);

Check warning on line 76 in test/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `.to.throw(Error,·/Not·a·valid·base16-encoded·SVM·address/` with `Error,⏎··········/Not·a·valid·base16-encoded·SVM·address/⏎········`
});
});
});
});
Loading