gitlab.com/SiaPrime/SiaPrime@v1.4.1/node/api/client/wallet.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"strconv"
     8  
     9  	"gitlab.com/SiaPrime/SiaPrime/crypto"
    10  	"gitlab.com/SiaPrime/SiaPrime/node/api"
    11  	"gitlab.com/SiaPrime/SiaPrime/types"
    12  )
    13  
    14  // WalletAddressGet requests a new address from the /wallet/address endpoint
    15  func (c *Client) WalletAddressGet() (wag api.WalletAddressGET, err error) {
    16  	err = c.get("/wallet/address", &wag)
    17  	return
    18  }
    19  
    20  // WalletAddressesGet requests the wallets known addresses from the
    21  // /wallet/addresses endpoint.
    22  func (c *Client) WalletAddressesGet() (wag api.WalletAddressesGET, err error) {
    23  	err = c.get("/wallet/addresses", &wag)
    24  	return
    25  }
    26  
    27  // WalletChangePasswordPost uses the /wallet/changepassword endpoint to change
    28  // the wallet's password.
    29  func (c *Client) WalletChangePasswordPost(currentPassword, newPassword string) (err error) {
    30  	values := url.Values{}
    31  	values.Set("newpassword", newPassword)
    32  	values.Set("encryptionpassword", currentPassword)
    33  	err = c.post("/wallet/changepassword", values.Encode(), nil)
    34  	return
    35  }
    36  
    37  // WalletInitPost uses the /wallet/init endpoint to initialize and encrypt a
    38  // wallet
    39  func (c *Client) WalletInitPost(password string, force bool) (wip api.WalletInitPOST, err error) {
    40  	values := url.Values{}
    41  	values.Set("encryptionpassword", password)
    42  	values.Set("force", strconv.FormatBool(force))
    43  	err = c.post("/wallet/init", values.Encode(), &wip)
    44  	return
    45  }
    46  
    47  // WalletInitSeedPost uses the /wallet/init/seed endpoint to initialize and
    48  // encrypt a wallet using a given seed.
    49  func (c *Client) WalletInitSeedPost(seed, password string, force bool) (err error) {
    50  	values := url.Values{}
    51  	values.Set("seed", seed)
    52  	values.Set("encryptionpassword", password)
    53  	values.Set("force", strconv.FormatBool(force))
    54  	err = c.post("/wallet/init/seed", values.Encode(), nil)
    55  	return
    56  }
    57  
    58  // WalletGet requests the /wallet api resource
    59  func (c *Client) WalletGet() (wg api.WalletGET, err error) {
    60  	err = c.get("/wallet", &wg)
    61  	return
    62  }
    63  
    64  // WalletLastAddressesGet returns the count last addresses generated by the
    65  // wallet in reverse order. That means the last generated address will be the
    66  // first one in the slice.
    67  func (c *Client) WalletLastAddressesGet(count uint64) (wag api.WalletAddressesGET, err error) {
    68  	err = c.get(fmt.Sprintf("/wallet/seedaddrs?count=%v", count), &wag)
    69  	return
    70  }
    71  
    72  // WalletLockPost uses the /wallet/lock endpoint to lock the wallet.
    73  func (c *Client) WalletLockPost() (err error) {
    74  	err = c.post("/wallet/lock", "", nil)
    75  	return
    76  }
    77  
    78  // WalletSeedPost uses the /wallet/seed endpoint to add a seed to the wallet's list
    79  // of seeds.
    80  func (c *Client) WalletSeedPost(seed, password string) (err error) {
    81  	values := url.Values{}
    82  	values.Set("seed", seed)
    83  	values.Set("encryptionpassword", password)
    84  	err = c.post("/wallet/seed", values.Encode(), nil)
    85  	return
    86  }
    87  
    88  // WalletSeedsGet uses the /wallet/seeds endpoint to return the wallet's
    89  // current seeds.
    90  func (c *Client) WalletSeedsGet() (wsg api.WalletSeedsGET, err error) {
    91  	err = c.get("/wallet/seeds", &wsg)
    92  	return
    93  }
    94  
    95  // WalletSiacoinsMultiPost uses the /wallet/siacoin api endpoint to send money
    96  // to multiple addresses at once
    97  func (c *Client) WalletSiacoinsMultiPost(outputs []types.SiacoinOutput) (wsp api.WalletSiacoinsPOST, err error) {
    98  	values := url.Values{}
    99  	marshaledOutputs, err := json.Marshal(outputs)
   100  	if err != nil {
   101  		return api.WalletSiacoinsPOST{}, err
   102  	}
   103  	values.Set("outputs", string(marshaledOutputs))
   104  	err = c.post("/wallet/siacoins", values.Encode(), &wsp)
   105  	return
   106  }
   107  
   108  // WalletSiacoinsPost uses the /wallet/siacoins api endpoint to send money to a
   109  // single address
   110  func (c *Client) WalletSiacoinsPost(amount types.Currency, destination types.UnlockHash) (wsp api.WalletSiacoinsPOST, err error) {
   111  	values := url.Values{}
   112  	values.Set("amount", amount.String())
   113  	values.Set("destination", destination.String())
   114  	err = c.post("/wallet/siacoins", values.Encode(), &wsp)
   115  	return
   116  }
   117  
   118  // WalletSignPost uses the /wallet/sign api endpoint to sign a transaction.
   119  func (c *Client) WalletSignPost(txn types.Transaction, toSign []crypto.Hash) (wspr api.WalletSignPOSTResp, err error) {
   120  	json, err := json.Marshal(api.WalletSignPOSTParams{
   121  		Transaction: txn,
   122  		ToSign:      toSign,
   123  	})
   124  	if err != nil {
   125  		return
   126  	}
   127  	err = c.post("/wallet/sign", string(json), &wspr)
   128  	return
   129  }
   130  
   131  // WalletSiafundsPost uses the /wallet/siafunds api endpoint to send siafunds
   132  // to a single address.
   133  func (c *Client) WalletSiafundsPost(amount types.Currency, destination types.UnlockHash) (wsp api.WalletSiafundsPOST, err error) {
   134  	values := url.Values{}
   135  	values.Set("amount", amount.String())
   136  	values.Set("destination", destination.String())
   137  	err = c.post("/wallet/siafunds", values.Encode(), &wsp)
   138  	return
   139  }
   140  
   141  // WalletSiagKeyPost uses the /wallet/siagkey endpoint to load a siag key into
   142  // the wallet.
   143  func (c *Client) WalletSiagKeyPost(keyfiles, password string) (err error) {
   144  	values := url.Values{}
   145  	values.Set("keyfiles", keyfiles)
   146  	values.Set("encryptionpassword", password)
   147  	err = c.post("/wallet/siagkey", values.Encode(), nil)
   148  	return
   149  }
   150  
   151  // WalletSweepPost uses the /wallet/sweep/seed endpoint to sweep a seed into
   152  // the current wallet.
   153  func (c *Client) WalletSweepPost(seed string) (wsp api.WalletSweepPOST, err error) {
   154  	values := url.Values{}
   155  	values.Set("seed", seed)
   156  	err = c.post("/wallet/sweep/seed", values.Encode(), &wsp)
   157  	return
   158  }
   159  
   160  // WalletTransactionsGet requests the/wallet/transactions api resource for a
   161  // certain startheight and endheight
   162  func (c *Client) WalletTransactionsGet(startHeight types.BlockHeight, endHeight types.BlockHeight) (wtg api.WalletTransactionsGET, err error) {
   163  	err = c.get(fmt.Sprintf("/wallet/transactions?startheight=%v&endheight=%v",
   164  		startHeight, endHeight), &wtg)
   165  	return
   166  }
   167  
   168  // WalletTransactionGet requests the /wallet/transaction/:id api resource for a
   169  // certain TransactionID.
   170  func (c *Client) WalletTransactionGet(id types.TransactionID) (wtg api.WalletTransactionGETid, err error) {
   171  	err = c.get("/wallet/transaction/"+id.String(), wtg)
   172  	return
   173  }
   174  
   175  // WalletUnlockPost uses the /wallet/unlock endpoint to unlock the wallet with
   176  // a given encryption key. Per default this key is the seed.
   177  func (c *Client) WalletUnlockPost(password string) (err error) {
   178  	values := url.Values{}
   179  	values.Set("encryptionpassword", password)
   180  	err = c.post("/wallet/unlock", values.Encode(), nil)
   181  	return
   182  }
   183  
   184  // WalletUnlockConditionsGet requests the /wallet/unlockconditions endpoint
   185  // and returns the UnlockConditions of addr.
   186  func (c *Client) WalletUnlockConditionsGet(addr types.UnlockHash) (wucg api.WalletUnlockConditionsGET, err error) {
   187  	err = c.get("/wallet/unlockconditions/"+addr.String(), &wucg)
   188  	return
   189  }
   190  
   191  // WalletUnspentGet requests the /wallet/unspent endpoint and returns all of
   192  // the unspent outputs related to the wallet.
   193  func (c *Client) WalletUnspentGet() (wug api.WalletUnspentGET, err error) {
   194  	err = c.get("/wallet/unspent", &wug)
   195  	return
   196  }
   197  
   198  // WalletWatchGet requests the /wallet/watch endpoint and returns the set of
   199  // currently watched addresses.
   200  func (c *Client) WalletWatchGet() (wwg api.WalletWatchGET, err error) {
   201  	err = c.get("/wallet/watch", &wwg)
   202  	return
   203  }
   204  
   205  // WalletWatchAddPost uses the /wallet/watch endpoint to add a set of addresses
   206  // to the watch set. The unused flag should be set to true if the addresses
   207  // have never appeared in the blockchain.
   208  func (c *Client) WalletWatchAddPost(addrs []types.UnlockHash, unused bool) error {
   209  	json, err := json.Marshal(api.WalletWatchPOST{
   210  		Addresses: addrs,
   211  		Remove:    false,
   212  		Unused:    unused,
   213  	})
   214  	if err != nil {
   215  		return err
   216  	}
   217  	return c.post("/wallet/watch", string(json), nil)
   218  }
   219  
   220  // WalletWatchRemovePost uses the /wallet/watch endpoint to remove a set of
   221  // addresses from the watch set. The unused flag should be set to true if the
   222  // addresses have never appeared in the blockchain.
   223  func (c *Client) WalletWatchRemovePost(addrs []types.UnlockHash, unused bool) error {
   224  	json, err := json.Marshal(api.WalletWatchPOST{
   225  		Addresses: addrs,
   226  		Remove:    true,
   227  		Unused:    unused,
   228  	})
   229  	if err != nil {
   230  		return err
   231  	}
   232  	return c.post("/wallet/watch", string(json), nil)
   233  }
   234  
   235  // Wallet033xPost uses the /wallet/033x endpoint to load a v0.3.3.x wallet into
   236  // the current wallet.
   237  func (c *Client) Wallet033xPost(path, password string) (err error) {
   238  	values := url.Values{}
   239  	values.Set("source", path)
   240  	values.Set("encryptionpassword", password)
   241  	err = c.post("/wallet/033x", values.Encode(), nil)
   242  	return
   243  }