Skip to content

Commit

Permalink
SHARD-1197: Refactor calculateAppReceiptDataHash (#114)
Browse files Browse the repository at this point in the history
* Refactor receipt handling: add `validateAppReceiptData`, simplify `calculateAppReceiptDataHash`, improve error handling, and remove data copying.

* Fix app receipt validation logic: ensure invalid appReceiptData results in correct validation state and update comments for clarity.
  • Loading branch information
S0naliThakur authored Dec 30, 2024
1 parent a0321aa commit 6dc460a
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/shardeum/verifyAppReceiptData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ export const verifyAppReceiptData = async (
// }
} else result = { valid: true, needToSave: true }

// Finally verify appReceiptData hash
const appReceiptDataCopy = { ...appReceiptData }
const calculatedAppReceiptDataHash = calculateAppReceiptDataHash(
appReceiptDataCopy,
failedReasons,
nestedCounterMessages
)
if (!validateAppReceiptData(appReceiptData, failedReasons, nestedCounterMessages)) {
result = { valid: false, needToSave: false }
return result
}

const calculatedAppReceiptDataHash = calculateAppReceiptDataHash(appReceiptData)

if (calculatedAppReceiptDataHash !== signedReceipt.proposal.appReceiptDataHash) {
failedReasons.push(
`appReceiptData hash mismatch: ${calculatedAppReceiptDataHash} != ${signedReceipt.proposal.appReceiptDataHash}`
Expand All @@ -120,19 +120,14 @@ export const verifyAppReceiptData = async (
}
return result
}

// Converting the correct appReceipt data format to get the correct hash
const calculateAppReceiptDataHash = (
appReceiptData: any,
failedReasons = [],
nestedCounterMessages = []
): string => {
const validateAppReceiptData = (appReceiptData: any, failedReasons = [], nestedCounterMessages = []): boolean => {
try {
if (appReceiptData.data && appReceiptData.data.receipt) {
if (appReceiptData.data.receipt.bitvector)
if (appReceiptData.data.receipt.bitvector) {
appReceiptData.data.receipt.bitvector = Uint8Array.from(
Object.values(appReceiptData.data.receipt.bitvector)
)
}
if (appReceiptData.data.receipt.logs && appReceiptData.data.receipt.logs.length > 0) {
appReceiptData.data.receipt.logs = appReceiptData.data.receipt.logs.map((log) => {
return log.map((log1) => {
Expand All @@ -149,12 +144,16 @@ const calculateAppReceiptDataHash = (
})
}
}
const hash = crypto.hashObj(appReceiptData)
return hash
return true
} catch (err) {
console.error(`calculateAppReceiptDataHash error: ${err}`)
failedReasons.push(`calculateAppReceiptDataHash error: ${err}`)
nestedCounterMessages.push(`calculateAppReceiptDataHash error`)
return ''
console.error(`validateAppReceiptData error: ${err}`)
failedReasons.push(`validateAppReceiptData error: ${err}`)
nestedCounterMessages.push(`validateAppReceiptData error`)
return false
}
}

// Use validateAppReceiptData to ensure appReceiptData is valid before calculating its hash with calculateAppReceiptDataHash
const calculateAppReceiptDataHash = (appReceiptData: any): string => {
return crypto.hashObj(appReceiptData)
}

0 comments on commit 6dc460a

Please sign in to comment.