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

check timestamps of BLS session sigs #512

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AuthSig } from '@lit-protocol/types';
import { uint8arrayToString } from '@lit-protocol/uint8arrays';
import { ethers } from 'ethers';
import { SiweError, SiweErrorType, SiweMessage } from 'siwe';

const LIT_SESSION_SIGNED_MESSAGE_PREFIX = 'lit_session:';

Expand All @@ -18,7 +19,8 @@ export const blsSessionSigVerify = (
// TODO: refactor type with merger of PR 'https://github.com/LIT-Protocol/js-sdk/pull/503`
verifier: (public_key: any, message: any, signature: any) => void,
networkPubKey: string,
authSig: AuthSig
authSig: AuthSig,
authSigSiweMessage: SiweMessage
): void => {
let sigJson = JSON.parse(authSig.sig);
// we do not nessesarly need to use ethers here but was a quick way
Expand All @@ -32,6 +34,34 @@ export const blsSessionSigVerify = (
);
const signatureBytes = Buffer.from(sigJson.ProofOfPossession, `hex`);

/** Check time or now */
const checkTime = new Date();

if (!authSigSiweMessage.expirationTime || !authSigSiweMessage.notBefore) {
throw new Error(
'Invalid SIWE message. Missing expirationTime or notBefore.'
);
}

// check timestamp of SIWE
const expirationDate = new Date(authSigSiweMessage.expirationTime);
if (checkTime.getTime() >= expirationDate.getTime()) {
throw new SiweError(
SiweErrorType.EXPIRED_MESSAGE,
`${checkTime.toISOString()} < ${expirationDate.toISOString()}`,
`${checkTime.toISOString()} >= ${expirationDate.toISOString()}`
);
}

const notBefore = new Date(authSigSiweMessage.notBefore);
if (checkTime.getTime() < notBefore.getTime()) {
throw new SiweError(
SiweErrorType.NOT_YET_VALID_MESSAGE,
`${checkTime.toISOString()} >= ${notBefore.toISOString()}`,
`${checkTime.toISOString()} < ${notBefore.toISOString()}`
);
}
MaximusHaximus marked this conversation as resolved.
Show resolved Hide resolved

verifier(
networkPubKey,
shaHashed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ export class LitNodeClientNodeJs
// it will fail. If the algo is not defined we can assume that it was an EOA wallet signing the message so we can use SIWE.
if (authSig.algo === `ed25519` || authSig.algo === undefined) {
try {
await authSigSiweMessage.validate(authSig.sig);
await authSigSiweMessage.verify(authSig.sig);
} catch (e) {
log(`Error while verifying ECDSA signature: `, e);
return true;
Expand All @@ -528,7 +528,8 @@ export class LitNodeClientNodeJs
blsSessionSigVerify(
blsSdk.verify_signature,
this.networkPubKey!,
authSig
authSig,
authSigSiweMessage
);
} catch (e) {
log(`Error while verifying bls signature: `, e);
Expand Down
Loading