Skip to content

Commit

Permalink
frontend: Add SIWE
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Jul 4, 2024
1 parent 63697e1 commit 8c01bad
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@metamask/detect-provider": "^2.0.0",
"@metamask/jazzicon": "^2.0.0",
"@oasisprotocol/demo-starter-backend": "workspace:^",
"@oasisprotocol/sapphire-paratime": "^1.3.2",
"@oasisprotocol/sapphire-paratime": "link:../../sapphire-paratime/clients/js",
"ethers": "^6.10.0",
"pinia": "^2.0.28",
"vue": "^3.2.45",
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export function useMessageBox(): ComputedRef<MessageBox | null> {
return null;
}

if (!eth.signer) {
console.error('[useMessageBox] Signer is not initialized');
if (!eth.provider) {
console.error('[useMessageBox] Provider is not initialized');
return null;
}

return MessageBox__factory.connect(addr, eth.signer as ContractRunner);
return MessageBox__factory.connect(addr, eth.unwrappedSigner as ContractRunner);
});
}

Expand Down
26 changes: 8 additions & 18 deletions frontend/src/stores/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import {
BrowserProvider,
type Eip1193Provider,
JsonRpcProvider,
JsonRpcSigner,
type Provider,
type Signer,
toBeHex,
toBeHex, toUtf8Bytes,
} from 'ethers';
import { defineStore } from 'pinia';
import { markRaw, type Raw, ref, shallowRef } from 'vue';
import { MetaMaskNotInstalledError } from '@/utils/errors';

export { Signature, toUtf8Bytes } from 'ethers';

export enum Network {
Unknown = 0,
Ethereum = 1,
Expand Down Expand Up @@ -61,7 +62,7 @@ export function networkName(network?: Network): string {

declare global {
interface Window {
ethereum: BrowserProvider & Eip1193Provider & sapphire.SapphireAnnex;
ethereum: BrowserProvider & sapphire.EIP2696_EthereumProvider;
}
}

Expand All @@ -77,7 +78,6 @@ export const useEthereumStore = defineStore('ethereum', () => {
}),
);

const signer = shallowRef<Signer | BrowserProvider | Raw<object>>();
const unwrappedSigner = shallowRef<Signer>();

const network = ref(Network.FromConfig);
Expand All @@ -102,20 +102,11 @@ export const useEthereumStore = defineStore('ethereum', () => {

const isSapphire = sapphire.NETWORKS[chainId];

let sapphireSigner: ReturnType<typeof sapphire.wrap>;

if (isSapphire) {
const signer = await browserProvider.getSigner();
sapphireSigner = sapphire.wrap<JsonRpcSigner>(signer);
}

signer.value = isSapphire ? markRaw(sapphireSigner!) : await browserProvider.getSigner();

unwrappedSigner.value = await browserProvider.getSigner(addr);
provider.value = isSapphire
? markRaw(sapphire.wrap(browserProvider.provider))
? markRaw(sapphire.wrapEthereumProvider(browserProvider.provider))
: browserProvider.provider;
unwrappedProvider.value = browserProvider.provider;
unwrappedSigner.value = await browserProvider.getSigner(addr);
network.value = chainId;
address.value = addr;
}
Expand All @@ -131,10 +122,10 @@ export const useEthereumStore = defineStore('ethereum', () => {
throw new Error('[useEthereumStore] Request account failed!');
}

await init(accounts[0], eth as unknown as Eip1193Provider);
await init(accounts[0], eth);

eth.on('accountsChanged', (accountsChanged: string[]) => {
init(accountsChanged[0], eth as unknown as Eip1193Provider);
init(accountsChanged[0], eth);
});
eth.on('chainChanged', () => {
window.location.reload();
Expand Down Expand Up @@ -219,7 +210,6 @@ export const useEthereumStore = defineStore('ethereum', () => {

return {
unwrappedSigner,
signer,
unwrappedProvider,
provider,
address,
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import { onMounted, ref } from 'vue';
import { useMessageBox, useUnwrappedMessageBox } from '../contracts';
import { Network, useEthereumStore } from '../stores/ethereum';
import { Network, Signature, toUtf8Bytes, useEthereumStore } from '../stores/ethereum';
import { abbrAddr } from '@/utils/utils';
import AppButton from '@/components/AppButton.vue';
import MessageLoader from '@/components/MessageLoader.vue';
import JazzIcon from '@/components/JazzIcon.vue';
import { retry } from '@/utils/promise';
import {toUtf8String} from "ethers";
const eth = useEthereumStore();
const messageBox = useMessageBox();
Expand All @@ -32,7 +33,9 @@ function handleError(error: Error, errorMessage: string) {
}
async function fetchMessage(): Promise<Message> {
const message = await messageBox.value!.message();
const siweMsg = toUtf8String(await messageBox.value!.getSiweMsg());
const auth = Signature.from(await eth.unwrappedSigner!.signMessage(siweMsg));
const message = await messageBox.value!.message(auth);
const author = await messageBox.value!.author();
return { message, author };
Expand Down

0 comments on commit 8c01bad

Please sign in to comment.