Browse Source

New branch for cleanup and web push

new-web-push
Matthew Raymer 1 year ago
parent
commit
852bd93f3f
  1. 113
      src/libs/endorserServer.ts

113
src/libs/endorserServer.ts

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

Loading…
Cancel
Save