@biconomy/passkey
Version:
Passkey validator plug in for Biconomy SDK
132 lines • 5.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toPasskeyValidator = toPasskeyValidator;
const sdk_1 = require("@biconomy/sdk");
const browser_1 = require("@simplewebauthn/browser");
const viem_1 = require("viem");
const constants_1 = require("./constants.js");
const utils_1 = require("./utils.js");
const signMessageUsingWebAuthn = async (message, chainId, allowCredentials) => {
let messageContent;
if (typeof message === "string") {
messageContent = message;
}
else if ("raw" in message && typeof message.raw === "string") {
messageContent = message.raw;
}
else if ("raw" in message && message.raw instanceof Uint8Array) {
messageContent = message.raw.toString();
}
else {
throw new Error("Unsupported message format");
}
const formattedMessage = messageContent.startsWith("0x")
? messageContent.slice(2)
: messageContent;
const challenge = (0, utils_1.base64FromUint8Array)((0, utils_1.hexStringToUint8Array)(formattedMessage), true);
const assertionOptions = {
challenge,
allowCredentials,
userVerification: "required"
};
const cred = await (0, browser_1.startAuthentication)(assertionOptions);
const { authenticatorData } = cred.response;
const authenticatorDataHex = (0, utils_1.uint8ArrayToHexString)((0, utils_1.b64ToBytes)(authenticatorData));
const clientDataJSON = atob(cred.response.clientDataJSON);
const { beforeType } = (0, utils_1.findQuoteIndices)(clientDataJSON);
const { signature } = cred.response;
const signatureHex = (0, utils_1.uint8ArrayToHexString)((0, utils_1.b64ToBytes)(signature));
const { r, s } = (0, utils_1.parseAndNormalizeSig)(signatureHex);
const encodedSignature = (0, viem_1.encodeAbiParameters)([
{ name: "authenticatorData", type: "bytes" },
{ name: "clientDataJSON", type: "string" },
{ name: "responseTypeLocation", type: "uint256" },
{ name: "r", type: "uint256" },
{ name: "s", type: "uint256" },
{ name: "usePrecompiled", type: "bool" }
], [
authenticatorDataHex,
clientDataJSON,
beforeType,
BigInt(r),
BigInt(s),
(0, utils_1.isRIP7212SupportedNetwork)(chainId)
]);
return encodedSignature;
};
async function toPasskeyValidator({ webAuthnKey, account }) {
return (0, sdk_1.toModule)({
signer: account.signer,
address: constants_1.PASSKEY_VALIDATOR_ADDRESS,
accountAddress: account.address,
signUserOpHash: async (userOpHash) => {
if (!account.client?.chain) {
throw new Error("Chain ID is required but not found in account client");
}
return signMessageUsingWebAuthn(userOpHash, account.client.chain.id, [{ id: webAuthnKey.authenticatorId, type: "public-key" }]);
},
initData: (0, viem_1.encodeAbiParameters)([
{
components: [
{ name: "x", type: "uint256" },
{ name: "y", type: "uint256" }
],
name: "webAuthnData",
type: "tuple"
},
{
name: "authenticatorIdHash",
type: "bytes32"
}
], [
{
x: webAuthnKey.pubX,
y: webAuthnKey.pubY
},
webAuthnKey.authenticatorIdHash
]),
moduleInitData: {
initData: (0, viem_1.encodeAbiParameters)([
{
components: [
{ name: "x", type: "uint256" },
{ name: "y", type: "uint256" }
],
name: "webAuthnData",
type: "tuple"
},
{
name: "authenticatorIdHash",
type: "bytes32"
}
], [
{
x: webAuthnKey.pubX,
y: webAuthnKey.pubY
},
webAuthnKey.authenticatorIdHash
]),
address: constants_1.PASSKEY_VALIDATOR_ADDRESS,
type: "validator"
},
deInitData: "0x",
async getStubSignature() {
return (0, viem_1.encodeAbiParameters)([
{ name: "authenticatorData", type: "bytes" },
{ name: "clientDataJSON", type: "string" },
{ name: "responseTypeLocation", type: "uint256" },
{ name: "r", type: "uint256" },
{ name: "s", type: "uint256" },
{ name: "usePrecompiled", type: "bool" }
], [
"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97631d00000000",
'{"type":"webauthn.get","challenge":"tbxXNFS9X_4Byr1cMwqKrIGB-_30a0QhZ6y7ucM0BOE","origin":"http://localhost:3000","crossOrigin":false, "other_keys_can_be_added_here":"do not compare clientDataJSON against a template. See https://goo.gl/yabPex"}',
1n,
44941127272049826721201904734628716258498742255959991581049806490182030242267n,
9910254599581058084911561569808925251374718953855182016200087235935345969636n,
false
]);
}
});
}
//# sourceMappingURL=toPasskeyValidator.js.map