Browse Source

Keys: Added toObject method and changed toJSON to return a string

patch-2
Braydon Fuller 10 years ago
parent
commit
c0bbf96dc1
  1. 10
      lib/privatekey.js
  2. 27
      lib/publickey.js
  3. 4
      test/privatekey.js
  4. 4
      test/publickey.js

10
lib/privatekey.js

@ -310,11 +310,9 @@ PrivateKey.prototype.toAddress = function() {
};
/**
* Will output the PrivateKey to a WIF string
*
* @returns {String} A WIF representation of the private key
* @returns {Object} A plain object representation
*/
PrivateKey.prototype.toJSON = function() {
PrivateKey.prototype.toObject = function toObject() {
return {
bn: this.bn.toString('hex'),
compressed: this.compressed,
@ -322,6 +320,10 @@ PrivateKey.prototype.toJSON = function() {
};
};
PrivateKey.prototype.toJSON = function toJSON() {
return JSON.stringify(this.toObject());
};
/**
* Will output the PrivateKey to a WIF string
*

27
lib/publickey.js

@ -6,7 +6,6 @@ var Point = require('./crypto/point');
var JSUtil = require('./util/js');
/**
*
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'.
*
* @example
@ -137,7 +136,6 @@ PublicKey._transformDER = function(buf){
};
/**
*
* Internal function to transform X into a public key point
*
* @param {Boolean} odd - If the point is above or below the x axis
@ -155,7 +153,6 @@ PublicKey._transformX = function(odd, x){
};
/**
*
* Instantiate a PublicKey from JSON
*
* @param {String} json - A JSON string
@ -172,7 +169,6 @@ PublicKey.fromJSON = function(json) {
};
/**
*
* Instantiate a PublicKey from a PrivateKey
*
* @param {PrivateKey} privkey - An instance of PrivateKey
@ -184,7 +180,6 @@ PublicKey.fromPrivateKey = function(privkey) {
};
/**
*
* Instantiate a PublicKey from a Buffer
*
* @param {Buffer} buf - A DER hex buffer
@ -196,7 +191,6 @@ PublicKey.fromBuffer = function(buf) {
};
/**
*
* Instantiate a PublicKey from a Point
*
* @param {Point} point - A Point instance
@ -210,7 +204,6 @@ PublicKey.fromPoint = function(point, compressed){
};
/**
*
* Instantiate a PublicKey from a DER Buffer
*
* @param {Buffer} buf - A DER Buffer
@ -222,7 +215,6 @@ PublicKey.fromDER = function(buf) {
};
/**
*
* Instantiate a PublicKey from a DER hex encoded string
*
* @param {String} str - A DER hex string
@ -236,7 +228,6 @@ PublicKey.fromString = function(str, encoding) {
};
/**
*
* Instantiate a PublicKey from an X Point
*
* @param {Boolean} odd - If the point is above or below the x axis
@ -250,7 +241,6 @@ PublicKey.fromX = function(odd, x) {
/**
*
* Check if there would be any errors when initializing a PublicKey
*
* @param {String} data - The encoded data in various formats
@ -268,7 +258,6 @@ PublicKey.getValidationError = function(data) {
};
/**
*
* Check if the parameters are valid
*
* @param {String} data - The encoded data in various formats
@ -280,12 +269,9 @@ PublicKey.isValid = function(data) {
};
/**
*
* Will output the PublicKey to JSON
*
* @returns {Object} A JSON object
* @returns {Object} A plain object of the PublicKey
*/
PublicKey.prototype.toJSON = function() {
PublicKey.prototype.toObject = function toObject() {
return {
x: this.point.getX().toString('hex'),
y: this.point.getY().toString('hex'),
@ -293,8 +279,11 @@ PublicKey.prototype.toJSON = function() {
};
};
PublicKey.prototype.toJSON = function toJSON(){
return JSON.stringify(this.toObject());
};
/**
*
* Will output the PublicKey to a Buffer
*
* @returns {Buffer} A DER hex encoded buffer
@ -305,7 +294,6 @@ PublicKey.prototype.toBuffer = function() {
};
/**
*
* Will output the PublicKey to a DER Buffer
*
* @returns {Buffer} A DER hex encoded buffer
@ -338,7 +326,6 @@ PublicKey.prototype.toDER = function(compressed) {
};
/**
*
* Will return an address for the public key
*
* @returns {Address} An address generated from the public key
@ -348,7 +335,6 @@ PublicKey.prototype.toAddress = function(network) {
};
/**
*
* Will output the PublicKey to a DER encoded hex string
*
* @returns {String} A DER hex encoded string
@ -359,7 +345,6 @@ PublicKey.prototype.toString = function() {
};
/**
*
* Will return a string formatted for the console
*
* @returns {String} Public key

4
test/privatekey.js

@ -132,11 +132,11 @@ describe('PrivateKey', function() {
describe('#json', function() {
it('should input/output json', function() {
var json = {
var json = JSON.stringify({
bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a',
compressed: false,
network: 'livenet'
};
});
PrivateKey.fromJSON(json).toJSON().should.deep.equal(json);
});

4
test/publickey.js

@ -124,11 +124,11 @@ describe('PublicKey', function() {
describe('#json', function() {
it('should input/ouput json', function() {
var json = {
var json = JSON.stringify({
x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
compressed: false
};
});
PublicKey.fromJSON(json).toJSON().should.deep.equal(json);
});

Loading…
Cancel
Save