Skip to content

Commit

Permalink
add validation for AuthAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
khancode committed Jul 27, 2023
1 parent 5878433 commit 34b667d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/xrpl/src/models/transactions/AMMBid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface AMMBid extends BaseTransaction {
* @param tx - An AMMBid Transaction.
* @throws When the AMMBid is Malformed.
*/
// eslint-disable-next-line max-lines-per-function -- necessary for validateAMMBid
export function validateAMMBid(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)

Expand Down Expand Up @@ -103,5 +104,48 @@ export function validateAMMBid(tx: Record<string, unknown>): void {
`AMMBid: AuthAccounts length must not be greater than ${MAX_AUTH_ACCOUNTS}`,
)
}
if (
!isAuthAccounts(
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Only used by JS
tx.Account as string,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Only used by JS
tx.AuthAccounts as Array<Record<string, unknown>>,
)
) {
throw new ValidationError(`AMMBid: invalid AuthAccounts`)
}
}
}

function isAuthAccounts(
senderAddress: string,
authAccounts: Array<Record<string, unknown>>,
): boolean {
for (const authAccount of authAccounts) {
if (
authAccount.AuthAccount == null ||
typeof authAccount.AuthAccount !== 'object'
) {
return false
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- used for null check
// @ts-expect-error -- used for null check
if (authAccount.AuthAccount.Account == null) {
return false
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- used for null check
// @ts-expect-error -- used for null check
if (typeof authAccount.AuthAccount.Account !== 'string') {
return false
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- used for null check
// @ts-expect-error -- used for null check
if (authAccount.AuthAccount.Account === senderAddress) {
throw new ValidationError(
`AMMBid: AuthAccounts must not include sender's address`,
)
}
}

return true
}
57 changes: 57 additions & 0 deletions packages/xrpl/test/models/AMMBid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,61 @@ describe('AMMBid', function () {
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})

it(`throws w/ AuthAccounts must be an AuthAccount array`, function () {
bid.AuthAccounts = 1234
const errorMessage = 'AMMBid: AuthAccounts must be an AuthAccount array'
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})

it(`throws w/ invalid AuthAccounts when AuthAccount is null`, function () {
bid.AuthAccounts[0] = {
AuthAccount: null,
}
const errorMessage = 'AMMBid: invalid AuthAccounts'
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})

it(`throws w/ invalid AuthAccounts when AuthAccount is undefined`, function () {
bid.AuthAccounts[0] = {
AuthAccount: undefined,
}
const errorMessage = 'AMMBid: invalid AuthAccounts'
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})

it(`throws w/ invalid AuthAccounts when AuthAccount is not an object`, function () {
bid.AuthAccounts[0] = {
AuthAccount: 1234,
}
const errorMessage = 'AMMBid: invalid AuthAccounts'
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})

it(`throws w/ invalid AuthAccounts when AuthAccount.Account is not a string`, function () {
bid.AuthAccounts[0] = {
AuthAccount: {
Account: 1234,
},
}
const errorMessage = 'AMMBid: invalid AuthAccounts'
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})

it(`throws w/ AuthAccounts must not include sender's address`, function () {
bid.AuthAccounts[0] = {
AuthAccount: {
Account: bid.Account,
},
}
const errorMessage =
"AMMBid: AuthAccounts must not include sender's address"
assert.throws(() => validateAMMBid(bid), ValidationError, errorMessage)
assert.throws(() => validate(bid), ValidationError, errorMessage)
})
})

0 comments on commit 34b667d

Please sign in to comment.