@0xsequence/connect
Version:
Connect package for Sequence Web SDK
42 lines • 2.04 kB
JavaScript
import { ETHAuth, Proof } from '@0xsequence/ethauth';
import { isValidTypedDataSignature } from '@0xsequence/provider';
import { DEFAULT_SESSION_EXPIRATION, LocalStorageKey } from '../constants/index.js';
import { publicClientToProvider, walletClientToSigner } from './adapters.js';
export const signEthAuthProof = async (walletClient, storage) => {
const proofInformation = await storage.getItem(LocalStorageKey.EthAuthProof);
// if proof information was generated and saved upon wallet connection, use that
if (proofInformation) {
return proofInformation;
}
// generate a new proof
const proofSettings = await storage.getItem(LocalStorageKey.EthAuthSettings);
if (!proofSettings) {
throw new Error('No ETHAuth settings found');
}
const walletAddress = walletClient.account.address;
const proof = new Proof();
proof.address = walletAddress;
proof.claims.app = proofSettings.app || 'app';
proof.claims.ogn = proofSettings.origin;
proof.claims.n = proofSettings.nonce;
proof.setExpiryIn(proofSettings.expiry ? Math.max(proofSettings.expiry, 200) : DEFAULT_SESSION_EXPIRATION);
const typedData = proof.messageTypedData();
const signer = await walletClientToSigner(walletClient);
const signature = await signer.signTypedData(typedData.domain, typedData.types, typedData.message);
proof.signature = signature;
const ethAuth = new ETHAuth();
const proofString = await ethAuth.encodeProof(proof, true);
return {
typedData,
proofString
};
};
export const validateEthProof = async (walletClient, publicClient, proof) => {
const walletAddress = walletClient.account.address;
const ethAuth = new ETHAuth();
const decodedProof = await ethAuth.decodeProof(proof.proofString, true);
const provider = publicClientToProvider(publicClient);
const isValid = await isValidTypedDataSignature(walletAddress, proof.typedData, decodedProof.signature, provider);
return isValid;
};
//# sourceMappingURL=ethAuth.js.map