diff --git a/extensions.go b/extensions.go index 197c2d1e..15fc29a1 100644 --- a/extensions.go +++ b/extensions.go @@ -299,3 +299,51 @@ func (c *Client) ExportWatchingWalletAsync(account string) FutureExportWatchingW func (c *Client) ExportWatchingWallet(account string) ([]byte, []byte, error) { return c.ExportWatchingWalletAsync(account).Receive() } + +// FutureSessionResult is a future promise to deliver the result of a +// SessionAsync RPC invocation (or an applicable error). +type FutureSessionResult chan *response + +// Receive waits for the response promised by the future and returns the +// session result. +func (r FutureSessionResult) Receive() (*btcjson.SessionResult, error) { + res, err := receiveFuture(r) + if err != nil { + return nil, err + } + + // Unmarsal result as a session result object. + var session btcjson.SessionResult + err = json.Unmarshal(res, &session) + if err != nil { + return nil, err + } + + return &session, nil +} + +// SessionAsync returns an instance of a type that can be used to get the result +// of the RPC at some future time by invoking the Receive function on the +// returned instance. +// +// See Session for the blocking version and more details. +// +// NOTE: This is a btcsuite extension. +func (c *Client) SessionAsync() FutureSessionResult { + // Not supported in HTTP POST mode. + if c.config.HTTPPostMode { + return newFutureError(ErrWebsocketsRequired) + } + + cmd := btcjson.NewSessionCmd() + return c.sendCmd(cmd) +} + +// Session returns details regarding a websocket client's current connection. +// +// This RPC requires the client to be running in websocket mode. +// +// NOTE: This is a btcsuite extension. +func (c *Client) Session() (*btcjson.SessionResult, error) { + return c.SessionAsync().Receive() +} diff --git a/notify.go b/notify.go index 72451f74..488b36f4 100644 --- a/notify.go +++ b/notify.go @@ -19,12 +19,12 @@ import ( ) var ( - // ErrNotificationsNotSupported is an error to describe the condition - // where the caller is trying to request notifications when they are - // not supported due to the client being configured to run in HTTP POST - // mode. - ErrNotificationsNotSupported = errors.New("notifications are not " + - "supported when running in HTTP POST mode") + // ErrWebsocketsRequired is an error to describe the condition where the + // caller is trying to use a websocket-only feature, such as requesting + // notifications or other websocket requests when the client is + // configured to run in HTTP POST mode. + ErrWebsocketsRequired = errors.New("a websocket connection is required " + + "to use this feature") ) // notificationState is used to track the current state of successfuly @@ -683,7 +683,7 @@ func (r FutureNotifyBlocksResult) Receive() error { func (c *Client) NotifyBlocksAsync() FutureNotifyBlocksResult { // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -731,7 +731,7 @@ func (r FutureNotifySpentResult) Receive() error { func (c *Client) notifySpentInternal(outpoints []btcjson.OutPoint) FutureNotifySpentResult { // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -763,7 +763,7 @@ func newOutPointFromWire(op *wire.OutPoint) btcjson.OutPoint { func (c *Client) NotifySpentAsync(outpoints []*wire.OutPoint) FutureNotifySpentResult { // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -819,7 +819,7 @@ func (r FutureNotifyNewTransactionsResult) Receive() error { func (c *Client) NotifyNewTransactionsAsync(verbose bool) FutureNotifyNewTransactionsResult { // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -868,7 +868,7 @@ func (r FutureNotifyReceivedResult) Receive() error { func (c *Client) notifyReceivedInternal(addresses []string) FutureNotifyReceivedResult { // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -892,7 +892,7 @@ func (c *Client) notifyReceivedInternal(addresses []string) FutureNotifyReceived func (c *Client) NotifyReceivedAsync(addresses []btcutil.Address) FutureNotifyReceivedResult { // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -965,7 +965,7 @@ func (c *Client) RescanAsync(startBlock *wire.ShaHash, // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in @@ -1042,7 +1042,7 @@ func (c *Client) RescanEndBlockAsync(startBlock *wire.ShaHash, // Not supported in HTTP POST mode. if c.config.HTTPPostMode { - return newFutureError(ErrNotificationsNotSupported) + return newFutureError(ErrWebsocketsRequired) } // Ignore the notification if the client is not interested in