Skip to content

Commit

Permalink
Merge pull request #2 from Rjected/multinet-address
Browse files Browse the repository at this point in the history
rpcclient: replace futures mainnet with params
  • Loading branch information
JFixby authored Sep 20, 2019
2 parents e0c3143 + d175fa0 commit 7ad7042
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 63 deletions.
69 changes: 46 additions & 23 deletions rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/go-socks/socks"
"github.com/btcsuite/websocket"
)
Expand Down Expand Up @@ -121,6 +122,10 @@ type Client struct {
// config holds the connection configuration assoiated with this client.
config *ConnConfig

// chainParams holds the params for the chain that this client is using,
// and is used for many wallet methods.
chainParams *chaincfg.Params

// wsConn is the underlying websocket connection when not in HTTP POST
// mode.
wsConn *websocket.Conn
Expand Down Expand Up @@ -261,31 +266,29 @@ func (c *Client) trackRegisteredNtfns(cmd interface{}) {
}
}

type (
// inMessage is the first type that an incoming message is unmarshaled
// into. It supports both requests (for notification support) and
// responses. The partially-unmarshaled message is a notification if
// the embedded ID (from the response) is nil. Otherwise, it is a
// response.
inMessage struct {
ID *float64 `json:"id"`
*rawNotification
*rawResponse
}
// inMessage is the first type that an incoming message is unmarshaled
// into. It supports both requests (for notification support) and
// responses. The partially-unmarshaled message is a notification if
// the embedded ID (from the response) is nil. Otherwise, it is a
// response.
type inMessage struct {
ID *float64 `json:"id"`
*rawNotification
*rawResponse
}

// rawNotification is a partially-unmarshaled JSON-RPC notification.
rawNotification struct {
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
}
// rawNotification is a partially-unmarshaled JSON-RPC notification.
type rawNotification struct {
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
}

// rawResponse is a partially-unmarshaled JSON-RPC response. For this
// to be valid (according to JSON-RPC 1.0 spec), ID may not be nil.
rawResponse struct {
Result json.RawMessage `json:"result"`
Error *btcjson.RPCError `json:"error"`
}
)
// rawResponse is a partially-unmarshaled JSON-RPC response. For this
// to be valid (according to JSON-RPC 1.0 spec), ID may not be nil.
type rawResponse struct {
Result json.RawMessage `json:"result"`
Error *btcjson.RPCError `json:"error"`
}

