-
Notifications
You must be signed in to change notification settings - Fork 10
/
account_id.go
45 lines (38 loc) · 1.13 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
package principal
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"hash/crc32"
)
var (
// DefaultSubAccount is the default sub-account represented by the empty byte array.
DefaultSubAccount [32]byte
)
type (
// AccountIdentifier is a unique identifier for an account.
AccountIdentifier [28]byte
// SubAccount is the sub-account identifier for an account.
SubAccount [32]byte
)
// Bytes returns the bytes of the account identifier, including the checksum.
func (id AccountIdentifier) Bytes() []byte {
crc := make([]byte, 4)
binary.BigEndian.PutUint32(crc, crc32.ChecksumIEEE(id[:]))
return append(crc, id[:]...)
}
// String returns the hexadecimal representation of the account identifier.
func (id AccountIdentifier) String() string {
return hex.EncodeToString(id.Bytes())
}
// AccountIdentifier returns the account identifier corresponding with the given sub-account.
func (p Principal) AccountIdentifier(subAccount [32]byte) AccountIdentifier {
h := sha256.New224()
h.Write([]byte("\x0Aaccount-id"))
h.Write(p.Raw)
h.Write(subAccount[:])
bs := h.Sum(nil)
var accountId [28]byte
copy(accountId[:], bs)
return accountId
}