Add account lookup endpoint

This commit is contained in:
Alex Gleason
2023-04-29 16:23:23 -05:00
parent 5118aaf46c
commit 9e73a8e43c
3 changed files with 72 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import { Context, getPublicKey } from '@/deps.ts';
import { Context, getPublicKey, nip19, nip21 } from '@/deps.ts';
import { type Event } from '@/event.ts';
/** Get the current time in Nostr format. */
@@ -21,4 +21,30 @@ function getKeys(c: Context) {
}
}
export { eventDateComparator, getKeys, nostrNow };
/** Return true if the value is a bech32 string, eg for use with NIP-19. */
function isBech32(value: unknown): value is string {
return typeof value === 'string' && nip21.BECH32_REGEX.test(value);
}
/** Return true if the value is a Nostr pubkey, private key, or event ID. */
function isNostrId(value: unknown): value is string {
return typeof value === 'string' && /^[0-9a-f]{64}$/.test(value);
}
/** Get pubkey from bech32 string, if applicable. */
function bech32ToPubkey(bech32: string): string | undefined {
try {
const decoded = nip19.decode(bech32);
switch (decoded.type) {
case 'nprofile':
return decoded.data.pubkey;
case 'npub':
return decoded.data;
}
} catch (_) {
//
}
}
export { bech32ToPubkey, eventDateComparator, getKeys, isBech32, isNostrId, nostrNow };