bug: fix build for older browsers

This commit is contained in:
2023-05-17 12:59:30 +01:00
parent 6e16e7b5fb
commit e0251ab389
18 changed files with 104 additions and 73 deletions

View File

@ -3,14 +3,14 @@
"version": "0.1.8",
"private": true,
"dependencies": {
"@cashu/cashu-ts": "^0.6.1",
"@fortawesome/fontawesome-svg-core": "^6.2.1",
"@fortawesome/free-solid-svg-icons": "^6.2.1",
"@fortawesome/react-fontawesome": "^0.2.0",
"@cashu/cashu-ts": "^0.6.1",
"@jukben/emoji-search": "^2.0.1",
"@lightninglabs/lnc-web": "^0.2.3-alpha",
"@noble/curves": "^1.0.0",
"@noble/hashes": "^1.2.0",
"@noble/secp256k1": "^1.7.0",
"@protobufjs/base64": "^1.1.2",
"@reduxjs/toolkit": "^1.9.1",
"@scure/bip32": "^1.1.5",
@ -69,7 +69,11 @@
},
"browserslist": {
"production": [
">0.5%"
"chrome >= 67",
"edge >= 79",
"firefox >= 68",
"opera >= 54",
"safari >= 14"
],
"development": [
"last 1 chrome version",

View File

@ -1,4 +1,4 @@
import * as secp from "@noble/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import * as base64 from "@protobufjs/base64";
import { hmacSha256, unwrap } from "Util";
import useLogin from "Hooks/useLogin";
@ -19,8 +19,8 @@ export default function useImgProxy() {
function signUrl(u: string) {
const result = hmacSha256(
secp.utils.hexToBytes(unwrap(settings).key),
secp.utils.hexToBytes(unwrap(settings).salt),
utils.hexToBytes(unwrap(settings).key),
utils.hexToBytes(unwrap(settings).salt),
te.encode(u)
);
return urlSafe(base64.encode(result, 0, result.byteLength));

View File

@ -1,5 +1,4 @@
import { useIntl } from "react-intl";
import * as secp from "@noble/secp256k1";
import { EmailRegex, MnemonicRegex } from "Const";
import { LoginStore } from "Login";
@ -21,7 +20,7 @@ export default function useLoginHandler() {
throw new Error(insecureMsg);
}
const hexKey = bech32ToHex(key);
if (secp.utils.isValidPrivateKey(hexKey)) {
if (hexKey.length === 64) {
LoginStore.loginWithPrivateKey(hexKey);
} else {
throw new Error("INVALID PRIVATE KEY");
@ -39,7 +38,7 @@ export default function useLoginHandler() {
const ent = generateBip39Entropy(key);
const keyHex = entropyToPrivateKey(ent);
LoginStore.loginWithPrivateKey(keyHex);
} else if (secp.utils.isValidPrivateKey(key)) {
} else if (key.length === 64) {
if (!hasSubtleCrypto) {
throw new Error(insecureMsg);
}

View File

@ -1,5 +1,6 @@
import { HexKey, RelaySettings } from "@snort/nostr";
import * as secp from "@noble/secp256k1";
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { DefaultRelays, SnortPubKey } from "Const";
import { LoginStore, UserPreferences, LoginSession } from "Login";
@ -57,7 +58,7 @@ export function clearEntropy(state: LoginSession) {
*/
export async function generateNewLogin() {
const ent = generateBip39Entropy();
const entropy = secp.utils.bytesToHex(ent);
const entropy = utils.bytesToHex(ent);
const privateKey = entropyToPrivateKey(ent);
let newRelays: Record<string, RelaySettings> = {};
@ -76,7 +77,7 @@ export async function generateNewLogin() {
console.warn(e);
}
const publicKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(privateKey));
const publicKey = utils.bytesToHex(secp.schnorr.getPublicKey(privateKey));
const publisher = new EventPublisher(publicKey, privateKey);
const ev = await publisher.contactList([bech32ToHex(SnortPubKey), publicKey], newRelays);
publisher.broadcast(ev);
@ -85,8 +86,8 @@ export async function generateNewLogin() {
}
export function generateRandomKey() {
const privateKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
const publicKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(privateKey));
const privateKey = utils.bytesToHex(secp.schnorr.utils.randomPrivateKey());
const publicKey = utils.bytesToHex(secp.schnorr.getPublicKey(privateKey));
return {
privateKey,
publicKey,

View File

@ -1,4 +1,6 @@
import * as secp from "@noble/secp256k1";
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { HexKey, RelaySettings } from "@snort/nostr";
import { DefaultRelays } from "Const";
@ -113,7 +115,7 @@ export class MultiAccountStore extends ExternalStore<LoginSession> {
}
loginWithPrivateKey(key: HexKey, entropy?: string, relays?: Record<string, RelaySettings>) {
const pubKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(key));
const pubKey = utils.bytesToHex(secp.schnorr.getPublicKey(key));
if (this.#accounts.has(pubKey)) {
throw new Error("Already logged in with this pubkey");
}
@ -167,7 +169,7 @@ export class MultiAccountStore extends ExternalStore<LoginSession> {
const privKey = window.localStorage.getItem(LegacyKeys.PrivateKeyItem);
if (privKey) {
const pubKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
const pubKey = utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
this.#accounts.set(pubKey, {
...LoggedOut,
privateKey: privKey,

View File

@ -1,4 +1,5 @@
import * as secp from "@noble/secp256k1";
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { EventKind, HexKey, RawEvent, Tag } from "@snort/nostr";
import base64 from "@protobufjs/base64";
import { sha256, unixNow } from "Util";
@ -29,7 +30,7 @@ export abstract class EventExt {
e.id = this.createId(e);
const sig = await secp.schnorr.sign(e.id, key);
e.sig = secp.utils.bytesToHex(sig);
e.sig = utils.bytesToHex(sig);
if (!(await secp.schnorr.verify(e.sig, e.id, e.pubkey))) {
throw new Error("Signing failed");
}
@ -158,7 +159,7 @@ export abstract class EventExt {
}
static async #getDmSharedKey(pubkey: HexKey, privkey: HexKey) {
const sharedPoint = secp.getSharedSecret(privkey, "02" + pubkey);
const sharedPoint = secp.secp256k1.getSharedSecret(privkey, "02" + pubkey);
const sharedX = sharedPoint.slice(1, 33);
return await window.crypto.subtle.importKey("raw", sharedX, { name: "AES-CBC" }, false, ["encrypt", "decrypt"]);
}

View File

@ -1,4 +1,5 @@
import * as secp from "@noble/secp256k1";
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import {
EventKind,
FullRelaySettings,
@ -60,7 +61,7 @@ export class EventPublisher {
constructor(pubKey: string, privKey?: string) {
if (privKey) {
this.#privateKey = privKey;
this.#pubKey = secp.utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
this.#pubKey = utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
} else {
this.#pubKey = pubKey;
}

View File

@ -1,4 +1,4 @@
import * as secp from "@noble/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { EventKind } from "@snort/nostr";
import { FileExtensionRegex, VoidCatHost } from "Const";
import { EventPublisher } from "System/EventPublisher";
@ -25,7 +25,7 @@ export default async function VoidCat(
"Content-Type": "application/octet-stream",
"V-Content-Type": file.type,
"V-Filename": filename,
"V-Full-Digest": secp.utils.bytesToHex(new Uint8Array(digest)),
"V-Full-Digest": utils.bytesToHex(new Uint8Array(digest)),
"V-Description": "Upload from https://snort.social",
"V-Strip-Metadata": "true",
},

View File

@ -1,4 +1,5 @@
import * as secp from "@noble/secp256k1";
import * as secp from "@noble/curves/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import { sha256 as hash } from "@noble/hashes/sha256";
import { hmac } from "@noble/hashes/hmac";
import { bytesToHex } from "@noble/hashes/utils";
@ -19,11 +20,11 @@ import {
import { MetadataCache } from "Cache";
export const sha256 = (str: string | Uint8Array): u256 => {
return secp.utils.bytesToHex(hash(str));
return utils.bytesToHex(hash(str));
};
export function getPublicKey(privKey: HexKey) {
return secp.utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
return utils.bytesToHex(secp.schnorr.getPublicKey(privKey));
}
export async function openFile(): Promise<File | undefined> {
@ -63,7 +64,7 @@ export function bech32ToHex(str: string) {
try {
const nKey = bech32.decode(str, 1_000);
const buff = bech32.fromWords(nKey.words);
return secp.utils.bytesToHex(Uint8Array.from(buff));
return utils.bytesToHex(Uint8Array.from(buff));
} catch {
return str;
}
@ -116,7 +117,7 @@ export function hexToBech32(hrp: string, hex?: string) {
try {
if (hrp === NostrPrefix.Note || hrp === NostrPrefix.PrivateKey || hrp === NostrPrefix.PublicKey) {
const buf = secp.utils.hexToBytes(hex);
const buf = utils.hexToBytes(hex);
return bech32.encode(hrp, bech32.toWords(buf));
} else {
return encodeTLV(hrp as NostrPrefix, hex);
@ -488,7 +489,7 @@ export function findTag(e: RawEvent, tag: string) {
}
export function hmacSha256(key: Uint8Array, ...messages: Uint8Array[]) {
return hmac(hash, key, secp.utils.concatBytes(...messages));
return hmac(hash, key, utils.concatBytes(...messages));
}
export function getRelayName(url: string) {

View File

@ -1,4 +1,4 @@
import * as secp from "@noble/secp256k1";
import * as utils from "@noble/curves/abstract/utils";
import * as bip39 from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
import { HDKey } from "@scure/bip32";
@ -18,7 +18,7 @@ export function generateBip39Entropy(mnemonic?: string): Uint8Array {
* Convert hex-encoded entropy into mnemonic phrase
*/
export function hexToMnemonic(hex: string): string {
const bytes = secp.utils.hexToBytes(hex);
const bytes = utils.hexToBytes(hex);
return bip39.entropyToMnemonic(bytes, wordlist);
}
@ -33,5 +33,5 @@ export function entropyToPrivateKey(entropy: Uint8Array): string {
throw new Error("INVALID KEY DERIVATION");
}
return secp.utils.bytesToHex(newKey.privateKey);
return utils.bytesToHex(newKey.privateKey);
}