From d6e3266179c65ad9cbc30c29f24bd8b3115f20bd Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 22 Sep 2014 13:55:40 -0700 Subject: [PATCH] toJSON/fromJSON for privkey, pubkey, keypair --- lib/keypair.js | 17 +++++++++++++++++ lib/privkey.js | 9 +++++++++ lib/pubkey.js | 9 +++++++++ test/keypair.js | 28 ++++++++++++++++++++++++++++ test/privkey.js | 20 ++++++++++++++++++++ test/pubkey.js | 21 +++++++++++++++++++++ 6 files changed, 104 insertions(+) diff --git a/lib/keypair.js b/lib/keypair.js index 1515769..bc3adf8 100644 --- a/lib/keypair.js +++ b/lib/keypair.js @@ -16,6 +16,23 @@ Keypair.prototype.set = function(obj) { return this; }; +Keypair.prototype.fromJSON = function(json) { + if (json.privkey) + this.set({privkey: Privkey().fromJSON(json.privkey)}); + if (json.pubkey) + this.set({pubkey: Pubkey().fromJSON(json.pubkey)}); + return this; +}; + +Keypair.prototype.toJSON = function() { + var json = {}; + if (this.privkey) + json.privkey = this.privkey.toJSON(); + if (this.pubkey) + json.pubkey = this.pubkey.toJSON(); + return json; +}; + Keypair.prototype.fromPrivkey = function(privkey) { this.privkey = privkey; this.privkey2pubkey(); diff --git a/lib/privkey.js b/lib/privkey.js index aa107a1..12f0b7c 100644 --- a/lib/privkey.js +++ b/lib/privkey.js @@ -22,6 +22,15 @@ Privkey.prototype.set = function(obj) { return this; }; +Privkey.prototype.fromJSON = function(json) { + this.fromString(json); + return this; +}; + +Privkey.prototype.toJSON = function() { + return this.toString(); +}; + Privkey.prototype.fromRandom = function() { do { var privbuf = Random.getRandomBuffer(32); diff --git a/lib/pubkey.js b/lib/pubkey.js index 4996247..a2db155 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -21,6 +21,15 @@ Pubkey.prototype.set = function(obj) { return this; }; +Pubkey.prototype.fromJSON = function(json) { + this.fromBuffer(new Buffer(json, 'hex')); + return this; +}; + +Pubkey.prototype.toJSON = function() { + return this.toBuffer().toString('hex'); +}; + Pubkey.prototype.fromPrivkey = function(privkey) { this.set({ point: Point.getG().mul(privkey.bn), diff --git a/test/keypair.js b/test/keypair.js index 5e9dd01..f6dbe61 100644 --- a/test/keypair.js +++ b/test/keypair.js @@ -29,6 +29,34 @@ describe('Keypair', function() { }); + describe('#fromJSON', function() { + + it('should make a keypair from this json', function() { + var privkey = Privkey().fromRandom(); + var pubkey = Pubkey().fromPrivkey(privkey); + var keypair = Keypair().fromJSON({ + privkey: privkey.toJSON(), + pubkey: pubkey.toJSON() + }) + keypair.privkey.toString().should.equal(privkey.toString()); + keypair.pubkey.toString().should.equal(pubkey.toString()); + }); + + }); + + describe('#toJSON', function() { + + it('should make json from this keypair', function() { + var json = Keypair().fromRandom().toJSON(); + should.exist(json.privkey); + should.exist(json.pubkey); + var keypair = Keypair().fromJSON(json); + keypair.toJSON().privkey.toString().should.equal(json.privkey.toString()); + keypair.toJSON().pubkey.toString().should.equal(json.pubkey.toString()); + }); + + }); + describe("#fromPrivkey", function() { it('should make a new key from a privkey', function() { diff --git a/test/privkey.js b/test/privkey.js index f9cb48e..745e72b 100644 --- a/test/privkey.js +++ b/test/privkey.js @@ -46,6 +46,26 @@ describe('Privkey', function() { }); + describe('#fromJSON', function() { + + it('should input this address correctly', function() { + var privkey = new Privkey(); + privkey.fromJSON(encmu); + privkey.toWIF().should.equal(encmu); + }); + + }); + + describe('#toString', function() { + + it('should output this address correctly', function() { + var privkey = new Privkey(); + privkey.fromJSON(encmu); + privkey.toJSON().should.equal(encmu); + }); + + }); + describe('#fromRandom', function() { it('should set bn gt 0 and lt n, and should be compressed', function() { diff --git a/test/pubkey.js b/test/pubkey.js index f3e3c1f..bc4f167 100644 --- a/test/pubkey.js +++ b/test/pubkey.js @@ -32,6 +32,27 @@ describe('Pubkey', function() { }); + describe('#fromJSON', function() { + + it('should input this public key', function() { + var pk = new Pubkey(); + pk.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 pk = new Pubkey(); + var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'; + pk.fromJSON(hex).toJSON().should.equal(hex); + }); + + }); + describe('#fromPrivkey', function() { it('should make a public key from a privkey', function() {