Replace usages of privateKey

This commit is contained in:
Kieran 2023-09-21 22:34:36 +01:00
parent 96d4e4bcc5
commit 01b3fd559b
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
8 changed files with 70 additions and 59 deletions

View File

@ -119,6 +119,10 @@ export function LoginUnlock() {
pub.pow(login.preferences.pow, DefaultPowWorker);
}
LoginStore.setPublisher(login.id, pub);
LoginStore.updateSession({
...login,
privateKeyData: key,
});
}
}
@ -142,7 +146,7 @@ export function LoginUnlock() {
<PinPrompt
subTitle={
<p>
<FormattedMessage defaultMessage="Enter pin to unlock private key" />
<FormattedMessage defaultMessage="Enter pin to unlock your private key" />
</p>
}
onResult={unlockSession}

View File

@ -6,6 +6,7 @@ import { LoginSessionType, LoginStore } from "Login";
import { generateBip39Entropy, entropyToPrivateKey } from "nip6";
import { getNip05PubKey } from "Pages/LoginPage";
import { bech32ToHex } from "SnortUtils";
import { unwrap } from "@snort/shared";
export class PinRequiredError extends Error {}
@ -54,11 +55,18 @@ export default function useLoginHandler() {
const hexKey = await getNip05PubKey(key);
LoginStore.loginWithPubkey(hexKey, LoginSessionType.PublicKey);
} else if (key.startsWith("bunker://")) {
if (!pin) throw new PinRequiredError();
const nip46 = new Nip46Signer(key);
await nip46.init();
const loginPubkey = await nip46.getPubKey();
LoginStore.loginWithPubkey(loginPubkey, LoginSessionType.Nip46, undefined, nip46.relays, nip46.privateKey);
LoginStore.loginWithPubkey(
loginPubkey,
LoginSessionType.Nip46,
undefined,
nip46.relays,
await PinEncrypted.create(unwrap(nip46.privateKey), pin),
);
nip46.close();
} else {
throw new Error("INVALID PRIVATE KEY");

View File

@ -96,7 +96,7 @@ export class MultiAccountStore extends ExternalStore<LoginSession> {
type: LoginSessionType,
relays?: Record<string, RelaySettings>,
remoteSignerRelays?: Array<string>,
privateKey?: string,
privateKey?: PinEncrypted,
) {
if (this.#accounts.has(key)) {
throw new Error("Already logged in with this pubkey");
@ -113,7 +113,7 @@ export class MultiAccountStore extends ExternalStore<LoginSession> {
},
preferences: deepClone(DefaultPreferences),
remoteSignerRelays,
privateKey,
privateKeyData: privateKey,
} as LoginSession;
const pub = createPublisher(newSession);
@ -228,10 +228,15 @@ export class MultiAccountStore extends ExternalStore<LoginSession> {
if (!this.#activeAccount && this.#accounts.size > 0) {
this.#activeAccount = this.#accounts.keys().next().value;
}
const toSave = [...this.#accounts.values()];
for (const v of toSave) {
const toSave = [];
for (const v of this.#accounts.values()) {
if (v.privateKeyData instanceof PinEncrypted) {
v.privateKeyData = v.privateKeyData.toPayload();
toSave.push({
...v,
privateKeyData: v.privateKeyData.toPayload(),
});
} else {
toSave.push({ ...v });
}
}

View File

@ -9,7 +9,6 @@ import { bech32ToHex, getPublicKey, unwrap } from "SnortUtils";
import ZapButton from "Element/ZapButton";
import useImgProxy from "Hooks/useImgProxy";
import Icon from "Icons/Icon";
import useLogin from "Hooks/useLogin";
import { generateNewLogin, LoginSessionType, LoginStore } from "Login";
import AsyncButton from "Element/AsyncButton";
import useLoginHandler, { PinRequiredError } from "Hooks/useLoginHandler";
@ -76,7 +75,6 @@ export async function getNip05PubKey(addr: string): Promise<string> {
export default function LoginPage() {
const navigate = useNavigate();
const login = useLogin();
const [key, setKey] = useState("");
const [error, setError] = useState("");
const [pin, setPin] = useState(false);
@ -89,12 +87,6 @@ export default function LoginPage() {
const hasSubtleCrypto = window.crypto.subtle !== undefined;
const [nostrConnect, setNostrConnect] = useState("");
useEffect(() => {
if (login.publicKey) {
navigate("/");
}
}, [login, navigate]);
useEffect(() => {
const ret = unwrap(Artwork.at(Artwork.length * Math.random()));
const url = proxy(ret.link);
@ -161,18 +153,25 @@ export default function LoginPage() {
}
function nip46Buttons() {
return null;
return (
<>
<AsyncButton type="button" onClick={startNip46}>
<FormattedMessage defaultMessage="Nostr Connect (NIP-46)" description="Login button for NIP-46 signer app" />
<FormattedMessage defaultMessage="Nostr Connect" description="Login button for NIP-46 signer app" />
</AsyncButton>
{nostrConnect && (
<Modal id="nostr-connect" onClose={() => setNostrConnect("")}>
<div className="flex f-col">
<>
<h2>
<FormattedMessage defaultMessage="Nostr Connect" />
</h2>
<p>
<FormattedMessage defaultMessage="Scan this QR code with your signer app to get started" />
</p>
<div className="flex-column f-center g12">
<QrCode data={nostrConnect} />
<Copy text={nostrConnect} />
</div>
</>
</Modal>
)}
</>
@ -188,7 +187,7 @@ export default function LoginPage() {
<>
<AsyncButton type="button" onClick={doNip07Login}>
<FormattedMessage
defaultMessage="Login with Extension (NIP-07)"
defaultMessage="Nostr Extension"
description="Login button for NIP7 key manager extension"
/>
</AsyncButton>
@ -269,7 +268,7 @@ export default function LoginPage() {
<p dir="auto">
<FormattedMessage defaultMessage="Your key" description="Label for key input" />
</p>
<div className="flex">
<div className="flex f-center g8">
<input
dir="auto"
type={isMasking ? "password" : "text"}
@ -282,7 +281,7 @@ export default function LoginPage() {
<Icon
name={isMasking ? "openeye" : "closedeye"}
size={30}
className="highlight btn-sm pointer"
className="highlight pointer"
onClick={() => setMasking(!isMasking)}
/>
</div>

View File

@ -1,6 +1,6 @@
import "./Keys.css";
import { FormattedMessage } from "react-intl";
import { encodeTLV, NostrPrefix } from "@snort/system";
import { encodeTLV, NostrPrefix, PinEncrypted } from "@snort/system";
import Copy from "Element/Copy";
import useLogin from "Hooks/useLogin";
@ -8,7 +8,7 @@ import { hexToMnemonic } from "nip6";
import { hexToBech32 } from "SnortUtils";
export default function ExportKeys() {
const { publicKey, privateKey, generatedEntropy } = useLogin();
const { publicKey, privateKeyData, generatedEntropy } = useLogin();
return (
<div className="flex-column g12">
<h3>
@ -16,12 +16,12 @@ export default function ExportKeys() {
</h3>
<Copy text={hexToBech32("npub", publicKey ?? "")} className="dashed" />
<Copy text={encodeTLV(NostrPrefix.Profile, publicKey ?? "")} className="dashed" />
{privateKey && (
{privateKeyData instanceof PinEncrypted && (
<>
<h3>
<FormattedMessage defaultMessage="Private Key" />
</h3>
<Copy text={hexToBech32("nsec", privateKey)} className="dashed" />
<Copy text={hexToBech32("nsec", privateKeyData.value)} className="dashed" />
</>
)}
{generatedEntropy && (

View File

@ -8,16 +8,7 @@ import WasmPath from "@snort/system-query/pkg/system_query_bg.wasm";
import { StrictMode } from "react";
import * as ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import {
EventPublisher,
NostrSystem,
ProfileLoaderService,
Nip7Signer,
PowWorker,
QueryOptimizer,
FlatReqFilter,
ReqFilter,
} from "@snort/system";
import { NostrSystem, ProfileLoaderService, PowWorker, QueryOptimizer, FlatReqFilter, ReqFilter } from "@snort/system";
import { SnortContext } from "@snort/system-react";
import * as serviceWorkerRegistration from "serviceWorkerRegistration";
@ -71,13 +62,9 @@ export const System = new NostrSystem({
relayMetrics: RelayMetrics,
queryOptimizer: WasmQueryOptimizer,
authHandler: async (c, r) => {
const { publicKey, privateKey } = LoginStore.snapshot();
if (privateKey) {
const pub = EventPublisher.privateKey(privateKey);
return await pub.nip42Auth(c, r);
}
if (publicKey) {
const pub = new EventPublisher(new Nip7Signer(), publicKey);
const { id } = LoginStore.snapshot();
const pub = LoginStore.getPublisher(id);
if (pub) {
return await pub.nip42Auth(c, r);
}
},

View File

@ -99,9 +99,6 @@
"25V4l1": {
"defaultMessage": "Banner"
},
"27jzYm": {
"defaultMessage": "Enter pin to unlock private key"
},
"2IFGap": {
"defaultMessage": "Donate"
},
@ -145,6 +142,9 @@
"3yk8fB": {
"defaultMessage": "Wallet"
},
"40VR6s": {
"defaultMessage": "Nostr Connect"
},
"450Fty": {
"defaultMessage": "None"
},
@ -226,6 +226,9 @@
"89q5wc": {
"defaultMessage": "Confirm Reposts"
},
"8Kboo2": {
"defaultMessage": "Scan this QR code with your signer app to get started"
},
"8QDesP": {
"defaultMessage": "Zap {n} sats"
},
@ -241,6 +244,10 @@
"8v1NN+": {
"defaultMessage": "Pairing phrase"
},
"8xNnhi": {
"defaultMessage": "Nostr Extension",
"description": "Login button for NIP7 key manager extension"
},
"9+Ddtu": {
"defaultMessage": "Next"
},
@ -563,6 +570,10 @@
"LwYmVi": {
"defaultMessage": "Zaps on this note will be split to the following users."
},
"M10zFV": {
"defaultMessage": "Nostr Connect",
"description": "Login button for NIP-46 signer app"
},
"M3Oirc": {
"defaultMessage": "Debug Menus"
},
@ -779,10 +790,6 @@
"VnXp8Z": {
"defaultMessage": "Avatar"
},
"VtPV/B": {
"defaultMessage": "Login with Extension (NIP-07)",
"description": "Login button for NIP7 key manager extension"
},
"VvaJst": {
"defaultMessage": "View Wallets"
},
@ -940,6 +947,9 @@
"e61Jf3": {
"defaultMessage": "Coming soon"
},
"e7VmYP": {
"defaultMessage": "Enter pin to unlock your private key"
},
"e7qqly": {
"defaultMessage": "Mark All Read"
},
@ -1009,10 +1019,6 @@
"hMzcSq": {
"defaultMessage": "Messages"
},
"hWSp+B": {
"defaultMessage": "Nostr Connect (NIP-46)",
"description": "Login button for NIP-46 signer app"
},
"hY4lzx": {
"defaultMessage": "Supports"
},

View File

@ -32,7 +32,6 @@
"1udzha": "Conversations",
"2/2yg+": "Add",
"25V4l1": "Banner",
"27jzYm": "Enter pin to unlock private key",
"2IFGap": "Donate",
"2LbrkB": "Enter password",
"2a2YiP": "{n} Bookmarks",
@ -47,6 +46,7 @@
"3tVy+Z": "{n} Followers",
"3xCwbZ": "OR",
"3yk8fB": "Wallet",
"40VR6s": "Nostr Connect",
"450Fty": "None",
"47FYwb": "Cancel",
"4IPzdn": "Primary Developers",
@ -74,11 +74,13 @@
"7hp70g": "NIP-05",
"8/vBbP": "Reposts ({n})",
"89q5wc": "Confirm Reposts",
"8Kboo2": "Scan this QR code with your signer app to get started",
"8QDesP": "Zap {n} sats",
"8Rkoyb": "Recipient",
"8Y6bZQ": "Invalid zap split: {input}",
"8g2vyB": "name too long",
"8v1NN+": "Pairing phrase",
"8xNnhi": "Nostr Extension",
"9+Ddtu": "Next",
"9HU8vw": "Reply",
"9SvQep": "Follows {n}",
@ -185,6 +187,7 @@
"Lu5/Bj": "Open on Zapstr",
"Lw+I+J": "{n,plural,=0{{name} zapped} other{{name} & {n} others zapped}}",
"LwYmVi": "Zaps on this note will be split to the following users.",
"M10zFV": "Nostr Connect",
"M3Oirc": "Debug Menus",
"MBAYRO": "Shows \"Copy ID\" and \"Copy Event JSON\" in the context menu on each message",
"MI2jkA": "Not available:",
@ -255,7 +258,6 @@
"VR5eHw": "Public key (npub/nprofile)",
"VlJkSk": "{n} muted",
"VnXp8Z": "Avatar",
"VtPV/B": "Login with Extension (NIP-07)",
"VvaJst": "View Wallets",
"Vx7Zm2": "How do keys work?",
"W1yoZY": "It looks like you dont have any subscriptions, you can get one {link}",
@ -307,6 +309,7 @@
"d7d0/x": "LN Address",
"dOQCL8": "Display name",
"e61Jf3": "Coming soon",
"e7VmYP": "Enter pin to unlock your private key",
"e7qqly": "Mark All Read",
"eHAneD": "Reaction emoji",
"eJj8HD": "Get Verified",
@ -330,7 +333,6 @@
"h8XMJL": "Badges",
"hK5ZDk": "the world",
"hMzcSq": "Messages",
"hWSp+B": "Nostr Connect (NIP-46)",
"hY4lzx": "Supports",
"hicxcO": "Show replies",
"hmZ3Bz": "Media",