Browse Source

New branch for cleanup and web push

kb/add-usage-guide
Matthew Raymer 1 year ago
parent
commit
852bd93f3f
  1. 117
      src/libs/endorserServer.ts

117
src/libs/endorserServer.ts

@ -112,27 +112,15 @@ export function isHiddenDid(did: string) {
/**
always returns text, maybe UNNAMED_VISIBLE or UNKNOWN_ENTITY
**/
export function didInfo(
did: string,
activeDid: string,
allMyDids: Array<string>,
contacts: Array<Contact>,
): string {
const myId: string | undefined = R.find(R.equals(did), allMyDids);
if (myId) {
return "You" + (myId !== activeDid ? " (Alt ID)" : "");
} else {
const contact: Contact | undefined = R.find((c) => c.did === did, contacts);
if (contact) {
return contact.name || "Someone Unnamed in Contacts";
} else if (!did) {
return "Unspecified Person";
} else if (isHiddenDid(did)) {
return "Someone Not In Network";
} else {
return "Someone Not In Contacts";
}
}
export function didInfo(did: string, activeDid: string, allMyDids: string[], contacts: Contact[]): string {
const myId = R.find(R.equals(did), allMyDids);
if (myId) return `You${myId !== activeDid ? " (Alt ID)" : ""}`;
const contact = R.find(c => c.did === did, contacts);
return contact ? contact.name || "Someone Unnamed in Contacts" :
!did ? "Unspecified Person" :
isHiddenDid(did) ? "Someone Not In Network" :
"Someone Not In Contacts";
}
export interface ResultWithType {
@ -171,30 +159,16 @@ export async function createAndSubmitGive(
fulfillsProjectHandleId?: string,
): Promise<CreateAndSubmitGiveResult> {
try {
// Make a claim
const vcClaim: GiveVerifiableCredential = {
"@context": "https://schema.org",
"@type": "GiveAction",
recipient: toDid ? { identifier: toDid } : undefined,
agent: fromDid ? { identifier: fromDid } : undefined,
description: description || undefined,
object: hours ? { amountOfThisGood: hours, unitCode: "HUR" } : undefined,
fulfills: fulfillsProjectHandleId ? { "@type": "PlanAction", identifier: fulfillsProjectHandleId } : undefined,
};
if (toDid) {
vcClaim.recipient = { identifier: toDid };
}
if (fromDid) {
vcClaim.agent = { identifier: fromDid };
}
if (description) {
vcClaim.description = description;
}
if (hours) {
vcClaim.object = { amountOfThisGood: hours, unitCode: "HUR" };
}
if (fulfillsProjectHandleId) {
vcClaim.fulfills = {
"@type": "PlanAction",
identifier: fulfillsProjectHandleId,
};
}
// Make a payload for the claim
const vcPayload = {
vc: {
"@context": ["https://www.w3.org/2018/credentials/v1"],
@ -205,14 +179,7 @@ export async function createAndSubmitGive(
// Create a signature using private key of identity
const firstKey = identity.keys[0];
if (!firstKey || !firstKey.privateKeyHex) {
throw {
error: "No private key",
message: `Your identifier ${identity.did} is not configured correctly. Use a different identifier.`,
};
}
const privateKeyHex = firstKey.privateKeyHex;
const privateKeyHex = firstKey?.privateKeyHex;
if (!privateKeyHex) {
throw {
@ -222,56 +189,40 @@ export async function createAndSubmitGive(
}
const signer = await SimpleSigner(privateKeyHex);
const alg = undefined;
// Create a JWT for the request
const vcJwt: string = await didJwt.createJWT(vcPayload, {
alg: alg,
issuer: identity.did,
signer: signer,
signer,
});
// Make the xhr request payload
const payload = JSON.stringify({ jwtEncoded: vcJwt });
const url = apiServer + "/api/v2/claim";
const url = `${apiServer}/api/v2/claim`;
const token = await accessToken(identity);
const headers = {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
};
const response = await axios.post(url, payload, { headers });
return {
type: "success",
response,
};
const response = await axios.post(url, payload, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});
return { type: "success", response };
} catch (error: unknown) {
let errorMessage: string;
if (error instanceof Error) {
// If it's a JavaScript Error object
errorMessage = error.message;
} else if (
typeof error === "object" &&
error !== null &&
"message" in error
) {
// If it's an object that has a 'message' property
errorMessage = (error as { message: string }).message;
} else {
// Unknown error shape, default message
errorMessage = "Unknown error";
}
const errorMessage: string =
error instanceof Error ? error.message :
(typeof error === "object" && error?.message) ? error.message :
"Unknown error";
return {
type: "error",
error: {
error: errorMessage,
userMessage: "Failed to create and submit the claim.",
},
};
userMessage: "Failed to create and submit the claim."
}
};
}
}

Loading…
Cancel
Save