Browse Source

Merge pull request #133 from maraoz/feature/idiomatic-Address

Idiomatic address use
patch-2
Ryan X. Charles 11 years ago
parent
commit
272378f43a
  1. 6
      Address.js
  2. 23
      README.md
  3. 22
      examples/Address.js
  4. 38
      test/test.Address.js

6
Address.js

@ -9,6 +9,7 @@ function Address() {
Address.parent = parent; Address.parent = parent;
parent.applyEncodingsTo(Address); parent.applyEncodingsTo(Address);
Address.prototype.validate = function() { Address.prototype.validate = function() {
this.doAsBinary(function() { this.doAsBinary(function() {
Address.super(this, 'validate', arguments); Address.super(this, 'validate', arguments);
@ -16,4 +17,9 @@ Address.prototype.validate = function() {
}); });
}; };
Address.prototype.isValid = function() {
var answer = Address.super(this, 'isValid', arguments);
return answer;
};
module.exports = require('soop')(Address); module.exports = require('soop')(Address);

23
README.md

@ -31,26 +31,19 @@ Validating a Bitcoin address:
```js ```js
var Address = require('bitcore/Address'); var Address = require('bitcore/Address');
var addrStrings = [ var addrs = [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx',
"A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1600 Pennsylvania Ave NW", '1600 Pennsylvania Ave NW',
].map(function(addr) { ].map(function(addr) {
return new Address(addr); return new Address(addr);
}); });
addrStrings.forEach(function(addr) { addrs.forEach(function(addr) {
var valid = addr.isValid();
try { console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid');
addr.validate();
console.log(addr.data + ": is valid");
} catch(e) {
console.log(addr.data + ": is not a valid address. " + e);
}
}); });
``` ```
## Monitoring Blocks and Transactions ## Monitoring Blocks and Transactions
For this example you need a running bitcoind instance with RPC enabled. For this example you need a running bitcoind instance with RPC enabled.

22
examples/Address.js

@ -4,22 +4,16 @@
var Address = require('../Address'); var Address = require('../Address');
var addrStrings = [ var addrs = [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx',
"A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1600 Pennsylvania Ave NW", '1600 Pennsylvania Ave NW',
].map(function(addr) { ].map(function(addr) {
return new Address(addr); return new Address(addr);
}); });
addrStrings.forEach(function(addr) { addrs.forEach(function(addr) {
var valid = addr.isValid();
try { console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid');
addr.validate();
console.log(addr.data + ": is valid");
} catch(e) {
console.log(addr.data + ": is not a valid address. " + e);
}
}); });

38
test/test.Address.js

@ -20,17 +20,31 @@ describe('Address', function() {
var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T'); var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T');
should.exist(a); should.exist(a);
}); });
it('should validate correctly', function() { var data = [
var a = new Address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
var m = new Address('32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6'); ['11111111111111111111111111122222234', false], // totally invalid
var b = new Address('11111111111111111111111111122222234'); ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true],
a.validate.bind(a).should.not.throw(Error); ['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true],
m.validate.bind(m).should.not.throw(Error); ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true],
b.validate.bind(b).should.throw(Error); ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW620', false], // bad checksum
['1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // data changed, original checksum.
['1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // invalid chars
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j', false], // checksums don't match.
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!', false], // bad char (!)
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz', false], // too long Bitcoin address
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz', false],// too long Bitcoin address
['2cFupjhnEsSn59qHXstmK2ffpLv2', false], // valid base58 invalid data
];
data.forEach(function(datum) {
var address = datum[0];
var result = datum[1];
it('should validate correctly ' + address, function() {
var a = new Address(address);
var s = a.toString();
a.isValid().should.equal(result);
s.should.equal(a.toString()); // check that validation doesn't change data
});
}); });
}); });

Loading…
Cancel
Save