upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/55.md
diff options
context:
space:
mode:
authorfiatjaf <fiatjaf@gmail.com>2024-06-05 15:24:43 -0300
committerfiatjaf <fiatjaf@gmail.com>2024-06-05 15:24:43 -0300
commita6dfc7b5e513ea3070abcded3608b28e4d4a1512 (patch)
treed0c1e9a6685a3842bd65c0b568eef7426773f9c0 /55.md
parent23d605140bdbe6ccc43c6ebbcd2412a05ff262fa (diff)
fix broken nip number.
Diffstat (limited to '55.md')
-rw-r--r--55.md538
1 files changed, 538 insertions, 0 deletions
diff --git a/55.md b/55.md
new file mode 100644
index 0000000..4565e8c
--- /dev/null
+++ b/55.md
@@ -0,0 +1,538 @@
1# NIP-55
2
3## Android Signer Application
4
5`draft` `optional`
6
7This NIP describes a method for 2-way communication between an Android signer and any Nostr client on Android. The Android signer is an Android Application and the client can be a web client or an Android application.
8
9# Usage for Android applications
10
11The Android signer uses Intents and Content Resolvers to communicate between applications.
12
13To be able to use the Android signer in your application you should add this to your AndroidManifest.xml:
14
15```xml
16<queries>
17 <intent>
18 <action android:name="android.intent.action.VIEW" />
19 <category android:name="android.intent.category.BROWSABLE" />
20 <data android:scheme="nostrsigner" />
21 </intent>
22</queries>
23```
24
25Then you can use this function to check if there's a signer application installed:
26
27```kotlin
28fun isExternalSignerInstalled(context: Context): Boolean {
29 val intent =
30 Intent().apply {
31 action = Intent.ACTION_VIEW
32 data = Uri.parse("nostrsigner:")
33 }
34 val infos = context.packageManager.queryIntentActivities(intent, 0)
35 return infos.size > 0
36}
37```
38
39## Using Intents
40
41To get the result back from the Signer Application you should use `registerForActivityResult` or `rememberLauncherForActivityResult` in Kotlin. If you are using another framework check the documentation of your framework or a third party library to get the result.
42
43```kotlin
44val launcher = rememberLauncherForActivityResult(
45 contract = ActivityResultContracts.StartActivityForResult(),
46 onResult = { result ->
47 if (result.resultCode != Activity.RESULT_OK) {
48 Toast.makeText(
49 context,
50 "Sign request rejected",
51 Toast.LENGTH_SHORT
52 ).show()
53 } else {
54 val signature = activityResult.data?.getStringExtra("signature")
55 // Do something with signature ...
56 }
57 }
58)
59```
60
61Create the Intent using the **nostrsigner** scheme:
62
63```kotlin
64val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$content"))
65```
66
67Set the Signer package name:
68
69```kotlin
70intent.`package` = "com.example.signer"
71```
72
73Send the Intent:
74
75```kotlin
76launcher.launch(intent)
77```
78
79### Methods
80
81- **get_public_key**
82 - params:
83
84 ```kotlin
85 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:"))
86 intent.`package` = "com.example.signer"
87 intent.putExtra("type", "get_public_key")
88 // You can send some default permissions for the user to authorize for ever
89 val permissions = listOf(
90 Permission(
91 type = "sign_event",
92 kind = 22242
93 ),
94 Permission(
95 type = "nip44_decrypt"
96 )
97 )
98 intent.putExtra("permissions", permissions.toJson())
99 context.startActivity(intent)
100 ```
101 - result:
102 - If the user approved intent it will return the **npub** in the signature field
103
104 ```kotlin
105 val npub = intent.data?.getStringExtra("signature")
106 // The package name of the signer application
107 val packageName = intent.data?.getStringExtra("package")
108 ```
109
110- **sign_event**
111 - params:
112
113 ```kotlin
114 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$eventJson"))
115 intent.`package` = "com.example.signer"
116 intent.putExtra("type", "sign_event")
117 // To handle results when not waiting between intents
118 intent.putExtra("id", event.id)
119 // Send the current logged in user npub
120 intent.putExtra("current_user", npub)
121
122 context.startActivity(intent)
123 ```
124 - result:
125 - If the user approved intent it will return the **signature**, **id** and **event** fields
126
127 ```kotlin
128 val signature = intent.data?.getStringExtra("signature")
129 // The id you sent
130 val id = intent.data?.getStringExtra("id")
131 val signedEventJson = intent.data?.getStringExtra("event")
132 ```
133
134- **nip04_encrypt**
135 - params:
136
137 ```kotlin
138 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$plaintext"))
139 intent.`package` = "com.example.signer"
140 intent.putExtra("type", "nip04_encrypt")
141 // to control the result in your application in case you are not waiting the result before sending another intent
142 intent.putExtra("id", "some_id")
143 // Send the current logged in user npub
144 intent.putExtra("current_user", account.keyPair.pubKey.toNpub())
145 // Send the hex pubKey that will be used for encrypting the data
146 intent.putExtra("pubKey", pubKey)
147
148 context.startActivity(intent)
149 ```
150 - result:
151 - If the user approved intent it will return the **signature** and **id** fields
152
153 ```kotlin
154 val encryptedText = intent.data?.getStringExtra("signature")
155 // the id you sent
156 val id = intent.data?.getStringExtra("id")
157 ```
158
159- **nip44_encrypt**
160 - params:
161
162 ```kotlin
163 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$plaintext"))
164 intent.`package` = "com.example.signer"
165 intent.putExtra("type", "nip44_encrypt")
166 // to control the result in your application in case you are not waiting the result before sending another intent
167 intent.putExtra("id", "some_id")
168 // Send the current logged in user npub
169 intent.putExtra("current_user", account.keyPair.pubKey.toNpub())
170 // Send the hex pubKey that will be used for encrypting the data
171 intent.putExtra("pubKey", pubKey)
172
173 context.startActivity(intent)
174 ```
175 - result:
176 - If the user approved intent it will return the **signature** and **id** fields
177
178 ```kotlin
179 val encryptedText = intent.data?.getStringExtra("signature")
180 // the id you sent
181 val id = intent.data?.getStringExtra("id")
182 ```
183
184- **nip04_decrypt**
185 - params:
186
187 ```kotlin
188 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$encryptedText"))
189 intent.`package` = "com.example.signer"
190 intent.putExtra("type", "nip04_decrypt")
191 // to control the result in your application in case you are not waiting the result before sending another intent
192 intent.putExtra("id", "some_id")
193 // Send the current logged in user npub
194 intent.putExtra("current_user", account.keyPair.pubKey.toNpub())
195 // Send the hex pubKey that will be used for decrypting the data
196 intent.putExtra("pubKey", pubKey)
197
198 context.startActivity(intent)
199 ```
200 - result:
201 - If the user approved intent it will return the **signature** and **id** fields
202
203 ```kotlin
204 val plainText = intent.data?.getStringExtra("signature")
205 // the id you sent
206 val id = intent.data?.getStringExtra("id")
207 ```
208
209- **nip44_decrypt**
210 - params:
211
212 ```kotlin
213 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$encryptedText"))
214 intent.`package` = "com.example.signer"
215 intent.putExtra("type", "nip04_decrypt")
216 // to control the result in your application in case you are not waiting the result before sending another intent
217 intent.putExtra("id", "some_id")
218 // Send the current logged in user npub
219 intent.putExtra("current_user", account.keyPair.pubKey.toNpub())
220 // Send the hex pubKey that will be used for decrypting the data
221 intent.putExtra("pubKey", pubKey)
222
223 context.startActivity(intent)
224 ```
225 - result:
226 - If the user approved intent it will return the **signature** and **id** fields
227
228 ```kotlin
229 val plainText = intent.data?.getStringExtra("signature")
230 // the id you sent
231 val id = intent.data?.getStringExtra("id")
232 ```
233
234- **decrypt_zap_event**
235 - params:
236
237 ```kotlin
238 val intent = Intent(Intent.ACTION_VIEW, Uri.parse("nostrsigner:$eventJson"))
239 intent.`package` = "com.example.signer"
240 intent.putExtra("type", "decrypt_zap_event")
241 // to control the result in your application in case you are not waiting the result before sending another intent
242 intent.putExtra("id", "some_id")
243 // Send the current logged in user npub
244 intent.putExtra("current_user", account.keyPair.pubKey.toNpub())
245 context.startActivity(intent)
246 ```
247 - result:
248 - If the user approved intent it will return the **signature** and **id** fields
249
250 ```kotlin
251 val eventJson = intent.data?.getStringExtra("signature")
252 // the id you sent
253 val id = intent.data?.getStringExtra("id")
254 ```
255
256## Using Content Resolver
257
258To get the result back from Signer Application you should use contentResolver.query in Kotlin. If you are using another framework check the documentation of your framework or a third party library to get the result.
259
260If the user did not check the "remember my choice" option, the npub is not in Signer Application or the signer type is not recognized the `contentResolver` will return null
261
262For the SIGN_EVENT type Signer Application returns two columns "signature" and "event". The column event is the signed event json
263
264For the other types Signer Application returns the column "signature"
265
266If the user chose to always reject the event, signer application will return the column "rejected" and you should not open signer application
267
268### Methods
269
270- **get_public_key**
271 - params:
272
273 ```kotlin
274 val result = context.contentResolver.query(
275 Uri.parse("content://com.example.signer.GET_PUBLIC_KEY"),
276 listOf("login"),
277 null,
278 null,
279 null
280 )
281 ```
282 - result:
283 - Will return the **npub** in the signature column
284
285 ```kotlin
286 if (result == null) return
287
288 if (result.moveToFirst()) {
289 val index = it.getColumnIndex("signature")
290 if (index < 0) return
291 val npub = it.getString(index)
292 }
293 ```
294
295- **sign_event**
296 - params:
297
298 ```kotlin
299 val result = context.contentResolver.query(
300 Uri.parse("content://com.example.signer.SIGN_EVENT"),
301 listOf("$eventJson", "", "${logged_in_user_npub}"),
302 null,
303 null,
304 null
305 )
306 ```
307 - result:
308 - Will return the **signature** and the **event** columns
309
310 ```kotlin
311 if (result == null) return
312
313 if (result.moveToFirst()) {
314 val index = it.getColumnIndex("signature")
315 val indexJson = it.getColumnIndex("event")
316 val signature = it.getString(index)
317 val eventJson = it.getString(indexJson)
318 }
319 ```
320
321- **nip04_encrypt**
322 - params:
323
324 ```kotlin
325 val result = context.contentResolver.query(
326 Uri.parse("content://com.example.signer.NIP04_ENCRYPT"),
327 listOf("$plainText", "${hex_pub_key}", "${logged_in_user_npub}"),
328 null,
329 null,
330 null
331 )
332 ```
333 - result:
334 - Will return the **signature** column
335
336 ```kotlin
337 if (result == null) return
338
339 if (result.moveToFirst()) {
340 val index = it.getColumnIndex("signature")
341 val encryptedText = it.getString(index)
342 }
343 ```
344
345- **nip44_encrypt**
346 - params:
347
348 ```kotlin
349 val result = context.contentResolver.query(
350 Uri.parse("content://com.example.signer.NIP44_ENCRYPT"),
351 listOf("$plainText", "${hex_pub_key}", "${logged_in_user_npub}"),
352 null,
353 null,
354 null
355 )
356 ```
357 - result:
358 - Will return the **signature** column
359
360 ```kotlin
361 if (result == null) return
362
363 if (result.moveToFirst()) {
364 val index = it.getColumnIndex("signature")
365 val encryptedText = it.getString(index)
366 }
367 ```
368
369- **nip04_decrypt**
370 - params:
371
372 ```kotlin
373 val result = context.contentResolver.query(
374 Uri.parse("content://com.example.signer.NIP04_DECRYPT"),
375 listOf("$encryptedText", "${hex_pub_key}", "${logged_in_user_npub}"),
376 null,
377 null,
378 null
379 )
380 ```
381 - result:
382 - Will return the **signature** column
383
384 ```kotlin
385 if (result == null) return
386
387 if (result.moveToFirst()) {
388 val index = it.getColumnIndex("signature")
389 val encryptedText = it.getString(index)
390 }
391 ```
392
393- **nip44_decrypt**
394 - params:
395
396 ```kotlin
397 val result = context.contentResolver.query(
398 Uri.parse("content://com.example.signer.NIP44_DECRYPT"),
399 listOf("$encryptedText", "${hex_pub_key}", "${logged_in_user_npub}"),
400 null,
401 null,
402 null
403 )
404 ```
405 - result:
406 - Will return the **signature** column
407
408 ```kotlin
409 if (result == null) return
410
411 if (result.moveToFirst()) {
412 val index = it.getColumnIndex("signature")
413 val encryptedText = it.getString(index)
414 }
415 ```
416
417- **decrypt_zap_event**
418 - params:
419
420 ```kotlin
421 val result = context.contentResolver.query(
422 Uri.parse("content://com.example.signer.DECRYPT_ZAP_EVENT"),
423 listOf("$eventJson", "", "${logged_in_user_npub}"),
424 null,
425 null,
426 null
427 )
428 ```
429 - result:
430 - Will return the **signature** column
431
432 ```kotlin
433 if (result == null) return
434
435 if (result.moveToFirst()) {
436 val index = it.getColumnIndex("signature")
437 val eventJson = it.getString(index)
438 }
439 ```
440
441# Usage for Web Applications
442
443Since web applications can't receive a result from the intent, you should add a modal to paste the signature or the event json or create a callback url.
444
445If you send the callback url parameter, Signer Application will send the result to the url.
446
447If you don't send a callback url, Signer Application will copy the result to the clipboard.
448
449You can configure the `returnType` to be **signature** or **event**.
450
451Android intents and browser urls have limitations, so if you are using the `returnType` of **event** consider using the parameter **compressionType=gzip** that will return "Signer1" + Base64 gzip encoded event json
452
453## Methods
454
455- **get_public_key**
456 - params:
457
458 ```js
459 window.href = `nostrsigner:?compressionType=none&returnType=signature&type=get_public_key&callbackUrl=https://example.com/?event=`;
460 ```
461
462- **sign_event**
463 - params:
464
465 ```js
466 window.href = `nostrsigner:${eventJson}?compressionType=none&returnType=signature&type=sign_event&callbackUrl=https://example.com/?event=`;
467 ```
468
469- **nip04_encrypt**
470 - params:
471
472 ```js
473 window.href = `nostrsigner:${plainText}?pubKey=${hex_pub_key}&compressionType=none&returnType=signature&type=nip04_encrypt&callbackUrl=https://example.com/?event=`;
474 ```
475
476- **nip44_encrypt**
477 - params:
478
479 ```js
480 window.href = `nostrsigner:${plainText}?pubKey=${hex_pub_key}&compressionType=none&returnType=signature&type=nip44_encrypt&callbackUrl=https://example.com/?event=`;
481 ```
482
483- **nip04_decrypt**
484 - params:
485
486 ```js
487 window.href = `nostrsigner:${encryptedText}?pubKey=${hex_pub_key}&compressionType=none&returnType=signature&type=nip04_decrypt&callbackUrl=https://example.com/?event=`;
488 ```
489
490- **nip44_decrypt**
491 - params:
492
493 ```js
494 window.href = `nostrsigner:${encryptedText}?pubKey=${hex_pub_key}&compressionType=none&returnType=signature&type=nip44_decrypt&callbackUrl=https://example.com/?event=`;
495 ```
496
497- **decrypt_zap_event**
498 - params:
499
500 ```js
501 window.href = `nostrsigner:${eventJson}?compressionType=none&returnType=signature&type=decrypt_zap_event&callbackUrl=https://example.com/?event=`;
502 ```
503
504## Example
505
506```js
507<!DOCTYPE html>
508<html lang="en">
509<head>
510 <meta charset="UTF-8">
511 <meta name="viewport" content="width=device-width, initial-scale=1.0">
512 <title>Document</title>
513</head>
514<body>
515 <h1>Test</h1>
516
517 <script>
518 window.onload = function() {
519 var url = new URL(window.location.href);
520 var params = url.searchParams;
521 if (params) {
522 var param1 = params.get("event");
523 if (param1) alert(param1)
524 }
525 let json = {
526 kind: 1,
527 content: "test"
528 }
529 let encodedJson = encodeURIComponent(JSON.stringify(json))
530 var newAnchor = document.createElement("a");
531 newAnchor.href = `nostrsigner:${encodedJson}?compressionType=none&returnType=signature&type=sign_event&callbackUrl=https://example.com/?event=`;
532 newAnchor.textContent = "Open External Signer";
533 document.body.appendChild(newAnchor)
534 }
535 </script>
536</body>
537</html>
538```