Skip to content

Commit

Permalink
Splits the RegExp to make it more readable (spruceid#24)
Browse files Browse the repository at this point in the history
* Improves RegExp readability and allow Nonces bigger then 8

* Fix dependabot warning

* Moves begin and and to message, moves endlines to beggining of sentence
  • Loading branch information
w4ll3 authored Dec 13, 2021
1 parent d3d8519 commit bc80efb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
34 changes: 17 additions & 17 deletions examples/notepad/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions lib/regex.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const DOMAIN = '([^/?#]*)';
const ADDRESS = '0x[a-zA-Z0-9]{40}';
const URI = '(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?';
const DATETIME =
'([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\\.[0-9]+)?(([Zz])|([\\+|\\-]([01][0-9]|2[0-3]):[0-5][0-9]))';
const REQUESTID = "[-._~!$&'()*+,;=:@%a-zA-Z0-9]*";
const DOMAIN =
'(?<domain>([^?#]*)) wants you to sign in with your Ethereum account:';
const ADDRESS = '\\n(?<address>0x[a-zA-Z0-9]{40})\\n\\n';
const STATEMENT = '((?<statement>[^\\n]+)\\n)?';
const URI = '(([^:?#]+):)?(([^?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))';
const URI_LINE = `\\nURI: (?<uri>${URI}?)`;
const VERSION = '\\nVersion: (?<version>1)';
const CHAIN_ID = '\\nChain ID: (?<chainId>[0-9]+)';
const NONCE = '\\nNonce: (?<nonce>[a-zA-Z0-9]{8,})';
const DATETIME = `([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`;
const ISSUED_AT = `\\nIssued At: (?<issuedAt>${DATETIME})`;
const EXPIRATION_TIME = `(\\nExpiration Time: (?<expirationTime>${DATETIME}))?`;
const NOT_BEFORE = `(\\nNot Before: (?<notBefore>${DATETIME}))?`;
const REQUEST_ID =
"(\\nRequest ID: (?<requestId>[-._~!$&'()*+,;=:@%a-zA-Z0-9]*))?";
const RESOURCES = `(\\nResources:(?<resources>(\\n- ${URI}?)+))?`;

const MESSAGE = `^${DOMAIN}${ADDRESS}${STATEMENT}${URI_LINE}${VERSION}${CHAIN_ID}${NONCE}${ISSUED_AT}${EXPIRATION_TIME}${NOT_BEFORE}${REQUEST_ID}${RESOURCES}$`;

export class ParsedMessage {
domain: string;
Expand All @@ -21,10 +33,7 @@ export class ParsedMessage {
match?: RegExpExecArray;

constructor(msg: string) {
const REGEX = new RegExp(
`^(?<domain>${DOMAIN})\\ wants\\ you\\ to\\ sign\\ in\\ with\\ your\\ Ethereum\\ account\\:\\n(?<address>${ADDRESS})\\n\\n((?<statement>[^\\n]+)\\n)?\\nURI\\:\\ (?<uri>${URI})\\nVersion\\:\\ (?<version>1)\\nChain\\ ID\\:\\ (?<chainId>[0-9]+)\\nNonce\\:\\ (?<nonce>[a-zA-Z0-9]{8})\\nIssued\\ At\\:\\ (?<issuedAt>${DATETIME})(\\nExpiration\\ Time\\:\\ (?<expirationTime>${DATETIME}))?(\\nNot\\ Before\\:\\ (?<notBefore>${DATETIME}))?(\\nRequest\\ ID\\:\\ (?<requestId>${REQUESTID}))?(\\nResources\\:(?<resources>(\\n-\\ ${URI})+))?$`,
'g'
);
const REGEX = new RegExp(MESSAGE, 'g');

let match = REGEX.exec(msg);
if (!match) {
Expand Down

0 comments on commit bc80efb

Please sign in to comment.