Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
629 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea | ||
node_modules | ||
/dist | ||
.idea/ | ||
node_modules/ | ||
dist/ | ||
|
||
yarn-error.log |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import SoulboundIdentity from '@masa-finance/masa-contracts-identity/deployments/goerli/SoulboundIdentity.json'; | ||
import SoulboundCreditReport from '@masa-finance/masa-contracts-identity/deployments/goerli/SoulboundCreditReport.json'; | ||
import SoulName from '@masa-finance/masa-contracts-identity/deployments/goerli/SoulName.json'; | ||
import SoulStore from '@masa-finance/masa-contracts-identity/deployments/goerli/SoulStore.json'; | ||
import SoulLinker from '@masa-finance/masa-contracts-identity/deployments/goerli/SoulLinker.json'; | ||
|
||
export { | ||
MASA_GOERLI as MASA, | ||
USDC_GOERLI as USDC, | ||
WETH_GOERLI as WETH, | ||
} from '@masa-finance/masa-contracts-identity/dist/src/constants'; | ||
|
||
export const SoulboundIdentityAddress = SoulboundIdentity.address; | ||
export const SoulboundCreditReportAddress = SoulboundCreditReport.address; | ||
export const SoulNameAddress = SoulName.address; | ||
export const SoulStoreAddress = SoulStore.address; | ||
export const SoulLinkerAddress = SoulLinker.address; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
SoulboundIdentity, | ||
SoulboundIdentity__factory, | ||
SoulboundCreditReport, | ||
SoulboundCreditReport__factory, | ||
SoulName, | ||
SoulName__factory, | ||
SoulLinker, | ||
SoulLinker__factory, | ||
SoulStore, | ||
SoulStore__factory, | ||
} from "@masa-finance/masa-contracts-identity"; | ||
import { ethers } from "ethers"; | ||
import * as goerli from "./goerli"; | ||
|
||
export interface Addresses { | ||
[index: string]: { | ||
MASA: string; | ||
USDC: string; | ||
WETH: string; | ||
SoulboundIdentityAddress: string; | ||
SoulboundCreditReportAddress: string; | ||
SoulNameAddress: string; | ||
SoulStoreAddress: string; | ||
SoulLinkerAddress: string; | ||
}; | ||
} | ||
|
||
export const addresses: Addresses = { | ||
goerli, | ||
}; | ||
|
||
export interface IIdentityContracts { | ||
SoulboundIdentityContract: SoulboundIdentity; | ||
SoulboundCreditReportContract: SoulboundCreditReport; | ||
SoulNameContract: SoulName; | ||
SoulLinkerContract: SoulLinker; | ||
SoulStoreContract: SoulStore; | ||
} | ||
|
||
export interface LoadContractArgs { | ||
provider?: any; | ||
network?: string; | ||
} | ||
|
||
export const loadIdentityContracts = async ({ | ||
provider, | ||
network = "goerli", | ||
}: LoadContractArgs): Promise<IIdentityContracts> => { | ||
const p = | ||
// take provider as is if supplied | ||
provider || | ||
// or try to load from the browser | ||
new ethers.providers.Web3Provider( | ||
// @ts-ignore | ||
window.ethereum | ||
); | ||
|
||
const SoulboundIdentityContract = SoulboundIdentity__factory.connect( | ||
addresses[network].SoulboundIdentityAddress, | ||
p | ||
); | ||
|
||
const SoulboundCreditReportContract = SoulboundCreditReport__factory.connect( | ||
addresses[network].SoulboundCreditReportAddress, | ||
p | ||
); | ||
|
||
const SoulNameContract = SoulName__factory.connect( | ||
addresses[network].SoulNameAddress, | ||
p | ||
); | ||
|
||
const SoulLinkerContract = SoulLinker__factory.connect( | ||
addresses[network].SoulLinkerAddress, | ||
p | ||
); | ||
|
||
const SoulStoreContract = SoulStore__factory.connect( | ||
addresses[network].SoulStoreAddress, | ||
p | ||
); | ||
|
||
return { | ||
SoulboundIdentityContract, | ||
SoulboundCreditReportContract, | ||
SoulNameContract, | ||
SoulLinkerContract, | ||
SoulStoreContract, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Masa from "../masa" | ||
|
||
export const patchMetadataUrl = (masa: Masa, tokeUri: string) => { | ||
const apiUrl: string = masa.config.apiUrl; | ||
const env: string = masa.config.environment; | ||
|
||
if (tokeUri.indexOf("beta") > -1) { | ||
if (apiUrl.indexOf("localhost") > -1 || apiUrl.indexOf("127.0.0.1") > -1) { | ||
return tokeUri.replace("https://beta.metadata.masa.finance/", apiUrl); | ||
} else { | ||
return tokeUri.replace("beta", env); | ||
} | ||
} | ||
return tokeUri; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const unpackSessionId = (cookie?: string): string | undefined => { | ||
if (!cookie) return; | ||
|
||
return cookie?.split(";")[0].split("=")[1]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { version as contractsVersion } from "@masa-finance/masa-contracts-identity/package.json"; | ||
import { version as sdkVersion } from "../../package.json"; | ||
|
||
export const version = () => { | ||
return { | ||
contractsVersion, | ||
sdkVersion, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { loadIdentityContracts } from "../contracts"; | ||
import Masa from "../masa" | ||
|
||
export const loadIdentity = async (masa: Masa, address: string) => { | ||
const identityContracts = await loadIdentityContracts({ | ||
provider: masa.config.provider, | ||
network: masa.config.network, | ||
}); | ||
|
||
let identityId; | ||
|
||
try { | ||
identityId = await identityContracts.SoulboundIdentityContract.tokenOfOwner( | ||
address | ||
); | ||
} catch { | ||
// ignore | ||
} | ||
|
||
if (!identityId) { | ||
console.error(`No identity for '${address}'!`); | ||
} | ||
|
||
return identityId; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export {sayHello, sayGoodbye} from './hello-world' | ||
|
||
export * from "./masa"; | ||
import { | ||
addresses, | ||
loadIdentityContracts, | ||
IIdentityContracts, | ||
} from "./contracts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ethers } from "ethers"; | ||
import { logout } from "./session/logout"; | ||
import { checkLogin } from "./session/check-login"; | ||
import { loadIdentity } from "./identity/load-identity"; | ||
import { version } from "./helpers/version"; | ||
import { login } from "./session/login"; | ||
import { addresses, loadIdentityContracts } from "./contracts"; | ||
import MasaClient from "./utils/clients/middleware"; | ||
|
||
export default class Masa { | ||
public readonly client: MasaClient; | ||
|
||
constructor({ | ||
provider, | ||
apiUrl, | ||
}: { | ||
provider: ethers.providers.JsonRpcProvider; | ||
apiUrl: string; | ||
}) { | ||
this.client = new MasaClient({ apiUrl }); | ||
|
||
this.config.apiUrl = apiUrl; | ||
this.config.provider = provider; | ||
} | ||
|
||
public config: { | ||
cookie?: string; | ||
apiUrl: string; | ||
environment: string; | ||
network: string; | ||
provider?: ethers.providers.JsonRpcProvider; | ||
} = { | ||
apiUrl: "https://dev.middleware.masa.finance", | ||
environment: "dev", | ||
network: "goerli", | ||
}; | ||
|
||
session = { | ||
checkLogin: () => checkLogin(this), | ||
sessionLogout: (cookie?: string) => this.client.sessionLogout(cookie), | ||
login, | ||
logout, | ||
}; | ||
|
||
identity = { | ||
loadIdentity, | ||
}; | ||
|
||
creditScore = { | ||
creditScoreMint: (address: string, signature: string) => | ||
this.client.creditScoreMint(address, signature, this.config.cookie), | ||
}; | ||
|
||
utils = { | ||
version, | ||
}; | ||
|
||
contracts = { | ||
loadIdentityContracts, | ||
addresses, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Masa from "../masa"; | ||
import { unpackSessionId } from "../helpers/unpack-session-id"; | ||
|
||
export const checkLogin = async (masa: Masa): Promise<boolean> => { | ||
let loggedIn = false; | ||
|
||
const checkSessionResponse = await masa.client.sessionCheck(); | ||
if (checkSessionResponse) { | ||
console.log(`User ID: '${checkSessionResponse.user.userId}'`); | ||
console.log(`Session ID: '${unpackSessionId(masa.config.cookie)}'`); | ||
console.log( | ||
`Signer Address: '${await masa.config.provider | ||
?.getSigner() | ||
.getAddress()}'` | ||
); | ||
console.log("\n"); | ||
|
||
loggedIn = true; | ||
} | ||
|
||
return loggedIn; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const getLoginTemplate = ( | ||
challenge: string, | ||
expires: string | ||
) => `Welcome to 🌽Masa Finance! | ||
Login with your soulbound web3 identity to unleash the power of DeFi. | ||
Your signature is valid till: ${expires}. | ||
Challenge: ${challenge}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { getLoginTemplate } from "./get-logintemplate"; | ||
import Masa from "../masa"; | ||
import { unpackSessionId } from "../helpers/unpack-session-id" | ||
|
||
export const login = async (masa: Masa) => { | ||
console.log("Logging in"); | ||
|
||
if (await masa.session.checkLogin()) { | ||
console.log("Already logged in! Please logout before logging in again."); | ||
} else { | ||
// get challenge | ||
const challengeData = await masa.client.getChallenge(); | ||
|
||
if (challengeData) { | ||
// sign | ||
const msg = getLoginTemplate( | ||
challengeData.challenge, | ||
challengeData.expires | ||
); | ||
|
||
const signer = await masa.config.provider?.getSigner(); | ||
if (!signer) return; | ||
|
||
const address = await signer.getAddress(); | ||
|
||
console.log(`Signer Address: '${address}'`); | ||
console.log(`Signing: \n'${msg}'\n`); | ||
|
||
const signature = await signer.signMessage(msg); | ||
console.log(`Signature: '${signature}'`); | ||
|
||
const checkSignatureData = await masa.client.checkSignature( | ||
address, | ||
signature, | ||
masa.config.cookie | ||
); | ||
|
||
if (checkSignatureData) { | ||
console.log("\nLogged in as:"); | ||
console.log(`Address: '${address}'`); | ||
console.log(`User ID: '${checkSignatureData.id}'`); | ||
|
||
if (challengeData.cookie) { | ||
console.log(`Session ID: '${unpackSessionId(challengeData.cookie)}'`); | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Masa from "../masa"; | ||
|
||
export const logout = async (masa: Masa) => { | ||
console.log("Logging out"); | ||
|
||
if (await masa.session.checkLogin()) { | ||
const logoutData = await masa.session.sessionLogout(); | ||
|
||
if (logoutData) { | ||
console.log(`Logout: ${logoutData.status}`); | ||
} | ||
} else { | ||
console.log("Not logged in please login first"); | ||
} | ||
}; |
Oops, something went wrong.