Skip to content

Commit d5c0224

Browse files
committed
feat: setup hw crypto
1 parent 1362ede commit d5c0224

File tree

2 files changed

+369
-313
lines changed

2 files changed

+369
-313
lines changed

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

Lines changed: 151 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,175 @@
11
<script lang="ts">
2-
import { goto } from "$app/navigation";
3-
import {
4-
PUBLIC_PROVISIONER_URL,
5-
PUBLIC_REGISTRY_URL,
6-
} from "$env/static/public";
7-
import { Hero } from "$lib/fragments";
8-
import { GlobalState } from "$lib/global";
9-
import { ButtonAction, Drawer } from "$lib/ui";
10-
import { capitalize } from "$lib/utils";
11-
import {
12-
generate,
13-
getPublicKey,
14-
// signPayload, verifySignature
15-
} from "@auvo/tauri-plugin-crypto-hw-api";
16-
import * as falso from "@ngneat/falso";
17-
import axios from "axios";
18-
import { getContext, onMount } from "svelte";
19-
import { Shadow } from "svelte-loading-spinners";
20-
import { v4 as uuidv4 } from "uuid";
2+
import { goto } from "$app/navigation";
3+
import {
4+
PUBLIC_PROVISIONER_URL,
5+
PUBLIC_REGISTRY_URL,
6+
} from "$env/static/public";
7+
import { Hero } from "$lib/fragments";
8+
import { GlobalState } from "$lib/global";
9+
import { ButtonAction, Drawer } from "$lib/ui";
10+
import { capitalize } from "$lib/utils";
11+
import {
12+
exists,
13+
generate,
14+
getPublicKey,
15+
// signPayload, verifySignature
16+
} from "@auvo/tauri-plugin-crypto-hw-api";
17+
import * as falso from "@ngneat/falso";
18+
import axios from "axios";
19+
import { getContext, onMount } from "svelte";
20+
import { Shadow } from "svelte-loading-spinners";
21+
import { v4 as uuidv4 } from "uuid";
2122
22-
let isPaneOpen = $state(false);
23-
let preVerified = $state(false);
24-
let loading = $state(false);
25-
let verificationId = $state("");
26-
let demoName = $state("");
27-
let verificationSuccess = $state(false);
23+
let isPaneOpen = $state(false);
24+
let preVerified = $state(false);
25+
let loading = $state(false);
26+
let verificationId = $state("");
27+
let demoName = $state("");
28+
let verificationSuccess = $state(false);
2829
29-
const handleGetStarted = async () => {
30-
//get started functionality
31-
isPaneOpen = true;
32-
preVerified = false;
33-
};
30+
const handleGetStarted = async () => {
31+
//get started functionality
32+
isPaneOpen = true;
33+
preVerified = false;
34+
};
3435
35-
const handlePreVerified = () => {
36-
isPaneOpen = true;
37-
preVerified = true;
38-
};
36+
const handlePreVerified = () => {
37+
isPaneOpen = true;
38+
preVerified = true;
39+
};
3940
40-
function generatePassportNumber() {
41-
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
42-
const randomLetters = () =>
43-
letters.charAt(Math.floor(Math.random() * letters.length)) +
44-
letters.charAt(Math.floor(Math.random() * letters.length));
45-
const randomDigits = () =>
46-
String(Math.floor(1000000 + Math.random() * 9000000)); // 7 digits
41+
function generatePassportNumber() {
42+
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
43+
const randomLetters = () =>
44+
letters.charAt(Math.floor(Math.random() * letters.length)) +
45+
letters.charAt(Math.floor(Math.random() * letters.length));
46+
const randomDigits = () =>
47+
String(Math.floor(1000000 + Math.random() * 9000000)); // 7 digits
4748
48-
return randomLetters() + randomDigits();
49-
}
49+
return randomLetters() + randomDigits();
50+
}
5051
51-
// IMO, call this function early, check if hardware even supports the app
52-
// docs: https://github.com/auvoid/tauri-plugin-crypto-hw/blob/48d0b9db7083f9819766e7b3bfd19e39de9a77f3/examples/tauri-app/src/App.svelte#L13
53-
async function generateApplicationKeyPair() {
54-
let res: string | undefined;
55-
try {
56-
res = await generate("default");
57-
console.log(res);
58-
} catch (e) {
59-
// Put hardware crypto missing error here
60-
console.log(e);
52+
// IMO, call this function early, check if hardware even supports the app
53+
// docs: https://github.com/auvoid/tauri-plugin-crypto-hw/blob/48d0b9db7083f9819766e7b3bfd19e39de9a77f3/examples/tauri-app/src/App.svelte#L13
54+
async function generateApplicationKeyPair() {
55+
let res: string | undefined;
56+
try {
57+
res = await generate("default");
58+
console.log(res);
59+
} catch (e) {
60+
// Put hardware crypto missing error here
61+
console.log(e);
62+
}
63+
return res;
6164
}
62-
return res;
63-
}
6465
65-
async function getApplicationPublicKey() {
66-
let res: string | undefined;
67-
try {
68-
res = await getPublicKey("default");
69-
console.log(res);
70-
} catch (e) {
71-
console.log(e);
66+
async function getApplicationPublicKey() {
67+
let res: string | undefined;
68+
try {
69+
res = await getPublicKey("default");
70+
console.log(res);
71+
} catch (e) {
72+
console.log(e);
73+
}
74+
return res; // check getPublicKey doc comments (multibase hex format)
7275
}
73-
return res; // check getPublicKey doc comments (multibase hex format)
74-
}
7576
76-
const handleNext = async () => {
77-
//handle next functionlity
78-
goto("/verify");
79-
};
77+
const handleNext = async () => {
78+
//handle next functionlity
79+
goto("/verify");
80+
};
81+
82+
let globalState: GlobalState;
83+
let handleContinue: () => Promise<void> | void;
84+
let handleFinalSubmit: () => Promise<void> | void;
85+
let ename: string;
86+
let uri: string;
87+
88+
let error: string | null = $state(null);
8089
81-
let globalState: GlobalState;
82-
let handleContinue: () => Promise<void> | void;
83-
let handleFinalSubmit: () => Promise<void> | void;
84-
let ename: string;
85-
let uri: string;
90+
onMount(async () => {
91+
globalState = getContext<() => GlobalState>("globalState")();
92+
// handle verification logic + sec user data in the store
8693
87-
let error: string | null = $state(null);
94+
// check if default keypair exists
95+
const keyExists = await exists("default");
96+
if (!keyExists) {
97+
// if not, generate it
98+
await generateApplicationKeyPair();
99+
}
88100
89-
onMount(() => {
90-
globalState = getContext<() => GlobalState>("globalState")();
91-
// handle verification logic + sec user data in the store
101+
handleContinue = async () => {
102+
loading = true;
103+
const {
104+
data: { token: registryEntropy },
105+
} = await axios.get(
106+
new URL("/entropy", PUBLIC_REGISTRY_URL).toString(),
107+
);
92108
93-
handleContinue = async () => {
94-
loading = true;
95-
const {
96-
data: { token: registryEntropy },
97-
} = await axios.get(
98-
new URL("/entropy", PUBLIC_REGISTRY_URL).toString(),
99-
);
109+
const { data } = await axios
110+
.post(
111+
new URL("/provision", PUBLIC_PROVISIONER_URL).toString(),
112+
{
113+
registryEntropy,
114+
namespace: uuidv4(),
115+
verificationId,
116+
publicKey: await getApplicationPublicKey(),
117+
},
118+
)
119+
.catch(() => {
120+
loading = false;
121+
console.log("caught");
122+
preVerified = false;
123+
verificationId = "";
124+
error = "Wrong pre-verificaiton code";
125+
setTimeout(() => {
126+
error = null;
127+
}, 6_000);
128+
return { data: null };
129+
});
130+
if (!data) return;
100131
101-
const { data } = await axios
102-
.post(new URL("/provision", PUBLIC_PROVISIONER_URL).toString(), {
103-
registryEntropy,
104-
namespace: uuidv4(),
105-
verificationId,
106-
})
107-
.catch(() => {
132+
// If verification is successful, show demo name input
133+
if (data.success === true) {
108134
loading = false;
109-
console.log("caught");
110-
preVerified = false;
111-
verificationId = "";
112-
error = "Wrong pre-verificaiton code";
113-
setTimeout(() => {
114-
error = null;
115-
}, 6_000);
116-
return { data: null };
117-
});
118-
if (!data) return;
135+
verificationSuccess = true;
136+
uri = data.uri;
137+
ename = data.w3id;
138+
}
139+
};
119140
120-
// If verification is successful, show demo name input
121-
if (data.success === true) {
122-
loading = false;
123-
verificationSuccess = true;
124-
uri = data.uri;
125-
ename = data.w3id;
126-
}
127-
};
141+
// New function to handle final submission with demo name
142+
handleFinalSubmit = async () => {
143+
loading = true;
128144
129-
// New function to handle final submission with demo name
130-
handleFinalSubmit = async () => {
131-
loading = true;
145+
const tenYearsLater = new Date();
146+
tenYearsLater.setFullYear(tenYearsLater.getFullYear() + 10);
147+
globalState.userController.user = {
148+
name:
149+
demoName ||
150+
capitalize(
151+
`${falso.randFirstName()} ${falso.randLastName()}`,
152+
),
153+
"Date of Birth": new Date().toDateString(),
154+
"ID submitted": `Passport - ${falso.randCountryCode()}`,
155+
"Passport Number": generatePassportNumber(),
156+
};
157+
globalState.userController.isFake = true;
158+
globalState.userController.document = {
159+
"Valid From": new Date(Date.now()).toDateString(),
160+
"Valid Until": tenYearsLater.toDateString(),
161+
"Verified On": new Date().toDateString(),
162+
};
163+
globalState.vaultController.vault = {
164+
uri,
165+
ename,
166+
};
132167
133-
const tenYearsLater = new Date();
134-
tenYearsLater.setFullYear(tenYearsLater.getFullYear() + 10);
135-
globalState.userController.user = {
136-
name:
137-
demoName ||
138-
capitalize(`${falso.randFirstName()} ${falso.randLastName()}`),
139-
"Date of Birth": new Date().toDateString(),
140-
"ID submitted": `Passport - ${falso.randCountryCode()}`,
141-
"Passport Number": generatePassportNumber(),
168+
setTimeout(() => {
169+
goto("/register");
170+
}, 10_000);
142171
};
143-
globalState.userController.isFake = true;
144-
globalState.userController.document = {
145-
"Valid From": new Date(Date.now()).toDateString(),
146-
"Valid Until": tenYearsLater.toDateString(),
147-
"Verified On": new Date().toDateString(),
148-
};
149-
globalState.vaultController.vault = {
150-
uri,
151-
ename,
152-
};
153-
154-
setTimeout(() => {
155-
goto("/register");
156-
}, 10_000);
157-
};
158-
});
172+
});
159173
</script>
160174

161175
<main

0 commit comments

Comments
 (0)