Merge pull request #425 from w3irdrobot/nip06

NIP06 support
This commit is contained in:
Kieran 2023-03-10 20:51:49 +00:00 committed by GitHub
commit 77b92eddf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 155 additions and 24 deletions

View File

@ -9,7 +9,7 @@ Snort supports the following NIP's:
- [ ] NIP-03: OpenTimestamps Attestations for Events
- [x] NIP-04: Encrypted Direct Message
- [x] NIP-05: Mapping Nostr keys to DNS-based internet identifiers
- [ ] NIP-06: Basic key derivation from mnemonic seed phrase
- [x] NIP-06: Basic key derivation from mnemonic seed phrase
- [x] NIP-07: `window.nostr` capability for web browsers
- [x] NIP-08: Handling Mentions
- [x] NIP-09: Event Deletion

View File

@ -12,6 +12,8 @@
"@noble/secp256k1": "^1.7.0",
"@protobufjs/base64": "^1.1.2",
"@reduxjs/toolkit": "^1.9.1",
"@scure/bip32": "^1.1.5",
"@scure/bip39": "^1.1.1",
"@snort/nostr": "^1.0.0",
"@szhsin/react-menu": "^3.3.1",
"base32-decode": "^1.0.0",

View File

@ -85,6 +85,11 @@ export const ZapperSpam = [
"e1ff3bfdd4e40315959b08b4fcc8245eaa514637e1d4ec2ae166b743341be1af", // benthecarman
];
/**
* NIP06-defined derivation path for private keys
*/
export const DerivationPath = "m/44'/1237'/0'/0/0";
/**
* Regex to match email address
*/
@ -92,6 +97,11 @@ export const EmailRegex =
// eslint-disable-next-line no-useless-escape
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
/**
* Regex to match a mnemonic seed
*/
export const MnemonicRegex = /^([^\s]+\s){11}[^\s]+$/;
/**
* Extract file extensions regex
*/

View File

@ -8,8 +8,8 @@ import { useIntl, FormattedMessage } from "react-intl";
import { RootState } from "State/Store";
import { setPrivateKey, setPublicKey, setRelays, setGeneratedPrivateKey } from "State/Login";
import { DefaultRelays, EmailRegex } from "Const";
import { bech32ToHex, unwrap } from "Util";
import { DefaultRelays, EmailRegex, MnemonicRegex } from "Const";
import { bech32ToHex, generateBip39Entropy, entropyToDerivedKey, unwrap } from "Util";
import { HexKey } from "@snort/nostr";
import ZapButton from "Element/ZapButton";
// import useImgProxy from "Feed/ImgProxy";
@ -97,12 +97,14 @@ export default function LoginPage() {
} else if (key.match(EmailRegex)) {
const hexKey = await getNip05PubKey(key);
dispatch(setPublicKey(hexKey));
} else if (key.match(MnemonicRegex)) {
const ent = generateBip39Entropy(key);
const keyHex = entropyToDerivedKey(ent);
dispatch(setPrivateKey(keyHex));
} else if (secp.utils.isValidPrivateKey(key)) {
dispatch(setPrivateKey(key));
} else {
if (secp.utils.isValidPrivateKey(key)) {
dispatch(setPrivateKey(key));
} else {
throw new Error("INVALID PRIVATE KEY");
}
throw new Error("INVALID PRIVATE KEY");
}
} catch (e) {
setError(`Failed to load NIP-05 pub key (${e})`);
@ -111,8 +113,10 @@ export default function LoginPage() {
}
async function makeRandomKey() {
const newKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
dispatch(setGeneratedPrivateKey(newKey));
const ent = generateBip39Entropy();
const entHex = secp.utils.bytesToHex(ent);
const newKeyHex = entropyToDerivedKey(ent);
dispatch(setGeneratedPrivateKey({ key: newKeyHex, entropy: entHex }));
navigate("/new");
}

View File

@ -46,5 +46,5 @@ export default defineMessages({
},
Bookmarks: { defaultMessage: "Bookmarks" },
BookmarksCount: { defaultMessage: "{n} Bookmarks" },
KeyPlaceholder: { defaultMessage: "nsec, npub, nip-05, hex" },
KeyPlaceholder: { defaultMessage: "nsec, npub, nip-05, hex, mnemonic" },
});

View File

