50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
export class SiweMessage {
|
|
constructor(param) {
|
|
Object.assign(this, param);
|
|
}
|
|
|
|
toMessage() {
|
|
const header = `${this.domain} wants you to sign in with your Ethereum account:`;
|
|
const uriField = `URI: ${this.uri}`;
|
|
let prefix = [header, this.address].join('\n');
|
|
const versionField = `Version: ${this.version}`;
|
|
|
|
const chainField = `Chain ID: ` + this.chainId || '1';
|
|
|
|
const nonceField = `Nonce: ${this.nonce}`;
|
|
|
|
const suffixArray = [uriField, versionField, chainField, nonceField];
|
|
|
|
this.issuedAt = this.issuedAt || new Date().toISOString();
|
|
|
|
suffixArray.push(`Issued At: ${this.issuedAt}`);
|
|
|
|
if (this.expirationTime) {
|
|
const expiryField = `Expiration Time: ${this.expirationTime}`;
|
|
|
|
suffixArray.push(expiryField);
|
|
}
|
|
|
|
if (this.notBefore) {
|
|
suffixArray.push(`Not Before: ${this.notBefore}`);
|
|
}
|
|
|
|
if (this.requestId) {
|
|
suffixArray.push(`Request ID: ${this.requestId}`);
|
|
}
|
|
|
|
if (this.resources) {
|
|
suffixArray.push(
|
|
[`Resources:`, ...this.resources.map(x => `- ${x}`)].join('\n')
|
|
);
|
|
}
|
|
|
|
const suffix = suffixArray.join('\n');
|
|
prefix = [prefix, this.statement].join('\n\n');
|
|
if (this.statement) {
|
|
prefix += '\n';
|
|
}
|
|
return [prefix, suffix].join('\n');
|
|
}
|
|
}
|