Skip to content

Commit

Permalink
full 2fa support
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Nov 8, 2022
1 parent f55fa42 commit 75d343e
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 102 deletions.
16 changes: 11 additions & 5 deletions src/2fa/2fa.ts
@@ -1,14 +1,20 @@
import { create2fa } from "./create";
import { burn2fa } from "./burn";
import { list2fas, load2faByIdentityId } from "./list";
import { list2fas, load2fasByIdentityId } from "./list";
import { BigNumber } from "ethers";
import Masa from "../masa";

export const twofa = (masa: Masa) => ({
mint: (address: string, phoneNumber: string, signature: string) =>
masa.client.twofaMint(address, phoneNumber, signature),
create: (phoneNumber: string) => create2fa(masa, phoneNumber),
mint: (
address: string,
phoneNumber: string,
code: string,
signature: string
) => masa.client.twoFAMint(address, phoneNumber, code, signature),
generate: (phoneNumber: string) => masa.client.twoFAGenerate(phoneNumber),
create: (phoneNumber: string, code: string) =>
create2fa(masa, phoneNumber, code),
burn: (twofaId: number) => burn2fa(masa, twofaId),
list: (address?: string) => list2fas(masa, address),
load: (identityId: BigNumber) => load2faByIdentityId(masa, identityId),
load: (identityId: BigNumber) => load2fasByIdentityId(masa, identityId),
});
6 changes: 3 additions & 3 deletions src/2fa/burn.ts
Expand Up @@ -16,7 +16,7 @@ export const burn2faById = async (

return true;
} catch (err: any) {
console.error(`Burning of 2fa Failed! '${err.message}'`);
console.error(`Burning of 2FA Failed! '${err.message}'`);
}

return false;
Expand All @@ -31,12 +31,12 @@ export const burn2fa = async (
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);

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
27 changes: 15 additions & 12 deletions src/2fa/create.ts
@@ -1,12 +1,13 @@
import Masa from "../masa";
import { BigNumber } from "ethers";

export const get2faTemplate = (phoneNumber: string) =>
`Phone Number: ${phoneNumber}`;
export const get2faTemplate = (phoneNumber: string, code: string) =>
`Phone Number: ${phoneNumber} Code: ${code}`;

