package main import ( "git.smesh.lol/smesh/web/common/helpers" "git.smesh.lol/smesh/web/common/jsbridge/ext" ) // Method name strings built from byte constants at init time. // Heap-allocated; immune to data section relocation bugs in wasm32. var ( _true string // heap-allocated, fed into jsonResult to avoid constant-fold _false string ) var ( mGetPublicKey string mSignEvent string mGetRelays string mNip44Encrypt string mNip44Decrypt string mGetSharedSecret string mNip04Encrypt string mNip04Decrypt string mGetVaultStatus string mLockVault string mListIdentities string mSwitchIdentity string mAddIdentity string mRemoveIdentity string mNsecLogin string mGetPermissions string mSetPermission string mPromptResponse string mGetMnemonic string mIsHD string mResetExtension string mValidateMnemonic string mGenerateMnemonic string mNwcList string mUnlockVault string mCreateVault string mExportVault string mImportVault string mCreateHDVault string mRestoreHDVault string mDeriveIdentity string mProbeAccount string mNwcAdd string mNwcRemove string mNwcBuildRequest string mNwcParseResponse string mEcdhWithSecret string mSignWithSecret string mPubkeyFromSecret string mLastUnlockError string ) func init() { _true = string(append([]byte(nil), 't', 'r', 'u', 'e')) _false = string(append([]byte(nil), 'f', 'a', 'l', 's', 'e')) mGetPublicKey = string(append([]byte(nil), 'g', 'e', 't', 'P', 'u', 'b', 'l', 'i', 'c', 'K', 'e', 'y')) mSignEvent = string(append([]byte(nil), 's', 'i', 'g', 'n', 'E', 'v', 'e', 'n', 't')) mGetRelays = string(append([]byte(nil), 'g', 'e', 't', 'R', 'e', 'l', 'a', 'y', 's')) mNip44Encrypt = string(append([]byte(nil), 'n', 'i', 'p', '4', '4', '.', 'e', 'n', 'c', 'r', 'y', 'p', 't')) mNip44Decrypt = string(append([]byte(nil), 'n', 'i', 'p', '4', '4', '.', 'd', 'e', 'c', 'r', 'y', 'p', 't')) mGetSharedSecret = string(append([]byte(nil), 'g', 'e', 't', 'S', 'h', 'a', 'r', 'e', 'd', 'S', 'e', 'c', 'r', 'e', 't')) mNip04Encrypt = string(append([]byte(nil), 'n', 'i', 'p', '0', '4', '.', 'e', 'n', 'c', 'r', 'y', 'p', 't')) mNip04Decrypt = string(append([]byte(nil), 'n', 'i', 'p', '0', '4', '.', 'd', 'e', 'c', 'r', 'y', 'p', 't')) mGetVaultStatus = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 't', 'V', 'a', 'u', 'l', 't', 'S', 't', 'a', 't', 'u', 's')) mLockVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'l', 'o', 'c', 'k', 'V', 'a', 'u', 'l', 't')) mListIdentities = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'l', 'i', 's', 't', 'I', 'd', 'e', 'n', 't', 'i', 't', 'i', 'e', 's')) mSwitchIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 's', 'w', 'i', 't', 'c', 'h', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y')) mAddIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'a', 'd', 'd', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y')) mRemoveIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'r', 'e', 'm', 'o', 'v', 'e', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y')) mNsecLogin = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 's', 'e', 'c', 'L', 'o', 'g', 'i', 'n')) mGetPermissions = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 't', 'P', 'e', 'r', 'm', 'i', 's', 's', 'i', 'o', 'n', 's')) mSetPermission = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 's', 'e', 't', 'P', 'e', 'r', 'm', 'i', 's', 's', 'i', 'o', 'n')) mPromptResponse = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'p', 'r', 'o', 'm', 'p', 't', 'R', 'e', 's', 'p', 'o', 'n', 's', 'e')) mGetMnemonic = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 't', 'M', 'n', 'e', 'm', 'o', 'n', 'i', 'c')) mIsHD = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'i', 's', 'H', 'D')) mResetExtension = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'r', 'e', 's', 'e', 't', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n')) mValidateMnemonic = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'v', 'a', 'l', 'i', 'd', 'a', 't', 'e', 'M', 'n', 'e', 'm', 'o', 'n', 'i', 'c')) mGenerateMnemonic = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 'n', 'e', 'r', 'a', 't', 'e', 'M', 'n', 'e', 'm', 'o', 'n', 'i', 'c')) mNwcList = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'l', 'i', 's', 't')) mUnlockVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'u', 'n', 'l', 'o', 'c', 'k', 'V', 'a', 'u', 'l', 't')) mCreateVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'c', 'r', 'e', 'a', 't', 'e', 'V', 'a', 'u', 'l', 't')) mExportVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'e', 'x', 'p', 'o', 'r', 't', 'V', 'a', 'u', 'l', 't')) mImportVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'i', 'm', 'p', 'o', 'r', 't', 'V', 'a', 'u', 'l', 't')) mCreateHDVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'c', 'r', 'e', 'a', 't', 'e', 'H', 'D', 'V', 'a', 'u', 'l', 't')) mRestoreHDVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'r', 'e', 's', 't', 'o', 'r', 'e', 'H', 'D', 'V', 'a', 'u', 'l', 't')) mDeriveIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'd', 'e', 'r', 'i', 'v', 'e', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y')) mProbeAccount = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'p', 'r', 'o', 'b', 'e', 'A', 'c', 'c', 'o', 'u', 'n', 't')) mNwcAdd = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'a', 'd', 'd')) mNwcRemove = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'r', 'e', 'm', 'o', 'v', 'e')) mNwcBuildRequest = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'b', 'u', 'i', 'l', 'd', 'R', 'e', 'q', 'u', 'e', 's', 't')) mNwcParseResponse = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'p', 'a', 'r', 's', 'e', 'R', 'e', 's', 'p', 'o', 'n', 's', 'e')) mEcdhWithSecret = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'e', 'c', 'd', 'h', 'W', 'i', 't', 'h', 'S', 'e', 'c', 'r', 'e', 't')) mSignWithSecret = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 's', 'i', 'g', 'n', 'W', 'i', 't', 'h', 'S', 'e', 'c', 'r', 'e', 't')) mPubkeyFromSecret = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'p', 'u', 'b', 'k', 'e', 'y', 'F', 'r', 'o', 'm', 'S', 'e', 'c', 'r', 'e', 't')) mLastUnlockError = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'l', 'a', 's', 't', 'U', 'n', 'l', 'o', 'c', 'k', 'E', 'r', 'r', 'o', 'r')) } // --- Response builders: byte constants only --- func jsonTrue() (s string) { return jsonResult(_true) } func jsonFalse() (s string) { return jsonResult(_false) } func jsonResult(val string) (s string) { b := append([]byte(nil), '{', '"', 'r', 'e', 's', 'u', 'l', 't', '"', ':') b = b | val b = append(b, '}') return string(b) } func jsonResultStr(s string) (sv string) { return jsonResult(helpers.JsonString(s)) } func jsonErr(msg string) (s string) { b := append([]byte(nil), '{', '"', 'e', 'r', 'r', 'o', 'r', '"', ':') b = b | helpers.JsonString(msg) b = append(b, '}') return string(b) } func unknownMethod() (s string) { return jsonErr(string(append([]byte(nil), 'u', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'm', 'e', 't', 'h', 'o', 'd'))) } func main() { loadVault() loadPermissions() ext.OnMessage(handleMessage) } // handleMessage dispatches using string equality against heap-built method names. func handleMessage(method, paramsJSON string, senderTabID int32, respond func(string)) { if method == mLockVault { respond(mgmtLockVault()); return } if method == mValidateMnemonic { respond(mgmtValidateMnemonic(paramsJSON)); return } if method == mSwitchIdentity { respond(mgmtSwitchIdentity(paramsJSON)); return } if method == mRemoveIdentity { respond(mgmtRemoveIdentity(paramsJSON)); return } if method == mResetExtension { respond(mgmtResetExtension()); return } if method == mCreateVault { respond(mgmtCreateVault(paramsJSON)); return } if method == mGetPublicKey { respond(nip07GetPublicKey()); return } if method == mSignEvent { respond(nip07SignEvent(paramsJSON)); return } if method == mGetRelays { respond("{\"result\":{}}"); return } if method == mNip44Encrypt { respond(nip07Nip44Encrypt(paramsJSON)); return } if method == mNip44Decrypt { respond(nip07Nip44Decrypt(paramsJSON)); return } if method == mGetSharedSecret { respond(nip07GetSharedSecret(paramsJSON)); return } if method == mNip04Encrypt { respond(nip07Nip04Encrypt(paramsJSON)); return } if method == mNip04Decrypt { respond(nip07Nip04Decrypt(paramsJSON)); return } if method == mGetVaultStatus { respond(mgmtGetVaultStatus()); return } if method == mListIdentities { respond(mgmtListIdentities()); return } if method == mAddIdentity { respond(mgmtAddIdentity(paramsJSON)); return } if method == mNsecLogin { respond(mgmtNsecLogin(paramsJSON)); return } if method == mGetPermissions { respond(mgmtGetPermissions()); return } if method == mSetPermission { respond(mgmtSetPermission(paramsJSON)); return } if method == mPromptResponse { respond(mgmtPromptResponse(paramsJSON)); return } if method == mGetMnemonic { respond(mgmtGetMnemonic()); return } if method == mIsHD { respond(mgmtIsHD()); return } if method == mGenerateMnemonic { respond(mgmtGenerateMnemonic()); return } if method == mNwcList { respond(nwcList()); return } if method == mUnlockVault { respond(mgmtUnlockVault(paramsJSON)); return } if method == mExportVault { respond(mgmtExportVault(paramsJSON)); return } if method == mImportVault { respond(mgmtImportVault(paramsJSON)); return } if method == mCreateHDVault { respond(mgmtCreateHDVault(paramsJSON)); return } if method == mRestoreHDVault { respond(mgmtRestoreHDVault(paramsJSON)); return } if method == mDeriveIdentity { respond(mgmtDeriveIdentity(paramsJSON)); return } if method == mProbeAccount { respond(mgmtProbeAccount(paramsJSON)); return } if method == mNwcAdd { respond(nwcAdd(paramsJSON)); return } if method == mNwcRemove { respond(nwcRemove(paramsJSON)); return } if method == mNwcBuildRequest { respond(nwcBuildRequest(paramsJSON)); return } if method == mNwcParseResponse { respond(nwcParseResponse(paramsJSON)); return } if method == mEcdhWithSecret { respond(cryptoEcdhWithSecret(paramsJSON)); return } if method == mSignWithSecret { respond(cryptoSignWithSecret(paramsJSON)); return } if method == mPubkeyFromSecret { respond(cryptoPubkeyFromSecret(paramsJSON)); return } if method == mLastUnlockError { respond(jsonResultStr(lastUnlockErr)); return } respond(unknownMethod()) }