bug: fix hmac for insecure contexts

This commit is contained in:
Kieran 2023-03-15 11:09:20 +00:00
parent 9f4d9bf117
commit b846c5720c
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 11 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import * as secp from "@noble/secp256k1";
import * as base64 from "@protobufjs/base64";
import { useSelector } from "react-redux";
import { RootState } from "State/Store";
import { unwrap } from "Util";
import { hmacSha256, unwrap } from "Util";
export interface ImgProxySettings {
url: string;
@ -19,7 +19,7 @@ export default function useImgProxy() {
}
async function signUrl(u: string) {
const result = await secp.utils.hmacSha256(
const result = await hmacSha256(
secp.utils.hexToBytes(unwrap(settings).key),
secp.utils.hexToBytes(unwrap(settings).salt),
te.encode(u)

View File

@ -1,5 +1,6 @@
import * as secp from "@noble/secp256k1";
import { sha256 as hash } from "@noble/hashes/sha256";
import { hmac } from "@noble/hashes/hmac";
import { bytesToHex } from "@noble/hashes/utils";
import { decode as invoiceDecode } from "light-bolt11-decoder";
import { bech32 } from "bech32";
@ -453,3 +454,11 @@ export function findTag(e: TaggedRawEvent, tag: string) {
});
return maybeTag && maybeTag[1];
}
export async function hmacSha256(key: Uint8Array, ...messages: Uint8Array[]) {
if (window.crypto.subtle) {
return await secp.utils.hmacSha256(key, ...messages);
} else {
return hmac(hash, key, secp.utils.concatBytes(...messages));
}
}