Skip to content

Commit

Permalink
fix: webauthn base64 url encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanRaj1608 committed Oct 3, 2024
1 parent ce480c9 commit f42778d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
6 changes: 2 additions & 4 deletions contracts/smart-account/modules/PasskeyRegistryModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.23;

import {BaseAuthorizationModule} from "./BaseAuthorizationModule.sol";
import {UserOperation} from "@account-abstraction/contracts/interfaces/UserOperation.sol";
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
import {Base64} from "solady/utils/Base64.sol";
import {Secp256r1, PassKeyId} from "./PasskeyValidationModules/Secp256r1.sol";
import {EIP1271_MAGIC_VALUE} from "contracts/smart-account/interfaces/ISignatureValidator.sol";
import {IPasskeyRegistryModule} from "../interfaces/modules/IPasskeyRegistryModule.sol";
Expand Down Expand Up @@ -163,9 +163,7 @@ contract PasskeyRegistryModule is
(bytes32, uint256, uint256, bytes, string, string)
);
(keyHash);
string memory opHashBase64 = Base64.encode(
bytes.concat(userOpDataHash)
);
string memory opHashBase64 = Base64.encode(abi.encodePacked(userOpDataHash), true, true);
string memory clientDataJSON = string.concat(
clientDataJSONPre,
opHashBase64,
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@
"@ethersproject/constants": "^5.6.1",
"@types/elliptic": "^6.4.18",
"axios": "^1.4.0",
"ethereumjs-util": "^7.1.0",
"hardhat-tracer": "^2.7.0",
"bn.js": "^5.2.1",
"dotenv": "^16.0.3",
"elliptic": "^6.5.4"
"elliptic": "^6.5.4",
"ethereumjs-util": "^7.1.0",
"hardhat-tracer": "^2.7.0",
"solady": "^0.0.248"
}
}
12 changes: 10 additions & 2 deletions test/module/PasskeyFlow.module.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ describe("Passkeys Registry Module:", function () {
const userOpHash = getUserOpHash(userOp, entryPoint.address, chainId);
const clientDataJSONPre = '{"type":"webauthn.get","challenge":"';
const clientDataJSONPost =
'","origin":"https://webauthn.me","crossOrigin":false}';
'","origin":"http://localhost:3000","crossOrigin":false}';

const userOpDataHashBuffer = Buffer.from(ethers.utils.arrayify(userOpHash));
const opHashBase64 = userOpDataHashBuffer.toString("base64");
let opHashBase64 = userOpDataHashBuffer.toString("base64");
opHashBase64 = convertBase64Url(opHashBase64);

const clientDataJSON = `${clientDataJSONPre}${opHashBase64}${clientDataJSONPost}`;

Expand All @@ -132,6 +133,9 @@ describe("Passkeys Registry Module:", function () {
const sigHash = ethers.utils.sha256(
ethers.utils.hexlify(concatenatedBuffer)
);

// console.log("sigHash", sigHash);

const bn = new BN(BigNumber.from(sigHash).toString());
const sign = keyPair.sign(bn);

Expand Down Expand Up @@ -164,3 +168,7 @@ describe("Passkeys Registry Module:", function () {
);
});
});

const convertBase64Url = (input: string): string => {
return input.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5615,6 +5615,11 @@ slice-ansi@^4.0.0:
astral-regex "^2.0.0"
is-fullwidth-code-point "^3.0.0"

solady@^0.0.248:
version "0.0.248"
resolved "https://registry.yarnpkg.com/solady/-/solady-0.0.248.tgz#19d02509d490a95320e416ec6a2b36d980e57bcf"
integrity sha512-GoiB6UuhM4Co56uBwxvzFLyDEwkMrTMkDDCA4Ft53sv1a7PyW0pj69JXqE6iExswV2aJfCF5OFs61gCEGkX3aw==

[email protected]:
version "0.7.3"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.7.3.tgz#04646961bd867a744f63d2b4e3c0701ffdc7d78a"
Expand Down

0 comments on commit f42778d

Please sign in to comment.