-
Notifications
You must be signed in to change notification settings - Fork 496
/
muxed_account.go
192 lines (170 loc) · 5.03 KB
/
muxed_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package xdr
import (
"fmt"
"github.com/pkg/errors"
"github.com/stellar/go/strkey"
)
func MustMuxedAddress(address string) MuxedAccount {
muxed := MuxedAccount{}
err := muxed.SetAddress(address)
if err != nil {
panic(err)
}
return muxed
}
func MustMuxedAddressPtr(address string) *MuxedAccount {
muxed := MustMuxedAddress(address)
return &muxed
}
func MuxedAccountFromAccountId(gAddress string, id uint64) (MuxedAccount, error) {
accountId, err := AddressToAccountId(gAddress)
if err != nil {
return MuxedAccount{}, err
}
return NewMuxedAccount(
CryptoKeyTypeKeyTypeMuxedEd25519,
MuxedAccountMed25519{
Id: Uint64(id),
Ed25519: accountId.MustEd25519(),
},
)
}
// SetEd25519Address modifies the receiver, setting it's value to the MuxedAccount form
// of the provided G-address. Unlike SetAddress(), it only supports G-addresses.
func (m *MuxedAccount) SetEd25519Address(address string) error {
if m == nil {
return nil
}
switch len(address) {
case 56:
raw, err := strkey.Decode(strkey.VersionByteAccountID, address)
if err != nil {
return err
}
if len(raw) != 32 {
return fmt.Errorf("invalid binary length: %d", len(raw))
}
var ui Uint256
copy(ui[:], raw)
*m, err = NewMuxedAccount(CryptoKeyTypeKeyTypeEd25519, ui)
return err
default:
return errors.New("invalid address length")
}
}
// SetAddress modifies the receiver, setting it's value to the MuxedAccount form
// of the provided strkey G-address or M-address, as described in SEP23.
func (m *MuxedAccount) SetAddress(address string) error {
if m == nil {
return nil
}
switch len(address) {
case 56:
return m.SetEd25519Address(address)
case 69:
raw, err := strkey.Decode(strkey.VersionByteMuxedAccount, address)
if err != nil {
return err
}
if len(raw) != 40 {
return fmt.Errorf("invalid binary length: %d", len(raw))
}
var muxed MuxedAccountMed25519
copy(muxed.Ed25519[:], raw[:32])
if err = muxed.Id.UnmarshalBinary(raw[32:]); err != nil {
return err
}
*m, err = NewMuxedAccount(CryptoKeyTypeKeyTypeMuxedEd25519, muxed)
return err
default:
return errors.New("invalid address length")
}
}
// AddressToMuxedAccount returns an MuxedAccount for a given address string
// or SEP23 M-address.
// If the address is not valid the error returned will not be nil
func AddressToMuxedAccount(address string) (MuxedAccount, error) {
result := MuxedAccount{}
err := result.SetAddress(address)
return result, err
}
// Address returns the strkey-encoded form of this MuxedAccount. In particular, it will
// return an M- strkey representation for CryptoKeyTypeKeyTypeMuxedEd25519 variants of the account
// (according to SEP23). This method will panic if the MuxedAccount is backed by a public key of an
// unknown type.
func (m *MuxedAccount) Address() string {
address, err := m.GetAddress()
if err != nil {
panic(err)
}
return address
}
// GetAddress returns the strkey-encoded form of this MuxedAccount. In particular, it will
// return an M-strkey representation for CryptoKeyTypeKeyTypeMuxedEd25519 variants of the account
// (according to SEP23). In addition it will return an error if the MuxedAccount is backed by a
// public key of an unknown type.
func (m *MuxedAccount) GetAddress() (string, error) {
if m == nil {
return "", nil
}
raw := make([]byte, 0, 40)
switch m.Type {
case CryptoKeyTypeKeyTypeEd25519:
ed, ok := m.GetEd25519()
if !ok {
return "", errors.New("could not get Ed25519")
}
raw = append(raw, ed[:]...)
return strkey.Encode(strkey.VersionByteAccountID, raw)
case CryptoKeyTypeKeyTypeMuxedEd25519:
ed, ok := m.GetMed25519()
if !ok {
return "", errors.New("could not get Med25519")
}
idBytes, err := ed.Id.MarshalBinary()
if err != nil {
return "", errors.Wrap(err, "could not marshal ID")
}
raw = append(raw, ed.Ed25519[:]...)
raw = append(raw, idBytes...)
return strkey.Encode(strkey.VersionByteMuxedAccount, raw)
default:
return "", fmt.Errorf("Unknown muxed account type: %v", m.Type)
}
}
// GetId retrieves the underlying memo ID if this is a fully muxed account. It
// will return an error if the muxed account does not have a memo ID (i.e it's
// of the key type Ed25519).
func (m *MuxedAccount) GetId() (uint64, error) {
if m == nil {
return 0, nil
}
switch m.Type {
case CryptoKeyTypeKeyTypeEd25519:
return 0, errors.New("muxed account has no ID")
case CryptoKeyTypeKeyTypeMuxedEd25519:
ed, ok := m.GetMed25519()
if !ok {
return 0, errors.New("could not get Med25519")
}
return uint64(ed.Id), nil
default:
return 0, fmt.Errorf("Unknown muxed account type: %v", m.Type)
}
}
// ToAccountId transforms a MuxedAccount to an AccountId, dropping the
// memo Id if necessary
func (m MuxedAccount) ToAccountId() AccountId {
result := AccountId{Type: PublicKeyTypePublicKeyTypeEd25519}
switch m.Type {
case CryptoKeyTypeKeyTypeEd25519:
ed := m.MustEd25519()
result.Ed25519 = &ed
case CryptoKeyTypeKeyTypeMuxedEd25519:
ed := m.MustMed25519().Ed25519
result.Ed25519 = &ed
default:
panic(fmt.Errorf("Unknown muxed account type: %v", m.Type))
}
return result
}