olalonde
11 years ago
21 changed files with 68 additions and 85 deletions
@ -1,5 +0,0 @@ |
|||
if (process.versions) { |
|||
module.exports = require('bignum'); |
|||
return; |
|||
} |
|||
module.exports = require('./browser/Bignum'); |
@ -1,5 +1 @@ |
|||
if (process.versions) { |
|||
module.exports = require('./node/Key'); |
|||
return; |
|||
} |
|||
module.exports = require('./browser/Key'); |
|||
module.exports = require('bindings')('KeyModule').Key; |
|||
|
@ -1,5 +1,39 @@ |
|||
if (process.versions) { |
|||
module.exports = require('./node/Point'); |
|||
return; |
|||
} |
|||
module.exports = require('./browser/Point'); |
|||
"use strict"; |
|||
|
|||
var imports = require('soop').imports(); |
|||
var bignum = imports.bignum || require('bignum'); |
|||
var CPPKey = imports.CPPKey || require('bindings')('KeyModule').Key; |
|||
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 u1 = p1.toUncompressedPubKey(); |
|||
var u2 = p2.toUncompressedPubKey(); |
|||
var pubKey = CPPKey.addUncompressed(u1, u2); |
|||
return Point.fromUncompressedPubKey(pubKey); |
|||
}; |
|||
|
|||
//convert the public key of a Key into a Point
|
|||
Point.fromUncompressedPubKey = function(pubkey) { |
|||
var point = new Point(); |
|||
point.x = bignum.fromBuffer(pubkey.slice(1, 33), {size: 32}); |
|||
point.y = bignum.fromBuffer(pubkey.slice(33, 65), {size: 32}); |
|||
return point; |
|||
}; |
|||
|
|||
//convert the Point into the Key containing a compressed public key
|
|||
Point.prototype.toUncompressedPubKey = function() { |
|||
var xbuf = this.x.toBuffer({size: 32}); |
|||
var ybuf = this.y.toBuffer({size: 32}); |
|||
var prefix = new Buffer([0x04]); |
|||
var pubkey = Buffer.concat([prefix, xbuf, ybuf]); |
|||
return pubkey; |
|||
}; |
|||
|
|||
module.exports = require('soop')(Point); |
|||
|
@ -1,5 +1,10 @@ |
|||
if (process.versions) { |
|||
module.exports = require('./node/SecureRandom'); |
|||
return; |
|||
var imports = require('soop'); |
|||
var crypto = imports.crypto || require('crypto'); |
|||
|
|||
var SecureRandom = require('./common/SecureRandom'); |
|||
|
|||
SecureRandom.getRandomBuffer = function(size) { |
|||
return crypto.randomBytes(size); |
|||
} |
|||
module.exports = require('./browser/SecureRandom'); |
|||
|
|||
module.exports = require('soop')(SecureRandom); |
|||
|
@ -1,3 +0,0 @@ |
|||
var Key = require('bindings')('KeyModule').Key; |
|||
|
|||
module.exports = Key; |
@ -1,39 +0,0 @@ |
|||
"use strict"; |
|||
|
|||
var imports = require('soop').imports(); |
|||
var bignum = imports.bignum || require('../Bignum'); |
|||
var CPPKey = imports.CPPKey || require('bindings')('KeyModule').Key; |
|||
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 u1 = p1.toUncompressedPubKey(); |
|||
var u2 = p2.toUncompressedPubKey(); |
|||
var pubKey = CPPKey.addUncompressed(u1, u2); |
|||
return Point.fromUncompressedPubKey(pubKey); |
|||
}; |
|||
|
|||
//convert the public key of a Key into a Point
|
|||
Point.fromUncompressedPubKey = function(pubkey) { |
|||
var point = new Point(); |
|||
point.x = bignum.fromBuffer(pubkey.slice(1, 33), {size: 32}); |
|||
point.y = bignum.fromBuffer(pubkey.slice(33, 65), {size: 32}); |
|||
return point; |
|||
}; |
|||
|
|||
//convert the Point into the Key containing a compressed public key
|
|||
Point.prototype.toUncompressedPubKey = function() { |
|||
var xbuf = this.x.toBuffer({size: 32}); |
|||
var ybuf = this.y.toBuffer({size: 32}); |
|||
var prefix = new Buffer([0x04]); |
|||
var pubkey = Buffer.concat([prefix, xbuf, ybuf]); |
|||
return pubkey; |
|||
}; |
|||
|
|||
module.exports = require('soop')(Point); |
@ -1,10 +0,0 @@ |
|||
var imports = require('soop'); |
|||
var crypto = imports.crypto || require('crypto'); |
|||
|
|||
var SecureRandom = require('../common/SecureRandom'); |
|||
|
|||
SecureRandom.getRandomBuffer = function(size) { |
|||
return crypto.randomBytes(size); |
|||
} |
|||
|
|||
module.exports = require('soop')(SecureRandom); |
Loading…
Reference in new issue