bug: fix build

This commit is contained in:
Kieran 2023-02-14 10:34:43 +00:00
parent 36dc9f6543
commit 042726525c
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
4 changed files with 59 additions and 58 deletions

View File

@ -21,6 +21,7 @@ Snort supports the following NIP's:
- [x] NIP-15: End of Stored Events Notice
- [x] NIP-19: bech32-encoded entities
- [x] NIP-20: Command Results
- [x] NIP-21: `nostr:` Protocol handler (`web+nostr`)
- [x] NIP-25: Reactions
- [x] NIP-26: Delegated Event Signing (Display delegated signings only)
- [ ] NIP-28: Public Chat

View File

@ -2,55 +2,58 @@ import * as secp from "@noble/secp256k1";
import { bech32 } from "bech32";
export enum NostrPrefix {
PublicKey = "npub",
PrivateKey = "nsec",
Note = "note",
PublicKey = "npub",
PrivateKey = "nsec",
Note = "note",
// TLV prefixes
Profile = "nprofile",
Event = "nevent",
Relay = "nrelay"
// TLV prefixes
Profile = "nprofile",
Event = "nevent",
Relay = "nrelay",
}
export interface TLVEntry {
type: number,
length: number,
value: string // hex encoded data
type: number;
length: number;
value: string; // hex encoded data
}
export function encodeTLV(hex: string, prefix: NostrPrefix, relays?: string[]) {
if (typeof hex !== "string" || hex.length === 0 || hex.length % 2 !== 0) {
return "";
}
if (typeof hex !== "string" || hex.length === 0 || hex.length % 2 !== 0) {
return "";
}
let enc = new TextEncoder();
let buf = secp.utils.hexToBytes(hex);
const enc = new TextEncoder();
const buf = secp.utils.hexToBytes(hex);
let tl0 = [0, buf.length, ...buf];
let tl1 = relays?.map(a => {
let data = enc.encode(a);
const tl0 = [0, buf.length, ...buf];
const tl1 =
relays
?.map(a => {
const data = enc.encode(a);
return [1, data.length, ...data];
}).flat() ?? [];
})
.flat() ?? [];
return bech32.encode(prefix, bech32.toWords([...tl0, ...tl1]));
return bech32.encode(prefix, bech32.toWords([...tl0, ...tl1]));
}
export function decodeTLV(str: string) {
let decoded = bech32.decode(str);
let data = bech32.fromWords(decoded.words);
const decoded = bech32.decode(str);
const data = bech32.fromWords(decoded.words);
let entries: TLVEntry[] = [];
let x = 0;
while (x < data.length) {
let t = data[x];
let l = data[x + 1];
let v = data.slice(x + 2, x + 2 + l);
entries.push({
type: t,
length: l,
value: secp.utils.bytesToHex(new Uint8Array(v))
});
x += 2 + l;
}
return entries;
}
const entries: TLVEntry[] = [];
let x = 0;
while (x < data.length) {
const t = data[x];
const l = data[x + 1];
const v = data.slice(x + 2, x + 2 + l);
entries.push({
type: t,
length: l,
value: secp.utils.bytesToHex(new Uint8Array(v)),
});
x += 2 + l;
}
return entries;
}

View File

@ -1,26 +1,23 @@
import { NostrPrefix } from "Nostr/Links";
import { useEffect } from "react";
import { redirect, useNavigate, useParams } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
export default function NostrLinkHandler() {
const params = useParams();
const navigate = useNavigate();
const link = decodeURIComponent(params["*"] ?? "");
const params = useParams();
const navigate = useNavigate();
const link = decodeURIComponent(params["*"] ?? "");
useEffect(() => {
if (link.length > 0) {
let ls = link.split(":");
let entity = ls[1];
if (entity.startsWith(NostrPrefix.PublicKey) || entity.startsWith(NostrPrefix.Profile)) {
navigate(`/p/${entity}`);
}
else if (entity.startsWith(NostrPrefix.Event) || entity.startsWith(NostrPrefix.Note)) {
navigate(`/e/${entity}`)
}
}
}, [link]);
useEffect(() => {
if (link.length > 0) {
const ls = link.split(":");
const entity = ls[1];
if (entity.startsWith(NostrPrefix.PublicKey) || entity.startsWith(NostrPrefix.Profile)) {
navigate(`/p/${entity}`);
} else if (entity.startsWith(NostrPrefix.Event) || entity.startsWith(NostrPrefix.Note)) {
navigate(`/e/${entity}`);
}
}
}, [link]);
return (
<>Could not handle {link}</>
)
}
return <>Could not handle {link}</>;
}

View File

@ -79,7 +79,7 @@ export function hexToBech32(hrp: string, hex?: string) {
try {
if (hrp === NostrPrefix.Note || hrp === NostrPrefix.PrivateKey || hrp === NostrPrefix.PublicKey) {
let buf = secp.utils.hexToBytes(hex);
const buf = secp.utils.hexToBytes(hex);
return bech32.encode(hrp, bech32.toWords(buf));
} else {
return encodeTLV(hex, hrp as NostrPrefix);