Fix NIP46 flow
This commit is contained in:
parent
01b3fd559b
commit
eef4270f84
@ -3,7 +3,7 @@ import "./LoginPage.css";
|
|||||||
import { CSSProperties, useEffect, useState } from "react";
|
import { CSSProperties, useEffect, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useIntl, FormattedMessage } from "react-intl";
|
import { useIntl, FormattedMessage } from "react-intl";
|
||||||
import { HexKey, Nip46Signer, PrivateKeySigner } from "@snort/system";
|
import { HexKey, Nip46Signer, PinEncrypted, PrivateKeySigner } from "@snort/system";
|
||||||
|
|
||||||
import { bech32ToHex, getPublicKey, unwrap } from "SnortUtils";
|
import { bech32ToHex, getPublicKey, unwrap } from "SnortUtils";
|
||||||
import ZapButton from "Element/ZapButton";
|
import ZapButton from "Element/ZapButton";
|
||||||
@ -76,6 +76,7 @@ export async function getNip05PubKey(addr: string): Promise<string> {
|
|||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [key, setKey] = useState("");
|
const [key, setKey] = useState("");
|
||||||
|
const [nip46Key, setNip46Key] = useState("");
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [pin, setPin] = useState(false);
|
const [pin, setPin] = useState(false);
|
||||||
const [art, setArt] = useState<ArtworkEntry>();
|
const [art, setArt] = useState<ArtworkEntry>();
|
||||||
@ -130,9 +131,10 @@ export default function LoginPage() {
|
|||||||
"getRelays" in unwrap(window.nostr) ? await unwrap(window.nostr?.getRelays).call(window.nostr) : undefined;
|
"getRelays" in unwrap(window.nostr) ? await unwrap(window.nostr?.getRelays).call(window.nostr) : undefined;
|
||||||
const pubKey = await unwrap(window.nostr).getPublicKey();
|
const pubKey = await unwrap(window.nostr).getPublicKey();
|
||||||
LoginStore.loginWithPubkey(pubKey, LoginSessionType.Nip7, relays);
|
LoginStore.loginWithPubkey(pubKey, LoginSessionType.Nip7, relays);
|
||||||
|
navigate("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startNip46() {
|
function generateNip46() {
|
||||||
const meta = {
|
const meta = {
|
||||||
name: "Snort",
|
name: "Snort",
|
||||||
url: window.location.href,
|
url: window.location.href,
|
||||||
@ -145,20 +147,30 @@ export default function LoginPage() {
|
|||||||
`metadata=${encodeURIComponent(JSON.stringify(meta))}`,
|
`metadata=${encodeURIComponent(JSON.stringify(meta))}`,
|
||||||
].join("&")}`;
|
].join("&")}`;
|
||||||
setNostrConnect(connectUrl);
|
setNostrConnect(connectUrl);
|
||||||
|
setNip46Key(newKey);
|
||||||
|
}
|
||||||
|
|
||||||
const signer = new Nip46Signer(connectUrl, new PrivateKeySigner(newKey));
|
async function startNip46(pin: string) {
|
||||||
|
if (!nostrConnect || !nip46Key) return;
|
||||||
|
|
||||||
|
const signer = new Nip46Signer(nostrConnect, new PrivateKeySigner(nip46Key));
|
||||||
await signer.init();
|
await signer.init();
|
||||||
await delay(500);
|
await delay(500);
|
||||||
await signer.describe();
|
await signer.describe();
|
||||||
|
LoginStore.loginWithPubkey(await signer.getPubKey(), LoginSessionType.Nip46, undefined, ["wss://relay.damus.io"], await PinEncrypted.create(nip46Key, pin));
|
||||||
|
navigate("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
function nip46Buttons() {
|
function nip46Buttons() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AsyncButton type="button" onClick={startNip46}>
|
<AsyncButton type="button" onClick={() => {
|
||||||
|
generateNip46();
|
||||||
|
setPin(true);
|
||||||
|
}}>
|
||||||
<FormattedMessage defaultMessage="Nostr Connect" description="Login button for NIP-46 signer app" />
|
<FormattedMessage defaultMessage="Nostr Connect" description="Login button for NIP-46 signer app" />
|
||||||
</AsyncButton>
|
</AsyncButton>
|
||||||
{nostrConnect && (
|
{nostrConnect && !pin && (
|
||||||
<Modal id="nostr-connect" onClose={() => setNostrConnect("")}>
|
<Modal id="nostr-connect" onClose={() => setNostrConnect("")}>
|
||||||
<>
|
<>
|
||||||
<h2>
|
<h2>
|
||||||
@ -307,8 +319,11 @@ export default function LoginPage() {
|
|||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
onResult={async pin => {
|
onResult={async pin => {
|
||||||
|
setPin(false);
|
||||||
if (key) {
|
if (key) {
|
||||||
await doLogin(pin);
|
await doLogin(pin);
|
||||||
|
} else if (nostrConnect) {
|
||||||
|
await startNip46(pin);
|
||||||
} else {
|
} else {
|
||||||
await makeRandomKey(pin);
|
await makeRandomKey(pin);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user