Address: Change discoverMeta to classifyVersion and add a general address validation example
This commit is contained in:
parent
3b061b057f
commit
56161676c3
|
@ -7,8 +7,8 @@ Wiki](https://en.bitcoin.it/wiki/Address) for more information.
|
|||
|
||||
## Instantiate an Address
|
||||
|
||||
To be able to receive bitcoin an address is needed, here is how to create an
|
||||
address from a new private key. Please see the [`PrivateKey`](PrivateKey.md) docs
|
||||
To be able to receive bitcoin an address is needed, here is how to create an
|
||||
address from a new private key. Please see the [`PrivateKey`](PrivateKey.md) docs
|
||||
for more information about exporting and saving a key.
|
||||
|
||||
```javascript
|
||||
|
@ -51,6 +51,12 @@ belong to.
|
|||
The code to do these validations looks like this:
|
||||
|
||||
```javascript
|
||||
|
||||
// validate an address
|
||||
if (Address.isValid(input){
|
||||
...
|
||||
}
|
||||
|
||||
// validate that an input field is a valid testnet address
|
||||
if (Address.isValid(input, Networks.testnet){
|
||||
...
|
||||
|
|
|
@ -117,30 +117,30 @@ Address._transformHash = function(hash){
|
|||
* @returns {Object} An object with keys: network and type
|
||||
* @private
|
||||
*/
|
||||
Address._discoverMeta = function(buffer){
|
||||
var meta = {};
|
||||
Address._classifyFromVersion = function(buffer){
|
||||
var version = {};
|
||||
switch(buffer[0]){ // the version byte
|
||||
case Networks.livenet.pubkeyhash:
|
||||
meta.network = Networks.livenet;
|
||||
meta.type = Address.PayToPublicKeyHash;
|
||||
version.network = Networks.livenet;
|
||||
version.type = Address.PayToPublicKeyHash;
|
||||
break;
|
||||
|
||||
case Networks.livenet.scripthash:
|
||||
meta.network = Networks.livenet;
|
||||
meta.type = Address.PayToScriptHash;
|
||||
version.network = Networks.livenet;
|
||||
version.type = Address.PayToScriptHash;
|
||||
break;
|
||||
|
||||
case Networks.testnet.pubkeyhash:
|
||||
meta.network = Networks.testnet;
|
||||
meta.type = Address.PayToPublicKeyHash;
|
||||
version.network = Networks.testnet;
|
||||
version.type = Address.PayToPublicKeyHash;
|
||||
break;
|
||||
|
||||
case Networks.testnet.scripthash:
|
||||
meta.network = Networks.testnet;
|
||||
meta.type = Address.PayToScriptHash;
|
||||
version.network = Networks.testnet;
|
||||
version.type = Address.PayToScriptHash;
|
||||
break;
|
||||
}
|
||||
return meta;
|
||||
return version;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -162,19 +162,19 @@ Address._transformBuffer = function(buffer, network, type){
|
|||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
|
||||
if (!meta.type || ( type && type !== meta.type )) {
|
||||
if (!bufferVersion.type || ( type && type !== bufferVersion.type )) {
|
||||
throw new TypeError('Address has mismatched type.');
|
||||
}
|
||||
|
||||
info.hashBuffer = buffer.slice(1);
|
||||
info.network = meta.network;
|
||||
info.type = meta.type;
|
||||
info.network = bufferVersion.network;
|
||||
info.type = bufferVersion.type;
|
||||
return info;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue