Skip to content

Commit 80a0388

Browse files
Merge pull request #197 from kpitapeersyst/xrpl/feat/contains-flag-util
feat(xrpl): add contains flag utility
2 parents c106bc0 + 77f748a commit 80a0388

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+831
-674
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
#### xrpl
13+
14+
- Added `flag` package with `Contains` utility function to check if a flag is fully set within a combined flag value.
15+
1016
### Fixed
1117

1218
#### xrpl

docs/docs/xrpl/flag.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# flag
2+
3+
## Overview
4+
5+
The `flag` package provides utility functions for working with bitwise flags in XRPL.
6+
7+
- `Contains`: Checks whether a flag is fully set within a combined flag value. Useful for inspecting transaction or ledger object flags.
8+
9+
## Usage
10+
11+
To import the package, you can use the following code:
12+
13+
```go
14+
import "github.com/Peersyst/xrpl-go/xrpl/flag"
15+
```
16+
17+
## API
18+
19+
### Contains
20+
21+
```go
22+
func Contains(currentFlag uint32, flag uint32) bool
23+
```
24+
25+
Returns `true` if all bits of `flag` are set in `currentFlag` (`(currentFlag & flag) == flag`). Returns `false` if any bit of `flag` is missing in `currentFlag`, or if `flag` is `0`.
26+
27+
:::warning
28+
29+
The comparison is based on the flag value as a `uint32`. Different contexts may use the same numeric values (e.g. a transaction flag and a ledger-state flag), so a match only indicates the bit is set — not that it belongs to a specific context. Always pair `Contains` with the flag constant that matches the context you are checking.
30+
31+
:::
32+
33+
### Example
34+
35+
```go
36+
package main
37+
38+
import (
39+
"fmt"
40+
41+
"github.com/Peersyst/xrpl-go/xrpl/flag"
42+
"github.com/Peersyst/xrpl-go/xrpl/transaction"
43+
)
44+
45+
func main() {
46+
offer := transaction.OfferCreate{
47+
BaseTx: transaction.BaseTx{
48+
Account: "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
49+
},
50+
TakerGets: nil,
51+
TakerPays: nil,
52+
}
53+
54+
// Set the sell flag on the offer
55+
offer.SetSellFlag()
56+
57+
// Check whether the sell flag is set
58+
if flag.Contains(offer.Flags, transaction.TfSell) {
59+
fmt.Println("Offer is a sell offer")
60+
}
61+
62+
// Check whether the fill-or-kill flag is set (it is not)
63+
if !flag.Contains(offer.Flags, transaction.TfFillOrKill) {
64+
fmt.Println("Offer is not fill-or-kill")
65+
}
66+
}
67+
```

examples/mptoken/rpc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func main() {
7272
MaximumAmount: &amount,
7373
MPTokenMetadata: types.MPTokenMetadata("464F4F"), // "FOO" in hex
7474
}
75-
// Since TransferFee is provided, enable the tfMPTCanTransfer flag.
75+
// Since TransferFee is provided, enable the TfMPTCanTransfer flag.
7676
issuanceTx.SetMPTCanTransferFlag()
7777

7878
// Flatten, autofill, sign, and submit the transaction.

examples/mptoken/ws/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func main() {
8787
MaximumAmount: &amount,
8888
MPTokenMetadata: types.MPTokenMetadata("464F4F"), // "FOO" in hex
8989
}
90-
// Since TransferFee is provided, enable the tfMPTCanTransfer flag.
90+
// Since TransferFee is provided, enable the TfMPTCanTransfer flag.
9191
issuanceTx.SetMPTCanTransferFlag()
9292

9393
// Flatten, autofill, sign, and submit the transaction.

xrpl/flag/flag.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Package flag provides utility functions for working with bitwise flags.
2+
package flag
3+
4+
// Contains checks if all bits of flag are present in currentFlag.
5+
// Returns false if flag is 0.
6+
// Note: It should be taken into account that the comparison is based on the flag value as a uint32.
7+
// Different contexts may use same values, and they will return ok as the value matches.
8+
func Contains(currentFlag, flag uint32) bool {
9+
return flag != 0 && (currentFlag&flag) == flag
10+
}

xrpl/flag/flag_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package flag
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
const (
10+
dummyFlagA = 262144
11+
dummyFlagB = 131072
12+
dummyFlagC = 65536
13+
)
14+
15+
func TestFlag_Contains(t *testing.T) {
16+
testCases := []struct {
17+
name string
18+
currentFlag uint32
19+
flag uint32
20+
expected bool
21+
}{
22+
{
23+
name: "pass - same flag",
24+
currentFlag: dummyFlagA,
25+
flag: dummyFlagA,
26+
expected: true,
27+
},
28+
{
29+
name: "pass - containing flag",
30+
currentFlag: dummyFlagA | dummyFlagB,
31+
flag: dummyFlagA,
32+
expected: true,
33+
},
34+
{
35+
name: "pass - not containing flag",
36+
currentFlag: dummyFlagA | dummyFlagB,
37+
flag: dummyFlagC,
38+
expected: false,
39+
},
40+
{
41+
name: "pass - zero flag",
42+
currentFlag: dummyFlagA,
43+
flag: 0,
44+
expected: false,
45+
},
46+
{
47+
name: "pass - zero current flag",
48+
currentFlag: 0,
49+
flag: 0,
50+
expected: false,
51+
},
52+
{
53+
name: "pass - partial overlap multi-bit flag",
54+
currentFlag: dummyFlagA,
55+
flag: dummyFlagA | dummyFlagB,
56+
expected: false,
57+
},
58+
}
59+
for _, tc := range testCases {
60+
t.Run(tc.name, func(t *testing.T) {
61+
actual := Contains(tc.currentFlag, tc.flag)
62+
assert.Equal(t, tc.expected, actual)
63+
})
64+
}
65+
}

xrpl/ledger-entry-types/account_root.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@ import (
66
)
77

