Browse Source

Address: Change discoverMeta to classifyVersion and add a general address validation example

patch-2
Braydon Fuller 10 years ago
parent
commit
56161676c3
  1. 6
      docs/Address.md
  2. 32
      lib/address.js

6
docs/Address.md

@ -51,6 +51,12 @@ belong to.
The code to do these validations looks like this: The code to do these validations looks like this:
```javascript ```javascript
// validate an address
if (Address.isValid(input){
...
}
// validate that an input field is a valid testnet address // validate that an input field is a valid testnet address
if (Address.isValid(input, Networks.testnet){ if (Address.isValid(input, Networks.testnet){
... ...

32
lib/address.js

@ -117,30 +117,30 @@ Address._transformHash = function(hash){
* @returns {Object} An object with keys: network and type * @returns {Object} An object with keys: network and type
* @private * @private
*/ */
Address._discoverMeta = function(buffer){ Address._classifyFromVersion = function(buffer){
var meta = {}; var version = {};
switch(buffer[0]){ // the version byte switch(buffer[0]){ // the version byte
case Networks.livenet.pubkeyhash: case Networks.livenet.pubkeyhash:
meta.network = Networks.livenet; version.network = Networks.livenet;
meta.type = Address.PayToPublicKeyHash; version.type = Address.PayToPublicKeyHash;
break; break;
case Networks.livenet.scripthash: case Networks.livenet.scripthash:
meta.network = Networks.livenet; version.network = Networks.livenet;
meta.type = Address.PayToScriptHash; version.type = Address.PayToScriptHash;
break; break;
case Networks.testnet.pubkeyhash: case Networks.testnet.pubkeyhash:
meta.network = Networks.testnet; version.network = Networks.testnet;
meta.type = Address.PayToPublicKeyHash; version.type = Address.PayToPublicKeyHash;
break; break;
case Networks.testnet.scripthash: case Networks.testnet.scripthash:
meta.network = Networks.testnet; version.network = Networks.testnet;
meta.type = Address.PayToScriptHash; version.type = Address.PayToScriptHash;
break; break;
} }
return meta; return version;
}; };
/** /**
@ -162,19 +162,19 @@ Address._transformBuffer = function(buffer, network, type){
} }
network = Networks.get(network); network = Networks.get(network);
var meta = Address._discoverMeta(buffer); var bufferVersion = Address._classifyFromVersion(buffer);
if (!meta.network || (network && network !== meta.network)) { if (!bufferVersion.network || (network && network !== bufferVersion.network)) {
throw new TypeError('Address has mismatched network type.'); throw new TypeError('Address has mismatched network type.');
} }
if (!meta.type || ( type && type !== meta.type )) { if (!bufferVersion.type || ( type && type !== bufferVersion.type )) {
throw new TypeError('Address has mismatched type.'); throw new TypeError('Address has mismatched type.');
} }
info.hashBuffer = buffer.slice(1); info.hashBuffer = buffer.slice(1);
info.network = meta.network; info.network = bufferVersion.network;
info.type = meta.type; info.type = bufferVersion.type;
return info; return info;
}; };

Loading…
Cancel
Save