mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-28 16:45:10 +08:00
* Add xstream * Add bigi * Add uuid-js * Add user-home * Add strip-bom * Add strip-ansi * Add slug * Add safe-regex * Add react-recaptcha * Add is-absolute-url * Add is-archive * Add is-compressed * Add is-relative-url * add is-root-path * Add is-root * Add is-text-path * add os-homedir * Add os-tmpdir * Add path-is-absolute * Add pad * Add number-is-nan * Add node-hid * Add is-finite * is-path-incwd * Add indent-string * Add cpy * Add camelcase-keys * Add blacklist * add http-codes * clamp-js * Add checkstyle-formatter * Add currency-formatter * Add multi-typeof * Add intl-messageformat * Add coinstring * Add ecurve * Add bitcoinjs-lib * Add deep-freeze * Add fuxxaldrin * Add react-body-classname * Add react-highlight-words * Update headers * Fix lint errors * remove xstream * Code review comments * Remove clamp-js in favour of https://github.com/DefinitelyTyped/DefinitelyTyped/pull/13527
55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
/// <reference types="node" />
|
|
|
|
import ecurve = require('ecurve');
|
|
import crypto = require('crypto');
|
|
|
|
import BigInteger = require('bigi');
|
|
import cs = require('coinstring');
|
|
|
|
|
|
var ecparams = ecurve.getCurveByName('secp256k1')
|
|
console.log(ecparams.n.toString(16))
|
|
// => fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
|
|
console.log(ecparams.G.getEncoded().toString('hex')) //getEncoded() returns type 'Buffer' instead of 'BigInteger'
|
|
// => 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
|
|
console.log(ecparams.h.toString(16))
|
|
// => 1
|
|
|
|
|
|
var privateKey = new Buffer("1184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd", 'hex')
|
|
|
|
var ecparams = ecurve.getCurveByName('secp256k1')
|
|
var curvePt = ecparams.G.multiply(BigInteger.fromBuffer(privateKey))
|
|
var x = curvePt.affineX.toBuffer(32)
|
|
var y = curvePt.affineY.toBuffer(32)
|
|
|
|
var publicKey = Buffer.concat([new Buffer([0x04]), x, y])
|
|
console.log(publicKey.toString('hex'))
|
|
// => 04d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6fbdd594388756a7beaf73b4822bc22d36e9bda7db82df2b8b623673eefc0b7495
|
|
|
|
//alternatively
|
|
publicKey = curvePt.getEncoded(false) //false forces uncompressed public key
|
|
console.log(publicKey.toString('hex'))
|
|
// => 04d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6fbdd594388756a7beaf73b4822bc22d36e9bda7db82df2b8b623673eefc0b7495
|
|
|
|
//want compressed?
|
|
publicKey = curvePt.getEncoded(true) //true forces compressed public key
|
|
console.log(publicKey.toString('hex'))
|
|
// => 03d0988bfa799f7d7ef9ab3de97ef481cd0f75d2367ad456607647edde665d6f6f
|
|
|
|
var sha = crypto.createHash('sha256').update(publicKey).digest()
|
|
var pubkeyHash = crypto.createHash('rmd160').update(sha).digest()
|
|
|
|
// pubkeyHash of compressed public key
|
|
console.log(pubkeyHash.toString('hex'))
|
|
// => a1c2f92a9dacbd2991c3897724a93f338e44bdc1
|
|
|
|
// address of compressed public key
|
|
console.log(cs.encode(pubkeyHash, 0x0)) //<-- 0x0 is for public addresses
|
|
// => 1FkKMsKNJqWSDvTvETqcCeHcUQQ64kSC6s
|
|
|
|
console.log(cs.encode(privateKey, 0x80)) //<--- 0x80 is for private addresses
|
|
// => 5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD
|
|
|
|
console.log(cs.encode(Buffer.concat([privateKey, new Buffer([0])]), 0x80)) // <-- compressed private address
|
|
// => KwomKti1X3tYJUUMb1TGSM2mrZk1wb1aHisUNHCQXTZq5aqzCxDY
|