Skip to content

Commit

Permalink
Replace Object.assign (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
w4ll3 authored Jun 29, 2023
1 parent 97531ef commit 2ae2213
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions packages/siwe/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
VerifyParams,
VerifyParamsKeys,
} from './types';
import { checkContractWalletSignature, generateNonce, checkInvalidKeys, isValidISO8601Date } from './utils';
import {
checkContractWalletSignature,
generateNonce,
checkInvalidKeys,
isValidISO8601Date,
} from './utils';

export class SiweMessage {
/**RFC 4501 dns authority that is requesting the signing. */
Expand Down Expand Up @@ -77,7 +82,18 @@ export class SiweMessage {
this.chainId = parsedMessage.chainId;
this.resources = parsedMessage.resources;
} else {
Object.assign(this, param);
this.domain = param.domain;
this.address = param.address;
this.statement = param?.statement;
this.uri = param.uri;
this.version = param.version;
this.chainId = param.chainId;
this.nonce = param.nonce;
this.issuedAt = param?.issuedAt;
this.expirationTime = param?.expirationTime;
this.notBefore = param?.notBefore;
this.requestId = param?.requestId;
this.resources = param?.resources;
if (typeof this.chainId === 'string') {
this.chainId = parseIntegerNumber(this.chainId);
}
Expand Down Expand Up @@ -202,22 +218,32 @@ export class SiweMessage {
}
};

const invalidParams: Array<keyof VerifyParams> = checkInvalidKeys<VerifyParams>(params, VerifyParamsKeys);
if(invalidParams.length > 0) {
const invalidParams: Array<keyof VerifyParams> =
checkInvalidKeys<VerifyParams>(params, VerifyParamsKeys);
if (invalidParams.length > 0) {
fail({
success: false,
data: this,
error: new Error(`${invalidParams.join(', ')} is/are not valid key(s) for VerifyParams.`),
})
error: new Error(
`${invalidParams.join(
', '
)} is/are not valid key(s) for VerifyParams.`
),
});
}

const invalidOpts: Array<keyof VerifyOpts> = checkInvalidKeys<VerifyOpts>(opts, VerifyOptsKeys);
if(invalidParams.length > 0) {
const invalidOpts: Array<keyof VerifyOpts> = checkInvalidKeys<VerifyOpts>(
opts,
VerifyOptsKeys
);
if (invalidParams.length > 0) {
fail({
success: false,
data: this,
error: new Error(`${invalidOpts.join(', ')} is/are not valid key(s) for VerifyOpts.`),
})
error: new Error(
`${invalidOpts.join(', ')} is/are not valid key(s) for VerifyOpts.`
),
});
}

const { signature, domain, nonce, time } = params;
Expand Down Expand Up @@ -303,7 +329,11 @@ export class SiweMessage {
data: this,
});
} else {
const EIP1271Promise = checkContractWalletSignature(this, signature, opts.provider)
const EIP1271Promise = checkContractWalletSignature(
this,
signature,
opts.provider
)
.then(isValid => {
if (!isValid) {
return {
Expand Down Expand Up @@ -331,7 +361,10 @@ export class SiweMessage {

Promise.all([
EIP1271Promise,
opts?.verificationFallback?.(params, opts, this, EIP1271Promise)?.then(res => res)?.catch((res: SiweResponse) => res)
opts
?.verificationFallback?.(params, opts, this, EIP1271Promise)
?.then(res => res)
?.catch((res: SiweResponse) => res),
]).then(([EIP1271Response, fallbackResponse]) => {
if (fallbackResponse) {
if (fallbackResponse.success) {
Expand All @@ -342,8 +375,7 @@ export class SiweMessage {
} else {
if (EIP1271Response.success) {
return resolve(EIP1271Response);
}
else {
} else {
fail(EIP1271Response);
}
}
Expand Down Expand Up @@ -414,7 +446,7 @@ export class SiweMessage {
}

/** `issuedAt` conforms to ISO-8601 and is a valid date. */
if(this.issuedAt) {
if (this.issuedAt) {
if (!isValidISO8601Date(this.issuedAt)) {
throw new Error(SiweErrorType.INVALID_TIME_FORMAT);
}
Expand Down

0 comments on commit 2ae2213

Please sign in to comment.