UNPKG

@turnkey/core

Version:

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

70 lines (66 loc) 2.16 kB
'use strict'; var apiKeyStamper = require('@turnkey/api-key-stamper'); var crypto = require('@turnkey/crypto'); let Keychain; try { Keychain = require("react-native-keychain"); } catch { throw new Error("Please install react-native-keychain in your app to use ReactNativeKeychainStamper"); } class ReactNativeKeychainStamper { async listKeyPairs() { return await Keychain.getAllGenericPasswordServices(); } async clearKeyPairs() { const keys = await this.listKeyPairs(); for (const key of keys) { await this.deleteKeyPair(key); } } async createKeyPair(externalKeyPair) { let privateKey; let publicKey; if (externalKeyPair) { privateKey = externalKeyPair.privateKey; publicKey = externalKeyPair.publicKey; } else { const pair = crypto.generateP256KeyPair(); privateKey = pair.privateKey; publicKey = pair.publicKey; } // store in Keychain await Keychain.setGenericPassword(publicKey, privateKey, { service: publicKey, }); return publicKey; } async deleteKeyPair(publicKeyHex) { await Keychain.resetGenericPassword({ service: publicKeyHex, }); } async getPrivateKey(publicKeyHex) { const creds = await Keychain.getGenericPassword({ service: publicKeyHex, }); if (!creds) return null; return creds.password; } async stamp(payload, publicKeyHex) { const privateKey = await this.getPrivateKey(publicKeyHex); if (!privateKey) { throw new Error(`No private key found for public key: ${publicKeyHex}`); } const stamper = new apiKeyStamper.ApiKeyStamper({ apiPublicKey: publicKeyHex, apiPrivateKey: privateKey, }); const { stampHeaderName, stampHeaderValue } = await stamper.stamp(payload); return { stampHeaderName, stampHeaderValue }; } } exports.ReactNativeKeychainStamper = ReactNativeKeychainStamper; //# sourceMappingURL=stamper.js.map