From 3e5e0057b194ccaf9f48085a6e650a3679ec267f Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 21 Feb 2014 15:19:38 -0300 Subject: [PATCH] Key tests working in the browser~!!! --- Key.js | 25 +++++++++++++++--------- browser/bitcoinjs-lib.js | 2 +- browser/eckey.js | 2 +- test/index.html | 1 - test/test.Key.js | 41 ++++++++++++++++++++++++++++++++-------- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/Key.js b/Key.js index 603ffd874..80187b550 100644 --- a/Key.js +++ b/Key.js @@ -7,23 +7,30 @@ if (process.versions) { } else { // pure js version var ECKey = require('./browser/bitcoinjs-lib.js').ECKey; - var kSpec = function() { - + var buffertools = require('buffertools'); + var kSpec = function(compressed, public, private) { + this.compressed = compressed; + this.public = public; + this.private = private; }; kSpec.generateSync = function() { var eck = new ECKey(); eck.setCompressed(true); var pub = eck.getPub(); - console.dir(eck); - console.log(pub); - return { - compressed: true, - public: new Buffer(33), - private: new Buffer(32) - }; + var ret = new this(true, new Buffer(pub), new Buffer(eck.priv.toByteArrayUnsigned())); + ret.eck = eck; + return ret; }; + + kSpec.prototype.regenerateSync = function() { + this.eck = new ECKey(buffertools.toHex(this.private)); + this.eck.setCompressed(true); + this.public = new Buffer(this.eck.getPub()); + return this; + }; + module.exports = { Key: kSpec }; diff --git a/browser/bitcoinjs-lib.js b/browser/bitcoinjs-lib.js index 3f7eb6ead..0f064d014 100644 --- a/browser/bitcoinjs-lib.js +++ b/browser/bitcoinjs-lib.js @@ -2708,7 +2708,7 @@ Bitcoin.ECKey = (function () { this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input)); } else { // Prepend zero byte to prevent interpretation as negative integer - this.priv = BigInteger.fromByteArrayUnsigned(Crypto.util.base64ToBytes(input)); + this.priv = BigInteger.fromByteArrayUnsigned(Crypto.util.hexToBytes(input)); } } this.compressed = !!ECKey.compressByDefault; diff --git a/browser/eckey.js b/browser/eckey.js index b9a695bd6..74e3e4562 100644 --- a/browser/eckey.js +++ b/browser/eckey.js @@ -20,7 +20,7 @@ Bitcoin.ECKey = (function () { this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input)); } else { // Prepend zero byte to prevent interpretation as negative integer - this.priv = BigInteger.fromByteArrayUnsigned(Crypto.util.base64ToBytes(input)); + this.priv = BigInteger.fromByteArrayUnsigned(Crypto.util.hexToBytes(input)); } } this.compressed = !!ECKey.compressByDefault; diff --git a/test/index.html b/test/index.html index ec2658f8b..fb6dce79b 100644 --- a/test/index.html +++ b/test/index.html @@ -8,7 +8,6 @@
- diff --git a/test/test.Key.js b/test/test.Key.js index 21c6b3742..1978fed18 100644 --- a/test/test.Key.js +++ b/test/test.Key.js @@ -3,6 +3,8 @@ var chai = require('chai'); var bitcore = require('../bitcore'); +var buffertools = require('buffertools'); + var should = chai.should(); var KeyModule = bitcore.KeyModule; @@ -22,14 +24,37 @@ describe('Key', function() { it('should be able to generateSync instance', function() { var k = Key.generateSync(); should.exist(k); - k.private.length.should.equal(32); - k.public.length.should.equal(33); + (k instanceof Key).should.be.ok; + }); + it('should retain some basic properties', function() { + var k = Key.generateSync(); + should.exist(k.private); + should.exist(k.public); should.exist(k.compressed); }); - + it('should have a valid public key', function() { + var k = Key.generateSync(); + k.compressed.should.be.ok; + k.public.length.should.equal(33); + k.public[0].should.be.above(1); + k.public[0].should.be.below(4); + }); + it('should have a valid private key', function() { + var k = Key.generateSync(); + k.private.length.should.equal(32); + }); + + it('should be able to regenerate from a private key', function() { + var k = Key.generateSync(); + var pkshex = 'b7dafe35d7d1aab78b53982c8ba554584518f86d50af565c98e053613c8f15e0'; + var pubhex = '02211c9570d24ba84a3ee31c8a08e93a6756b3f3beac76a4ab8d9748ca78203389'; + k.private = buffertools.fromHex(new Buffer(pkshex)); + k.regenerateSync(); + k.compressed.should.be.ok; + buffertools.toHex(k.private).should.equal(pkshex); + buffertools.toHex(k.public).should.equal(pubhex); + }); + + + }); - - - - -