Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHARD-1197: Refactor calculateAppReceiptDataHash #114

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading