-
Notifications
You must be signed in to change notification settings - Fork 10
/
principal.go
72 lines (64 loc) · 1.45 KB
/
principal.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
package idl
import (
"bytes"
"fmt"
"math/big"
"github.com/aviate-labs/agent-go/principal"
"github.com/aviate-labs/leb128"
)
type PrincipalType struct {
primType
}
func (PrincipalType) Decode(r *bytes.Reader) (any, error) {
b, err := r.ReadByte()
if err != nil {
return nil, err
}
if b != 0x01 {
return nil, fmt.Errorf("cannot decode principal")
}
l, err := leb128.DecodeUnsigned(r)
if err != nil {
return nil, err
}
if l.Uint64() == 0 {
return principal.Principal{Raw: []byte{}}, nil
}
v := make([]byte, l.Uint64())
if _, err := r.Read(v); err != nil {
return nil, err
}
return principal.Principal{Raw: v}, nil
}
func (PrincipalType) EncodeType(_ *TypeDefinitionTable) ([]byte, error) {
return leb128.EncodeSigned(big.NewInt(principalType))
}
func (PrincipalType) EncodeValue(v any) ([]byte, error) {
v_, ok := v.(principal.Principal)
if !ok {
return nil, NewEncodeValueError(v, principalType)
}
l, err := leb128.EncodeUnsigned(big.NewInt(int64(len(v_.Raw))))
if err != nil {
return nil, err
}
return concat([]byte{0x01}, l, v_.Raw), nil
}
func (PrincipalType) String() string {
return "principal"
}
func (PrincipalType) UnmarshalGo(raw any, _v any) error {
v, ok := _v.(*principal.Principal)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
if p, ok := raw.(principal.Principal); ok {
*v = p
return nil
}
if b, ok := raw.([]byte); ok {
*v = principal.Principal{Raw: b}
return nil
}
return NewUnmarshalGoError(raw, _v)
}