Skip to content
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

chore(EIP55): optional EIP55 validation #206

Open
wants to merge 1 commit into
base: main
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
4 changes: 2 additions & 2 deletions packages/siwe-parser/lib/abnf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class ParsedMessage {
requestId: string | null;
resources: Array<string> | null;

constructor(msg: string) {
constructor(msg: string, parserOptions = { validateEIP55Address: true }) {
const parser = new apgLib.parser();
parser.ast = new apgLib.ast();
const id = apgLib.ids;
Expand Down Expand Up @@ -359,7 +359,7 @@ export class ParsedMessage {
throw new Error("Domain cannot be empty.");
}

if (!isEIP55Address(this.address)) {
if (parserOptions.validateEIP55Address && !isEIP55Address(this.address)) {
throw new Error("Address not conformant to EIP-55.");
}
}
Expand Down
16 changes: 13 additions & 3 deletions packages/siwe/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getAddress, Provider, verifyMessage } from './ethersCompat';
import {
SiweError,
SiweErrorType,
SiweMessageConstructorOptions,
SiweResponse,
VerifyOpts,
VerifyOptsKeys,
Expand Down Expand Up @@ -61,15 +62,24 @@ export class SiweMessage {
* expressed as RFC 3986 URIs separated by `\n- `. */
resources?: Array<string>;

/** If the library should validate the address against EIP-55, defaults to true */
validateEIP55Address = true;

/**
* Creates a parsed Sign-In with Ethereum Message (EIP-4361) object from a
* string or an object. If a string is used an ABNF parser is called to
* validate the parameter, otherwise the fields are attributed.
* @param param {string | SiweMessage} Sign message as a string or an object.
*/
constructor(param: string | Partial<SiweMessage>) {
constructor(param: string | Partial<SiweMessage>, options?: SiweMessageConstructorOptions) {
if(options.validateEIP55Address !== undefined){
this.validateEIP55Address = options.validateEIP55Address;
}

if (typeof param === 'string') {
const parsedMessage = new ParsedMessage(param);
const parsedMessage = new ParsedMessage(param, {
validateEIP55Address: this.validateEIP55Address
});
this.scheme = parsedMessage.scheme;
this.domain = parsedMessage.domain;
this.address = parsedMessage.address;
Expand Down Expand Up @@ -426,7 +436,7 @@ export class SiweMessage {
}

/** EIP-55 `address` check. */
if (!isEIP55Address(this.address)) {
if (this.validateEIP55Address && !isEIP55Address(this.address)) {
throw new SiweError(
SiweErrorType.INVALID_ADDRESS,
getAddress(this.address),
Expand Down
5 changes: 5 additions & 0 deletions packages/siwe/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,8 @@ export enum SiweErrorType {
/** Thrown when some required field is missing. */
UNABLE_TO_PARSE = 'Unable to parse the message.',
}

export interface SiweMessageConstructorOptions {
/** If the library should validate the address against EIP-55, defaults to true */
validateEIP55Address?: boolean
}