Merge pull request #728 from braydonf/feature/keys-standardized-interface

Keys: Modified and added standardized toJSON/fromJSON prototypes
This commit is contained in:
Yemel Jardi 2014-12-12 16:04:52 -03:00
commit 15ba6e2ba1
4 changed files with 49 additions and 36 deletions

View File

@ -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()
};
};
/**

View File

@ -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
};
};
/**

View File

@ -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);
});
});

View File

@ -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);
});
});