Merge pull request #31 from radicle-dev/cloudhead/safe-set-name

Get Safe integration working
This commit is contained in:
Alexis Sellier
2021-06-22 13:57:00 +02:00
committed by GitHub
14 changed files with 1050 additions and 53 deletions
+5 -1
View File
@@ -10,7 +10,11 @@
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="preload" href="/fonts/Inter-Regular.woff" as="font" type="font/woff">
<link rel="preload" href="/fonts/Inter-SemiBold.otf" as="font" type="font/otf">
<script type="text/javascript">
// Workaround to get `global` to work in old-school transitive
// dependencies.
window.global = window;
</script>
<title>Radicle Interface</title>
</head>
<body>
+852
View File
File diff suppressed because it is too large Load Diff
+5
View File
@@ -21,7 +21,12 @@
"vite-plugin-rewrite-all": "^0.1.2"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk": "^0.2.0",
"@gnosis.pm/safe-core-sdk-types": "^0.1.1",
"@gnosis.pm/safe-service-client": "^0.1.0-alpha.0",
"@stardazed/streams": "^3.1.0",
"@types/marked": "^2.0.3",
"buffer": "^6.0.3",
"ethereum-blockies": "^0.1.1",
"ethers": "^5.0.31",
"marked": "^2.0.7",
+2 -1
View File
@@ -37,7 +37,7 @@
<style>
.address {
display: flex;
display: inline-flex;
align-items: center;
height: 100%;
}
@@ -53,6 +53,7 @@
.loading {
margin-left: 1rem;
width: 4rem;
height: 1.5rem;
}
</style>
+2 -1
View File
@@ -11,6 +11,7 @@
export let message = "";
export let floating = false;
export let subtle = false;
export let action = floating ? "Close" : "Back";
let body = message || (error && error.message) || "";
</script>
@@ -32,7 +33,7 @@
<span slot="actions">
<button on:click={() => dispatch('close')}>
Back
{action}
</button>
</span>
</Modal>
+14 -6
View File
@@ -8,6 +8,12 @@
</script>
<style>
.container {
display: flex;
align-items: center;
height: 100%;
}
.spinner {
margin: auto 0;
width: 70px;
@@ -106,10 +112,12 @@
}
</style>
<div class="spinner" class:fade-in={fadeIn} class:small class:center class:margins class:condensed>
<div class="bounce1" style="background-color: var(--color-{color})"></div>
{#if !condensed}
<div class="bounce2" style="background-color: var(--color-{color})"></div>
<div class="bounce3" style="background-color: var(--color-{color})"></div>
{/if}
<div class="container">
<div class="spinner" class:fade-in={fadeIn} class:small class:center class:margins class:condensed>
<div class="bounce1" style="background-color: var(--color-{color})"></div>
{#if !condensed}
<div class="bounce2" style="background-color: var(--color-{color})"></div>
<div class="bounce3" style="background-color: var(--color-{color})"></div>
{/if}
</div>
</div>
+37
View File
@@ -1,6 +1,8 @@
import * as ethers from 'ethers';
import type { TransactionResponse } from '@ethersproject/providers';
import type { ContractReceipt } from '@ethersproject/contracts';
import { OperationType } from "@gnosis.pm/safe-core-sdk-types";
import { assert } from '@app/error';
import * as utils from '@app/utils';
import type { Config } from '@app/config';
@@ -74,6 +76,36 @@ export class Org {
{ gasLimit: 200_000 });
}
async setNameMultisig(name: string, config: Config): Promise<void> {
assert(config.signer);
assert(config.safe.client);
const safeAddress = ethers.utils.getAddress(this.owner);
const orgAddress = ethers.utils.getAddress(this.address);
const org = new ethers.Contract(
this.address,
config.abi.org,
config.signer
);
const unsignedTx = await org.populateTransaction.setName(
name,
config.provider.network.ensAddress,
);
const txData = unsignedTx.data;
if (! txData) {
throw new Error("Org::setNameMultisig: Could not generate transaction for `setName` call");
}
const safeTx = {
to: orgAddress,
value: ethers.constants.Zero.toString(),
data: txData,
operation: OperationType.Call,
};
await utils.proposeSafeTransaction(safeTx, safeAddress, config);
}
async setOwner(address: string, config: Config): Promise<TransactionResponse> {
assert(config.signer);
@@ -93,6 +125,11 @@ export class Org {
return [];
}
async isMember(address: string, config: Config): Promise<boolean> {
const members = await this.getMembers(config);
return members.includes(ethers.utils.getAddress(address));
}
async getProjects(config: Config): Promise<Array<Project>> {
const result = await utils.querySubgraph(
config.orgs.subgraph, GetProjects, { org: this.address }
+27 -13
View File
@@ -45,8 +45,14 @@
};
$: label = name && parseEnsLabel(name, config);
$: isOwner = (org: Org): boolean => {
return $session ? utils.isAddressEqual(org.owner, $session.address) : false;
$: isAuthorized = async (org: Org): Promise<boolean> => {
if ($session) {
if (utils.isAddressEqual(org.owner, $session.address)) {
return true;
}
return await org.isMember($session.address, config);
}
return false;
};
</script>
@@ -171,17 +177,21 @@
<div class="label">Owner</div>
<div><Address resolve {config} address={org.owner} /></div>
<div>
{#if isOwner(org)}
<button class="tiny secondary" on:click={transferOwnership}>
Transfer
</button>
{/if}
{#await isAuthorized(org)}
<!-- Loading -->
{:then authorized}
{#if authorized}
<button class="tiny secondary" on:click={transferOwnership}>
Transfer
</button>
{/if}
{/await}
</div>
<!-- Name -->
<div class="label">Name</div>
<div>
{#await org.lookupAddress(config)}
<Loading small />
<div class="loading"><Loading small /></div>
{:then name}
{#if name}
<Link to={`/registrations/${label}`}>{name}</Link>
@@ -191,11 +201,15 @@
{/await}
</div>
<div>
{#if isOwner(org)}
<button class="tiny secondary" on:click={setName}>
Set
</button>
{/if}
{#await isAuthorized(org)}
<!-- Loading -->
{:then authorized}
{#if authorized}
<button class="tiny secondary" on:click={setName}>
Set
</button>
{/if}
{/await}
</div>
</div>
+2 -2
View File
@@ -15,7 +15,7 @@
"contractHash": "0x5c34bb0755876de98e801805e6685456eea739ad0abba1cc450a7ee0f2a70b74"
},
"safe": {
"api": "https://safe-transaction.gnosis.io/api/v1",
"api": "https://safe-transaction.gnosis.io",
"subgraph": null,
"viewer": "https://gnosis-safe.io/app/#/safes"
},
@@ -59,7 +59,7 @@
"contractHash": "0x5c34bb0755876de98e801805e6685456eea739ad0abba1cc450a7ee0f2a70b74"
},
"safe": {
"api": "https://safe-transaction.rinkeby.gnosis.io/api/v1",
"api": "https://safe-transaction.rinkeby.gnosis.io",
"subgraph": "https://api.thegraph.com/subgraphs/name/radicle-dev/gnosis-safe-rinkeby",
"viewer": "https://rinkeby.gnosis-safe.io/app/#/safes"
},
+10 -2
View File
@@ -1,5 +1,6 @@
import { ethers } from "ethers";
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
import SafeServiceClient from "@gnosis.pm/safe-service-client";
import config from "@app/config.json";
declare global {
@@ -7,7 +8,6 @@ declare global {
ethereum: any;
registrarState: any;
}
}
export class Config {
@@ -19,7 +19,12 @@ export class Config {
gasLimits: { createOrg: number };
provider: ethers.providers.JsonRpcProvider;
signer: ethers.Signer & TypedDataSigner | null;
safe: { api: string | null; subgraph: string; viewer: string | null };
safe: {
api?: string;
client?: SafeServiceClient;
subgraph: string;
viewer: string | null;
};
abi: { [contract: string]: string[] };
seed: { api?: string };
tokens: string[];
@@ -44,6 +49,9 @@ export class Config {
this.orgFactory = cfg.orgFactory;
this.orgs = cfg.orgs;
this.safe = cfg.safe;
this.safe.client = this.safe.api
? new SafeServiceClient(this.safe.api)
: undefined;
this.provider = provider;
this.signer = signer;
this.gasLimits = gasLimits;
+43 -25
View File
@@ -7,6 +7,8 @@
import type { Org } from '@app/base/orgs/Org';
import Loading from '@app/Loading.svelte';
import Error from '@app/Error.svelte';
import Address from '@app/Address.svelte';
import * as utils from '@app/utils';
const dispatch = createEventDispatcher();
@@ -16,16 +18,23 @@
enum State {
Idle,
Checking,
// Single-sig states.
Signing,
Pending,
Success,
// Multi-sig states.
Proposing,
Proposed,
Failed,
Mismatch,
}
let name = "";
let state = State.Idle;
let error: string | null = null;
let mismatchError = false; // Set if the name entered does not resolve to the address.
const onSubmit = async () => {
state = State.Checking;
@@ -34,20 +43,25 @@
let resolved = await config.provider.resolveName(domain);
if (resolved && isAddressEqual(resolved, org.address)) {
state = State.Signing;
try {
let tx = await org.setName(domain, config);
state = State.Pending;
await tx.wait();
state = State.Success;
if (utils.isSafe(org.owner, config)) {
state = State.Proposing;
await org.setNameMultisig(domain, config);
state = State.Proposed;
} else {
state = State.Signing;
let tx = await org.setName(domain, config);
state = State.Pending;
await tx.wait();
state = State.Success;
}
} catch (e) {
console.error(e);
state = State.Failed;
error = e.message;
}
} else {
state = State.Idle;
mismatchError = true;
state = State.Mismatch;
}
};
</script>
@@ -69,31 +83,31 @@
</button>
</div>
</Modal>
{:else if mismatchError}
<Modal floating error>
{:else if state === State.Proposed}
<Modal floating>
<div slot="title">
👻
🪴
</div>
<div slot="body">
<div class="error">
The name <strong>{name}.{config.registrar.domain}</strong> does not
resolve to <strong>{formatAddress(org.address)}</strong>. Please update
The ENS record for {name}.{config.registrar.domain} to
point to the correct address and try again.
</div>
<div slot="subtitle">
<p>The transaction to set the ENS name for <strong>{formatAddress(org.address)}</strong>
to <strong>{name}.{config.registrar.domain}</strong> was proposed to:</p>
<p><Address address={org.owner} {config} compact /></p>
</div>
<div slot="actions">
<button class="secondary" on:click={() => mismatchError = false}>
Back
</button>
<button class="text" on:click={() => dispatch('close')}>
Cancel
<button class="small" on:click={() => dispatch('close')}>
Done
</button>
</div>
</Modal>
{:else if state === State.Mismatch}
<Error floating title="🖊️" action="Okay" on:close>
The name <strong>{name}.{config.registrar.domain}</strong> does not
resolve to <strong>{formatAddress(org.address)}</strong>. Please update
The ENS record for {name}.{config.registrar.domain} to
point to the correct address and try again.
</Error>
{:else if state === State.Failed && error}
<Error floating title="Transaction failed" message={error} on:close />
{:else}
@@ -107,6 +121,10 @@
Please confirm the transaction in your wallet.
{:else if state == State.Pending}
Transaction is being processed by the network...
{:else if state == State.Proposing}
Proposal is being submitted to the safe
<strong>{formatAddress(org.owner)}</strong>,
please sign the transaction in your wallet.
{:else}
Set an ENS name for <strong>{formatAddress(org.address)}</strong>
{/if}
@@ -114,7 +132,7 @@
<div slot="body">
{#if state === State.Idle || state === State.Checking}
<DomainInput root={config.registrar.domain} on:input={() => mismatchError = false}
<DomainInput root={config.registrar.domain}
autofocus disabled={state !== State.Idle} bind:value={name} />
{:else}
<Loading small center />
+7
View File
@@ -0,0 +1,7 @@
// This shim is used as a stand-in for the node "stream" library, used by
// the "keccack" package depended on by @gnosis.pm/safe-core-sdk.
import * as streams from '@stardazed/streams';
// "Transform" is the old name for "TransformStream".
export const Transform = streams.TransformStream;
export default streams;
+42 -2
View File
@@ -2,7 +2,9 @@ import { ethers } from "ethers";
import type { BigNumber } from "ethers";
import multibase from 'multibase';
import multihashes from 'multihashes';
import EthersSafe from "@gnosis.pm/safe-core-sdk";
import type { Config } from '@app/config';
import { assert } from '@app/error';
export enum AddressType {
Contract,
@@ -17,6 +19,13 @@ export interface Safe {
threshold: number;
}
export interface SafeTransaction {
to: string;
value: string;
data: string;
operation: number;
}
export function isAddressEqual(left: string, right: string): boolean {
return left.toLowerCase() === right.toLowerCase();
}
@@ -178,7 +187,7 @@ export async function isSafe(address: string, config: Config): Promise<boolean>
if (! config.safe.api) return false;
const addr = ethers.utils.getAddress(address);
const response = await fetch(`${config.safe.api}/safes/${addr}`, { method: 'HEAD' });
const response = await fetch(`${config.safe.api}/api/v1/safes/${addr}`, { method: 'HEAD' });
return response.ok;
}
@@ -188,7 +197,7 @@ export async function getSafe(address: string, config: Config): Promise<Safe | n
if (! config.safe.api) return null;
const addr = ethers.utils.getAddress(address);
const response = await fetch(`${config.safe.api}/safes/${addr}`, {
const response = await fetch(`${config.safe.api}/api/v1/safes/${addr}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
@@ -221,3 +230,34 @@ export async function getTokens(address: string, config: Config):
export function isMarkdownPath(path: string): boolean {
return /\.(md|mkd|markdown)$/i.test(path);
}
// Propose a Gnosis Safe multi-sig transaction.
export async function proposeSafeTransaction(
safeTx: SafeTransaction,
safeAddress: string,
config: Config
): Promise<void> {
assert(config.signer);
assert(config.safe.client);
const safeSdk = await EthersSafe.create({
ethers, safeAddress, providerOrSigner: config.signer,
});
const estimation = await config.safe.client.estimateSafeTransaction(
safeAddress,
safeTx
);
const transaction = await safeSdk.createTransaction({
...safeTx,
safeTxGas: Number(estimation.safeTxGas),
});
const safeTxHash = await safeSdk.getTransactionHash(transaction);
const signature = await safeSdk.signTransactionHash(safeTxHash);
await config.safe.client.proposeTransaction(
safeAddress,
transaction.data,
safeTxHash,
signature
);
}
+2
View File
@@ -11,6 +11,8 @@ const config: UserConfig = {
resolve: {
alias: {
"@app": path.resolve("./src"),
// Polyfill for Node.js "stream" library.
"stream": path.resolve("./src/polyfills/stream.ts"),
},
},
build: {