mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-04 21:15:47 +00:00
update validateXChainModifyBridge
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import { ValidationError } from '../../errors'
|
||||
import { Amount, XChainBridge } from '../common'
|
||||
|
||||
import { BaseTransaction, GlobalFlags, validateBaseTransaction } from './common'
|
||||
import {
|
||||
BaseTransaction,
|
||||
GlobalFlags,
|
||||
isAmount,
|
||||
isXChainBridge,
|
||||
validateBaseTransaction,
|
||||
} from './common'
|
||||
|
||||
export enum XChainModifyBridgeFlags {
|
||||
tfClearAccountCreateAmount = 0x00010000,
|
||||
@@ -39,4 +45,23 @@ export function validateXChainModifyBridge(tx: Record<string, unknown>): void {
|
||||
if (tx.XChainBridge == null) {
|
||||
throw new ValidationError('XChainModifyBridge: missing field XChainBridge')
|
||||
}
|
||||
|
||||
if (!isXChainBridge(tx.XChainBridge)) {
|
||||
throw new ValidationError('XChainModifyBridge: invalid field XChainBridge')
|
||||
}
|
||||
|
||||
if (tx.SignatureReward !== undefined && !isAmount(tx.SignatureReward)) {
|
||||
throw new ValidationError(
|
||||
'XChainModifyBridge: invalid field SignatureReward',
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
tx.MinAccountCreateAmount !== undefined &&
|
||||
!isAmount(tx.MinAccountCreateAmount)
|
||||
) {
|
||||
throw new ValidationError(
|
||||
'XChainModifyBridge: invalid field MinAccountCreateAmount',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,13 @@
|
||||
import { TRANSACTION_TYPES } from 'ripple-binary-codec'
|
||||
|
||||
import { ValidationError } from '../../errors'
|
||||
import { Amount, IssuedCurrencyAmount, Memo, Signer } from '../common'
|
||||
import {
|
||||
Amount,
|
||||
IssuedCurrencyAmount,
|
||||
Memo,
|
||||
Signer,
|
||||
XChainBridge,
|
||||
} from '../common'
|
||||
import { onlyHasFields } from '../utils'
|
||||
|
||||
const MEMO_SIZE = 3
|
||||
@@ -51,6 +57,7 @@ function isSigner(obj: unknown): boolean {
|
||||
}
|
||||
|
||||
const ISSUED_CURRENCY_SIZE = 3
|
||||
const XCHAIN_BRIDGE_SIZE = 4
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object'
|
||||
@@ -60,7 +67,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
* Verify the form and type of an IssuedCurrencyAmount at runtime.
|
||||
*
|
||||
* @param input - The input to check the form and type of.
|
||||
* @returns Whether the IssuedCurrencyAmount is malformed.
|
||||
* @returns Whether the IssuedCurrencyAmount is properly formed.
|
||||
*/
|
||||
export function isIssuedCurrency(
|
||||
input: unknown,
|
||||
@@ -78,12 +85,29 @@ export function isIssuedCurrency(
|
||||
* Verify the form and type of an Amount at runtime.
|
||||
*
|
||||
* @param amount - The object to check the form and type of.
|
||||
* @returns Whether the Amount is malformed.
|
||||
* @returns Whether the Amount is properly formed.
|
||||
*/
|
||||
export function isAmount(amount: unknown): amount is Amount {
|
||||
return typeof amount === 'string' || isIssuedCurrency(amount)
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the form and type of an XChainBridge at runtime.
|
||||
*
|
||||
* @param input - The input to check the form and type of.
|
||||
* @returns Whether the XChainBridge is properly formed.
|
||||
*/
|
||||
export function isXChainBridge(input: unknown): input is XChainBridge {
|
||||
return (
|
||||
isRecord(input) &&
|
||||
Object.keys(input).length === XCHAIN_BRIDGE_SIZE &&
|
||||
typeof input.LockingChainDoor === 'string' &&
|
||||
typeof input.LockingChainIssue === 'string' &&
|
||||
typeof input.IssuingChainDoor === 'string' &&
|
||||
typeof input.IsusingChainIssue === 'string'
|
||||
)
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- no global flags right now, so this is fine
|
||||
export interface GlobalFlags {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user