Add nip06 private key generation

This commit is contained in:
w3irdrobot
2023-03-10 14:02:43 -05:00
parent 1357b7a903
commit debc9566cc
10 changed files with 107 additions and 11 deletions

View File

@ -5,10 +5,13 @@ import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import * as secp from "@noble/secp256k1";
import { useIntl, FormattedMessage } from "react-intl";
import { HDKey } from "@scure/bip32";
import { wordlist } from "@scure/bip39/wordlists/english";
import * as bip39 from "@scure/bip39";
import { RootState } from "State/Store";
import { setPrivateKey, setPublicKey, setRelays, setGeneratedPrivateKey } from "State/Login";
import { DefaultRelays, EmailRegex } from "Const";
import { DefaultRelays, EmailRegex, DerivationPath } from "Const";
import { bech32ToHex, unwrap } from "Util";
import { HexKey } from "@snort/nostr";
import ZapButton from "Element/ZapButton";
@ -111,8 +114,18 @@ export default function LoginPage() {
}
async function makeRandomKey() {
const newKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
dispatch(setGeneratedPrivateKey(newKey));
const mn = bip39.generateMnemonic(wordlist);
const ent = bip39.mnemonicToEntropy(mn, wordlist);
const entHex = secp.utils.bytesToHex(ent);
const masterKey = HDKey.fromMasterSeed(ent);
const newKey = masterKey.derive(DerivationPath);
if (!newKey.privateKey) {
throw new Error("INVALID PRIVATE KEY DERIVATION");
}
const newKeyHex = secp.utils.bytesToHex(newKey.privateKey);
dispatch(setGeneratedPrivateKey({ key: newKeyHex, entropy: entHex }));
navigate("/new");
}

View File

@ -6,7 +6,7 @@ import Logo from "Element/Logo";
import { CollapsedSection } from "Element/Collapsed";
import Copy from "Element/Copy";
import { RootState } from "State/Store";
import { hexToBech32 } from "Util";
import { hexToBech32, hexToMnemonic } from "Util";
import messages from "./messages";
@ -68,7 +68,7 @@ const Extensions = () => {
};
export default function NewUserFlow() {
const { publicKey, privateKey } = useSelector((s: RootState) => s.login);
const { publicKey, privateKey, generatedEntropy } = useSelector((s: RootState) => s.login);
const navigate = useNavigate();
return (
@ -91,6 +91,10 @@ export default function NewUserFlow() {
<FormattedMessage {...messages.YourPrivkey} />
</h2>
<Copy text={hexToBech32("nsec", privateKey ?? "")} />
<h2>
<FormattedMessage {...messages.YourMnemonic} />
</h2>
<Copy text={hexToMnemonic(generatedEntropy ?? "")} />
<div className="next-actions">
<button type="button" onClick={() => navigate("/new/username")}>
<FormattedMessage {...messages.KeysSaved} />{" "}

View File

@ -10,6 +10,7 @@ export default defineMessages({
},
YourPubkey: { defaultMessage: "Your public key" },
YourPrivkey: { defaultMessage: "Your private key" },
YourMnemonic: { defaultMessage: "Your mnemonic phrase" },
KeysSaved: { defaultMessage: "I have saved my keys, continue" },
WhatIsSnort: { defaultMessage: "What is Snort and how does it work?" },
WhatIsSnortIntro: {