88
const (
9-
// Enable Clawback for this account. (Requires the Clawback amendment.)
10-
lsfAllowTrustLineClawback uint32 = 0x80000000
11-
// Allow IOUs to be used as escrow amounts for an issuer
12-
lsfAllowTrustLineLocking uint32 = 0x40000000
13-
// Enable rippling on this addresses's trust lines by default. Required for issuing addresses; discouraged for others.
14-
lsfDefaultRipple uint32 = 0x00800000
15-
// This account has DepositAuth enabled, meaning it can only receive funds from transactions it sends, and from preauthorized accounts. (Added by the DepositAuth amendment)
16-
lsfDepositAuth uint32 = 0x01000000
17-
// Disallows use of the master key to sign transactions for this account.
18-
lsfDisableMaster uint32 = 0x00100000
19-
// This account blocks incoming Checks. (Added by the DisallowIncoming amendment.)
20-
lsfDisallowIncomingCheck uint32 = 0x08000000
21-
// This account blocks incoming NFTokenOffers. (Added by the DisallowIncoming amendment.)
22-
lsfDisallowIncomingNFTokenOffer uint32 = 0x04000000
23-
// This account blocks incoming Payment Channels. (Added by the DisallowIncoming amendment.)
24-
lsfDisallowIncomingPayChan uint32 = 0x10000000
25-
// This account blocks incoming trust lines. (Added by the DisallowIncoming amendment.)
26-
lsfDisallowIncomingTrustline uint32 = 0x20000000
27-
// Client applications should not send XRP to this account. (Advisory; not enforced by the protocol.)
28-
lsfDisallowXRP uint32 = 0x00080000
29-
// All assets issued by this account are frozen.
30-
lsfGlobalFreeze uint32 = 0x00400000
31-
// This account cannot freeze trust lines connected to it. Once enabled, cannot be disabled.
32-
lsfNoFreeze uint32 = 0x00200000
33-
// This account has used its free SetRegularKey transaction.
34-
lsfPasswordSpent uint32 = 0x00010000
35-
// This account must individually approve other users for those users to hold this account's tokens.
36-
lsfRequireAuth uint32 = 0x00040000
37-
// Requires incoming payments to specify a Destination Tag.
38-
lsfRequireDestTag uint32 = 0x00020000
9+
// LsfAllowTrustLineClawback enables Clawback for this account. (Requires the Clawback amendment.)
10+
LsfAllowTrustLineClawback uint32 = 0x80000000
11+
// LsfAllowTrustLineLocking allows IOUs to be used as escrow amounts for an issuer
12+
LsfAllowTrustLineLocking uint32 = 0x40000000
13+
// LsfDefaultRipple enables rippling on this addresses's trust lines by default. Required for issuing addresses; discouraged for others.
14+
LsfDefaultRipple uint32 = 0x00800000
15+
// LsfDepositAuth this account has DepositAuth enabled, meaning it can only receive funds from transactions it sends, and from preauthorized accounts. (Added by the DepositAuth amendment)
16+
LsfDepositAuth uint32 = 0x01000000
17+
// LsfDisableMaster disallows use of the master key to sign transactions for this account.
18+
LsfDisableMaster uint32 = 0x00100000
19+
// LsfDisallowIncomingCheck this account blocks incoming Checks. (Added by the DisallowIncoming amendment.)
20+
LsfDisallowIncomingCheck uint32 = 0x08000000
21+
// LsfDisallowIncomingNFTokenOffer this account blocks incoming NFTokenOffers. (Added by the DisallowIncoming amendment.)
22+
LsfDisallowIncomingNFTokenOffer uint32 = 0x04000000
23+
// LsfDisallowIncomingPayChan this account blocks incoming Payment Channels. (Added by the DisallowIncoming amendment.)
24+
LsfDisallowIncomingPayChan uint32 = 0x10000000
25+
// LsfDisallowIncomingTrustline this account blocks incoming trust lines. (Added by the DisallowIncoming amendment.)
26+
LsfDisallowIncomingTrustline uint32 = 0x20000000
27+
// LsfDisallowXRP client applications should not send XRP to this account. (Advisory; not enforced by the protocol.)
28+
LsfDisallowXRP uint32 = 0x00080000
29+
// LsfGlobalFreeze all assets issued by this account are frozen.
30+
LsfGlobalFreeze uint32 = 0x00400000
31+
// LsfNoFreeze this account cannot freeze trust lines connected to it. Once enabled, cannot be disabled.
32+
LsfNoFreeze uint32 = 0x00200000
33+
// LsfPasswordSpent this account has used its free SetRegularKey transaction.
34+
LsfPasswordSpent uint32 = 0x00010000
35+
// LsfRequireAuth this account must individually approve other users for those users to hold this account's tokens.
36+
LsfRequireAuth uint32 = 0x00040000
37+
// LsfRequireDestTag requires incoming payments to specify a Destination Tag.
38+
LsfRequireDestTag uint32 = 0x00020000
3939
)
4040

