UNPKG

@turnkey/core

Version:

A core JavaScript web and React Native package for interfacing with Turnkey's infrastructure.

66 lines (63 loc) 2.42 kB
import { isWeb, isReactNative } from '../../utils.mjs'; import { IndexedDbStamper } from './web/stamper.mjs'; class CrossPlatformApiKeyStamper { constructor(storageManager) { this.storageManager = storageManager; // Use init method to set up the stamper based on the platform. It's async, so can't be done in the constructor. } async init() { if (isWeb()) { this.stamper = new IndexedDbStamper(); } else if (isReactNative()) { try { // Dynamic import to prevent bundling the native module in web environments. const { ReactNativeKeychainStamper } = await import('./mobile/stamper.mjs'); this.stamper = new ReactNativeKeychainStamper(); } catch (error) { throw new Error(`Failed to load keychain stamper for react-native: ${error}`); } } else { throw new Error("Unsupported platform for API key stamper"); } } listKeyPairs() { return this.stamper.listKeyPairs(); } createKeyPair(externalKeyPair) { return this.stamper.createKeyPair(externalKeyPair); } deleteKeyPair(publicKeyHex) { return this.stamper.deleteKeyPair(publicKeyHex); } clearKeyPairs() { return this.stamper.clearKeyPairs(); } // This allows forcing a specific public key to find the key pair for stamping. The key pair must already exist in indexedDB / Keychain. // This is useful if you need to stamp with a specific key pair without having an active session. // See "signUpWithPasskey" function in core.ts for usage setPublicKeyOverride(publicKeyHex) { this.publicKeyOverride = publicKeyHex; } getPublicKeyOverride() { return this.publicKeyOverride; } clearPublicKeyOverride() { this.publicKeyOverride = undefined; } async stamp(payload) { let publicKeyHex = this.publicKeyOverride; if (!publicKeyHex) { const session = await this.storageManager.getActiveSession(); if (!session) { throw new Error("No active session or token available."); } publicKeyHex = session.publicKey; } return this.stamper.stamp(payload, publicKeyHex); } } export { CrossPlatformApiKeyStamper }; //# sourceMappingURL=base.mjs.map