Skip to content

Commit

Permalink
refactor authcred check to work
Browse files Browse the repository at this point in the history
  • Loading branch information
achowdhry-ripple committed Dec 19, 2024
1 parent f30f9f5 commit 250a67c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions packages/xrpl/src/models/transactions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const ISSUE_SIZE = 2
const ISSUED_CURRENCY_SIZE = 3
const XCHAIN_BRIDGE_SIZE = 4
const MPTOKEN_SIZE = 2
const AUTHORIZE_CREDENTIAL_SIZE = 1

function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object'
Expand Down Expand Up @@ -128,6 +129,22 @@ export function isIssuedCurrency(
)
}

/**
* Verify the form and type of an AuthorizeCredential at runtime
*
* @param input - The input to check the form and type of
* @returns Whether the AuthorizeCredential is properly formed
*/
function isAuthorizeCredential(input: unknown): input is AuthorizeCredential {
return (
isRecord(input) &&
isRecord(input.Credential) &&
Object.keys(input).length === AUTHORIZE_CREDENTIAL_SIZE &&
typeof input.Credential.CredentialType === 'string' &&
typeof input.Credential.Issuer === 'string'
)
}

/**
* Verify the form and type of an MPT at runtime.
*
Expand Down Expand Up @@ -441,7 +458,7 @@ export function validateCredentialType(tx: Record<string, unknown>): void {
* @param isStringID Toggle for if array contains IDs instead of AuthorizeCredential objects
* @throws Validation Error if the formatting is incorrect
*/
// eslint-disable-next-line max-lines-per-function -- dumb
// eslint-disable-next-line max-lines-per-function -- separating logic further will add unnecessary complexity
export function validateCredentialsList(
credentials: unknown,
transactionType: string,
Expand Down Expand Up @@ -484,15 +501,6 @@ export function validateCredentialsList(
}
}

function isAuthorizeCredential(
value: AuthorizeCredential,
): value is AuthorizeCredential {
if (value.Credential.CredentialType && value.Credential.Issuer) {
return true
}
return false
}

function containsDuplicates(objectList: object[]): boolean {
const objSet = new Set(objectList.map((obj) => JSON.stringify(obj)))
if (objSet.size !== objectList.length) {
Expand Down

0 comments on commit 250a67c

Please sign in to comment.