Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

starkbank-ecdsa

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

starkbank-ecdsa

fast openSSL-compatible implementation of the Elliptic Curve Digital Signature Algorithm (ECDSA)

  • 1.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
72K
increased by15.9%
Maintainers
2
Weekly downloads
 
Created
Source

A lightweight and fast ECDSA implementation

Overview

This is a pure JS implementation of the Elliptic Curve Digital Signature Algorithm. It is compatible with OpenSSL and uses elegant math such as Jacobian Coordinates to speed up the ECDSA on pure JS.

Installation

To install StarkBank`s ECDSA for Node JS, run:

npm install starkbank-ecdsa

Curves

We currently support secp256k1, but it's super easy to add more curves to the project. Just add them on curve.js

Speed

We ran a test on Node 13.1.0 on a MAC Pro i5 2019. The libraries ran 100 times and showed the average times displayed bellow:

Librarysignverify
crypto0.5ms1.0ms
starkbank-ecdsa6.3ms15.0ms

Sample Code

How to sign a json message for Stark Bank:

var ellipticcurve = require("starkbank-ecdsa");
var Ecdsa = ellipticcurve.Ecdsa;
var PrivateKey = ellipticcurve.PrivateKey;

// Generate privateKey from PEM string
var privateKey = PrivateKey.fromPem("-----BEGIN EC PARAMETERS-----\nBgUrgQQACg==\n-----END EC PARAMETERS-----\n-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK\noUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB\nRmpeRREXj5aog/Mq8RrdYy75W9q/Ig==\n-----END EC PRIVATE KEY-----\n");

// Create message from json
let message = JSON.stringify({
    "transfers": [
        {
            "amount": 100000000,
            "taxId": "594.739.480-42",
            "name": "Daenerys Targaryen Stormborn",
            "bankCode": "341",
            "branchCode": "2201",
            "accountNumber": "76543-8",
            "tags": ["daenerys", "targaryen", "transfer-1-external-id"]
        }
    ]
});

signature = Ecdsa.sign(message, privateKey);

// Generate Signature in base64. This result can be sent to Stark Bank in header as Digital-Signature parameter
console.log(signature.toBase64());

// To double check if message matches the signature
let publicKey = privateKey.publicKey();

console.log(Ecdsa.verify(message, signature, publicKey));

Simple use:

var ellipticcurve = require("starkbank-ecdsa");
var Ecdsa = ellipticcurve.Ecdsa;
var PrivateKey = ellipticcurve.PrivateKey;

// Generate new Keys
let privateKey = new PrivateKey();
let publicKey = privateKey.publicKey();

let message = "My test message";

// Generate Signature
let signature = Ecdsa.sign(message, privateKey);

// Verify if signature is valid
console.log(Ecdsa.verify(message, signature, publicKey));

OpenSSL

This library is compatible with OpenSSL, so you can use it to generate keys:

openssl ecparam -name secp256k1 -genkey -out privateKey.pem
openssl ec -in privateKey.pem -pubout -out publicKey.pem

Create a message.txt file and sign it:

openssl dgst -sha256 -sign privateKey.pem -out signatureDer.txt message.txt

To verify, do this:

var ellipticcurve = require("starkbank-ecdsa");
var Ecdsa = ellipticcurve.Ecdsa;
var Signature = ellipticcurve.Signature;
var PublicKey = ellipticcurve.PublicKey;
var File = ellipticcurve.utils.File;

let publicKeyPem = File.read("publicKey.pem");
let signatureDer = File.read("signatureDer.txt", "binary");
let message = File.read("message.txt");

let publicKey = PublicKey.fromPem(publicKeyPem);
let signature = Signature.fromDer(signatureDer);

console.log(Ecdsa.verify(message, signature, publicKey));

You can also verify it on terminal:

openssl dgst -sha256 -verify publicKey.pem -signature signatureDer.txt message.txt

NOTE: If you want to create a Digital Signature to use in the Stark Bank, you need to convert the binary signature to base64.

openssl base64 -in signatureDer.txt -out signatureBase64.txt

You can do the same with this library:

var ellipticcurve = require("starkbank-ecdsa");
var Signature = ellipticcurve.Signature;
var File = ellipticcurve.utils.File;

let signatureDer = File.read("signatureDer.txt", "binary");

let signature = Signature.fromDer(signatureDer);

console.log(signature.toBase64());

Run all unit tests

Run tests in Mocha framework

node test

or

./node_modules/mocha/bin/mocha

Keywords

FAQs

Package last updated on 31 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc