diff --git a/lib/ecdsa.js b/lib/ecdsa.js index d4b6429..f6bc3d3 100644 --- a/lib/ecdsa.js +++ b/lib/ecdsa.js @@ -14,7 +14,7 @@ var ECDSA = function ECDSA(obj) { ECDSA.prototype.set = function(obj) { this.hashbuf = obj.hashbuf || this.hashbuf || undefined; - this.key = obj.key || this.key || undefined; + this.keypair = obj.keypair || this.keypair || undefined; this.sig = obj.sig || this.sig || undefined; this.k = obj.k || this.k || undefined; this.verified = obj.verified || this.verified || undefined; @@ -30,8 +30,8 @@ ECDSA.prototype.calci = function() { continue; } - if (Qprime.point.eq(this.key.pubkey.point)) { - this.sig.compressed = this.key.pubkey.compressed; + if (Qprime.point.eq(this.keypair.pubkey.point)) { + this.sig.compressed = this.keypair.pubkey.compressed; return this; } } @@ -44,8 +44,8 @@ ECDSA.prototype.fromString = function(str) { var obj = JSON.parse(str); if (obj.hashbuf) this.hashbuf = new Buffer(obj.hashbuf, 'hex'); - if (obj.key) - this.key = Keypair().fromString(obj.key); + if (obj.keypair) + this.keypair = Keypair().fromString(obj.keypair); if (obj.sig) this.sig = Signature().fromString(obj.sig); if (obj.k) @@ -117,7 +117,7 @@ ECDSA.prototype.sigError = function() { return 'Invalid hash'; try { - this.key.pubkey.validate(); + this.keypair.pubkey.validate(); } catch (e) { return 'Invalid pubkey: ' + e; } @@ -134,7 +134,7 @@ ECDSA.prototype.sigError = function() { var u1 = sinv.mul(e).mod(n); var u2 = sinv.mul(r).mod(n); - var p = Point.getG().mulAdd(u1, this.key.pubkey.point, u2); + var p = Point.getG().mulAdd(u1, this.keypair.pubkey.point, u2); if (p.isInfinity()) return 'p is infinity'; @@ -146,7 +146,7 @@ ECDSA.prototype.sigError = function() { ECDSA.prototype.sign = function() { var hashbuf = this.hashbuf; - var privkey = this.key.privkey; + var privkey = this.keypair.privkey; var k = this.k; var d = privkey.bn; @@ -166,7 +166,7 @@ ECDSA.prototype.sign = function() { var s = k.invm(N).mul(e.add(d.mul(r))).mod(N); } while (r.cmp(0) <= 0 || s.cmp(0) <= 0); - this.sig = new Signature({r: r, s: s, compressed: this.key.pubkey.compressed}); + this.sig = new Signature({r: r, s: s, compressed: this.keypair.pubkey.compressed}); return this.sig; }; @@ -179,8 +179,8 @@ ECDSA.prototype.toString = function() { var obj = {}; if (this.hashbuf) obj.hashbuf = this.hashbuf.toString('hex'); - if (this.key) - obj.key = this.key.toString(); + if (this.keypair) + obj.keypair = this.keypair.toString(); if (this.sig) obj.sig = this.sig.toString(); if (this.k) diff --git a/lib/message.js b/lib/message.js index 1cfbe3c..0536d43 100644 --- a/lib/message.js +++ b/lib/message.js @@ -16,7 +16,7 @@ var Message = function Message(obj) { Message.prototype.set = function(obj) { this.messagebuf = obj.messagebuf || this.messagebuf; - this.key = obj.key || this.key; + this.keypair = obj.keypair || this.keypair; this.sig = obj.sig || this.sig; this.address = obj.address || this.address; this.verified = typeof obj.verified !== 'undefined' ? obj.verified : this.verified; @@ -40,8 +40,8 @@ Message.magicHash = function(messagebuf) { return hashbuf; }; -Message.sign = function(messagebuf, key) { - var m = Message({messagebuf: messagebuf, key: key}); +Message.sign = function(messagebuf, keypair) { + var m = Message({messagebuf: messagebuf, keypair: keypair}); m.sign(); var sigbuf = m.sig.toCompact(); var sigstr = sigbuf.toString('base64'); @@ -60,7 +60,7 @@ Message.verify = function(messagebuf, sigstr, address) { Message.prototype.sign = function() { var hashbuf = Message.magicHash(this.messagebuf); - var ecdsa = ECDSA({hashbuf: hashbuf, key: this.key}); + var ecdsa = ECDSA({hashbuf: hashbuf, keypair: this.keypair}); ecdsa.signRandomK(); ecdsa.calci(); this.sig = ecdsa.sig; @@ -73,15 +73,15 @@ Message.prototype.verify = function() { var ecdsa = new ECDSA(); ecdsa.hashbuf = hashbuf; ecdsa.sig = this.sig; - ecdsa.key = new Keypair(); - ecdsa.key.pubkey = ecdsa.sig2pubkey(); + ecdsa.keypair = new Keypair(); + ecdsa.keypair.pubkey = ecdsa.sig2pubkey(); if (!ecdsa.verify()) { this.verified = false; return this; } - var address = Address().fromPubkey(ecdsa.key.pubkey, undefined, this.sig.compressed); + var address = Address().fromPubkey(ecdsa.keypair.pubkey, undefined, this.sig.compressed); //TODO: what if livenet/testnet mismatch? if (address.hashbuf.toString('hex') === this.address.hashbuf.toString('hex')) this.verified = true; diff --git a/test/ecdsa.js b/test/ecdsa.js index 0a9bff9..c285a4d 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -16,9 +16,9 @@ describe("ECDSA", function() { var ecdsa = new ECDSA(); ecdsa.hashbuf = Hash.sha256(new Buffer('test data')); - ecdsa.key = new Keypair(); - ecdsa.key.privkey = new Privkey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))}); - ecdsa.key.pubkey = new Pubkey({ + ecdsa.keypair = new Keypair(); + ecdsa.keypair.privkey = new Privkey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))}); + ecdsa.keypair.pubkey = new Pubkey({ point: point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')), BN().fromBuffer(new Buffer('4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380', 'hex'))) }); @@ -45,10 +45,10 @@ describe("ECDSA", function() { var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10); var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10); var ecdsa = new ECDSA(); - ecdsa.key = new Keypair(); - ecdsa.key.privkey = Privkey(); - ecdsa.key.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test'))); - ecdsa.key.privkey2pubkey(); + ecdsa.keypair = new Keypair(); + ecdsa.keypair.privkey = Privkey(); + ecdsa.keypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test'))); + ecdsa.keypair.privkey2pubkey(); ecdsa.hashbuf = hashbuf; ecdsa.sig = new Signature({r: r, s: s}); @@ -65,7 +65,7 @@ describe("ECDSA", function() { var ecdsa2 = new ECDSA(); ecdsa2.fromString(str); should.exist(ecdsa.hashbuf); - should.exist(ecdsa.key); + should.exist(ecdsa.keypair); }); }); @@ -96,7 +96,7 @@ describe("ECDSA", function() { ecdsa.sign(); ecdsa.sig.i = 1; var pubkey = ecdsa.sig2pubkey(); - pubkey.point.eq(ecdsa.key.pubkey.point).should.equal(true); + pubkey.point.eq(ecdsa.keypair.pubkey.point).should.equal(true); }); }); @@ -119,8 +119,8 @@ describe("ECDSA", function() { ecdsa.hashbuf = Hash.sha256(new Buffer('test')); var pk = new Pubkey(); pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex')); - ecdsa.key = new Keypair(); - ecdsa.key.pubkey = pk; + ecdsa.keypair = new Keypair(); + ecdsa.keypair.pubkey = pk; ecdsa.sig = new Signature(); ecdsa.sig.r = BN(0); ecdsa.sig.s = BN(0); diff --git a/test/message.js b/test/message.js index f90e5f6..7138271 100644 --- a/test/message.js +++ b/test/message.js @@ -26,27 +26,27 @@ describe('Message', function() { describe('@sign', function() { var messagebuf = new Buffer('this is my message'); - var key = Keypair().fromRandom(); + var keypair = Keypair().fromRandom(); it('should return a base64 string', function() { - var sigstr = Message.sign(messagebuf, key); + var sigstr = Message.sign(messagebuf, keypair); var sigbuf = new Buffer(sigstr, 'base64'); sigbuf.length.should.equal(1 + 32 + 32); }); it('should sign with a compressed pubkey', function() { - var key = Keypair().fromRandom(); - key.pubkey.compressed = true; - var sigstr = Message.sign(messagebuf, key); + var keypair = Keypair().fromRandom(); + keypair.pubkey.compressed = true; + var sigstr = Message.sign(messagebuf, keypair); var sigbuf = new Buffer(sigstr, 'base64'); sigbuf[0].should.be.above(27 + 4 - 1); sigbuf[0].should.be.below(27 + 4 + 4 - 1); }); it('should sign with an uncompressed pubkey', function() { - var key = Keypair().fromRandom(); - key.pubkey.compressed = false; - var sigstr = Message.sign(messagebuf, key); + var keypair = Keypair().fromRandom(); + keypair.pubkey.compressed = false; + var sigstr = Message.sign(messagebuf, keypair); var sigbuf = new Buffer(sigstr, 'base64'); sigbuf[0].should.be.above(27 - 1); sigbuf[0].should.be.below(27 + 4 - 1); @@ -56,11 +56,11 @@ describe('Message', function() { describe('@verify', function() { var messagebuf = new Buffer('this is my message'); - var key = Keypair().fromRandom(); + var keypair = Keypair().fromRandom(); it('should verify a signed message', function() { - var sigstr = Message.sign(messagebuf, key); - var addr = Address().fromPubkey(key.pubkey); + var sigstr = Message.sign(messagebuf, keypair); + var addr = Address().fromPubkey(keypair.pubkey); Message.verify(messagebuf, sigstr, addr).should.equal(true); }); @@ -75,12 +75,12 @@ describe('Message', function() { describe('#sign', function() { var messagebuf = new Buffer('this is my message'); - var key = Keypair().fromRandom(); + var keypair = Keypair().fromRandom(); it('should sign a message', function() { var message = new Message(); message.messagebuf = messagebuf; - message.key = key; + message.keypair = keypair; message.sign(); var sig = message.sig; should.exist(sig); @@ -90,13 +90,13 @@ describe('Message', function() { describe('#verify', function() { var messagebuf = new Buffer('this is my message'); - var key = Keypair().fromRandom(); + var keypair = Keypair().fromRandom(); it('should verify a message that was just signed', function() { var message = new Message(); message.messagebuf = messagebuf; - message.key = key; - message.address = Address().fromPubkey(key.pubkey); + message.keypair = keypair; + message.address = Address().fromPubkey(keypair.pubkey); message.sign(); message.verify(); message.verified.should.equal(true);