Class: Key

Key(key, options)

new Key(key, options)

A high level class for accepting and processing keys

Parameters:
Name Type Description
key Object
options Object
Properties
Name Type Attributes Description
format SerializableFormat
kty String

normalized key type name

crv String <optional>

normalized curve name (EC & ED only)

oid String <optional>

ASN oid algorithm descriptor

selector KeySelector | PEMKeySelector
Source:
Throws:

Methods

(static) from(key, format) → {Key}

Import a key

Parameters:
Name Type Description
key JWK | String
format SerializableFormat
Source:
Throws:
Returns:
Type
Key
Examples

Decode PEM and convert to JWK

const keyto = require('@trust/keyto')

let pemPrivate = getPrivatePemStringSomehow()
let jwk = getPublicJwkSomehow()

// String data can either be passed in directly:
let key = keyto.from(pemPrivate, 'pem').toJwk('public')

// Or can be passed in as an object instead:
let key = keyto.from({ key: pemPrivate }, 'pem').toJwk('public')
assertEqual(jwk, key)

Decode HEX (Blockchain) private key and convert to PEM PKCS8 public key

const keyto = require('@trust/keyto')

let blk = getPrivateBlockchainHexStringSomehow()
let pemPublic = getPublicPemSomehow()

let key = keyto.from(blk, 'blk').toString('pem', 'public_pkcs8')
assertEqual(pemPublic, key)

toJwk(selector) → {JWK}

Convert a key to JWK.

Parameters:
Name Type Description
selector KeySelector
Source:
Returns:
Type
JWK

toString(formatopt, selectoropt) → {String}

Serialize a key to the specified format

Parameters:
Name Type Attributes Default Description
format SerializableFormat <optional>
pem
selector KeySelector | PEMKeySelector <optional>
public_pkcs8
Source:
Returns:
Type
String