github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/node/api/client/wallet.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"strconv"
     8  
     9  	"SiaPrime/crypto"
    10  	"SiaPrime/node/api"
    11  	"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  // WalletLockPost uses the /wallet/lock endpoint to lock the wallet.
    65  func (c *Client) WalletLockPost() (err error) {
    66  	err = c.post("/wallet/lock", "", nil)
    67  	return
    68  }
    69  
    70  // WalletSeedPost uses the /wallet/seed endpoint to add a seed to the wallet's list
    71  // of seeds.
    72  func (c *Client) WalletSeedPost(seed, password string) (err error) {
    73  	values := url.Values{}
    74  	values.Set("seed", seed)
    75  	values.Set("encryptionpassword", password)
    76  	err = c.post("/wallet/seed", values.Encode(), nil)
    77  	return
    78  }
    79  
    80  // WalletSeedsGet uses the /wallet/seeds endpoint to return the wallet's
    81  // current seeds.
    82  func (c *Client) WalletSeedsGet() (wsg api.WalletSeedsGET, err error) {
    83  	err = c.get("/wallet/seeds", &wsg)
    84  	return
    85  }
    86  
    87  // WalletSiacoinsMultiPost uses the /wallet/siacoin api endpoint to send money
    88  // to multiple addresses at once
    89  func (c *Client) WalletSiacoinsMultiPost(outputs []types.SiacoinOutput) (wsp api.WalletSiacoinsPOST, err error) {
    90  	values := url.Values{}
    91  	marshaledOutputs, err := json.Marshal(outputs)
    92  	if err != nil {
    93  		return api.WalletSiacoinsPOST{}, err
    94  	}
    95  	values.Set("outputs", string(marshaledOutputs))
    96  	err = c.post("/wallet/siacoins", values.Encode(), &wsp)
    97  	return
    98  }
    99  
   100  // WalletSiacoinsPost uses the /wallet/siacoins api endpoint to send money to a
   101  // single address
   102  func (c *Client) WalletSiacoinsPost(amount types.Currency, destination types.UnlockHash) (wsp api.WalletSiacoinsPOST, err error) {
   103  	values := url.Values{}
   104  	values.Set("amount", amount.String())
   105  	values.Set("destination", destination.String())
   106  	err = c.post("/wallet/siacoins", values.Encode(), &wsp)
   107  	return
   108  }
   109  
   110  // WalletSignPost uses the /wallet/sign api endpoint to sign a transaction.
   111  func (c *Client) WalletSignPost(txn types.Transaction, toSign []crypto.Hash) (wspr api.WalletSignPOSTResp, err error) {
   112  	json, err := json.Marshal(api.WalletSignPOSTParams{
   113  		Transaction: txn,
   114  		ToSign:      toSign,
   115  	})
   116  	if err != nil {
   117  		return
   118  	}
   119  	err = c.post("/wallet/sign", string(json), &wspr)
   120  	return
   121  }
   122  
   123  // WalletSiafundsPost uses the /wallet/siafunds api endpoint to send siafunds
   124  // to a single address.
   125  func (c *Client) WalletSiafundsPost(amount types.Currency, destination types.UnlockHash) (wsp api.WalletSiafundsPOST, err error) {
   126  	values := url.Values{}
   127  	values.Set("amount", amount.String())
   128  	values.Set("destination", destination.String())
   129  	err = c.post("/wallet/siafunds", values.Encode(), &wsp)
   130  	return
   131  }
   132  
   133  // WalletSiagKeyPost uses the /wallet/siagkey endpoint to load a siag key into
   134  // the wallet.
   135  func (c *Client) WalletSiagKeyPost(keyfiles, password string) (err error) {
   136  	values := url.Values{}
   137  	values.Set("keyfiles", keyfiles)
   138  	values.Set("encryptionpassword", password)
   139  	err = c.post("/wallet/siagkey", values.Encode(), nil)
   140  	return
   141  }
   142  
   143  // WalletSweepPost uses the /wallet/sweep/seed endpoint to sweep a seed into
   144  // the current wallet.
   145  func (c *Client) WalletSweepPost(seed string) (wsp api.WalletSweepPOST, err error) {
   146  	values := url.Values{}
   147  	values.Set("seed", seed)
   148  	err = c.post("/wallet/sweep/seed", values.Encode(), &wsp)
   149  	return
   150  }
   151  
   152  // WalletTransactionsGet requests the/wallet/transactions api resource for a
   153  // certain startheight and endheight
   154  func (c *Client) WalletTransactionsGet(startHeight types.BlockHeight, endHeight types.BlockHeight) (wtg api.WalletTransactionsGET, err error) {
   155  	err = c.get(fmt.Sprintf("/wallet/transactions?startheight=%v&endheight=%v",
   156  		startHeight, endHeight), &wtg)
   157  	return
   158  }
   159  
   160  // WalletTransactionGet requests the /wallet/transaction/:id api resource for a
   161  // certain TransactionID.
   162  func (c *Client) WalletTransactionGet(id types.TransactionID) (wtg api.WalletTransactionGETid, err error) {
   163  	err = c.get("/wallet/transaction/"+id.String(), wtg)
   164  	return
   165  }
   166  
   167  // WalletUnlockPost uses the /wallet/unlock endpoint to unlock the wallet with
   168  // a given encryption key. Per default this key is the seed.
   169  func (c *Client) WalletUnlockPost(password string) (err error) {
   170  	values := url.Values{}
   171  	values.Set("encryptionpassword", password)
   172  	err = c.post("/wallet/unlock", values.Encode(), nil)
   173  	return
   174  }
   175  
   176  // WalletUnlockConditionsGet requests the /wallet/unlockconditions endpoint
   177  // and returns the UnlockConditions of addr.
   178  func (c *Client) WalletUnlockConditionsGet(addr types.UnlockHash) (wucg api.WalletUnlockConditionsGET, err error) {
   179  	err = c.get("/wallet/unlockconditions/"+addr.String(), &wucg)
   180  	return
   181  }
   182  
   183  // WalletUnspentGet requests the /wallet/unspent endpoint and returns all of
   184  // the unspent outputs related to the wallet.
   185  func (c *Client) WalletUnspentGet() (wug api.WalletUnspentGET, err error) {
   186  	err = c.get("/wallet/unspent", &wug)
   187  	return
   188  }
   189  
   190  // WalletWatchGet requests the /wallet/watch endpoint and returns the set of
   191  // currently watched addresses.
   192  func (c *Client) WalletWatchGet() (wwg api.WalletWatchGET, err error) {
   193  	err = c.get("/wallet/watch", &wwg)
   194  	return
   195  }
   196  
   197  // WalletWatchPost uses the /wallet/watch endpoint to add a set of addresses
   198  // to the watch set. The unused flag should be set to true if the addresses
   199  // have never appeared in the blockchain.
   200  func (c *Client) WalletWatchAddPost(addrs []types.UnlockHash, unused bool) error {
   201  	json, err := json.Marshal(api.WalletWatchPOST{
   202  		Addresses: addrs,
   203  		Remove:    false,
   204  		Unused:    unused,
   205  	})
   206  	if err != nil {
   207  		return err
   208  	}
   209  	return c.post("/wallet/watch", string(json), nil)
   210  }
   211  
   212  // WalletWatchPost uses the /wallet/watch endpoint to remove a set of
   213  // addresses from the watch set. The unused flag should be set to true if the
   214  // addresses have never appeared in the blockchain.
   215  func (c *Client) WalletWatchRemovePost(addrs []types.UnlockHash, unused bool) error {
   216  	json, err := json.Marshal(api.WalletWatchPOST{
   217  		Addresses: addrs,
   218  		Remove:    true,
   219  		Unused:    unused,
   220  	})
   221  	if err != nil {
   222  		return err
   223  	}
   224  	return c.post("/wallet/watch", string(json), nil)
   225  }
   226  
   227  // Wallet033xPost uses the /wallet/033x endpoint to load a v0.3.3.x wallet into
   228  // the current wallet.
   229  func (c *Client) Wallet033xPost(path, password string) (err error) {
   230  	values := url.Values{}
   231  	values.Set("source", path)
   232  	values.Set("encryptionpassword", password)
   233  	err = c.post("/wallet/033x", values.Encode(), nil)
   234  	return
   235  }