Skip to content

Commit

Permalink
Remove extractPasskeyCoordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed Nov 21, 2024
1 parent c112631 commit 8c9346b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ async function importLibs() {
* @returns {PasskeyCoordinates} An object containing the x and y coordinates of the public key.
* @throws {Error} Throws an error if the key is empty or if the coordinates cannot be extracted.
*/
async function decodePublicKeyForReactNative(publicKey: string): Promise<PasskeyCoordinates> {
export async function decodePublicKeyForReactNative(
publicKey: string
): Promise<PasskeyCoordinates> {
const { p256, AsnParser, ECPublicKey } = await importLibs()

let publicKeyBytes = base64ToUint8Array(publicKey)
Expand Down Expand Up @@ -116,7 +118,7 @@ async function decodePublicKeyForReactNative(publicKey: string): Promise<Passkey
* the x and y coordinates of the public key.
* @throws {Error} Throws an error if the key coordinates cannot be extracted.
*/
async function decodePublicKeyForWeb(publicKey: ArrayBuffer): Promise<PasskeyCoordinates> {
export async function decodePublicKeyForWeb(publicKey: ArrayBuffer): Promise<PasskeyCoordinates> {
const algorithm = {
name: 'ECDSA',
namedCurve: 'P-256',
Expand Down Expand Up @@ -146,7 +148,7 @@ async function decodePublicKeyForWeb(publicKey: ArrayBuffer): Promise<PasskeyCoo
* @returns {PasskeyCoordinates} Object containing the coordinates derived from the public key of the passkey.
* @throws {Error} Throws an error if the coordinates could not be extracted via `p256.ProjectivePoint.fromHex`
*/
async function decodePublicKey(
export async function decodePublicKey(
response: PasskeyAuthenticatorAttestationResponse
): Promise<PasskeyCoordinates> {
const publicKey = response.getPublicKey()
Expand Down
27 changes: 2 additions & 25 deletions packages/protocol-kit/tests/e2e/utils/passkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PasskeyArgType, PasskeyClient } from '@safe-global/protocol-kit'
import { WebAuthnCredentials } from './webauthnShim'
import { WalletClient, keccak256, toBytes, Transport, Chain, Account } from 'viem'
import { asHex } from '@safe-global/protocol-kit/utils/types'
import { PasskeyCoordinates } from '@safe-global/protocol-kit/types'
import { decodePublicKeyForWeb } from '@safe-global/protocol-kit/utils'

let singleInstance: WebAuthnCredentials

Expand All @@ -20,29 +20,6 @@ export function getWebAuthnCredentials() {
return singleInstance
}

async function extractPasskeyCoordinates(publicKey: ArrayBuffer): Promise<PasskeyCoordinates> {
const algorithm = {
name: 'ECDSA',
namedCurve: 'P-256',
hash: { name: 'SHA-256' }
}

const key = await crypto.subtle.importKey('spki', publicKey, algorithm, true, ['verify'])

const { x, y } = await crypto.subtle.exportKey('jwk', key)

const isValidCoordinates = !!x && !!y

if (!isValidCoordinates) {
throw new Error('Failed to generate passkey Coordinates. crypto.subtle.exportKey() failed')
}

return {
x: '0x' + Buffer.from(x, 'base64').toString('hex'),
y: '0x' + Buffer.from(y, 'base64').toString('hex')
}
}

/**
* Deploys the passkey contract for each of the signers.
* @param passkeys An array of PasskeyClient representing the passkeys to deploy.
Expand Down Expand Up @@ -110,7 +87,7 @@ export async function createMockPasskey(

const rawId = Buffer.from(passkeyCredential.rawId).toString('hex')

const coordinates = await extractPasskeyCoordinates(exportedPublicKey)
const coordinates = await decodePublicKeyForWeb(exportedPublicKey)

const passkey: PasskeyArgType = {
rawId,
Expand Down

0 comments on commit 8c9346b

Please sign in to comment.