# key-encoder

> Library for encoding ECDSA private keys to PEM, DER and raw hex formats

Latest version **2.0.3** (published 2019-10-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install key-encoder
pnpm add key-encoder
yarn add key-encoder
bun add key-encoder
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2019-10-17 |
| First published | 2015-09-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 33.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 47 |
| Author | Halfmoon Labs, Inc. |
| Maintainers | ablankstein, onename, shea256 |
| Keywords | elliptic, elliptic curve, asn, pem, der, ecdsa, crypto, cryptography, private, key, encode, decode, secp256k1, bitcoin, curve, private key, private keys, public key, public keys, keys, ecdsa key, ecdsa keys, key converter, convert keys, key format, key formatter, format keys, key encoder, encode keys |

## Links

- npm: https://www.npmjs.com/package/key-encoder
- Repository: https://github.com/blockstack/key-encoder-js
- Issues: https://github.com/blockstack/key-encoder-js/issues
- npm.io page: https://npm.io/package/key-encoder

## Dependencies (4)

- [bn.js](https://npm.io/package/bn.js.md) ^4.11.8
- [asn1.js](https://npm.io/package/asn1.js.md) ^5.0.1
- [elliptic](https://npm.io/package/elliptic.md) ^6.4.1
- [@types/elliptic](https://npm.io/package/@types/elliptic.md) ^6.4.9

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 2.0.3 (latest) — 2019-10-17
- 2.1.0-beta.0 (beta) — 2021-06-09
- 2.0.3-beta.0 — 2019-10-15
- 2.0.2 — 2019-06-25
- 2.0.1 — 2019-06-03
- 2.0.0 — 2019-06-03
- 1.1.7 — 2018-12-17
- 1.1.6 — 2015-10-06
- 1.1.5 — 2015-10-06
- 1.1.4 — 2015-10-06
- 1.1.3 — 2015-09-23
- 1.1.2 — 2015-09-23
- 1.1.1 — 2015-09-22
- 1.1.0 — 2015-09-22
- 1.0.6 — 2015-09-17
- … 6 more at https://npm.io/package/key-encoder/versions

## README

# Key Encoder JS

[![CircleCI](https://img.shields.io/circleci/project/blockstack/key-encoder-js/master.svg)](https://circleci.com/gh/blockstack/key-encoder-js/tree/master)
[![npm](https://img.shields.io/npm/l/key-encoder.svg)](https://www.npmjs.com/package/key-encoder)
[![npm](https://img.shields.io/npm/v/key-encoder.svg)](https://www.npmjs.com/package/key-encoder)
[![npm](https://img.shields.io/npm/dm/key-encoder.svg)](https://www.npmjs.com/package/key-encoder)
[![Slack](http://slack.blockstack.org/badge.svg)](http://slack.blockstack.org/)

### Installation

```
$ npm install key-encoder
```

### Getting Started

To get started, first define your key encoder and raw private/public keys.

#### SECP256k1 Key Encoders

```js
var KeyEncoder = require('key-encoder'),
    keyEncoder = new KeyEncoder('secp256k1')
```

As shown above, there is built in support for SECP256k1 (the curve Bitcoin uses), but you can pass in your own curve parameters for any curve you'd like.

#### Key Encoders w/ Custom Curves

```js
var EC = require('elliptic').ec
var encoderOptions = {
    curveParameters: [1, 3, 132, 0, 10],
    privatePEMOptions: {label: 'EC PRIVATE KEY'},
    publicPEMOptions: {label: 'PUBLIC KEY'},
    curve: new EC('secp256k1')
}
var keyEncoder = new KeyEncoder(encoderOptions)
```

#### Declaring Raw Keys

```js
var rawPrivateKey = '844055cca13efd78ce79a4c3a4c5aba5db0ebeb7ae9d56906c03d333c5668d5b',
    rawPublicKey = '04147b79e9e1dd3324ceea115ff4037b6c877c73777131418bfb2b713effd0f502327b923861581bd5535eeae006765269f404f5f5c52214e9721b04aa7d040a75'
```

### Encoding Private Keys

Encode to and from raw, PEM, and DER formats.

#### Encoding Private Keys as PEMs

```js
var pemPrivateKey = keyEncoder.encodePrivate(rawPrivateKey, 'raw', 'pem')
```

Example output:

```
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIIRAVcyhPv14znmkw6TFq6XbDr63rp1WkGwD0zPFZo1boAcGBSuBBAAK
oUQDQgAEFHt56eHdMyTO6hFf9AN7bId8c3dxMUGL+ytxPv/Q9QIye5I4YVgb1VNe
6uAGdlJp9AT19cUiFOlyGwSqfQQKdQ==
-----END EC PRIVATE KEY-----
```

#### Encoding Private Keys to DER Format

```js
var derPrivateKey = keyEncoder.encodePrivate(rawPrivateKey, 'raw', 'der')
```

Example output:

```
30740201010420844055cca13efd78ce79a4c3a4c5aba5db0ebeb7ae9d56906c03d333c5668d5ba00706052b8104000aa14403420004147b79e9e1dd3324ceea115ff4037b6c877c73777131418bfb2b713effd0f502327b923861581bd5535eeae006765269f404f5f5c52214e9721b04aa7d040a75
```

### Encoding Public Keys

Encode to and from raw, PEM, and DER formats.

#### Encoding Public Keys as PEMs

```js
var pemPublicKey = keyEncoder.encodePublic(rawPublicKey, 'raw', 'pem')
```

Example output:

```
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEFHt56eHdMyTO6hFf9AN7bId8c3dxMUGL
+ytxPv/Q9QIye5I4YVgb1VNe6uAGdlJp9AT19cUiFOlyGwSqfQQKdQ==
-----END PUBLIC KEY-----
```

#### Encoding Public Keys to DER Format

```js
var derPublicKey = keyEncoder.encodePublic(rawPublicKey, 'raw', 'der')
```

Example output:

```
3056301006072a8648ce3d020106052b8104000a03420004147b79e9e1dd3324ceea115ff4037b6c877c73777131418bfb2b713effd0f502327b923861581bd5535eeae006765269f404f5f5c52214e9721b04aa7d040a75
```

---
_Source: https://npm.io/package/key-encoder · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
