Add IDX as user identity provider

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
This commit is contained in:
Sebastian Martinez
2021-06-24 12:17:45 -03:00
parent 73a213f234
commit dcdec8c8e1
9 changed files with 771 additions and 21 deletions
+2
View File
@@ -8,6 +8,7 @@
import Registrations from '@app/base/registrations/Routes.svelte';
import Orgs from '@app/base/orgs/Routes.svelte';
import Users from '@app/base/users/Routes.svelte';
import IDX from "@app/base/idx/Routes.svelte";
import Projects from '@app/base/projects/Routes.svelte';
import Resolver from '@app/base/resolver/Routes.svelte';
import Header from '@app/Header.svelte';
@@ -65,6 +66,7 @@
<Registrations {config} session={$session} />
<Orgs {config} />
<Users {config} />
<IDX {config} />
<Projects {config} />
<Resolver {config} />
</Router>
+27
View File
@@ -0,0 +1,27 @@
<script lang="ts">
import { onMount } from 'svelte';
import type { Config } from '@app/config';
import { Caip10Link } from '@ceramicnetwork/stream-caip10-link';
import { EthereumAuthProvider } from '@ceramicnetwork/blockchain-utils-linking';
export let did: string;
export let caip10: string;
export let config: Config;
onMount(async () => {
if (config.signer) {
const address = await config.signer.getAddress();
const ethAuthProvider = new EthereumAuthProvider(
// TODO: Using the window.ethereum provider here since the config provider is throwing error.
window.ethereum, address
);
const accountLink = await Caip10Link.fromAccount(config.ceramic.client, caip10.toLowerCase());
await accountLink.setDid(
did,
ethAuthProvider
);
}
});
</script>
+11
View File
@@ -0,0 +1,11 @@
<script lang="ts">
import { Route } from "svelte-routing";
import Link from '@app/base/idx/Link.svelte';
import type { Config } from '@app/config';
export let config: Config;
</script>
<Route path="/idx/:did/link/:caip10" let:params>
<Link {config} did={params.did} caip10={params.caip10} />
</Route>
+1
View File
@@ -65,6 +65,7 @@
},
"tokens": []
},
"ceramic": { "api": "https://ceramic-clay.3boxlabs.com" },
"radicle": { "api": "" },
"abi": {
"registrar": [
+9
View File
@@ -1,6 +1,8 @@
import { ethers } from "ethers";
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
import SafeServiceClient from "@gnosis.pm/safe-service-client";
import CeramicClient from "@ceramicnetwork/http-client";
import { IDX } from "@ceramicstudio/idx";
import config from "@app/config.json";
declare global {
@@ -27,6 +29,8 @@ export class Config {
};
abi: { [contract: string]: string[] };
seed: { api?: string };
idx: { client: IDX };
ceramic: { client: CeramicClient };
tokens: string[];
token: ethers.Contract;
@@ -42,6 +46,9 @@ export class Config {
throw `Network ${network.name} is not supported`;
}
const ceramic = new CeramicClient(config.ceramic.api);
const idx = new IDX({ ceramic });
this.network = network;
this.seed = { api };
this.registrar = cfg.registrar;
@@ -56,6 +63,8 @@ export class Config {
this.signer = signer;
this.gasLimits = gasLimits;
this.abi = config.abi;
this.idx = { client: idx };
this.ceramic = { client: ceramic };
this.tokens = cfg.tokens;
this.token = new ethers.Contract(
this.radToken.address,
+11 -5
View File
@@ -36,7 +36,7 @@ export interface Store extends Readable<State> {
setTxSigning(): void;
setTxPending(tx: TransactionResponse): void;
setTxConfirmed(tx: TransactionReceipt): void;
setChangedAccount([address]: string[]): void;
setChangedAccount(address: string): void;
}
export const loadState = (initial: State): Store => {
@@ -154,7 +154,8 @@ export const loadState = (initial: State): Store => {
}
});
},
setChangedAccount: ([address]: string[]) => {
setChangedAccount: (address: string) => {
store.update(s => {
switch (s.connection) {
case Connection.Connected:
@@ -164,7 +165,7 @@ export const loadState = (initial: State): Store => {
disconnectWallet();
} else {
s.session.address = address;
window.localStorage.setItem("session", JSON.stringify({ address, tokenBalance: s.session.tokenBalance, tx: s.session.tx }));
window.localStorage.setItem("session", JSON.stringify({ ...s.session }));
}
return s;
default:
@@ -183,10 +184,15 @@ export const session = derived(state, s => {
return null;
});
window.ethereum?.on('chainChanged', () => location.reload());
window.ethereum?.on('chainChanged', () => {
// We disconnect the wallet to avoid out of sync state
// between the account address and IDX DIDs
disconnectWallet();
location.reload();
});
// Updates state when user changes accounts
window.ethereum?.on("accountsChanged", async (address: string[]) => {
window.ethereum?.on("accountsChanged", async ([address]: string) => {
const config = await getConfig();
state.setChangedAccount(address);
state.refreshBalance(config);
+49
View File
@@ -5,6 +5,14 @@ import multihashes from 'multihashes';
import EthersSafe from "@gnosis.pm/safe-core-sdk";
import type { Config } from '@app/config';
import { assert } from '@app/error';
import type { Registration } from "@app/base/registrations/registrar";
import { getRegistration } from '@app/base/registrations/registrar';
import type { BasicProfile } from "@ceramicstudio/idx-constants";
export interface Profile {
ens: Registration | null;
idx: BasicProfile | null;
}
export enum AddressType {
Contract,
@@ -34,6 +42,10 @@ export function formatBalance(n: BigNumber): string {
return ethers.utils.commify(parseFloat(ethers.utils.formatUnits(n)).toFixed(2));
}
export function formatCAIP10Address(address: string, protocol: string, impl: number): string {
return `${address.toLowerCase()}@${protocol}:${impl.toString()}`;
}
export function formatAddress(addr: string): string {
return formatHash(ethers.utils.getAddress(addr));
}
@@ -73,6 +85,11 @@ export function isUrl(input: string): boolean {
return /^https?:\/\//.test(input);
}
// Check whether the input is a DID
export function isDid(input: string): boolean {
return /^did:[a-zA-Z0-9]+:[a-zA-Z0-9]+$/.test(input);
}
// Check whether the input is an Ethereum address.
export function isAddress(input: string): boolean {
return ethers.utils.isAddress(input);
@@ -182,6 +199,38 @@ export async function resolveLabel(label: string, config: Config): Promise<strin
return config.provider.resolveName(`${label}.${config.registrar.domain}`);
}
export async function lookupAddress(address: string, config: Config): Promise<Profile> {
const profile: Profile = { ens: null, idx: null };
try {
const [ens, idx] = await Promise.allSettled([
resolveEnsProfile(address, config),
resolveIdxProfile(formatCAIP10Address(address, "eip155", config.network.chainId), config)
]);
if (ens.status == "fulfilled") profile.ens = ens.value;
if (idx.status == "fulfilled") profile.idx = idx.value;
} catch (error) {
console.error(error);
}
return profile;
}
// Resolves an IDX profile or return null
export async function resolveIdxProfile(caip10: string, config: Config): Promise<BasicProfile | null> {
return config.idx.client.get<BasicProfile>("basicProfile", caip10);
}
// Resolves an ENS profile or return null
export async function resolveEnsProfile(address: string, config: Config): Promise<Registration | null> {
const label = await config.provider.lookupAddress(address);
if (label && await resolveLabel(parseEnsLabel(label, config), config)) {
return await getRegistration(label, config);
}
return null;
}
// Check whether a Gnosis Safe exists at an address.
export async function isSafe(address: string, config: Config): Promise<boolean> {
if (! config.safe.api) return false;