update hook

update hook

add test and validation

add estimate fee func

fix estimated fee error

add hook utils
This commit is contained in:
Denis Angell
2023-02-23 03:55:30 -05:00
parent 6d6b5a5863
commit 3a2a0918f8
11 changed files with 395 additions and 23 deletions

View File

@@ -17,6 +17,7 @@ export interface SetHook extends BaseTransaction {
}
const MAX_HOOKS = 4
const HEX_REGEX = /^[0-9A-Fa-f]{64}$/u
/**
* Verify the form and type of an SetHook at runtime.
@@ -27,21 +28,29 @@ const MAX_HOOKS = 4
export function validateSetHook(tx: Record<string, unknown>): void {
validateBaseTransaction(tx)
if (tx.Hooks === undefined) {
throw new ValidationError('SetHook: missing field Hooks')
}
if (!Array.isArray(tx.Hooks)) {
throw new ValidationError('SetHook: invalid Hooks')
}
if (tx.Hooks.length === 0) {
throw new ValidationError('SetHook: need at least 1 hook in Hooks')
}
if (tx.Hooks.length > MAX_HOOKS) {
throw new ValidationError(
`SetHook: maximum of ${MAX_HOOKS} hooks allowed in Hooks`,
)
}
for (const hook of tx.Hooks) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Should be a Hook
const hookObject = hook as Hook
const { HookOn, HookNamespace } = hookObject.Hook
if (HookOn !== undefined && !HEX_REGEX.test(HookOn)) {
throw new ValidationError(
`SetHook: HookOn in Hook must be a 256-bit (32-byte) hexadecimal value`,
)
}
if (HookNamespace !== undefined && !HEX_REGEX.test(HookNamespace)) {
throw new ValidationError(
`SetHook: HookNamespace in Hook must be a 256-bit (32-byte) hexadecimal value`,
)
}
}
}