/
split_id.go
130 lines (110 loc) · 3.16 KB
/
split_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
package base
import (
"bytes"
"fmt"
"strings"
"github.com/AssetMantle/schema/go/ids"
"github.com/AssetMantle/schema/go/ids/constants"
stringUtilities "github.com/AssetMantle/schema/go/ids/utilities"
)
var _ ids.SplitID = (*SplitID)(nil)
func (splitID *SplitID) ValidateBasic() error {
if splitID == nil {
return fmt.Errorf("split ID is empty")
}
if err := splitID.AssetID.ValidateBasic(); err != nil {
return err
}
if err := splitID.OwnerID.ValidateBasic(); err != nil {
return err
}
return nil
}
func (splitID *SplitID) GetTypeID() ids.StringID {
return NewStringID(constants.SplitIDType)
}
func (*SplitID) FromString(idString string) (ids.ID, error) {
idString = strings.TrimSpace(idString)
if idString == "" {
return PrototypeSplitID(), nil
}
assetIDAndOwnerID := stringUtilities.SplitCompositeIDString(idString)
assetID, err := PrototypeAssetID().FromString(assetIDAndOwnerID[0])
if err != nil {
return PrototypeSplitID(), err
}
ownerID := PrototypeIdentityID()
if len(assetIDAndOwnerID) == 2 {
OwnerID, err := PrototypeIdentityID().FromString(assetIDAndOwnerID[1])
if err != nil {
return PrototypeSplitID(), err
}
ownerID = OwnerID.(*IdentityID)
}
splitID := &SplitID{AssetID: assetID.(*AssetID), OwnerID: ownerID.(*IdentityID)}
if err := splitID.ValidateBasic(); err != nil {
return PrototypeSplitID(), err
}
return splitID, nil
}
func (splitID *SplitID) AsString() string {
return stringUtilities.JoinIDStrings(splitID.AssetID.AsString(), splitID.OwnerID.AsString())
}
func (splitID *SplitID) GetAssetID() ids.AssetID {
return splitID.AssetID
}
func (splitID *SplitID) GetOwnerID() ids.IdentityID {
return splitID.OwnerID
}
func (splitID *SplitID) IsSplitID() {}
func (splitID *SplitID) Bytes() []byte {
// NOTE: if modified, also modify the MustGetFromPrefixedStoreKeyBytes Method
return append(
splitID.AssetID.Bytes(),
splitID.OwnerID.Bytes()...)
}
// TODO optimize this
func (*SplitID) MustGetFromPrefixedStoreKeyBytes(prefixBytes, storeKeyBytes []byte) ids.SplitID {
keyBytes := bytes.TrimPrefix(storeKeyBytes, prefixBytes)
splitID := &SplitID{
AssetID: &AssetID{HashID: &HashID{IDBytes: keyBytes[:len(keyBytes)-32]}},
OwnerID: &IdentityID{HashID: &HashID{IDBytes: keyBytes[len(keyBytes)-32:]}},
}
if bytes.Compare(splitID.Bytes(), keyBytes) != 0 {
panic("invalid key")
}
return splitID
}
func (splitID *SplitID) SplitIDString() string {
return stringUtilities.JoinIDStrings(splitID.AssetID.AsString(), splitID.OwnerID.AsString())
}
func (splitID *SplitID) Compare(id ids.ID) int {
return bytes.Compare(splitID.Bytes(), id.ToAnyID().Get().(*SplitID).Bytes())
}
func (splitID *SplitID) ToAnyID() ids.AnyID {
return &AnyID{
Impl: &AnyID_SplitID{
SplitID: splitID,
},
}
}
func splitIDFromInterface(i interface{}) *SplitID {
switch value := i.(type) {
case *SplitID:
return value
default:
panic(i)
}
}
func NewSplitID(assetID ids.AssetID, ownerID ids.IdentityID) ids.SplitID {
return &SplitID{
AssetID: assetID.(*AssetID),
OwnerID: ownerID.(*IdentityID),
}
}
func PrototypeSplitID() ids.SplitID {
return &SplitID{
AssetID: PrototypeAssetID().(*AssetID),
OwnerID: PrototypeIdentityID().(*IdentityID),
}
}