mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-04-03 09:28:18 +02:00
support for nip95
This commit is contained in:
parent
9ee1250f3a
commit
d1cb699027
@ -112,7 +112,7 @@ class Account(
|
||||
val content = blockList?.content ?: ""
|
||||
if (content.isEmpty()) return@launch
|
||||
AmberUtils.content = ""
|
||||
AmberUtils.decryptBookmark(
|
||||
AmberUtils.decrypt(
|
||||
content,
|
||||
keyPair.pubKey.toHexKey()
|
||||
)
|
||||
@ -823,31 +823,75 @@ class Account(
|
||||
}
|
||||
|
||||
fun createNip95(byteArray: ByteArray, headerInfo: FileHeader): Pair<FileStorageEvent, FileStorageHeaderEvent>? {
|
||||
if (!isWriteable()) return null
|
||||
if (!isWriteable() && !loginWithAmber) return null
|
||||
|
||||
val data = FileStorageEvent.create(
|
||||
mimeType = headerInfo.mimeType ?: "",
|
||||
data = byteArray,
|
||||
privateKey = keyPair.privKey!!
|
||||
)
|
||||
if (loginWithAmber) {
|
||||
val unsignedData = FileStorageEvent.create(
|
||||
mimeType = headerInfo.mimeType ?: "",
|
||||
data = byteArray,
|
||||
pubKey = keyPair.pubKey.toHexKey()
|
||||
)
|
||||
|
||||
val signedEvent = FileStorageHeaderEvent.create(
|
||||
data,
|
||||
mimeType = headerInfo.mimeType,
|
||||
hash = headerInfo.hash,
|
||||
size = headerInfo.size.toString(),
|
||||
dimensions = headerInfo.dim,
|
||||
blurhash = headerInfo.blurHash,
|
||||
description = headerInfo.description,
|
||||
sensitiveContent = headerInfo.sensitiveContent,
|
||||
privateKey = keyPair.privKey!!
|
||||
)
|
||||
AmberUtils.openAmber(unsignedData)
|
||||
if (AmberUtils.content.isBlank()) return null
|
||||
val data = FileStorageEvent(
|
||||
unsignedData.id,
|
||||
unsignedData.pubKey,
|
||||
unsignedData.createdAt,
|
||||
unsignedData.tags,
|
||||
unsignedData.content,
|
||||
AmberUtils.content
|
||||
)
|
||||
|
||||
return Pair(data, signedEvent)
|
||||
val unsignedEvent = FileStorageHeaderEvent.create(
|
||||
data,
|
||||
mimeType = headerInfo.mimeType,
|
||||
hash = headerInfo.hash,
|
||||
size = headerInfo.size.toString(),
|
||||
dimensions = headerInfo.dim,
|
||||
blurhash = headerInfo.blurHash,
|
||||
description = headerInfo.description,
|
||||
sensitiveContent = headerInfo.sensitiveContent,
|
||||
pubKey = keyPair.pubKey.toHexKey()
|
||||
)
|
||||
|
||||
AmberUtils.openAmber(unsignedEvent)
|
||||
if (AmberUtils.content.isBlank()) return null
|
||||
val signedEvent = FileStorageHeaderEvent(
|
||||
unsignedEvent.id,
|
||||
unsignedEvent.pubKey,
|
||||
unsignedEvent.createdAt,
|
||||
unsignedEvent.tags,
|
||||
unsignedEvent.content,
|
||||
AmberUtils.content
|
||||
)
|
||||
|
||||
return Pair(data, signedEvent)
|
||||
} else {
|
||||
val data = FileStorageEvent.create(
|
||||
mimeType = headerInfo.mimeType ?: "",
|
||||
data = byteArray,
|
||||
privateKey = keyPair.privKey!!
|
||||
)
|
||||
|
||||
val signedEvent = FileStorageHeaderEvent.create(
|
||||
data,
|
||||
mimeType = headerInfo.mimeType,
|
||||
hash = headerInfo.hash,
|
||||
size = headerInfo.size.toString(),
|
||||
dimensions = headerInfo.dim,
|
||||
blurhash = headerInfo.blurHash,
|
||||
description = headerInfo.description,
|
||||
sensitiveContent = headerInfo.sensitiveContent,
|
||||
privateKey = keyPair.privKey!!
|
||||
)
|
||||
|
||||
return Pair(data, signedEvent)
|
||||
}
|
||||
}
|
||||
|
||||
fun sendNip95(data: FileStorageEvent, signedEvent: FileStorageHeaderEvent, relayList: List<Relay>? = null): Note? {
|
||||
if (!isWriteable()) return null
|
||||
if (!isWriteable() && !loginWithAmber) return null
|
||||
|
||||
Client.send(data, relayList = relayList)
|
||||
LocalCache.consume(data, null)
|
||||
@ -1312,7 +1356,7 @@ class Account(
|
||||
}
|
||||
val msg = Event.mapper.writeValueAsString(privTags)
|
||||
|
||||
AmberUtils.encryptBookmark(msg, keyPair.pubKey.toHexKey())
|
||||
AmberUtils.encrypt(msg, keyPair.pubKey.toHexKey())
|
||||
|
||||
if (AmberUtils.content.isBlank()) {
|
||||
return null
|
||||
@ -1357,7 +1401,7 @@ class Account(
|
||||
}
|
||||
val msg = Event.mapper.writeValueAsString(privTags)
|
||||
|
||||
AmberUtils.encryptBookmark(msg, keyPair.pubKey.toHexKey())
|
||||
AmberUtils.encrypt(msg, keyPair.pubKey.toHexKey())
|
||||
|
||||
if (AmberUtils.content.isBlank()) {
|
||||
return null
|
||||
@ -1432,7 +1476,7 @@ class Account(
|
||||
}
|
||||
val msg = Event.mapper.writeValueAsString(privTags)
|
||||
|
||||
AmberUtils.encryptBookmark(msg, keyPair.pubKey.toHexKey())
|
||||
AmberUtils.encrypt(msg, keyPair.pubKey.toHexKey())
|
||||
|
||||
if (AmberUtils.content.isBlank()) {
|
||||
return null
|
||||
@ -1597,7 +1641,7 @@ class Account(
|
||||
}
|
||||
val msg = Event.mapper.writeValueAsString(privTags)
|
||||
|
||||
AmberUtils.encryptBookmark(msg, keyPair.pubKey.toHexKey())
|
||||
AmberUtils.encrypt(msg, keyPair.pubKey.toHexKey())
|
||||
|
||||
if (AmberUtils.content.isBlank()) {
|
||||
return null
|
||||
|
@ -6,6 +6,7 @@ import androidx.activity.result.ActivityResultLauncher
|
||||
import com.vitorpamplona.amethyst.ServiceManager
|
||||
import com.vitorpamplona.amethyst.ui.actions.SignerType
|
||||
import com.vitorpamplona.quartz.encoders.HexKey
|
||||
import com.vitorpamplona.quartz.events.EventInterface
|
||||
|
||||
object AmberUtils {
|
||||
var content: String = ""
|
||||
@ -33,7 +34,20 @@ object AmberUtils {
|
||||
intentResult.launch(intent)
|
||||
}
|
||||
|
||||
fun decryptBookmark(encryptedContent: String, pubKey: HexKey) {
|
||||
fun openAmber(event: EventInterface) {
|
||||
isActivityRunning = true
|
||||
openAmber(
|
||||
event.toJson(),
|
||||
SignerType.SIGN_EVENT,
|
||||
IntentUtils.decryptActivityResultLauncher,
|
||||
""
|
||||
)
|
||||
while (isActivityRunning) {
|
||||
Thread.sleep(250)
|
||||
}
|
||||
}
|
||||
|
||||
fun decrypt(encryptedContent: String, pubKey: HexKey) {
|
||||
if (content.isBlank()) {
|
||||
isActivityRunning = true
|
||||
openAmber(
|
||||
@ -48,7 +62,7 @@ object AmberUtils {
|
||||
}
|
||||
}
|
||||
|
||||
fun encryptBookmark(decryptedContent: String, pubKey: HexKey) {
|
||||
fun encrypt(decryptedContent: String, pubKey: HexKey) {
|
||||
if (content.isBlank()) {
|
||||
isActivityRunning = true
|
||||
openAmber(
|
||||
|
@ -285,7 +285,14 @@ open class NewPostViewModel() : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun upload(galleryUri: Uri, description: String, sensitiveContent: Boolean, server: ServersAvailable, context: Context, relayList: List<Relay>? = null) {
|
||||
fun upload(
|
||||
galleryUri: Uri,
|
||||
description: String,
|
||||
sensitiveContent: Boolean,
|
||||
server: ServersAvailable,
|
||||
context: Context,
|
||||
relayList: List<Relay>? = null
|
||||
) {
|
||||
isUploadingImage = true
|
||||
contentToAddUrl = null
|
||||
|
||||
|
@ -18,7 +18,7 @@ object BookmarkPrivateFeedFilter : FeedFilter<Note>() {
|
||||
|
||||
if (account.loginWithAmber) {
|
||||
if (AmberUtils.content.isBlank()) {
|
||||
AmberUtils.decryptBookmark(bookmarks?.content ?: "", account.keyPair.pubKey.toHexKey())
|
||||
AmberUtils.decrypt(bookmarks?.content ?: "", account.keyPair.pubKey.toHexKey())
|
||||
bookmarks?.decryptedContent = AmberUtils.content
|
||||
}
|
||||
|
||||
|
@ -481,7 +481,7 @@ fun NoteDropDownMenu(note: Note, popupExpanded: MutableState<Boolean>, accountVi
|
||||
scope.launch(Dispatchers.IO) {
|
||||
if (accountViewModel.loggedInWithAmber()) {
|
||||
val bookmarks = accountViewModel.userProfile().latestBookmarkList
|
||||
AmberUtils.decryptBookmark(
|
||||
AmberUtils.decrypt(
|
||||
bookmarks?.content ?: "",
|
||||
accountViewModel.account.keyPair.pubKey.toHexKey()
|
||||
)
|
||||
@ -503,7 +503,7 @@ fun NoteDropDownMenu(note: Note, popupExpanded: MutableState<Boolean>, accountVi
|
||||
scope.launch(Dispatchers.IO) {
|
||||
if (accountViewModel.loggedInWithAmber()) {
|
||||
val bookmarks = accountViewModel.userProfile().latestBookmarkList
|
||||
AmberUtils.decryptBookmark(
|
||||
AmberUtils.decrypt(
|
||||
bookmarks?.content ?: "",
|
||||
accountViewModel.account.keyPair.pubKey.toHexKey()
|
||||
)
|
||||
@ -526,7 +526,7 @@ fun NoteDropDownMenu(note: Note, popupExpanded: MutableState<Boolean>, accountVi
|
||||
scope.launch(Dispatchers.IO) {
|
||||
if (accountViewModel.loggedInWithAmber()) {
|
||||
val bookmarks = accountViewModel.userProfile().latestBookmarkList
|
||||
AmberUtils.decryptBookmark(
|
||||
AmberUtils.decrypt(
|
||||
bookmarks?.content ?: "",
|
||||
accountViewModel.account.keyPair.pubKey.toHexKey()
|
||||
)
|
||||
@ -551,7 +551,7 @@ fun NoteDropDownMenu(note: Note, popupExpanded: MutableState<Boolean>, accountVi
|
||||
scope.launch(Dispatchers.IO) {
|
||||
if (accountViewModel.loggedInWithAmber()) {
|
||||
val bookmarks = accountViewModel.userProfile().latestBookmarkList
|
||||
AmberUtils.decryptBookmark(
|
||||
AmberUtils.decrypt(
|
||||
bookmarks?.content ?: "",
|
||||
accountViewModel.account.keyPair.pubKey.toHexKey()
|
||||
)
|
||||
|
@ -40,6 +40,21 @@ class FileStorageEvent(
|
||||
return Base64.getEncoder().encodeToString(bytes)
|
||||
}
|
||||
|
||||
fun create(
|
||||
mimeType: String,
|
||||
data: ByteArray,
|
||||
pubKey: HexKey,
|
||||
createdAt: Long = TimeUtils.now()
|
||||
): FileStorageEvent {
|
||||
val tags = listOfNotNull(
|
||||
listOf(TYPE, mimeType)
|
||||
)
|
||||
|
||||
val content = encode(data)
|
||||
val id = generateId(pubKey, createdAt, kind, tags, content)
|
||||
return FileStorageEvent(id.toHexKey(), pubKey, createdAt, tags, content, "")
|
||||
}
|
||||
|
||||
fun create(
|
||||
mimeType: String,
|
||||
data: ByteArray,
|
||||
|
@ -39,6 +39,45 @@ class FileStorageHeaderEvent(
|
||||
private const val TORRENT_INFOHASH = "i"
|
||||
private const val BLUR_HASH = "blurhash"
|
||||
|
||||
fun create(
|
||||
storageEvent: FileStorageEvent,
|
||||
mimeType: String? = null,
|
||||
description: String? = null,
|
||||
hash: String? = null,
|
||||
size: String? = null,
|
||||
dimensions: String? = null,
|
||||
blurhash: String? = null,
|
||||
magnetURI: String? = null,
|
||||
torrentInfoHash: String? = null,
|
||||
encryptionKey: AESGCM? = null,
|
||||
sensitiveContent: Boolean? = null,
|
||||
pubKey: HexKey,
|
||||
createdAt: Long = TimeUtils.now()
|
||||
): FileStorageHeaderEvent {
|
||||
val tags = listOfNotNull(
|
||||
listOf("e", storageEvent.id),
|
||||
mimeType?.let { listOf(MIME_TYPE, mimeType) },
|
||||
hash?.let { listOf(HASH, it) },
|
||||
size?.let { listOf(FILE_SIZE, it) },
|
||||
dimensions?.let { listOf(DIMENSION, it) },
|
||||
blurhash?.let { listOf(BLUR_HASH, it) },
|
||||
magnetURI?.let { listOf(MAGNET_URI, it) },
|
||||
torrentInfoHash?.let { listOf(TORRENT_INFOHASH, it) },
|
||||
encryptionKey?.let { listOf(ENCRYPTION_KEY, it.key, it.nonce) },
|
||||
sensitiveContent?.let {
|
||||
if (it) {
|
||||
listOf("content-warning", "")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
val content = description ?: ""
|
||||
val id = generateId(pubKey, createdAt, kind, tags, content)
|
||||
return FileStorageHeaderEvent(id.toHexKey(), pubKey, createdAt, tags, content, "")
|
||||
}
|
||||
|
||||
fun create(
|
||||
storageEvent: FileStorageEvent,
|
||||
mimeType: String? = null,
|
||||
|
Loading…
x
Reference in New Issue
Block a user