Skip to content

Keystore.encrypt

Encrypts a private key as a JSON keystore using a derived key.

Supports the following key derivation functions (KDFs): - Keystore.pbkdf2 - Keystore.scrypt

Imports

Named
import { Keystore } from 'ox'

Examples

import { Keystore, Secp256k1 } from 'ox'
 
// Generate a random private key.
const privateKey = Secp256k1.randomPrivateKey()
 
// Derive key from password.
const key = Keystore.pbkdf2({ password: 'testpassword' })
 
// Encrypt the private key.
const encrypted = await Keystore.encrypt(privateKey, key)
{
"crypto": {
"cipher": "aes-128-ctr",
"ciphertext": "...",
"cipherparams": {
"iv": "...",
},
"kdf": "pbkdf2",
"kdfparams": {
"salt": "...",
"dklen": 32,
"prf": "hmac-sha256",
"c": 262144,
},
"mac": "...",
},
"id": "...",
"version": 3,
}

Definition

function encrypt(
  privateKey: Bytes.Bytes | Hex.Hex,
  key: Key,
  options?: encrypt.Options,
): Promise<Keystore>

Source: src/core/Keystore.ts

Parameters

privateKey

  • Type: Bytes.Bytes | Hex.Hex

Private key to encrypt.

key

Key to use for encryption.

key.c

  • Type: number

key.dklen

  • Type: number

key.n

  • Type: number

key.p

  • Type: number

key.prf

  • Type: "hmac-sha256"

key.r

  • Type: number

key.salt

  • Type: string

options

  • Type: encrypt.Options
  • Optional

Encryption options.

options.id

  • Type: string
  • Optional

UUID.

Return Type

Encrypted keystore.

Keystore.Keystore