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
|
## Instantiate an Address
|
||||||
|
|
||||||
To be able to receive bitcoin an address is needed, here is how to create an
|
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
|
address from a new private key. Please see the [`PrivateKey`](PrivateKey.md) docs
|
||||||
for more information about exporting and saving a key.
|
for more information about exporting and saving a key.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -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){
|
||||||
...
|
...
|
||||||
|
|||||||
@ -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…
Reference in New Issue
Block a user