Skip to content

Commit

Permalink
feat: load stamps from cache api before ceramic
Browse files Browse the repository at this point in the history
Co-authored-by: schultztimothy <[email protected]>
Co-authored-by: Lucian <[email protected]>
  • Loading branch information
3 people committed Feb 3, 2023
1 parent 49fafb8 commit f777cfb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
10 changes: 10 additions & 0 deletions app/context/ceramicContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ export type AllProvidersState = {
};
};

export class NoStampsInCacheError extends Error {
constructor(message: string) {
super(message);
this.name = "NoStampsInCacheError";
}
}

const getProviderSpec = (platform: PLATFORM_ID, provider: string): ProviderSpec => {
return STAMP_PROVIDERS[platform]
?.find((i) => i.providers.find((p) => p.name == provider))
Expand Down Expand Up @@ -561,6 +568,9 @@ export const CeramicContextProvider = ({ children }: { children: any }) => {
setPassport(undefined);
if (!skipLoadingState) setIsLoadingPassport(IsLoadingPassportState.FailedToConnect);
break;
case "NoStampsInCache":
datadogRum.addError("No stamps found in cache", { address });
throw new NoStampsInCacheError("No stamps found in cache");
}

setPassportLoadResponse({ passport, status, errorDetails });
Expand Down
23 changes: 11 additions & 12 deletions database-client/src/ceramicCacheClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ export class CeramicCacheDatabase implements DataStorageBase {
let errorDetails: PassportLoadErrorDetails;

try {
const response = await axios.get(
`${this.ceramicCacheUrl}/ceramic-cache/stamp?address=${this.address}`,
)
const response = await axios.get(`${this.ceramicCacheUrl}/ceramic-cache/stamp?address=${this.address}`);
const { data } = response;
if (data && data.success === 200) {
passport = {
issuanceDate: null,
expiryDate: null,
stamps: data.stamps,
};

if (data.stamps.length === 0) {
status = "NoStampsInCache";
}
}
} catch (e) {
Expand All @@ -61,14 +63,11 @@ export class CeramicCacheDatabase implements DataStorageBase {
this.logger.info(`adding stamp to ceramicCache address: ${this.address}`);
try {
// Todo will need to validate ownership / pass signature
await axios.post(
`${this.ceramicCacheUrl}/ceramic-cache/stamp`,
{
address: this.address,
provider: stamp.provider,
stamp: stamp.credential,
},
)
await axios.post(`${this.ceramicCacheUrl}/ceramic-cache/stamp`, {
address: this.address,
provider: stamp.provider,
stamp: stamp.credential,
});
} catch (e) {
this.logger.error(`Error saving stamp to ceramicCache address: ${this.address}:` + e.toString());
}
Expand All @@ -80,7 +79,7 @@ export class CeramicCacheDatabase implements DataStorageBase {
data: {
address: this.address,
provider: provider,
}
},
});
} catch (e) {
this.logger.error(`Error deleting stamp from ceramicCache for ${provider} on ${this.address}: ` + e.toString());
Expand Down
3 changes: 2 additions & 1 deletion types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export type PassportLoadStatus =
| "DoesNotExist"
| "ExceptionRaised"
| "StampCacaoError"
| "PassportCacaoError";
| "PassportCacaoError"
| "NoStampsInCache";

export type PassportLoadErrorDetails = {
stampStreamIds: string[];
Expand Down

0 comments on commit f777cfb

Please sign in to comment.