/
account_id.go
133 lines (113 loc) · 3.06 KB
/
account_id.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
package xdr
import (
"errors"
"fmt"
"github.com/stellar/go/strkey"
)
// Address returns the strkey encoded form of this AccountId. This method will
// panic if the accountid is backed by a public key of an unknown type.
func (aid AccountId) Address() string {
address, err := aid.GetAddress()
if err != nil {
panic(err)
}
return address
}
// GetAddress returns the strkey encoded form of this AccountId, and an error
// if the AccountId is backed by a public key of an unknown type.
func (aid *AccountId) GetAddress() (string, error) {
if aid == nil {
return "", nil
}
switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
ed, ok := aid.GetEd25519()
if !ok {
return "", fmt.Errorf("Could not get Ed25519")
}
raw := make([]byte, 32)
copy(raw, ed[:])
return strkey.Encode(strkey.VersionByteAccountID, raw)
default:
return "", fmt.Errorf("Unknown account id type: %v", aid.Type)
}
}
// Equals returns true if `other` is equivalent to `aid`
func (aid *AccountId) Equals(other AccountId) bool {
if aid.Type != other.Type {
return false
}
switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
l := aid.MustEd25519()
r := other.MustEd25519()
return l == r
default:
panic(fmt.Errorf("Unknown account id type: %v", aid.Type))
}
}
// LedgerKey implements the `Keyer` interface
func (aid *AccountId) LedgerKey() (key LedgerKey, err error) {
return key, key.SetAccount(*aid)
}
func (e *EncodingBuffer) accountIdCompressEncodeTo(aid AccountId) error {
if err := e.xdrEncoderBuf.WriteByte(byte(aid.Type)); err != nil {
return err
}
switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
_, err := e.xdrEncoderBuf.Write(aid.Ed25519[:])
return err
default:
panic("Unknown type")
}
}
func MustAddress(address string) AccountId {
aid := AccountId{}
err := aid.SetAddress(address)
if err != nil {
panic(err)
}
return aid
}
func MustAddressPtr(address string) *AccountId {
aid := MustAddress(address)
return &aid
}
// AddressToAccountId returns an AccountId for a given address string.
// If the address is not valid the error returned will not be nil
func AddressToAccountId(address string) (AccountId, error) {
result := AccountId{}
err := result.SetAddress(address)
return result, err
}
// SetAddress modifies the receiver, setting it's value to the AccountId form
// of the provided address.
func (aid *AccountId) SetAddress(address string) error {
if aid == nil {
return nil
}
raw, err := strkey.Decode(strkey.VersionByteAccountID, address)
if err != nil {
return err
}
if len(raw) != 32 {
return errors.New("invalid address")
}
var ui Uint256
copy(ui[:], raw)
*aid, err = NewAccountId(PublicKeyTypePublicKeyTypeEd25519, ui)
return err
}
// ToMuxedAccount transforms an AccountId into a MuxedAccount with
// a zero memo id
func (aid *AccountId) ToMuxedAccount() MuxedAccount {
result := MuxedAccount{Type: CryptoKeyTypeKeyTypeEd25519}
switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
result.Ed25519 = aid.Ed25519
default:
panic(fmt.Errorf("Unknown account id type: %v", aid.Type))
}
return result
}