forked from stellar/go
/
ledger_key.go
executable file
·87 lines (75 loc) · 2.25 KB
/
ledger_key.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
package xdr
import "fmt"
// LedgerKey implements the `Keyer` interface
func (key *LedgerKey) LedgerKey() LedgerKey {
return *key
}
// Equals returns true if `other` is equivalent to `key`
func (key *LedgerKey) Equals(other LedgerKey) bool {
if key.Type != other.Type {
return false
}
switch key.Type {
case LedgerEntryTypeAccount:
l := key.MustAccount()
r := other.MustAccount()
return l.AccountId.Equals(r.AccountId)
case LedgerEntryTypeData:
l := key.MustData()
r := other.MustData()
return l.AccountId.Equals(r.AccountId) && l.DataName == r.DataName
case LedgerEntryTypeOffer:
l := key.MustOffer()
r := other.MustOffer()
return l.SellerId.Equals(r.SellerId) && l.OfferId == r.OfferId
case LedgerEntryTypeTrustline:
l := key.MustTrustLine()
r := other.MustTrustLine()
return l.AccountId.Equals(r.AccountId) && l.Asset.Equals(r.Asset)
default:
panic(fmt.Errorf("Unknown ledger key type: %v", key.Type))
}
}
// SetAccount mutates `key` such that it represents the identity of `account`
func (key *LedgerKey) SetAccount(account AccountId) error {
data := LedgerKeyAccount{account}
nkey, err := NewLedgerKey(LedgerEntryTypeAccount, data)
if err != nil {
return err
}
*key = nkey
return nil
}
// SetData mutates `key` such that it represents the identity of the
// data entry owned by `account` and for `name`.
func (key *LedgerKey) SetData(account AccountId, name string) error {
data := LedgerKeyData{account, String64(name)}
nkey, err := NewLedgerKey(LedgerEntryTypeData, data)
if err != nil {
return err
}
*key = nkey
return nil
}
// SetOffer mutates `key` such that it represents the identity of the
// data entry owned by `account` and for offer `id`.
func (key *LedgerKey) SetOffer(account AccountId, id uint64) error {
data := LedgerKeyOffer{account, Uint64(id)}
nkey, err := NewLedgerKey(LedgerEntryTypeOffer, data)
if err != nil {
return err
}
*key = nkey
return nil
}
// SetTrustline mutates `key` such that it represents the identity of the
// trustline owned by `account` and for `asset`.
func (key *LedgerKey) SetTrustline(account AccountId, line Asset) error {
data := LedgerKeyTrustLine{account, line}
nkey, err := NewLedgerKey(LedgerEntryTypeTrustline, data)
if err != nil {
return err
}
*key = nkey
return nil
}