From 96737b6e217f55f2b1c43340f04be3eb1ed6045b Mon Sep 17 00:00:00 2001 From: veralygit <57183851+veralygit@users.noreply.github.com> Date: Thu, 9 Feb 2023 17:26:33 +0800 Subject: [PATCH 1/3] feat(walletkit-ui): add authentication to store --- .../walletkit-ui/src/store/authentication.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 packages/walletkit-ui/src/store/authentication.ts diff --git a/packages/walletkit-ui/src/store/authentication.ts b/packages/walletkit-ui/src/store/authentication.ts new file mode 100644 index 0000000..41fc7b5 --- /dev/null +++ b/packages/walletkit-ui/src/store/authentication.ts @@ -0,0 +1,33 @@ +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; + +export interface Authentication { + consume: (passphrase: string) => Promise; // also serve as passphrase validation logic + onAuthenticated: (result: T) => Promise; + // OPTIONAL error handler + // (CRITICAL invalid passphare error is auto handled, include wipe wallet if necessary) + onError?: (e: Error) => void; + + // messages in passcode UI + // message and loading are mandatory, default message is for signing transaction + title?: string; + message: string; + loading: string; + additionalMessage?: string; + additionalMessageUrl?: string; + successMessage?: string; +} + +const initialState: { authentication?: Authentication } = {}; + +export const authentication = createSlice({ + name: "authentication", + initialState, + reducers: { + prompt: (state, action: PayloadAction>) => { + state.authentication = action.payload; + }, + dismiss: (state) => { + delete state.authentication; + }, + }, +}); From 1f0134006542a7cb0c60669657d9e5e335e527c2 Mon Sep 17 00:00:00 2001 From: veralygit <57183851+veralygit@users.noreply.github.com> Date: Fri, 10 Feb 2023 11:07:47 +0800 Subject: [PATCH 2/3] add auth test file and include in index.ts --- .../src/store/authentication.unit.ts | 48 +++++++++++++++++++ packages/walletkit-ui/src/store/index.ts | 1 + 2 files changed, 49 insertions(+) create mode 100644 packages/walletkit-ui/src/store/authentication.unit.ts diff --git a/packages/walletkit-ui/src/store/authentication.unit.ts b/packages/walletkit-ui/src/store/authentication.unit.ts new file mode 100644 index 0000000..c815908 --- /dev/null +++ b/packages/walletkit-ui/src/store/authentication.unit.ts @@ -0,0 +1,48 @@ +import {Authentication,authentication} from "./authentication"; + + +describe("authentication reducer", () => { + let initialState: { authentication?: Authentication } + + beforeEach(()=>{ + initialState = { + authentication: { + consume: () => new Promise(() => {}), + onAuthenticated: ()=>new Promise(() => {}), + message: "Enter passcode to continue", + loading: "Verifying access" + } + } + }) + + it("should handle initial state", () => { + expect(authentication.reducer(undefined, { type: "unknown" })).toEqual({ + }); + }); + + it("should handle prompt", () =>{ + + const payload: Authentication = { + consume: () => new Promise(() => {}), + onAuthenticated: ()=>new Promise(() => {}), + message: "Enter change passcode to continue", + loading: "Verifying access" + } + + const actual = authentication.reducer( + initialState, + authentication.actions.prompt(payload) + ); + expect(actual.authentication).toMatchObject(payload); + + }) + + it("should handle dismiss state", () =>{ + const actual = authentication.reducer( + initialState, + authentication.actions.dismiss() + ); + expect(actual.authentication).toStrictEqual(undefined) + + }) +}) diff --git a/packages/walletkit-ui/src/store/index.ts b/packages/walletkit-ui/src/store/index.ts index ade4a7f..01ebf62 100644 --- a/packages/walletkit-ui/src/store/index.ts +++ b/packages/walletkit-ui/src/store/index.ts @@ -1,3 +1,4 @@ +export * from "./authentication" export * from "./block"; export * from "./loans"; export * from "./ocean"; From 1dadac65e8c89e81069414140297fdde154f2a9a Mon Sep 17 00:00:00 2001 From: veralygit <57183851+veralygit@users.noreply.github.com> Date: Fri, 10 Feb 2023 11:18:13 +0800 Subject: [PATCH 3/3] prettier --- .../src/store/authentication.unit.ts | 41 ++++++++----------- packages/walletkit-ui/src/store/index.ts | 2 +- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/packages/walletkit-ui/src/store/authentication.unit.ts b/packages/walletkit-ui/src/store/authentication.unit.ts index c815908..8bf93fb 100644 --- a/packages/walletkit-ui/src/store/authentication.unit.ts +++ b/packages/walletkit-ui/src/store/authentication.unit.ts @@ -1,48 +1,43 @@ -import {Authentication,authentication} from "./authentication"; - +import { Authentication, authentication } from "./authentication"; describe("authentication reducer", () => { - let initialState: { authentication?: Authentication } + let initialState: { authentication?: Authentication }; - beforeEach(()=>{ + beforeEach(() => { initialState = { authentication: { consume: () => new Promise(() => {}), - onAuthenticated: ()=>new Promise(() => {}), + onAuthenticated: () => new Promise(() => {}), message: "Enter passcode to continue", - loading: "Verifying access" - } - } - }) + loading: "Verifying access", + }, + }; + }); it("should handle initial state", () => { - expect(authentication.reducer(undefined, { type: "unknown" })).toEqual({ - }); + expect(authentication.reducer(undefined, { type: "unknown" })).toEqual({}); }); - it("should handle prompt", () =>{ - + it("should handle prompt", () => { const payload: Authentication = { consume: () => new Promise(() => {}), - onAuthenticated: ()=>new Promise(() => {}), + onAuthenticated: () => new Promise(() => {}), message: "Enter change passcode to continue", - loading: "Verifying access" - } + loading: "Verifying access", + }; const actual = authentication.reducer( initialState, authentication.actions.prompt(payload) ); expect(actual.authentication).toMatchObject(payload); + }); - }) - - it("should handle dismiss state", () =>{ + it("should handle dismiss state", () => { const actual = authentication.reducer( initialState, authentication.actions.dismiss() ); - expect(actual.authentication).toStrictEqual(undefined) - - }) -}) + expect(actual.authentication).toStrictEqual(undefined); + }); +}); diff --git a/packages/walletkit-ui/src/store/index.ts b/packages/walletkit-ui/src/store/index.ts index 7ec75d3..060e5c8 100644 --- a/packages/walletkit-ui/src/store/index.ts +++ b/packages/walletkit-ui/src/store/index.ts @@ -1,5 +1,5 @@ -export * from "./authentication" export * from "./auctions"; +export * from "./authentication"; export * from "./block"; export * from "./loans"; export * from "./ocean";