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
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions src/utils/AddressUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,20 @@
}

// 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));

Check warning on line 228 in src/utils/AddressUtils.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`
}

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 Down
Loading