import { getKeypair } from '@base-org/account'; const existingKeyPair = await getKeypair(); if (existingKeyPair) { console.log('Found existing key pair'); } else { console.log('No existing key pair found'); }
{ publicKey: "0x04a1b2c3d4e5f6...", privateKey: "0x1a2b3c4d5e6f7a..." }
Retrieve an existing P256 key pair from storage
null
Show P256KeyPair properties
import { getKeypair, generateKeyPair } from '@base-org/account'; async function getOrCreateKeyPair() { // Try to get existing key pair first let keyPair = await getKeypair(); if (!keyPair) { // Generate new key pair if none exists console.log('No existing key pair, generating new one...'); keyPair = await generateKeyPair(); } else { console.log('Using existing key pair'); } return keyPair; }
getKeypair
try { const keyPair = await getKeypair(); if (keyPair) { // Use existing keys } else { // No keys found, may need to generate new ones } } catch (error) { console.error('Error accessing key storage:', error); // Handle storage access errors }
class KeyManager { private keyPair: P256KeyPair | null = null; async initialize() { try { // Load existing keys this.keyPair = await getKeypair(); if (this.keyPair) { console.log('Loaded existing key pair'); } else { console.log('No stored keys found'); } return !!this.keyPair; } catch (error) { console.error('Failed to initialize key manager:', error); return false; } } hasKeys(): boolean { return !!this.keyPair; } async ensureKeys(): Promise<P256KeyPair> { if (!this.keyPair) { console.log('Generating new key pair...'); this.keyPair = await generateKeyPair(); } return this.keyPair; } getPublicKey(): string | null { return this.keyPair?.publicKey || null; } }
Was this page helpful?