Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 128 additions & 10 deletions kbchat/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,149 @@ import (
"github.com/keybase/go-keybase-chat-bot/kbchat/types/stellar1"
)

type WalletOutput struct {
type walletMethod string

type walletTxIDAPIReq struct {
Method walletMethod `json:"method"`
Params walletTxIDParams `json:"params"`
}

type walletTxIDParams struct {
Options txIDOptions `json:"options"`
}

type txIDOptions struct {
TxID string `json:"txid"`
}

type walletSendAPIReq struct {
Method walletMethod `json:"method"`
Params walletSendParams `json:"params"`
}

type walletSendParams struct {
Options sendOptions `json:"options"`
}

type sendOptions struct {
Recipient string `json:"recipient"`
Amount string `json:"amount"`
Currency *string `json:"currency"`
Message *string `json:"message"`
FromAccountID *string `json:"from-account-id"`
MemoText *string `json:"memo-text"`
}

type getRes struct {
Result stellar1.PaymentCLILocal `json:"result"`
Error Error `json:"error,omitempty"`
}

func (a *API) GetWalletTxDetails(txID string) (wOut WalletOutput, err error) {
type sendRes struct {
Result stellar1.SendResultCLILocal `json:"result"`
Error Error `json:"error,omitempty"`
}

type cancelRes struct {
Result stellar1.RelayClaimResult `json:"result"`
Error Error `json:"error,omitempty"`
}

func (a *API) GetWalletTxDetails(txID string) (result stellar1.PaymentCLILocal, err error) {
a.Lock()
defer a.Unlock()

txIDEscaped, err := json.Marshal(txID)
opts := txIDOptions{
TxID: txID,
}
args := walletTxIDAPIReq{Method: "details", Params: walletTxIDParams{Options: opts}}
apiInput, err := json.Marshal(args)
if err != nil {
return wOut, err
return result, err
}
apiInput := fmt.Sprintf(`{"method": "details", "params": {"options": {"txid": %s}}}`, txIDEscaped)
cmd := a.runOpts.Command("wallet", "api")
cmd.Stdin = strings.NewReader(apiInput)
cmd.Stdin = strings.NewReader(string(apiInput))
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return wOut, err
return result, err
}

if err := json.Unmarshal(out.Bytes(), &wOut); err != nil {
return wOut, fmt.Errorf("unable to decode wallet output: %s", err.Error())
response := getRes{}
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
return result, fmt.Errorf("unable to decode wallet output: %s", err.Error())
}
if response.Error.Message != "" {
return result, response.Error
}
return response.Result, nil
}

return wOut, nil
func (a *API) SendWalletTx(recipient string, amount string, currency *string, message *string, fromAccountID *string, memoText *string) (result stellar1.SendResultCLILocal, err error) {
a.Lock()
defer a.Unlock()

opts := sendOptions{
Recipient: recipient,
Amount: amount,
Currency: currency,
Message: message,
FromAccountID: fromAccountID,
MemoText: memoText,
}
args := walletSendAPIReq{Method: "send", Params: walletSendParams{Options: opts}}
apiInput, err := json.Marshal(args)
if err != nil {
return result, err
}
cmd := a.runOpts.Command("wallet", "api")
cmd.Stdin = strings.NewReader(string(apiInput))
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return result, err
}

response := sendRes{}
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
return result, fmt.Errorf("unable to decode wallet output: %s", err.Error())
}
if response.Error.Message != "" {
return result, response.Error
}
return response.Result, nil
}

func (a *API) CancelWalletTx(txID string) (result stellar1.RelayClaimResult, err error) {
a.Lock()
defer a.Unlock()

opts := txIDOptions{
TxID: txID,
}
args := walletTxIDAPIReq{Method: "cancel", Params: walletTxIDParams{Options: opts}}
apiInput, err := json.Marshal(args)
if err != nil {
return result, err
}

cmd := a.runOpts.Command("wallet", "api")
cmd.Stdin = strings.NewReader(string(apiInput))
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return result, err
}

response := cancelRes{}
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
return result, fmt.Errorf("unable to decode wallet output: %s", err.Error())
}
if response.Error.Message != "" {
return result, response.Error
}
return response.Result, nil
}