Skip to content

Commit

Permalink
feat(iam): fixing the API for converting the credential
Browse files Browse the repository at this point in the history
  • Loading branch information
nutrina committed Sep 8, 2023
1 parent 51757e7 commit 14d1f06
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
73 changes: 71 additions & 2 deletions iam/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
PassportAttestation,
EasRequestBody,
VerifiedPayload,
VerifiableCredential,
} from "@gitcoin/passport-types";
import onchainInfo from "../../deployments/onchainInfo.json";

Expand All @@ -42,7 +43,9 @@ import {
issueChallengeCredential,
issueHashedCredential,
verifyCredential,
issueEip712Credential,
} from "@gitcoin/passport-identity/dist/commonjs/src/credentials";
import { stampCredentialDocument } from "@gitcoin/passport-identity/dist/commonjs/src/signingDocuments";

// All provider exports from platforms
import { providers, platforms } from "@gitcoin/passport-platforms";
Expand Down Expand Up @@ -173,8 +176,11 @@ const errorRes = (res: Response, error: string, errorCode: number): Response =>

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const addErrorDetailsToMessage = (message: string, error: any): string => {
if (error instanceof IAMError || error instanceof Error) message += `, ${error.name}: ${error.message}`;

if (error instanceof IAMError || error instanceof Error) {
message += `, ${error.name}: ${error.message}`;
} else if (typeof error === "string") {
message += `, ${error}`;
}
return message;
};

Expand Down Expand Up @@ -446,6 +452,69 @@ app.post("/api/v0.0.0/verify", (req: Request, res: Response): void => {
});
});

// expose convert entry point that will convert a V1 stamp to V2 stamp (Ed25519Signature2018 to EIP712)
app.post("/api/v0.0.0/convert", (req: Request, res: Response): void => {
const credential: VerifiableCredential = req.body as VerifiableCredential;

// Validate credential first
verifyCredential(DIDKit, credential)
.then((verified) => {
// If the credential has been succesfully validateLocaleAndSetLanguage,
// and if the issuer matches our expected issuer
// then issue the new credential
if (verified && issuer === credential.issuer) {
DIDKit.keyToVerificationMethod("ethr", eip712Key)
.then((_verificationMethod) => {
const verificationMethod = _verificationMethod as string;
// Extract the payload from the old VC
const { id, hash, provider } = credential.credentialSubject;

issueEip712Credential(
DIDKit,
eip712Key,
{ expiresAt: new Date(credential.expirationDate) },
{
credentialSubject: {
"@context": [
{
customInfo: "https://schema.org/Thing",
hash: "https://schema.org/Text",
metaPointer: "https://schema.org/URL",
provider: "https://schema.org/Text",
},
],

id,
hash,
provider,
},
},
stampCredentialDocument(verificationMethod),
["https://w3id.org/vc/status-list/2021/v1"]
)
.then((credential) => {
res.json(credential);
})
.catch((error) => {
return void errorRes(res, addErrorDetailsToMessage("Failed to issue new credential", error), 500);
});
})
.catch((error) => {
return void errorRes(res, addErrorDetailsToMessage("Failed to create verification method", error), 500);
});
} else {
return void errorRes(res, "Invalid credential.", 400);
}
})
.catch((error) => {
return void errorRes(
res,
addErrorDetailsToMessage("Unable to validate the provided credential, an unexpected error occured", error),
500
);
});
});

// Expose entry point for getting eas payload for moving stamps on-chain (Stamp Attestations)
// This function will receive an array of stamps, validate them and return an array of eas payloads
app.post("/api/v0.0.0/eas", (req: Request, res: Response): void => {
Expand Down
26 changes: 19 additions & 7 deletions identity/src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,30 @@ const _issueEd25519Credential = async (
return JSON.parse(credential) as VerifiableCredential;
};

const _issueEip712Credential = async (
type CredentialExiresInSeconds = {
expiresInSeconds: number;
};

type CredentialExiresAt = {
expiresAt: Date;
};

export const issueEip712Credential = async (
DIDKit: DIDKitLib,
key: string,
expiresInSeconds: number,
expiration: CredentialExiresInSeconds | CredentialExiresAt,
fields: { [k: string]: any }, // eslint-disable-line @typescript-eslint/no-explicit-any
signingDocument: DocumentSignatureTypes<DocumentType>,
additionalContexts: string[] = []
): Promise<VerifiableCredential> => {
// get DID from key
const issuer = DIDKit.keyToDID("ethr", key);

const expirationDate = addSeconds(new Date(), expiresInSeconds).toISOString();
const expiresInSeconds = (expiration as CredentialExiresInSeconds).expiresInSeconds;
const expirationDate =
expiresInSeconds !== undefined
? addSeconds(new Date(), expiresInSeconds).toISOString()
: (expiration as CredentialExiresAt).expiresAt.toISOString();
const credentialInput = {
"@context": ["https://www.w3.org/2018/credentials/v1", ...additionalContexts],
type: ["VerifiableCredential"],
Expand Down Expand Up @@ -130,10 +142,10 @@ export const issueChallengeCredential = async (
if (signatureType === "EIP712") {
const verificationMethod = await DIDKit.keyToVerificationMethod("ethr", key);

credential = await _issueEip712Credential(
credential = await issueEip712Credential(
DIDKit,
key,
CHALLENGE_EXPIRES_AFTER_SECONDS,
{ expiresInSeconds: CHALLENGE_EXPIRES_AFTER_SECONDS },
{
credentialSubject: {
"@context": [
Expand Down Expand Up @@ -201,10 +213,10 @@ export const issueHashedCredential = async (
if (signatureType === "EIP712") {
const verificationMethod = await DIDKit.keyToVerificationMethod("ethr", key);
// generate a verifiableCredential
credential = await _issueEip712Credential(
credential = await issueEip712Credential(
DIDKit,
key,
expiresInSeconds,
{ expiresInSeconds },
{
credentialSubject: {
"@context": [
Expand Down

0 comments on commit 14d1f06

Please sign in to comment.