UNPKG

@rize-labs/banana-wallet-sdk-astar-test

Version:

Smart contract wallet sdk package by Banana Wallet

120 lines 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyFingerprint = exports.registerFingerprint = exports.getUserOp = void 0; const base64url = require("./utils/base64url-arraybuffer"); const uuid_1 = require("uuid"); const axios_1 = require("axios"); const routes_1 = require("./routes"); const utils_1 = require("ethers/lib/utils"); const ethers_1 = require("ethers"); function getUserOp(reqId) { const msg1 = Buffer.concat([ Buffer.from("\x19Ethereum Signed Message:\n32", "ascii"), Buffer.from((0, utils_1.arrayify)(reqId)), ]); return msg1; } exports.getUserOp = getUserOp; const registerFingerprint = async () => { const uuid = (0, uuid_1.v4)(); const chanllenge = (0, uuid_1.v4)(); const isPlatformSupported = await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(); const authenticationSupport = isPlatformSupported ? "platform" : "cross-platform"; const publicKeyParams = { challenge: Uint8Array.from(chanllenge, (c) => c.charCodeAt(0)), rp: { name: "Banana Smart Wallet", }, user: { id: Uint8Array.from(uuid, (c) => c.charCodeAt(0)), name: "bananawallet", displayName: "Banana Smart Wallet", }, pubKeyCredParams: [{ type: "public-key", alg: -7 }], authenticatorSelection: { authenticatorAttachment: authenticationSupport, userVerification: "required", }, timeout: 60000, attestation: "none", }; let publicKeyCredential; try { publicKeyCredential = await navigator.credentials.create({ publicKey: publicKeyParams, }); } catch (err) { console.log("algo not supported, trying again", err); // @ts-ignore publicKeyParams.authenticatorSelection.authenticatorAttachment = "cross-platform"; publicKeyCredential = await navigator.credentials.create({ publicKey: publicKeyParams, }); } if (publicKeyCredential === null) { // alert('Failed to get credential') return Promise.reject(new Error("Failed to create credential")); } const response = await (0, axios_1.default)({ url: routes_1.REGISTRATION_LAMBDA_URL, method: "post", params: { aObject: JSON.stringify(Array.from(new Uint8Array(publicKeyCredential.response.attestationObject))), rawId: JSON.stringify(//@ts-ignore Array.from(new Uint8Array(publicKeyCredential?.rawId))), }, }); return { q0: response.data.message.q0hexString, q1: response.data.message.q1hexString, encodedId: response.data.message.encodedId, }; }; exports.registerFingerprint = registerFingerprint; const verifyFingerprint = async (userOp, reqId, encodedId) => { const decodedId = base64url.decode(encodedId); const credential = await navigator.credentials.get({ publicKey: { // Set the WebAuthn credential to use for the assertion allowCredentials: [ { id: decodedId, type: "public-key", }, ], challenge: Uint8Array.from(reqId, (c) => c.charCodeAt(0)).buffer, // Set the required authentication factors userVerification: "required", }, }); if (credential === null) { // alert('Failed to get credential') return Promise.reject(new Error("Failed to get credential")); } //@ts-ignore const response = credential.response; const clientDataJSON = Buffer.from(response.clientDataJSON); const signature = await (0, axios_1.default)({ url: routes_1.VERIFICATION_LAMBDA_URL, method: "post", params: { authDataRaw: JSON.stringify(Array.from(new Uint8Array(response.authenticatorData))), cData: JSON.stringify(Array.from(new Uint8Array(response.clientDataJSON))), signature: JSON.stringify(Array.from(new Uint8Array(response.signature))), }, }); const value = clientDataJSON.toString("hex").slice(72, 248); const clientDataJsonRequestId = ethers_1.ethers.utils.keccak256("0x" + value); userOp.signature = signature.data.message.finalSignature + clientDataJsonRequestId.slice(2); return { newUserOp: userOp, process: signature.data.message.processStatus, }; }; exports.verifyFingerprint = verifyFingerprint; //# sourceMappingURL=WebAuthnContext.js.map