main.mx raw
1 package main
2
3 import (
4 "git.smesh.lol/smesh/web/common/helpers"
5 "git.smesh.lol/smesh/web/common/jsbridge/ext"
6 )
7
8 // Method name strings built from byte constants at init time.
9 // Heap-allocated; immune to data section relocation bugs in wasm32.
10 var (
11 _true string // heap-allocated, fed into jsonResult to avoid constant-fold
12 _false string
13 )
14
15 var (
16 mGetPublicKey string
17 mSignEvent string
18 mGetRelays string
19 mNip44Encrypt string
20 mNip44Decrypt string
21 mGetSharedSecret string
22 mNip04Encrypt string
23 mNip04Decrypt string
24 mGetVaultStatus string
25 mLockVault string
26 mListIdentities string
27 mSwitchIdentity string
28 mAddIdentity string
29 mRemoveIdentity string
30 mNsecLogin string
31 mGetPermissions string
32 mSetPermission string
33 mPromptResponse string
34 mGetMnemonic string
35 mIsHD string
36 mResetExtension string
37 mValidateMnemonic string
38 mGenerateMnemonic string
39 mNwcList string
40 mUnlockVault string
41 mCreateVault string
42 mExportVault string
43 mImportVault string
44 mCreateHDVault string
45 mRestoreHDVault string
46 mDeriveIdentity string
47 mProbeAccount string
48 mNwcAdd string
49 mNwcRemove string
50 mNwcBuildRequest string
51 mNwcParseResponse string
52 mEcdhWithSecret string
53 mSignWithSecret string
54 mPubkeyFromSecret string
55 mLastUnlockError string
56 )
57
58 func init() {
59 _true = string(append([]byte(nil), 't', 'r', 'u', 'e'))
60 _false = string(append([]byte(nil), 'f', 'a', 'l', 's', 'e'))
61 mGetPublicKey = string(append([]byte(nil), 'g', 'e', 't', 'P', 'u', 'b', 'l', 'i', 'c', 'K', 'e', 'y'))
62 mSignEvent = string(append([]byte(nil), 's', 'i', 'g', 'n', 'E', 'v', 'e', 'n', 't'))
63 mGetRelays = string(append([]byte(nil), 'g', 'e', 't', 'R', 'e', 'l', 'a', 'y', 's'))
64 mNip44Encrypt = string(append([]byte(nil), 'n', 'i', 'p', '4', '4', '.', 'e', 'n', 'c', 'r', 'y', 'p', 't'))
65 mNip44Decrypt = string(append([]byte(nil), 'n', 'i', 'p', '4', '4', '.', 'd', 'e', 'c', 'r', 'y', 'p', 't'))
66 mGetSharedSecret = string(append([]byte(nil), 'g', 'e', 't', 'S', 'h', 'a', 'r', 'e', 'd', 'S', 'e', 'c', 'r', 'e', 't'))
67 mNip04Encrypt = string(append([]byte(nil), 'n', 'i', 'p', '0', '4', '.', 'e', 'n', 'c', 'r', 'y', 'p', 't'))
68 mNip04Decrypt = string(append([]byte(nil), 'n', 'i', 'p', '0', '4', '.', 'd', 'e', 'c', 'r', 'y', 'p', 't'))
69 mGetVaultStatus = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 't', 'V', 'a', 'u', 'l', 't', 'S', 't', 'a', 't', 'u', 's'))
70 mLockVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'l', 'o', 'c', 'k', 'V', 'a', 'u', 'l', 't'))
71 mListIdentities = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'l', 'i', 's', 't', 'I', 'd', 'e', 'n', 't', 'i', 't', 'i', 'e', 's'))
72 mSwitchIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 's', 'w', 'i', 't', 'c', 'h', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y'))
73 mAddIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'a', 'd', 'd', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y'))
74 mRemoveIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'r', 'e', 'm', 'o', 'v', 'e', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y'))
75 mNsecLogin = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 's', 'e', 'c', 'L', 'o', 'g', 'i', 'n'))
76 mGetPermissions = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 't', 'P', 'e', 'r', 'm', 'i', 's', 's', 'i', 'o', 'n', 's'))
77 mSetPermission = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 's', 'e', 't', 'P', 'e', 'r', 'm', 'i', 's', 's', 'i', 'o', 'n'))
78 mPromptResponse = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'p', 'r', 'o', 'm', 'p', 't', 'R', 'e', 's', 'p', 'o', 'n', 's', 'e'))
79 mGetMnemonic = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'g', 'e', 't', 'M', 'n', 'e', 'm', 'o', 'n', 'i', 'c'))
80 mIsHD = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'i', 's', 'H', 'D'))
81 mResetExtension = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'r', 'e', 's', 'e', 't', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n'))
82 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'))
83 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'))
84 mNwcList = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'l', 'i', 's', 't'))
85 mUnlockVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'u', 'n', 'l', 'o', 'c', 'k', 'V', 'a', 'u', 'l', 't'))
86 mCreateVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'c', 'r', 'e', 'a', 't', 'e', 'V', 'a', 'u', 'l', 't'))
87 mExportVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'e', 'x', 'p', 'o', 'r', 't', 'V', 'a', 'u', 'l', 't'))
88 mImportVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'i', 'm', 'p', 'o', 'r', 't', 'V', 'a', 'u', 'l', 't'))
89 mCreateHDVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'c', 'r', 'e', 'a', 't', 'e', 'H', 'D', 'V', 'a', 'u', 'l', 't'))
90 mRestoreHDVault = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'r', 'e', 's', 't', 'o', 'r', 'e', 'H', 'D', 'V', 'a', 'u', 'l', 't'))
91 mDeriveIdentity = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'd', 'e', 'r', 'i', 'v', 'e', 'I', 'd', 'e', 'n', 't', 'i', 't', 'y'))
92 mProbeAccount = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'p', 'r', 'o', 'b', 'e', 'A', 'c', 'c', 'o', 'u', 'n', 't'))
93 mNwcAdd = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'a', 'd', 'd'))
94 mNwcRemove = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'n', 'w', 'c', '.', 'r', 'e', 'm', 'o', 'v', 'e'))
95 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'))
96 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'))
97 mEcdhWithSecret = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 'e', 'c', 'd', 'h', 'W', 'i', 't', 'h', 'S', 'e', 'c', 'r', 'e', 't'))
98 mSignWithSecret = string(append([]byte(nil), 's', 'm', 'e', 's', 'h', '.', 's', 'i', 'g', 'n', 'W', 'i', 't', 'h', 'S', 'e', 'c', 'r', 'e', 't'))
99 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'))
100 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'))
101 }
102
103 // --- Response builders: byte constants only ---
104
105 func jsonTrue() (s string) { return jsonResult(_true) }
106 func jsonFalse() (s string) { return jsonResult(_false) }
107
108 func jsonResult(val string) (s string) {
109 b := append([]byte(nil), '{', '"', 'r', 'e', 's', 'u', 'l', 't', '"', ':')
110 b = b | val
111 b = append(b, '}')
112 return string(b)
113 }
114
115 func jsonResultStr(s string) (sv string) {
116 return jsonResult(helpers.JsonString(s))
117 }
118
119 func jsonErr(msg string) (s string) {
120 b := append([]byte(nil), '{', '"', 'e', 'r', 'r', 'o', 'r', '"', ':')
121 b = b | helpers.JsonString(msg)
122 b = append(b, '}')
123 return string(b)
124 }
125
126 func unknownMethod() (s string) {
127 return jsonErr(string(append([]byte(nil), 'u', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'm', 'e', 't', 'h', 'o', 'd')))
128 }
129
130 func main() {
131 loadVault()
132 loadPermissions()
133 ext.OnMessage(handleMessage)
134 }
135
136 // handleMessage dispatches using string equality against heap-built method names.
137 func handleMessage(method, paramsJSON string, senderTabID int32, respond func(string)) {
138 if method == mLockVault { respond(mgmtLockVault()); return }
139 if method == mValidateMnemonic { respond(mgmtValidateMnemonic(paramsJSON)); return }
140 if method == mSwitchIdentity { respond(mgmtSwitchIdentity(paramsJSON)); return }
141 if method == mRemoveIdentity { respond(mgmtRemoveIdentity(paramsJSON)); return }
142 if method == mResetExtension { respond(mgmtResetExtension()); return }
143 if method == mCreateVault { respond(mgmtCreateVault(paramsJSON)); return }
144 if method == mGetPublicKey { respond(nip07GetPublicKey()); return }
145 if method == mSignEvent { respond(nip07SignEvent(paramsJSON)); return }
146 if method == mGetRelays { respond("{\"result\":{}}"); return }
147 if method == mNip44Encrypt { respond(nip07Nip44Encrypt(paramsJSON)); return }
148 if method == mNip44Decrypt { respond(nip07Nip44Decrypt(paramsJSON)); return }
149 if method == mGetSharedSecret { respond(nip07GetSharedSecret(paramsJSON)); return }
150 if method == mNip04Encrypt { respond(nip07Nip04Encrypt(paramsJSON)); return }
151 if method == mNip04Decrypt { respond(nip07Nip04Decrypt(paramsJSON)); return }
152 if method == mGetVaultStatus { respond(mgmtGetVaultStatus()); return }
153 if method == mListIdentities { respond(mgmtListIdentities()); return }
154 if method == mAddIdentity { respond(mgmtAddIdentity(paramsJSON)); return }
155 if method == mNsecLogin { respond(mgmtNsecLogin(paramsJSON)); return }
156 if method == mGetPermissions { respond(mgmtGetPermissions()); return }
157 if method == mSetPermission { respond(mgmtSetPermission(paramsJSON)); return }
158 if method == mPromptResponse { respond(mgmtPromptResponse(paramsJSON)); return }
159 if method == mGetMnemonic { respond(mgmtGetMnemonic()); return }
160 if method == mIsHD { respond(mgmtIsHD()); return }
161 if method == mGenerateMnemonic { respond(mgmtGenerateMnemonic()); return }
162 if method == mNwcList { respond(nwcList()); return }
163 if method == mUnlockVault { respond(mgmtUnlockVault(paramsJSON)); return }
164 if method == mExportVault { respond(mgmtExportVault(paramsJSON)); return }
165 if method == mImportVault { respond(mgmtImportVault(paramsJSON)); return }
166 if method == mCreateHDVault { respond(mgmtCreateHDVault(paramsJSON)); return }
167 if method == mRestoreHDVault { respond(mgmtRestoreHDVault(paramsJSON)); return }
168 if method == mDeriveIdentity { respond(mgmtDeriveIdentity(paramsJSON)); return }
169 if method == mProbeAccount { respond(mgmtProbeAccount(paramsJSON)); return }
170 if method == mNwcAdd { respond(nwcAdd(paramsJSON)); return }
171 if method == mNwcRemove { respond(nwcRemove(paramsJSON)); return }
172 if method == mNwcBuildRequest { respond(nwcBuildRequest(paramsJSON)); return }
173 if method == mNwcParseResponse { respond(nwcParseResponse(paramsJSON)); return }
174 if method == mEcdhWithSecret { respond(cryptoEcdhWithSecret(paramsJSON)); return }
175 if method == mSignWithSecret { respond(cryptoSignWithSecret(paramsJSON)); return }
176 if method == mPubkeyFromSecret { respond(cryptoPubkeyFromSecret(paramsJSON)); return }
177 if method == mLastUnlockError { respond(jsonResultStr(lastUnlockErr)); return }
178 respond(unknownMethod())
179 }
180