EOSIO / eosjs-ecc

Elliptic curve cryptography functions: Private Key, Public Key, Signature, AES, Encryption, Decryption
287 stars 119 forks source link

NPM Build Status

Elliptic curve cryptography functions (ECC)

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

Import

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

Include

<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

Table of Contents

wif

Wallet Import Format

Type: 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

unsafeRandomKey

Does not pause to gather CPU entropy.

Returns Promise<PrivateKey> test key

randomKey

Parameters

Examples

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

Returns Promise<wif>

seedPrivate

Parameters

Examples

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

Returns wif

privateToPublic

Parameters

Examples

ecc.privateToPublic(wif) === pubkey

Returns pubkey

isValidPublic

Parameters

Examples

ecc.isValidPublic(pubkey) === true

Returns boolean valid

isValidPrivate

Parameters

Examples

ecc.isValidPrivate(wif) === true

Returns boolean valid

sign

Create a signature using data or a hash.

Parameters

Examples

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

Returns string string signature

signHash

Parameters

Returns string string signature

verify

Verify signed data.

Parameters

Examples

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

Returns boolean

recover

Recover the public key used to create the signature.

Parameters

Examples

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

Returns pubkey

recoverHash

Parameters

Returns PublicKey

sha256

Parameters

Examples

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

Returns (string | Buffer) Buffer when encoding is null, or string

pubkey

EOSKey..

Type: string

Usage (Object API)

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()

Browser

git clone https://github.com/EOSIO/eosjs-ecc.git
cd eosjs-ecc
yarn
yarn build_browser
# builds: ./dist/eosjs-ecc.js
# Verify release hash
<script src=eosjs-ecc.js></script>
var ecc = eosjs_ecc

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