From 39f3984ee46afdda6242204bcddedf556e3ccd51 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 9 Oct 2023 12:25:38 -0400 Subject: [PATCH] refactor: implement bip39 --- package.json | 5 +- .../lume/LumeIdentity/LumeIdentityContext.ts | 60 ++++++------------- .../lume/LumeIdentity/components.tsx | 59 +++++++++--------- 3 files changed, 51 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index ac9aa8b..9dd07e8 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,16 @@ "storybook": "storybook dev -p 6006", "build-storybook": "storybook build" }, - "files": ["lib"], + "files": [ + "lib" + ], "dependencies": { "@lumeweb/kernel-network-registry-client": "0.1.0-develop.10", "@lumeweb/libkernel": "0.1.0-develop.65", "@radix-ui/react-dialog": "^1.0.5", "@radix-ui/react-icons": "^1.3.0", "@radix-ui/react-slot": "^1.0.2", + "@scure/bip39": "^1.2.1", "class-variance-authority": "^0.7.0", "clsx": "^2.0.0", "framer-motion": "^10.16.4", diff --git a/src/components/lume/LumeIdentity/LumeIdentityContext.ts b/src/components/lume/LumeIdentity/LumeIdentityContext.ts index 4887935..3b9bfc2 100644 --- a/src/components/lume/LumeIdentity/LumeIdentityContext.ts +++ b/src/components/lume/LumeIdentity/LumeIdentityContext.ts @@ -1,48 +1,26 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; +import { + login, + loginComplete, + logoutComplete, +} from "@lumeweb/libkernel/kernel"; -export type Session = string; -export const LumeIdentityContext = React.createContext< - | { - session: Session | undefined; - setSession: React.Dispatch>; - } - | undefined ->(undefined); export function useLumeIndentity() { - const contextValue = React.useContext(LumeIdentityContext); - - // When the `session` changes we want to update the `session` in the local storage? - React.useEffect(() => { - if (contextValue?.session) { - localStorage.setItem("lume-session", contextValue.session); - } else { - localStorage.removeItem("lume-session"); - } - }, [contextValue?.session]); - - // Get the session from the local storage - React.useEffect(() => { - const session = localStorage.getItem("lume-session"); - if (session) { - contextValue?.setSession(session); - } - }, []); - - if (contextValue === undefined) { - throw new Error( - "useLumeIdentity hook is being used outside of its context. Please ensure that it is wrapped within a ." - ); - } + const [loggedIn, setLoggedIn] = useState(false); + useEffect(() => { + loginComplete().then(() => { + setLoggedIn(true); + }); + logoutComplete().then(() => { + setLoggedIn(false); + }); + }); return { - isSignedIn: !!contextValue.session, - signIn: (key: string) => { - console.log("signing in with key", key); - // TODO: From the key generate a session, and store it - contextValue.setSession("session"); - }, - signOut: () => { - contextValue.setSession(undefined); + isSignedIn: loggedIn, + async signIn(key: Uint8Array) { + await login(key); }, + signOut: () => {}, }; } diff --git a/src/components/lume/LumeIdentity/components.tsx b/src/components/lume/LumeIdentity/components.tsx index 6c26519..7e55f38 100644 --- a/src/components/lume/LumeIdentity/components.tsx +++ b/src/components/lume/LumeIdentity/components.tsx @@ -13,6 +13,13 @@ import { AnimatePresence, m } from "framer-motion"; import { useLumeIndentity } from "./LumeIdentityContext"; import { useMemo, useState } from "react"; +import * as bip39 from "@scure/bip39"; +import { wordlist } from "@scure/bip39/wordlists/english"; + +async function seedToKey(seed: string) { + return bip39.mnemonicToSeed(seed); +} + // Extracted components const SubmitButtonComponent = () => { const { setVisibleComponent } = useSwitchableComponent(); @@ -20,8 +27,7 @@ const SubmitButtonComponent = () => { ) : null} @@ -192,9 +188,10 @@ const SeedPhraseGenerationComponent = ({ phraseLength = 12 }) => { initial={{ opacity: 0, y: -50 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: 50 }} - transition={{ type: "linear", delay: 2, duration: 0.5 }} - > - @@ -206,17 +203,17 @@ const SeedPhraseGenerationComponent = ({ phraseLength = 12 }) => { export const SubmitButton = makeSwitchable( SubmitButtonComponent, - "submit-button" + "submit-button", ); export const SeedPhraseInput = makeSwitchable( SeedPhraseInputComponent, - "seed-phrase-input" + "seed-phrase-input", ); export const SetupAccountKey = makeSwitchable( SetupAccountKeyComponent, - "setup-account-key" + "setup-account-key", ); export const SeedPhraseGeneration = makeSwitchable( SeedPhraseGenerationComponent, - "seed-phrase-form" + "seed-phrase-form", );