-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.go
137 lines (110 loc) · 3.4 KB
/
utils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package microstellar
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
"github.com/stellar/go/amount"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/strkey"
"github.com/stellar/go/xdr"
)
func debugf(method string, msg string, args ...interface{}) {
logrus.WithFields(logrus.Fields{"lib": "microstellar", "method": method}).Debugf(msg, args...)
}
// ParseAmount converts a currency amount string to an int64
func ParseAmount(v string) (int64, error) {
return amount.ParseInt64(v)
}
// ToAmountString converts an int64 amount to a string
func ToAmountString(v int64) string {
return amount.StringFromInt64(v)
}
// ValidAddress returns error if address is an invalid stellar address
func ValidAddress(address string) error {
_, err := strkey.Decode(strkey.VersionByteAccountID, address)
return errors.Wrap(err, "invalid address")
}
// ValidSeed returns error if the seed is invalid
func ValidSeed(seed string) error {
_, err := strkey.Decode(strkey.VersionByteSeed, seed)
return errors.Wrap(err, "invalid seed")
}
// ValidAddressOrSeed returns true if the string is a valid address or seed
func ValidAddressOrSeed(addressOrSeed string) bool {
err := ValidAddress(addressOrSeed)
if err == nil {
return true
}
err = ValidSeed(addressOrSeed)
return err == nil
}
// ErrorString parses the horizon error out of err.
func ErrorString(err error, showStackTrace ...bool) string {
var errorString string
herr, isHorizonError := errors.Cause(err).(*horizon.Error)
if isHorizonError {
errorString += fmt.Sprintf("%v: %v", herr.Problem.Status, herr.Problem.Title)
resultCodes, err := herr.ResultCodes()
if err == nil {
errorString += fmt.Sprintf(" (%v)", resultCodes)
}
} else {
errorString = fmt.Sprintf("%v", err)
}
if len(showStackTrace) > 0 {
if isHorizonError {
errorString += fmt.Sprintf("\nDetail: %s\nType: %s\n", herr.Problem.Detail, herr.Problem.Type)
}
errorString += fmt.Sprintf("\nStack trace:\n%+v\n", err)
}
return errorString
}
// FundWithFriendBot funds address on the test network with some initial funds.
func FundWithFriendBot(address string) (string, error) {
debugf("FundWithFriendBot", "funding address: %s", address)
resp, err := http.Get("https://friendbot.stellar.org/?addr=" + address)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// DecodeTx extracts a TransactionEnvelope out of the base64-encoded string.
func DecodeTx(base64tx string) (*xdr.TransactionEnvelope, error) {
var tx xdr.TransactionEnvelope
if base64tx[len(base64tx)-1] != '=' {
base64tx = base64tx + "=="
}
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64tx))
_, err := xdr.Unmarshal(reader, &tx)
if err != nil {
return nil, errors.Wrapf(err, "error decoding base64 transaction")
}
return &tx, nil
}
// DecodeTxToJSON converts the base-64 TX to a JSON string.
func DecodeTxToJSON(base64tx string, pretty bool) (string, error) {
xdr, err := DecodeTx(base64tx)
if err != nil {
return "", errors.Wrap(err, "DecodeTxToJSON")
}
var xdrJSON []byte
if pretty {
xdrJSON, err = json.MarshalIndent(xdr, "", " ")
} else {
xdrJSON, err = json.Marshal(xdr)
}
if err != nil {
return "", errors.Wrap(err, "json.MarshalIndent")
}
return string(xdrJSON), nil
}