Ryan X. Charles
11 years ago
9 changed files with 326 additions and 325 deletions
@ -1,115 +1,5 @@ |
|||
|
|||
|
|||
if (process.versions) { |
|||
// c++ native version
|
|||
var KeyModule = require('bindings')('KeyModule'); |
|||
var Key = KeyModule.Key; |
|||
module.exports = Key; |
|||
} else { |
|||
// pure js version
|
|||
var ECKey = require('../browser/vendor-bundle.js').ECKey; |
|||
var buffertools = require('buffertools'); |
|||
|
|||
var kSpec = function() { |
|||
this._pub = null; |
|||
this.compressed = true; // default
|
|||
}; |
|||
|
|||
var bufferToArray = kSpec.bufferToArray = function(buffer) { |
|||
var ret = []; |
|||
|
|||
var l = buffer.length; |
|||
for(var i =0; i<l; i++) { |
|||
ret.push(buffer.readUInt8(i)); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
|
|||
Object.defineProperty(kSpec.prototype, 'public', { |
|||
set: function(p){ |
|||
if (!Buffer.isBuffer(p) ) { |
|||
throw new Error('Arg should be a buffer'); |
|||
} |
|||
var type = p[0]; |
|||
this.compressed = type!==0x04; |
|||
this._pub = p; |
|||
}, |
|||
get: function(){ |
|||
return this._pub; |
|||
} |
|||
}); |
|||
|
|||
kSpec.generateSync = function() { |
|||
var eck = new ECKey(); |
|||
eck.setCompressed(true); |
|||
var pub = eck.getPub(); |
|||
|
|||
var ret = new kSpec(); |
|||
ret.private = new Buffer(eck.priv.toByteArrayUnsigned()); |
|||
ret.public = new Buffer(pub); |
|||
return ret; |
|||
}; |
|||
|
|||
kSpec.prototype.regenerateSync = function() { |
|||
if (!this.private) { |
|||
throw new Error('Key does not have a private key set'); |
|||
} |
|||
|
|||
var eck = new ECKey(buffertools.toHex(this.private)); |
|||
eck.setCompressed(this.compressed); |
|||
this.public = new Buffer(eck.getPub()); |
|||
return this; |
|||
}; |
|||
|
|||
kSpec.prototype.signSync = function(hash) { |
|||
if (!this.private) { |
|||
throw new Error('Key does not have a private key set'); |
|||
} |
|||
|
|||
if (!Buffer.isBuffer(hash) || hash.length !== 32) { |
|||
throw new Error('Arg should be a 32 bytes hash buffer'); |
|||
} |
|||
var eck = new ECKey(buffertools.toHex(this.private)); |
|||
eck.setCompressed(this.compressed); |
|||
var signature = eck.sign(bufferToArray(hash)); |
|||
// return it as a buffer to keep c++ compatibility
|
|||
return new Buffer(signature); |
|||
}; |
|||
|
|||
kSpec.prototype.verifySignature = function(hash, sig, callback) { |
|||
try { |
|||
var result = this.verifySignatureSync(hash, sig); |
|||
callback(null, result); |
|||
} catch (e) { |
|||
callback(e); |
|||
} |
|||
}; |
|||
|
|||
kSpec.prototype.verifySignatureSync = function(hash, sig) { |
|||
var self = this; |
|||
|
|||
if (!Buffer.isBuffer(hash) || hash.length !== 32) { |
|||
throw new Error('Arg 1 should be a 32 bytes hash buffer'); |
|||
} |
|||
if (!Buffer.isBuffer(sig)) { |
|||
throw new Error('Arg 2 should be a buffer'); |
|||
} |
|||
if (!self.public) { |
|||
throw new Error('Key does not have a public key set'); |
|||
} |
|||
|
|||
var eck = new ECKey(); |
|||
eck.setPub(bufferToArray(self.public)); |
|||
eck.setCompressed(self.compressed); |
|||
var sigA = bufferToArray(sig); |
|||
var ret = eck.verify(bufferToArray(hash),sigA); |
|||
return ret; |
|||
}; |
|||
|
|||
module.exports = kSpec; |
|||
|
|||
module.exports = require('./node/Key'); |
|||
return; |
|||
} |
|||
|
|||
|
|||
module.exports = require('./browser/Key'); |
|||
|
@ -1,138 +1,5 @@ |
|||
"use strict"; |
|||
|
|||
var imports = require('soop').imports(); |
|||
var Key = imports.Key || require('./Key'); |
|||
var bignum = imports.bignum || require('bignum'); |
|||
var assert = require('assert'); |
|||
|
|||
//browser
|
|||
if (!process.versions) { |
|||
var ECKey = require('../browser/vendor-bundle.js').ECKey; |
|||
var ECPointFp = require('../browser/vendor-bundle.js').ECPointFp; |
|||
var ECFieldElementFp = require('../browser/vendor-bundle.js').ECFieldElementFp; |
|||
var getSECCurveByName = require('../browser/vendor-bundle.js').getSECCurveByName; |
|||
var BigInteger = require('../browser/vendor-bundle.js').BigInteger; |
|||
var should = require('chai').should(); |
|||
if (process.versions) { |
|||
module.exports = require('./node/Point'); |
|||
return; |
|||
} |
|||
|
|||
|
|||
//a point on the secp256k1 curve
|
|||
//x and y are bignums
|
|||
var Point = function(x, y) { |
|||
this.x = x; |
|||
this.y = y; |
|||
}; |
|||
|
|||
Point.add = function(p1, p2) { |
|||
|
|||
//node
|
|||
if (process.versions) { |
|||
var key1 = p1.toKey(); |
|||
key1.compressed = false; |
|||
var key2 = p2.toKey(); |
|||
key2.compressed = false; |
|||
var pubKey = Key.addUncompressed(key1.public, key2.public); |
|||
var key = new Key(); |
|||
key.compressed = false; |
|||
key.public = pubKey; |
|||
key.compressed = true; |
|||
return Point.fromKey(key); |
|||
} |
|||
|
|||
//browser
|
|||
else { |
|||
var ecparams = getSECCurveByName('secp256k1'); |
|||
|
|||
var p1xhex = p1.x.toBuffer({size: 32}).toString('hex'); |
|||
var p1x = new BigInteger(p1xhex, 16); |
|||
var p1yhex = p1.y.toBuffer({size: 32}).toString('hex'); |
|||
var p1y = new BigInteger(p1yhex, 16); |
|||
var p1px = new ECFieldElementFp(ecparams.getCurve().getQ(), p1x); |
|||
var p1py = new ECFieldElementFp(ecparams.getCurve().getQ(), p1y); |
|||
var p1p = new ECPointFp(ecparams.getCurve(), p1px, p1py); |
|||
|
|||
var p2xhex = p2.x.toBuffer({size: 32}).toString('hex'); |
|||
var p2x = new BigInteger(p2xhex, 16); |
|||
var p2yhex = p2.y.toBuffer({size: 32}).toString('hex'); |
|||
var p2y = new BigInteger(p2yhex, 16); |
|||
var p2px = new ECFieldElementFp(ecparams.getCurve().getQ(), p2x); |
|||
var p2py = new ECFieldElementFp(ecparams.getCurve().getQ(), p2y); |
|||
var p2p = new ECPointFp(ecparams.getCurve(), p2px, p2py); |
|||
|
|||
var p = p1p.add(p2p); |
|||
|
|||
var point = new Point(); |
|||
var pointxbuf = new Buffer(p.getX().toBigInteger().toByteArrayUnsigned()); |
|||
point.x = bignum.fromBuffer(pointxbuf, {size: pointxbuf.length}); |
|||
assert(pointxbuf.length <= 32); |
|||
var pointybuf = new Buffer(p.getY().toBigInteger().toByteArrayUnsigned()); |
|||
assert(pointybuf.length <= 32); |
|||
point.y = bignum.fromBuffer(pointybuf, {size: pointybuf.length}); |
|||
|
|||
return point; |
|||
} |
|||
|
|||
}; |
|||
|
|||
//convert the public key of a Key into a Point
|
|||
Point.fromKey = function(key) { |
|||
|
|||
//node
|
|||
if (process.versions) { |
|||
var point = new Point(); |
|||
var pubKeyBuf = new Buffer(key.public); |
|||
var key2 = new Key(); |
|||
key2.compressed = key.compressed; |
|||
key2.public = pubKeyBuf; |
|||
key2.compressed = false; |
|||
point.x = bignum.fromBuffer(key2.public.slice(1, 33), {size: 32}); |
|||
point.y = bignum.fromBuffer(key2.public.slice(33, 65), {size: 32}); |
|||
return point; |
|||
} |
|||
|
|||
//browser
|
|||
else { |
|||
var point = new Point(); |
|||
var pubKeyBuf = new Buffer(key.public); |
|||
var key2 = new ECKey(); |
|||
key2.setCompressed(key.compressed); |
|||
key2.setPub(Key.bufferToArray(pubKeyBuf)); |
|||
key2.setCompressed(false); |
|||
point.x = bignum.fromBuffer((new Buffer(key2.getPub())).slice(1, 33), {size: 32}); |
|||
point.y = bignum.fromBuffer((new Buffer(key2.getPub())).slice(33, 65), {size: 32}); |
|||
return point; |
|||
} |
|||
}; |
|||
|
|||
//convert the Point into the Key containing a compressed public key
|
|||
Point.prototype.toKey = function() { |
|||
|
|||
//node
|
|||
if (process.versions) { |
|||
var xbuf = this.x.toBuffer({size: 32}); |
|||
var ybuf = this.y.toBuffer({size: 32}); |
|||
var key = new Key(); |
|||
key.compressed = false; |
|||
var prefix = new Buffer([0x04]); |
|||
key.public = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
|||
key.compressed = true; |
|||
return key; |
|||
} |
|||
|
|||
//browser
|
|||
else { |
|||
var xbuf = this.x.toBuffer({size: 32}); |
|||
var ybuf = this.y.toBuffer({size: 32}); |
|||
var key = new ECKey(); |
|||
key.setCompressed(false); |
|||
var prefix = new Buffer([0x04]); |
|||
var pub = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
|||
key.setPub(Key.bufferToArray(pub)); |
|||
key.setCompressed(true); |
|||
var key2 = new Key(); |
|||
key2.public = new Buffer(key.getPub()); |
|||
return key2; |
|||
} |
|||
}; |
|||
|
|||
module.exports = require('soop')(Point); |
|||
module.exports = require('./browser/Point'); |
|||
|
@ -0,0 +1,102 @@ |
|||
var ECKey = require('../../browser/vendor-bundle.js').ECKey; |
|||
var buffertools = require('buffertools'); |
|||
|
|||
var Key = function() { |
|||
this._pub = null; |
|||
this.compressed = true; // default
|
|||
}; |
|||
|
|||
var bufferToArray = Key.bufferToArray = function(buffer) { |
|||
var ret = []; |
|||
|
|||
var l = buffer.length; |
|||
for(var i =0; i<l; i++) { |
|||
ret.push(buffer.readUInt8(i)); |
|||
} |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
|
|||
Object.defineProperty(Key.prototype, 'public', { |
|||
set: function(p){ |
|||
if (!Buffer.isBuffer(p) ) { |
|||
throw new Error('Arg should be a buffer'); |
|||
} |
|||
var type = p[0]; |
|||
this.compressed = type!==0x04; |
|||
this._pub = p; |
|||
}, |
|||
get: function(){ |
|||
return this._pub; |
|||
} |
|||
}); |
|||
|
|||
Key.generateSync = function() { |
|||
var eck = new ECKey(); |
|||
eck.setCompressed(true); |
|||
var pub = eck.getPub(); |
|||
|
|||
var ret = new Key(); |
|||
ret.private = new Buffer(eck.priv.toByteArrayUnsigned()); |
|||
ret.public = new Buffer(pub); |
|||
return ret; |
|||
}; |
|||
|
|||
Key.prototype.regenerateSync = function() { |
|||
if (!this.private) { |
|||
throw new Error('Key does not have a private key set'); |
|||
} |
|||
|
|||
var eck = new ECKey(buffertools.toHex(this.private)); |
|||
eck.setCompressed(this.compressed); |
|||
this.public = new Buffer(eck.getPub()); |
|||
return this; |
|||
}; |
|||
|
|||
Key.prototype.signSync = function(hash) { |
|||
if (!this.private) { |
|||
throw new Error('Key does not have a private key set'); |
|||
} |
|||
|
|||
if (!Buffer.isBuffer(hash) || hash.length !== 32) { |
|||
throw new Error('Arg should be a 32 bytes hash buffer'); |
|||
} |
|||
var eck = new ECKey(buffertools.toHex(this.private)); |
|||
eck.setCompressed(this.compressed); |
|||
var signature = eck.sign(bufferToArray(hash)); |
|||
// return it as a buffer to keep c++ compatibility
|
|||
return new Buffer(signature); |
|||
}; |
|||
|
|||
Key.prototype.verifySignature = function(hash, sig, callback) { |
|||
try { |
|||
var result = this.verifySignatureSync(hash, sig); |
|||
callback(null, result); |
|||
} catch (e) { |
|||
callback(e); |
|||
} |
|||
}; |
|||
|
|||
Key.prototype.verifySignatureSync = function(hash, sig) { |
|||
var self = this; |
|||
|
|||
if (!Buffer.isBuffer(hash) || hash.length !== 32) { |
|||
throw new Error('Arg 1 should be a 32 bytes hash buffer'); |
|||
} |
|||
if (!Buffer.isBuffer(sig)) { |
|||
throw new Error('Arg 2 should be a buffer'); |
|||
} |
|||
if (!self.public) { |
|||
throw new Error('Key does not have a public key set'); |
|||
} |
|||
|
|||
var eck = new ECKey(); |
|||
eck.setPub(bufferToArray(self.public)); |
|||
eck.setCompressed(self.compressed); |
|||
var sigA = bufferToArray(sig); |
|||
var ret = eck.verify(bufferToArray(hash),sigA); |
|||
return ret; |
|||
}; |
|||
|
|||
module.exports = Key; |
@ -0,0 +1,81 @@ |
|||
"use strict"; |
|||
|
|||
var imports = require('soop').imports(); |
|||
var Key = imports.Key || require('./Key'); |
|||
var bignum = imports.bignum || require('bignum'); |
|||
var assert = require('assert'); |
|||
var ECKey = require('../../browser/vendor-bundle.js').ECKey; |
|||
var ECPointFp = require('../../browser/vendor-bundle.js').ECPointFp; |
|||
var ECFieldElementFp = require('../../browser/vendor-bundle.js').ECFieldElementFp; |
|||
var getSECCurveByName = require('../../browser/vendor-bundle.js').getSECCurveByName; |
|||
var BigInteger = require('../../browser/vendor-bundle.js').BigInteger; |
|||
var should = require('chai').should(); |
|||
|
|||
//a point on the secp256k1 curve
|
|||
//x and y are bignums
|
|||
var Point = function(x, y) { |
|||
this.x = x; |
|||
this.y = y; |
|||
}; |
|||
|
|||
Point.add = function(p1, p2) { |
|||
var ecparams = getSECCurveByName('secp256k1'); |
|||
|
|||
var p1xhex = p1.x.toBuffer({size: 32}).toString('hex'); |
|||
var p1x = new BigInteger(p1xhex, 16); |
|||
var p1yhex = p1.y.toBuffer({size: 32}).toString('hex'); |
|||
var p1y = new BigInteger(p1yhex, 16); |
|||
var p1px = new ECFieldElementFp(ecparams.getCurve().getQ(), p1x); |
|||
var p1py = new ECFieldElementFp(ecparams.getCurve().getQ(), p1y); |
|||
var p1p = new ECPointFp(ecparams.getCurve(), p1px, p1py); |
|||
|
|||
var p2xhex = p2.x.toBuffer({size: 32}).toString('hex'); |
|||
var p2x = new BigInteger(p2xhex, 16); |
|||
var p2yhex = p2.y.toBuffer({size: 32}).toString('hex'); |
|||
var p2y = new BigInteger(p2yhex, 16); |
|||
var p2px = new ECFieldElementFp(ecparams.getCurve().getQ(), p2x); |
|||
var p2py = new ECFieldElementFp(ecparams.getCurve().getQ(), p2y); |
|||
var p2p = new ECPointFp(ecparams.getCurve(), p2px, p2py); |
|||
|
|||
var p = p1p.add(p2p); |
|||
|
|||
var point = new Point(); |
|||
var pointxbuf = new Buffer(p.getX().toBigInteger().toByteArrayUnsigned()); |
|||
point.x = bignum.fromBuffer(pointxbuf, {size: pointxbuf.length}); |
|||
assert(pointxbuf.length <= 32); |
|||
var pointybuf = new Buffer(p.getY().toBigInteger().toByteArrayUnsigned()); |
|||
assert(pointybuf.length <= 32); |
|||
point.y = bignum.fromBuffer(pointybuf, {size: pointybuf.length}); |
|||
|
|||
return point; |
|||
}; |
|||
|
|||
//convert the public key of a Key into a Point
|
|||
Point.fromKey = function(key) { |
|||
var point = new Point(); |
|||
var pubKeyBuf = new Buffer(key.public); |
|||
var key2 = new ECKey(); |
|||
key2.setCompressed(key.compressed); |
|||
key2.setPub(Key.bufferToArray(pubKeyBuf)); |
|||
key2.setCompressed(false); |
|||
point.x = bignum.fromBuffer((new Buffer(key2.getPub())).slice(1, 33), {size: 32}); |
|||
point.y = bignum.fromBuffer((new Buffer(key2.getPub())).slice(33, 65), {size: 32}); |
|||
return point; |
|||
}; |
|||
|
|||
//convert the Point into the Key containing a compressed public key
|
|||
Point.prototype.toKey = function() { |
|||
var xbuf = this.x.toBuffer({size: 32}); |
|||
var ybuf = this.y.toBuffer({size: 32}); |
|||
var key = new ECKey(); |
|||
key.setCompressed(false); |
|||
var prefix = new Buffer([0x04]); |
|||
var pub = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
|||
key.setPub(Key.bufferToArray(pub)); |
|||
key.setCompressed(true); |
|||
var key2 = new Key(); |
|||
key2.public = new Buffer(key.getPub()); |
|||
return key2; |
|||
}; |
|||
|
|||
module.exports = require('soop')(Point); |
@ -0,0 +1 @@ |
|||
module.exports = require('bindings')('KeyModule').Key; |
@ -0,0 +1,53 @@ |
|||
"use strict"; |
|||
|
|||
var imports = require('soop').imports(); |
|||
var Key = imports.Key || require('../Key'); |
|||
var bignum = imports.bignum || require('bignum'); |
|||
var assert = require('assert'); |
|||
|
|||
//a point on the secp256k1 curve
|
|||
//x and y are bignums
|
|||
var Point = function(x, y) { |
|||
this.x = x; |
|||
this.y = y; |
|||
}; |
|||
|
|||
Point.add = function(p1, p2) { |
|||
var key1 = p1.toKey(); |
|||
key1.compressed = false; |
|||
var key2 = p2.toKey(); |
|||
key2.compressed = false; |
|||
var pubKey = Key.addUncompressed(key1.public, key2.public); |
|||
var key = new Key(); |
|||
key.compressed = false; |
|||
key.public = pubKey; |
|||
key.compressed = true; |
|||
return Point.fromKey(key); |
|||
}; |
|||
|
|||
//convert the public key of a Key into a Point
|
|||
Point.fromKey = function(key) { |
|||
var point = new Point(); |
|||
var pubKeyBuf = new Buffer(key.public); |
|||
var key2 = new Key(); |
|||
key2.compressed = key.compressed; |
|||
key2.public = pubKeyBuf; |
|||
key2.compressed = false; |
|||
point.x = bignum.fromBuffer(key2.public.slice(1, 33), {size: 32}); |
|||
point.y = bignum.fromBuffer(key2.public.slice(33, 65), {size: 32}); |
|||
return point; |
|||
}; |
|||
|
|||
//convert the Point into the Key containing a compressed public key
|
|||
Point.prototype.toKey = function() { |
|||
var xbuf = this.x.toBuffer({size: 32}); |
|||
var ybuf = this.y.toBuffer({size: 32}); |
|||
var key = new Key(); |
|||
key.compressed = false; |
|||
var prefix = new Buffer([0x04]); |
|||
key.public = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
|||
key.compressed = true; |
|||
return key; |
|||
}; |
|||
|
|||
module.exports = require('soop')(Point); |
@ -0,0 +1,81 @@ |
|||
var chai = chai || require('chai'); |
|||
var bitcore = bitcore || require('../bitcore'); |
|||
var coinUtil = coinUtil || bitcore.util; |
|||
var buffertools = require('buffertools'); |
|||
|
|||
var should = chai.should(); |
|||
var assert = chai.assert; |
|||
|
|||
var Key = bitcore.Key; |
|||
|
|||
//addUncompressed is a node-only interface feature
|
|||
if (typeof process !== 'undefined' && process.versions) { |
|||
describe('#Key.node', function() { |
|||
describe('#addUncompressed', function() { |
|||
it('should exist', function() { |
|||
should.exist(Key.addUncompressed); |
|||
}); |
|||
|
|||
it('should add two uncompressed public keys', function() { |
|||
var key1 = Key.generateSync(); |
|||
key1.compressed = false; |
|||
var key2 = Key.generateSync(); |
|||
key2.compressed = false; |
|||
var pubkey1 = key1.public; |
|||
var pubkey2 = key2.public; |
|||
var pubkey = Key.addUncompressed(pubkey1, pubkey2); |
|||
pubkey.length.should.equal(65); |
|||
}); |
|||
|
|||
it('a + b should equal b + a', function() { |
|||
var key1 = Key.generateSync(); |
|||
key1.compressed = false; |
|||
var key2 = Key.generateSync(); |
|||
key2.compressed = false; |
|||
var pubkey1 = key1.public; |
|||
var pubkey2 = key2.public; |
|||
var r1 = Key.addUncompressed(pubkey1, pubkey2); |
|||
var r2 = Key.addUncompressed(pubkey2, pubkey1); |
|||
r1.toString('hex').should.equal(r2.toString('hex')); |
|||
}); |
|||
|
|||
it('should be able to add these two public keys without error', function() { |
|||
var key1 = new Key(); |
|||
key1.private = coinUtil.sha256("first " + 3); |
|||
key1.compressed = false; |
|||
key1.regenerateSync(); |
|||
var key2 = new Key(); |
|||
key2.private = coinUtil.sha256("second " + 3); |
|||
key2.compressed = false; |
|||
key2.regenerateSync(); |
|||
var pubkey1 = key1.public; |
|||
var pubkey2 = key2.public; |
|||
var pubkey = Key.addUncompressed(pubkey1, pubkey2); |
|||
pubkey.length.should.equal(65); |
|||
var key = new Key(); |
|||
key.public = pubkey; |
|||
assert(key.public !== null); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('node only Key functionality', function() { |
|||
it('should not fail when called as Key() without "new"', function() { |
|||
var key = Key(); |
|||
should.exist(key); |
|||
}); |
|||
it('should not fail when called as Key() without "new" with some args', function() { |
|||
var key = Key(1, 2, 3, 4, 5); |
|||
should.exist(key); |
|||
}); |
|||
it('should have correct properties when called with Key() without "new"', function() { |
|||
var key = Key(); |
|||
key.compressed.should.equal(true); |
|||
should.not.exist(key.public); |
|||
should.not.exist(key.private); |
|||
should.exist(key); |
|||
}); |
|||
|
|||
}); |
|||
}); |
|||
} |
Loading…
Reference in new issue