-
Notifications
You must be signed in to change notification settings - Fork 6
[Fil 1005] extended refreshes with the gov teams approval/rejection step #125
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
Merged
JAG-UK
merged 4 commits into
fidlabs:master
from
RafalMagrys:FIL-1005-rkhp-extend-refreshes-with-the-gov-teams-approval-rejection-step
Sep 23, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
packages/application/src/api/http/controllers/authutils.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { getPublicKey, etc } from '@noble/secp256k1'; | ||
| import { hmac } from '@noble/hashes/hmac'; | ||
| import { sha256 } from '@noble/hashes/sha256'; | ||
| import cbor from 'cbor'; | ||
| import { verifyLedgerPoP } from './authutils'; | ||
| import { FilecoinTxBuilder } from '@src/testing/mocks/builders'; | ||
|
|
||
| // Ensure noble-secp256k1 HMAC is set in case setup didn't run yet | ||
| if (!etc.hmacSha256Sync) { | ||
| etc.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => | ||
| hmac(sha256, key, etc.concatBytes(...msgs)); | ||
| } | ||
|
|
||
| describe('verifyLedgerPoP (integration)', () => { | ||
| it('returns true for a valid signed transaction and matching challenge', async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withChallenge(challenge) | ||
| .build(); | ||
|
|
||
| const ok = await verifyLedgerPoP(address, pubKeyBase64, transaction, challenge); | ||
| expect(ok).toBe(true); | ||
| }); | ||
|
|
||
| it("throws when To/From/Nonce don't match expected (replay guard)", async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withChallenge(challenge) | ||
| .build(); | ||
|
|
||
| const parsed = JSON.parse(transaction); | ||
| parsed.Message.From = 'f1different'; | ||
|
|
||
| await expect( | ||
| verifyLedgerPoP(address, pubKeyBase64, JSON.stringify(parsed), challenge), | ||
| ).rejects.toThrow("addresses don't match"); | ||
| }); | ||
|
|
||
| it('throws when derived address from pubkey does not match provided address', async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, transaction } = await new FilecoinTxBuilder().withChallenge(challenge).build(); | ||
|
|
||
| // Provide a mismatching pubkey (different private key) | ||
| const otherPriv = Uint8Array.from( | ||
| Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), | ||
| ); | ||
| const otherPub = getPublicKey(otherPriv, false); | ||
| const otherPubB64 = Buffer.from(otherPub).toString('base64'); | ||
|
|
||
| await expect(verifyLedgerPoP(address, otherPubB64, transaction, challenge)).rejects.toThrow( | ||
| 'wrong key for address', | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when pre-image doesn't match", async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withChallenge(challenge) | ||
| .build(); | ||
| await expect( | ||
| verifyLedgerPoP(address, pubKeyBase64, transaction, 'different-challenge'), | ||
| ).rejects.toThrow("pre-images don't match"); | ||
| }); | ||
|
|
||
| it("throws when signature doesn't exist", async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withChallenge(challenge) | ||
| .build(); | ||
| const parsed = JSON.parse(transaction); | ||
| parsed.Signature.Data = ''; | ||
|
|
||
| await expect( | ||
| verifyLedgerPoP(address, pubKeyBase64, JSON.stringify(parsed), challenge), | ||
| ).rejects.toThrow("signature doesn't exist"); | ||
| }); | ||
|
|
||
| it('throws when signature has wrong length', async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withChallenge(challenge) | ||
| .build(); | ||
| const parsed = JSON.parse(transaction); | ||
| const sigBytes = Buffer.from(parsed.Signature.Data, 'base64'); | ||
| // Drop recovery byte to make it 64 | ||
| const wrongLen = sigBytes.subarray(0, 64); | ||
| parsed.Signature.Data = Buffer.from(wrongLen).toString('base64'); | ||
|
|
||
| await expect( | ||
| verifyLedgerPoP(address, pubKeyBase64, JSON.stringify(parsed), challenge), | ||
| ).rejects.toThrow('Bad signature length: 64'); | ||
| }); | ||
|
|
||
| it('should throw "addresses don\'t match" when nonce is not 0', async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withCustomMessage({ Nonce: 1 }) | ||
| .withChallenge(challenge) | ||
| .build(); | ||
|
|
||
| await expect(verifyLedgerPoP(address, pubKeyBase64, transaction, challenge)).rejects.toThrow( | ||
| "addresses don't match", | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw "addresses don\'t match" when address does not match', async () => { | ||
| const challenge = 'challenge'; | ||
| const { address, pubKeyBase64, transaction } = await new FilecoinTxBuilder() | ||
| .withChallenge(challenge) | ||
| .build(); | ||
|
|
||
| const parsed = JSON.parse(transaction); | ||
| parsed.Message.To = 'f1evc3p45ke4apzvi5ix25mniemuva6umggusmdif'; | ||
|
|
||
| await expect( | ||
| verifyLedgerPoP(address, pubKeyBase64, JSON.stringify(parsed), challenge), | ||
| ).rejects.toThrow("addresses don't match"); | ||
| }); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,11 @@ import { | |
| httpPut, | ||
| request, | ||
| requestBody, | ||
| requestParam, | ||
| response, | ||
| } from 'inversify-express-utils'; | ||
|
|
||
| import { badRequest, ok } from '@src/api/http/processors/response'; | ||
| import { badPermissions, badRequest, ok } from '@src/api/http/processors/response'; | ||
| import { TYPES } from '@src/types'; | ||
| import { RefreshIssuesCommand } from '@src/application/use-cases/refresh-issues/refresh-issues.command'; | ||
| import { GetRefreshesQuery } from '@src/application/queries/get-refreshes/get-refreshes.query'; | ||
|
|
@@ -21,6 +22,14 @@ import { UpsertIssueCommand } from '@src/application/use-cases/refresh-issues/up | |
| import { IssuesWebhookPayload } from '@src/infrastructure/clients/github'; | ||
| import { validateIssueUpsert, validateRefreshesQuery } from '@src/api/http/validators'; | ||
| import { RESPONSE_MESSAGES } from '@src/constants'; | ||
| import { validateGovernanceReview } from '../validators'; | ||
| import { validateRequest } from '../middleware/validate-request.middleware'; | ||
| import { GovernanceReviewDto } from '@src/application/dtos/GovernanceReviewDto'; | ||
| import { RoleService } from '@src/application/services/role.service'; | ||
| import { SignatureType } from '@src/patterns/decorators/signature-guard.decorator'; | ||
| import { SignatureGuard } from '@src/patterns/decorators/signature-guard.decorator'; | ||
| import { RejectRefreshCommand } from '@src/application/use-cases/refresh-issues/reject-refesh.command'; | ||
| import { ApproveRefreshCommand } from '@src/application/use-cases/refresh-issues/approve-refresh.command'; | ||
|
|
||
| const RES = RESPONSE_MESSAGES.REFRESH_CONTROLLER; | ||
|
|
||
|
|
@@ -30,6 +39,7 @@ export class RefreshController { | |
| @inject(TYPES.QueryBus) private readonly _queryBus: IQueryBus, | ||
| @inject(TYPES.CommandBus) private readonly _commandBus: ICommandBus, | ||
| @inject(TYPES.IssueMapper) private readonly _issueMapper: IIssueMapper, | ||
| @inject(TYPES.RoleService) private readonly _roleService: RoleService, | ||
| ) {} | ||
|
|
||
| @httpGet('', ...validateRefreshesQuery) | ||
|
|
@@ -83,4 +93,36 @@ export class RefreshController { | |
|
|
||
| return res.json(ok(RES.REFRESH_SUCCESS, result)); | ||
| } | ||
|
|
||
| @httpPost('/:githubIssueNumber/review', validateRequest(validateGovernanceReview)) | ||
| @SignatureGuard(SignatureType.RefreshReview) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reusable NestJS like guard to handle signature |
||
| async approveRefresh( | ||
| @requestParam('githubIssueNumber') githubIssueNumber: string, | ||
| @requestBody() approveRefreshDto: GovernanceReviewDto, | ||
| @response() res: Response, | ||
| ) { | ||
| const id = parseInt(githubIssueNumber); | ||
| const address = approveRefreshDto.details.reviewerAddress; | ||
| const role = this._roleService.getRole(address); | ||
| if (role !== 'GOVERNANCE_TEAM') { | ||
| console.log(`Not a governance team member: ${role}`); | ||
| return res.status(403).json(badPermissions()); | ||
| } | ||
|
|
||
| const { result } = approveRefreshDto; | ||
|
|
||
| const command = | ||
| result === 'approve' | ||
| ? new ApproveRefreshCommand(id, approveRefreshDto.details.finalDataCap) | ||
| : new RejectRefreshCommand(id); | ||
|
|
||
| const refreshResult = await this._commandBus.send(command); | ||
| if (!refreshResult.success) { | ||
| return res | ||
| .status(400) | ||
| .json(badRequest(RES.FAILED_TO_UPSERT_ISSUE, [refreshResult.error.message])); | ||
| } | ||
|
|
||
| return res.json(ok(RES.REFRESH_SUCCESS, result)); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
packages/application/src/api/http/middleware/validate-request.middleware.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Request, Response, NextFunction } from 'express'; | ||
| import { validationResult, ValidationChain } from 'express-validator'; | ||
| import { badRequest } from '@src/api/http/processors/response'; | ||
|
|
||
| export function validateRequest(validators: ValidationChain[]) { | ||
| return async (req: Request, res: Response, next: NextFunction) => { | ||
| await Promise.all(validators.map(validator => validator.run(req))); | ||
|
|
||
| const errors = validationResult(req); | ||
|
|
||
| if (!errors.isEmpty()) { | ||
| const errorMessages = errors.array().map(error => error.msg); | ||
| return res.status(400).json(badRequest('Validation failed', errorMessages)); | ||
| } | ||
|
|
||
| next(); | ||
| }; | ||
| } |
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
65 changes: 65 additions & 0 deletions
65
packages/application/src/api/http/validators/governance-review.validator.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { VALIDATION_MESSAGES } from '@src/constants/validation-messages'; | ||
| import { body, param } from 'express-validator'; | ||
|
|
||
| export const validateGovernanceReview = [ | ||
| param('githubIssueNumber') | ||
| .isInt({ min: 1, max: Number.MAX_SAFE_INTEGER }) | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_GITHUB_ISSUE_NUMBER.INVALID) | ||
| .bail(), | ||
|
|
||
| body('result') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_RESULT.REQUIRED) | ||
| .bail() | ||
| .isIn(['approve', 'reject']) | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_RESULT.INVALID) | ||
| .bail(), | ||
|
|
||
| body('details') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS.REQUIRED) | ||
| .bail() | ||
| .isObject() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS.INVALID), | ||
|
|
||
| body('details.reviewerAddress') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_REVIEWER_ADDRESS.REQUIRED) | ||
| .bail() | ||
| .isString() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_REVIEWER_ADDRESS.INVALID) | ||
| .bail(), | ||
|
|
||
| body('details.reviewerPublicKey') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_REVIEWER_PUBLIC_KEY.REQUIRED) | ||
| .bail() | ||
| .isString() | ||
| .bail() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_REVIEWER_PUBLIC_KEY.INVALID) | ||
| .bail(), | ||
|
|
||
| body('details.finalDataCap') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_FINAL_DATACAP.REQUIRED) | ||
| .bail() | ||
| .isNumeric() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_FINAL_DATACAP.INVALID) | ||
| .bail(), | ||
|
|
||
| body('details.allocatorType') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_ALLOCATOR_TYPE.REQUIRED) | ||
| .bail() | ||
| .isString() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_ALLOCATOR_TYPE.INVALID) | ||
| .bail(), | ||
|
|
||
| body('signature') | ||
| .exists() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_SIGNATURE.REQUIRED) | ||
| .bail() | ||
| .isString() | ||
| .withMessage(VALIDATION_MESSAGES.GOVERNANCE_REVIEW_DETAILS_SIGNATURE.INVALID) | ||
| .bail(), | ||
| ]; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './github-issue.validator'; | ||
| export * from './governance-review.validator'; |
12 changes: 12 additions & 0 deletions
12
packages/application/src/application/dtos/GovernanceReviewDto.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export interface GovernanceReviewDetailsDto { | ||
| reviewerAddress: string; | ||
| reviewerPublicKey: string; | ||
| finalDataCap: number; | ||
| allocatorType: string; | ||
| } | ||
|
|
||
| export interface GovernanceReviewDto { | ||
| result: string; | ||
| details: GovernanceReviewDetailsDto; | ||
| signature: string; | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added tests for the authutils because I want to understand how it works in order to test the endpoint.