-
Notifications
You must be signed in to change notification settings - Fork 210
/
key.go
52 lines (40 loc) · 1.23 KB
/
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
package keeper
import (
"bytes"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
types "github.com/akash-network/akash-api/go/node/cert/v1beta3"
)
const (
keyAddrPrefixLen = 2 // 1 byte for PrefixCertificateID followed by 1 byte for owner_address_len
)
// certificateKey creates a store key of the format:
// prefix_bytes | owner_address_len (1 byte) | owner_address_bytes | serial_bytes
func certificateKey(id types.CertID) []byte {
buf := bytes.NewBuffer(types.PrefixCertificateID())
if _, err := buf.Write(address.MustLengthPrefix(id.Owner.Bytes())); err != nil {
panic(err)
}
if _, err := buf.Write(id.Serial.Bytes()); err != nil {
panic(err)
}
return buf.Bytes()
}
func certificatePrefix(id sdk.Address) []byte {
buf := bytes.NewBuffer(types.PrefixCertificateID())
if _, err := buf.Write(address.MustLengthPrefix(id.Bytes())); err != nil {
panic(err)
}
return buf.Bytes()
}
func certificateSerialFromKey(key []byte) big.Int {
if len(key) < keyAddrPrefixLen {
panic("invalid key size")
}
addrLen := int(key[keyAddrPrefixLen-1])
if len(key) < keyAddrPrefixLen+addrLen+1 {
panic("invalid key size")
}
return *new(big.Int).SetBytes(key[keyAddrPrefixLen+addrLen:])
}