fix: nip6

closes #729
This commit is contained in:
kieran 2024-09-09 15:03:18 +01:00
parent 7822f6a62b
commit 40ffebb0c2
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
8 changed files with 83 additions and 26 deletions

View File

@ -7,8 +7,8 @@
"@noble/curves": "^1.4.0",
"@noble/hashes": "^1.4.0",
"@scure/base": "^1.1.6",
"@scure/bip32": "^1.4.0",
"@scure/bip39": "^1.3.0",
"@scure/bip32": "^1.5.0",
"@scure/bip39": "^1.4.0",
"@snort/shared": "workspace:*",
"@snort/system": "workspace:*",
"@snort/system-react": "workspace:*",

View File

@ -4,6 +4,7 @@
width: 210px;
background-color: var(--gray);
z-index: 2;
background-size: cover;
}
.avatar[data-domain="iris.to"],

View File

@ -33,9 +33,9 @@ export default function useLoginHandler() {
if (!hasSubtleCrypto) {
throw new Error(insecureMsg);
}
const ent = generateBip39Entropy(key);
const hexKey = entropyToPrivateKey(ent);
LoginStore.loginWithPrivateKey(await pin(hexKey));
const entropy = generateBip39Entropy(key);
const privKey = await entropyToPrivateKey(entropy);
LoginStore.loginWithPrivateKey(await pin(privKey));
return;
} else if (key.length === 64) {
if (!hasSubtleCrypto) {

View File

@ -6,7 +6,7 @@ import { FormattedMessage } from "react-intl";
import Copy from "@/Components/Copy/Copy";
import useLogin from "@/Hooks/useLogin";
import { hexToBech32 } from "@/Utils";
import { hexToMnemonic } from "@/Utils/nip6";
import { seedToMnemonic } from "@/Utils/nip6";
export default function ExportKeys() {
const { publicKey, privateKeyData, generatedEntropy } = useLogin();
@ -44,7 +44,7 @@ export default function ExportKeys() {
<FormattedMessage defaultMessage="Mnemonic" />
</div>
<div className="mnemonic-grid">
{hexToMnemonic(generatedEntropy ?? "")
{seedToMnemonic(generatedEntropy ?? "")
.split(" ")
.map((a, i) => (
<div key={a} className="flex items-center word">

View File

@ -49,11 +49,6 @@ export const DefaultImgProxy = {
salt: "a897770d9abf163de055e9617891214e75a9016d748f8ef865e6ffbcb9ed932295659549773a22a019a5f06d0b440c320be411e3fddfe784e199e4f03d74bd9b",
};
/**
* NIP06-defined derivation path for private keys
*/
export const DerivationPath = "m/44'/1237'/0'/0/0";
/**
* Blaster relays
*/

View File

@ -1,5 +1,6 @@
import * as utils from "@noble/curves/abstract/utils";
import * as secp from "@noble/curves/secp256k1";
import { bytesToHex } from "@noble/hashes/utils";
import { unixNowMs } from "@snort/shared";
import {
EventPublisher,
@ -47,9 +48,8 @@ export async function generateNewLogin(
pin: (key: string) => Promise<KeyStorage>,
profile: UserMetadata,
) {
const ent = generateBip39Entropy();
const entropy = utils.bytesToHex(ent);
const privateKey = entropyToPrivateKey(ent);
const entropy = generateBip39Entropy();
const privateKey = await entropyToPrivateKey(entropy);
const newRelays = {} as Record<string, RelaySettings>;
// Use current timezone info to determine approx location
@ -95,7 +95,7 @@ export async function generateNewLogin(
system.BroadcastEvent(ev3);
Promise.all(Blasters.map(a => system.WriteOnceToRelay(a, ev3)));
LoginStore.loginWithPrivateKey(await pin(privateKey), entropy, newRelays);
LoginStore.loginWithPrivateKey(await pin(privateKey), bytesToHex(entropy), newRelays);
}
export function generateRandomKey() {

View File

@ -1,11 +1,15 @@
import * as utils from "@noble/curves/abstract/utils";
import { bytesToHex } from "@noble/hashes/utils";
import { HDKey } from "@scure/bip32";
import * as bip39 from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
import { DerivationPath } from "@/Utils/Const";
/**
* NIP06-defined derivation path for private keys
*/
export const DerivationPath = "m/44'/1237'/0'/0/0";
export function generateBip39Entropy(mnemonic?: string): Uint8Array {
export function generateBip39Entropy(mnemonic?: string) {
try {
const mn = mnemonic ?? bip39.generateMnemonic(wordlist, 256);
return bip39.mnemonicToEntropy(mn, wordlist);
@ -15,9 +19,9 @@ export function generateBip39Entropy(mnemonic?: string): Uint8Array {
}
/**
* Convert hex-encoded entropy into mnemonic phrase
* Convert hex-encoded seed into mnemonic phrase
*/
export function hexToMnemonic(hex: string): string {
export function seedToMnemonic(hex: string) {
const bytes = utils.hexToBytes(hex);
return bip39.entropyToMnemonic(bytes, wordlist);
}
@ -25,8 +29,8 @@ export function hexToMnemonic(hex: string): string {
/**
* Derrive NIP-06 private key from master key
*/
export function entropyToPrivateKey(entropy: Uint8Array): string {
const masterKey = HDKey.fromMasterSeed(entropy);
export function seedToPrivateKey(seed: Uint8Array) {
const masterKey = HDKey.fromMasterSeed(seed);
const newKey = masterKey.derive(DerivationPath);
if (!newKey.privateKey) {
@ -35,3 +39,16 @@ export function entropyToPrivateKey(entropy: Uint8Array): string {
return utils.bytesToHex(newKey.privateKey);
}
export async function entropyToPrivateKey(entropy: Uint8Array) {
const mm = bip39.entropyToMnemonic(entropy, wordlist);
const seed = await bip39.mnemonicToSeed(mm);
const masterKey = HDKey.fromMasterSeed(seed);
const newKey = masterKey.derive(DerivationPath);
if (!newKey.privateKey) {
throw new Error("INVALID KEY DERIVATION");
}
return bytesToHex(newKey.privateKey);
}

View File

@ -4085,6 +4085,15 @@ __metadata:
languageName: node
linkType: hard
"@noble/curves@npm:~1.6.0":
version: 1.6.0
resolution: "@noble/curves@npm:1.6.0"
dependencies:
"@noble/hashes": "npm:1.5.0"
checksum: 10/9090b5a020b7e38c7b6d21506afaacd0c7557129d716a174334c1efc36385bf3ca6de16a543c216db58055e019c6a6c3bea8d9c0b79386e6bacff5c4c6b438a9
languageName: node
linkType: hard
"@noble/hashes@npm:1.3.1":
version: 1.3.1
resolution: "@noble/hashes@npm:1.3.1"
@ -4106,6 +4115,13 @@ __metadata:
languageName: node
linkType: hard
"@noble/hashes@npm:1.5.0, @noble/hashes@npm:~1.5.0":
version: 1.5.0
resolution: "@noble/hashes@npm:1.5.0"
checksum: 10/da7fc7af52af7afcf59810a7eea6155075464ff462ffda2572dc6d57d53e2669b1ea2ec774e814f6273f1697e567f28d36823776c9bf7068cba2a2855140f26e
languageName: node
linkType: hard
"@noble/hashes@npm:^1.3.3, @noble/hashes@npm:~1.3.1":
version: 1.3.3
resolution: "@noble/hashes@npm:1.3.3"
@ -4521,6 +4537,13 @@ __metadata:
languageName: node
linkType: hard
"@scure/base@npm:~1.1.7, @scure/base@npm:~1.1.8":
version: 1.1.8
resolution: "@scure/base@npm:1.1.8"
checksum: 10/5b764c0e98610bc4993479965db718457d91b68d3c6f1339e3cc74e53fc6b0ae0428d1d64d29a0de0cee9d966034674d4464fdbd2d1dbef27013927b2fe05c45
languageName: node
linkType: hard
"@scure/bip32@npm:1.3.1":
version: 1.3.1
resolution: "@scure/bip32@npm:1.3.1"
@ -4532,7 +4555,7 @@ __metadata:
languageName: node
linkType: hard
"@scure/bip32@npm:^1.3.3, @scure/bip32@npm:^1.4.0":
"@scure/bip32@npm:^1.3.3":
version: 1.4.0
resolution: "@scure/bip32@npm:1.4.0"
dependencies:
@ -4543,6 +4566,17 @@ __metadata:
languageName: node
linkType: hard
"@scure/bip32@npm:^1.5.0":
version: 1.5.0
resolution: "@scure/bip32@npm:1.5.0"
dependencies:
"@noble/curves": "npm:~1.6.0"
"@noble/hashes": "npm:~1.5.0"
"@scure/base": "npm:~1.1.7"
checksum: 10/17e296a782e09aec18ed27e2e8bb6a76072604c40997ec49a6840f223296421612dbe6b44275f04db9acd6da6cefb0322141110f5ac9dc686eb0c44d5bd868fa
languageName: node
linkType: hard
"@scure/bip39@npm:1.2.1":
version: 1.2.1
resolution: "@scure/bip39@npm:1.2.1"
@ -4553,7 +4587,7 @@ __metadata:
languageName: node
linkType: hard
"@scure/bip39@npm:^1.2.2, @scure/bip39@npm:^1.3.0":
"@scure/bip39@npm:^1.2.2":
version: 1.3.0
resolution: "@scure/bip39@npm:1.3.0"
dependencies:
@ -4563,6 +4597,16 @@ __metadata:
languageName: node
linkType: hard
"@scure/bip39@npm:^1.4.0":
version: 1.4.0
resolution: "@scure/bip39@npm:1.4.0"
dependencies:
"@noble/hashes": "npm:~1.5.0"
"@scure/base": "npm:~1.1.8"
checksum: 10/f86e0e79768c95bc684ed6de92892b1a6f228db0f8fab836f091c0ec0f6d1e291b8c4391cfbeaa9ea83f41045613535b1940cd10e7d780a5b73db163b1e7f151
languageName: node
linkType: hard
"@sinclair/typebox@npm:^0.27.8":
version: 0.27.8
resolution: "@sinclair/typebox@npm:0.27.8"
@ -4598,8 +4642,8 @@ __metadata:
"@noble/curves": "npm:^1.4.0"
"@noble/hashes": "npm:^1.4.0"
"@scure/base": "npm:^1.1.6"
"@scure/bip32": "npm:^1.4.0"
"@scure/bip39": "npm:^1.3.0"
"@scure/bip32": "npm:^1.5.0"
"@scure/bip39": "npm:^1.4.0"
"@snort/shared": "workspace:*"
"@snort/system": "workspace:*"
"@snort/system-react": "workspace:*"