safe-global / safe-core-sdk

The Safe{Core} SDK allows builders to add account abstraction functionality into their apps.
https://docs.safe.global/sdk/overview
MIT License
242 stars 177 forks source link

Update passkey type #859

Closed DaniSomoza closed 1 week ago

DaniSomoza commented 2 weeks ago

What it solves

Updated the PasskeyArgType type from:

export type PasskeyArgType = {
  rawId: ArrayBuffer // required to sign data
  publicKey: ArrayBuffer // required to generate X & Y Coordinates
}

to:

export type PasskeyArgType = {
  rawId: string // required to sign data
  coordinates: PasskeyCoordinates // required to sign data
  customVerifierAddress?: string // optional
}

Now create and store a passkey is more simple:

import { PasskeyArgType, extractPasskeyData } from "@safe-global/protocol-kit";

  // Generate a passkey credential using WebAuthn API
  const passkeyCredential = await navigator.credentials.create({
    publicKey: {
      pubKeyCredParams: [
        {
          // ECDSA w/ SHA-256: https://datatracker.ietf.org/doc/html/rfc8152#section-8.1
          alg: -7,
          type: "public-key",
        },
      ],
      challenge: crypto.getRandomValues(new Uint8Array(32)),
      rp: {
        name: "Safe SmartAccount",
      },
      user: {
        displayName: label,
        id: crypto.getRandomValues(new Uint8Array(32)),
        name: label,
      },
      timeout: 60_000,
      attestation: "none",
    },
  });

  if (!passkeyCredential) {
    throw Error("Passkey creation failed: No credential was returned.");
  }

  const passkey = await extractPasskeyData(passkeyCredential);

  console.log("new passkey created: ", passkey);