Skip to content

Commit

Permalink
Remove types
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed Nov 21, 2024
1 parent 8c9346b commit 0638fa2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 69 deletions.
10 changes: 3 additions & 7 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ import {
SwapOwnerTxParams,
SafeModulesPaginated,
RemovePasskeyOwnerTxParams,
PasskeyArgType,
PasskeyCredential,
PasskeyAuthenticatorAttestationResponse
PasskeyArgType
} from './types'
import {
EthSafeSignature,
Expand Down Expand Up @@ -1705,12 +1703,10 @@ class Safe {

/**
* This method creates a signer to be used with the init method
* @param {PasskeyCredential<PasskeyAuthenticatorAttestationResponse>} credential - The credential to be used to create the signer. Can be generated in the web with navigator.credentials.create
* @param {Credential} credential - The credential to be used to create the signer. Can be generated in the web with navigator.credentials.create
* @returns {PasskeyArgType} - The signer to be used with the init method
*/
static createPasskeySigner = async (
credential: PasskeyCredential<PasskeyAuthenticatorAttestationResponse>
): Promise<PasskeyArgType> => {
static createPasskeySigner = async (credential: Credential): Promise<PasskeyArgType> => {
return extractPasskeyData(credential)
}
}
Expand Down
41 changes: 1 addition & 40 deletions packages/protocol-kit/src/types/passkeys.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,9 @@
export type PasskeyCredentialRequestOptions = {
publicKey: PasskeyPublicKeyCredentialRequestOptions
}

export interface PasskeyPublicKeyCredentialRequestOptions {
challenge: Uint8Array
rpId?: string
allowCredentials: {
type: 'public-key'
id: Uint8Array
}[]
userVerification?: Exclude<PasskeyUserVerificationRequirement, 'discouraged'>
attestation?: 'none'
}

export type PasskeyUserVerificationRequirement = 'required' | 'preferred' | 'discouraged'

export type PasskeyCredential<T> = {
id: string
type: 'public-key'
rawId: ArrayBuffer
response: T
}

export type PasskeyAuthenticatorAttestationResponse = {
clientDataJSON: ArrayBuffer
attestationObject: ArrayBuffer
getPublicKey(): ArrayBuffer | string
}

export interface PasskeyAuthenticatorAssertionResponse {
clientDataJSON: ArrayBuffer
authenticatorData: ArrayBuffer
signature: ArrayBuffer
userHandle: ArrayBuffer
}

export type PasskeyCoordinates = {
x: string
y: string
}

export type GetPasskeyCredentialFn = (
options?: PasskeyCredentialRequestOptions
) => Promise<PasskeyCredential<PasskeyAuthenticatorAssertionResponse>>
export type GetPasskeyCredentialFn = (options?: CredentialRequestOptions) => Promise<Credential>

export type PasskeyArgType = {
rawId: string // required to sign data
Expand Down
12 changes: 6 additions & 6 deletions packages/protocol-kit/src/utils/passkeys/PasskeyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import {
PasskeyClient,
SafeWebAuthnSignerFactoryContractImplementationType,
SafeWebAuthnSharedSignerContractImplementationType,
GetPasskeyCredentialFn,
PasskeyCredential,
PasskeyAuthenticatorAssertionResponse
GetPasskeyCredentialFn
} from '@safe-global/protocol-kit/types'
import { getDefaultFCLP256VerifierAddress } from './extractPasskeyData'
import { asHex } from '../types'
Expand All @@ -48,13 +46,15 @@ const sign = async (
allowCredentials: [{ type: 'public-key', id: passkeyRawId }],
userVerification: 'required'
}
})) as PasskeyCredential<PasskeyAuthenticatorAssertionResponse>
})) as PublicKeyCredential

if (!assertion?.response?.authenticatorData) {
const assertionResponse = assertion.response as AuthenticatorAssertionResponse

if (!assertionResponse?.authenticatorData) {
throw new Error('Failed to sign data with passkey Signer')
}

const { authenticatorData, signature, clientDataJSON } = assertion.response
const { authenticatorData, signature, clientDataJSON } = assertionResponse

return encodeAbiParameters(parseAbiParameters('bytes, bytes, uint256[2]'), [
toHex(new Uint8Array(authenticatorData)),
Expand Down
26 changes: 11 additions & 15 deletions packages/protocol-kit/src/utils/passkeys/extractPasskeyData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Buffer } from 'buffer'
import { getFCLP256VerifierDeployment } from '@safe-global/safe-modules-deployments'
import {
PasskeyArgType,
PasskeyAuthenticatorAttestationResponse,
PasskeyCoordinates,
PasskeyCredential
} from '@safe-global/protocol-kit/types'
import { PasskeyArgType, PasskeyCoordinates } from '@safe-global/protocol-kit/types'

/**
* Converts a Base64 URL-encoded string to a Uint8Array.
Expand Down Expand Up @@ -144,14 +139,15 @@ export async function decodePublicKeyForWeb(publicKey: ArrayBuffer): Promise<Pas
/**
* Decodes the x and y coordinates of the public key from a created public key credential response.
*
* @param {Pick<AuthenticatorAttestationResponse, 'attestationObject'>} response
* @param {AuthenticatorResponse} response
* @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`
*/
export async function decodePublicKey(
response: PasskeyAuthenticatorAttestationResponse
response: AuthenticatorResponse
): Promise<PasskeyCoordinates> {
const publicKey = response.getPublicKey()
const publicKeyAuthenticatorResponse = response as AuthenticatorAttestationResponse
const publicKey = publicKeyAuthenticatorResponse.getPublicKey()

if (!publicKey) {
throw new Error('Failed to generate passkey coordinates. getPublicKey() failed')
Expand All @@ -175,17 +171,17 @@ export async function decodePublicKey(
/**
* Extracts and returns the passkey data (coordinates and rawId) from a given passkey Credential.
*
* @param {PasskeyCredential<PasskeyAuthenticatorAttestationResponse>} passkeyCredential - The passkey credential generated via `navigator.credentials.create()` or other method in another platforms.
* @param {Credential} passkeyCredential - The passkey credential generated via `navigator.credentials.create()` or other method in another platforms.
* @returns {Promise<PasskeyArgType>} A promise that resolves to an object containing the coordinates and the rawId derived from the passkey.
* This is the important information in the Safe account context and should be stored securely as it is used to verify the passkey and to instantiate the SDK
* as a signer (`Safe.init())
* @throws {Error} Throws an error if the coordinates could not be extracted
*/
export async function extractPasskeyData(
passkeyCredential: PasskeyCredential<PasskeyAuthenticatorAttestationResponse>
): Promise<PasskeyArgType> {
const rawId = Buffer.from(passkeyCredential.rawId).toString('hex')
const coordinates = await decodePublicKey(passkeyCredential.response)
export async function extractPasskeyData(passkeyCredential: Credential): Promise<PasskeyArgType> {
const passkeyPublicKeyCredential = passkeyCredential as PublicKeyCredential

const rawId = Buffer.from(passkeyPublicKeyCredential.rawId).toString('hex')
const coordinates = await decodePublicKey(passkeyPublicKeyCredential.response)

return {
rawId,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-starter-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"@safe-global/api-kit": "^2.5.4",
"@safe-global/protocol-kit": "^5.0.4",
"@safe-global/protocol-kit": "file:.yalc/@safe-global/protocol-kit",
"@safe-global/relay-kit": "^3.2.4",
"@safe-global/types-kit": "^1.0.0",
"viem": "^2.21.8"
Expand Down

0 comments on commit 0638fa2

Please sign in to comment.