Fix length validation error on HookName deletion (#71)

This commit is contained in:
tequ
2026-07-03 14:50:05 +09:00
committed by GitHub
parent 94751fc4bc
commit 919315ef69
4 changed files with 66 additions and 2 deletions

View File

@@ -2,6 +2,9 @@
## Unreleased Changes
### Fixed
* Fix HookName validation on HookName deletion
## 4.1.0 (2026-06-23)
### Added

View File

@@ -79,7 +79,11 @@ export function validateSetHook(tx: Record<string, unknown>): void {
`SetHook: HookNamespace in Hook must be a 256-bit (32-byte) hexadecimal value`,
)
}
if (HookName !== undefined && !HOOKNAME_REGEX.test(HookName)) {
if (
HookName !== undefined &&
HookName.length !== 0 &&
!HOOKNAME_REGEX.test(HookName)
) {
throw new ValidationError(
`SetHook: HookName in Hook must be a hex string of 8-32 hex characters`,
)

View File

@@ -135,4 +135,49 @@ describe('SetHook', function () {
},
TIMEOUT,
)
it('hook name update', async () => {
let setHookTx: SetHook = {
TransactionType: 'SetHook',
Account: wallet.classicAddress,
Hooks: [
{
Hook: {
CreateCode: acceptHook,
HookApiVersion: 0,
HookName: '484F4F4B',
HookOn: '00'.repeat(32),
HookNamespace: '00'.repeat(32),
},
},
],
}
await testTransaction(testContext.client, setHookTx, wallet)
{
const ledgerEntryResponse = await testContext.client.request({
command: 'ledger_entry',
hook: { account: wallet.classicAddress },
})
const node = ledgerEntryResponse.result.node as Hook
const hook = node.Hooks[0].Hook
expect(hook.HookName).toEqual('484F4F4B')
}
// delete HookName
setHookTx = {
TransactionType: 'SetHook',
Account: wallet.classicAddress,
Hooks: [{ Hook: { HookName: '' } }],
}
await testTransaction(testContext.client, setHookTx, wallet)
{
const ledgerEntryResponse = await testContext.client.request({
command: 'ledger_entry',
hook: { account: wallet.classicAddress },
})
const node = ledgerEntryResponse.result.node as Hook
const hook = node.Hooks[0].Hook
expect(hook.HookName).toBeUndefined()
}
})
})

View File

@@ -175,7 +175,7 @@ describe('SetHook', function () {
assert.throws(() => validate(setHookTx), ValidationError, errorMessage)
})
it.each(['', '0'.repeat(7), '0'.repeat(33), 'ZZZZZZZZ'])(
it.each(['00', '0000', '000000', '0'.repeat(7), '0'.repeat(33), 'ZZZZZZZZ'])(
`throws w/ invalid HookName in Hooks: %s`,
function (value: string) {
setHookTx.Hooks = [
@@ -195,4 +195,16 @@ describe('SetHook', function () {
assert.throws(() => validate(setHookTx), ValidationError, errorMessage)
},
)
it(`valid HookName "" (deletion) in Hooks`, function () {
setHookTx.Hooks = [
{
Hook: {
HookName: '',
},
},
]
assert.doesNotThrow(() => validateSetHook(setHookTx))
assert.doesNotThrow(() => validate(setHookTx))
})
})