fix: JWT wallet passthrough, duration-aware premium, Unix-second timestamps, pagination clamping#143
Open
CelestinaBeing wants to merge 1 commit into
Conversation
…um, fix timestamps, clamp pagination Closes Parashield-Protocol#111, Closes Parashield-Protocol#112, Closes Parashield-Protocol#113, Closes Parashield-Protocol#114 - fix(Parashield-Protocol#112): confirmPolicy now throws UnauthorizedException when no wallet in JWT/header (instead of a misleading ForbiddenException), and passes the verified authenticatedWallet to confirmAndCreatePolicy as a separate argument. The service validates the XDR source against the JWT wallet first, then against dto.walletAddress, so neither can be spoofed independently. - fix(Parashield-Protocol#111): calculatePremium now accepts durationDays and pro-rates the premium against a 30-day base period (coverage * rate * duration / (10000 * 30)), matching the PolicyEngine contract formula. Both the buyPolicy quote in the controller and the createPolicy call in the service now pass dto.duration so short/long policies price correctly. - fix(Parashield-Protocol#113): createPolicy now derives startTime and endTime from Unix seconds (Math.floor(Date.now() / 1000)) before constructing Date objects, eliminating the sub-second millisecond component that caused policies to expire 0-999 ms early compared to Soroban contract timestamps. - fix(Parashield-Protocol#114): getClaimsByWallet clamps page to min 1 and limit to 1-100 before calculating skip = (safePage - 1) * safeLimit, preventing negative skip values (page=0), zero take values (limit=0), and unbounded result sets (limit=10000) that caused row skips or repeats.
|
@CelestinaBeing Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four backend bug fixes across
policy.controller.ts,policy.service.ts, andclaims.service.ts:#112 —
confirmPolicydoes not pass wallet from JWT to serviceRoot cause: When
req.user?.walletAddressandreq.walletwere both absent,authedWalletwasundefinedand noUnauthorizedExceptionwas thrown. The service received onlydtoand never saw the JWT-verified wallet.Fix:
confirmPolicynow explicitly throwsUnauthorizedException('Not authenticated')whenauthedWalletis falsy.authedWalletis passed toconfirmAndCreatePolicy(dto, authedWallet)as a typed second argument.tx.source === authenticatedWallet(JWT wallet) first, thentx.source === dto.walletAddress, so neither can be spoofed independently.#111 —
calculatePremiumdoes not account for durationRoot cause:
calculatePremium(coverageXlm, premiumRate)ignoredduration, so a 30-day and a 365-day policy quoted the same premium.Fix:
calculatePremiumsignature extended to(coverageXlm, premiumRate, durationDays).Math.ceil(coverage * rate * duration / (10000 * 30))— pro-rated against a 30-day base period, matching the PolicyEngine contract.buyPolicy(controller) and the storedpremiumPaidincreatePolicy(service) now passdto.duration.#113 —
createPolicyuses JavaScript Date milliseconds vs Soroban Unix secondsRoot cause:
new Date()includes milliseconds; Soroban timestamps are Unix seconds. A policy stored withendTime = new Date(ms)wherems % 1000 ≠ 0could expire 0–999 ms before the on-chain contract expected.Fix:
#114 —
getClaimsByWalletpagination can skip rows or return unbounded resultsRoot cause: Raw
pageandlimitvalues were passed straight to Prisma.page=0producedskip = -limit(Prisma error or negative offset),limit=0producedtake=0(empty result forever), andlimit=100000could return the entire table.Fix:
Page numbers are 1-based; limit is clamped to 1–100.
Files changed
src/policy/policy.controller.tssrc/policy/policy.service.tssrc/claims/claims.service.tsCloses #111, Closes #112, Closes #113, Closes #114