UNPKG

@turnkey/core

Version:

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

68 lines (64 loc) 2.5 kB
'use strict'; var utils = require('../../utils.js'); var stamper = require('./web/stamper.js'); 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 (utils.isWeb()) { this.stamper = new stamper.IndexedDbStamper(); } else if (utils.isReactNative()) { try { // Dynamic import to prevent bundling the native module in web environments. const { ReactNativeKeychainStamper } = await Promise.resolve().then(function () { return require('./mobile/stamper.js'); }); 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); } } exports.CrossPlatformApiKeyStamper = CrossPlatformApiKeyStamper; //# sourceMappingURL=base.js.map