references.ts (3860B)
1 import { decodePointer, naddrEncode } from 'applesauce-core/helpers/pointers'; 2 import { bech32 } from '@scure/base'; 3 import { canonicalPublicKey } from '../contracts/public-key.ts'; 4 5 const maximumRouteBytes = 2048; 6 const encoder = new TextEncoder(); 7 const strictIdentifier = new TextDecoder('utf-8', { 8 fatal: true, 9 ignoreBOM: true 10 }); 11 12 export type ProductCoordinate = Readonly<{ 13 kind: 30402; 14 pubkey: string; 15 identifier: string; 16 }>; 17 export type ProductReference = ProductCoordinate & Readonly<{ naddr: string }>; 18 19 function boundedText(value: unknown): value is string { 20 return ( 21 typeof value === 'string' && 22 value.length > 0 && 23 value.length <= maximumRouteBytes && 24 value.isWellFormed() && 25 encoder.encode(value).length <= maximumRouteBytes 26 ); 27 } 28 29 // Applesauce owns semantic pointer decoding. This bounded preflight checks the 30 // first identifier's raw bytes because its selected decoder replaces malformed 31 // UTF8 and removes one leading BOM. Unknown well-framed TLVs remain ignorable. 32 function identifierBytes(value: string): Uint8Array { 33 const { prefix, words } = bech32.decode(value, maximumRouteBytes); 34 if (prefix !== 'naddr') throw new Error('Unsupported reference'); 35 const bytes = Uint8Array.from(bech32.fromWords(words)); 36 let identifier: Uint8Array | undefined; 37 for (let offset = 0; offset < bytes.length;) { 38 if (offset + 2 > bytes.length) throw new Error('Truncated reference'); 39 const type = bytes[offset]; 40 const length = bytes[offset + 1]; 41 const end = offset + 2 + length; 42 if (end > bytes.length) throw new Error('Truncated reference'); 43 if (type === 0 && identifier === undefined) 44 identifier = bytes.slice(offset + 2, end); 45 offset = end; 46 } 47 if (identifier === undefined) throw new Error('Missing identifier'); 48 return identifier; 49 } 50 51 export function decodeProductReference( 52 value: unknown 53 ): ProductReference | undefined { 54 if ( 55 !boundedText(value) || 56 !/^[a-zA-Z0-9]+$/.test(value) || 57 (value !== value.toLowerCase() && value !== value.toUpperCase()) 58 ) 59 return undefined; 60 try { 61 const originalBytes = identifierBytes(value); 62 const identifier = strictIdentifier.decode(originalBytes); 63 const decoded = decodePointer(value); 64 if (decoded.type !== 'naddr' || decoded.data.kind !== 30402) 65 return undefined; 66 const pubkey = canonicalPublicKey(decoded.data.pubkey); 67 if (pubkey === undefined) return undefined; 68 // The only accepted mismatch is the inspected upstream decoder's removal 69 // of exactly the first leading BOM. Keep the byte-derived identifier. 70 const expected = identifier.startsWith('\ufeff') 71 ? identifier.slice(1) 72 : identifier; 73 if (decoded.data.identifier !== expected) return undefined; 74 const naddr = naddrEncode({ kind: 30402, pubkey, identifier }); 75 if (!boundedText(naddr)) return undefined; 76 const encodedBytes = identifierBytes(naddr); 77 if ( 78 encodedBytes.length !== originalBytes.length || 79 encodedBytes.some((byte, index) => byte !== originalBytes[index]) 80 ) 81 return undefined; 82 return { kind: 30402, pubkey, identifier, naddr }; 83 } catch { 84 return undefined; 85 } 86 } 87 88 export function encodeProductReference( 89 value: Readonly<{ kind: number; pubkey: string; identifier: string }> 90 ): string | undefined { 91 const { kind, pubkey, identifier } = value; 92 if ( 93 kind !== 30402 || 94 canonicalPublicKey(pubkey) === undefined || 95 typeof identifier !== 'string' || 96 !identifier.isWellFormed() || 97 identifier.length > 255 || 98 encoder.encode(identifier).length > 255 99 ) 100 return undefined; 101 try { 102 const encoded = naddrEncode({ kind: 30402, pubkey, identifier }); 103 const decoded = decodeProductReference(encoded); 104 return decoded?.identifier === identifier ? decoded.naddr : undefined; 105 } catch { 106 return undefined; 107 } 108 }