UNPKG

@0xsequence/connect

Version:
47 lines 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateEthProof = exports.signEthAuthProof = void 0; const ethauth_1 = require("@0xsequence/ethauth"); const provider_1 = require("@0xsequence/provider"); const index_js_1 = require("../constants/index.js"); const adapters_js_1 = require("./adapters.js"); const signEthAuthProof = async (walletClient, storage) => { const proofInformation = await storage.getItem(index_js_1.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(index_js_1.LocalStorageKey.EthAuthSettings); if (!proofSettings) { throw new Error('No ETHAuth settings found'); } const walletAddress = walletClient.account.address; const proof = new ethauth_1.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) : index_js_1.DEFAULT_SESSION_EXPIRATION); const typedData = proof.messageTypedData(); const signer = await (0, adapters_js_1.walletClientToSigner)(walletClient); const signature = await signer.signTypedData(typedData.domain, typedData.types, typedData.message); proof.signature = signature; const ethAuth = new ethauth_1.ETHAuth(); const proofString = await ethAuth.encodeProof(proof, true); return { typedData, proofString }; }; exports.signEthAuthProof = signEthAuthProof; const validateEthProof = async (walletClient, publicClient, proof) => { const walletAddress = walletClient.account.address; const ethAuth = new ethauth_1.ETHAuth(); const decodedProof = await ethAuth.decodeProof(proof.proofString, true); const provider = (0, adapters_js_1.publicClientToProvider)(publicClient); const isValid = await (0, provider_1.isValidTypedDataSignature)(walletAddress, proof.typedData, decodedProof.signature, provider); return isValid; }; exports.validateEthProof = validateEthProof; //# sourceMappingURL=ethAuth.js.map