-
Notifications
You must be signed in to change notification settings - Fork 142
/
agentid_eth.go
97 lines (78 loc) · 2.26 KB
/
agentid_eth.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
package isc
import (
"errors"
"fmt"
"io"
"github.com/ethereum/go-ethereum/common"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
// EthereumAddressAgentID is an AgentID formed by an Ethereum address
type EthereumAddressAgentID struct {
chainID ChainID
eth common.Address
}
var _ AgentID = &EthereumAddressAgentID{}
func NewEthereumAddressAgentID(chainID ChainID, eth common.Address) *EthereumAddressAgentID {
return &EthereumAddressAgentID{chainID: chainID, eth: eth}
}
func ethAgentIDFromString(contractPart, chainIDPart string) (*EthereumAddressAgentID, error) {
data, err := iotago.DecodeHex(contractPart)
if err != nil {
return nil, err
}
if len(data) != common.AddressLength {
return nil, errors.New("invalid ETH address string")
}
chainID, err := ChainIDFromString(chainIDPart)
if err != nil {
return nil, fmt.Errorf("invalid chainID: %w", err)
}
return &EthereumAddressAgentID{eth: common.BytesToAddress(data), chainID: chainID}, nil
}
func (a *EthereumAddressAgentID) Bytes() []byte {
return rwutil.WriteToBytes(a)
}
func (a *EthereumAddressAgentID) Equals(other AgentID) bool {
if other == nil {
return false
}
if other.Kind() != a.Kind() {
return false
}
b := other.(*EthereumAddressAgentID)
return b.eth == a.eth && b.chainID.Equals(a.chainID)
}
func (a *EthereumAddressAgentID) EthAddress() common.Address {
return a.eth
}
func (a *EthereumAddressAgentID) ChainID() ChainID {
return a.chainID
}
func (a *EthereumAddressAgentID) BelongsToChain(cID ChainID) bool {
return a.chainID.Equals(cID)
}
func (a *EthereumAddressAgentID) BytesWithoutChainID() []byte {
return a.eth[:]
}
func (a *EthereumAddressAgentID) Kind() AgentIDKind {
return AgentIDKindEthereumAddress
}
func (a *EthereumAddressAgentID) String() string {
// eth.String includes 0x prefix
return a.eth.String() + AgentIDStringSeparator + a.chainID.String()
}
func (a *EthereumAddressAgentID) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.ReadKindAndVerify(rwutil.Kind(a.Kind()))
rr.Read(&a.chainID)
rr.ReadN(a.eth[:])
return rr.Err
}
func (a *EthereumAddressAgentID) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteKind(rwutil.Kind(a.Kind()))
ww.Write(&a.chainID)
ww.WriteN(a.eth[:])
return ww.Err
}