signer_wasm.mx raw

   1  //go:build wasm
   2  //:build wasm
   3  
   4  package signer
   5  
   6  import (
   7  	"git.smesh.lol/smesh/web/common/jsbridge/callback"
   8  	"unsafe"
   9  )
  10  
  11  //:wasmimport bridge signer_has_signer
  12  func wasmHasSigner() (n int32)
  13  
  14  //:wasmimport bridge signer_has_mls
  15  func wasmHasMLS() (n int32)
  16  
  17  //:wasmimport bridge signer_get_public_key
  18  func wasmGetPublicKey(cbID int32)
  19  
  20  //:wasmimport bridge signer_sign_event
  21  func wasmSignEvent(ptr *byte, len int32, cap int32, cbID int32)
  22  
  23  //:wasmimport bridge signer_get_shared_secret
  24  func wasmGetSharedSecret(ptr *byte, len int32, cap int32, cbID int32)
  25  
  26  //:wasmimport bridge signer_nip44_decrypt
  27  func wasmNip44Decrypt(pkPtr *byte, pkLen int32, pkCap int32, ctPtr *byte, ctLen int32, ctCap int32, cbID int32)
  28  
  29  //:wasmimport bridge signer_nip44_encrypt
  30  func wasmNip44Encrypt(pkPtr *byte, pkLen int32, pkCap int32, ptPtr *byte, ptLen int32, ptCap int32, cbID int32)
  31  
  32  //:wasmimport bridge signer_is_installed
  33  func wasmIsInstalled(cbID int32)
  34  
  35  //:wasmimport bridge signer_get_vault_status
  36  func wasmGetVaultStatus(cbID int32)
  37  
  38  //:wasmimport bridge signer_create_vault
  39  func wasmCreateVault(ptr *byte, len int32, cap int32, cbID int32)
  40  
  41  //:wasmimport bridge signer_unlock_vault
  42  func wasmUnlockVault(ptr *byte, len int32, cap int32, cbID int32)
  43  
  44  //:wasmimport bridge signer_last_unlock_error
  45  func wasmLastUnlockError(cbID int32)
  46  
  47  //:wasmimport bridge signer_lock_vault
  48  func wasmLockVault(cbID int32)
  49  
  50  //:wasmimport bridge signer_list_identities
  51  func wasmListIdentities(cbID int32)
  52  
  53  //:wasmimport bridge signer_add_identity
  54  func wasmAddIdentity(ptr *byte, len int32, cap int32, cbID int32)
  55  
  56  //:wasmimport bridge signer_nsec_login
  57  func wasmNsecLogin(ptr *byte, len int32, cap int32, cbID int32)
  58  
  59  //:wasmimport bridge signer_switch_identity
  60  func wasmSwitchIdentity(ptr *byte, len int32, cap int32, cbID int32)
  61  
  62  //:wasmimport bridge signer_export_vault
  63  func wasmExportVault(ptr *byte, len int32, cap int32, cbID int32)
  64  
  65  //:wasmimport bridge signer_import_vault
  66  func wasmImportVault(ptr *byte, len int32, cap int32, cbID int32)
  67  
  68  //:wasmimport bridge signer_remove_identity
  69  func wasmRemoveIdentity(ptr *byte, len int32, cap int32, cbID int32)
  70  
  71  //:wasmimport bridge signer_generate_mnemonic
  72  func wasmGenerateMnemonic(cbID int32)
  73  
  74  //:wasmimport bridge signer_validate_mnemonic
  75  func wasmValidateMnemonic(ptr *byte, len int32, cap int32, cbID int32)
  76  
  77  //:wasmimport bridge signer_create_hd_vault
  78  func wasmCreateHDVault(pwPtr *byte, pwLen int32, pwCap int32, mnPtr *byte, mnLen int32, mnCap int32, cbID int32)
  79  
  80  //:wasmimport bridge signer_restore_hd_vault
  81  func wasmRestoreHDVault(pwPtr *byte, pwLen int32, pwCap int32, mnPtr *byte, mnLen int32, mnCap int32, lblPtr *byte, lblLen int32, lblCap int32, cbID int32)
  82  
  83  //:wasmimport bridge signer_derive_identity
  84  func wasmDeriveIdentity(ptr *byte, len int32, cap int32, cbID int32)
  85  
  86  //:wasmimport bridge signer_get_mnemonic
  87  func wasmGetMnemonic(cbID int32)
  88  
  89  //:wasmimport bridge signer_probe_account
  90  func wasmProbeAccount(index int32, cbID int32)
  91  
  92  //:wasmimport bridge signer_is_hd
  93  func wasmIsHD(cbID int32)
  94  
  95  //:wasmimport bridge signer_reset_extension
  96  func wasmResetExtension(cbID int32)
  97  
  98  //:wasmimport bridge signer_nwc_list
  99  func wasmNwcList(cbID int32)
 100  
 101  //:wasmimport bridge signer_nwc_add
 102  func wasmNwcAdd(uriPtr *byte, uriLen int32, uriCap int32, aliasPtr *byte, aliasLen int32, aliasCap int32, createdAt int64, cbID int32)
 103  
 104  //:wasmimport bridge signer_nwc_remove
 105  func wasmNwcRemove(ptr *byte, len int32, cap int32, cbID int32)
 106  
 107  //:wasmimport bridge signer_nwc_build_request
 108  func wasmNwcBuildRequest(idPtr *byte, idLen int32, idCap int32, methodPtr *byte, methodLen int32, methodCap int32, paramsPtr *byte, paramsLen int32, paramsCap int32, encPtr *byte, encLen int32, encCap int32, createdAt int64, cbID int32)
 109  
 110  //:wasmimport bridge signer_nwc_parse_response
 111  func wasmNwcParseResponse(idPtr *byte, idLen int32, idCap int32, ctPtr *byte, ctLen int32, ctCap int32, encPtr *byte, encLen int32, encCap int32, cbID int32)
 112  
 113  //:wasmimport bridge signer_on_state_change
 114  func wasmOnStateChange(cbID int32)
 115  
 116  //:wasmimport bridge signer_ecdh_with_secret
 117  func wasmEcdhWithSecret(skPtr *byte, skLen int32, skCap int32, pkPtr *byte, pkLen int32, pkCap int32, cbID int32)
 118  
 119  //:wasmimport bridge signer_sign_with_secret
 120  func wasmSignWithSecret(skPtr *byte, skLen int32, skCap int32, evPtr *byte, evLen int32, evCap int32, cbID int32)
 121  
 122  //:wasmimport bridge signer_pubkey_from_secret
 123  func wasmPubkeyFromSecret(skPtr *byte, skLen int32, skCap int32, cbID int32)
 124  
 125  func HasSigner() (ok bool) { return wasmHasSigner() != 0 }
 126  func HasMLS() (ok bool) { return wasmHasMLS() != 0 }
 127  
 128  func GetPublicKey(fn func(string)) {
 129  	wasmGetPublicKey(callback.RegisterSOnce(fn))
 130  }
 131  
 132  func SignEvent(eventJSON string, fn func(string)) {
 133  	wasmSignEvent(unsafe.StringData(eventJSON), int32(len(eventJSON)), int32(len(eventJSON)), callback.RegisterSOnce(fn))
 134  }
 135  
 136  func GetSharedSecret(peerPubkey string, fn func(string)) {
 137  	wasmGetSharedSecret(unsafe.StringData(peerPubkey), int32(len(peerPubkey)), int32(len(peerPubkey)), callback.RegisterSOnce(fn))
 138  }
 139  
 140  func Nip44Decrypt(peerPubkey, ciphertext string, fn func(string)) {
 141  	wasmNip44Decrypt(
 142  		unsafe.StringData(peerPubkey), int32(len(peerPubkey)), int32(len(peerPubkey)),
 143  		unsafe.StringData(ciphertext), int32(len(ciphertext)), int32(len(ciphertext)),
 144  		callback.RegisterSOnce(fn),
 145  	)
 146  }
 147  
 148  func Nip44Encrypt(peerPubkey, plaintext string, fn func(string)) {
 149  	wasmNip44Encrypt(
 150  		unsafe.StringData(peerPubkey), int32(len(peerPubkey)), int32(len(peerPubkey)),
 151  		unsafe.StringData(plaintext), int32(len(plaintext)), int32(len(plaintext)),
 152  		callback.RegisterSOnce(fn),
 153  	)
 154  }
 155  
 156  func IsInstalled(fn func(bool))       { wasmIsInstalled(callback.RegisterBOnce(fn)) }
 157  func GetVaultStatus(fn func(string))   { wasmGetVaultStatus(callback.RegisterSOnce(fn)) }
 158  
 159  func CreateVault(password string, fn func(bool)) {
 160  	wasmCreateVault(unsafe.StringData(password), int32(len(password)), int32(len(password)), callback.RegisterBOnce(fn))
 161  }
 162  
 163  func UnlockVault(password string, fn func(bool)) {
 164  	wasmUnlockVault(unsafe.StringData(password), int32(len(password)), int32(len(password)), callback.RegisterBOnce(fn))
 165  }
 166  
 167  func LastUnlockError(fn func(string)) {
 168  	wasmLastUnlockError(callback.RegisterSOnce(fn))
 169  }
 170  
 171  func LockVault(fn func())             { wasmLockVault(callback.Register0Once(fn)) }
 172  func ListIdentities(fn func(string))  { wasmListIdentities(callback.RegisterSOnce(fn)) }
 173  
 174  func AddIdentity(nsec string, fn func(bool)) {
 175  	wasmAddIdentity(unsafe.StringData(nsec), int32(len(nsec)), int32(len(nsec)), callback.RegisterBOnce(fn))
 176  }
 177  
 178  func NsecLogin(nsec string, fn func(bool)) {
 179  	wasmNsecLogin(unsafe.StringData(nsec), int32(len(nsec)), int32(len(nsec)), callback.RegisterBOnce(fn))
 180  }
 181  
 182  func SwitchIdentity(pubkey string, fn func(bool)) {
 183  	wasmSwitchIdentity(unsafe.StringData(pubkey), int32(len(pubkey)), int32(len(pubkey)), callback.RegisterBOnce(fn))
 184  }
 185  
 186  func ExportVault(password string, fn func(string)) {
 187  	wasmExportVault(unsafe.StringData(password), int32(len(password)), int32(len(password)), callback.RegisterSOnce(fn))
 188  }
 189  
 190  func ImportVault(data string, fn func(bool)) {
 191  	wasmImportVault(unsafe.StringData(data), int32(len(data)), int32(len(data)), callback.RegisterBOnce(fn))
 192  }
 193  
 194  func RemoveIdentity(pubkey string, fn func(bool)) {
 195  	wasmRemoveIdentity(unsafe.StringData(pubkey), int32(len(pubkey)), int32(len(pubkey)), callback.RegisterBOnce(fn))
 196  }
 197  
 198  func GenerateMnemonic(fn func(string)) { wasmGenerateMnemonic(callback.RegisterSOnce(fn)) }
 199  
 200  func ValidateMnemonic(mnemonic string, fn func(bool)) {
 201  	wasmValidateMnemonic(unsafe.StringData(mnemonic), int32(len(mnemonic)), int32(len(mnemonic)), callback.RegisterBOnce(fn))
 202  }
 203  
 204  func CreateHDVault(password, mnemonic string, fn func(string)) {
 205  	wasmCreateHDVault(
 206  		unsafe.StringData(password), int32(len(password)), int32(len(password)),
 207  		unsafe.StringData(mnemonic), int32(len(mnemonic)), int32(len(mnemonic)),
 208  		callback.RegisterSOnce(fn),
 209  	)
 210  }
 211  
 212  func RestoreHDVault(password, mnemonic, label string, fn func(bool)) {
 213  	wasmRestoreHDVault(
 214  		unsafe.StringData(password), int32(len(password)), int32(len(password)),
 215  		unsafe.StringData(mnemonic), int32(len(mnemonic)), int32(len(mnemonic)),
 216  		unsafe.StringData(label), int32(len(label)), int32(len(label)),
 217  		callback.RegisterBOnce(fn),
 218  	)
 219  }
 220  
 221  func DeriveIdentity(accountStr string, fn func(string)) {
 222  	wasmDeriveIdentity(unsafe.StringData(accountStr), int32(len(accountStr)), int32(len(accountStr)), callback.RegisterSOnce(fn))
 223  }
 224  
 225  func GetMnemonic(fn func(string))      { wasmGetMnemonic(callback.RegisterSOnce(fn)) }
 226  
 227  func ProbeAccount(index int32, fn func(string)) {
 228  	wasmProbeAccount(index, callback.RegisterSOnce(fn))
 229  }
 230  
 231  func IsHD(fn func(bool))              { wasmIsHD(callback.RegisterBOnce(fn)) }
 232  func ResetExtension(fn func(bool))    { wasmResetExtension(callback.RegisterBOnce(fn)) }
 233  func NwcList(fn func(string))         { wasmNwcList(callback.RegisterSOnce(fn)) }
 234  
 235  func NwcAdd(uri, alias string, createdAt int64, fn func(string)) {
 236  	wasmNwcAdd(
 237  		unsafe.StringData(uri), int32(len(uri)), int32(len(uri)),
 238  		unsafe.StringData(alias), int32(len(alias)), int32(len(alias)),
 239  		createdAt,
 240  		callback.RegisterSOnce(fn),
 241  	)
 242  }
 243  
 244  func NwcRemove(id string, fn func(bool)) {
 245  	wasmNwcRemove(unsafe.StringData(id), int32(len(id)), int32(len(id)), callback.RegisterBOnce(fn))
 246  }
 247  
 248  func NwcBuildRequest(id, method, paramsJSON, encryption string, createdAt int64, fn func(string)) {
 249  	wasmNwcBuildRequest(
 250  		unsafe.StringData(id), int32(len(id)), int32(len(id)),
 251  		unsafe.StringData(method), int32(len(method)), int32(len(method)),
 252  		unsafe.StringData(paramsJSON), int32(len(paramsJSON)), int32(len(paramsJSON)),
 253  		unsafe.StringData(encryption), int32(len(encryption)), int32(len(encryption)),
 254  		createdAt,
 255  		callback.RegisterSOnce(fn),
 256  	)
 257  }
 258  
 259  func NwcParseResponse(id, ciphertext, encryption string, fn func(string)) {
 260  	wasmNwcParseResponse(
 261  		unsafe.StringData(id), int32(len(id)), int32(len(id)),
 262  		unsafe.StringData(ciphertext), int32(len(ciphertext)), int32(len(ciphertext)),
 263  		unsafe.StringData(encryption), int32(len(encryption)), int32(len(encryption)),
 264  		callback.RegisterSOnce(fn),
 265  	)
 266  }
 267  
 268  // OnStateChange registers a persistent (non-once) string callback.
 269  // The JS bridge calls it on every state change for the session lifetime.
 270  func OnStateChange(fn func(string)) {
 271  	wasmOnStateChange(callback.RegisterS(fn))
 272  }
 273  
 274  // EcdhWithSecret: ECDH with a caller-provided secret key (not the vault identity).
 275  // secretHex is 64-char hex secret key, peerPubkeyHex is 64-char hex pubkey.
 276  // fn receives the shared secret as 64-char hex, or "" on failure.
 277  func EcdhWithSecret(secretHex, peerPubkeyHex string, fn func(string)) {
 278  	wasmEcdhWithSecret(
 279  		unsafe.StringData(secretHex), int32(len(secretHex)), int32(len(secretHex)),
 280  		unsafe.StringData(peerPubkeyHex), int32(len(peerPubkeyHex)), int32(len(peerPubkeyHex)),
 281  		callback.RegisterSOnce(fn),
 282  	)
 283  }
 284  
 285  // SignWithSecret: sign an event with a caller-provided secret key.
 286  // secretHex is 64-char hex secret key, eventJSON is the unsigned event.
 287  // fn receives the signed event JSON, or "" on failure.
 288  func SignWithSecret(secretHex, eventJSON string, fn func(string)) {
 289  	wasmSignWithSecret(
 290  		unsafe.StringData(secretHex), int32(len(secretHex)), int32(len(secretHex)),
 291  		unsafe.StringData(eventJSON), int32(len(eventJSON)), int32(len(eventJSON)),
 292  		callback.RegisterSOnce(fn),
 293  	)
 294  }
 295  
 296  // PubkeyFromSecret: derive x-only pubkey from a caller-provided secret key.
 297  // secretHex is 64-char hex secret key.
 298  // fn receives the pubkey as 64-char hex, or "" on failure.
 299  func PubkeyFromSecret(secretHex string, fn func(string)) {
 300  	wasmPubkeyFromSecret(
 301  		unsafe.StringData(secretHex), int32(len(secretHex)), int32(len(secretHex)),
 302  		callback.RegisterSOnce(fn),
 303  	)
 304  }
 305