Browse Source

Merge pull request #450 from matiu/feature/sin03

Feature/sin03
patch-2
Ryan X. Charles 11 years ago
parent
commit
0f737b4f00
  1. 2
      lib/Address.js
  2. 3
      lib/SIN.js
  3. 26
      test/test.Address.js
  4. 27
      test/test.SIN.js

2
lib/Address.js

@ -38,7 +38,7 @@ var Script = require('./Script');
var util = require('util');
function Address(version, hash) {
if (hash && hash.length && hash.length != 20)
if (hash && hash.length && (!Buffer.isBuffer(hash) || hash.length != 20))
throw new Error('Hash must be 20 bytes');
Address.super_.call(this, version, hash);
}

3
lib/SIN.js

@ -9,6 +9,9 @@ function SIN(type, payload) {
SIN.super_.call(this, type, payload);
return;
}
if (!Buffer.isBuffer(payload) || payload.length != 20)
throw new Error('Payload must be 20 bytes');
this.data = new Buffer(1 + 1 + payload.length);
this.converters = this.encodings['binary'].converters;
this._encoding = this.encodings['binary']._encoding;

26
test/test.Address.js

@ -102,11 +102,28 @@ describe('Address', function() {
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var f = function() {new bitcore.Address(0, key.public);};
var f = function() {
new bitcore.Address(0, key.public);
};
expect(f).to.throw(Error);
});
});
describe('constructor, 2 params', function() {
it('should make an address from a version, hash', function() {
var hash = new Buffer('1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex');
var addr = new Address(0, hash);
addr.toString().should.equal('13SE7uKmnQwGA8X1A8WcZnX2ceQRDEzsAd');
});
it('should fail with param version, string', function() {
var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0';
(function() {
var addr = new Address(0, hash);
}).should.throw();
});
});
describe('#fromPubKey', function() {
it('should make pubkeyhash address from an uncompressed public key', function() {
var pubkey = new Buffer('04fa05ce8b25010cb6e17a30e0b66668bf083c40687547748ec330ee77adf53a42abd3d26148cbacfcf79c907ddefeb2c37f8bebc0a695ba79d634449d871de218', 'hex');
@ -134,7 +151,9 @@ describe('Address', function() {
var pubkey3 = new Buffer('032c0d2e394541e2efdc7ac3500e16e7e69df541f38670402e95aa477202fa06bb', 'hex');
var sortedPubKeys = [pubkey3, pubkey2, pubkey1];
var mReq = 2;
var script = bitcore.Script.createMultisig(mReq, sortedPubKeys, {noSorting: true});
var script = bitcore.Script.createMultisig(mReq, sortedPubKeys, {
noSorting: true
});
var hash = bitcore.util.sha256ripe160(script.getBuffer());
var version = bitcore.networks['livenet'].P2SHVersion;
var addr = new Address(version, hash);
@ -221,8 +240,7 @@ describe('Address', function() {
(new Address.fromScriptPubKey(c[0], 'testnet')).toString().should.equal(c[1]);
var s = new bitcore.Script(new Buffer(c[0], 'hex'));
(new Address.fromScriptPubKey(s, 'testnet')).toString().should.equal(c[1]);
}
else {
} else {
var as = new Address.fromScriptPubKey(c[0], 'testnet');
for (var j in as) {
as[j].toString().should.equal(c[1][j]);

27
test/test.SIN.js

@ -37,17 +37,35 @@ describe('SIN', function() {
describe('#SIN', function() {
it('should be able to create a new SIN with a version byte', function() {
var myhash = bitcore.util.sha256ripe160('test');
var myhash = new Buffer('1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex');
var sin = new SIN(SIN.SIN_EPHEM, myhash);
should.exist(sin);
});
it('should fail with param version, string', function() {
var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0';
(function() {
var sin = new SIN(SIN.SIN_EPHEM, hash);
}).should.throw();
});
it('should fail with wrong sized hash', function() {
var myhash = new Buffer('111ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex');
(function() {
var sin = new SIN(SIN.SIN_EPHEM, myhash);
}).should.throw();
});
});
describe('#fromPubKey', function() {
it('should fail to create a new SIN not using a pub key', function() {
(function() { SIN.fromPubKey('1234')}).should.throw();
(function() {
SIN.fromPubKey('1234')
}).should.throw();
});
it('should fail to create a new SIN not using a pub key case 2', function() {
(function() { SIN.fromPubKey('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27')}).should.throw();
(function() {
SIN.fromPubKey('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27')
}).should.throw();
});
it('should be able to create a new SIN using a pub key', function() {
var pubkey1 = new Buffer('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27', 'hex');
@ -58,6 +76,3 @@ describe('SIN', function() {
});
});

Loading…
Cancel
Save