-
Notifications
You must be signed in to change notification settings - Fork 147
/
agentid.go
145 lines (127 loc) · 3.46 KB
/
agentid.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package isc
import (
"errors"
"io"
"strings"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
type AgentIDKind rwutil.Kind
const (
AgentIDKindNil AgentIDKind = iota
AgentIDKindAddress
AgentIDKindContract
AgentIDKindEthereumAddress
AgentIDIsNil AgentIDKind = 0x80
)
const AgentIDStringSeparator = "@"
// AgentID represents any entity that can hold assets on L2 and/or call contracts.
type AgentID interface {
Bytes() []byte
BelongsToChain(ChainID) bool
BytesWithoutChainID() []byte
Equals(other AgentID) bool
Kind() AgentIDKind
Read(r io.Reader) error
String() string
Write(w io.Writer) error
}
// AgentIDWithL1Address is an AgentID backed by an L1 address (either AddressAgentID or ContractAgentID).
type AgentIDWithL1Address interface {
AgentID
Address() iotago.Address
}
// AddressFromAgentID returns the L1 address of the AgentID, if applicable.
func AddressFromAgentID(a AgentID) (iotago.Address, bool) {
wa, ok := a.(AgentIDWithL1Address)
if !ok {
return nil, false
}
return wa.Address(), true
}
// HnameFromAgentID returns the hname of the AgentID, or HnameNil if not applicable.
func HnameFromAgentID(a AgentID) Hname {
if ca, ok := a.(*ContractAgentID); ok {
return ca.Hname()
}
return HnameNil
}
// NewAgentID creates an AddressAgentID if the address is not an AliasAddress;
// otherwise a ContractAgentID with hname = HnameNil.
func NewAgentID(addr iotago.Address) AgentID {
if addr.Type() == iotago.AddressAlias {
chainID := ChainIDFromAddress(addr.(*iotago.AliasAddress))
return NewContractAgentID(chainID, HnameNil)
}
return NewAddressAgentID(addr)
}
func AgentIDFromBytes(data []byte) (AgentID, error) {
rr := rwutil.NewBytesReader(data)
return AgentIDFromReader(rr), rr.Err
}
func AgentIDFromReader(rr *rwutil.Reader) (ret AgentID) {
kind := rr.ReadKind()
switch AgentIDKind(kind) {
case AgentIDIsNil:
return nil
case AgentIDKindNil:
ret = new(NilAgentID)
case AgentIDKindAddress:
ret = new(AddressAgentID)
case AgentIDKindContract:
ret = new(ContractAgentID)
case AgentIDKindEthereumAddress:
ret = new(EthereumAddressAgentID)
default:
if rr.Err == nil {
rr.Err = errors.New("invalid AgentID kind")
return nil
}
}
rr.PushBack().WriteKind(kind)
rr.Read(ret)
return ret
}
func AgentIDToWriter(ww *rwutil.Writer, agent AgentID) {
if agent == nil {
ww.WriteKind(rwutil.Kind(AgentIDIsNil))
return
}
ww.Write(agent)
}
// AgentIDFromString parses the human-readable string representation
func AgentIDFromString(s string) (AgentID, error) {
if s == nilAgentIDString {
return &NilAgentID{}, nil
}
var contractPart, addrPart string
{
parts := strings.Split(s, AgentIDStringSeparator)
switch len(parts) {
case 1:
addrPart = parts[0]
case 2:
addrPart = parts[1]
contractPart = parts[0]
default:
return nil, errors.New("invalid AgentID format")
}
}
if contractPart != "" {
if strings.HasPrefix(contractPart, "0x") {
return ethAgentIDFromString(contractPart, addrPart)
}
return contractAgentIDFromString(contractPart, addrPart)
}
if strings.HasPrefix(addrPart, string(parameters.L1().Protocol.Bech32HRP)) {
return addressAgentIDFromString(s)
}
return nil, errors.New("invalid AgentID string")
}
// NewRandomAgentID creates random AgentID
func NewRandomAgentID() AgentID {
return NewContractAgentID(RandomChainID(), Hn("testName"))
}