add utilities to convert nostr npubs to and from hex strings
This commit is contained in:
		
							parent
							
								
									4a177547fb
								
							
						
					
					
						commit
						3bbeb113a7
					
				
					 1 changed files with 12 additions and 0 deletions
				
			
		
							
								
								
									
										12
									
								
								src/nostr.ts
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								src/nostr.ts
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk";
 | 
			
		||||
import {generateSecretKey} from "nostr-tools";
 | 
			
		||||
import {randomTimeUpTo2DaysInThePast} from "./general.ts";
 | 
			
		||||
import {decode as nip19Decode, npubEncode} from "nostr-tools/nip19";
 | 
			
		||||
 | 
			
		||||
export async function encryptEventForRecipient(
 | 
			
		||||
  ndk: NDK,
 | 
			
		||||
| 
						 | 
				
			
			@ -25,3 +26,14 @@ export async function encryptEventForRecipient(
 | 
			
		|||
  giftWrap.ndk = ndk;
 | 
			
		||||
  return giftWrap;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function npubToPubKeyString(npub: string) {
 | 
			
		||||
  const decoded = nip19Decode(npub);
 | 
			
		||||
  if (decoded.type !== 'npub')
 | 
			
		||||
    throw new Error('Invalid npub');
 | 
			
		||||
  return decoded.data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function pubKeyStringToNpub(pubKey: string) {
 | 
			
		||||
  return npubEncode(pubKey);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue