This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extras.go
175 lines (150 loc) · 3.71 KB
/
extras.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package stellar1
import (
"encoding/hex"
"errors"
"fmt"
"time"
)
const (
KeybaseTransactionIDLen = 16
KeybaseTransactionIDSuffix = 0x30
KeybaseTransactionIDSuffixHex = "30"
)
func KeybaseTransactionIDFromString(s string) (KeybaseTransactionID, error) {
if len(s) != hex.EncodedLen(KeybaseTransactionIDLen) {
return "", fmt.Errorf("bad KeybaseTransactionID %q: must be %d bytes long", s, KeybaseTransactionIDLen)
}
suffix := s[len(s)-2:]
if suffix != KeybaseTransactionIDSuffixHex {
return "", fmt.Errorf("bad KeybaseTransactionID %q: must end in 0x%x", s, KeybaseTransactionIDSuffix)
}
return KeybaseTransactionID(s), nil
}
func (k KeybaseTransactionID) String() string {
return string(k)
}
func (k KeybaseTransactionID) Eq(b KeybaseTransactionID) bool {
return k == b
}
func (t TransactionID) String() string {
return string(t)
}
func (t TransactionID) Eq(b TransactionID) bool {
return t == b
}
func ToTimeMs(t time.Time) TimeMs {
// the result of calling UnixNano on the zero Time is undefined.
// https://golang.org/pkg/time/#Time.UnixNano
if t.IsZero() {
return 0
}
return TimeMs(t.UnixNano() / 1000000)
}
func FromTimeMs(t TimeMs) time.Time {
if t == 0 {
return time.Time{}
}
return time.Unix(0, int64(t)*1000000)
}
func (t TimeMs) Time() time.Time {
return FromTimeMs(t)
}
func (a AccountID) String() string {
return string(a)
}
func (a AccountID) Eq(b AccountID) bool {
return a == b
}
func (a AccountID) IsNil() bool {
return len(a) == 0
}
func (a AccountID) LossyAbbreviation() string {
if len(a) != 56 {
return "[invalid account id]"
}
return fmt.Sprintf("%v...%v", a[:2], a[len(a)-4:len(a)])
}
func (s SecretKey) String() string {
return "[secret key redacted]"
}
func (s SecretKey) SecureNoLogString() string {
return string(s)
}
// CheckInvariants checks that the bundle satisfies
// 1. No duplicate account IDs
// 2. At most one primary account
func (s Bundle) CheckInvariants() error {
accountIDs := make(map[AccountID]bool)
var foundPrimary bool
for _, entry := range s.Accounts {
_, found := accountIDs[entry.AccountID]
if found {
return fmt.Errorf("duplicate account ID: %v", entry.AccountID)
}
accountIDs[entry.AccountID] = true
if entry.IsPrimary {
if foundPrimary {
return errors.New("multiple primary accounts")
}
foundPrimary = true
}
if entry.Mode == AccountMode_NONE {
return errors.New("account missing mode")
}
}
if s.Revision < 1 {
return fmt.Errorf("revision %v < 1", s.Revision)
}
return nil
}
func (s Bundle) PrimaryAccount() (BundleEntry, error) {
for _, entry := range s.Accounts {
if entry.IsPrimary {
return entry, nil
}
}
return BundleEntry{}, errors.New("primary stellar account not found")
}
func (a *Asset) IsNativeXLM() bool {
return a.Type == "native"
}
func AssetNative() Asset {
return Asset{
Type: "native",
Code: "",
Issuer: "",
}
}
func (t TransactionStatus) ToPaymentStatus() PaymentStatus {
switch t {
case TransactionStatus_PENDING:
return PaymentStatus_PENDING
case TransactionStatus_SUCCESS:
return PaymentStatus_COMPLETED
case TransactionStatus_ERROR_TRANSIENT, TransactionStatus_ERROR_PERMANENT:
return PaymentStatus_ERROR
default:
return PaymentStatus_UNKNOWN
}
}
func (t TransactionStatus) Details(errMsg string) (status, detail string) {
switch t {
case TransactionStatus_PENDING:
status = "pending"
case TransactionStatus_SUCCESS:
status = "completed"
case TransactionStatus_ERROR_TRANSIENT, TransactionStatus_ERROR_PERMANENT:
status = "error"
detail = errMsg
default:
status = "unknown"
detail = errMsg
}
return status, detail
}
func NewPaymentLocal(txid TransactionID, ctime TimeMs) *PaymentLocal {
return &PaymentLocal{
Id: txid.String(),
Time: ctime,
}
}