Skip to content

Commit e2e99c3

Browse files
committed
Create SessKey on login with Metamask
1 parent 08b66e2 commit e2e99c3

File tree

6 files changed

+76
-45
lines changed

6 files changed

+76
-45
lines changed

front/bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"crypto-js": "^4.2.0",
1313
"elliptic": "^6.6.1",
1414
"hyli": "^0.3.0",
15-
"hyli-wallet": "0.5.13",
15+
"hyli-wallet": "0.5.14",
1616
"react": "^19.2.0",
1717
"react-dom": "^19.2.0",
1818
"react-router-dom": "^7.9.3",
@@ -495,7 +495,7 @@
495495

496496
"hyli-noir": ["[email protected]", "", { "dependencies": { "hyli": "^0.4.1", "noir-jwt": "^0.4.5" }, "peerDependencies": { "@aztec/bb.js": "0.87.9", "@noir-lang/noir_js": "1.0.0-beta.11", "@noir-lang/noir_wasm": "1.0.0-beta.11" } }, "sha512-2aAhpbYvaGzxuWS5Jqcl5QY51FcPg4wrPdbWR3GJBYapiw9So/C40lkmEyEVmEXE+F5cDPNSFs67Hvjnb5Camg=="],
497497

498-
"hyli-wallet": ["[email protected].13", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-XWM+X9AMrPM7BijFpXgyYcVNJgEVCnAPubOLhb7CCALixhp0FhlUj4c1Qkz9yfN8D83jE4iF9j2F7Af/4YmcEA=="],
498+
"hyli-wallet": ["[email protected].14", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-93jZglYUh9zU+mx+Kf31xwgY17qaDX0SwDm1YXouifMVXAy0ZPK0jP+gSdO1pQsKLfHs4LYljUWngr80zwgGyA=="],
499499

500500
"idb-keyval": ["[email protected]", "", {}, "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg=="],
501501

front/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"crypto-js": "^4.2.0",
1919
"elliptic": "^6.6.1",
2020
"hyli": "^0.3.0",
21-
"hyli-wallet": "0.5.13",
21+
"hyli-wallet": "0.5.14",
2222
"react": "^19.2.0",
2323
"react-dom": "^19.2.0",
2424
"react-router-dom": "^7.9.3",

hyli-wallet/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyli-wallet",
3-
"version": "0.5.13",
3+
"version": "0.5.14",
44
"type": "module",
55
"license": "MIT",
66
"repository": {

hyli-wallet/src/providers/MetamaskAuthProvider.ts

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -318,44 +318,75 @@ export class MetamaskAuthProvider implements AuthProvider<MetamaskAuthCredential
318318
salt,
319319
};
320320

321-
let newSessionKey;
322-
if (registerSessionKey) {
323-
const nodeService = NodeService.getInstance();
324-
const secp256k1Blob: Secp256k1Blob = {
325-
identity,
326-
data: digest,
327-
public_key: publicKey,
328-
signature: compactSignature,
329-
};
330-
const secp_blob = {
331-
contract_name: "secp256k1",
332-
data: serializeSecp256k1Blob(secp256k1Blob),
333-
};
334-
335-
const wallet_blob = verifyIdentityBlob(username, nonce);
336-
337-
const { duration, whitelist } = registerSessionKey;
338-
const expiration = Date.now() + duration;
339-
newSessionKey = sessionKeyService.generateSessionKey(expiration, whitelist);
340-
341-
const newPKblob = addSessionKeyBlob(username, newSessionKey.publicKey, expiration, nonce, whitelist)
342-
const blobTx: BlobTransaction = {
343-
identity,
344-
// warning: secp_blob need to be at index 1
345-
blobs: [wallet_blob, secp_blob, newPKblob],
346-
};
347-
348-
349-
onWalletEvent?.({ account: identity, type: "sending_blob", message: "Sending blob transaction" });
350-
351-
const txHash = await hashBlobTransaction(blobTx);
352-
onWalletEvent?.({ account: identity, type: "blob_sent", message: `Blob transaction sent: ${txHash}` });
353-
354-
await nodeService.client.sendBlobTx(blobTx);
321+
const sessionKeyPromise = registerSessionKey ? WalletOperations.getOrReuseSessionKey(wallet) : undefined;
355322

356-
// TODO(?): Assert transaction settles to assure the session key is valid (?)
323+
if (registerSessionKey) {
324+
try {
325+
const existingSessionKey = sessionKeyPromise ? await sessionKeyPromise : undefined;
326+
if (existingSessionKey) {
327+
wallet.sessionKey = existingSessionKey;
328+
} else {
329+
const nodeService = NodeService.getInstance();
330+
const secp256k1Blob: Secp256k1Blob = {
331+
identity,
332+
data: digest,
333+
public_key: publicKey,
334+
signature: compactSignature,
335+
};
336+
const secp_blob = {
337+
contract_name: "secp256k1",
338+
data: serializeSecp256k1Blob(secp256k1Blob),
339+
};
340+
341+
const wallet_blob = verifyIdentityBlob(username, nonce);
342+
343+
const { duration, whitelist, laneId } = registerSessionKey;
344+
const expiration = Date.now() + duration;
345+
const generatedSessionKey = sessionKeyService.generateSessionKey(expiration, whitelist);
346+
const newSessionKey = laneId
347+
? { ...generatedSessionKey, laneId }
348+
: generatedSessionKey;
349+
350+
const newPKblob = addSessionKeyBlob(
351+
username,
352+
newSessionKey.publicKey,
353+
expiration,
354+
nonce,
355+
whitelist,
356+
laneId,
357+
);
358+
const blobTx: BlobTransaction = {
359+
identity,
360+
// warning: secp_blob need to be at index 1
361+
blobs: [wallet_blob, secp_blob, newPKblob],
362+
};
363+
364+
onWalletEvent?.({ account: identity, type: "sending_blob", message: "Sending blob transaction" });
365+
366+
const txHash = await hashBlobTransaction(blobTx);
367+
onWalletEvent?.({
368+
account: identity,
369+
type: "blob_sent",
370+
message: `Blob transaction sent: ${txHash}`,
371+
});
372+
373+
await nodeService.client.sendBlobTx(blobTx);
374+
375+
// TODO(?): Assert transaction settles to assure the session key is valid (?)
376+
wallet.sessionKey = newSessionKey;
377+
}
378+
} catch (sessionKeyError) {
379+
console.error("Failed to register session key via MetaMask:", sessionKeyError);
380+
onError?.(
381+
sessionKeyError instanceof Error
382+
? sessionKeyError
383+
: new Error("Failed to register MetaMask session key"),
384+
);
385+
}
357386
}
358387

388+
wallet = WalletOperations.cleanExpiredSessionKeys(wallet);
389+
359390
onWalletEvent?.({
360391
account: identity,
361392
type: "logged_in",

hyli-wallet/vue/bun.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
"": {
55
"name": "hyli-wallet-vue",
66
"dependencies": {
7-
"hyli-wallet": "^0.5.11",
7+
"hyli-wallet": "^0.5.14",
88
},
99
"devDependencies": {
1010
"@vitejs/plugin-vue": "^6.0.1",
1111
"@vue/tsconfig": "^0.8.1",
12-
"typescript": "^5.9.2",
12+
"typescript": "^5.9.3",
1313
"unplugin-dts": "^1.0.0-beta.6",
1414
"vite": "npm:[email protected]",
1515
"vite-bundle-analyzer": "^1.2.3",
16-
"vue-tsc": "^3.1.0",
16+
"vue-tsc": "^3.1.2",
1717
},
1818
"peerDependencies": {
1919
"vue": "^3.5.22",
@@ -218,7 +218,7 @@
218218

219219
"hyli-noir": ["[email protected]", "", { "dependencies": { "hyli": "^0.4.1", "noir-jwt": "^0.4.5" }, "peerDependencies": { "@aztec/bb.js": "0.87.9", "@noir-lang/noir_js": "1.0.0-beta.11", "@noir-lang/noir_wasm": "1.0.0-beta.11" } }, "sha512-2aAhpbYvaGzxuWS5Jqcl5QY51FcPg4wrPdbWR3GJBYapiw9So/C40lkmEyEVmEXE+F5cDPNSFs67Hvjnb5Camg=="],
220220

221-
"hyli-wallet": ["[email protected].13", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-XWM+X9AMrPM7BijFpXgyYcVNJgEVCnAPubOLhb7CCALixhp0FhlUj4c1Qkz9yfN8D83jE4iF9j2F7Af/4YmcEA=="],
221+
"hyli-wallet": ["[email protected].14", "", { "dependencies": { "@noble/hashes": "^2.0.1", "@types/crypto-js": "^4.2.2", "@types/elliptic": "^6.4.18", "@types/react-router-dom": "^5.3.3", "elliptic": "^6.6.1", "hyli": "^0.4.1", "js-sha3": "^0.9.3" }, "peerDependencies": { "hyli-noir": "^0.3.0", "react": "^19.1.1", "react-dom": "^19.1.1", "react-router-dom": "^7.9.3" } }, "sha512-93jZglYUh9zU+mx+Kf31xwgY17qaDX0SwDm1YXouifMVXAy0ZPK0jP+gSdO1pQsKLfHs4LYljUWngr80zwgGyA=="],
222222

223223
"idb-keyval": ["[email protected]", "", {}, "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg=="],
224224

hyli-wallet/vue/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyli-wallet-vue",
3-
"version": "0.1.13",
3+
"version": "0.1.14",
44
"type": "module",
55
"main": "./dist/hyli-wallet.cjs.js",
66
"module": "./dist/hyli-wallet.es.js",
@@ -26,7 +26,7 @@
2626
"vue": "^3.5.22"
2727
},
2828
"dependencies": {
29-
"hyli-wallet": "^0.5.13"
29+
"hyli-wallet": "^0.5.14"
3030
},
3131
"devDependencies": {
3232
"@vitejs/plugin-vue": "^6.0.1",

0 commit comments

Comments
 (0)