snort/src/Util.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-01-06 14:36:13 +00:00
import * as secp from "@noble/secp256k1";
import { bech32 } from "bech32";
2023-01-03 16:55:51 +00:00
export async function openFile() {
return new Promise((resolve, reject) => {
let elm = document.createElement("input");
elm.type = "file";
elm.onchange = (e) => {
resolve(e.target.files[0]);
};
elm.click();
});
}
2023-01-06 14:36:13 +00:00
/**
* Parse bech32 ids
* @param {string} id bech32 id
*/
export function parseId(id) {
const hrp = ["note1", "npub", "nsec"];
try {
if (hrp.some(a => id.startsWith(a))) {
return bech32ToHex(id);
}
} catch (e) { }
return id;
}
export function bech32ToHex(str) {
let nKey = bech32.decode(str);
let buff = bech32.fromWords(nKey.words);
return secp.utils.bytesToHex(Uint8Array.from(buff));
}
/**
* Convert hex note id to bech32 link url
* @param {string} hex
* @returns
*/
export function eventLink(hex) {
let buf = secp.utils.hexToBytes(hex);
return `/e/${bech32.encode("note1", bech32.toWords(buf))}`;
}
/**
* Convert hex pubkey to bech32 link url
* @param {string} hex
* @returns
*/
export function profileLink(hex) {
let buf = secp.utils.hexToBytes(hex);
return `/p/${bech32.encode("npub", bech32.toWords(buf))}`;
}