-
Notifications
You must be signed in to change notification settings - Fork 33
/
address.go
131 lines (122 loc) · 4.25 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gobcy
import (
"errors"
"strconv"
)
//GetAddrBal returns balance information for a given public
//address. Fastest Address API call, but does not
//include transaction details.
func (api *API) GetAddrBal(hash string, params map[string]string) (addr Addr, err error) {
u, err := api.buildURL("/addrs/"+hash+"/balance", params)
if err != nil {
return
}
err = getResponse(u, &addr)
return
}
//GetAddr returns information for a given public
//address, including a slice of confirmed and unconfirmed
//transaction outpus via the TXRef arrays in the Address
//type. Returns more information than GetAddrBal, but
//slightly slower.
func (api *API) GetAddr(hash string, params map[string]string) (addr Addr, err error) {
u, err := api.buildURL("/addrs/"+hash, params)
if err != nil {
return
}
err = getResponse(u, &addr)
return
}
//GetAddrNext returns a given Addr's next page of TXRefs,
//if Addr.HasMore is true. If HasMore is false, will
//return an error. It assumes default API URL parameters.
func (api *API) GetAddrNext(this Addr) (next Addr, err error) {
if !this.HasMore {
err = errors.New("Func GetAddrNext: this Addr doesn't have more TXRefs according to its HasMore")
return
}
before := this.TXRefs[len(this.TXRefs)-1].BlockHeight
next, err = api.GetAddr(this.Address, map[string]string{"before": strconv.Itoa(before)})
return
}
//GetAddrFull returns information for a given public
//address, including a slice of TXs associated
//with this address. Returns more data than GetAddr since
//it includes full transactions, but slowest Address query.
func (api *API) GetAddrFull(hash string, params map[string]string) (addr Addr, err error) {
u, err := api.buildURL("/addrs/"+hash+"/full", params)
if err != nil {
return
}
err = getResponse(u, &addr)
return
}
//GetAddrFullNext returns a given Addr's next page of TXs,
//if Addr.HasMore is true. If HasMore is false, will
//return an error. It assumes default API URL parameters, like GetAddrFull.
func (api *API) GetAddrFullNext(this Addr) (next Addr, err error) {
if !this.HasMore {
err = errors.New("Func GetAddrFullNext: this Addr doesn't have more TXs according to its HasMore")
return
}
before := this.TXs[len(this.TXs)-1].BlockHeight
next, err = api.GetAddrFull(this.Address, map[string]string{"before": strconv.Itoa(before)})
return
}
//GenAddrKeychain generates a public/private key pair for use with
//transactions within the specified coin/chain. Please note that
//this call must be made over SSL, and it is not recommended to keep
//large amounts in these addresses, or for very long.
func (api *API) GenAddrKeychain() (pair AddrKeychain, err error) {
u, err := api.buildURL("/addrs", nil)
if err != nil {
return
}
err = postResponse(u, nil, &pair)
return
}
//GenAddrMultisig generates a P2SH multisignature address using an array
//of PubKeys and the ScriptType from a AddrKeychain. Other fields are
//ignored, and the ScriptType must be a "multisig-n-of-m" type. Returns
//an AddrKeychain with the same PubKeys, ScriptType, and the proper
//P2SH address in the AddrKeychain's address field.
func (api *API) GenAddrMultisig(multi AddrKeychain) (addr AddrKeychain, err error) {
if len(multi.PubKeys) == 0 || multi.ScriptType == "" {
err = errors.New("GenAddrMultisig: PubKeys or ScriptType are empty.")
return
}
u, err := api.buildURL("/addrs", nil)
if err != nil {
return
}
err = postResponse(u, &multi, &addr)
return
}
//Faucet funds the AddrKeychain with an amount. Only works on BlockCypher's
//Testnet and Bitcoin Testnet3. Returns the transaction hash funding
//your AddrKeychain.
func (api *API) Faucet(a AddrKeychain, amount int) (txhash string, err error) {
if !(api.Coin == "bcy" && api.Chain == "test") && !(api.Coin == "btc" && api.Chain == "test3") {
err = errors.New("Faucet: Cannot use Faucet unless on BlockCypher Testnet or Bitcoin Testnet3.")
return
}
u, err := api.buildURL("/faucet", nil)
if err != nil {
return
}
type FauxAddr struct {
Address string `json:"address"`
Amount int `json:"amount"`
}
var addr string
//for easy funding/testing of OAPAddresses
if a.OriginalAddress != "" {
addr = a.OriginalAddress
} else {
addr = a.Address
}
txref := make(map[string]string)
err = postResponse(u, &FauxAddr{addr, amount}, &txref)
txhash = txref["tx_ref"]
return
}