Refactoring and cleanup of Signer
This commit is contained in:
parent
e2a3c680d1
commit
370ff822d0
@ -8,23 +8,19 @@
|
|||||||
import Icon from "@iconify/svelte";
|
import Icon from "@iconify/svelte";
|
||||||
import { createLitSession } from "./createLitSession";
|
import { createLitSession } from "./createLitSession";
|
||||||
import { connectProvider } from "./setupLit";
|
import { connectProvider } from "./setupLit";
|
||||||
import { ethers } from "ethers";
|
|
||||||
import Signer from "./Signer.svelte";
|
|
||||||
|
|
||||||
const redirectUri = "http://localhost:3000/";
|
const redirectUri = "http://localhost:3000/";
|
||||||
|
|
||||||
let sessionSigs = null;
|
let sessionSigs = null;
|
||||||
let litNodeClient, error, currentPKP, authMethod, provider;
|
let litNodeClient, error, currentPKP, authMethod, provider;
|
||||||
let messageToSign = { user: "Sam", loggedIn: true };
|
|
||||||
let status = "Initializing...";
|
let status = "Initializing...";
|
||||||
let jsonObjectToVerify = null;
|
|
||||||
let pkps: IRelayPKP[] = [];
|
let pkps: IRelayPKP[] = [];
|
||||||
let view = "SIGN_IN";
|
let view = "SIGN_IN";
|
||||||
let sessionSigsObject;
|
let sessionSigsObject;
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
litNodeClient = new LitNodeClient({ litNetwork: "serrano" });
|
// litNodeClient = new LitNodeClient({ litNetwork: "serrano" });
|
||||||
await litNodeClient.connect();
|
// await litNodeClient.connect();
|
||||||
const sessionSigsLocalStorage = localStorage.getItem("google-signature");
|
const sessionSigsLocalStorage = localStorage.getItem("google-signature");
|
||||||
const currentPKPLocalStorage = localStorage.getItem("current-pkp");
|
const currentPKPLocalStorage = localStorage.getItem("current-pkp");
|
||||||
if (sessionSigsLocalStorage && currentPKPLocalStorage) {
|
if (sessionSigsLocalStorage && currentPKPLocalStorage) {
|
||||||
@ -33,7 +29,6 @@
|
|||||||
} else {
|
} else {
|
||||||
initialize();
|
initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sessionSigsLocalStorage) {
|
if (sessionSigsLocalStorage) {
|
||||||
sessionSigsObject = JSON.parse(sessionSigsLocalStorage);
|
sessionSigsObject = JSON.parse(sessionSigsLocalStorage);
|
||||||
}
|
}
|
||||||
@ -118,66 +113,6 @@
|
|||||||
view = "ERROR";
|
view = "ERROR";
|
||||||
status = `Error: ${err.message}`;
|
status = `Error: ${err.message}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function signMessageWithPKP() {
|
|
||||||
try {
|
|
||||||
// Create a specific JSON object
|
|
||||||
const jsonString = JSON.stringify(messageToSign);
|
|
||||||
|
|
||||||
// Convert the JSON string to an array of character codes
|
|
||||||
const toSign = ethers.getBytes(ethers.hashMessage(jsonString));
|
|
||||||
|
|
||||||
const litActionCode = `
|
|
||||||
const go = async () => {
|
|
||||||
const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName });
|
|
||||||
};
|
|
||||||
go();
|
|
||||||
`;
|
|
||||||
|
|
||||||
// Sign message
|
|
||||||
const results = await litNodeClient.executeJs({
|
|
||||||
code: litActionCode,
|
|
||||||
sessionSigs: sessionSigs,
|
|
||||||
jsParams: {
|
|
||||||
toSign: toSign,
|
|
||||||
publicKey: currentPKP.publicKey,
|
|
||||||
sigName: "sig1",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get signature
|
|
||||||
const result = results.signatures["sig1"];
|
|
||||||
const signature = ethers.Signature.from({
|
|
||||||
r: "0x" + result.r,
|
|
||||||
s: "0x" + result.s,
|
|
||||||
v: result.recid,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add the signature to the JSON object
|
|
||||||
messageToSign.signature = signature;
|
|
||||||
|
|
||||||
jsonObjectToVerify = { ...messageToSign };
|
|
||||||
|
|
||||||
// Display the signed JSON
|
|
||||||
status = JSON.stringify(messageToSign, null, 2);
|
|
||||||
|
|
||||||
// Verify the signature
|
|
||||||
const recoveredAddr = ethers.verifyMessage(jsonString, signature);
|
|
||||||
|
|
||||||
// Check if the address associated with the signature is the same as the current PKP
|
|
||||||
const verified =
|
|
||||||
currentPKP.ethAddress.toLowerCase() === recoveredAddr.toLowerCase();
|
|
||||||
|
|
||||||
if (verified) {
|
|
||||||
status = "The signature is valid.";
|
|
||||||
} else {
|
|
||||||
status = "The signature is invalid.";
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
setError(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex items-center justify-center h-screen">
|
<div class="flex items-center justify-center h-screen">
|
||||||
@ -197,21 +132,13 @@
|
|||||||
<div>
|
<div>
|
||||||
<h3>Your PKP Address:</h3>
|
<h3>Your PKP Address:</h3>
|
||||||
<p>{currentPKP.ethAddress}</p>
|
<p>{currentPKP.ethAddress}</p>
|
||||||
<h1>Ready to sign</h1>
|
|
||||||
<Signer
|
|
||||||
{litNodeClient}
|
|
||||||
{currentPKP}
|
|
||||||
{sessionSigs}
|
|
||||||
on:status={(e) => (status = e.detail)}
|
|
||||||
on:error={(e) => setError(e.detail)}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<div class="mt-4 text-center">
|
<div class="mt-4 text-center">
|
||||||
<h1>Status</h1>
|
<h1>Status</h1>
|
||||||
<p>{status}</p>
|
<p>{status}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-4 text-center">
|
<!-- <div class="mt-4 text-center">
|
||||||
Session Signature
|
Session Signature
|
||||||
{#if sessionSigsObject}
|
{#if sessionSigsObject}
|
||||||
<div class="mt-4 text-center">
|
<div class="mt-4 text-center">
|
||||||
@ -229,27 +156,6 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div> -->
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
|
||||||
.container {
|
|
||||||
/* ...existing styles... */
|
|
||||||
}
|
|
||||||
|
|
||||||
.session-sig {
|
|
||||||
border: 1px solid #ddd;
|
|
||||||
padding: 1em;
|
|
||||||
margin-bottom: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.session-sig h2 {
|
|
||||||
color: #333;
|
|
||||||
}
|
|
||||||
|
|
||||||
.session-sig p,
|
|
||||||
.session-sig pre {
|
|
||||||
color: #666;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
6
src/lib/IProvider.ts
Normal file
6
src/lib/IProvider.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export interface IProvider {
|
||||||
|
mintPKPThroughRelayer(authMethod: any): Promise<string>;
|
||||||
|
relay: {
|
||||||
|
pollRequestUntilTerminalState(txHash: string): Promise<any>;
|
||||||
|
};
|
||||||
|
}
|
@ -1,19 +1,29 @@
|
|||||||
<!-- SignVerifyMessage.svelte -->
|
<!-- SignVerifyMessage.svelte -->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ethers } from "ethers";
|
import { ethers } from "ethers";
|
||||||
import { createEventDispatcher } from "svelte";
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
export let litNodeClient;
|
export let messageToSign = {};
|
||||||
export let currentPKP;
|
|
||||||
export let sessionSigs;
|
|
||||||
export let messageToSign = { user: "Sam", loggedIn: true };
|
|
||||||
|
|
||||||
|
let currentPKP;
|
||||||
|
let sessionSigs;
|
||||||
let status = "";
|
let status = "";
|
||||||
let jsonObjectToVerify = null;
|
let litNodeClient;
|
||||||
|
let messageSignature;
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
onMount(async () => {
|
||||||
|
litNodeClient = new LitNodeClient({ litNetwork: "serrano" });
|
||||||
|
await litNodeClient.connect();
|
||||||
|
|
||||||
async function signMessageWithPKP() {
|
const sessionSigsLocalStorage = localStorage.getItem("google-signature");
|
||||||
|
const currentPKPLocalStorage = localStorage.getItem("current-pkp");
|
||||||
|
if (sessionSigsLocalStorage && currentPKPLocalStorage) {
|
||||||
|
sessionSigs = JSON.parse(sessionSigsLocalStorage);
|
||||||
|
currentPKP = JSON.parse(currentPKPLocalStorage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function signMessageWithPKP() {
|
||||||
const userConfirmed = window.confirm(
|
const userConfirmed = window.confirm(
|
||||||
"Do you want to sign the following message?\n\n" +
|
"Do you want to sign the following message?\n\n" +
|
||||||
JSON.stringify(messageToSign, null, 2)
|
JSON.stringify(messageToSign, null, 2)
|
||||||
@ -32,11 +42,11 @@
|
|||||||
const toSign = ethers.getBytes(ethers.hashMessage(jsonString));
|
const toSign = ethers.getBytes(ethers.hashMessage(jsonString));
|
||||||
|
|
||||||
const litActionCode = `
|
const litActionCode = `
|
||||||
const go = async () => {
|
const go = async () => {
|
||||||
const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName });
|
const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName });
|
||||||
};
|
};
|
||||||
go();
|
go();
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Sign message
|
// Sign message
|
||||||
const results = await litNodeClient.executeJs({
|
const results = await litNodeClient.executeJs({
|
||||||
@ -51,22 +61,17 @@
|
|||||||
|
|
||||||
// Get signature
|
// Get signature
|
||||||
const result = results.signatures["sig1"];
|
const result = results.signatures["sig1"];
|
||||||
const signature = ethers.Signature.from({
|
messageSignature = ethers.Signature.from({
|
||||||
r: "0x" + result.r,
|
r: "0x" + result.r,
|
||||||
s: "0x" + result.s,
|
s: "0x" + result.s,
|
||||||
v: result.recid,
|
v: result.recid,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add the signature to the JSON object
|
|
||||||
messageToSign.signature = signature;
|
|
||||||
|
|
||||||
jsonObjectToVerify = { ...messageToSign };
|
|
||||||
|
|
||||||
// Display the signed JSON
|
// Display the signed JSON
|
||||||
status = JSON.stringify(messageToSign, null, 2);
|
status = JSON.stringify(messageToSign, null, 2);
|
||||||
|
|
||||||
// Verify the signature
|
// Verify the signature
|
||||||
const recoveredAddr = ethers.verifyMessage(jsonString, signature);
|
const recoveredAddr = ethers.verifyMessage(jsonString, messageSignature);
|
||||||
|
|
||||||
// Check if the address associated with the signature is the same as the current PKP
|
// Check if the address associated with the signature is the same as the current PKP
|
||||||
const verified =
|
const verified =
|
||||||
@ -77,11 +82,8 @@
|
|||||||
} else {
|
} else {
|
||||||
status = "The signature is invalid.";
|
status = "The signature is invalid.";
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch("status", status);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
dispatch("error", err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@ -90,3 +92,15 @@
|
|||||||
{#if messageToSign}
|
{#if messageToSign}
|
||||||
<pre>{JSON.stringify(messageToSign)}</pre>
|
<pre>{JSON.stringify(messageToSign)}</pre>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if status}
|
||||||
|
<div class="mt-4 text-center">
|
||||||
|
<p>Status: {status}</p>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
{#if messageSignature}
|
||||||
|
<div class="mt-4 text-center">
|
||||||
|
<h3>Signature</h3>
|
||||||
|
<pre>{JSON.stringify(messageSignature)}</pre>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
<script>
|
<script>
|
||||||
import GoogleAuth from "$lib/GoogleAuth.svelte";
|
import GoogleAuth from "$lib/GoogleAuth.svelte";
|
||||||
|
import Signer from "$lib/Signer.svelte";
|
||||||
|
|
||||||
|
let messageToSign = { user: "Sam", loggedIn: true };
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<GoogleAuth />
|
<GoogleAuth />
|
||||||
|
<Signer {messageToSign} />
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
// src/routes/api/auth/+server.ts
|
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user