export const create2fa = async (
masa: Masa,
phoneNumber: string
phoneNumber: string,
code: string
): Promise<
| {
tokenId: string | BigNumber;
Expand All @@ -21,7 +22,7 @@ export const create2fa = async (
const identityId = await masa.identity.load(address);
if (!identityId) return;

const msg = get2faTemplate(phoneNumber);
const msg = get2faTemplate(phoneNumber, code);

console.log(`Signer Address: '${address}'`);
console.log(`Signing: \n'${msg}'\n`);
Expand All @@ -30,25 +31,27 @@ export const create2fa = async (
const signature = await masa.config.wallet.signMessage(msg);
console.log(`Signature: '${signature}'`);

// 2. mint 2fa
console.log("\nCreating 2fa");
const storeMetadataData = await masa.twofa.mint(
// 2. mint 2FA
console.log("\nCreating 2FA");
const mint2FAData = await masa.twofa.mint(
address,
phoneNumber,
code,
signature
);

if (storeMetadataData) {
const { success, message, tokenId } = storeMetadataData;
if (mint2FAData) {
const { success, message, tokenId } = mint2FAData;

if (!success) {
console.error(`Creating 2fa failed! '${message}'`);
} else {
if (success) {
console.log("2FA Created", tokenId);
return {
tokenId,
success,
message,
};
} else {
console.error(`Creating 2FA failed! '${message}'`);
}
}
} else {
Expand Down
46 changes: 22 additions & 24 deletions src/2fa/list.ts
@@ -1,42 +1,41 @@
import { BigNumber } from "ethers";
import Masa from "../masa";
import { I2fa } from "../interface";
import { I2FA } from "../interface";
import { patchMetadataUrl } from "../helpers";

export const load2faByIdentityId = async (
export const load2fasByIdentityId = async (
masa: Masa,
twofaId: BigNumber
identityId: BigNumber
): Promise<
{
tokenId: BigNumber;
tokenUri: string;
metadata: I2fa;
metadata: I2FA;
}[]
> => {
const twofas = [];
const twoFAs = [];
const identityContracts = await masa.contracts.loadIdentityContracts();

const twofaIds: BigNumber[] = await identityContracts.SoulLinkerContract[
"getSBTLinks(uint256,address)"
](twofaId, identityContracts.Soulbound2FA.address);
](identityId, identityContracts.Soulbound2FA.address);

for (const tokenId of twofaIds) {
const tokenUri = patchMetadataUrl(
masa,
await identityContracts.Soulbound2FA.tokenURI(tokenId)
);

console.log(`Metadata Url: ${tokenUri}`);
const metadata = (await masa.metadata.retrieve(tokenUri)) as I2fa;
const metadata = (await masa.metadata.retrieve(tokenUri)) as I2FA;

twofas.push({
twoFAs.push({
tokenId,
tokenUri,
metadata,
});
}

return twofas;
return twoFAs;
};

export const list2fas = async (
Expand All @@ -46,26 +45,25 @@ export const list2fas = async (
| {
tokenId: BigNumber;
tokenUri: string;
metadata: I2fa;
metadata: I2FA;
}[]
| undefined
> => {
if (await masa.session.checkLogin()) {
address = address || (await masa.config.wallet.getAddress());

const identityId = await masa.identity.load(address);
if (!identityId) return;
address = address || (await masa.config.wallet.getAddress());

const twofas = await load2faByIdentityId(masa, identityId);
const identityId = await masa.identity.load(address);
if (!identityId) return;

if (twofas.length === 0) console.log("No 2fas found");
const twofas = await load2fasByIdentityId(masa, identityId);

for (const twofa of twofas) {
console.log(`Metadata: ${JSON.stringify(twofa.metadata, null, 2)}`);
}
if (twofas.length === 0) console.log("No 2FAs found");

return twofas;
} else {
console.log("Not logged in please login first");
let i = 1;
for (const twofa of twofas) {
console.log(`Token: ${i}`);
i++;
console.log(`Metadata: ${JSON.stringify(twofa.metadata, null, 2)}`);
}

return twofas;
};
24 changes: 9 additions & 15 deletions src/credit-score/list.ts
Expand Up @@ -52,24 +52,18 @@ export const listCreditReports = async (
}[]
| undefined
> => {
if (await masa.session.checkLogin()) {
address = address || (await masa.config.wallet.getAddress());
address = address || (await masa.config.wallet.getAddress());

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

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

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

for (const creditReport of creditReports) {
console.log(
`Metadata: ${JSON.stringify(creditReport.metadata, null, 2)}`
);
}

return creditReports;
} else {
console.log("Not logged in please login first");
for (const creditReport of creditReports) {
console.log(`Metadata: ${JSON.stringify(creditReport.metadata, null, 2)}`);
}

return creditReports;
};
4 changes: 2 additions & 2 deletions src/interface/2fa.ts
@@ -1,6 +1,6 @@
export interface I2fa {
export interface I2FA {
name: "Masa Soulbound 2FA v1.0.0";
description: "A decentralized 2fa";
description: "A decentralized 2FA";
image: "https://metadata.masa.finance/v1.0/2fa/2fa.png";
properties: {
tokenId: string;
Expand Down
60 changes: 26 additions & 34 deletions src/soul-names/list.ts
Expand Up @@ -51,39 +51,31 @@ export const loadSoulNamesByIdentityId = async (
return soulNameDetails;
};

export const listSoulnames = async (masa: Masa, address?: string) => {
if (await masa.session.checkLogin()) {
address = address || (await masa.config.wallet.getAddress());

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

const soulNames = await loadSoulNamesByIdentityId(masa, identityId);

for (const soulName of soulNames) {
console.log(`\nToken: ${parseInt(soulName.index) + 1}`);
console.log(`Name: ${soulName.tokenDetails.sbtName}`);
console.log(`Token ID: ${soulName.tokenDetails.tokenId.toNumber()}`);
console.log(
`Identity ID: ${soulName.tokenDetails.identityId.toNumber()}`
);
console.log(`Active: ${soulName.tokenDetails.active}`);
console.log(`Metadata Uri: ${soulName.tokenUri}`);
if (soulName.metadata)
console.log(`Metadata: ${JSON.stringify(soulName.metadata, null, 2)}`);

console.log(
`Expiry Date: ${new Date(
soulName.tokenDetails.expirationDate.toNumber() * 1000
).toUTCString()}`
);
}

return soulNames;
} else {
console.log("Not logged in please login first");

return [];
export const listSoulNames = async (masa: Masa, address?: string) => {
address = address || (await masa.config.wallet.getAddress());

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

const soulNames = await loadSoulNamesByIdentityId(masa, identityId);

for (const soulName of soulNames) {
console.log(`\nToken: ${parseInt(soulName.index) + 1}`);
console.log(`Name: ${soulName.tokenDetails.sbtName}`);
console.log(`Token ID: ${soulName.tokenDetails.tokenId.toNumber()}`);
console.log(`Identity ID: ${soulName.tokenDetails.identityId.toNumber()}`);
console.log(`Active: ${soulName.tokenDetails.active}`);
console.log(`Metadata Uri: ${soulName.tokenUri}`);
if (soulName.metadata)
console.log(`Metadata: ${JSON.stringify(soulName.metadata, null, 2)}`);

console.log(
`Expiry Date: ${new Date(
soulName.tokenDetails.expirationDate.toNumber() * 1000
).toUTCString()}`
);
}

return soulNames;
};
4 changes: 2 additions & 2 deletions src/soul-names/soul-names.ts
Expand Up @@ -3,15 +3,15 @@ import { PaymentMethod } from "../contracts";
import Masa from "../masa";
import { burnSoulName } from "./burn";
import { createSoulName, getRegistrationPrice } from "./create";
import { listSoulnames, loadSoulNamesByIdentityId } from "./list";
import { listSoulNames, loadSoulNamesByIdentityId } from "./list";

export const soulNames = (masa: Masa) => ({
getRegistrationPrice: (
soulName: string,
duration: number,
paymentMethod: PaymentMethod
) => getRegistrationPrice(masa, soulName, duration, paymentMethod),
list: (address?: string) => listSoulnames(masa, address),
list: (address?: string) => listSoulNames(masa, address),
loadSoulNamesByIdentityId: (identityId: BigNumber) =>
loadSoulNamesByIdentityId(masa, identityId),
create: (soulName: string, duration: number, paymentMethod: PaymentMethod) =>
Expand Down

0 comments on commit 75d343e

Please sign in to comment.