Elliptic curve cryptography functions (ECC)
Private Key, Public Key, Signature, AES, Encryption / Decryption
Import
import ecc from 'agrjs-ecc'
const ecc = require('agrjs-ecc')
Include
- Install with:
npm install agrjs-ecc
- Html script tag, see releases for the correct version and its matching script integrity hash.
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/agrjs-ecc@4.0.4/lib/agrjs-ecc.min.js"
integrity="sha512-dYFDmK/d9r3/NCp6toLtfkwOjSMRBaEzaGAx1tfRItC0nsI0hVLERk05iNBQR7uDNI7ludYhcBI4vUiFHdjsTQ=="
crossorigin="anonymous"></script>
</head>
<body>
See console object: agrjs_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
cpuEntropyBits
number gather additional entropy
from a CPU mining algorithm. This will already happen once by
default. (optional, default 0
)
Examples
ecc.randomKey().then(privateKey => {
console.log('Private Key:\t', privateKey)
console.log('Public Key:\t', ecc.privateToPublic(privateKey))
})
Returns Promise<wif>
seedPrivate
Parameters
seed
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
ecc.seedPrivate('secret') === wif
Returns wif
privateToPublic
Parameters
wif
wifpubkey_prefix
string public key prefix (optional, default 'AGR'
)
Examples
ecc.privateToPublic(wif) === pubkey
Returns pubkey
isValidPublic
Parameters
pubkey
pubkey like AGRKey..pubkey_prefix
string (optional, default 'AGR'
)
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
data
(string | Buffer)privateKey
(wif | PrivateKey)encoding
String data encoding (if string) (optional, default 'utf8'
)
Examples
ecc.sign('I am alive', wif)
Returns string string signature
signHash
Parameters
dataSha256
(String | Buffer) sha256 hash 32 byte buffer or stringprivateKey
(wif | PrivateKey)encoding
String dataSha256 encoding (if string) (optional, default 'hex'
)
Returns string string signature
verify
Verify signed data.
Parameters
signature
(string | Buffer) buffer or hex stringdata
(string | Buffer)pubkey
(pubkey | PublicKey)encoding
(optional, default 'utf8'
)hashData
boolean sha256 hash data before verify (optional, default true
)
Examples
ecc.verify(signature, 'I am alive', pubkey) === true
Returns boolean
recover
Recover the public key used to create the signature.
Parameters
signature
(String | Buffer) (AGRbase58sig.., Hex, Buffer)data
(String | Buffer) full dataencoding
String data encoding (if data is a string) (optional, default 'utf8'
)
Examples
ecc.recover(signature, 'I am alive') === pubkey
Returns pubkey
recoverHash
Parameters
signature
(String | Buffer) (AGRbase58sig.., Hex, Buffer)dataSha256
(String | Buffer) sha256 hash 32 byte buffer or hex stringencoding
String dataSha256 encoding (if dataSha256 is a string) (optional, default 'hex'
)
Returns PublicKey
sha256
Parameters
data
(string | Buffer) always binary, you may need Buffer.from(data, 'hex')resultEncoding
(optional, default 'hex'
)encoding
string result encoding 'hex', 'binary' or 'base64' (optional, default 'hex'
)
Examples
ecc.sha256('hashme') === '02208b..'
ecc.sha256(Buffer.from('02208b', 'hex')) === '29a23..'
Returns (string | Buffer) Buffer when encoding is null, or string
pubkey
AGRKey..
Type: string
Usage (Object API)
let {PrivateKey, PublicKey, Signature, Aes, key_utils, config} = require('agrjs-ecc')
let privateWif
PrivateKey.randomKey().then(privateKey => privateWif = privateKey.toWif())
pubkey = PrivateKey.fromString(privateWif).toPublic().toString()
Browser
git clone https://github.com/AGRIO/agrjs-ecc.git
cd agrjs-ecc
npm install
npm run build_browser
<script src=agrjs-ecc.js></script>
var ecc = agrjs_ecc
ecc.randomKey().then(privateWif => {
var pubkey = ecc.privateToPublic(privateWif)
console.log(pubkey)
})