Skip to content

Commit

Permalink
feat(app): make passport network agnostic, passports are always on "m…
Browse files Browse the repository at this point in the history
…ainnet"

- creating did with network id 1, regardless of the network to which the user connected
- do not force use to switch to mainnet
  • Loading branch information
nutrina committed Jan 13, 2023
1 parent 6e928ea commit e06490c
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 42 deletions.
1 change: 1 addition & 0 deletions app/__mocks__/@didtools/pkh-ethereum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const EthereumWebAuth = { getAuthMethod: jest.fn() };
5 changes: 5 additions & 0 deletions app/__mocks__/did-session/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class DIDSession {
authorize() {
return "some-session-str";
}
}
39 changes: 34 additions & 5 deletions app/context/userContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { useViewerConnection } from "@self.id/framework";
import { datadogLogs } from "@datadog/browser-logs";
import { datadogRum } from "@datadog/browser-rum";

import { DIDSession } from "did-session";
import { EthereumWebAuth } from "@didtools/pkh-ethereum";
import { AccountId } from "caip";

export interface UserContextState {
loggedIn: boolean;
handleConnection: () => void;
Expand Down Expand Up @@ -110,7 +114,7 @@ export const UserContextProvider = ({ children }: { children: any }) => {
// check that passportLogin isnt mid-way through
if (wallet && !loggingIn) {
// ensure that passport is connected to mainnet
const hasCorrectChainId = await ensureMainnet();
const hasCorrectChainId = process.env.NEXT_PUBLIC_FF_MULTICHAIN_SIGNATURE === "on" ? true : await ensureMainnet();
// mark that we're attempting to login
setLoggingIn(true);
// with loaded chainId
Expand All @@ -128,19 +132,44 @@ export const UserContextProvider = ({ children }: { children: any }) => {
const sessionStr = localStorage.getItem(sessionKey);

// @ts-ignore
let selfId = await ceramicConnect(ethAuthProvider, sessionStr);
// When sessionStr is null, this will create a new selfId. We want to avoid this, becasue we want to make sure
// that chainId 1 is in the did
let selfId = !!sessionStr ? await ceramicConnect(ethAuthProvider, sessionStr) : null;

if (
// @ts-ignore
!selfId ||
// @ts-ignore
!selfId?.client?.session ||
// @ts-ignore
selfId?.client?.session?.isExpired ||
// @ts-ignore
selfId?.client?.session?.expireInSecs < 3600
) {
// If the session loaded is not valid, or if it is expired or close to expire, we create
// a new connection to ceramic
selfId = await ceramicConnect(ethAuthProvider);
if (process.env.NEXT_PUBLIC_FF_MULTICHAIN_SIGNATURE === "on") {
// If the session loaded is not valid, or if it is expired or close to expire, we create
// a new session
// Also we enforce the "1" chainId, as we always want to use mainnet dids, in order to avoid confusion
// as to where a passport / stamp has been stored
const authMethod = await EthereumWebAuth.getAuthMethod(
wallet.provider,
new AccountId({
chainId: "eip155:1",
address: address,
})
);
const session = await DIDSession.authorize(authMethod, {
expiresInSecs: 24 * 3600,
resources: ["ceramic://*"],
});
const newSessionStr = session.serialize();

selfId = await ceramicConnect(ethAuthProvider, newSessionStr);
} else {
// If the session loaded is not valid, or if it is expired or close to expire, we create
// a new connection to ceramic
selfId = await ceramicConnect(ethAuthProvider);
}
}

// Store the session in localstorage
Expand Down
5 changes: 3 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"@chakra-ui/react": "^1.8.8",
"@datadog/browser-logs": "^4.11.2",
"@datadog/browser-rum": "^4.11.2",
"@didtools/pkh-ethereum": "^0.0.3",
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@ethersproject/providers": "^5.6.4",
"@glazed/did-session": "^0.1.1",
"@gitcoin/passport-database-client": "^1.0.0",
"@gitcoin/passport-identity": "^1.0.0",
"@gitcoin/passport-types": "^1.0.0",
"@gitcoin/passport-platforms": "^1.0.0",
"@gitcoin/passport-types": "^1.0.0",
"@self.id/framework": "0.3.3",
"@web3-onboard/core": "^2.3.2",
"@web3-onboard/injected-wallets": "^2.0.12",
Expand All @@ -32,6 +32,7 @@
"@web3-onboard/walletconnect": "^2.0.5",
"@web3-onboard/walletlink": "^2.0.4",
"broadcast-channel": "^4.11.0",
"did-session": "^1.0.0",
"framer-motion": "^6.3.0",
"next": "latest",
"node-fetch": "^3.2.4",
Expand Down
33 changes: 25 additions & 8 deletions app/utils/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,34 @@ const walletConnect = walletConnectModule();
// Include ledger capabilities
const ledger = ledgerModule();

const chains = [
{
id: "0x1",
token: "ETH",
label: "Ethereum Mainnet",
rpcUrl: MAINNET_RPC_URL,
},
];

if (process.env.NEXT_PUBLIC_FF_MULTICHAIN_SIGNATURE === "on") {
chains.push({
id: "0x89",
token: "MATIC",
label: "Polygon Mainnet",
rpcUrl: "https://matic-mainnet.chainstacklabs.com",
});
chains.push({
id: "0xfa",
token: "FTM",
label: "Fantom Mainnet",
rpcUrl: "https://rpc.ftm.tools/",
});
}

// Exports onboard-core instance (https://github.com/blocknative/web3-onboard)
export const initWeb3Onboard = init({
wallets: [injected, ledger, walletLink, walletConnect],
chains: [
{
id: "0x1",
token: "ETH",
label: "Ethereum Mainnet",
rpcUrl: MAINNET_RPC_URL,
},
],
chains: chains,
appMetadata: {
name: "Passport",
icon: "/assets/gitcoinLogo.svg",
Expand Down
Loading

0 comments on commit e06490c

Please sign in to comment.