@turnkey/core
Version:
A core JavaScript web and React Native package for interfacing with Turnkey's infrastructure.
68 lines (65 loc) • 2.12 kB
JavaScript
import { ApiKeyStamper } from '@turnkey/api-key-stamper';
import { generateP256KeyPair } from '@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 = 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({
apiPublicKey: publicKeyHex,
apiPrivateKey: privateKey,
});
const { stampHeaderName, stampHeaderValue } = await stamper.stamp(payload);
return { stampHeaderName, stampHeaderValue };
}
}
export { ReactNativeKeychainStamper };
//# sourceMappingURL=stamper.mjs.map