bug: redirect tlv entry

This commit is contained in:
2023-02-16 16:32:56 +00:00
parent cafd820fd9
commit 3b68153105
3 changed files with 56 additions and 11 deletions

View File

@ -1,20 +1,42 @@
import { NostrPrefix } from "@snort/nostr";
import { decodeTLV, NostrPrefix, TLVEntryType } from "@snort/nostr";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { useNavigate, useParams } from "react-router-dom";
import { setRelays } from "State/Login";
import { eventLink, profileLink } from "Util";
export default function NostrLinkHandler() {
const params = useParams();
const dispatch = useDispatch();
const navigate = useNavigate();
const link = decodeURIComponent(params["*"] ?? "");
const link = decodeURIComponent(params["*"] ?? "").toLowerCase();
useEffect(() => {
if (link.length > 0) {
const ls = link.split(":");
const entity = ls[1];
if (entity.startsWith(NostrPrefix.PublicKey) || entity.startsWith(NostrPrefix.Profile)) {
const entity = link.startsWith("web+nostr:") ? link.split(":")[1] : link;
if (entity.startsWith(NostrPrefix.PublicKey)) {
navigate(`/p/${entity}`);
} else if (entity.startsWith(NostrPrefix.Event) || entity.startsWith(NostrPrefix.Note)) {
} else if (entity.startsWith(NostrPrefix.Note)) {
navigate(`/e/${entity}`);
} else if (entity.startsWith(NostrPrefix.Profile) || entity.startsWith(NostrPrefix.Event)) {
const decoded = decodeTLV(entity);
console.debug(decoded);
const id = decoded.find(a => a.type === TLVEntryType.Special)?.value as string;
const relays = decoded.filter(a => a.type === TLVEntryType.Relay);
if (relays.length > 0) {
const relayObj = {
relays: Object.fromEntries(relays.map(a => [a.value, { read: true, write: false }])),
createdAt: new Date().getTime(),
};
dispatch(setRelays(relayObj));
}
if (entity.startsWith(NostrPrefix.Profile)) {
navigate(profileLink(id));
} else if (entity.startsWith(NostrPrefix.Event)) {
navigate(eventLink(id));
}
}
}
}, [link]);

View File

@ -41,7 +41,7 @@ export function parseId(id: string) {
}
export function bech32ToHex(str: string) {
const nKey = bech32.decode(str);
const nKey = bech32.decode(str, 1_000);
const buff = bech32.fromWords(nKey.words);
return secp.utils.bytesToHex(Uint8Array.from(buff));
}