missing fields

This commit is contained in:
Denis Angell
2023-05-17 21:32:28 +00:00
parent a97c7bb21b
commit 4fbd5383c4
7 changed files with 85 additions and 19 deletions

View File

@@ -97,7 +97,7 @@ export interface Hook {
* The object that describes the hook in Hooks. * The object that describes the hook in Hooks.
*/ */
Hook: { Hook: {
HookHash: string HookHash?: string
/** /**
* The code that is executed when the hook is triggered. * The code that is executed when the hook is triggered.
*/ */

View File

@@ -4,7 +4,13 @@
import { TRANSACTION_TYPES } from '@transia/ripple-binary-codec' import { TRANSACTION_TYPES } from '@transia/ripple-binary-codec'
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Amount, IssuedCurrencyAmount, Memo, Signer } from '../common' import {
Amount,
HookParameter,
IssuedCurrencyAmount,
Memo,
Signer,
} from '../common'
import { onlyHasFields } from '../utils' import { onlyHasFields } from '../utils'
const MEMO_SIZE = 3 const MEMO_SIZE = 3
@@ -163,6 +169,10 @@ export interface BaseTransaction {
* The network id of the transaction. * The network id of the transaction.
*/ */
NetworkID?: number NetworkID?: number
/**
* The hook parameters of the transaction.
*/
HookParameters?: HookParameter[]
} }
/** /**

View File

@@ -43,7 +43,7 @@ export {
export { PaymentChannelCreate } from './paymentChannelCreate' export { PaymentChannelCreate } from './paymentChannelCreate'
export { PaymentChannelFund } from './paymentChannelFund' export { PaymentChannelFund } from './paymentChannelFund'
export { SetRegularKey } from './setRegularKey' export { SetRegularKey } from './setRegularKey'
export { SetHook } from './setHook' export { SetHookFlagsInterface, SetHookFlags, SetHook } from './setHook'
export { SignerListSet } from './signerListSet' export { SignerListSet } from './signerListSet'
export { TicketCreate } from './ticketCreate' export { TicketCreate } from './ticketCreate'
export { TrustSetFlagsInterface, TrustSetFlags, TrustSet } from './trustSet' export { TrustSetFlagsInterface, TrustSetFlags, TrustSet } from './trustSet'

View File

@@ -1,7 +1,30 @@
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Hook } from '../common' import { Hook } from '../common'
import { BaseTransaction, validateBaseTransaction } from './common' import { BaseTransaction, GlobalFlags, validateBaseTransaction } from './common'
/**
* Enum representing values for Set Hook Transaction Flags.
*
* @category Transaction Flags
*/
export enum SetHookFlags {
/**
*/
hsfOverride = 0x00000001,
/**
*/
hsfNSDelete = 0x00000010,
/**
*/
hsfCollect = 0x00000100,
}
export interface SetHookFlagsInterface extends GlobalFlags {
hsfOverride?: boolean
hsfNSDelete?: boolean
hsfCollect?: boolean
}
/** /**
* *
@@ -14,6 +37,8 @@ export interface SetHook extends BaseTransaction {
* *
*/ */
Hooks: Hook[] Hooks: Hook[]
Flags?: number | SetHookFlagsInterface
} }
const MAX_HOOKS = 4 const MAX_HOOKS = 4

View File