@ -1,19 +1,27 @@
import { useIntl, FormattedMessage } from "react-intl";
import { useDispatch } from "react-redux";
import { useNavigate, Link } from "react-router-dom";
import { RecommendedFollows } from "Const";
import Logo from "Element/Logo";
import FollowListBase from "Element/FollowListBase";
import { useMemo } from "react";
import { clearEntropy } from "State/Login";
import messages from "./messages";
export default function DiscoverFollows() {
const { formatMessage } = useIntl();
const dispatch = useDispatch();
const navigate = useNavigate();
const sortedReccomends = useMemo(() => {
return RecommendedFollows.sort(() => (Math.random() >= 0.5 ? -1 : 1));
}, []);
async function clearEntropyAndGo() {
dispatch(clearEntropy());
navigate("/");
}
return (
<div className="main-content new-user" dir="auto">
<Logo />
@ -27,7 +35,7 @@ export default function DiscoverFollows() {
<FormattedMessage {...messages.Share} values={{ link: <Link to="/">{formatMessage(messages.World)}</Link> }} />
</p>
<div className="next-actions continue-actions">
<button type="button" onClick={() => navigate("/")}>
<button type="button" onClick={() => clearEntropyAndGo()}>
<FormattedMessage {...messages.Done} />{" "}
</button>
</div>

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: {

View File

@ -98,6 +98,11 @@ export interface LoginStore {
*/
privateKey?: HexKey;
/**
* BIP39-generated, hex-encoded entropy
*/
generatedEntropy?: string;
/**
* Current users public key
*/
@ -253,6 +258,11 @@ export interface SetFollowsPayload {
createdAt: number;
}
export interface SetGeneratedKeyPayload {
key: HexKey;
entropy: HexKey;
}
export const ReadPreferences = () => {
const pref = window.localStorage.getItem(UserPreferencesKey);
if (pref) {
@ -315,12 +325,16 @@ const LoginSlice = createSlice({
window.localStorage.setItem(PrivateKeyItem, action.payload);
state.publicKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(action.payload));
},
setGeneratedPrivateKey: (state, action: PayloadAction<HexKey>) => {
setGeneratedPrivateKey: (state, action: PayloadAction<SetGeneratedKeyPayload>) => {
state.loggedOut = false;
state.newUserKey = true;
state.privateKey = action.payload;
window.localStorage.setItem(PrivateKeyItem, action.payload);
state.publicKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(action.payload));
state.privateKey = action.payload.key;
state.generatedEntropy = action.payload.entropy;
window.localStorage.setItem(PrivateKeyItem, action.payload.key);
state.publicKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(action.payload.key));
},
clearEntropy: state => {
state.generatedEntropy = undefined;
},
setPublicKey: (state, action: PayloadAction<HexKey>) => {
window.localStorage.setItem(PublicKeyItem, action.payload);
@ -468,6 +482,7 @@ export const {
init,
setPrivateKey,
setGeneratedPrivateKey,
clearEntropy,
setPublicKey,
setRelays,
removeRelay,

View File

@ -5,7 +5,11 @@ import { decode as invoiceDecode } from "light-bolt11-decoder";
import { bech32 } from "bech32";
import base32Decode from "base32-decode";
import { HexKey, TaggedRawEvent, u256, EventKind, encodeTLV, NostrPrefix } from "@snort/nostr";
import * as bip39 from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
import { HDKey } from "@scure/bip32";
import { DerivationPath } from "Const";
import { MetadataCache } from "State/Users";
export const sha256 = (str: string) => {
@ -100,6 +104,39 @@ export function hexToBech32(hrp: string, hex?: string) {
}
}
export function generateBip39Entropy(mnemonic?: string): Uint8Array {
try {
const mn = mnemonic ?? bip39.generateMnemonic(wordlist);
return bip39.mnemonicToEntropy(mn, wordlist);
} catch (e) {
throw new Error("INVALID MNEMONIC PHRASE");
}
}
/**
* Convert hex-encoded entropy into mnemonic phrase
*/
export function hexToMnemonic(hex: string): string {
const bytes = secp.utils.hexToBytes(hex);
return bip39.entropyToMnemonic(bytes, wordlist);
}
/**
* Convert mnemonic phrase into hex-encoded private key
* using the derivation path specified in NIP06
* @param mnemonic the mnemonic-encoded entropy
*/
export function entropyToDerivedKey(entropy: Uint8Array): string {
const masterKey = HDKey.fromMasterSeed(entropy);
const newKey = masterKey.derive(DerivationPath);
if (!newKey.privateKey) {
throw new Error("INVALID KEY DERIVATION");
}
return secp.utils.bytesToHex(newKey.privateKey);
}
/**
* Convert hex pubkey to bech32 link url
*/

View File

@ -197,9 +197,6 @@
"B6+XJy": {
"defaultMessage": "zapped"
},
"B6H7eJ": {
"defaultMessage": "nsec, npub, nip-05, hex"
},
"BOUMjw": {
"defaultMessage": "No nostr users found for {twitterUsername}"
},
@ -312,6 +309,9 @@
"IEwZvs": {
"defaultMessage": "Are you sure you want to unpin this note?"
},
"IKOPx/": {
"defaultMessage": "Donate Page"
},
"INSqIz": {
"defaultMessage": "Twitter username..."
},
@ -327,6 +327,9 @@
"JHEHCk": {
"defaultMessage": "Zaps ({n})"
},
"JXtsQW": {
"defaultMessage": "Fast Zap Donation"
},
"JkLHGw": {
"defaultMessage": "Website"
},
@ -348,6 +351,9 @@
"KahimY": {
"defaultMessage": "Unknown event kind: {kind}"
},
"L7SZPr": {
"defaultMessage": "For more information about donations see {link}."
},
"LXxsbk": {
"defaultMessage": "Anonymous"
},
@ -410,6 +416,9 @@
"PCSt5T": {
"defaultMessage": "Preferences"
},
"PLSbmL": {
"defaultMessage": "Your mnemonic phrase"
},
"Pe0ogR": {
"defaultMessage": "Theme"
},
@ -439,6 +448,9 @@
"RahCRH": {
"defaultMessage": "Expired"
},
"RfhLwC": {
"defaultMessage": "By: {author}"
},
"RhDAoS": {
"defaultMessage": "Are you sure you want to delete {id}"
},
@ -492,6 +504,9 @@
"WxthCV": {
"defaultMessage": "e.g. Jack"
},
"X7xU8J": {
"defaultMessage": "nsec, npub, nip-05, hex, mnemonic"
},
"XgWvGA": {
"defaultMessage": "Reactions"
},
@ -599,6 +614,9 @@
"gjBiyj": {
"defaultMessage": "Loading..."
},
"h8XMJL": {
"defaultMessage": "Badges"
},
"hCUivF": {
"defaultMessage": "Notes will stream in real time into global and posts tab"
},
@ -782,6 +800,9 @@
"rudscU": {
"defaultMessage": "Failed to load follows, please try again later"
},
"sBz4+I": {
"defaultMessage": "For each Fast Zap an additional {percentage}% ({amount} sats) of the zap amount will be sent to the Snort developers as a donation."
},
"sWnYKw": {
"defaultMessage": "Snort is designed to have a similar experience to Twitter."
},

View File

@ -63,7 +63,6 @@
"AyGauy": "Login",
"B4C47Y": "name too short",
"B6+XJy": "zapped",
"B6H7eJ": "nsec, npub, nip-05, hex",
"BOUMjw": "No nostr users found for {twitterUsername}",
"BOr9z/": "Snort is an open source project built by passionate people in their free time",
"BcGMo+": "Notes hold text content, the most popular usage of these notes is to store \"tweet like\" messages.",
@ -101,11 +100,13 @@
"HOzFdo": "Muted",
"HbefNb": "Open Wallet",
"IEwZvs": "Are you sure you want to unpin this note?",
"IKOPx/": "Donate Page",
"INSqIz": "Twitter username...",
"IUZC+0": "This means that nobody can modify notes which you have created and everybody can easily verify that the notes they are reading are created by you.",
"Iwm6o2": "NIP-05 Shop",
"JCIgkj": "Username",
"JHEHCk": "Zaps ({n})",
"JXtsQW": "Fast Zap Donation",
"JkLHGw": "Website",
"K3r6DQ": "Delete",
"K7AkdL": "Show",
@ -113,6 +114,7 @@
"KQvWvD": "Deleted",
"KWuDfz": "I have saved my keys, continue",
"KahimY": "Unknown event kind: {kind}",
"L7SZPr": "For more information about donations see {link}.",
"LXxsbk": "Anonymous",
"LgbKvU": "Comment",
"LxY9tW": "Generate Key",
@ -133,6 +135,7 @@
"P7FD0F": "System (Default)",
"P7nJT9": "Total today (UTC): {amount} sats",
"PCSt5T": "Preferences",
"PLSbmL": "Your mnemonic phrase",
"Pe0ogR": "Theme",
"PrsIg7": "Reactions will be shown on every page, if disabled no reactions will be shown",
"QDFTjG": "{n} Relays",
@ -142,6 +145,7 @@
"R2OqnW": "Delete Account",
"RDZVQL": "Check",
"RahCRH": "Expired",
"RfhLwC": "By: {author}",
"RhDAoS": "Are you sure you want to delete {id}",
"RoOyAh": "Relays",
"Rs4kCE": "Bookmark",
@ -159,6 +163,7 @@
"W9355R": "Unmute",
"WONP5O": "Find your twitter follows on nostr (Data provided by {provider})",
"WxthCV": "e.g. Jack",
"X7xU8J": "nsec, npub, nip-05, hex, mnemonic",
"XgWvGA": "Reactions",
"Y31HTH": "Help fund the development of Snort",
"YDURw6": "Service URL",
@ -194,6 +199,7 @@
"gDZkld": "Snort is a Nostr UI, nostr is a decentralised protocol for saving and distributing \"notes\".",
"gDzDRs": "Emoji to send when reactiong to a note",
"gjBiyj": "Loading...",
"h8XMJL": "Badges",
"hCUivF": "Notes will stream in real time into global and posts tab",
"hK5ZDk": "the world",
"hMzcSq": "Messages",
@ -254,6 +260,7 @@
"rfuMjE": "(Default)",
"rrfdTe": "This is the same technology which is used by Bitcoin and has been proven to be extremely secure.",
"rudscU": "Failed to load follows, please try again later",
"sBz4+I": "For each Fast Zap an additional {percentage}% ({amount} sats) of the zap amount will be sent to the Snort developers as a donation.",
"sWnYKw": "Snort is designed to have a similar experience to Twitter.",
"svOoEH": "Name-squatting and impersonation is not allowed. Snort and our partners reserve the right to terminate your handle (not your account - nobody can take that away) for violating this rule.",
"tOdNiY": "Dark",

View File

@ -1681,12 +1681,12 @@
dependencies:
eslint-scope "5.1.1"
"@noble/hashes@^1.2.0":
"@noble/hashes@^1.2.0", "@noble/hashes@~1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.2.0.tgz#a3150eeb09cc7ab207ebf6d7b9ad311a9bdbed12"
integrity sha512-FZfhjEDbT5GRswV3C6uvLPHMiVD6lQBmpoX5+eSiPaMTXte/IKqI5dykDxzZB/WBeK/CDuQRBWarPdi3FNY2zQ==
"@noble/secp256k1@^1.7.0", "@noble/secp256k1@^1.7.1":
"@noble/secp256k1@^1.7.0", "@noble/secp256k1@^1.7.1", "@noble/secp256k1@~1.7.0":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.7.1.tgz#b251c70f824ce3ca7f8dc3df08d58f005cc0507c"
integrity sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw==
@ -1789,6 +1789,28 @@
resolved "https://registry.yarnpkg.com/@rushstack/eslint-patch/-/eslint-patch-1.2.0.tgz#8be36a1f66f3265389e90b5f9c9962146758f728"
integrity sha512-sXo/qW2/pAcmT43VoRKOJbDOfV3cYpq3szSVfIThQXNt+E4DfKj361vaAt3c88U5tPUxzEswam7GW48PJqtKAg==
"@scure/base@~1.1.0":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==
"@scure/bip32@^1.1.5":
version "1.1.5"
resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-1.1.5.tgz#d2ccae16dcc2e75bc1d75f5ef3c66a338d1ba300"
integrity sha512-XyNh1rB0SkEqd3tXcXMi+Xe1fvg+kUIcoRIEujP1Jgv7DqW2r9lg3Ah0NkFaCs9sTkQAQA8kw7xiRXzENi9Rtw==
dependencies:
"@noble/hashes" "~1.2.0"
"@noble/secp256k1" "~1.7.0"
"@scure/base" "~1.1.0"
"@scure/bip39@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.1.1.tgz#b54557b2e86214319405db819c4b6a370cf340c5"
integrity sha512-t+wDck2rVkh65Hmv280fYdVdY25J9YeEUIgn2LG1WM6gxFkGzcksoDiUkWVpVp3Oex9xGC68JU2dSbUfwZ2jPg==
dependencies:
"@noble/hashes" "~1.2.0"
"@scure/base" "~1.1.0"
"@sinclair/typebox@^0.24.1":
version "0.24.51"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f"