Private key management

Introduction

Let's review the following code examples on how to store and load private keys:

Storing private keys

In the following code snippet we encrypt the private key of an identity using the password SuperDuperSecretPassword. The resulting JSON object can be stored in a file, or in a browser's local storage.

const identity = identityManager.generateSimpleIdentity()
const password = 'SuperDuperSecretPassword'
RadixKeyStore.encryptKey(identity.address, password).then((encryptedKey) => {
console.log('Private key encrypted')
}).catch((error) => {
console.error('Error encrypting private key', error)
})

The key storage format is compatible with the Radix Desktop and Android wallet applications.

Loading private keys

In the following code snippet we decrypt a private key using SuperDuperSecretPassword as the decryption password.

const encryptedKey = loadKeyFromStorage() // This object is what you get from RadixKeyStore.encryptKey(...)
const password = 'SuperDuperSecretPassword'
RadixKeyStore.decryptKey(encryptedKey, password).then((address) => {
console.log('Private key successfuly decrypted')
const identity = new RadixSimpleIdentity(address)
}).catch((error) => {
console.error('Error decrypting private key', error)
})