// response is the raw bytes of a JSON-RPC result, or the error if the response
// error object was non-null.
Expand Down Expand Up @@ -1065,6 +1068,9 @@ type ConnConfig struct {
// Pass is the passphrase to use to authenticate to the RPC server.
Pass string

// Params is the string represening the network that the server is running.
Params string

// DisableTLS specifies whether transport layer security should be
// disabled. It is recommended to always use TLS if the RPC server
// supports it as otherwise your username and password is sent across
Expand Down Expand Up @@ -1262,6 +1268,23 @@ func New(config *ConnConfig, ntfnHandlers *NotificationHandlers) (*Client, error
shutdown: make(chan struct{}),
}

// Default network is mainnet, no parameters are necessary but if mainnet
// is specified it will be the param
switch config.Params {
case "":
fallthrough
case chaincfg.MainNetParams.Name:
client.chainParams = &chaincfg.MainNetParams
case chaincfg.TestNet3Params.Name:
client.chainParams = &chaincfg.TestNet3Params
case chaincfg.RegressionNetParams.Name:
client.chainParams = &chaincfg.RegressionNetParams
case chaincfg.SimNetParams.Name:
client.chainParams = &chaincfg.SimNetParams
default:
return nil, fmt.Errorf("rpcclient.New: Unknown chain %s", config.Params)
}

if start {
log.Infof("Established connection to RPC server %s",
config.Host)
Expand Down
121 changes: 81 additions & 40 deletions rpcclient/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
Expand Down Expand Up @@ -758,20 +757,20 @@ type FutureAddMultisigAddressResult chan *response
// Receive waits for the response promised by the future and returns the
// multisignature address that requires the specified number of signatures for
// the provided addresses.
func (r FutureAddMultisigAddressResult) Receive() (btcutil.Address, error) {
func (r FutureAddMultisigAddressResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
return "", err
}

// Unmarshal result as a string.
var addr string
err = json.Unmarshal(res, &addr)
if err != nil {
return nil, err
return "", err
}

return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
return addr, nil
}

// AddMultisigAddressAsync returns an instance of a type that can be used to get
Expand All @@ -792,8 +791,15 @@ func (c *Client) AddMultisigAddressAsync(requiredSigs int, addresses []btcutil.A
// AddMultisigAddress adds a multisignature address that requires the specified
// number of signatures for the provided addresses to the wallet.
func (c *Client) AddMultisigAddress(requiredSigs int, addresses []btcutil.Address, account string) (btcutil.Address, error) {
return c.AddMultisigAddressAsync(requiredSigs, addresses,
var addr string
var err error
addr, err = c.AddMultisigAddressAsync(requiredSigs, addresses,
account).Receive()
if err != nil {
return nil, err
}

return btcutil.DecodeAddress(addr, c.chainParams)
}

// FutureCreateMultisigResult is a future promise to deliver the result of a
Expand Down Expand Up @@ -872,20 +878,20 @@ type FutureGetNewAddressResult chan *response

// Receive waits for the response promised by the future and returns a new
// address.
func (r FutureGetNewAddressResult) Receive() (btcutil.Address, error) {
func (r FutureGetNewAddressResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
return "", err
}

// Unmarshal result as a string.
var addr string
err = json.Unmarshal(res, &addr)
if err != nil {
return nil, err
return "", err
}

return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
return addr, nil
}

// GetNewAddressAsync returns an instance of a type that can be used to get the
Expand All @@ -898,9 +904,17 @@ func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult {
return c.sendCmd(cmd)
}

// GetNewAddress returns a new address.
// GetNewAddress returns a new address, and decodes based on the client's
// chain params.
func (c *Client) GetNewAddress(account string) (btcutil.Address, error) {
return c.GetNewAddressAsync(account).Receive()
var addr string
var err error
addr, err = c.GetNewAddressAsync(account).Receive()
if err != nil {
return nil, err
}

return btcutil.DecodeAddress(addr, c.chainParams)
}

// FutureGetRawChangeAddressResult is a future promise to deliver the result of
Expand All @@ -910,20 +924,20 @@ type FutureGetRawChangeAddressResult chan *response
// Receive waits for the response promised by the future and returns a new
// address for receiving change that will be associated with the provided
// account. Note that this is only for raw transactions and NOT for normal use.
func (r FutureGetRawChangeAddressResult) Receive() (btcutil.Address, error) {
func (r FutureGetRawChangeAddressResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
return "", err
}

// Unmarshal result as a string.
var addr string
err = json.Unmarshal(res, &addr)
if err != nil {
return nil, err
return "", err
}

return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
return addr, nil
}

// GetRawChangeAddressAsync returns an instance of a type that can be used to
Expand All @@ -940,7 +954,14 @@ func (c *Client) GetRawChangeAddressAsync(account string) FutureGetRawChangeAddr
// associated with the provided account. Note that this is only for raw
// transactions and NOT for normal use.
func (c *Client) GetRawChangeAddress(account string) (btcutil.Address, error) {
return c.GetRawChangeAddressAsync(account).Receive()
var addr string
var err error
addr, err = c.GetRawChangeAddressAsync(account).Receive()
if err != nil {
return nil, err
}

return btcutil.DecodeAddress(addr, c.chainParams)
}

// FutureAddWitnessAddressResult is a future promise to deliver the result of
Expand All @@ -949,20 +970,20 @@ type FutureAddWitnessAddressResult chan *response

// Receive waits for the response promised by the future and returns the new
// address.
func (r FutureAddWitnessAddressResult) Receive() (btcutil.Address, error) {
func (r FutureAddWitnessAddressResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
return "", err
}

// Unmarshal result as a string.
var addr string
err = json.Unmarshal(res, &addr)
if err != nil {
return nil, err
return "", err
}

return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
return addr, nil
}

// AddWitnessAddressAsync returns an instance of a type that can be used to get
Expand All @@ -978,7 +999,14 @@ func (c *Client) AddWitnessAddressAsync(address string) FutureAddWitnessAddressR
// AddWitnessAddress adds a witness address for a script and returns the new
// address (P2SH of the witness script).
func (c *Client) AddWitnessAddress(address string) (btcutil.Address, error) {
return c.AddWitnessAddressAsync(address).Receive()
var addr string
var err error
addr, err = c.AddWitnessAddressAsync(address).Receive()
if err != nil {
return nil, err
}

return btcutil.DecodeAddress(addr, c.chainParams)
}

// FutureGetAccountAddressResult is a future promise to deliver the result of a
Expand All @@ -987,20 +1015,20 @@ type FutureGetAccountAddressResult chan *response

// Receive waits for the response promised by the future and returns the current
// Bitcoin address for receiving payments to the specified account.
func (r FutureGetAccountAddressResult) Receive() (btcutil.Address, error) {
func (r FutureGetAccountAddressResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
return "", err
}

// Unmarshal result as a string.
var addr string
err = json.Unmarshal(res, &addr)
if err != nil {
return nil, err
return "", err
}

return btcutil.DecodeAddress(addr, &chaincfg.MainNetParams)
return addr, nil
}

// GetAccountAddressAsync returns an instance of a type that can be used to get
Expand All @@ -1016,7 +1044,14 @@ func (c *Client) GetAccountAddressAsync(account string) FutureGetAccountAddressR
// GetAccountAddress returns the current Bitcoin address for receiving payments
// to the specified account.
func (c *Client) GetAccountAddress(account string) (btcutil.Address, error) {
return c.GetAccountAddressAsync(account).Receive()
var addr string
var err error
addr, err = c.GetAccountAddressAsync(account).Receive()
if err != nil {
return nil, err
}

return btcutil.DecodeAddress(addr, c.chainParams)
}

// FutureGetAccountResult is a future promise to deliver the result of a
Expand Down Expand Up @@ -1090,7 +1125,7 @@ type FutureGetAddressesByAccountResult chan *response

// Receive waits for the response promised by the future and returns the list of
// addresses associated with the passed account.
func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error) {
func (r FutureGetAddressesByAccountResult) Receive() ([]string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
Expand All @@ -1103,17 +1138,7 @@ func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error)
return nil, err
}

addrs := make([]btcutil.Address, 0, len(addrStrings))
for _, addrStr := range addrStrings {
addr, err := btcutil.DecodeAddress(addrStr,
&chaincfg.MainNetParams)
if err != nil {
return nil, err
}
addrs = append(addrs, addr)
}

return addrs, nil
return addrStrings, nil
}

// GetAddressesByAccountAsync returns an instance of a type that can be used to
Expand All @@ -1129,7 +1154,23 @@ func (c *Client) GetAddressesByAccountAsync(account string) FutureGetAddressesBy
// GetAddressesByAccount returns the list of addresses associated with the
// passed account.
func (c *Client) GetAddressesByAccount(account string) ([]btcutil.Address, error) {
return c.GetAddressesByAccountAsync(account).Receive()
var addrStrings []string
var err error
addrStrings, err = c.GetAddressesByAccountAsync(account).Receive()
if err != nil {
return nil, err
}

addrs := make([]btcutil.Address, 0, len(addrStrings))
for _, addrStr := range addrStrings {
addr, err := btcutil.DecodeAddress(addrStr, c.chainParams)
if err != nil {
return nil, err
}
addrs = append(addrs, addr)
}

return addrs, nil
}

// FutureMoveResult is a future promise to deliver the result of a MoveAsync,
Expand Down

0 comments on commit 7ad7042

Please sign in to comment.