@reown/appkit-siwx
Version:
The chain agnostic tool to enable authentication for AppKit applications.
87 lines • 3.35 kB
JavaScript
/**
* This is the base class for a SIWX messenger.
* It provides the basic structure for creating a SIWX message and util functions.
*/
export class SIWXMessenger {
constructor(params) {
this.expiration = params.expiration;
this.getNonce = params.getNonce;
this.getRequestId = params.getRequestId;
this.domain = params.domain;
this.uri = params.uri;
this.statement = params.statement;
this.resources = params.resources;
}
/**
* This is the default method to create a SIWXMessage object.
* It will call the `getNonce`, `getRequestId`, `getExpirationTime`, `getIssuedAt`, and `getNotBefore` methods.
* It appends the `stringify` method to the object as `toString`.
*
* @param input SIWXMessage.Input
* @returns
*/
async createMessage(input) {
const params = {
accountAddress: input.accountAddress,
chainId: input.chainId,
version: this.version,
domain: this.domain,
uri: this.uri,
statement: this.statement,
resources: this.resources,
nonce: await this.getNonce(input),
requestId: await this.getRequestId?.(),
expirationTime: this.getExpirationTime(input),
issuedAt: this.getIssuedAt(),
notBefore: this.getNotBefore(input)
};
const methods = {
toString: () => this.stringify(params)
};
return Object.assign(params, methods);
}
/**
* This method generates the expiration time based on the `notBefore` value and the `expiration` value.
* You may override this method to provide a custom expiration time.
*
* @param Pick<SIWXMessage.Input, 'notBefore'>
* @returns string | undefined - The expiration time in `stringifyDate` format or undefined if not available
*/
getExpirationTime({ notBefore }) {
if (typeof this.expiration === 'undefined') {
return undefined;
}
const startingAt = notBefore ? new Date(notBefore).getTime() : Date.now();
return this.stringifyDate(new Date(startingAt + this.expiration));
}
/**
* This method generates the `notBefore` formatted value based on the `notBefore` input.
* You may override this method to provide a custom `notBefore` value.
*
* @param Pick<SIWXMessage.Input, 'notBefore'>
* @returns string | undefined - The `notBefore` value in `stringifyDate` format or undefined if not available
*/
getNotBefore({ notBefore }) {
return notBefore ? this.stringifyDate(new Date(notBefore)) : undefined;
}
/**
* This method generates the `issuedAt` formatted value based on the current date.
* You may override this method to provide a custom `issuedAt` value.
*
* @returns string - The `issuedAt` value in `stringifyDate` format
*/
getIssuedAt() {
return this.stringifyDate(new Date());
}
/**
* This method converts a date object to a formatted string.
* You may override this method to provide a custom date format.
*
* @param date Date
* @returns string - The date in ISO format
*/
stringifyDate(date) {
return date.toISOString();
}
}
//# sourceMappingURL=SIWXMessenger.js.map