This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
wallet_client.go
66 lines (59 loc) · 2.09 KB
/
wallet_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package client
import (
"encoding/json"
"fmt"
"github.com/threefoldtech/rivine/pkg/api"
"github.com/threefoldtech/rivine/types"
)
// WalletClient is used to easily interact with the wallet through the HTTP REST API.
type WalletClient struct {
bc BaseClient
}
// NewWalletClient creates a new WalletClient,
// that can be used for easy interaction with the Wallet API exposed via the HTTP REST API.
func NewWalletClient(bc BaseClient) *WalletClient {
if bc == nil {
panic("no BaseClient given")
}
return &WalletClient{
bc: bc,
}
}
// NewPublicKey creates a new public key (from an index and the wallet's primary seed), and returns it.
func (wallet *WalletClient) NewPublicKey() (types.PublicKey, error) {
var result api.WalletPublicKeyGET
err := wallet.bc.HTTP().GetWithResponse("/wallet/publickey", &result)
if err != nil {
return types.PublicKey{}, fmt.Errorf("failed to get (new) public key: %v", err)
}
return result.PublicKey, nil
}
// FundCoins collects coin inputs owned by this daemon's wallet,
// that are sufficient to fund the given amount, optionally returning a refund coin output as well.
func (wallet *WalletClient) FundCoins(amount types.Currency, refundAddress *types.UnlockHash, newRefundAddress bool) ([]types.CoinInput, *types.CoinOutput, error) {
var result api.WalletFundCoins
r := fmt.Sprintf("/wallet/fund/coins?amount=%s", amount.String())
if refundAddress != nil {
r += "&refund=" + refundAddress.String()
} else {
r += fmt.Sprintf("&refund=%t", newRefundAddress)
}
err := wallet.bc.HTTP().GetWithResponse(r, &result)
if err != nil {
return nil, nil, fmt.Errorf("failed to get fund coins: %v", err)
}
return result.CoinInputs, result.RefundCoinOutput, nil
}
// GreedySignTx signs the given transactions greedy,
// meaning that all fulfillments that can be signed, will be signed.
func (wallet *WalletClient) GreedySignTx(t *types.Transaction) error {
b, err := json.Marshal(t)
if err != nil {
return err
}
err = wallet.bc.HTTP().PostWithResponse("/wallet/sign", string(b), t)
if err != nil {
return fmt.Errorf("Failed to sign transaction: %v", err)
}
return nil
}