-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaccount.go
171 lines (140 loc) · 4.22 KB
/
account.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
package microstellar
import (
"encoding/base64"
"github.com/stellar/go/clients/horizon"
)
// Address represents a stellar address or public key
type Address string
// Seed represents a stellar seed or private
type Seed string
// KeyPair represents a key pair for a signer on a stellar account. An account
// can have multiple signers.
type KeyPair struct {
Seed string // private key
Address string // public key
}
// Balance is the balance amount of the asset in the account.
type Balance struct {
Asset *Asset `json:"asset"`
Amount string `json:"amount"`
Limit string `json:"limit"`
}
// Signer represents a key that can sign for an account.
type Signer struct {
PublicKey string `json:"public_key"`
Weight int32 `json:"weight"`
Key string `json:"key"`
Type string `json:"type"`
}
// Thresholds represent the signing thresholds on the account
type Thresholds struct {
High byte `json:"high"`
Medium byte `json:"medium"`
Low byte `json:"low"`
}
// Flags contains the auth flags in the account
type Flags struct {
AuthRequired bool `json:"auth_required"`
AuthRevocable bool `json:"auth_revocable"`
AuthImmutable bool `json:"auth_immutable"`
}
// Account represents an account on the stellar network.
type Account struct {
Address string `json:"address"`
Balances []Balance `json:"balances"`
Signers []Signer `json:"signers"`
Flags Flags `json:"flags"`
NativeBalance Balance `json:"native_balance"`
HomeDomain string `json:"home_domain"`
Thresholds Thresholds `json:"thresholds"`
Data map[string]string `json:"data"`
Sequence string `json:"seq"`
}
// newAccount creates a new initialized account
func newAccount() *Account {
account := &Account{}
account.NativeBalance = Balance{NativeAsset, "0", ""}
account.Signers = []Signer{
Signer{},
}
account.Balances = []Balance{}
return account
}
// newAccountFromHorizon creates a new account from a Horizon JSON response.
func newAccountFromHorizon(ha horizon.Account) *Account {
account := newAccount()
account.Address = ha.HistoryAccount.AccountID
account.HomeDomain = ha.HomeDomain
account.Sequence = ha.Sequence
for _, b := range ha.Balances {
if b.Asset.Type == string(NativeType) {
account.NativeBalance = Balance{NativeAsset, b.Balance, ""}
continue
}
balance := Balance{
Asset: NewAsset(b.Asset.Code, b.Asset.Issuer, AssetType(b.Asset.Type)),
Amount: b.Balance,
Limit: b.Limit,
}
account.Balances = append(account.Balances, balance)
}
account.Signers = []Signer{}
for _, s := range ha.Signers {
signer := Signer{
PublicKey: s.PublicKey,
Weight: s.Weight,
Key: s.Key,
Type: s.Type,
}
account.Signers = append(account.Signers, signer)
}
account.Thresholds.High = ha.Thresholds.HighThreshold
account.Thresholds.Medium = ha.Thresholds.MedThreshold
account.Thresholds.Low = ha.Thresholds.LowThreshold
account.Flags.AuthRequired = ha.Flags.AuthRequired
account.Flags.AuthRevocable = ha.Flags.AuthRevocable
account.Data = map[string]string{}
for k, v := range ha.Data {
account.Data[k] = v
}
return account
}
// GetBalance returns the balance for asset in account. If no balance is
// found for the asset, returns "".
func (account *Account) GetBalance(asset *Asset) string {
if asset.IsNative() {
return account.GetNativeBalance()
}
for _, b := range account.Balances {
if asset.Equals(*b.Asset) {
return b.Amount
}
}
return ""
}
// GetNativeBalance returns the balance of the native currency (typically lumens)
// in the account.
func (account *Account) GetNativeBalance() string {
return account.NativeBalance.Amount
}
// GetMasterWeight returns the weight of the primary key in the account.
func (account *Account) GetMasterWeight() int32 {
for _, a := range account.Signers {
if a.PublicKey == account.Address {
return a.Weight
}
}
return -1
}
// GetData decodes and returns the base-64 encoded data in "key"
func (account *Account) GetData(key string) ([]byte, bool) {
v, ok := account.Data[key]
if ok {
decodedVal, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return nil, false
}
return decodedVal, true
}
return nil, false
}