-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_id.go
76 lines (62 loc) · 1.54 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
package xdr
import (
"errors"
"fmt"
"github.com/payshares/go/psrkey"
)
// Address returns the psrkey 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 {
if aid == nil {
return ""
}
switch aid.Type {
case PublicKeyTypePublicKeyTypeEd25519:
ed := aid.MustEd25519()
raw := make([]byte, 32)
copy(raw, ed[:])
return psrkey.MustEncode(psrkey.VersionByteAccountID, raw)
default:
panic(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() (ret LedgerKey) {
err := ret.SetAccount(*aid)
if err != nil {
panic(err)
}
return
}
// 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 := psrkey.Decode(psrkey.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
}