add validate methods to other xchain txs

This commit is contained in:
Mayukha Vadari
2023-02-15 12:14:59 -05:00
parent dd841c9441
commit d29bdbb5de
9 changed files with 421 additions and 34 deletions

118
create_validate.py Normal file
View File

@@ -0,0 +1,118 @@
"""
Helper script to write `validate` methods for transactions.
"""
import sys
NORMAL_TYPES = ["number", "string"]
NUMBERS = ["0", "1"]
def main():
model_name = sys.argv[1]
filename = f"./packages/xrpl/src/models/transactions/{model_name}.ts"
model, tx_name = get_model(filename)
return process_model(model, tx_name)
# Extract just the model from the file
def get_model(filename):
model = ""
started = False
ended = False
with open(filename) as f:
for line in f:
if ended:
continue
if not started and not line.startswith("export"):
continue
if not started and "Flags" in line:
continue
if not started:
started = True
model += line
if line == '}\n':
ended = True
lines = model.split("\n")
name_line = lines[0].split(" ")
tx_name = name_line[2]
return model, tx_name
# Process the model and build the `validate` method
def get_if_line_param_part(param: str, param_type: str):
if param_type in NORMAL_TYPES:
return f"typeof tx.{param} !== \"{param_type}\""
elif param_type in NUMBERS:
return f"tx.{param} !== {param_type}"
else:
return f"!is{param_type}(tx.{param})"
def process_model(model, tx_name):
output = ""
for line in model.split("\n"):
if line == "":
continue
if line.startswith("export"):
continue
if line == "}":
continue
line = line.strip()
if line.startswith("TransactionType"):
continue
if line.startswith("Flags"):
continue
split = line.split(" ")
param = split[0].strip("?:")
param_types = split[1:]
optional = split[0].endswith("?:")
if optional:
if_line = f" if(tx.{param} !== undefined && "
else:
output += f" if (tx.{param} == null) {{\n"
output += f" throw new ValidationError('{tx_name}: missing field {param}')\n"
output += " }\n\n"
if_line = " if("
if len(param_types) == 1:
param_type = param_types[0]
if_line += get_if_line_param_part(param, param_type)
else:
i = 0
if_outputs = []
while i < len(param_types):
param_type = param_types[i]
if_outputs.append(get_if_line_param_part(param, param_type))
i += 2
if_line += "(" + " && ".join(if_outputs) + ")"
if_line += ") {\n"
output += if_line
output += f" throw new ValidationError('{tx_name}: invalid field {param}')\n"
output += " }\n\n"
output = output[:-1]
output += "}\n"
output = f"""/**
* Verify the form and type of a {tx_name} at runtime.
*
* @param tx - A {tx_name} Transaction.
* @throws When the {tx_name} is malformed.
*/
export function validate{tx_name}(tx: Record<string, unknown>): void {{
validateBaseTransaction(tx)
""" + output
return output
if __name__ == "__main__":
print(main())

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -25,6 +30,7 @@ export interface XChainAccountCreateCommit extends BaseTransaction {
* @param tx - A XChainAccountCreateCommit Transaction.
* @throws When the XChainAccountCreateCommit is malformed.
*/
// eslint-disable-next-line max-lines-per-function -- okay for this function, there's a lot of things to check
export function validateXChainAccountCreateCommit(
tx: Record<string, unknown>,
): void {
@@ -36,19 +42,44 @@ export function validateXChainAccountCreateCommit(
)
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError(
'XChainAccountCreateCommit: invalid field XChainBridge',
)
}
if (tx.SignatureReward == null) {
throw new ValidationError(
'XChainAccountCreateCommit: missing field SignatureReward',
)
}
if (
typeof tx.SignatureReward !== 'number' &&
typeof tx.SignatureReward !== 'string'
) {
throw new ValidationError(
'XChainAccountCreateCommit: invalid field SignatureReward',
)
}
if (tx.Destination == null) {
throw new ValidationError(
'XChainAccountCreateCommit: missing field Destination',
)
}
if (typeof tx.Destination !== 'string') {
throw new ValidationError(
'XChainAccountCreateCommit: invalid field Destination',
)
}
if (tx.Amount == null) {
throw new ValidationError('XChainAccountCreateCommit: missing field Amount')
}
if (!isAmount(tx.Amount)) {
throw new ValidationError('XChainAccountCreateCommit: invalid field Amount')
}
}

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -39,7 +44,7 @@ export interface XChainAddAccountCreateAttestation extends BaseTransaction {
* @param tx - A XChainAddAccountCreateAttestation Transaction.
* @throws When the XChainAddAccountCreateAttestation is malformed.
*/
// eslint-disable-next-line max-lines-per-function, complexity -- needed
// eslint-disable-next-line max-lines-per-function, max-statements, complexity -- okay for this function, lots of things to check
export function validateXChainAddAccountCreateAttestation(
tx: Record<string, unknown>,
): void {
@@ -47,61 +52,133 @@ export function validateXChainAddAccountCreateAttestation(
if (tx.Amount == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.Amount',
'XChainAddAccountCreateAttestation: missing field Amount',
)
}
if (!isAmount(tx.Amount)) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field Amount',
)
}
if (tx.AttestationRewardAccount == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.AttestationRewardAccount',
'XChainAddAccountCreateAttestation: missing field AttestationRewardAccount',
)
}
if (typeof tx.AttestationRewardAccount !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field AttestationRewardAccount',
)
}
if (tx.AttestationSignerAccount == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field AttestationSignerAccount',
)
}
if (typeof tx.AttestationSignerAccount !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field AttestationSignerAccount',
)
}
if (tx.Destination == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.Destination',
'XChainAddAccountCreateAttestation: missing field Destination',
)
}
if (typeof tx.Destination !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field Destination',
)
}
if (tx.OtherChainSource == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.OtherChainSource',
'XChainAddAccountCreateAttestation: missing field OtherChainSource',
)
}
if (typeof tx.OtherChainSource !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field OtherChainSource',
)
}
if (tx.PublicKey == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.PublicKey',
'XChainAddAccountCreateAttestation: missing field PublicKey',
)
}
if (typeof tx.PublicKey !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field PublicKey',
)
}
if (tx.Signature == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.Signature',
'XChainAddAccountCreateAttestation: missing field Signature',
)
}
if (typeof tx.Signature !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field Signature',
)
}
if (tx.SignatureReward == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.SignatureReward',
'XChainAddAccountCreateAttestation: missing field SignatureReward',
)
}
if (!isAmount(tx.SignatureReward)) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field SignatureReward',
)
}
if (tx.WasLockingChainSend == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.WasLockingChainSend',
'XChainAddAccountCreateAttestation: missing field WasLockingChainSend',
)
}
if (tx.WasLockingChainSend !== 0 && tx.WasLockingChainSend !== 1) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field WasLockingChainSend',
)
}
if (tx.XChainAccountCreateCount == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.XChainAccountCreateCount',
'XChainAddAccountCreateAttestation: missing field XChainAccountCreateCount',
)
}
if (typeof tx.XChainAccountCreateCount !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field XChainAccountCreateCount',
)
}
if (tx.XChainBridge == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.XChainBridge',
'XChainAddAccountCreateAttestation: missing field XChainBridge',
)
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field XChainBridge',
)
}
}

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -37,69 +42,119 @@ export interface XChainAddClaimAttestation extends BaseTransaction {
* @param tx - A XChainAddClaimAttestation Transaction.
* @throws When the XChainAddClaimAttestation is malformed.
*/
// eslint-disable-next-line max-lines-per-function, complexity -- needed
// eslint-disable-next-line max-lines-per-function, max-statements, complexity -- okay for this function, lots of things to check
export function validateXChainAddClaimAttestation(
tx: Record<string, unknown>,
): void {
validateBaseTransaction(tx)
if (tx.Amount == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.Amount',
)
throw new ValidationError('XChainAddClaimAttestation: missing field Amount')
}
if (!isAmount(tx.Amount)) {
throw new ValidationError('XChainAddClaimAttestation: invalid field Amount')
}
if (tx.AttestationRewardAccount == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.AttestationRewardAccount',
'XChainAddClaimAttestation: missing field AttestationRewardAccount',
)
}
if (tx.Destination != null && typeof tx.Destination !== 'string') {
if (typeof tx.AttestationRewardAccount !== 'string') {
throw new ValidationError(
'XChainAddAccountCreateAttestation: invalid field tx.Destination',
'XChainAddClaimAttestation: invalid field AttestationRewardAccount',
)
}
if (tx.AttestationSignerAccount == null) {
throw new ValidationError(
'XChainAddClaimAttestation: missing field AttestationSignerAccount',
)
}
if (typeof tx.AttestationSignerAccount !== 'string') {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field AttestationSignerAccount',
)
}
if (tx.Destination !== undefined && typeof tx.Destination !== 'string') {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field Destination',
)
}
if (tx.OtherChainSource == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.OtherChainSource',
'XChainAddClaimAttestation: missing field OtherChainSource',
)
}
if (typeof tx.OtherChainSource !== 'string') {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field OtherChainSource',
)
}
if (tx.PublicKey == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.PublicKey',
'XChainAddClaimAttestation: missing field PublicKey',
)
}
if (typeof tx.PublicKey !== 'string') {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field PublicKey',
)
}
if (tx.Signature == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.Signature',
'XChainAddClaimAttestation: missing field Signature',
)
}
if (typeof tx.Signature !== 'string') {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field Signature',
)
}
if (tx.WasLockingChainSend == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.WasLockingChainSend',
'XChainAddClaimAttestation: missing field WasLockingChainSend',
)
}
if (tx.XChainAccountCreateCount == null) {
if (tx.WasLockingChainSend !== 0 && tx.WasLockingChainSend !== 1) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.XChainAccountCreateCount',
'XChainAddClaimAttestation: invalid field WasLockingChainSend',
)
}
if (tx.XChainBridge == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.XChainBridge',
'XChainAddClaimAttestation: missing field XChainBridge',
)
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field XChainBridge',
)
}
if (tx.XChainClaimID == null) {
throw new ValidationError(
'XChainAddAccountCreateAttestation: missing field tx.XChainClaimID',
'XChainAddClaimAttestation: missing field XChainClaimID',
)
}
if (typeof tx.XChainClaimID !== 'string') {
throw new ValidationError(
'XChainAddClaimAttestation: invalid field XChainClaimID',
)
}
}

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -27,6 +32,7 @@ export interface XChainClaim extends BaseTransaction {
* @param tx - A XChainClaim Transaction.
* @throws When the XChainClaim is malformed.
*/
// eslint-disable-next-line complexity -- okay for this function, lots of things to check
export function validateXChainClaim(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)
@@ -34,15 +40,41 @@ export function validateXChainClaim(tx: Record<string, unknown>): void {
throw new ValidationError('XChainClaim: missing field XChainBridge')
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError('XChainClaim: invalid field XChainBridge')
}
if (tx.XChainClaimID == null) {
throw new ValidationError('XChainClaim: missing field XChainClaimID')
}
if (
typeof tx.XChainClaimID !== 'number' &&
typeof tx.XChainClaimID !== 'string'
) {
throw new ValidationError('XChainClaim: invalid field XChainClaimID')
}
if (tx.Destination == null) {
throw new ValidationError('XChainClaim: missing field Destination')
}
if (typeof tx.Destination !== 'string') {
throw new ValidationError('XChainClaim: invalid field Destination')
}
if (
tx.DestinationTag !== undefined &&
typeof tx.DestinationTag !== 'number'
) {
throw new ValidationError('XChainClaim: invalid field DestinationTag')
}
if (tx.Amount == null) {
throw new ValidationError('XChainClaim: missing field Amount')
}
if (!isAmount(tx.Amount)) {
throw new ValidationError('XChainClaim: invalid field Amount')
}
}

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -32,11 +37,35 @@ export function validateXChainCommit(tx: Record<string, unknown>): void {
throw new ValidationError('XChainCommit: missing field XChainBridge')
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError('XChainCommit: invalid field XChainBridge')
}
if (tx.XChainClaimID == null) {
throw new ValidationError('XChainCommit: missing field XChainClaimID')
}
if (
typeof tx.XChainClaimID !== 'number' &&
typeof tx.XChainClaimID !== 'string'
) {
throw new ValidationError('XChainCommit: invalid field XChainClaimID')
}
if (
tx.OtherChainDestination !== undefined &&
typeof tx.OtherChainDestination !== 'string'
) {
throw new ValidationError(
'XChainCommit: invalid field OtherChainDestination',
)
}
if (tx.Amount == null) {
throw new ValidationError('XChainCommit: missing field Amount')
}
if (!isAmount(tx.Amount)) {
throw new ValidationError('XChainCommit: invalid field Amount')
}
}

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -30,9 +35,28 @@ export function validateXChainCreateBridge(tx: Record<string, unknown>): void {
throw new ValidationError('XChainCreateBridge: missing field XChainBridge')
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError('XChainCreateBridge: invalid field XChainBridge')
}
if (tx.SignatureReward == null) {
throw new ValidationError(
'XChainCreateBridge: missing field SignatureReward',
)
}
if (!isAmount(tx.SignatureReward)) {
throw new ValidationError(
'XChainCreateBridge: invalid field SignatureReward',
)
}
if (
tx.MinAccountCreateAmount !== undefined &&
!isAmount(tx.MinAccountCreateAmount)
) {
throw new ValidationError(
'XChainCreateBridge: invalid field MinAccountCreateAmount',
)
}
}

