Skip to content

Commit 0c52c22

Browse files
authored
feat: setup hw crypto (#295)
* feat: setup hw crypto * feat: signing logic * feat: run formatter
1 parent b0c19d1 commit 0c52c22

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

infrastructure/eid-wallet/src/routes/(app)/sign/+page.svelte

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<script lang="ts">
22
import { goto } from "$app/navigation";
3-
import { onMount, getContext } from "svelte";
43
import AppNav from "$lib/fragments/AppNav/AppNav.svelte";
54
import type { GlobalState } from "$lib/global";
65
import { Drawer } from "$lib/ui";
76
import * as Button from "$lib/ui/Button";
7+
import { exists, signPayload } from "@auvo/tauri-plugin-crypto-hw-api";
8+
import { getContext, onMount } from "svelte";
89
910
const globalState = getContext<() => GlobalState>("globalState")();
1011
@@ -15,7 +16,14 @@ interface SigningData {
1516
}
1617
1718
let signingData: SigningData | null = $state(null);
18-
let decodedData: any = $state(null);
19+
let decodedData: {
20+
pollId: string;
21+
voteData: {
22+
optionId?: number;
23+
ranks?: Record<string, number>;
24+
};
25+
userId: string;
26+
} | null = $state(null);
1927
let signingStatus: "pending" | "signing" | "success" | "error" =
2028
$state("pending");
2129
let errorMessage = $state("");
@@ -71,14 +79,35 @@ async function handleSign() {
7179
// For now, we'll simulate the signing process
7280
await new Promise((resolve) => setTimeout(resolve, 2000)); // Simulate signing delay
7381
82+
// check if default key pair exists
83+
const keyExists = exists("default");
84+
85+
if (!keyExists) {
86+
// this would only indicate that it is an old evault/wallet
87+
// ask them to delete and make a new one maybe or some fallback
88+
// behaviour if we need it
89+
throw new Error("Default key pair does not exist");
90+
}
91+
7492
// Create the signed payload
75-
const signedPayload = {
93+
const signedPayload: {
94+
sessionId: string;
95+
publicKey: string; // Use eName as public key for now
96+
message: string;
97+
signature?: string;
98+
} = {
7699
sessionId: signingData.session,
77-
signature: "simulated_signature_" + Date.now(), // In real implementation, this would be the actual signature
78100
publicKey: vault.ename, // Use eName as public key for now
79101
message: messageToSign,
80102
};
81103
104+
const signature = await signPayload(
105+
"default",
106+
JSON.stringify(signedPayload),
107+
);
108+
109+
signedPayload.signature = signature;
110+
82111
// Send the signed payload to the redirect URI
83112
const response = await fetch(signingData.redirect_uri, {
84113
method: "POST",

infrastructure/eid-wallet/src/routes/(auth)/onboarding/+page.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { GlobalState } from "$lib/global";
99
import { ButtonAction, Drawer } from "$lib/ui";
1010
import { capitalize } from "$lib/utils";
1111
import {
12+
exists,
1213
generate,
1314
getPublicKey,
1415
// signPayload, verifySignature
@@ -86,10 +87,17 @@ let uri: string;
8687
8788
let error: string | null = $state(null);
8889
89-
onMount(() => {
90+
onMount(async () => {
9091
globalState = getContext<() => GlobalState>("globalState")();
9192
// handle verification logic + sec user data in the store
9293
94+
// check if default keypair exists
95+
const keyExists = await exists("default");
96+
if (!keyExists) {
97+
// if not, generate it
98+
await generateApplicationKeyPair();
99+
}
100+
93101
handleContinue = async () => {
94102
loading = true;
95103
const {
@@ -103,6 +111,7 @@ onMount(() => {
103111
registryEntropy,
104112
namespace: uuidv4(),
105113
verificationId,
114+
publicKey: await getApplicationPublicKey(),
106115
})
107116
.catch(() => {
108117
loading = false;

infrastructure/eid-wallet/src/routes/(auth)/verify/+page.svelte

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { GlobalState } from "$lib/global";
99
import { ButtonAction } from "$lib/ui";
1010
import Drawer from "$lib/ui/Drawer/Drawer.svelte";
1111
import { capitalize } from "$lib/utils";
12+
import {
13+
exists,
14+
generate,
15+
getPublicKey,
16+
// signPayload, verifySignature
17+
} from "@auvo/tauri-plugin-crypto-hw-api";
1218
import axios from "axios";
1319
import { getContext, onMount } from "svelte";
1420
import { Shadow } from "svelte-loading-spinners";
@@ -135,12 +141,43 @@ function watchEventStream(id: string) {
135141
};
136142
}
137143
144+
// IMO, call this function early, check if hardware even supports the app
145+
// docs: https://github.com/auvoid/tauri-plugin-crypto-hw/blob/48d0b9db7083f9819766e7b3bfd19e39de9a77f3/examples/tauri-app/src/App.svelte#L13
146+
async function generateApplicationKeyPair() {
147+
let res: string | undefined;
148+
try {
149+
res = await generate("default");
150+
console.log(res);
151+
} catch (e) {
152+
// Put hardware crypto missing error here
153+
console.log(e);
154+
}
155+
return res;
156+
}
157+
158+
async function getApplicationPublicKey() {
159+
let res: string | undefined;
160+
try {
161+
res = await getPublicKey("default");
162+
console.log(res);
163+
} catch (e) {
164+
console.log(e);
165+
}
166+
return res; // check getPublicKey doc comments (multibase hex format)
167+
}
168+
138169
let handleContinue: () => Promise<void>;
139170
140171
onMount(() => {
141172
globalState = getContext<() => GlobalState>("globalState")();
142173
// handle verification logic + sec user data in the store
143174
175+
// check if default key pair exists
176+
const keyExists = exists("default");
177+
if (!keyExists) {
178+
generateApplicationKeyPair();
179+
}
180+
144181
handleContinue = async () => {
145182
if ($status !== "approved") return verifStep.set(0);
146183
if (!globalState) throw new Error("Global state is not defined");
@@ -171,6 +208,7 @@ onMount(() => {
171208
registryEntropy,
172209
namespace: uuidv4(),
173210
verificationId: $verificaitonId,
211+
publicKey: await getApplicationPublicKey(),
174212
},
175213
);
176214
if (data.success === true) {

0 commit comments

Comments
 (0)