-
Notifications
You must be signed in to change notification settings - Fork 95
/
address_restricted.go
149 lines (118 loc) · 4.85 KB
/
address_restricted.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
146
147
148
149
package iotago
import (
"bytes"
"context"
"io"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/byteutils"
"github.com/iotaledger/hive.go/serializer/v2/serix"
"github.com/iotaledger/hive.go/serializer/v2/stream"
"github.com/iotaledger/iota.go/v4/hexutil"
)
type RestrictedAddress struct {
Address Address `serix:""`
AllowedCapabilities AddressCapabilitiesBitMask `serix:",omitempty"`
}
func (addr *RestrictedAddress) Clone() Address {
return &RestrictedAddress{
Address: addr.Address.Clone(),
AllowedCapabilities: addr.AllowedCapabilities.Clone(),
}
}
func (addr *RestrictedAddress) StorageScore(_ *StorageScoreStructure, _ StorageScoreFunc) StorageScore {
return 0
}
func (addr *RestrictedAddress) ID() []byte {
addressID := addr.Address.ID()
capabilities := lo.PanicOnErr(CommonSerixAPI().Encode(context.TODO(), addr.AllowedCapabilities))
// prefix the ID of the underlying address with the AddressType, and append the capabilties
return byteutils.ConcatBytes([]byte{byte(AddressRestricted)}, addressID, capabilities)
}
func (addr *RestrictedAddress) Key() string {
return string(addr.ID())
}
func (addr *RestrictedAddress) Equal(other Address) bool {
otherAddr, is := other.(*RestrictedAddress)
if !is {
return false
}
// check equality of the underlying address and the capabilities
return addr.Address.Equal(otherAddr.Address) && bytes.Equal(addr.AllowedCapabilities, otherAddr.AllowedCapabilities)
}
func (addr *RestrictedAddress) Type() AddressType {
return AddressRestricted
}
func (addr *RestrictedAddress) Bech32(hrp NetworkPrefix) string {
return bech32StringBytes(hrp, addr.ID())
}
func (addr *RestrictedAddress) String() string {
return hexutil.EncodeHex(addr.ID())
}
func (addr *RestrictedAddress) Size() int {
// address type + underlying address + capabilities
return serializer.SmallTypeDenotationByteSize + addr.Address.Size() + addr.AllowedCapabilities.Size()
}
func (addr *RestrictedAddress) CannotReceiveNativeTokens() bool {
return addr.AllowedCapabilities.CannotReceiveNativeTokens()
}
func (addr *RestrictedAddress) CannotReceiveMana() bool {
return addr.AllowedCapabilities.CannotReceiveMana()
}
func (addr *RestrictedAddress) CannotReceiveOutputsWithTimelockUnlockCondition() bool {
return addr.AllowedCapabilities.CannotReceiveOutputsWithTimelockUnlockCondition()
}
func (addr *RestrictedAddress) CannotReceiveOutputsWithExpirationUnlockCondition() bool {
return addr.AllowedCapabilities.CannotReceiveOutputsWithExpirationUnlockCondition()
}
func (addr *RestrictedAddress) CannotReceiveOutputsWithStorageDepositReturnUnlockCondition() bool {
return addr.AllowedCapabilities.CannotReceiveOutputsWithStorageDepositReturnUnlockCondition()
}
func (addr *RestrictedAddress) CannotReceiveAccountOutputs() bool {
return addr.AllowedCapabilities.CannotReceiveAccountOutputs()
}
func (addr *RestrictedAddress) CannotReceiveAnchorOutputs() bool {
return addr.AllowedCapabilities.CannotReceiveAnchorOutputs()
}
func (addr *RestrictedAddress) CannotReceiveNFTOutputs() bool {
return addr.AllowedCapabilities.CannotReceiveNFTOutputs()
}
func (addr *RestrictedAddress) CannotReceiveDelegationOutputs() bool {
return addr.AllowedCapabilities.CannotReceiveDelegationOutputs()
}
func (addr *RestrictedAddress) AllowedCapabilitiesBitMask() AddressCapabilitiesBitMask {
return addr.AllowedCapabilities
}
// RestrictedAddressWithCapabilities returns a restricted address for the given underlying address.
func RestrictedAddressWithCapabilities(address Address, opts ...options.Option[AddressCapabilitiesOptions]) *RestrictedAddress {
return &RestrictedAddress{
Address: address,
AllowedCapabilities: AddressCapabilitiesBitMaskWithCapabilities(opts...),
}
}
// RestrictedAddressFromBytes parses the RestrictedAddress from the given reader.
func RestrictedAddressFromReader(reader io.ReadSeeker) (Address, error) {
// skip the address type byte
if _, err := stream.Skip(reader, serializer.SmallTypeDenotationByteSize); err != nil {
return nil, ierrors.Wrap(err, "unable to skip address type byte")
}
address, err := AddressFromReader(reader)
if err != nil {
return nil, ierrors.Wrap(err, "unable to read restricted address")
}
capabilities, err := stream.ReadBytesWithSize(reader, serializer.SeriLengthPrefixTypeAsByte)
if err != nil {
return nil, ierrors.Wrap(err, "unable to read address capabilities")
}
restrictedAddress := &RestrictedAddress{
Address: address,
AllowedCapabilities: capabilities,
}
_, err = CommonSerixAPI().Encode(context.TODO(), restrictedAddress, serix.WithValidation())
if err != nil {
return nil, ierrors.Wrap(err, "restricted address validation failed")
}
return restrictedAddress, nil
}