From 5c7149aeab60c5b5103e5521a99ac6be02c8d550 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 22 Sep 2014 14:45:41 -0700 Subject: [PATCH] Stealthkey toJSON/fromJSON --- lib/expmt/stealthkey.js | 15 +++++++++++++++ test/stealthkey.js | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/expmt/stealthkey.js b/lib/expmt/stealthkey.js index 7cf49bd..7cf4d0f 100644 --- a/lib/expmt/stealthkey.js +++ b/lib/expmt/stealthkey.js @@ -27,6 +27,21 @@ Stealthkey.prototype.set = function(obj) { return this; }; +Stealthkey.prototype.fromJSON = function(json) { + this.set({ + payloadKeypair: Keypair().fromJSON(json.payloadKeypair), + scanKeypair: Keypair().fromJSON(json.scanKeypair) + }); + return this; +}; + +Stealthkey.prototype.toJSON = function() { + return { + payloadKeypair: this.payloadKeypair.toJSON(), + scanKeypair: this.scanKeypair.toJSON() + }; +}; + Stealthkey.prototype.fromRandom = function() { this.payloadKeypair = Keypair().fromRandom(); this.scanKeypair = Keypair().fromRandom(); diff --git a/test/stealthkey.js b/test/stealthkey.js index 7f2d8a7..17eed96 100644 --- a/test/stealthkey.js +++ b/test/stealthkey.js @@ -49,6 +49,30 @@ describe('Stealthkey', function() { }); + describe('#fromJSON', function() { + + it('should make a stealthkey from this JSON', function() { + var sk = Stealthkey().fromJSON({ + payloadKeypair: stealthkey.payloadKeypair.toJSON(), + scanKeypair: stealthkey.scanKeypair.toJSON() + }); + sk.payloadKeypair.toString().should.equal(stealthkey.payloadKeypair.toString()); + sk.scanKeypair.toString().should.equal(stealthkey.scanKeypair.toString()); + }); + + }); + + describe('#toJSON', function() { + + it('should convert this stealthkey to json', function() { + var json = stealthkey.toJSON() + var json2 = Stealthkey().fromJSON(json).toJSON(); + json.payloadKeypair.privkey.should.equal(json2.payloadKeypair.privkey); + json.scanKeypair.privkey.should.equal(json2.scanKeypair.privkey); + }); + + }); + describe('#fromRandom', function() { it('should create a new stealthkey from random', function() {