4141
// AccountRoot ledger entry type describes a single account, its settings, and XRP balance.
@@ -73,7 +73,7 @@ type AccountRoot struct {
7373
Account types.Address
7474
// The identifying hash of the transaction most recently sent by this account.
7575
// This field must be enabled to use the AccountTxnID transaction field.
76-
// To enable it, send an AccountSet transaction with the asfAccountTxnID flag enabled.
76+
// To enable it, send an AccountSet transaction with the AsfAccountTxnID flag enabled.
7777
AccountTxnID types.Hash256 `json:",omitempty"`
7878
// (Added by the AMM amendment) The ledger entry ID of the corresponding AMM ledger entry.
7979
// Set during account creation; cannot be modified. If present, indicates that this is a
@@ -135,75 +135,75 @@ func (*AccountRoot) EntryType() EntryType {
135135

136136
// SetLsfAllowTrustLineClawback sets the AllowTrustLineClawback flag.
137137
func (a *AccountRoot) SetLsfAllowTrustLineClawback() {
138-
a.Flags |= lsfAllowTrustLineClawback
138+
a.Flags |= LsfAllowTrustLineClawback
139139
}
140140

141141
// SetLsfAllowTrustLineLocking sets the AllowTrustLineLocking flag.
142142
func (a *AccountRoot) SetLsfAllowTrustLineLocking() {
143-
a.Flags |= lsfAllowTrustLineLocking
143+
a.Flags |= LsfAllowTrustLineLocking
144144
}
145145

146146
// SetLsfDefaultRipple sets the DefaultRipple flag.
147147
func (a *AccountRoot) SetLsfDefaultRipple() {
148-
a.Flags |= lsfDefaultRipple
148+
a.Flags |= LsfDefaultRipple
149149
}
150150

151151
// SetLsfDepositAuth sets the DepositAuth flag.
152152
func (a *AccountRoot) SetLsfDepositAuth() {
153-
a.Flags |= lsfDepositAuth
153+
a.Flags |= LsfDepositAuth
154154
}
155155

156156
// SetLsfDisableMaster sets the DisableMaster flag.
157157
func (a *AccountRoot) SetLsfDisableMaster() {
158-
a.Flags |= lsfDisableMaster
158+
a.Flags |= LsfDisableMaster
159159
}
160160

161161
// SetLsfDisallowIncomingCheck sets the DisallowIncomingCheck flag.
162162
func (a *AccountRoot) SetLsfDisallowIncomingCheck() {
163-
a.Flags |= lsfDisallowIncomingCheck
163+
a.Flags |= LsfDisallowIncomingCheck
164164
}
165165

166166
// SetLsfDisallowIncomingNFTokenOffer sets the DisallowIncomingNFTokenOffer flag.
167167
func (a *AccountRoot) SetLsfDisallowIncomingNFTokenOffer() {
168-
a.Flags |= lsfDisallowIncomingNFTokenOffer
168+
a.Flags |= LsfDisallowIncomingNFTokenOffer
169169
}
170170

171171
// SetLsfDisallowIncomingPayChan sets the DisallowIncomingPayChan flag.
172172
func (a *AccountRoot) SetLsfDisallowIncomingPayChan() {
173-
a.Flags |= lsfDisallowIncomingPayChan
173+
a.Flags |= LsfDisallowIncomingPayChan
174174
}
175175

176176
// SetLsfDisallowIncomingTrustline sets the DisallowIncomingTrustline flag.
177177
func (a *AccountRoot) SetLsfDisallowIncomingTrustline() {
178-
a.Flags |= lsfDisallowIncomingTrustline
178+
a.Flags |= LsfDisallowIncomingTrustline
179179
}
180180

181181
// SetLsfDisallowXRP sets the DisallowXRP flag.
182182
func (a *AccountRoot) SetLsfDisallowXRP() {
183-
a.Flags |= lsfDisallowXRP
183+
a.Flags |= LsfDisallowXRP
184184
}
185185

186186
// SetLsfGlobalFreeze sets the GlobalFreeze flag.
187187
func (a *AccountRoot) SetLsfGlobalFreeze() {
188-
a.Flags |= lsfGlobalFreeze
188+
a.Flags |= LsfGlobalFreeze
189189
}
190190

191191
// SetLsfNoFreeze sets the NoFreeze flag.
192192
func (a *AccountRoot) SetLsfNoFreeze() {
193-
a.Flags |= lsfNoFreeze
193+
a.Flags |= LsfNoFreeze
194194
}
195195

196196
// SetLsfPasswordSpent sets the PasswordSpent flag.
197197
func (a *AccountRoot) SetLsfPasswordSpent() {
198-
a.Flags |= lsfPasswordSpent
198+
a.Flags |= LsfPasswordSpent
199199
}
200200

201201
// SetLsfRequireAuth sets the RequireAuth flag.
202202
func (a *AccountRoot) SetLsfRequireAuth() {
203-
a.Flags |= lsfRequireAuth
203+
a.Flags |= LsfRequireAuth
204204
}
205205

206206
// SetLsfRequireDestTag sets the RequireDestTag flag.
207207
func (a *AccountRoot) SetLsfRequireDestTag() {
208-
a.Flags |= lsfRequireDestTag
208+
a.Flags |= LsfRequireDestTag
209209
}

0 commit comments

Comments
 (0)