mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2024-09-29 16:30:49 +00:00
Merge pull request #653 from greenart7c3/double_encryption_fix_2
fix double encryption when sending dms, when login with external signer set privKey to null to be sure we have no private keys
This commit is contained in:
commit
6fe713107e
@ -250,7 +250,12 @@ object LocalPreferences {
|
|||||||
|
|
||||||
val prefs = encryptedPreferences(account.userProfile().pubkeyNpub())
|
val prefs = encryptedPreferences(account.userProfile().pubkeyNpub())
|
||||||
prefs.edit().apply {
|
prefs.edit().apply {
|
||||||
account.keyPair.privKey?.let { putString(PrefKeys.NOSTR_PRIVKEY, it.toHexKey()) }
|
putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, account.loginWithExternalSigner)
|
||||||
|
if (account.loginWithExternalSigner) {
|
||||||
|
remove(PrefKeys.NOSTR_PRIVKEY)
|
||||||
|
} else {
|
||||||
|
account.keyPair.privKey?.let { putString(PrefKeys.NOSTR_PRIVKEY, it.toHexKey()) }
|
||||||
|
}
|
||||||
account.keyPair.pubKey.let { putString(PrefKeys.NOSTR_PUBKEY, it.toHexKey()) }
|
account.keyPair.pubKey.let { putString(PrefKeys.NOSTR_PUBKEY, it.toHexKey()) }
|
||||||
putStringSet(PrefKeys.FOLLOWING_CHANNELS, account.followingChannels)
|
putStringSet(PrefKeys.FOLLOWING_CHANNELS, account.followingChannels)
|
||||||
putStringSet(PrefKeys.FOLLOWING_COMMUNITIES, account.followingCommunities)
|
putStringSet(PrefKeys.FOLLOWING_COMMUNITIES, account.followingCommunities)
|
||||||
@ -283,7 +288,6 @@ object LocalPreferences {
|
|||||||
} else {
|
} else {
|
||||||
putBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, account.showSensitiveContent!!)
|
putBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, account.showSensitiveContent!!)
|
||||||
}
|
}
|
||||||
putBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, account.loginWithExternalSigner)
|
|
||||||
}.apply()
|
}.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -381,7 +385,8 @@ object LocalPreferences {
|
|||||||
|
|
||||||
return@withContext with(encryptedPreferences(npub)) {
|
return@withContext with(encryptedPreferences(npub)) {
|
||||||
val pubKey = getString(PrefKeys.NOSTR_PUBKEY, null) ?: return@with null
|
val pubKey = getString(PrefKeys.NOSTR_PUBKEY, null) ?: return@with null
|
||||||
val privKey = getString(PrefKeys.NOSTR_PRIVKEY, null)
|
val loginWithExternalSigner = getBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, false)
|
||||||
|
val privKey = if (loginWithExternalSigner) null else getString(PrefKeys.NOSTR_PRIVKEY, null)
|
||||||
val followingChannels = getStringSet(PrefKeys.FOLLOWING_CHANNELS, null) ?: setOf()
|
val followingChannels = getStringSet(PrefKeys.FOLLOWING_CHANNELS, null) ?: setOf()
|
||||||
val followingCommunities = getStringSet(PrefKeys.FOLLOWING_COMMUNITIES, null) ?: setOf()
|
val followingCommunities = getStringSet(PrefKeys.FOLLOWING_COMMUNITIES, null) ?: setOf()
|
||||||
val hiddenUsers = getStringSet(PrefKeys.HIDDEN_USERS, emptySet()) ?: setOf()
|
val hiddenUsers = getStringSet(PrefKeys.HIDDEN_USERS, emptySet()) ?: setOf()
|
||||||
@ -453,7 +458,6 @@ object LocalPreferences {
|
|||||||
val useProxy = getBoolean(PrefKeys.USE_PROXY, false)
|
val useProxy = getBoolean(PrefKeys.USE_PROXY, false)
|
||||||
val proxyPort = getInt(PrefKeys.PROXY_PORT, 9050)
|
val proxyPort = getInt(PrefKeys.PROXY_PORT, 9050)
|
||||||
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
||||||
val loginWithExternalSigner = getBoolean(PrefKeys.LOGIN_WITH_EXTERNAL_SIGNER, false)
|
|
||||||
|
|
||||||
val showSensitiveContent = if (contains(PrefKeys.SHOW_SENSITIVE_CONTENT)) {
|
val showSensitiveContent = if (contains(PrefKeys.SHOW_SENSITIVE_CONTENT)) {
|
||||||
getBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, false)
|
getBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, false)
|
||||||
|
@ -1532,40 +1532,51 @@ class Account(
|
|||||||
val repliesToHex = listOfNotNull(replyingTo?.idHex).ifEmpty { null }
|
val repliesToHex = listOfNotNull(replyingTo?.idHex).ifEmpty { null }
|
||||||
val mentionsHex = mentions?.map { it.pubkeyHex }
|
val mentionsHex = mentions?.map { it.pubkeyHex }
|
||||||
|
|
||||||
var localMessage = message
|
|
||||||
if (loginWithExternalSigner) {
|
if (loginWithExternalSigner) {
|
||||||
ExternalSignerUtils.encrypt(localMessage, toUser, "encrypt")
|
ExternalSignerUtils.encrypt(message, toUser, "encrypt")
|
||||||
val eventContent = ExternalSignerUtils.content["encrypt"] ?: ""
|
val eventContent = ExternalSignerUtils.content["encrypt"] ?: ""
|
||||||
if (eventContent.isBlank()) return
|
if (eventContent.isBlank()) return
|
||||||
localMessage = eventContent
|
|
||||||
ExternalSignerUtils.content.remove("encrypt")
|
ExternalSignerUtils.content.remove("encrypt")
|
||||||
}
|
val unsignedEvent = PrivateDmEvent.create(
|
||||||
|
recipientPubKey = toUser.hexToByteArray(),
|
||||||
|
publishedRecipientPubKey = toUser.hexToByteArray(),
|
||||||
|
msg = eventContent,
|
||||||
|
replyTos = repliesToHex,
|
||||||
|
mentions = mentionsHex,
|
||||||
|
zapReceiver = zapReceiver,
|
||||||
|
markAsSensitive = wantsToMarkAsSensitive,
|
||||||
|
zapRaiserAmount = zapRaiserAmount,
|
||||||
|
geohash = geohash,
|
||||||
|
keyPair = keyPair,
|
||||||
|
advertiseNip18 = false
|
||||||
|
)
|
||||||
|
|
||||||
var signedEvent = PrivateDmEvent.create(
|
ExternalSignerUtils.openSigner(unsignedEvent)
|
||||||
recipientPubKey = toUser.hexToByteArray(),
|
val signature = ExternalSignerUtils.content[unsignedEvent.id] ?: ""
|
||||||
publishedRecipientPubKey = toUser.hexToByteArray(),
|
if (signature.isBlank()) {
|
||||||
msg = localMessage,
|
|
||||||
replyTos = repliesToHex,
|
|
||||||
mentions = mentionsHex,
|
|
||||||
zapReceiver = zapReceiver,
|
|
||||||
markAsSensitive = wantsToMarkAsSensitive,
|
|
||||||
zapRaiserAmount = zapRaiserAmount,
|
|
||||||
geohash = geohash,
|
|
||||||
keyPair = keyPair,
|
|
||||||
advertiseNip18 = false
|
|
||||||
)
|
|
||||||
|
|
||||||
if (loginWithExternalSigner) {
|
|
||||||
ExternalSignerUtils.openSigner(signedEvent)
|
|
||||||
val eventContent = ExternalSignerUtils.content[signedEvent.id] ?: ""
|
|
||||||
if (eventContent.isBlank()) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
signedEvent = PrivateDmEvent.create(signedEvent, eventContent)
|
val signedEvent = PrivateDmEvent.create(unsignedEvent, signature)
|
||||||
}
|
Client.send(signedEvent)
|
||||||
|
LocalCache.consume(signedEvent, null)
|
||||||
|
} else {
|
||||||
|
val signedEvent = PrivateDmEvent.create(
|
||||||
|
recipientPubKey = toUser.hexToByteArray(),
|
||||||
|
publishedRecipientPubKey = toUser.hexToByteArray(),
|
||||||
|
msg = message,
|
||||||
|
replyTos = repliesToHex,
|
||||||
|
mentions = mentionsHex,
|
||||||
|
zapReceiver = zapReceiver,
|
||||||
|
markAsSensitive = wantsToMarkAsSensitive,
|
||||||
|
zapRaiserAmount = zapRaiserAmount,
|
||||||
|
geohash = geohash,
|
||||||
|
keyPair = keyPair,
|
||||||
|
advertiseNip18 = false
|
||||||
|
)
|
||||||
|
|
||||||
Client.send(signedEvent)
|
Client.send(signedEvent)
|
||||||
LocalCache.consume(signedEvent, null)
|
LocalCache.consume(signedEvent, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendNIP24PrivateMessage(
|
fun sendNIP24PrivateMessage(
|
||||||
|
@ -55,16 +55,22 @@ class AccountStateViewModel() : ViewModel() {
|
|||||||
val pubKeyParsed = parsed?.hex?.hexToByteArray()
|
val pubKeyParsed = parsed?.hex?.hexToByteArray()
|
||||||
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
||||||
|
|
||||||
|
if (loginWithExternalSigner && pubKeyParsed == null) {
|
||||||
|
throw Exception("Invalid key while trying to login with external signer")
|
||||||
|
}
|
||||||
|
|
||||||
val account =
|
val account =
|
||||||
if (key.startsWith("nsec")) {
|
if (loginWithExternalSigner) {
|
||||||
Account(KeyPair(privKey = key.bechToBytes()), proxy = proxy, proxyPort = proxyPort, loginWithExternalSigner = loginWithExternalSigner)
|
Account(KeyPair(pubKey = pubKeyParsed), proxy = proxy, proxyPort = proxyPort, loginWithExternalSigner = true)
|
||||||
|
} else if (key.startsWith("nsec")) {
|
||||||
|
Account(KeyPair(privKey = key.bechToBytes()), proxy = proxy, proxyPort = proxyPort)
|
||||||
} else if (pubKeyParsed != null) {
|
} else if (pubKeyParsed != null) {
|
||||||
Account(KeyPair(pubKey = pubKeyParsed), proxy = proxy, proxyPort = proxyPort, loginWithExternalSigner = loginWithExternalSigner)
|
Account(KeyPair(pubKey = pubKeyParsed), proxy = proxy, proxyPort = proxyPort)
|
||||||
} else if (EMAIL_PATTERN.matcher(key).matches()) {
|
} else if (EMAIL_PATTERN.matcher(key).matches()) {
|
||||||
// Evaluate NIP-5
|
// Evaluate NIP-5
|
||||||
Account(KeyPair(), proxy = proxy, proxyPort = proxyPort, loginWithExternalSigner = loginWithExternalSigner)
|
Account(KeyPair(), proxy = proxy, proxyPort = proxyPort)
|
||||||
} else {
|
} else {
|
||||||
Account(KeyPair(Hex.decode(key)), proxy = proxy, proxyPort = proxyPort, loginWithExternalSigner = loginWithExternalSigner)
|
Account(KeyPair(Hex.decode(key)), proxy = proxy, proxyPort = proxyPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalPreferences.updatePrefsForLogin(account)
|
LocalPreferences.updatePrefsForLogin(account)
|
||||||
|
@ -130,6 +130,49 @@ class PrivateDmEvent(
|
|||||||
return PrivateDmEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig?.toHexKey() ?: "")
|
return PrivateDmEvent(id.toHexKey(), pubKey, createdAt, tags, content, sig?.toHexKey() ?: "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createWithoutSignature(
|
||||||
|
msg: String,
|
||||||
|
replyTos: List<String>? = null,
|
||||||
|
mentions: List<String>? = null,
|
||||||
|
zapReceiver: List<ZapSplitSetup>? = null,
|
||||||
|
keyPair: KeyPair,
|
||||||
|
createdAt: Long = TimeUtils.now(),
|
||||||
|
publishedRecipientPubKey: ByteArray? = null,
|
||||||
|
advertiseNip18: Boolean = true,
|
||||||
|
markAsSensitive: Boolean,
|
||||||
|
zapRaiserAmount: Long?,
|
||||||
|
geohash: String? = null
|
||||||
|
): PrivateDmEvent {
|
||||||
|
val message = if (advertiseNip18) { nip18Advertisement } else { "" } + msg
|
||||||
|
val content = message
|
||||||
|
val tags = mutableListOf<List<String>>()
|
||||||
|
publishedRecipientPubKey?.let {
|
||||||
|
tags.add(listOf("p", publishedRecipientPubKey.toHexKey()))
|
||||||
|
}
|
||||||
|
replyTos?.forEach {
|
||||||
|
tags.add(listOf("e", it))
|
||||||
|
}
|
||||||
|
mentions?.forEach {
|
||||||
|
tags.add(listOf("p", it))
|
||||||
|
}
|
||||||
|
zapReceiver?.forEach {
|
||||||
|
tags.add(listOf("zap", it.lnAddressOrPubKeyHex, it.relay ?: "", it.weight.toString()))
|
||||||
|
}
|
||||||
|
if (markAsSensitive) {
|
||||||
|
tags.add(listOf("content-warning", ""))
|
||||||
|
}
|
||||||
|
zapRaiserAmount?.let {
|
||||||
|
tags.add(listOf("zapraiser", "$it"))
|
||||||
|
}
|
||||||
|
geohash?.let {
|
||||||
|
tags.add(listOf("g", it))
|
||||||
|
}
|
||||||
|
|
||||||
|
val pubKey = keyPair.pubKey.toHexKey()
|
||||||
|
val id = generateId(pubKey, createdAt, kind, tags, content)
|
||||||
|
return PrivateDmEvent(id.toHexKey(), pubKey, createdAt, tags, content, "")
|
||||||
|
}
|
||||||
|
|
||||||
fun create(
|
fun create(
|
||||||
unsignedEvent: PrivateDmEvent,
|
unsignedEvent: PrivateDmEvent,
|
||||||
signature: String,
|
signature: String,
|
||||||
|
Loading…
Reference in New Issue
Block a user