universal-ecdsa

A universal JavaScript Elliptic Curve Digital Signature Algorithm (ECDSA) for the Koblitz secp256k1 curve.

Usage no npm install needed!

<script type="module">
  import universalEcdsa from 'https://cdn.skypack.dev/universal-ecdsa';
</script>

README

secp256k1 logo

Universal ECDSA

NPM Package CI status License: MIT

A Universal JavaScript Elliptic Curve Digital Signature Algorithm (ECDSA) for the Koblitz secp256k1 curve.

Info

The Web Assembly binary file esdsa.json that is consumed by the JavaScript environments is compiled from C/C++, see cpp/readme.md.

  • ~28 kB (minifeied + gzipped) to bundle

Setup

npm i universal-ecdsa

Suport

  • Node.js ^12.20.1 || >= 13.2
  • Browser defaults, no IE 11

You will need Node +15 web crypto for passing test.

API

function get_public_key

Generates a compressed public key for the secp256k1 curve.

Parameter Type Description
private_key Uint8Array secp256k1 valid private key.

Returns: Uint8Array — Public key.

Examples

Ways to import.

import { get_public_key } from 'universal-ecdsa'

Ways to require.

const { get_public_key } = require('universal-ecdsa')

Usage get_public_key.

const private_key = new Uint8Array([
  210, 101, 63, 247, 203, 178, 216, 255, 18, 154, 194, 126, 245, 120, 28, 230,
  139, 37, 88, 196, 26, 116, 175, 31, 45, 220, 166, 53, 203, 238, 240, 125
])

get_public_key(private_key).then(console.log) // compressed public key.

The logged output was [2, …, 207].


function sha256

Universal sha256 message digest helper function.

Parameter Type Description
data Uint8Array Binary data to hash.

Returns: Uint8Array — Message digest.

Examples

Ways to import.

import { sha256 } from 'universal-ecdsa'

Ways to require.

const { sha256 } = require('universal-ecdsa')

Usage sha256 in node.

const array = Uint8Array.from(
  Buffer.from('The quick brown fox jumped over the lazy dog')
)

sha256(array).then(console.log)

The logged output is [215, …, 146 ]


function sign

Generates a digital signature on the secp256k1 Koblitz curve.

Parameter Type Description
Arg object Argument.
Arg.private_key Uin8Array secp256k1 private key.
Arg.data Uin8Array Data to sign.

Returns: Signature — Digital signature object containing r and s values.

Examples

Ways to import.

import { sign } from 'universal-ecdsa'

Ways to require.

const { sign } = require('universal-ecdsa')

Usage sign.

const private_key = new Uint8Array([
  210, 101, 63, 247, 203, 178, 216, 255, 18, 154, 194, 126, 245, 120, 28, 230,
  139, 37, 88, 196, 26, 116, 175, 31, 45, 220, 166, 53, 203, 238, 240, 125
])

const data = Uint8Array.from([104, 101, 108, 108, 111])
sign({ data, private_key }).then(console.log)

The logged output is { r: [23, …, 89], s: [111, …, 142] }