implemented compression and returntype when theres a callbackurl

This commit is contained in:
greenart7c3
2023-10-18 11:47:24 -03:00
parent 3eaa24dad9
commit 20e0e9bbb5
8 changed files with 70 additions and 35 deletions
+1
View File
@@ -125,3 +125,4 @@ lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/
quartz/
+5 -1
View File
@@ -157,6 +157,10 @@ context.contentResolver.query(
* For Encryption/Decryption you should also send the hex pubkey that will be used to encrypt/decrypt the data eg (S.pubKey=cb8b8d378690f9a4a4f412c4e295051c4b76c3db24dc3941860fd3980f07d21d)
* S.returnType can be signature or event
* S.compressionType can be none or gzip (used when you using S.returnType == "event") because there are limitations to android intents and browsers urls
```js
<!DOCTYPE html>
<html lang="en">
@@ -182,7 +186,7 @@ context.contentResolver.query(
}
let encodedJson = encodeURIComponent(JSON.stringify(json))
var newAnchor = document.createElement("a");
newAnchor.href = `intent:${encodedJson}#Intent;scheme=nostrsigner;S.type=sign_event;S.callbackUrl=https://example.com/?event=;end`;
newAnchor.href = `intent:${encodedJson}#Intent;scheme=nostrsigner;S.compressionType=none;S.returnType=signature;S.type=sign_event;S.callbackUrl=https://example.com/?event=;end`;
newAnchor.textContent = "Open amber";
document.body.appendChild(newAnchor)
}
+2 -2
View File
@@ -69,12 +69,12 @@ android {
dependencies {
implementation('com.github.vitorpamplona.amethyst:quartz:main-SNAPSHOT') {
exclude group: 'net.java.dev.jna'
exclude group: 'net.java.dev.jna'
}
implementation 'net.java.dev.jna:jna:5.13.0@aar'
implementation "androidx.core:core-splashscreen:1.0.1"
implementation 'androidx.core:core-ktx:1.12.0'
implementation "androidx.core:core-ktx:$core_ktx_version"
implementation 'androidx.activity:activity-compose:1.8.0'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
@@ -8,7 +8,9 @@ class IntentData(
val type: SignerType,
val pubKey: HexKey,
val id: String,
val callBackUrl: String?
val callBackUrl: String?,
val compression: CompressionType,
val returnType: ReturnType
)
enum class SignerType {
@@ -20,3 +22,13 @@ enum class SignerType {
GET_PUBLIC_KEY,
DECRYPT_ZAP_EVENT
}
enum class ReturnType {
SIGNATURE,
EVENT
}
enum class CompressionType {
NONE,
GZIP
}
@@ -2,7 +2,9 @@ package com.greenart7c3.nostrsigner.service
import android.content.Intent
import android.net.Uri
import com.greenart7c3.nostrsigner.models.CompressionType
import com.greenart7c3.nostrsigner.models.IntentData
import com.greenart7c3.nostrsigner.models.ReturnType
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.service.model.AmberEvent
import java.net.URLDecoder
@@ -37,7 +39,19 @@ object IntentUtils {
val pubKey = intent.extras?.getString("pubKey") ?: ""
val id = intent.extras?.getString("id") ?: ""
return IntentData(data, name, type, pubKey, id, intent.extras?.getString("callbackUrl"))
val compressionType = if (intent.extras?.getString("compression") == "gzip") CompressionType.GZIP else CompressionType.NONE
val returnType = if (intent.extras?.getString("returnType") == "event") ReturnType.EVENT else ReturnType.SIGNATURE
return IntentData(
data,
name,
type,
pubKey,
id,
intent.extras?.getString("callbackUrl"),
compressionType,
returnType
)
}
return null
}
@@ -80,10 +80,8 @@ fun HomeScreen(
remember.value,
clipboardManager,
sig,
"",
sig,
it.callBackUrl,
it.type
it
)
}
return@LoginWithPubKey
@@ -156,10 +154,8 @@ fun HomeScreen(
remember.value,
clipboardManager,
result,
it.id,
result,
it.callBackUrl,
it.type
it
)
}
}
@@ -247,10 +243,8 @@ fun HomeScreen(
remember.value,
clipboardManager,
resultEvent.toJson(),
it.id,
resultEvent.toJson(),
it.callBackUrl,
it.type
it
)
}
} else {
@@ -268,10 +262,8 @@ fun HomeScreen(
remember.value,
clipboardManager,
signedEvent.toJson(),
it.id,
signedEvent.sig,
it.callBackUrl,
it.type
it
)
}
}
@@ -52,9 +52,10 @@ import androidx.navigation.navArgument
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.models.CompressionType
import com.greenart7c3.nostrsigner.models.History
import com.greenart7c3.nostrsigner.models.IntentData
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.models.ReturnType
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.service.getAppCompatActivity
import com.greenart7c3.nostrsigner.service.toShortenHex
@@ -74,36 +75,46 @@ fun sendResult(
rememberChoice: Boolean,
clipboardManager: ClipboardManager,
event: String,
id: String,
value: String,
callBackUrl: String?,
type: SignerType
intentData: IntentData
) {
val activity = context.getAppCompatActivity()
if (packageName != null) {
if (rememberChoice) {
account.savedApps[key] = rememberChoice
}
account.history.add(History(packageName, type.toString(), TimeUtils.now()))
account.history.add(History(packageName, intentData.type.toString(), TimeUtils.now()))
LocalPreferences.saveToEncryptedStorage(account)
val intent = Intent()
intent.putExtra("signature", value)
intent.putExtra("id", id)
intent.putExtra("id", intentData.id)
intent.putExtra("event", event)
activity?.setResult(RESULT_OK, intent)
} else if (callBackUrl != null) {
// Compress the string using GZIP
val byteArrayOutputStream = ByteArrayOutputStream()
val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream)
gzipOutputStream.write(event.toByteArray())
gzipOutputStream.close()
} else if (intentData.callBackUrl != null) {
if (intentData.returnType == ReturnType.SIGNATURE) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(intentData.callBackUrl + Uri.encode(value))
context.startActivity(intent)
} else {
if (intentData.compression == CompressionType.GZIP) {
// Compress the string using GZIP
val byteArrayOutputStream = ByteArrayOutputStream()
val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream)
gzipOutputStream.write(event.toByteArray())
gzipOutputStream.close()
// Convert the compressed data to Base64
val compressedData = byteArrayOutputStream.toByteArray()
val encodedString = Base64.getEncoder().encodeToString(compressedData)
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(callBackUrl + Uri.encode("Signer1$encodedString"))
context.startActivity(intent)
// Convert the compressed data to Base64
val compressedData = byteArrayOutputStream.toByteArray()
val encodedString = Base64.getEncoder().encodeToString(compressedData)
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(intentData.callBackUrl + Uri.encode("Signer1$encodedString"))
context.startActivity(intent)
} else {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(intentData.callBackUrl + Uri.encode(event))
context.startActivity(intent)
}
}
} else {
clipboardManager.setText(AnnotatedString(value))
Toast.makeText(
+1
View File
@@ -6,6 +6,7 @@ buildscript {
compose_ui_version = '1.5.3'
compose_ui_test_version = '1.5.3'
nav_version = "2.7.4"
core_ktx_version = '1.12.0'
}
}