Skip to content

Commit

Permalink
Use an int for the chain ID (spruceid#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbihel authored Feb 4, 2022
1 parent 66d91f6 commit b1905b3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
6 changes: 3 additions & 3 deletions lib/abnf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ParsedMessage {
statement: string | null;
uri: string;
version: string;
chainId: string;
chainId: number;
nonce: string;
issuedAt: string;
expirationTime: string | null;
Expand Down Expand Up @@ -267,11 +267,11 @@ export class ParsedMessage {
) {
const ret = id.SEM_OK;
if (state === id.SEM_PRE) {
data.chainId = apgLib.utils.charsToString(
data.chainId = parseInt(apgLib.utils.charsToString(
chars,
phraseIndex,
phraseLength
);
));
}
return ret;
};
Expand Down
8 changes: 5 additions & 3 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SiweMessage {
version: string;
/**EIP-155 Chain ID to which the session is bound, and the network where
* Contract Accounts must be resolved. */
chainId: string;
chainId: number;
/**Randomized token used to prevent replay attacks, at least 8 alphanumeric
* characters. */
nonce: string;
Expand Down Expand Up @@ -100,6 +100,9 @@ export class SiweMessage {
this.resources = parsedMessage.resources;
} else {
Object.assign(this, param);
if (typeof this.chainId === 'string') {
this.chainId = parseInt(this.chainId)
}
}
}

Expand Down Expand Up @@ -242,8 +245,7 @@ export class SiweMessage {
}
if (missing.length > 0) {
throw new Error(
`${
ErrorTypes.MALFORMED_SESSION
`${ErrorTypes.MALFORMED_SESSION
} missing: ${missing.join(', ')}.`
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ParsedMessage {
statement: string | null;
uri: string;
version: string;
chainId: string;
chainId: number;
nonce: string;
issuedAt: string;
expirationTime: string | null;
Expand All @@ -46,7 +46,7 @@ export class ParsedMessage {
this.uri = match?.groups?.uri;
this.version = match?.groups?.version;
this.nonce = match?.groups?.nonce;
this.chainId = match?.groups?.chainId;
this.chainId = parseInt(match?.groups?.chainId);
this.issuedAt = match?.groups?.issuedAt;
this.expirationTime = match?.groups?.expirationTime;
this.notBefore = match?.groups?.notBefore;
Expand Down
16 changes: 8 additions & 8 deletions test/parsing_positive.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z",
"resources": ["ipfs://Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu", "https://example.com/my-web2-claim.json"]
Expand All @@ -21,7 +21,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z"
}
Expand All @@ -34,7 +34,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24Z"
}
Expand All @@ -47,7 +47,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z"
}
Expand All @@ -60,7 +60,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z"
}
Expand All @@ -73,7 +73,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z"
}
Expand All @@ -86,7 +86,7 @@
"statement": "I accept the ServiceOrg Terms of Service: https://service.org/tos",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z"
}
Expand All @@ -98,7 +98,7 @@
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"uri": "https://service.org/login",
"version": "1",
"chainId": "1",
"chainId": 1,
"nonce": "32891757",
"issuedAt": "2021-09-30T16:25:24.000Z"
}
Expand Down
8 changes: 4 additions & 4 deletions test/validation_negative.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"version": "1",
"nonce": "lx2nx4so",
"issuedAt": "2022-01-05T14:27:30.883Z",
"chainId": "1",
"chainId": 1,
"expirationTime": "2021-01-05T00:00:00Z",
"signature": "0x5e6834e82ec12532e3954882610b26ef83c16d25a38caccc6a009a488b6ad1237318ec6cd2fd83c19f49d0cb0848c70e9a5a4bf550ce5d69bc1b023b9f6b7f601b"
},
Expand All @@ -19,7 +19,7 @@
"version": "1",
"nonce": "rmplqh1gf",
"issuedAt": "2022-01-05T14:31:43.954Z",
"chainId": "1",
"chainId": 1,
"expirationTime": "2022-01-07T14:31:43.952Z",
"signature": "0xf2e8420fc1b722bf4941f5a0464f98172a758ceda5039f622e425fb69fd19b20e444bba7c9a8a8d7e2b5e453553efe7c9460be5d211abe473fc146d51bb04d0cb1b"
},
Expand All @@ -31,7 +31,7 @@
"version": "1",
"nonce": "rmplqh1gf",
"issuedAt": "2022-01-05T14:31:43.954Z",
"chainId": "1",
"chainId": 1,
"expirationTime": "2022-01-07T14:31:43.952Z",
"signature": "0x31df81dc02344c9156e6f71da46e2db624b38f8f806290d670d46492b834b2e7575cbce9f48169356cfb577b910d8e30732fcf23c1ac0021d08b945ed7ee118e1b"
},
Expand All @@ -43,7 +43,7 @@
"version": "1",
"nonce": "o8zxjgmp",
"issuedAt": "2022-01-05T14:50:55.688Z",
"chainId": "1",
"chainId": 1,
"expirationTime": "2020-02-32T00:00:00.000Z",
"signature": "0x8b457a36dad94cb9c07cfcad08664c988d795b35762f1316438b9590c27f4a5e028e923066a49908c88a1e2fba299c1e6d6d8206181339343e0ef53be01d078f1c"
}
Expand Down
2 changes: 1 addition & 1 deletion test/validation_positive.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"version": "1",
"nonce": "bTyXgcQxn2htgkjJn",
"issuedAt": "2022-01-27T17:09:38.578Z",
"chainId": "1",
"chainId": 1,
"expirationTime": "2100-01-07T14:31:43.952Z",
"signature": "0xdc35c7f8ba2720df052e0092556456127f00f7707eaa8e3bbff7e56774e7f2e05a093cfc9e02964c33d86e8e066e221b7d153d27e5a2e97ccd5ca7d3f2ce06cb1b"
}
Expand Down

0 comments on commit b1905b3

Please sign in to comment.