Daniel Cousens
11 years ago
9 changed files with 158 additions and 187 deletions
@ -1,64 +1,27 @@ |
|||
var base58 = require('./base58') |
|||
var assert = require('assert') |
|||
var base58check = require('./base58check') |
|||
var convert = require('./convert') |
|||
var bitcoin = require('./network').bitcoin.pubKeyHash |
|||
|
|||
function Address(bytes, version) { |
|||
if (!(this instanceof Address)) { |
|||
return new Address(bytes, version) |
|||
} |
|||
function Address(hash, version) { |
|||
assert(Buffer.isBuffer(hash), 'First argument must be a Buffer') |
|||
assert.strictEqual(hash.length, 20, 'Invalid hash length') |
|||
assert.strictEqual(version & 0xFF, version, 'Invalid version byte') |
|||
|
|||
if (bytes instanceof Address) { |
|||
this.hash = bytes.hash |
|||
this.version = bytes.version |
|||
} |
|||
else if (typeof bytes === 'string') { |
|||
if (bytes.length <= 35) { |
|||
var decode = base58check.decode(bytes) |
|||
|
|||
this.hash = decode.payload |
|||
this.version = decode.version |
|||
} |
|||
else if (bytes.length <= 40) { |
|||
this.hash = convert.hexToBytes(bytes) |
|||
this.version = version || bitcoin |
|||
} |
|||
else { |
|||
throw new Error('Invalid or unrecognized input') |
|||
} |
|||
} |
|||
else { |
|||
this.hash = bytes |
|||
this.version = version || bitcoin |
|||
} |
|||
this.hash = hash |
|||
this.version = version |
|||
} |
|||
|
|||
/** |
|||
* Serialize this object as a standard Bitcoin address. |
|||
* Returns the address as a base58-encoded string in the standardized format. |
|||
*/ |
|||
Address.prototype.toString = function () { |
|||
return base58check.encode(this.hash.slice(0), this.version) |
|||
} |
|||
// Import functions
|
|||
Address.fromBase58Check = function(string) { |
|||
var decode = base58check.decode(string) |
|||
|
|||
/** |
|||
* Returns the version of an address, e.g. if the address belongs to the main |
|||
* net or the test net. |
|||
*/ |
|||
Address.getVersion = function (address) { |
|||
return base58.decode(address)[0] |
|||
return new Address(decode.payload, decode.version) |
|||
} |
|||
Address.prototype.fromString = Address.prototype.fromBase58Check |
|||
|
|||
/** |
|||
* Returns true if a bitcoin address is a valid address, otherwise false. |
|||
*/ |
|||
Address.validate = function (address) { |
|||
try { |
|||
base58check.decode(address) |
|||
return true |
|||
} catch (e) { |
|||
return false |
|||
} |
|||
// Export functions
|
|||
Address.prototype.toBase58Check = function () { |
|||
return base58check.encode(this.hash, this.version) |
|||
} |
|||
Address.prototype.toString = Address.prototype.toBase58Check |
|||
|
|||
module.exports = Address |
|||
|
@ -1,111 +1,49 @@ |
|||
var assert = require('assert') |
|||
var Address = require('../src/address') |
|||
var network = require('../src/network') |
|||
var base58 = require('../src/base58') |
|||
var base58check = require('../src/base58check') |
|||
var bitcoin = network.bitcoin.pubKeyHash |
|||
var testnet = network.testnet.pubKeyHash |
|||
var Address = require('..').Address |
|||
var fixtures = require('./fixtures/address') |
|||
|
|||
describe('Address', function() { |
|||
var testnetAddress, bitcoinAddress |
|||
var testnetP2shAddress, bitcoinP2shAddress |
|||
var bothVectors = fixtures.pubKeyHash.concat(fixtures.scriptHash) |
|||
|
|||
beforeEach(function(){ |
|||
bitcoinAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' |
|||
testnetAddress = 'mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef' |
|||
bitcoinP2shAddress = '3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt' |
|||
testnetP2shAddress = '2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7' |
|||
}) |
|||
|
|||
describe('parsing', function() { |
|||
it('works with Address object', function() { |
|||
var addr = new Address(new Address('mwrB4fgT1KSBCqELaWv7o7tsExuQzW3NY3', network.testnet.pubKeyHash)) |
|||
|
|||
assert.equal(addr.toString(), 'mwrB4fgT1KSBCqELaWv7o7tsExuQzW3NY3') |
|||
assert.equal(addr.version, network.testnet.pubKeyHash) |
|||
}) |
|||
|
|||
it('works with hex', function() { |
|||
var addr = new Address('13483382d3c3d43fc9d7b52e652b6bbb70e8b667') |
|||
assert.equal(addr.toString(), '12kxLGqrnnchwN9bHHNV2fWDtJGwxKTcJS') |
|||
}) |
|||
|
|||
it('throws error for invalid or unrecognized input', function() { |
|||
assert.throws(function() { |
|||
new Address('beepboopbeepboopbeepboopbeepboopbeepboopbeep') |
|||
}, Error) |
|||
}) |
|||
|
|||
it('works for byte input', function() { |
|||
var hash = base58check.decode(bitcoinAddress) |
|||
var addr = new Address(hash.payload) |
|||
assert.equal(addr.hash, hash.payload) |
|||
assert.equal(network.bitcoin.pubKeyHash, hash.version) |
|||
describe('Constructor', function() { |
|||
it('does not mutate the input', function() { |
|||
bothVectors.forEach(function(f) { |
|||
var hash = new Buffer(f.hex, 'hex') |
|||
var addr = new Address(hash, f.version) |
|||
|
|||
var hash = base58check.decode(testnetAddress) |
|||
var addr = new Address(hash.payload) |
|||
assert.equal(addr.hash, hash.payload) |
|||
assert.equal(network.testnet.pubKeyHash, hash.version) |
|||
}) |
|||
|
|||
it('fails for bad input', function() { |
|||
assert.throws(function() { |
|||
new Address('foo') |
|||
}, Error) |
|||
assert.equal(addr.version, f.version) |
|||
assert.equal(addr.hash.toString('hex'), f.hex) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('getVersion', function() { |
|||
it('returns the proper address version', function() { |
|||
assert.equal(Address.getVersion(bitcoinAddress), network.bitcoin.pubKeyHash) |
|||
assert.equal(Address.getVersion(testnetAddress), network.testnet.pubKeyHash) |
|||
describe('fromBase58Check', function() { |
|||
it('throws on invalid base58check', function() { |
|||
fixtures.malformed.forEach(function(f) { |
|||
assert.throws(function() { |
|||
Address.fromBase58Check(f.base58check) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('toString', function() { |
|||
it('defaults to base58', function() { |
|||
var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ' |
|||
assert.equal((new Address(addr)).toString(), addr) |
|||
}) |
|||
}) |
|||
bothVectors.forEach(function(f) { |
|||
it('imports ' + f.description + ' correctly', function() { |
|||
var addr = Address.fromBase58Check(f.base58check) |
|||
|
|||
describe('Constructor', function(){ |
|||
it('resolves version correctly', function(){ |
|||
assert.equal((new Address(testnetAddress)).version, testnet) |
|||
assert.equal((new Address(bitcoinAddress)).version, bitcoin) |
|||
assert.equal((new Address(testnetP2shAddress)).version, network.testnet.scriptHash) |
|||
assert.equal((new Address(bitcoinP2shAddress)).version, network.bitcoin.scriptHash) |
|||
assert.equal(addr.version, f.version) |
|||
assert.equal(addr.hash.toString('hex'), f.hex) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('validate', function() { |
|||
it('validates known good addresses', function() { |
|||
function validate(addr, expectedVersion) { |
|||
assert.ok(Address.validate(addr)) |
|||
} |
|||
|
|||
validate(testnetAddress) |
|||
validate(bitcoinAddress) |
|||
validate('12KYrjTdVGjFMtaxERSk3gphreJ5US8aUP') |
|||
validate('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y') |
|||
validate('1oNLrsHnBcR6dpaBpwz3LSwutbUNkNSjs') |
|||
validate('1SQHtwR5oJRKLfiWQ2APsAd9miUc4k2ez') |
|||
validate('116CGDLddrZhMrTwhCVJXtXQpxygTT1kHd') |
|||
|
|||
// p2sh addresses
|
|||
validate(testnetP2shAddress) |
|||
validate(bitcoinP2shAddress) |
|||
}) |
|||
|
|||
it('does not validate illegal examples', function() { |
|||
function invalid(addr) { |
|||
assert.ok(!Address.validate(addr)) |
|||
} |
|||
describe('toBase58Check', function() { |
|||
bothVectors.forEach(function(f) { |
|||
it('exports ' + f.description + ' correctly', function() { |
|||
var addr = Address.fromBase58Check(f.base58check) |
|||
var result = addr.toBase58Check() |
|||
|
|||
invalid(''); //empty should be invalid
|
|||
invalid('%%@'); // invalid base58 string
|
|||
invalid('1A1zP1eP5QGefi2DzPTf2L5SLmv7DivfNz'); // bad address (doesn't checksum)
|
|||
invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhe'); // bad address (doesn't checksum)
|
|||
assert.equal(result, f.base58check) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -0,0 +1,42 @@ |
|||
module.exports = { |
|||
pubKeyHash: [ |
|||
{ |
|||
description: 'pubKeyHash (bitcoin)', |
|||
version: 0, |
|||
hex: '751e76e8199196d454941c45d1b3a323f1433bd6', |
|||
base58check: '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH' |
|||
}, |
|||
{ |
|||
description: 'pubKeyHash (testnet)', |
|||
version: 111, |
|||
hex: '751e76e8199196d454941c45d1b3a323f1433bd6', |
|||
base58check: 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r' |
|||
} |
|||
], |
|||
scriptHash: [ |
|||
{ |
|||
description: 'scriptHash (bitcoin)', |
|||
version: 5, |
|||
hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6', |
|||
base58check: '3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr' |
|||
}, |
|||
{ |
|||
description: 'scriptHash (testnet)', |
|||
version: 196, |
|||
hex: 'cd7b44d0b03f2d026d1e586d7ae18903b0d385f6', |
|||
base58check: '2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w' |
|||
} |
|||
], |
|||
malformed: [ |
|||
'45k2PvUfZw', |
|||
'8cVHMKGRJGMEVz', |
|||
'AMPCMAGBmj9EE9oGED', |
|||
'oJPsqvHTSFFWMcmNS3aDidZexw', |
|||
'bpiuHmqwCdiHx4ASNLGvZeBw9taY', |
|||
'2ansc1MsREU2HetNdPGs2eHXTY16ircdyaH', |
|||
'iTKsHH39ooQPFxzX6RFtjPESpQ1', |
|||
'4TU74v3jnoTZGV5UuJGcr7XRg7hU', |
|||
'2a3wk37F1YmfqVtBam4gEn63oNuj', |
|||
'3rtH2aquyk4q1KGaXuiMGxaGfVPH' |
|||
] |
|||
} |
Loading…
Reference in new issue