-
Notifications
You must be signed in to change notification settings - Fork 95
/
address_capabilities_bitmask.go
199 lines (159 loc) · 7 KB
/
address_capabilities_bitmask.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package iotago
import (
"context"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/serializer/v2"
)
const (
canReceiveNativeTokensBitIndex = iota
canReceiveManaBitIndex
canReceiveOutputsWithTimelockUnlockConditionBitIndex
canReceiveOutputsWithExpirationUnlockConditionBitIndex
canReceiveOutputsWithStorageDepositReturnUnlockConditionBitIndex
canReceiveAccountOutputsBitIndex
canReceiveAnchorOutputsBitIndex
canReceiveNFTOutputsBitIndex
canReceiveDelegationOutputsBitIndex
)
// AddressCapabilitiesOptions defines the possible capabilities of an AddressCapabilitiesBitMask.
type AddressCapabilitiesOptions struct {
canReceiveNativeTokens bool
canReceiveMana bool
canReceiveOutputsWithTimelockUnlockCondition bool
canReceiveOutputsWithExpirationUnlockCondition bool
canReceiveOutputsWithStorageDepositReturnUnlockCondition bool
canReceiveAccountOutputs bool
canReceiveAnchorOutputs bool
canReceiveNFTOutputs bool
canReceiveDelegationOutputs bool
}
func WithAddressCanReceiveAnything() options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveNativeTokens = true
o.canReceiveMana = true
o.canReceiveOutputsWithTimelockUnlockCondition = true
o.canReceiveOutputsWithExpirationUnlockCondition = true
o.canReceiveOutputsWithStorageDepositReturnUnlockCondition = true
o.canReceiveAccountOutputs = true
o.canReceiveAnchorOutputs = true
o.canReceiveNFTOutputs = true
o.canReceiveDelegationOutputs = true
}
}
func WithAddressCanReceiveNativeTokens(canReceiveNativeTokens bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveNativeTokens = canReceiveNativeTokens
}
}
func WithAddressCanReceiveMana(canReceiveMana bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveMana = canReceiveMana
}
}
func WithAddressCanReceiveOutputsWithTimelockUnlockCondition(canReceiveOutputsWithTimelockUnlockCondition bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveOutputsWithTimelockUnlockCondition = canReceiveOutputsWithTimelockUnlockCondition
}
}
func WithAddressCanReceiveOutputsWithExpirationUnlockCondition(canReceiveOutputsWithExpirationUnlockCondition bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveOutputsWithExpirationUnlockCondition = canReceiveOutputsWithExpirationUnlockCondition
}
}
func WithAddressCanReceiveOutputsWithStorageDepositReturnUnlockCondition(canReceiveOutputsWithStorageDepositReturnUnlockCondition bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveOutputsWithStorageDepositReturnUnlockCondition = canReceiveOutputsWithStorageDepositReturnUnlockCondition
}
}
func WithAddressCanReceiveAccountOutputs(canReceiveAccountOutputs bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveAccountOutputs = canReceiveAccountOutputs
}
}
func WithAddressCanReceiveAnchorOutputs(canReceiveAnchorOutputs bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveAnchorOutputs = canReceiveAnchorOutputs
}
}
func WithAddressCanReceiveNFTOutputs(canReceiveNFTOutputs bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveNFTOutputs = canReceiveNFTOutputs
}
}
func WithAddressCanReceiveDelegationOutputs(canReceiveDelegationOutputs bool) options.Option[AddressCapabilitiesOptions] {
return func(o *AddressCapabilitiesOptions) {
o.canReceiveDelegationOutputs = canReceiveDelegationOutputs
}
}
type AddressCapabilitiesBitMask []byte
func AddressCapabilitiesBitMaskFromBytes(bytes []byte) (AddressCapabilitiesBitMask, int, error) {
var result AddressCapabilitiesBitMask
consumed, err := CommonSerixAPI().Decode(context.TODO(), bytes, &result)
return result, consumed, err
}
func AddressCapabilitiesBitMaskWithCapabilities(opts ...options.Option[AddressCapabilitiesOptions]) AddressCapabilitiesBitMask {
options := options.Apply(new(AddressCapabilitiesOptions), opts)
bm := []byte{}
if options.canReceiveNativeTokens {
bm = BitMaskSetBit(bm, canReceiveNativeTokensBitIndex)
}
if options.canReceiveMana {
bm = BitMaskSetBit(bm, canReceiveManaBitIndex)
}
if options.canReceiveOutputsWithTimelockUnlockCondition {
bm = BitMaskSetBit(bm, canReceiveOutputsWithTimelockUnlockConditionBitIndex)
}
if options.canReceiveOutputsWithExpirationUnlockCondition {
bm = BitMaskSetBit(bm, canReceiveOutputsWithExpirationUnlockConditionBitIndex)
}
if options.canReceiveOutputsWithStorageDepositReturnUnlockCondition {
bm = BitMaskSetBit(bm, canReceiveOutputsWithStorageDepositReturnUnlockConditionBitIndex)
}
if options.canReceiveAccountOutputs {
bm = BitMaskSetBit(bm, canReceiveAccountOutputsBitIndex)
}
if options.canReceiveAnchorOutputs {
bm = BitMaskSetBit(bm, canReceiveAnchorOutputsBitIndex)
}
if options.canReceiveNFTOutputs {
bm = BitMaskSetBit(bm, canReceiveNFTOutputsBitIndex)
}
if options.canReceiveDelegationOutputs {
bm = BitMaskSetBit(bm, canReceiveDelegationOutputsBitIndex)
}
return bm
}
func (bm AddressCapabilitiesBitMask) Clone() AddressCapabilitiesBitMask {
return lo.CopySlice(bm)
}
func (bm AddressCapabilitiesBitMask) Size() int {
return serializer.SmallTypeDenotationByteSize + len(bm)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveNativeTokens() bool {
return !BitMaskHasBit(bm, canReceiveNativeTokensBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveMana() bool {
return !BitMaskHasBit(bm, canReceiveManaBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveOutputsWithTimelockUnlockCondition() bool {
return !BitMaskHasBit(bm, canReceiveOutputsWithTimelockUnlockConditionBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveOutputsWithExpirationUnlockCondition() bool {
return !BitMaskHasBit(bm, canReceiveOutputsWithExpirationUnlockConditionBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveOutputsWithStorageDepositReturnUnlockCondition() bool {
return !BitMaskHasBit(bm, canReceiveOutputsWithStorageDepositReturnUnlockConditionBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveAccountOutputs() bool {
return !BitMaskHasBit(bm, canReceiveAccountOutputsBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveAnchorOutputs() bool {
return !BitMaskHasBit(bm, canReceiveAnchorOutputsBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveNFTOutputs() bool {
return !BitMaskHasBit(bm, canReceiveNFTOutputsBitIndex)
}
func (bm AddressCapabilitiesBitMask) CannotReceiveDelegationOutputs() bool {
return !BitMaskHasBit(bm, canReceiveDelegationOutputsBitIndex)
}