NoteId.swift (1025B)
1 // 2 // NoteId.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-07-28. 6 // 7 8 import Foundation 9 10 typealias NoteKey = UInt64 11 12 struct NoteId: IdType, TagKey, TagConvertible { 13 let id: Data 14 15 init(_ data: Data) { 16 self.id = data 17 } 18 19 init?(hex: String) { 20 guard let note_id = hex_decode_noteid(hex) else { 21 return nil 22 } 23 self = note_id 24 } 25 26 var bech32: String { 27 bech32_note_id(self) 28 } 29 30 /// Refer to this NoteId as a QuoteId 31 var quote_id: QuoteId { 32 QuoteId(self.id) 33 } 34 35 var keychar: AsciiCharacter { "e" } 36 37 var tag: [String] { 38 ["e", self.hex()] 39 } 40 41 static func from_tag(tag: TagSequence) -> NoteId? { 42 var i = tag.makeIterator() 43 44 guard tag.count >= 2, 45 let t0 = i.next(), 46 let key = t0.single_char, 47 key == "e", 48 let t1 = i.next(), 49 let note_id = t1.id().map(NoteId.init) 50 else { return nil } 51 52 return note_id 53 } 54 }