feat: Add parseAccountRootFlags (#1699)

* Add parseAccountRootFlags and interface

* Add tests for parseAccountRootFlags
This commit is contained in:
Jackson Mills
2021-10-07 16:23:42 -07:00
committed by GitHub
parent 0674d21134
commit 7e70142044
3 changed files with 84 additions and 1 deletions

View File

@@ -19,6 +19,18 @@ export default interface AccountRoot extends BaseLedgerEntry {
TransferRate?: number
}
export interface AccountRootFlagsInterface {
lsfPasswordSpent?: boolean
lsfRequireDestTag?: boolean
lsfRequireAuth?: boolean
lsfDisallowXRP?: boolean
lsfDisableMaster?: boolean
lsfNoFreeze?: boolean
lsfGlobalFreeze?: boolean
lsfDefaultRipple?: boolean
lsfDepositAuth?: boolean
}
export enum AccountRootFlags {
lsfPasswordSpent = 0x00010000,
lsfRequireDestTag = 0x00020000,

View File

@@ -2,6 +2,10 @@
/* eslint-disable no-bitwise -- flags require bitwise operations */
import { ValidationError } from '../../errors'
import {
AccountRootFlagsInterface,
AccountRootFlags,
} from '../ledger/accountRoot'
import {
AccountSetFlagsInterface,
AccountSetTfFlags,
@@ -19,6 +23,29 @@ import {
import type { Transaction } from '../transactions/transaction'
import { TrustSetFlagsInterface, TrustSetFlags } from '../transactions/trustSet'
import { isFlagEnabled } from '.'
/**
* Convert an AccountRoot Flags number into an interface for easy interpretation.
*
* @param flags - A number which is the bitwise and of all enabled AccountRootFlagsInterface.
* @returns An interface with all flags as booleans.
*/
// eslint-disable-next-line import/no-unused-modules -- Used by end user
export function parseAccountRootFlags(
flags: number,
): AccountRootFlagsInterface {
const flagsInterface: AccountRootFlagsInterface = {}
Object.keys(AccountRootFlags).forEach((flag) => {
if (isFlagEnabled(flags, AccountRootFlags[flag])) {
flagsInterface[flag] = true
}
})
return flagsInterface
}
/**
* Sets a transaction's flags to its numeric representation.
*

View File

@@ -12,8 +12,11 @@ import {
TrustSet,
TrustSetFlags,
} from 'xrpl-local'
import { AccountRootFlags } from 'xrpl-local/models/ledger'
import { isFlagEnabled } from 'xrpl-local/models/utils'
import setTransactionFlagsToNumber from 'xrpl-local/models/utils/flags'
import setTransactionFlagsToNumber, {
parseAccountRootFlags,
} from 'xrpl-local/models/utils/flags'
/**
* Utils Testing.
@@ -146,5 +149,46 @@ describe('Models Utils', function () {
setTransactionFlagsToNumber(tx)
assert.strictEqual(tx.Flags, 0)
})
it('parseAccountRootFlags all enabled', function () {
const accountRootFlags =
AccountRootFlags.lsfDefaultRipple |
AccountRootFlags.lsfDepositAuth |
AccountRootFlags.lsfDisableMaster |
AccountRootFlags.lsfDisallowXRP |
AccountRootFlags.lsfGlobalFreeze |
AccountRootFlags.lsfNoFreeze |
AccountRootFlags.lsfPasswordSpent |
AccountRootFlags.lsfRequireAuth |
AccountRootFlags.lsfRequireDestTag
const parsed = parseAccountRootFlags(accountRootFlags)
assert.isTrue(
parsed.lsfDefaultRipple &&
parsed.lsfDepositAuth &&
parsed.lsfDisableMaster &&
parsed.lsfDisallowXRP &&
parsed.lsfGlobalFreeze &&
parsed.lsfNoFreeze &&
parsed.lsfPasswordSpent &&
parsed.lsfRequireAuth &&
parsed.lsfRequireDestTag,
)
})
it('parseAccountFlags all false', function () {
const parsed = parseAccountRootFlags(0)
assert.isUndefined(parsed.lsfDefaultRipple)
assert.isUndefined(parsed.lsfDepositAuth)
assert.isUndefined(parsed.lsfDisableMaster)
assert.isUndefined(parsed.lsfDisallowXRP)
assert.isUndefined(parsed.lsfGlobalFreeze)
assert.isUndefined(parsed.lsfNoFreeze)
assert.isUndefined(parsed.lsfPasswordSpent)
assert.isUndefined(parsed.lsfRequireAuth)
assert.isUndefined(parsed.lsfRequireDestTag)
})
})
})