2014-01-11 07:57:43 +01:00
|
|
|
/* global describe, it */
|
2013-02-17 06:39:15 +01:00
|
|
|
var assert = require('assert');
|
2014-01-11 07:57:43 +01:00
|
|
|
var Address = require('../src/address.js');
|
2014-03-05 22:53:29 +01:00
|
|
|
var network = require('../src/network.js');
|
2014-01-11 07:57:43 +01:00
|
|
|
|
|
|
|
describe('Address', function() {
|
|
|
|
describe('toString', function() {
|
|
|
|
it('defaults to base58', function() {
|
|
|
|
var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ';
|
|
|
|
assert.equal((new Address(addr)).toString(), addr);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('validate', function() {
|
|
|
|
it('validates known good addresses', function() {
|
2014-03-05 22:53:29 +01:00
|
|
|
function validate(addr, expectedVersion) {
|
2014-01-11 07:57:43 +01:00
|
|
|
assert.ok(Address.validate(addr));
|
2014-03-05 22:53:29 +01:00
|
|
|
var address = new Address(addr);
|
|
|
|
assert.ok(address.version == expectedVersion);
|
2014-01-11 07:57:43 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 22:53:29 +01:00
|
|
|
validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', network.mainnet.addressVersion);
|
|
|
|
validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', network.testnet.addressVersion);
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-03-05 22:53:29 +01:00
|
|
|
validate('12KYrjTdVGjFMtaxERSk3gphreJ5US8aUP', network.mainnet.addressVersion);
|
|
|
|
validate('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y', network.mainnet.addressVersion);
|
|
|
|
validate('1oNLrsHnBcR6dpaBpwz3LSwutbUNkNSjs', network.mainnet.addressVersion);
|
|
|
|
validate('1SQHtwR5oJRKLfiWQ2APsAd9miUc4k2ez', network.mainnet.addressVersion);
|
|
|
|
validate('116CGDLddrZhMrTwhCVJXtXQpxygTT1kHd', network.mainnet.addressVersion);
|
2014-01-11 07:57:43 +01:00
|
|
|
|
|
|
|
// p2sh addresses
|
2014-03-05 22:53:29 +01:00
|
|
|
validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', network.mainnet.p2shVersion);
|
|
|
|
validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7', network.testnet.p2shVersion);
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not validate illegal examples', function() {
|
|
|
|
function invalid(addr) {
|
|
|
|
assert.ok(!Address.validate(addr));
|
|
|
|
}
|
2014-03-05 22:53:29 +01:00
|
|
|
function invalidNetwork(addr, unexpectedVersion) {
|
|
|
|
assert.ok(Address.validate(addr)); //must be a valid address itself
|
|
|
|
if(addr.length >= 34 && unexpectedVersion !== undefined) {
|
|
|
|
var address = new Address(addr);
|
|
|
|
if(unexpectedVersion !== undefined)
|
|
|
|
assert.ok(address.version != unexpectedVersion);
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 07:57:43 +01:00
|
|
|
|
2014-03-05 22:53:29 +01:00
|
|
|
invalid(''); //empty should be invalid
|
|
|
|
invalid('%%@'); // invalid base58 string
|
|
|
|
invalid('1A1zP1eP5QGefi2DzPTf2L5SLmv7DivfNz'); // bad address (doesn't checksum)
|
|
|
|
invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhe'); // bad address (doesn't checksum)
|
|
|
|
|
|
|
|
//and test for the wrong networks
|
|
|
|
invalidNetwork('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', network.mainnet.addressVersion);
|
|
|
|
invalidNetwork('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', network.testnet.addressVersion);
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|