nostr.ts (2090B)
1 2 // these are handles not actual pointers 3 export type Note = i32; 4 export type Event = i32; 5 6 export enum EventType { 7 OK = 1, 8 NOTE = 2, 9 NOTICE = 3, 10 EOSE = 4 11 } 12 13 enum Command { 14 POOL_SEND = 1, 15 ADD_RELAY = 2, 16 EVENT_AWAIT = 3, 17 EVENT_GET_TYPE = 4, 18 EVENT_GET_NOTE = 5, 19 NOTE_GET_KIND = 6, 20 NOTE_GET_CONTENT = 7, 21 NOTE_GET_CONTENT_LENGTH = 8, 22 } 23 24 declare function nostr_log(log: string): void; 25 declare function nostr_cmd(cmd: i32, val: i32, len: i32): i32; 26 declare function nostr_pool_send_to(req: string, req_len: i32, to: string, to_len: i32): void; 27 declare function nostr_set_bool(key: string, key_len: i32, val: i32): i32; 28 29 export function pool_send(req: string): void { 30 nostr_cmd(Command.POOL_SEND, changetype<i32>(req), req.length) 31 } 32 33 export function pool_send_to(req: string, to: string): void { 34 return nostr_pool_send_to(req, req.length, to, to.length) 35 } 36 37 export function pool_add_relay(relay: string): boolean { 38 let ok = nostr_cmd(Command.ADD_RELAY, changetype<i32>(relay), relay.length) 39 return ok as boolean 40 } 41 42 export function event_await(subid: string): Event { 43 return nostr_cmd(Command.EVENT_AWAIT, changetype<i32>(subid), subid.length) as i32 44 } 45 46 export function event_get_type(ev: Event): EventType { 47 if (!ev) return 0; 48 return nostr_cmd(Command.EVENT_GET_TYPE, ev, 0) as EventType 49 } 50 51 export function event_get_note(ev: Event): Note { 52 if (!ev) return 0; 53 return nostr_cmd(Command.EVENT_GET_NOTE, ev, 0) 54 } 55 56 export function set_bool_setting(setting: string, value: boolean): i32 { 57 return nostr_set_bool(setting, setting.length, value) 58 } 59 60 export function note_get_kind(note: Note): u32 { 61 if (!note) return 0; 62 return nostr_cmd(Command.NOTE_GET_KIND, note, 0); 63 } 64 65 function note_get_content_length(): i32 { 66 return nostr_cmd(Command.NOTE_GET_CONTENT_LENGTH, note, 0) 67 } 68 69 export function log(log: string): void { 70 nostr_log(log) 71 } 72 73 export function note_get_content(): string { 74 let res = nostr_cmd(Command.NOTE_GET_CONTENT, note, 0); 75 if (!res) return ""; 76 77 let len = note_get_content_length() 78 let codes = TypedArray.wrap() 79 80 return String.fromCharCodes(codes) 81 } 82