You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.4 KiB
67 lines
2.4 KiB
10 years ago
|
title: Address
|
||
|
description: A simple interface to generate and validate a bitcoin address.
|
||
|
---
|
||
10 years ago
|
# Address
|
||
10 years ago
|
|
||
|
## Description
|
||
10 years ago
|
|
||
10 years ago
|
Represents a bitcoin address. Addresses are the most popular way to make bitcoin transactions. See [the official Bitcoin Wiki](https://en.bitcoin.it/wiki/Address) for technical background information.
|
||
10 years ago
|
|
||
|
## Instantiate an Address
|
||
|
|
||
10 years ago
|
To be able to receive bitcoins an address is needed, but in order to spend them a private key is necessary. Please take a look at the [`PrivateKey`](privatekey.md) docs for more information about exporting and saving a key.
|
||
10 years ago
|
|
||
|
```javascript
|
||
|
var privateKey = new PrivateKey();
|
||
|
var address = privateKey.toAddress();
|
||
|
```
|
||
|
|
||
10 years ago
|
You can also instantiate an Address from a String, [PublicKey](publickey.md), or [HDPublicKey](hierarchical.md), in case you are not the owner of the private key.
|
||
10 years ago
|
|
||
|
```javascript
|
||
|
// from a string
|
||
|
var address = Address.fromString('mwkXG8NnB2snbqWTcpNiK6qqGHm1LebHDc');
|
||
|
|
||
|
// a default network address from a public key
|
||
|
var publicKey = PublicKey(privateKey);
|
||
10 years ago
|
var address = new Address(publicKey);
|
||
|
// alternative interface
|
||
10 years ago
|
var address = Address.fromPublicKey(publicKey);
|
||
|
|
||
|
// a testnet address from a public key
|
||
10 years ago
|
var publicKey = new PublicKey(privateKey);
|
||
|
var address = new Address(publicKey, Networks.testnet);
|
||
10 years ago
|
```
|
||
|
|
||
|
## Validating an Address
|
||
|
|
||
10 years ago
|
The main use that we expect you'll have for the `Address` class in Bitcore is validating that an address is a valid one, what type of address it is (you may be interested on knowing if the address is a simple "pay to public key hash" address or a "pay to script hash" address) and what network does the address belong to.
|
||
10 years ago
|
|
||
|
The code to do these validations looks like this:
|
||
|
|
||
|
```javascript
|
||
10 years ago
|
// validate an address
|
||
|
if (Address.isValid(input){
|
||
|
...
|
||
|
}
|
||
|
|
||
10 years ago
|
// validate that an input field is a valid testnet address
|
||
|
if (Address.isValid(input, Networks.testnet){
|
||
|
...
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// validate that an input field is a valid livenet pubkeyhash
|
||
|
if (Address.isValid(input, Networks.livenet, Address.Pay2PubKeyHash){
|
||
|
...
|
||
|
}
|
||
|
|
||
|
// get the specific validation error that can occurred
|
||
|
var error = Address.getValidationError(input, Networks.testnet);
|
||
|
if (error) {
|
||
|
// handle the error
|
||
|
}
|
||
|
}
|
||
10 years ago
|
```
|
||
10 years ago
|
|
||
|
The errors are listed in the generated file in the [errors folder](https://github.com/bitpay/bitcore/tree/master/lib/errors). There's a structure to errors defined in the [spec.js file](https://github.com/bitpay/bitcore/tree/master/lib/errors/spec.js).
|