update unlock page

This commit is contained in:
Ren Amamiya
2023-07-09 17:38:54 +07:00
parent a099f4fc85
commit 5787eff7d5
6 changed files with 353 additions and 267 deletions

View File

@@ -17,7 +17,7 @@
}, },
"dependencies": { "dependencies": {
"@headlessui/react": "^1.7.15", "@headlessui/react": "^1.7.15",
"@nostr-dev-kit/ndk": "^0.7.4", "@nostr-dev-kit/ndk": "^0.7.5",
"@radix-ui/react-popover": "^1.0.6", "@radix-ui/react-popover": "^1.0.6",
"@radix-ui/react-tooltip": "^1.0.6", "@radix-ui/react-tooltip": "^1.0.6",
"@tanstack/react-query": "^4.29.19", "@tanstack/react-query": "^4.29.19",

54
pnpm-lock.yaml generated
View File

@@ -5,8 +5,8 @@ dependencies:
specifier: ^1.7.15 specifier: ^1.7.15
version: 1.7.15(react-dom@18.2.0)(react@18.2.0) version: 1.7.15(react-dom@18.2.0)(react@18.2.0)
'@nostr-dev-kit/ndk': '@nostr-dev-kit/ndk':
specifier: ^0.7.4 specifier: ^0.7.5
version: 0.7.4(typescript@4.9.5) version: 0.7.5(typescript@4.9.5)
'@radix-ui/react-popover': '@radix-ui/react-popover':
specifier: ^1.0.6 specifier: ^1.0.6
version: 1.0.6(@types/react-dom@18.2.6)(@types/react@18.2.14)(react-dom@18.2.0)(react@18.2.0) version: 1.0.6(@types/react-dom@18.2.6)(@types/react@18.2.14)(react-dom@18.2.0)(react@18.2.0)
@@ -904,8 +904,8 @@ packages:
'@nodelib/fs.scandir': 2.1.5 '@nodelib/fs.scandir': 2.1.5
fastq: 1.15.0 fastq: 1.15.0
/@nostr-dev-kit/ndk@0.7.4(typescript@4.9.5): /@nostr-dev-kit/ndk@0.7.5(typescript@4.9.5):
resolution: {integrity: sha512-wMNv9CREN8j40KERIvfXxCCg08CIP6VPUjlFI+Jg9+MTPoqvt0Vpwo/lD8lhWWJNPwC3RRyr4ywogApB9tevbA==} resolution: {integrity: sha512-P3hcvWq0jMlTBiasMtcqP2FclGwVks1LIwhRhyJb5XesLEYvnWh1wBWnx3+UQCj9uuu7fBMkmrY7AAi3CTuaXA==}
dependencies: dependencies:
'@noble/hashes': 1.3.1 '@noble/hashes': 1.3.1
'@noble/secp256k1': 2.0.0 '@noble/secp256k1': 2.0.0
@@ -937,7 +937,7 @@ packages:
resolution: {integrity: sha512-7kZUAaLscfgbwBQRbvdMYaZOWyMEcPTH/tJjnyAWJ/dvvs9Ef+CERx/qJb9GExJpl1qipaDGn7KqHnFGGixd0w==} resolution: {integrity: sha512-7kZUAaLscfgbwBQRbvdMYaZOWyMEcPTH/tJjnyAWJ/dvvs9Ef+CERx/qJb9GExJpl1qipaDGn7KqHnFGGixd0w==}
engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0} engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0}
dependencies: dependencies:
semver: 7.5.3 semver: 7.5.4
dev: false dev: false
/@pkgjs/parseargs@0.11.0: /@pkgjs/parseargs@0.11.0:
@@ -1814,7 +1814,7 @@ packages:
graphemer: 1.4.0 graphemer: 1.4.0
ignore: 5.2.4 ignore: 5.2.4
natural-compare-lite: 1.4.0 natural-compare-lite: 1.4.0
semver: 7.5.3 semver: 7.5.4
tsutils: 3.21.0(typescript@4.9.5) tsutils: 3.21.0(typescript@4.9.5)
typescript: 4.9.5 typescript: 4.9.5
transitivePeerDependencies: transitivePeerDependencies:
@@ -1883,7 +1883,7 @@ packages:
debug: 4.3.4 debug: 4.3.4
globby: 11.1.0 globby: 11.1.0
is-glob: 4.0.3 is-glob: 4.0.3
semver: 7.5.3 semver: 7.5.4
tsutils: 3.21.0(typescript@4.9.5) tsutils: 3.21.0(typescript@4.9.5)
typescript: 4.9.5 typescript: 4.9.5
transitivePeerDependencies: transitivePeerDependencies:
@@ -1903,7 +1903,7 @@ packages:
'@typescript-eslint/typescript-estree': 5.61.0(typescript@4.9.5) '@typescript-eslint/typescript-estree': 5.61.0(typescript@4.9.5)
eslint: 8.44.0 eslint: 8.44.0
eslint-scope: 5.1.1 eslint-scope: 5.1.1
semver: 7.5.3 semver: 7.5.4
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
- typescript - typescript
@@ -2126,7 +2126,7 @@ packages:
postcss: ^8.1.0 postcss: ^8.1.0
dependencies: dependencies:
browserslist: 4.21.9 browserslist: 4.21.9
caniuse-lite: 1.0.30001513 caniuse-lite: 1.0.30001514
fraction.js: 4.2.0 fraction.js: 4.2.0
normalize-range: 0.1.2 normalize-range: 0.1.2
picocolors: 1.0.0 picocolors: 1.0.0
@@ -2184,8 +2184,8 @@ packages:
engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7} engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7}
hasBin: true hasBin: true
dependencies: dependencies:
caniuse-lite: 1.0.30001513 caniuse-lite: 1.0.30001514
electron-to-chromium: 1.4.453 electron-to-chromium: 1.4.454
node-releases: 2.0.13 node-releases: 2.0.13
update-browserslist-db: 1.0.11(browserslist@4.21.9) update-browserslist-db: 1.0.11(browserslist@4.21.9)
dev: true dev: true
@@ -2204,7 +2204,7 @@ packages:
dependencies: dependencies:
'@npmcli/fs': 3.1.0 '@npmcli/fs': 3.1.0
fs-minipass: 3.0.2 fs-minipass: 3.0.2
glob: 10.3.1 glob: 10.3.3
lru-cache: 7.18.3 lru-cache: 7.18.3
minipass: 5.0.0 minipass: 5.0.0
minipass-collect: 1.0.2 minipass-collect: 1.0.2
@@ -2245,8 +2245,8 @@ packages:
engines: {node: '>=6'} engines: {node: '>=6'}
dev: false dev: false
/caniuse-lite@1.0.30001513: /caniuse-lite@1.0.30001514:
resolution: {integrity: sha512-pnjGJo7SOOjAGytZZ203Em95MRM8Cr6jhCXNF/FAXTpCTRTECnqQWLpiTRqrFtdYcth8hf4WECUpkezuYsMVww==} resolution: {integrity: sha512-ENcIpYBmwAAOm/V2cXgM7rZUrKKaqisZl4ZAI520FIkqGXUxJjmaIssbRW5HVVR5tyV6ygTLIm15aU8LUmQSaQ==}
dev: true dev: true
/chalk@2.4.2: /chalk@2.4.2:
@@ -2402,7 +2402,7 @@ packages:
resolution: {integrity: sha512-cllzD6IU/mzXBs5OdQVWL3+ne5Elpu3Wdm7h5OldMbGXk76yr9XzHlQXWJ4zfs0ZAibe26rkbs4KvMAJm7fIZA==} resolution: {integrity: sha512-cllzD6IU/mzXBs5OdQVWL3+ne5Elpu3Wdm7h5OldMbGXk76yr9XzHlQXWJ4zfs0ZAibe26rkbs4KvMAJm7fIZA==}
engines: {node: '>=14.x'} engines: {node: '>=14.x'}
dependencies: dependencies:
semver: 7.5.3 semver: 7.5.4
dev: false dev: false
/cross-env@7.0.3: /cross-env@7.0.3:
@@ -2625,8 +2625,8 @@ packages:
/eastasianwidth@0.2.0: /eastasianwidth@0.2.0:
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
/electron-to-chromium@1.4.453: /electron-to-chromium@1.4.454:
resolution: {integrity: sha512-BU8UtQz6CB3T7RIGhId4BjmjJVXQDujb0+amGL8jpcluFJr6lwspBOvkUbnttfpZCm4zFMHmjrX1QrdPWBBMjQ==} resolution: {integrity: sha512-pmf1rbAStw8UEQ0sr2cdJtWl48ZMuPD9Sto8HVQOq9vx9j2WgDEN6lYoaqFvqEHYOmGA9oRGn7LqWI9ta0YugQ==}
dev: true dev: true
/emoji-regex@8.0.0: /emoji-regex@8.0.0:
@@ -3047,7 +3047,7 @@ packages:
dependencies: dependencies:
create-esm-loader: 0.2.3 create-esm-loader: 0.2.3
npm-run-all: 4.1.5 npm-run-all: 4.1.5
semver: 7.5.3 semver: 7.5.4
typescript: 5.1.6 typescript: 5.1.6
dev: false dev: false
@@ -3325,8 +3325,8 @@ packages:
dependencies: dependencies:
is-glob: 4.0.3 is-glob: 4.0.3
/glob@10.3.1: /glob@10.3.3:
resolution: {integrity: sha512-9BKYcEeIs7QwlCYs+Y3GBvqAMISufUS0i2ELd11zpZjxI5V9iyRj0HgzB5/cLf2NY4vcYBTYzJ7GIui7j/4DOw==} resolution: {integrity: sha512-92vPiMb/iqpmEgsOoIDvTjc50wf9CCCvMzsi6W0JLPeUKE8TWP1a73PgqSrqy7iAZxaSD1YdzU7QZR5LF51MJw==}
engines: {node: '>=16 || 14 >=14.17'} engines: {node: '>=16 || 14 >=14.17'}
hasBin: true hasBin: true
dependencies: dependencies:
@@ -3334,7 +3334,7 @@ packages:
jackspeak: 2.2.1 jackspeak: 2.2.1
minimatch: 9.0.3 minimatch: 9.0.3
minipass: 5.0.0 minipass: 5.0.0
path-scurry: 1.10.0 path-scurry: 1.10.1
dev: false dev: false
/glob@7.1.6: /glob@7.1.6:
@@ -4243,7 +4243,7 @@ packages:
nopt: 6.0.0 nopt: 6.0.0
npmlog: 6.0.2 npmlog: 6.0.2
rimraf: 3.0.2 rimraf: 3.0.2
semver: 7.5.3 semver: 7.5.4
tar: 6.1.15 tar: 6.1.15
which: 2.0.2 which: 2.0.2
transitivePeerDependencies: transitivePeerDependencies:
@@ -4277,7 +4277,7 @@ packages:
dependencies: dependencies:
hosted-git-info: 4.1.0 hosted-git-info: 4.1.0
is-core-module: 2.12.1 is-core-module: 2.12.1
semver: 7.5.3 semver: 7.5.4
validate-npm-package-license: 3.0.4 validate-npm-package-license: 3.0.4
dev: false dev: false
@@ -4532,8 +4532,8 @@ packages:
/path-parse@1.0.7: /path-parse@1.0.7:
resolution: {integrity: sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==} resolution: {integrity: sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==}
/path-scurry@1.10.0: /path-scurry@1.10.1:
resolution: {integrity: sha512-tZFEaRQbMLjwrsmidsGJ6wDMv0iazJWk6SfIKnY4Xru8auXgmJkOBa5DUbYFcFD2Rzk2+KDlIiF0GVXNCbgC7g==} resolution: {integrity: sha512-MkhCqzzBEpPvxxQ71Md0b1Kk51W01lrYvlMzSUaIzNsODdd7mqhiimSZlr+VegAz5Z6Vzt9Xg2ttE//XBhH3EQ==}
engines: {node: '>=16 || 14 >=14.17'} engines: {node: '>=16 || 14 >=14.17'}
dependencies: dependencies:
lru-cache: 10.0.0 lru-cache: 10.0.0
@@ -5110,8 +5110,8 @@ packages:
resolution: {integrity: sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==} resolution: {integrity: sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==}
hasBin: true hasBin: true
/semver@7.5.3: /semver@7.5.4:
resolution: {integrity: sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==} resolution: {integrity: sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==}
engines: {node: '>=10'} engines: {node: '>=10'}
hasBin: true hasBin: true
dependencies: dependencies:

524
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,3 @@
import { getPublicKey } from 'nostr-tools';
import { useState } from 'react'; import { useState } from 'react';
import { Resolver, useForm } from 'react-hook-form'; import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
@@ -30,10 +29,13 @@ const resolver: Resolver<FormValues> = async (values) => {
export function UnlockScreen() { export function UnlockScreen() {
const navigate = useNavigate(); const navigate = useNavigate();
const setPassword = useStronghold((state) => state.setPassword);
const [passwordInput, setPasswordInput] = useState('password'); const [passwordInput, setPasswordInput] = useState('password');
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [setPrivkey, setPassword] = useStronghold((state) => [
state.setPrivkey,
state.setPassword,
]);
const { account } = useAccount(); const { account } = useAccount();
const { load } = useSecureStorage(); const { load } = useSecureStorage();
@@ -61,22 +63,12 @@ export function UnlockScreen() {
setPassword(data.password); setPassword(data.password);
// load private in secure storage // load private in secure storage
const privkey = await load(account.pubkey, data.password); try {
const privkey = await load(account.pubkey, data.password);
if (!privkey) { setPrivkey(privkey);
setLoading(false); // redirect to home
setError('password', {
type: 'custom',
message: "Can't get private key",
});
}
const tempPubkey = getPublicKey(privkey);
if (tempPubkey === account.pubkey) {
// redirect to next step
navigate('/', { replace: true }); navigate('/', { replace: true });
} else { } catch {
setLoading(false); setLoading(false);
setError('password', { setError('password', {
type: 'custom', type: 'custom',

View File

@@ -2,12 +2,18 @@ import { create } from 'zustand';
interface StrongholdState { interface StrongholdState {
password: null | string; password: null | string;
privkey: null | string;
setPassword: (password: string) => void; setPassword: (password: string) => void;
setPrivkey: (privkey: string) => void;
} }
export const useStronghold = create<StrongholdState>((set) => ({ export const useStronghold = create<StrongholdState>((set) => ({
password: null, password: null,
privkey: null,
setPassword: (password: string) => { setPassword: (password: string) => {
set({ password: password }); set({ password: password });
}, },
setPrivkey: (privkey: string) => {
set({ privkey: privkey });
},
})); }));

View File

@@ -2,6 +2,8 @@ import { NDKEvent, NDKKind, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';
import { useNDK } from '@libs/ndk/provider'; import { useNDK } from '@libs/ndk/provider';
import { useStronghold } from '@stores/stronghold';
import { useAccount } from '@utils/hooks/useAccount'; import { useAccount } from '@utils/hooks/useAccount';
import { useSecureStorage } from '@utils/hooks/useSecureStorage'; import { useSecureStorage } from '@utils/hooks/useSecureStorage';
@@ -10,6 +12,8 @@ export function usePublish() {
const { account } = useAccount(); const { account } = useAccount();
const { load } = useSecureStorage(); const { load } = useSecureStorage();
const privkey = useStronghold((state) => state.privkey);
const publish = async ({ const publish = async ({
content, content,
kind, kind,
@@ -19,10 +23,10 @@ export function usePublish() {
kind: NDKKind; kind: NDKKind;
tags: string[][]; tags: string[][];
}): Promise<NDKEvent> => { }): Promise<NDKEvent> => {
const privkey = await load(account.pubkey); const securePrivkey = await load(account.pubkey);
const event = new NDKEvent(ndk); const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(privkey); const signer = new NDKPrivateKeySigner(privkey ? privkey : securePrivkey);
event.content = content; event.content = content;
event.kind = kind; event.kind = kind;