mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-20 06:54:20 +01:00
Add account lookup endpoint
This commit is contained in:
30
src/utils.ts
30
src/utils.ts
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user