forked from spruceid/siwe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.ts
47 lines (44 loc) · 1.98 KB
/
regex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const DOMAIN = '(([a-zA-Z]([a-zA-Z\\d-]*[a-zA-Z\\d])*)\\.)+[a-zA-Z]{2,}';
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]*";
export class ParsedMessage {
domain: string;
address: string;
statement: string;
uri: string;
version: string;
nonce: string;
issuedAt: string;
expirationTime: string | null;
notBefore: string | null;
requestId: string | null;
chainId: string | null;
resources: string[] | null;
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)\\nNonce\\:\\ (?<nonce>[a-zA-Z0-9]{8})\\nIssued\\ At\\:\\ (?<issuedAt>${DATETIME})(\\nExpiration\\ Time\\:\\ (?<expirationTime>${DATETIME}))?(\\nNot\\ Before\\:\\ (?<notBefore>${DATETIME}))?(\\nRequest\\ ID\\:\\ (?<requestId>${REQUESTID}))?(\\nChain\\ ID\\:\\ (?<chainId>[0-9]+))?(\\nResources\\:(?<resources>(\\n-\\ ${URI})+))?$`,
'g'
);
let match = REGEX.exec(msg);
if (!match) {
throw new Error('Message did not match the regular expression.');
}
this.match = match;
this.domain = match?.groups?.domain;
this.address = match?.groups?.address;
this.statement = match?.groups?.statement;
this.uri = match?.groups?.uri;
this.version = match?.groups?.version;
this.nonce = match?.groups?.nonce;
this.chainId = match?.groups?.chainId;
this.issuedAt = match?.groups?.issuedAt;
this.expirationTime = match?.groups?.expirationTime;
this.notBefore = match?.groups?.notBefore;
this.requestId = match?.groups?.requestId;
this.resources = match?.groups?.resources?.split('\n- ').slice(1);
}
}