- Notifications
You must be signed in to change notification settings - Fork127
Node.js binding for an Optimized C library for EC operations on curve secp256k1
License
NotificationsYou must be signed in to change notification settings
cryptocoinjs/secp256k1-node
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This module provides native bindings tobitcoin-core/secp256k1. In browserelliptic will be used as fallback.
Works on node version 14.0.0 or greater, because useN-API.
npm install secp256k1
git clone git@github.com:cryptocoinjs/secp256k1-node.gitcd secp256k1-nodegit submodule update --initnpm installThe easiest way to build the package on windows is to installwindows-build-tools.
Or install the following software:
- Git:https://git-scm.com/download/win
- nvm:https://github.com/coreybutler/nvm-windows
- Python 2.7:https://www.python.org/downloads/release/python-2712/
- Visual C++ Build Tools:http://landinghub.visualstudio.com/visual-cpp-build-tools (Custom Install, and select both Windows 8.1 and Windows 10 SDKs)
And run commands:
npm config set msvs_version 2015 --globalnpm install npm@next -gBased on:
- API Reference (v4.x) (current version)
- API Reference (v3.x)
- API Reference (v2.x)
const{ randomBytes}=require('crypto')constsecp256k1=require('secp256k1')// or require('secp256k1/elliptic')// if you want to use pure js implementation in node// generate message to sign// message should have 32-byte length, if you have some other length you can hash message// for example `msg = sha256(rawMessage)`constmsg=randomBytes(32)// generate privKeyletprivKeydo{privKey=randomBytes(32)}while(!secp256k1.privateKeyVerify(privKey))// get the public key in a compressed formatconstpubKey=secp256k1.publicKeyCreate(privKey)// sign the messageconstsigObj=secp256k1.ecdsaSign(msg,privKey)// verify the signatureconsole.log(secp256k1.ecdsaVerify(sigObj.signature,msg,pubKey))// => true
*.verify return false for high signatures
const{ randomBytes}=require('crypto')// const secp256k1 = require('./elliptic')constsecp256k1=require('./')// generate privKeyfunctiongetPrivateKey(){while(true){constprivKey=randomBytes(32)if(secp256k1.privateKeyVerify(privKey))returnprivKey}}// generate private and public keysconstprivKey=getPrivateKey()constpubKey=secp256k1.publicKeyCreate(privKey)// compressed public key from X and Yfunctionhashfn(x,y){constpubKey=newUint8Array(33)pubKey[0]=(y[31]&1)===0 ?0x02 :0x03pubKey.set(x,1)returnpubKey}// get X point of ecdhconstecdhPointX=secp256k1.ecdh(pubKey,privKey,{ hashfn},Buffer.alloc(33))console.log(ecdhPointX.toString('hex'))
This library is free and open-source software released under the MIT license.
About
Node.js binding for an Optimized C library for EC operations on curve secp256k1
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.