Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
harden metadata loading
  • Loading branch information
H34D committed Nov 16, 2022
1 parent d09883f commit 9a5a6b3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/2fa/2FA.ts
@@ -1,5 +1,5 @@
import { create2FA } from "./create";
import { burn2fa } from "./burn";
import { burn2FA } from "./burn";
import { list2FAs, load2FAsByIdentityId } from "./list";
import { BigNumber } from "ethers";
import Masa from "../masa";
Expand All @@ -14,7 +14,7 @@ export const twoFA = (masa: Masa) => ({
generate: (phoneNumber: string) => masa.client.twoFAGenerate(phoneNumber),
create: (phoneNumber: string, code: string) =>
create2FA(masa, phoneNumber, code),
burn: (twofaId: number) => burn2fa(masa, twofaId),
burn: (twoFAId: number) => burn2FA(masa, twoFAId),
list: (address?: string) => list2FAs(masa, address),
load: (identityId: BigNumber) => load2FAsByIdentityId(masa, identityId),
});
16 changes: 8 additions & 8 deletions src/2fa/burn.ts
@@ -1,13 +1,13 @@
import Masa from "../masa";

export const burn2faById = async (
export const burn2FAById = async (
masa: Masa,
twofaId: number
twoFAId: number
): Promise<boolean> => {
try {
const tx = await masa.contracts.identity.Soulbound2FA.connect(
masa.config.wallet
).burn(twofaId);
).burn(twoFAId);

console.log("Waiting for the burn tx to finalize");
await tx.wait();
Expand All @@ -20,21 +20,21 @@ export const burn2faById = async (
return false;
};

export const burn2fa = async (
export const burn2FA = async (
masa: Masa,
twofaId: number
twoFAId: number
): Promise<boolean> => {
let success = false;

if (await masa.session.checkLogin()) {
const identityId = await masa.identity.load();
if (!identityId) return success;
console.log(`Burning 2FA with id '${twofaId}'!`);
console.log(`Burning 2FA with id '${twoFAId}'!`);

success = await burn2faById(masa, twofaId);
success = await burn2FAById(masa, twoFAId);

if (success) {
console.log(`Burned 2FA with id '${twofaId}'!`);
console.log(`Burned 2FA with id '${twoFAId}'!`);
}
} else {
console.log("Not logged in please login first");
Expand Down
11 changes: 7 additions & 4 deletions src/2fa/list.ts
Expand Up @@ -10,7 +10,7 @@ export const load2FAsByIdentityId = async (
{
tokenId: BigNumber;
tokenUri: string;
metadata: I2FA;
metadata?: I2FA;
}[]
> => {
const twoFSIds: BigNumber[] =
Expand All @@ -25,7 +25,7 @@ export const load2FAsByIdentityId = async (
await masa.contracts.identity.Soulbound2FA.tokenURI(tokenId)
);

const metadata = (await masa.metadata.retrieve(tokenUri)) as I2FA;
const metadata = <I2FA | undefined>await masa.metadata.retrieve(tokenUri);

return {
tokenId,
Expand All @@ -43,7 +43,7 @@ export const list2FAs = async (
{
tokenId: BigNumber;
tokenUri: string;
metadata: I2FA;
metadata?: I2FA;
}[]
> => {
address = address || (await masa.config.wallet.getAddress());
Expand All @@ -58,8 +58,11 @@ export const list2FAs = async (
let i = 1;
for (const twoFA of twoFAs) {
console.log(`Token: ${i}`);
console.log(`Token ID: ${twoFA.tokenId}`);
i++;
console.log(`Metadata: ${JSON.stringify(twoFA.metadata, null, 2)}`);
if (twoFA.metadata) {
console.log(`Metadata: ${JSON.stringify(twoFA.metadata, null, 2)}`);
}
}

return twoFAs;
Expand Down
1 change: 1 addition & 0 deletions src/contracts/load-Identity-contracts.ts
Expand Up @@ -24,6 +24,7 @@ export const loadIdentityContracts = ({
provider ||
// or try to load from the browser
new ethers.providers.Web3Provider(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.ethereum
);
Expand Down
35 changes: 21 additions & 14 deletions src/credit-score/list.ts
Expand Up @@ -10,7 +10,7 @@ export const loadCreditScoresByIdentityId = async (
{
tokenId: BigNumber;
tokenUri: string;
metadata: ICreditScore;
metadata?: ICreditScore;
}[]
> => {
const creditScoreIds: BigNumber[] =
Expand All @@ -31,7 +31,9 @@ export const loadCreditScoresByIdentityId = async (
);

console.log(`Metadata Url: ${tokenUri}`);
const metadata = (await masa.metadata.retrieve(tokenUri)) as ICreditScore;
const metadata: ICreditScore | undefined = <ICreditScore | undefined>(
await masa.metadata.retrieve(tokenUri)
);

return {
tokenId,
Expand All @@ -46,25 +48,30 @@ export const listCreditReports = async (
masa: Masa,
address?: string
): Promise<
| {
tokenId: BigNumber;
tokenUri: string;
metadata: ICreditScore;
}[]
| undefined
{
tokenId: BigNumber;
tokenUri: string;
metadata?: ICreditScore;
}[]
> => {
address = address || (await masa.config.wallet.getAddress());

const identityId = await masa.identity.load(address);
if (!identityId) return;
if (!identityId) return [];

const creditReports = await loadCreditScoresByIdentityId(masa, identityId);
const creditScores = await loadCreditScoresByIdentityId(masa, identityId);

if (creditReports.length === 0) console.log("No Credit Scores found");
if (creditScores.length === 0) console.log("No Credit Scores found");

for (const creditReport of creditReports) {
console.log(`Metadata: ${JSON.stringify(creditReport.metadata, null, 2)}`);
let i = 1;
for (const creditScore of creditScores) {
console.log(`Token: ${i}`);
console.log(`Token ID: ${creditScore.tokenId}`);
i++;
if (creditScore.metadata) {
console.log(`Metadata: ${JSON.stringify(creditScore.metadata, null, 2)}`);
}
}

return creditReports;
return creditScores;
};
File renamed without changes.
2 changes: 1 addition & 1 deletion src/identity/index.ts
Expand Up @@ -2,4 +2,4 @@ export * from "./burn";
export * from "./create";
export * from "./load";
export * from "./show";
export * from "./indetity";
export * from "./identity";
4 changes: 3 additions & 1 deletion src/identity/show.ts
Expand Up @@ -18,7 +18,9 @@ export const loadIdentityDetails = async (
](identityId)
);

const metadata = (await masa.metadata.retrieve(tokenUri)) as IIdentity;
const metadata: IIdentity | undefined = <IIdentity | undefined>(
await masa.metadata.retrieve(tokenUri)
);

return {
tokenId: identityId,
Expand Down
14 changes: 9 additions & 5 deletions src/utils/clients/middleware.ts
Expand Up @@ -42,11 +42,15 @@ export class MasaClient {
getMetadata = async (
uri: string
): Promise<IIdentity | ICreditScore | I2FA | undefined> => {
const metadataResponse = await this.middlewareClient.get(uri, {
headers: {
cookie: this.cookie ? [this.cookie] : undefined,
},
});
const metadataResponse = await this.middlewareClient
.get(uri, {
headers: {
cookie: this.cookie ? [this.cookie] : undefined,
},
})
.catch((err) => {
console.error("Failed to load Metadata!", err.message, uri);
});

if (metadataResponse) {
const { data: metadata } = metadataResponse;
Expand Down

0 comments on commit 9a5a6b3

Please sign in to comment.