nostrscript: add a helper function

This commit is contained in:
William Casarin
2023-07-03 16:59:50 -07:00
parent a6cbf50def
commit 30e33a01c1
9 changed files with 98 additions and 3 deletions

View File

@@ -281,6 +281,23 @@ func nscript_add_relay(script: NostrScript, relay: String) -> Bool {
}
@_cdecl("nscript_set_bool")
public func nscript_set_bool(interp: UnsafeMutablePointer<wasm_interp>?, setting: UnsafePointer<UInt16>, setting_len: Int32, val: Int32) -> Int32 {
guard let setting = asm_str(cstr: setting, len: setting_len),
UserSettingsStore.bool_options.contains(setting)
else {
stack_push_i32(interp, 0);
return 1;
}
let key = pk_setting_key(UserSettingsStore.pubkey ?? "", key: setting)
UserDefaults.standard.set(val > 0 ? true : false, forKey: key)
stack_push_i32(interp, 1);
return 1;
}
@_cdecl("nscript_pool_send_to")
public func nscript_pool_send_to(interp: UnsafeMutablePointer<wasm_interp>?, preq: UnsafePointer<UInt16>, req_len: Int32, to: UnsafePointer<UInt16>, to_len: Int32) -> Int32 {

View File

@@ -0,0 +1,18 @@
import * as nostr from './nostr'
export function go(): i32 {
var setting = "mny`or"
var new_setting = ""
for (let i = 0; i < setting.length; i++) {
new_setting += String.fromCharCode(setting.charCodeAt(i) + 1);
}
// this should fail
if (nostr.set_bool_setting("shmorg", true)) {
// you shouldn't be able to set settings that dont exist
return 0;
}
return nostr.set_bool_setting(new_setting, false)
}
go()

Binary file not shown.

View File

@@ -24,6 +24,7 @@ enum Command {
declare function nostr_log(log: string): void;
declare function nostr_cmd(cmd: i32, val: i32, len: i32): i32;
declare function nostr_pool_send_to(req: string, req_len: i32, to: string, to_len: i32): void;
declare function nostr_set_bool(key: string, key_len: i32, val: i32): i32;
export function pool_send(req: string): void {
nostr_cmd(Command.POOL_SEND, changetype<i32>(req), req.length)
@@ -52,6 +53,10 @@ export function event_get_note(ev: Event): Note {
return nostr_cmd(Command.EVENT_GET_NOTE, ev, 0)
}
export function set_bool_setting(setting: string, value: boolean): i32 {
return nostr_set_bool(setting, setting.length, value)
}
export function note_get_kind(note: Note): u32 {
if (!note) return 0;
return nostr_cmd(Command.NOTE_GET_KIND, note, 0);