Skip to content

Commit

Permalink
chore(iam): finished IAM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianHymer committed Oct 11, 2024
1 parent 4dc46b9 commit 2c8eaaf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
17 changes: 11 additions & 6 deletions iam/__tests__/scrollDevBadge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from "ethers";
import { scrollDevBadgeHandler, getScrollRpcUrl } from "../src/utils/scrollDevBadge";
import { getAttestationSignerForChain } from "../src/utils/attestations";
import { hasValidIssuer } from "../src/issuers";
import { getEASFeeAmount } from "../src/utils/easFees";

// Mock external dependencies
jest.mock("@spruceid/didkit-wasm-node");
Expand All @@ -19,12 +20,14 @@ describe("scrollDevBadgeHandler", () => {

beforeEach(() => {
const mockSigner = {
_signTypedData: jest.fn().mockResolvedValue("0xsignature"),
_signTypedData: jest.fn().mockResolvedValue("0xSignature"),
};
(getAttestationSignerForChain as jest.Mock).mockResolvedValue(mockSigner);

(hasValidIssuer as jest.Mock).mockReturnValue(true);

(getEASFeeAmount as jest.Mock).mockReturnValue(1234);

mockJson = jest.fn();
mockStatus = jest.fn().mockReturnThis();
mockRes = {
Expand All @@ -46,16 +49,16 @@ describe("scrollDevBadgeHandler", () => {
level: 1,
},
});
process.env.SCROLL_BADGE_ATTESTATION_SCHEMA_UID = "0xschema";
process.env.SCROLL_BADGE_ATTESTATION_SCHEMA_UID = "0xSchema";
process.env.ALCHEMY_API_KEY = "test-api-key";

(ethers.Contract as jest.Mock).mockImplementation(() => ({
(ethers.Contract as unknown as jest.Mock).mockImplementation(() => ({
badgeLevel: jest.fn().mockResolvedValue({ toNumber: () => 0 }),
}));
});

it("should return an error if no credentials are provided", async () => {
scrollDevBadgeHandler(mockReq as Request, mockRes as Response);
await scrollDevBadgeHandler(mockReq as Request, mockRes as Response);
expect(mockStatus).toHaveBeenCalledWith(400);
expect(mockJson).toHaveBeenCalledWith({ error: "No stamps provided" });
});
Expand All @@ -72,17 +75,19 @@ describe("scrollDevBadgeHandler", () => {
credentialSubject: {
id: "did:pkh:eip155:1:0x1234567890123456789012345678901234567890",
provider: "test-provider",
hash: "base64:dGVzdGhhc2g=",
hash: "v0.0.0:JnHtXuRm2roGRwbYfHtWYSwMma3Oeh3yUl3hmZ3k96U=",
},
issuer: "did:key:test",
};
mockReq.body.credentials = [mockCredential];

// Mock the necessary functions
jest.spyOn(ethers.utils, "splitSignature").mockReturnValue({ v: 27, r: "0xr", s: "0xs" });
jest.spyOn(ethers.utils, "splitSignature").mockReturnValue({ v: 27, r: "0xr", s: "0xs" } as any);

await scrollDevBadgeHandler(mockReq as Request, mockRes as Response);

await new Promise((resolve) => setTimeout(resolve, 400));

expect(mockJson).toHaveBeenCalled();
const response = mockJson.mock.calls[0][0];
expect(response).toHaveProperty("passport");
Expand Down
24 changes: 1 addition & 23 deletions iam/src/utils/scrollDevBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ async function queryBadgeLevel({
}
}

export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
export const scrollDevBadgeHandler = (req: Request, res: Response): Promise<void> => {
try {
console.log("Processing scroll badge request");
const { credentials, nonce, chainIdHex } = req.body as EasRequestBody;
if (!Object.keys(onchainInfo).includes(chainIdHex)) {
return void errorRes(res, `No onchainInfo found for chainId ${chainIdHex}`, 404);
Expand All @@ -110,15 +109,8 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
if (!credentials.every((credential) => credential.credentialSubject.id.split(":")[4] === recipient))
return void errorRes(res, "Every credential's id must be equivalent", 400);

console.log("A");
Promise.all(
credentials.map(async (credential) => {
console.log(
"Verifying credential",
credential,
hasValidIssuer(credential.issuer),
await verifyCredential(DIDKit, credential)
);
return {
credential,
verified: hasValidIssuer(credential.issuer) && (await verifyCredential(DIDKit, credential)),
Expand All @@ -135,22 +127,17 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
> = JSON.parse(process.env.SCROLL_BADGE_PROVIDER_INFO);

const badgeProviders = Object.keys(SCROLL_BADGE_PROVIDER_INFO);
console.log("B");

const invalidCredentials = credentialVerifications
.filter(
({ verified, credential }) => !verified || !badgeProviders.includes(credential.credentialSubject.provider)
)
.map(({ credential }) => credential);

console.log("badgeProviders", badgeProviders);
console.log(JSON.stringify(invalidCredentials, null, 2));
if (invalidCredentials.length > 0) {
return void errorRes(res, { invalidCredentials }, 400);
}

console.log("C");

const groupedCredentialInfo = credentialVerifications.reduce(
(acc, { credential }) => {
const provider = credential.credentialSubject.provider;
Expand All @@ -175,8 +162,6 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
>
);

console.log({ groupedCredentialInfo });

const rpcUrl = getScrollRpcUrl({ chainIdHex: attestationChainIdHex });
const onchainBadgeData = await Promise.all(
Object.keys(groupedCredentialInfo).map(async (contractAddress) => {
Expand Down Expand Up @@ -210,8 +195,6 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {

const hashes = credentialInfo.filter(({ level }) => requiredLevels.includes(level)).map(({ hash }) => hash);

console.log({ contractAddress, maxCredentialLevel, hashes });

const data = BADGE_SCHEMA_ENCODER.encodeData([
{ name: "badge", value: contractAddress, type: "address" },
{
Expand All @@ -232,7 +215,6 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
})
.filter(Boolean);

console.log({ badgeRequestData });
if (badgeRequestData.length === 0) {
return void errorRes(res, "All badges already claimed", 400);
}
Expand All @@ -251,8 +233,6 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
fee: fee.toString(),
};

console.log("passportAttestation", JSON.stringify(passportAttestation, null, 2));

const domainSeparator = getAttestationDomainSeparator(attestationChainIdHex);

const signer = await getAttestationSignerForChain(attestationChainIdHex);
Expand All @@ -262,8 +242,6 @@ export const scrollDevBadgeHandler = (req: Request, res: Response): void => {
.then((signature) => {
const { v, r, s } = utils.splitSignature(signature);

console.log({ v, r, s });

const payload: EasPayload = {
passport: passportAttestation,
signature: { v, r, s },
Expand Down

0 comments on commit 2c8eaaf

Please sign in to comment.