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
|
2023-01-06 14:45:30 +00:00
|
|
|
* https://github.com/nostr-protocol/nips/blob/master/19.md
|
2023-01-06 14:36:13 +00:00
|
|
|
* @param {string} id bech32 id
|
|
|
|
*/
|
|
|
|
export function parseId(id) {
|
2023-01-06 14:45:30 +00:00
|
|
|
const hrp = ["note", "npub", "nsec"];
|
2023-01-06 14:36:13 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2023-01-07 20:54:12 +00:00
|
|
|
/**
|
|
|
|
* Decode bech32 to string UTF-8
|
|
|
|
* @param {string} str bech32 encoded string
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function bech32ToText(str) {
|
|
|
|
let decoded = bech32.decode(str, 1000);
|
|
|
|
let buf = bech32.fromWords(decoded.words);
|
|
|
|
return new TextDecoder().decode(Uint8Array.from(buf));
|
|
|
|
}
|
|
|
|
|
2023-01-06 14:36:13 +00:00
|
|
|
/**
|
|
|
|
* Convert hex note id to bech32 link url
|
|
|
|
* @param {string} hex
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function eventLink(hex) {
|
|
|
|
let buf = secp.utils.hexToBytes(hex);
|
2023-01-06 14:45:30 +00:00
|
|
|
return `/e/${bech32.encode("note", bech32.toWords(buf))}`;
|
2023-01-06 14:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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))}`;
|
|
|
|
}
|