From 4ddf3d117cf3fc9df4e16f0a74c4dcc5ebed0d37 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 12 Dec 2014 13:39:49 -0500 Subject: [PATCH] Keys: Modified and added standardized toJSON/fromJSON prototypes --- lib/privatekey.js | 21 ++++++++++++++------- lib/publickey.js | 25 +++++++++++++++++-------- test/privatekey.js | 16 ++++++++++------ test/publickey.js | 23 ++++++++--------------- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index e2a95a2..0fd2ed5 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -3,7 +3,7 @@ var Address = require('./address'); var base58check = require('./encoding/base58check'); var BN = require('./crypto/bn'); -var jsUtil = require('./util/js'); +var JSUtil = require('./util/js'); var Networks = require('./networks'); var Point = require('./crypto/point'); var PublicKey = require('./publickey'); @@ -51,7 +51,7 @@ var PrivateKey = function PrivateKey(data, network, compressed) { } else if (data instanceof Buffer || data instanceof Uint8Array) { info = PrivateKey._transformBuffer(data, network, compressed); } else if (typeof(data) === 'string'){ - if (jsUtil.isHexa(data)) { + if (JSUtil.isHexa(data)) { info.bn = BN(new Buffer(data, 'hex')); } else { info = PrivateKey._transformWIF(data, network, compressed); @@ -183,14 +183,17 @@ PrivateKey.fromWIF = function(str) { }; /** - * Instantiate a PrivateKey from a WIF JSON string + * Instantiate a PrivateKey from a JSON string * - * @param {String} str - The WIF encoded private key string + * @param {String} json - The JSON encoded private key string * @returns {PrivateKey} A new valid instance of PrivateKey */ PrivateKey.fromJSON = function(json) { - var info = PrivateKey._transformWIF(json); - return new PrivateKey(info.bn, info.network, info.compressed); + if (JSUtil.isValidJson(json)) { + json = JSON.parse(json); + } + var bn = BN(json.bn, 'hex'); + return new PrivateKey(bn, json.network, json.compressed); }; /** @@ -312,7 +315,11 @@ PrivateKey.prototype.toAddress = function() { * @returns {String} A WIF representation of the private key */ PrivateKey.prototype.toJSON = function() { - return this.toString(); + return { + bn: this.bn.toString('hex'), + compressed: this.compressed, + network: this.network.toString() + }; }; /** diff --git a/lib/publickey.js b/lib/publickey.js index d399ef9..c556e7a 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -1,8 +1,9 @@ 'use strict'; -var Point = require('./crypto/point'); -var BN = require('./crypto/bn'); var Address = require('./address'); +var BN = require('./crypto/bn'); +var Point = require('./crypto/point'); +var JSUtil = require('./util/js'); /** * @@ -157,13 +158,17 @@ PublicKey._transformX = function(odd, x){ * * Instantiate a PublicKey from JSON * - * @param {String} json - A JSON string of DER encoded public key + * @param {String} json - A JSON string * @returns {PublicKey} A new valid instance of PublicKey */ PublicKey.fromJSON = function(json) { - var buf = new Buffer(json, 'hex'); - var info = PublicKey._transformDER(buf); - return new PublicKey(info.point, info.compressed); + if (JSUtil.isValidJson(json)) { + json = JSON.parse(json); + } + var x = BN(json.x, 'hex'); + var y = BN(json.y, 'hex'); + var point = new Point(x, y); + return new PublicKey(point, json.compressed); }; /** @@ -278,10 +283,14 @@ PublicKey.isValid = function(data) { * * Will output the PublicKey to JSON * - * @returns {String} A hex encoded string + * @returns {Object} A JSON object */ PublicKey.prototype.toJSON = function() { - return this.toBuffer().toString('hex'); + return { + x: this.point.getX().toString('hex'), + y: this.point.getY().toString('hex'), + compressed: this.compressed + }; }; /** diff --git a/test/privatekey.js b/test/privatekey.js index e2cf3a4..8ba951e 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -129,11 +129,15 @@ describe('PrivateKey', function() { privkey.toString().should.equal(encmu); }); - describe('#fromJSON', function() { + describe('#json', function() { - it('should input this address correctly', function() { - var privkey = PrivateKey.fromJSON(encmu); - privkey.toWIF().should.equal(encmu); + it('should input/output json', function() { + var json = { + bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a', + compressed: false, + network: 'livenet' + }; + PrivateKey.fromJSON(json).toJSON().should.deep.equal(json); }); }); @@ -141,8 +145,8 @@ describe('PrivateKey', function() { describe('#toString', function() { it('should output this address correctly', function() { - var privkey = PrivateKey.fromJSON(encmu); - privkey.toJSON().should.equal(encmu); + var privkey = PrivateKey.fromWIF(encmu); + privkey.toString().should.equal(encmu); }); }); diff --git a/test/publickey.js b/test/publickey.js index 5184ddd..fd74f32 100644 --- a/test/publickey.js +++ b/test/publickey.js @@ -121,22 +121,15 @@ describe('PublicKey', function() { }); }); - describe('#fromJSON', function() { + describe('#json', function() { - it('should input this public key', function() { - var pk = PublicKey.fromJSON('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); - pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); - pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); - }); - - }); - - describe('#toJSON', function() { - - it('should output this pubkey', function() { - var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'; - var pk = PublicKey.fromJSON(hex); - pk.toJSON().should.equal(hex); + it('should input/ouput json', function() { + var json = { + x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', + y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', + compressed: false + }; + PublicKey.fromJSON(json).toJSON().should.deep.equal(json); }); });