/
agentid_address.go
76 lines (61 loc) · 1.57 KB
/
agentid_address.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
package isc
import (
"io"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
// AddressAgentID is an AgentID backed by a non-alias address.
type AddressAgentID struct {
a iotago.Address
}
var _ AgentIDWithL1Address = &AddressAgentID{}
func NewAddressAgentID(addr iotago.Address) *AddressAgentID {
return &AddressAgentID{a: addr}
}
func addressAgentIDFromString(s string) (*AddressAgentID, error) {
_, addr, err := iotago.ParseBech32(s)
if err != nil {
return nil, err
}
return &AddressAgentID{a: addr}, nil
}
func (a *AddressAgentID) Address() iotago.Address {
return a.a
}
func (a *AddressAgentID) Bytes() []byte {
return rwutil.WriteToBytes(a)
}
func (a *AddressAgentID) BelongsToChain(ChainID) bool {
return false
}
func (a *AddressAgentID) BytesWithoutChainID() []byte {
return a.Bytes()
}
func (a *AddressAgentID) Equals(other AgentID) bool {
if other == nil {
return false
}
if other.Kind() != a.Kind() {
return false
}
return other.(*AddressAgentID).a.Equal(a.a)
}
func (a *AddressAgentID) Kind() AgentIDKind {
return AgentIDKindAddress
}
func (a *AddressAgentID) String() string {
return a.a.Bech32(parameters.L1().Protocol.Bech32HRP)
}
func (a *AddressAgentID) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.ReadKindAndVerify(rwutil.Kind(a.Kind()))
a.a = AddressFromReader(rr)
return rr.Err
}
func (a *AddressAgentID) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteKind(rwutil.Kind(a.Kind()))
AddressToWriter(ww, a.a)
return ww.Err
}