Examples

Random Key-pairs

Generate and print a random keyPair

import { generateKeyPair } from 'plug-keygen'

const keyPair = generateKeyPair()
console.log(keyPair.publicKey.toString('hex'))

Reusable Root Node

Create an HD root node context bound to a root seed and curve type:

import { deriveNode } from 'plug-keygen'

const seed = '4ed9c4bceb0dd53cd55a4f8a442def799d01444c99d045193...'
const rootNode = deriveNode(seed, 'm')

const childNode = rootNode.deriveNode("m/1234'")
// Or, const childNode = deriveNode(rootNode, "m/1234'")

console.log(childNode.keyPair.publicKey.toString('hex'))

Mnemonic Phrases

Derive a key pair from a mnemonic root seed

import * as Keygen from 'plug-keygen'

const seedPhrase = Keygen.generateMnemonic()
const seed = Keygen.mnemonicToSeed(seedPhrase)
const childKeyPair = Keygen.deriveKeyPair(seed, "m/1234'")

console.log(childKeyPair.publicKey.toString('hex'))

Signing

Sign and verify a challenge using a key pair

import { randomBytes } from 'crypto'
import { generateKeyPair } from 'plug-keygen'

const challenge = randomBytes(32)
const keyPair = generateKeyPair()

const signature = keyPair.sign(challenge)
const isValid = keyPair.verifySignature(challenge, signature)

Signature Verification

Sign and verify a challenge without a key pair. Useful when verifying signatures for which we don’t own the private key.

import { randomBytes } from 'crypto'
import * as Keygen from 'plug-keygen'

const challenge = randomBytes(32)
const { privateKey, publicKey, address } = Keygen.generateKeyPair()

const signature = Keygen.sign(privateKey, challenge)
// Always check public key if verifying address ownership.
const publicKeyMatchesAddress =
Keygen.addressFromPublicKey(publicKey) === address
const verified = Keygen.verifySignature(publicKey, challenge, signature)

const isValid = verified && publicKeyMatchesAddress