From 14748ae2b5b7ae8ec7d46183c3cadbe074d8bd9c Mon Sep 17 00:00:00 2001 From: Eric Martindale Date: Mon, 6 Oct 2014 17:54:20 -0400 Subject: [PATCH] Make more tests pass with Identity(). --- examples/identity.js | 5 ++++- lib/keypair.js | 8 +++++--- test/identity.js | 23 +++++++++++------------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/examples/identity.js b/examples/identity.js index 25fc992e4..7990fd95f 100644 --- a/examples/identity.js +++ b/examples/identity.js @@ -1,10 +1,13 @@ var Identity = require('../lib/identity'); var KeyPair = require('../lib/keypair'); +var Hash = require('../lib/hash'); var keypair = new KeyPair().fromRandom(); console.log( 'keypair:' , keypair ); -console.log( 'public key:' , keypair.pubkey.toString() ); +console.log( 'private key:' , keypair.privkey.toString('hex') ); +console.log( 'public key:' , keypair.pubkey.toString('hex') ); +console.log( 'public hash:' , Hash.sha256ripemd160( keypair.pubkey.toBuffer() ).toString('hex') ); var identity = new Identity().fromPubkey( keypair.pubkey ); diff --git a/lib/keypair.js b/lib/keypair.js index 1ba511dd9..037547c1e 100644 --- a/lib/keypair.js +++ b/lib/keypair.js @@ -6,11 +6,13 @@ var point = require('./point'); var Keypair = function Keypair(obj) { if (!(this instanceof Keypair)) return new Keypair(obj); - - if (!obj) { + + // breaks some tests + // TODO: allow keys to be created with simply `new Keypair()` (random gen.) + /*if (!obj) { var privkey = Privkey().fromRandom(); var obj = this.fromPrivkey( privkey ); - } + }*/ if (obj) this.set(obj); diff --git a/test/identity.js b/test/identity.js index 6fbf0be1b..8246df044 100644 --- a/test/identity.js +++ b/test/identity.js @@ -5,19 +5,18 @@ var Identity = require('../lib/identity'); describe('Identity', function() { - var knownPrivKey = 'L1KL3xHiuBF9YuBKTZMorW6TVDG2QW9UHWdSFEzcVFpLuGxTe9bQ'; - var knownPubKey = '026006aa5fd6e800e6b529258dbb73b531da735a863c87e6673bf96def1372a59e'; - var knownIdent = 'Tf8vkF9HPCDbNcLoFHk8ENAwJLQMVmWRz5P'; + var knownPrivKey = 'L3qq9TMqq2Wfax48vTsywEtCTCTbr18PuMwkvpLUseCJPBhGhAJj'; + var knownPubKey = '03663eadf291f099c7c87cf04dc037c49b4d168ff67a0f86d95fa77e525af4dee6'; + var knownPubKeyHash = 'c3f89d708d24c0eaf5953d103b95dfec87ab66f6'; + var knownIdent = 'TfFQdYg2BmLVWew1faUytoG3EpZZ8ebTqHr'; - //var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); - var pubkey = '0348dc031a1499c455eeb593407e0505dfbd95a88411fa78e8cab9cbe89d064048'; - var key = new PubKey(); - key.fromString( pubkey ); + var pubkeyhash = new Buffer( knownPubKeyHash , 'hex'); //var buf = Buffer.concat([ new Buffer([0]), new Buffer([0]), pubkeyhash ]); // note: key is wrong string until I figure out how to duplicate the generation of short keys - var buf = Buffer.concat([ new Buffer( 0x0f ) , new Buffer( 0x02 ) , new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex') ]) - var str = 'Tf3sWHK314o6hpeUDHpqu8RAxPypoAinbDg'; + //var buf = Buffer.concat([ new Buffer( 0x0f ) , new Buffer( 0x02 ) , pubkeyhash ]) + var buf = Buffer.concat([ new Buffer([0x0f]) , new Buffer([0x02]) , pubkeyhash ]) + var str = knownIdent; it('should create a new identity object', function() { var identity = new Identity(); @@ -77,7 +76,7 @@ describe('Identity', function() { it('should make this identity from an uncompressed pubkey', function() { var pubkey = new PubKey(); - pubkey.fromDER(new Buffer( knownPubKey , 'hex')); + pubkey.fromDER(new Buffer( knownPubKeyUncomp , 'hex')); var identity = new Identity(); pubkey.compressed = false; identity.fromPubkey(pubkey, 'ephemeral'); @@ -91,7 +90,7 @@ describe('Identity', function() { it('should derive from this known ephemeral identity string', function() { var identity = new Identity(); identity.fromString( str ); - identity.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex')); + identity.toBuffer().slice(2).toString('hex').should.equal(pubkeyhash.toString('hex')); }); }); @@ -125,7 +124,7 @@ describe('Identity', function() { it('should output this known hash', function() { var identity = new Identity(); identity.fromString(str); - identity.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex')); + identity.toBuffer().slice(2).toString('hex').should.equal(pubkeyhash.toString('hex')); }); });