# eosjs-ecc

> Elliptic curve cryptography functions

Latest version **4.0.7** (published 2019-09-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install eosjs-ecc
pnpm add eosjs-ecc
yarn add eosjs-ecc
bun add eosjs-ecc
```

## 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 | 4.0.7 |
| Published | 2019-09-23 |
| First published | 2017-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 1.2 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 289 |
| Maintainers | b1serviceaccount, benbenben1010, chris-allnutt, j8u8s8t8i8n8, jeffreyssmith2nd, julienheller, smetelin, takn |
| Keywords | ECC, Private Key, Public Key, Signature, AES, Encryption, Decryption |

## Links

- npm: https://www.npmjs.com/package/eosjs-ecc
- Repository: https://github.com/EOSIO/eosjs-ecc
- Homepage: https://github.com/EOSIO/eosjs-ecc#readme
- Issues: https://github.com/EOSIO/eosjs-ecc/issues
- npm.io page: https://npm.io/package/eosjs-ecc

## Dependencies (9)

- [bigi](https://npm.io/package/bigi.md) 1.4.2
- [bs58](https://npm.io/package/bs58.md) 4.0.1
- [ecurve](https://npm.io/package/ecurve.md) 1.0.5
- [bytebuffer](https://npm.io/package/bytebuffer.md) 5.0.1
- [create-hash](https://npm.io/package/create-hash.md) 1.1.3
- [create-hmac](https://npm.io/package/create-hmac.md) 1.1.6
- [randombytes](https://npm.io/package/randombytes.md) 2.0.5
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) 7.6.0
- [browserify-aes](https://npm.io/package/browserify-aes.md) 1.0.6

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 4.0.7 (latest) — 2019-09-23
- 4.0.4-168dae3.0 (edge) — 2019-09-23
- 4.0.0 (dawn3) — 2018-05-04
- 4.0.4-4baf37f.0 — 2019-09-23
- 4.0.7-ea61ee8.0 — 2019-07-31
- 4.0.4 — 2018-08-22
- 4.0.3 — 2018-08-14
- 4.0.2 — 2018-06-26
- 4.0.1 — 2018-05-24
- 3.1.0 — 2018-04-30
- 3.0.3 — 2018-04-30
- 3.0.2 — 2018-03-23
- 3.0.1 — 2018-03-09
- 3.0.0 — 2018-03-08
- 2.0.1 — 2017-12-14
- … 17 more at https://npm.io/package/eosjs-ecc/versions

## README

[![NPM](https://img.shields.io/npm/v/eosjs-ecc.svg)](https://www.npmjs.org/package/eosjs-ecc)
[![Build Status](https://travis-ci.org/EOSIO/eosjs-ecc.svg?branch=master)](https://travis-ci.org/EOSIO/eosjs-ecc)

# Elliptic curve cryptography functions (ECC)

Private Key, Public Key, Signature, AES, Encryption / Decryption

# Import

```js
import ecc from 'eosjs-ecc'
// or
const ecc = require('eosjs-ecc')
```

# Include

-   Install with: `yarn add eosjs-ecc`
-   Html script tag, see [releases](https://github.com/EOSIO/eosjs-ecc/releases) for the correct **version** and its matching script **integrity** hash.

```html
<html>
<head>
  <meta charset="utf-8">
  <!--
  sha512-cL+IQQaQ586s9DrXfGtDheRpj5iDKh2M+xlpfwbhNjRIp4BGQ1fkM/vB4Ta8mc+f51YBW9sJiPcyMDIreJe6gQ== lib/eosjs-ecc.js
  sha512-dYFDmK/d9r3/NCp6toLtfkwOjSMRBaEzaGAx1tfRItC0nsI0hVLERk05iNBQR7uDNI7ludYhcBI4vUiFHdjsTQ== lib/eosjs-ecc.min.js
  sha512-eq1SCoSe38uR1UVuQMwR73VgY8qKTBDc87n2nIiC5WLhn1o2y1U6c5wY8lrigVX7INM8fM0PxDlMX5WvpghKig== lib/eosjs-ecc.min.js.map
  -->
  <script src="https://cdn.jsdelivr.net/npm/eosjs-ecc@4.0.4/lib/eosjs-ecc.min.js"
    integrity="sha512-dYFDmK/d9r3/NCp6toLtfkwOjSMRBaEzaGAx1tfRItC0nsI0hVLERk05iNBQR7uDNI7ludYhcBI4vUiFHdjsTQ=="
    crossorigin="anonymous"></script>

</head>
<body>
  See console object: eosjs_ecc
</body>
</html>
```

# Common API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [wif](#wif)
-   [ecc](#ecc)
    -   [initialize](#initialize)
    -   [unsafeRandomKey](#unsaferandomkey)
    -   [randomKey](#randomkey)
        -   [Parameters](#parameters)
        -   [Examples](#examples)
    -   [seedPrivate](#seedprivate)
        -   [Parameters](#parameters-1)
        -   [Examples](#examples-1)
    -   [privateToPublic](#privatetopublic)
        -   [Parameters](#parameters-2)
        -   [Examples](#examples-2)
    -   [isValidPublic](#isvalidpublic)
        -   [Parameters](#parameters-3)
        -   [Examples](#examples-3)
    -   [isValidPrivate](#isvalidprivate)
        -   [Parameters](#parameters-4)
        -   [Examples](#examples-4)
    -   [sign](#sign)
        -   [Parameters](#parameters-5)
        -   [Examples](#examples-5)
    -   [signHash](#signhash)
        -   [Parameters](#parameters-6)
    -   [verify](#verify)
        -   [Parameters](#parameters-7)
        -   [Examples](#examples-6)
    -   [recover](#recover)
        -   [Parameters](#parameters-8)
        -   [Examples](#examples-7)
    -   [recoverHash](#recoverhash)
        -   [Parameters](#parameters-9)
    -   [sha256](#sha256)
        -   [Parameters](#parameters-10)
        -   [Examples](#examples-8)
-   [pubkey](#pubkey)

## wif

[Wallet Import Format](https://en.bitcoin.it/wiki/Wallet_import_format)

Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)

## ecc

### initialize

Initialize by running some self-checking code.  This should take a
second to gather additional CPU entropy used during private key
generation.

Initialization happens once even if called multiple times.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)** 

### unsafeRandomKey

Does not pause to gather CPU entropy.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;PrivateKey>** test key

### randomKey

#### Parameters

-   `cpuEntropyBits` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** gather additional entropy
    from a CPU mining algorithm.  This will already happen once by
    default. (optional, default `0`)

#### Examples

```javascript
ecc.randomKey().then(privateKey => {
console.log('Private Key:\t', privateKey) // wif
console.log('Public Key:\t', ecc.privateToPublic(privateKey)) // EOSkey...
})
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[wif](#wif)>** 

### seedPrivate

#### Parameters

-   `seed` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** any length string.  This is private.  The same
    seed produces the same private key every time.  At least 128 random
    bits should be used to produce a good private key.

#### Examples

```javascript
ecc.seedPrivate('secret') === wif
```

Returns **[wif](#wif)** 

### privateToPublic

#### Parameters

-   `wif` **[wif](#wif)** 
-   `pubkey_prefix` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** public key prefix (optional, default `'EOS'`)

#### Examples

```javascript
ecc.privateToPublic(wif) === pubkey
```

Returns **[pubkey](#pubkey)** 

### isValidPublic

#### Parameters

-   `pubkey` **[pubkey](#pubkey)** like EOSKey..
-   `pubkey_prefix` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**  (optional, default `'EOS'`)

#### Examples

```javascript
ecc.isValidPublic(pubkey) === true
```

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** valid

### isValidPrivate

#### Parameters

-   `wif` **[wif](#wif)** 

#### Examples

```javascript
ecc.isValidPrivate(wif) === true
```

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** valid

### sign

Create a signature using data or a hash.

#### Parameters

-   `data` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** 
-   `privateKey` **([wif](#wif) | PrivateKey)** 
-   `encoding` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** data encoding (if string) (optional, default `'utf8'`)

#### Examples

```javascript
ecc.sign('I am alive', wif)
```

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** string signature

### signHash

#### Parameters

-   `dataSha256` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** sha256 hash 32 byte buffer or string
-   `privateKey` **([wif](#wif) | PrivateKey)** 
-   `encoding` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** dataSha256 encoding (if string) (optional, default `'hex'`)

Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** string signature

### verify

Verify signed data.

#### Parameters

-   `signature` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** buffer or hex string
-   `data` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** 
-   `pubkey` **([pubkey](#pubkey) | PublicKey)** 
-   `encoding`   (optional, default `'utf8'`)
-   `hashData` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** sha256 hash data before verify (optional, default `true`)

#### Examples

```javascript
ecc.verify(signature, 'I am alive', pubkey) === true
```

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 

### recover

Recover the public key used to create the signature.

#### Parameters

-   `signature` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** (EOSbase58sig.., Hex, Buffer)
-   `data` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** full data
-   `encoding` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** data encoding (if data is a string) (optional, default `'utf8'`)

#### Examples

```javascript
ecc.recover(signature, 'I am alive') === pubkey
```

Returns **[pubkey](#pubkey)** 

### recoverHash

#### Parameters

-   `signature` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** (EOSbase58sig.., Hex, Buffer)
-   `dataSha256` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** sha256 hash 32 byte buffer or hex string
-   `encoding` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** dataSha256 encoding (if dataSha256 is a string) (optional, default `'hex'`)

Returns **PublicKey** 

### sha256

#### Parameters

-   `data` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** always binary, you may need Buffer.from(data, 'hex')
-   `resultEncoding`   (optional, default `'hex'`)
-   `encoding` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** result encoding 'hex', 'binary' or 'base64' (optional, default `'hex'`)

#### Examples

```javascript
ecc.sha256('hashme') === '02208b..'
```

```javascript
ecc.sha256(Buffer.from('02208b', 'hex')) === '29a23..'
```

Returns **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** Buffer when encoding is null, or string

## pubkey

EOSKey..

Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)

# Usage (Object API)

```js
let {PrivateKey, PublicKey, Signature, Aes, key_utils, config} = require('eosjs-ecc')

// Create a new random private key
let privateWif
PrivateKey.randomKey().then(privateKey => privateWif = privateKey.toWif())

// Convert to a public key
pubkey = PrivateKey.fromString(privateWif).toPublic().toString()
```

-   [PrivateKey](./src/key_private.js)
-   [PublicKey](./src/key_public.js)
-   [Signature](./src/signature.js)
-   [Aes](./src/aes.js)
-   [key_utils](./src/key_utils.js)
-   [config](./src/config.js)

# Browser

```bash
git clone https://github.com/EOSIO/eosjs-ecc.git
cd eosjs-ecc
yarn
yarn build_browser
# builds: ./dist/eosjs-ecc.js
# Verify release hash
```

```html
<script src=eosjs-ecc.js></script>
```

```js
var ecc = eosjs_ecc

ecc.randomKey().then(privateWif =>  {
  var pubkey = ecc.privateToPublic(privateWif)
  console.log(pubkey)
})
```

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