@@ -13,6 +13,7 @@ import { DepositPreauth, validateDepositPreauth } from './depositPreauth'
import { EscrowCancel, validateEscrowCancel } from './escrowCancel' import { EscrowCancel, validateEscrowCancel } from './escrowCancel'
import { EscrowCreate, validateEscrowCreate } from './escrowCreate' import { EscrowCreate, validateEscrowCreate } from './escrowCreate'
import { EscrowFinish, validateEscrowFinish } from './escrowFinish' import { EscrowFinish, validateEscrowFinish } from './escrowFinish'
import { Invoke, validateInvoke } from './invoke'
import { TransactionMetadata } from './metadata' import { TransactionMetadata } from './metadata'
import { import {
NFTokenAcceptOffer, NFTokenAcceptOffer,
@@ -62,6 +63,7 @@ export type Transaction =
| EscrowCancel | EscrowCancel
| EscrowCreate | EscrowCreate
| EscrowFinish | EscrowFinish
| Invoke
| NFTokenAcceptOffer | NFTokenAcceptOffer
| NFTokenBurn | NFTokenBurn
| NFTokenCancelOffer | NFTokenCancelOffer
@@ -142,6 +144,10 @@ export function validate(transaction: Record<string, unknown>): void {
validateEscrowFinish(tx) validateEscrowFinish(tx)
break break
case 'Invoke':
validateInvoke(tx)
break
case 'NFTokenAcceptOffer': case 'NFTokenAcceptOffer':
validateNFTokenAcceptOffer(tx) validateNFTokenAcceptOffer(tx)
break break

View File

@@ -20,6 +20,7 @@ import {
PaymentChannelClaimFlagsInterface, PaymentChannelClaimFlagsInterface,
PaymentChannelClaimFlags, PaymentChannelClaimFlags,
} from '../transactions/paymentChannelClaim' } from '../transactions/paymentChannelClaim'
import { SetHookFlagsInterface, SetHookFlags } from '../transactions/setHook'
import type { Transaction } from '../transactions/transaction' import type { Transaction } from '../transactions/transaction'
import { TrustSetFlagsInterface, TrustSetFlags } from '../transactions/trustSet' import { TrustSetFlagsInterface, TrustSetFlags } from '../transactions/trustSet'
@@ -75,6 +76,15 @@ export function setTransactionFlagsToNumber(tx: Transaction): void {
case 'TrustSet': case 'TrustSet':
tx.Flags = convertTrustSetFlagsToNumber(tx.Flags) tx.Flags = convertTrustSetFlagsToNumber(tx.Flags)
return return
case 'SetHook':
tx.Flags = convertSetHookFlagsToNumber(tx.Flags)
tx.Hooks.forEach((h) => {
h.Hook.Flags = convertSetHookFlagsToNumber(
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- idk
h.Hook.Flags as SetHookFlagsInterface,
)
})
return
default: default:
tx.Flags = 0 tx.Flags = 0
} }
@@ -108,6 +118,10 @@ function convertTrustSetFlagsToNumber(flags: TrustSetFlagsInterface): number {
return reduceFlags(flags, TrustSetFlags) return reduceFlags(flags, TrustSetFlags)
} }
function convertSetHookFlagsToNumber(flags: SetHookFlagsInterface): number {
return reduceFlags(flags, SetHookFlags)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- added ValidationError check for flagEnum // eslint-disable-next-line @typescript-eslint/no-explicit-any -- added ValidationError check for flagEnum
function reduceFlags(flags: GlobalFlags, flagEnum: any): number { function reduceFlags(flags: GlobalFlags, flagEnum: any): number {
return Object.keys(flags).reduce((resultFlags, flag) => { return Object.keys(flags).reduce((resultFlags, flag) => {

View File

@@ -4,9 +4,11 @@
* This module contains the transaction types and the function to calculate the hook on * This module contains the transaction types and the function to calculate the hook on
*/ */
// eslint-disable-next-line @typescript-eslint/no-require-imports -- Required import {
TRANSACTION_TYPES,
TRANSACTION_TYPE_MAP,
} from '@transia/ripple-binary-codec'
import createHash = require('create-hash') import createHash = require('create-hash')
import { TRANSACTION_TYPES, TRANSACTION_TYPE_MAP } from '@transia/ripple-binary-codec'
import { XrplError } from '../errors' import { XrplError } from '../errors'
import { HookParameter } from '../models/common' import { HookParameter } from '../models/common'
@@ -42,7 +44,7 @@ export function calculateHookOn(arr: Array<keyof TTS>): string {
`invalid transaction type '${String(nth)}' in HookOn array`, `invalid transaction type '${String(nth)}' in HookOn array`,
) )
} }
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Required
const tts: Record<string, number> = TRANSACTION_TYPE_MAP const tts: Record<string, number> = TRANSACTION_TYPE_MAP
let value = BigInt(hash) let value = BigInt(hash)
// eslint-disable-next-line no-bitwise -- Required // eslint-disable-next-line no-bitwise -- Required
@@ -84,6 +86,14 @@ export async function hexNamespace(namespace: string): Promise<string> {
return (await sha256(namespace)).toUpperCase() return (await sha256(namespace)).toUpperCase()
} }
function isHex(value: string): boolean {
return /^[0-9A-F]+$/iu.test(value)
}
function hexValue(value: string): string {
return Buffer.from(value, 'utf8').toString('hex').toUpperCase()
}
/** /**
* Calculate the hex of the hook parameters * Calculate the hex of the hook parameters
* *
@@ -93,20 +103,21 @@ export async function hexNamespace(namespace: string): Promise<string> {
export function hexHookParameters(data: HookParameter[]): HookParameter[] { export function hexHookParameters(data: HookParameter[]): HookParameter[] {
const hookParameters: HookParameter[] = [] const hookParameters: HookParameter[] = []
for (const parameter of data) { for (const parameter of data) {
let hookPName = parameter.HookParameter.HookParameterName
let hookPValue = parameter.HookParameter.HookParameterValue
if (!isHex(hookPName)) {
hookPName = hexValue(hookPName)
}
if (!isHex(hookPValue)) {
hookPValue = hexValue(hookPValue)
}
hookParameters.push({ hookParameters.push({
HookParameter: { HookParameter: {
HookParameterName: Buffer.from( HookParameterName: hookPName,
parameter.HookParameter.HookParameterName, HookParameterValue: hookPValue,
'utf8',
)
.toString('hex')
.toUpperCase(),
HookParameterValue: Buffer.from(
parameter.HookParameter.HookParameterValue,
'utf8',
)
.toString('hex')
.toUpperCase(),
}, },
}) })
} }