add Signature(r, s) convenience

This commit is contained in:
Ryan X. Charles 2014-09-01 21:08:16 -07:00
parent 8a3d71b596
commit ef3a89f254
2 changed files with 24 additions and 7 deletions

View File

@ -2,11 +2,19 @@ var BN = require('./bn');
var Point = require('./point');
var Pubkey = require('./pubkey');
var Signature = function Signature(obj) {
var Signature = function Signature(r, s) {
if (!(this instanceof Signature))
return new Signature(obj);
if (obj)
if (r instanceof BN) {
this.set({
r: r,
s: s
});
}
else if (r) {
var obj = r;
this.set(obj);
}
};
Signature.prototype.set = function(obj) {

View File

@ -1,4 +1,4 @@
var bn = require('../lib/bn');
var BN = require('../lib/bn');
var should = require('chai').should();
var Signature = require('../lib/signature');
@ -9,6 +9,15 @@ describe('Signature', function() {
should.exist(sig);
});
it('should work with conveniently setting r, s', function() {
var r = BN();
var s = BN();
var sig = new Signature(r, s);
should.exist(sig);
sig.r.toString().should.equal(r.toString());
sig.s.toString().should.equal(s.toString());
});
describe('#set', function() {
it('should set compressed', function() {
@ -116,8 +125,8 @@ describe('Signature', function() {
describe('#toDER', function() {
it('should convert these known r and s values into a known signature', function() {
var r = bn('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = bn('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature({r: r, s: s});
var der = sig.toDER(r, s);
der.toString('hex').should.equal('30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72');
@ -128,8 +137,8 @@ describe('Signature', function() {
describe('#toString', function() {
it('should convert this signature in to hex DER', function() {
var r = bn('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = bn('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature({r: r, s: s});
var hex = sig.toString();
hex.should.equal('30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72');