Skip to content

Commit

Permalink
refactor verifyDebugDropNGT() to return a more useful object
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmxdiqbal committed Dec 17, 2024
1 parent 55598db commit 56d82ed
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/p2p/ServiceQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,8 @@ const debugDropNGTGossipRoute: P2P.P2PTypes.GossipHandler<any> = async (payload,
cycle = CycleChain.getCycleChain(cycle.counter - 1, cycle.counter - 1)[0]
}
const verificationResult = verifyDebugDropNGT(payload, cycle)
if (verificationResult === 1) {
console.log('debug-drop-ngt - signer is not authorized')
return
}
if (verificationResult === 2) {
console.log('debug-drop-ngt - signature invalid')
if (verificationResult.success === false) {
console.log(`debug-drop-ngt - ${verificationResult.message}`)
return
}
const unsignedRemoveNetworkTx = {
Expand Down Expand Up @@ -289,15 +285,12 @@ export function init(): void {
}

const verificationResult = verifyDebugDropNGT(reqParamsDropNGT, CycleChain.newest)
if (verificationResult === 1) {
res.send({ status: 'fail', error: 'signer is not authorized' })
}
if (verificationResult === 2) {
res.send({ status: 'fail', error: 'signature invalid' })
if (verificationResult.success === false) {
res.send({ status: 'fail', error: verificationResult.message })
}

debugDropNGTs.push(reqParamsDropNGT)
res.json({ status: 'ok' })
res.json({ status: 'ok', message: verificationResult.message })
})

network.registerExternalGet('debug-clear-network-txlist', isDebugModeMiddleware, (req, res) => {
Expand Down Expand Up @@ -824,7 +817,7 @@ function sortedInsert(
}
}

function verifyDebugDropNGT(reqParamsDropNGT, cycle): number {
function verifyDebugDropNGT(reqParamsDropNGT, cycle): { success: boolean; message: string } {
const payload = {
route: stripQueryParams(reqParamsDropNGT.url, ['sig', 'sig_counter', 'nodePubkeys']), //<- we're gonna hash, these query artificats need to be excluded from the hash
count: reqParamsDropNGT.sigCounter,
Expand Down Expand Up @@ -853,15 +846,24 @@ function verifyDebugDropNGT(reqParamsDropNGT, cycle): number {
if (verified === true) {
const authorized = ensureKeySecurity(ownerPk, DevSecurityLevel.High)
if (authorized) {
return 0
return {
success: true,
message: 'Signature is correct and signer is authorized'
}
} else {
/* prettier-ignore */ if (logFlags.verbose) console.log('Authorization failed for security level HIGH')
/* prettier-ignore */ nestedCountersInstance.countEvent( 'security', 'Authorization failed for security level: HIGH' )
return 1
/* prettier-ignore */ nestedCountersInstance.countEvent( 'security', 'Authorization failed for security level HIGH' )
return {
success: false,
message: 'Authorization failed for security level HIGH'
}
}
} else {
/* prettier-ignore */ if (logFlags.verbose) console.log('Signature is not correct')
return 2
/* prettier-ignore */ if (logFlags.verbose) console.log('Signature verification failed')
return {
success: false,
message: 'Signature verification failed'
}
}
}

Expand All @@ -883,15 +885,24 @@ function verifyDebugDropNGT(reqParamsDropNGT, cycle): number {
reqParamsDropNGT.owner = ownerPk
const authorized = ensureKeySecurity(ownerPk, DevSecurityLevel.High)
if (authorized) {
return 0
return {
success: true,
message: 'Signature is correct and signer is authorized'
}
} else {
/* prettier-ignore */ if (logFlags.verbose) console.log('Authorization failed for security level HIGH')
/* prettier-ignore */ nestedCountersInstance.countEvent( 'security', 'Authorization failed for security level: HIGH' )
return 1
/* prettier-ignore */ nestedCountersInstance.countEvent( 'security', 'Authorization failed for security level HIGH' )
return {
success: false,
message: 'Authorization failed for security level HIGH'
}
}
} else {
/* prettier-ignore */ if (logFlags.verbose) console.log('Signature is not correct')
return 2
/* prettier-ignore */ if (logFlags.verbose) console.log('Signature verification failed')
return {
success: false,
message: 'Signature verification failed'
}
}
}
}
Expand Down

0 comments on commit 56d82ed

Please sign in to comment.