View File

@@ -1,7 +1,12 @@
import { ValidationError } from '../../errors'
import { Amount, XChainBridge } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common'
import {
BaseTransaction,
isAmount,
isXChainBridge,
validateBaseTransaction,
} from './common'
/**
*
@@ -30,15 +35,31 @@ export function validateXChainCreateClaimID(tx: Record<string, unknown>): void {
throw new ValidationError('XChainCreateClaimID: missing field XChainBridge')
}
if (!isXChainBridge(tx.XChainBridge)) {
throw new ValidationError('XChainCreateClaimID: invalid field XChainBridge')
}
if (tx.SignatureReward == null) {
throw new ValidationError(
'XChainCreateClaimID: missing field SignatureReward',
)
}
if (!isAmount(tx.SignatureReward)) {
throw new ValidationError(
'XChainCreateClaimID: invalid field SignatureReward',
)
}
if (tx.OtherChainSource == null) {
throw new ValidationError(
'XChainCreateClaimID: missing field OtherChainSource',
)
}
if (typeof tx.OtherChainSource !== 'string') {
throw new ValidationError(
'XChainCreateClaimID: invalid field OtherChainSource',
)
}
}

View File

@@ -104,7 +104,7 @@ export function isXChainBridge(input: unknown): input is XChainBridge {
typeof input.LockingChainDoor === 'string' &&
typeof input.LockingChainIssue === 'string' &&
typeof input.IssuingChainDoor === 'string' &&
typeof input.IsusingChainIssue === 'string'
typeof input.IssuingChainIssue === 'string'
)
}