-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from zerodevapp/feat/react-native-passkeys-su…
…pport feat: add react-native support for passkeys
- Loading branch information
Showing
21 changed files
with
568 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @zerodev/react-native-passkeys-utils | ||
|
||
## 5.4.2 | ||
|
||
### Patch Changes | ||
|
||
- feat: export parseLoginCred | ||
|
||
## 5.4.1 | ||
|
||
### Patch Changes | ||
|
||
- feat: add parseLoginCred | ||
|
||
## 5.4.0 | ||
|
||
### Patch Changes | ||
|
||
- feat: add react native passkeys support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { signMessageWithReactNativePasskeys } from "./signMessageWithReactNativePasskeys.js" | ||
export { parsePasskeyCred, parseLoginCred } from "./utils.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "@zerodev/react-native-passkeys-utils", | ||
"version": "5.4.2", | ||
"author": "ZeroDev", | ||
"main": "./_cjs/index.js", | ||
"module": "./_esm/index.js", | ||
"types": "./_types/index.d.ts", | ||
"typings": "./_types/index.d.ts", | ||
"sideEffects": false, | ||
"license": "MIT", | ||
"files": [ | ||
"_esm", | ||
"_cjs", | ||
"_types", | ||
"./**/*.ts", | ||
"!_esm/**/*.tsbuildinfo", | ||
"!_cjs/**/*.tsbuildinfo", | ||
"!_types/**/*.tsbuildinfo", | ||
"!.env", | ||
"!./**/*.test.ts", | ||
"!.changeset" | ||
], | ||
"scripts": { | ||
"build": "bun run clean && bun run build:cjs && bun run build:esm && bun run build:types", | ||
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./_cjs/package.json", | ||
"build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./_esm/package.json", | ||
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./_types --emitDeclarationOnly --declaration --declarationMap", | ||
"clean": "rimraf _esm _cjs _types", | ||
"changeset": "changeset", | ||
"changeset:release": "bun run build && changeset publish", | ||
"changeset:version": "changeset version && bun install --lockfile-only", | ||
"format": "biome format . --write", | ||
"lint": "biome check .", | ||
"lint:fix": "bun run lint --apply" | ||
}, | ||
"dependencies": { | ||
"@noble/curves": "^1.3.0", | ||
"@simplewebauthn/types": "^12.0.0", | ||
"asn1js": "^3.0.5", | ||
"react-native-passkeys": "^0.3.1" | ||
}, | ||
"peerDependencies": { | ||
"viem": "^2.21.40", | ||
"@zerodev/webauthn-key": "^5.4.2" | ||
}, | ||
"type": "module" | ||
} |
92 changes: 92 additions & 0 deletions
92
plugins/react-native-passkeys-utils/signMessageWithReactNativePasskeys.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { PublicKeyCredentialRequestOptionsJSON } from "@simplewebauthn/typescript-types" | ||
import * as passkey from "react-native-passkeys" | ||
import { type SignableMessage, encodeAbiParameters } from "viem" | ||
|
||
import { | ||
b64ToBytes, | ||
base64FromUint8Array, | ||
findQuoteIndices, | ||
hexStringToUint8Array, | ||
isRIP7212SupportedNetwork, | ||
parseAndNormalizeSig, | ||
uint8ArrayToHexString | ||
} from "./utils" | ||
|
||
export async function signMessageWithReactNativePasskeys( | ||
message: SignableMessage, | ||
rpID: string, | ||
chainId: number, | ||
allowCredentials?: PublicKeyCredentialRequestOptionsJSON["allowCredentials"] | ||
) { | ||
let messageContent: string | ||
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) { | ||
// convert raw bytes -> hex string (without 0x) | ||
messageContent = Buffer.from(message.raw).toString("hex") | ||
} else { | ||
throw new Error("Unsupported message format") | ||
} | ||
|
||
// remove any 0x prefix for simpler base64 conversion | ||
const formattedMessage = messageContent.startsWith("0x") | ||
? messageContent.slice(2) | ||
: messageContent | ||
|
||
// convert hex -> base64 | ||
const challenge = base64FromUint8Array( | ||
hexStringToUint8Array(formattedMessage), | ||
true | ||
) | ||
|
||
// call react-native-passkeys | ||
const cred = await passkey.get({ | ||
rpId: rpID, | ||
challenge, | ||
allowCredentials, | ||
userVerification: "required" | ||
}) | ||
|
||
if (!cred) { | ||
throw new Error("No passkey credential found") | ||
} | ||
|
||
// get authenticator data | ||
const { authenticatorData } = cred.response | ||
const authenticatorDataHex = uint8ArrayToHexString( | ||
b64ToBytes(authenticatorData) | ||
) | ||
|
||
// parse client data JSON | ||
const clientDataJSON = atob(cred.response.clientDataJSON) | ||
const { beforeType } = findQuoteIndices(clientDataJSON) | ||
|
||
// parse signature r/s | ||
const { signature } = cred.response | ||
const signatureHex = uint8ArrayToHexString(b64ToBytes(signature)) | ||
const { r, s } = parseAndNormalizeSig(signatureHex) | ||
|
||
// encode signature in the format the Passkey Validator expects | ||
const encodedSignature = 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), | ||
isRIP7212SupportedNetwork(chainId) | ||
] | ||
) | ||
|
||
return encodedSignature | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../templates/typescript/tsconfig.build.json", | ||
"include": ["./"], | ||
"exclude": ["./**/*.test.ts", "./**/*.test-d.ts", "./**/*.bench.ts"], | ||
"compilerOptions": { | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"rootDir": "./", | ||
"baseUrl": "./" | ||
} | ||
} |
Oops, something went wrong.