Keys: Added toObject method and changed toJSON to return a string
This commit is contained in:
parent
1c0caac8c3
commit
c0bbf96dc1
@ -310,11 +310,9 @@ PrivateKey.prototype.toAddress = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Will output the PrivateKey to a WIF string
|
* @returns {Object} A plain object representation
|
||||||
*
|
|
||||||
* @returns {String} A WIF representation of the private key
|
|
||||||
*/
|
*/
|
||||||
PrivateKey.prototype.toJSON = function() {
|
PrivateKey.prototype.toObject = function toObject() {
|
||||||
return {
|
return {
|
||||||
bn: this.bn.toString('hex'),
|
bn: this.bn.toString('hex'),
|
||||||
compressed: this.compressed,
|
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
|
* Will output the PrivateKey to a WIF string
|
||||||
*
|
*
|
||||||
|
|||||||
@ -6,7 +6,6 @@ var Point = require('./crypto/point');
|
|||||||
var JSUtil = require('./util/js');
|
var JSUtil = require('./util/js');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'.
|
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
@ -137,7 +136,6 @@ PublicKey._transformDER = function(buf){
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Internal function to transform X into a public key point
|
* Internal function to transform X into a public key point
|
||||||
*
|
*
|
||||||
* @param {Boolean} odd - If the point is above or below the x axis
|
* @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
|
* Instantiate a PublicKey from JSON
|
||||||
*
|
*
|
||||||
* @param {String} json - A JSON string
|
* @param {String} json - A JSON string
|
||||||
@ -172,7 +169,6 @@ PublicKey.fromJSON = function(json) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Instantiate a PublicKey from a PrivateKey
|
* Instantiate a PublicKey from a PrivateKey
|
||||||
*
|
*
|
||||||
* @param {PrivateKey} privkey - An instance of PrivateKey
|
* @param {PrivateKey} privkey - An instance of PrivateKey
|
||||||
@ -184,7 +180,6 @@ PublicKey.fromPrivateKey = function(privkey) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Instantiate a PublicKey from a Buffer
|
* Instantiate a PublicKey from a Buffer
|
||||||
*
|
*
|
||||||
* @param {Buffer} buf - A DER hex buffer
|
* @param {Buffer} buf - A DER hex buffer
|
||||||
@ -196,7 +191,6 @@ PublicKey.fromBuffer = function(buf) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Instantiate a PublicKey from a Point
|
* Instantiate a PublicKey from a Point
|
||||||
*
|
*
|
||||||
* @param {Point} point - A Point instance
|
* @param {Point} point - A Point instance
|
||||||
@ -210,7 +204,6 @@ PublicKey.fromPoint = function(point, compressed){
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Instantiate a PublicKey from a DER Buffer
|
* Instantiate a PublicKey from a DER Buffer
|
||||||
*
|
*
|
||||||
* @param {Buffer} buf - 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
|
* Instantiate a PublicKey from a DER hex encoded string
|
||||||
*
|
*
|
||||||
* @param {String} str - A DER hex string
|
* @param {String} str - A DER hex string
|
||||||
@ -236,7 +228,6 @@ PublicKey.fromString = function(str, encoding) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Instantiate a PublicKey from an X Point
|
* Instantiate a PublicKey from an X Point
|
||||||
*
|
*
|
||||||
* @param {Boolean} odd - If the point is above or below the x axis
|
* @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
|
* Check if there would be any errors when initializing a PublicKey
|
||||||
*
|
*
|
||||||
* @param {String} data - The encoded data in various formats
|
* @param {String} data - The encoded data in various formats
|
||||||
@ -268,7 +258,6 @@ PublicKey.getValidationError = function(data) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Check if the parameters are valid
|
* Check if the parameters are valid
|
||||||
*
|
*
|
||||||
* @param {String} data - The encoded data in various formats
|
* @param {String} data - The encoded data in various formats
|
||||||
@ -280,12 +269,9 @@ PublicKey.isValid = function(data) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @returns {Object} A plain object of the PublicKey
|
||||||
* Will output the PublicKey to JSON
|
|
||||||
*
|
|
||||||
* @returns {Object} A JSON object
|
|
||||||
*/
|
*/
|
||||||
PublicKey.prototype.toJSON = function() {
|
PublicKey.prototype.toObject = function toObject() {
|
||||||
return {
|
return {
|
||||||
x: this.point.getX().toString('hex'),
|
x: this.point.getX().toString('hex'),
|
||||||
y: this.point.getY().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
|
* Will output the PublicKey to a Buffer
|
||||||
*
|
*
|
||||||
* @returns {Buffer} A DER hex encoded buffer
|
* @returns {Buffer} A DER hex encoded buffer
|
||||||
@ -305,7 +294,6 @@ PublicKey.prototype.toBuffer = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Will output the PublicKey to a DER Buffer
|
* Will output the PublicKey to a DER Buffer
|
||||||
*
|
*
|
||||||
* @returns {Buffer} A DER hex encoded 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
|
* Will return an address for the public key
|
||||||
*
|
*
|
||||||
* @returns {Address} An address generated from 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
|
* Will output the PublicKey to a DER encoded hex string
|
||||||
*
|
*
|
||||||
* @returns {String} A DER hex encoded string
|
* @returns {String} A DER hex encoded string
|
||||||
@ -359,7 +345,6 @@ PublicKey.prototype.toString = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Will return a string formatted for the console
|
* Will return a string formatted for the console
|
||||||
*
|
*
|
||||||
* @returns {String} Public key
|
* @returns {String} Public key
|
||||||
|
|||||||
@ -132,11 +132,11 @@ describe('PrivateKey', function() {
|
|||||||
describe('#json', function() {
|
describe('#json', function() {
|
||||||
|
|
||||||
it('should input/output json', function() {
|
it('should input/output json', function() {
|
||||||
var json = {
|
var json = JSON.stringify({
|
||||||
bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a',
|
bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a',
|
||||||
compressed: false,
|
compressed: false,
|
||||||
network: 'livenet'
|
network: 'livenet'
|
||||||
};
|
});
|
||||||
PrivateKey.fromJSON(json).toJSON().should.deep.equal(json);
|
PrivateKey.fromJSON(json).toJSON().should.deep.equal(json);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -124,11 +124,11 @@ describe('PublicKey', function() {
|
|||||||
describe('#json', function() {
|
describe('#json', function() {
|
||||||
|
|
||||||
it('should input/ouput json', function() {
|
it('should input/ouput json', function() {
|
||||||
var json = {
|
var json = JSON.stringify({
|
||||||
x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
|
x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
|
||||||
y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
|
y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
|
||||||
compressed: false
|
compressed: false
|
||||||
};
|
});
|
||||||
PublicKey.fromJSON(json).toJSON().should.deep.equal(json);
|
PublicKey.fromJSON(json).toJSON().should.deep.equal(json);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user