Commit b13c9818 authored by Christopher Schinnerl's avatar Christopher Schinnerl

add api endpoint and test

parent e6b1ccae
Pipeline #42875206 passed with stages
in 44 minutes and 53 seconds
......@@ -61,6 +61,14 @@ func (c *Client) WalletGet() (wg api.WalletGET, err error) {
return
}
// WalletLastAddressesGet returns the count last addresses generated by the
// wallet in reverse order. That means the last generated address will be the
// first one in the slice.
func (c *Client) WalletLastAddressesGet(count uint64) (wag api.WalletAddressesGET, err error) {
err = c.get(fmt.Sprintf("/wallet/addresses?count=%v", count), &wag)
return
}
// WalletLockPost uses the /wallet/lock endpoint to lock the wallet.
func (c *Client) WalletLockPost() (err error) {
err = c.post("/wallet/lock", "", nil)
......@@ -194,7 +202,7 @@ func (c *Client) WalletWatchGet() (wwg api.WalletWatchGET, err error) {
return
}
// WalletWatchPost uses the /wallet/watch endpoint to add a set of addresses
// WalletWatchAddPost uses the /wallet/watch endpoint to add a set of addresses
// to the watch set. The unused flag should be set to true if the addresses
// have never appeared in the blockchain.
func (c *Client) WalletWatchAddPost(addrs []types.UnlockHash, unused bool) error {
......@@ -209,7 +217,7 @@ func (c *Client) WalletWatchAddPost(addrs []types.UnlockHash, unused bool) error
return c.post("/wallet/watch", string(json), nil)
}
// WalletWatchPost uses the /wallet/watch endpoint to remove a set of
// WalletWatchRemovePost uses the /wallet/watch endpoint to remove a set of
// addresses from the watch set. The unused flag should be set to true if the
// addresses have never appeared in the blockchain.
func (c *Client) WalletWatchRemovePost(addrs []types.UnlockHash, unused bool) error {
......
......@@ -256,6 +256,28 @@ func (api *API) walletAddressHandler(w http.ResponseWriter, req *http.Request, _
// walletAddressHandler handles API calls to /wallet/addresses.
func (api *API) walletAddressesHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// If count is specified we return the last count addresses.
c := req.FormValue("count")
if c != "" {
var count uint64
_, err := fmt.Sscan(c, &count)
if err != nil {
WriteError(w, Error{"Failed to parse count: " + err.Error()}, http.StatusBadRequest)
return
}
// Get the last count addresses.
addresses, err := api.wallet.LastAddresses(count)
if err != nil {
WriteError(w, Error{fmt.Sprintf("Error when calling /wallet/addresses: %v", err)}, http.StatusBadRequest)
return
}
// Send the response.
WriteJSON(w, WalletAddressesGET{
Addresses: addresses,
})
}
// If count wasn't specified we return all addresses sorted alphabetically.
addresses, err := api.wallet.AllAddresses()
if err != nil {
WriteError(w, Error{fmt.Sprintf("Error when calling /wallet/addresses: %v", err)}, http.StatusBadRequest)
......
......@@ -481,3 +481,60 @@ func TestFileContractUnspentOutputs(t *testing.T) {
t.Fatal("wallet's spendable outputs did not contain file contract output")
}
}
// TestWalletLastAddresses tests the /wallet/addresses endpoint with a
// specified count.
func TestWalletLastAddresses(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// Create a new server
testNode, err := siatest.NewCleanNode(node.AllModules(siatest.TestDir(t.Name())))
if err != nil {
t.Fatal(err)
}
defer func() {
if err := testNode.Close(); err != nil {
t.Fatal(err)
}
}()
// The wallet should have 0 addresses.
wag, err := testNode.WalletAddressesGet()
if err != nil {
t.Fatal(err)
}
if len(wag.Addresses) != 0 {
t.Fatal("Wallet should have 0 addresses but had", len(wag.Addresses))
}
// Generate n addresses.
n := 10
addresses := make([]types.UnlockHash, 0, n)
for i := 0; i < n; i++ {
wag, err := testNode.WalletAddressGet()
if err != nil {
t.Fatal(err)
}
addresses = append(addresses, wag.Address)
}
// The wallet should have n addresses now.
wag, err = testNode.WalletAddressesGet()
if err != nil {
t.Fatal(err)
}
if len(wag.Addresses) != n {
t.Fatal("Wallet should have 100 addresses but had", len(wag.Addresses))
}
// Get the n addresses in reverse order.
wlag, err := testNode.WalletLastAddressesGet(uint64(n))
if err != nil {
t.Fatal(err)
}
// Make sure the returned addresses
for i := range wag.Addresses {
if addresses[i] != wlag.Addresses[len(wlag.Addresses)-1-i] {
t.Fatal("addresses don't match for i =", i)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment