Added exports for messages, labels and adddresses

and some code improvements
This commit is contained in:
2017-07-21 10:27:20 +02:00
parent fd08fa3883
commit 6c04aa683e
12 changed files with 453 additions and 56 deletions

23
exports/build.gradle Normal file
View File

@ -0,0 +1,23 @@
uploadArchives {
repositories {
mavenDeployer {
pom.project {
name 'Jabit Exports'
artifactId = 'jabit-exports'
description 'Import and export data to JSON files.'
}
}
}
}
dependencies {
compile project(':core')
compile 'org.slf4j:slf4j-api'
compile 'com.beust:klaxon'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'com.nhaarman:mockito-kotlin:1.5.0'
testCompile project(path: ':core', configuration: 'testArtifacts')
testCompile project(':cryptography-bc')
}

View File

@ -0,0 +1,81 @@
/*
* Copyright 2017 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.exports
import ch.dissem.bitmessage.entity.BitmessageAddress
import ch.dissem.bitmessage.entity.valueobject.PrivateKey
import ch.dissem.bitmessage.factory.Factory
import ch.dissem.bitmessage.utils.Encode
import com.beust.klaxon.*
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.util.*
/**
* Exports and imports contacts and identities
*/
object ContactExport {
fun exportContacts(contacts: List<BitmessageAddress>, includePrivateKey: Boolean = false) = json {
val base64 = Base64.getEncoder()
array(
contacts.map {
obj(
"alias" to it.alias,
"address" to it.address,
"chan" to it.isChan,
"subscribed" to it.isSubscribed,
"pubkey" to it.pubkey?.let {
val out = ByteArrayOutputStream()
it.writeUnencrypted(out)
base64.encodeToString(out.toByteArray())
},
"privateKey" to if (includePrivateKey) {
it.privateKey?.let { base64.encodeToString(Encode.bytes(it)) }
} else {
null
}
)
}
)
}
fun importContacts(input: JsonArray<*>): List<BitmessageAddress> {
return input.filterIsInstance(JsonObject::class.java).map { json ->
val base64 = Base64.getDecoder()
fun JsonObject.bytes(fieldName: String) = string(fieldName)?.let { base64.decode(it) }
val privateKey = json.bytes("privateKey")?.let { PrivateKey.read(ByteArrayInputStream(it)) }
if (privateKey != null) {
BitmessageAddress(privateKey)
} else {
BitmessageAddress(json.string("address") ?: throw IllegalArgumentException("address expected"))
}.apply {
alias = json.string("alias")
isChan = json.boolean("chan") ?: false
isSubscribed = json.boolean("subscribed") ?: false
pubkey = json.bytes("pubkey")?.let {
Factory.readPubkey(
version = version,
stream = stream,
`is` = ByteArrayInputStream(it),
length = it.size,
encrypted = false
)
}
}
}
}
}

View File

@ -0,0 +1,106 @@
/*
* Copyright 2017 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.exports
import ch.dissem.bitmessage.entity.BitmessageAddress
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.bitmessage.entity.valueobject.InventoryVector
import ch.dissem.bitmessage.entity.valueobject.Label
import ch.dissem.bitmessage.utils.Encode
import ch.dissem.bitmessage.utils.TTL
import com.beust.klaxon.*
import java.util.*
/**
* Exports and imports messages and labels
*/
object MessageExport {
fun exportLabels(labels: List<Label>) = json {
array(
labels.map {
obj(
"label" to it.toString(),
"type" to it.type?.name,
"color" to it.color
)
}
)
}
fun exportMessages(messages: List<Plaintext>) = json {
val base64 = Base64.getEncoder()
array(messages.map {
obj(
"type" to it.type.name,
"from" to it.from.address,
"to" to it.to?.address,
"subject" to it.subject,
"body" to it.text,
"conversationId" to it.conversationId.toString(),
"msgId" to it.inventoryVector?.hash?.let { base64.encodeToString(it) },
"encoding" to it.encodingCode,
"status" to it.status.name,
"message" to base64.encodeToString(it.message),
"ackData" to it.ackData?.let { base64.encodeToString(it) },
"ackMessage" to it.ackMessage?.let { base64.encodeToString(Encode.bytes(it)) },
"signature" to it.signature?.let { base64.encodeToString(it) },
"sent" to it.sent,
"received" to it.received,
"ttl" to it.ttl,
"labels" to array(it.labels.map { it.toString() })
)
})
}
fun importMessages(input: JsonArray<*>, labels: Map<String, Label>): List<Plaintext> {
return input.filterIsInstance(JsonObject::class.java).map { json ->
val base64 = Base64.getDecoder()
fun JsonObject.bytes(fieldName: String) = string(fieldName)?.let { base64.decode(it) }
Plaintext.Builder(Plaintext.Type.valueOf(json.string("type") ?: "MSG"))
.from(json.string("from")?.let { BitmessageAddress(it) } ?: throw IllegalArgumentException("'from' address expected"))
.to(json.string("to")?.let { BitmessageAddress(it) })
.conversation(json.string("conversationId")?.let { UUID.fromString(it) } ?: UUID.randomUUID())
.IV(json.bytes("msgId")?.let { InventoryVector(it) })
.encoding(json.long("encoding") ?: throw IllegalArgumentException("encoding expected"))
.status(json.string("status")?.let { Plaintext.Status.valueOf(it) } ?: throw IllegalArgumentException("status expected"))
.message(json.bytes("message") ?: throw IllegalArgumentException("message expected"))
.ackData(json.bytes("ackData"))
.ackMessage(json.bytes("ackMessage"))
.signature(json.bytes("signature"))
.sent(json.long("sent"))
.received(json.long("received"))
.ttl(json.long("ttl") ?: TTL.msg)
.labels(
json.array<String>("labels")?.map { labels[it] }?.filterNotNull() ?: emptyList()
)
.build()
}
}
fun importLabels(input: JsonArray<Any?>): List<Label> {
return input.filterIsInstance(JsonObject::class.java).map { json ->
Label(
label = json.string("label") ?: throw IllegalArgumentException("label expected"),
type = json.string("type")?.let { Label.Type.valueOf(it) },
color = json.int("color") ?: 0
)
}
}
fun createLabelMap(labels: List<Label>) = labels.associateBy { it.toString() }
}

View File

@ -0,0 +1,73 @@
/*
* Copyright 2017 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.exports
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography
import ch.dissem.bitmessage.entity.BitmessageAddress
import ch.dissem.bitmessage.utils.TestUtils
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.nullValue
import org.junit.Assert.assertThat
import org.junit.Test
class ContactExportTest {
init {
TestUtils.mockedInternalContext(cryptography = BouncyCryptography())
}
@Test
fun `ensure contacts are exported`() {
val alice = BitmessageAddress("BM-2cTtkBnb4BUYDndTKun6D9PjtueP2h1bQj")
alice.alias = "Alice"
alice.isSubscribed = true
val contacts = listOf(
BitmessageAddress("BM-2cWJ4UFRTCehWuWNsW8fJkAYMxU4S8jxci"),
TestUtils.loadContact(),
alice
)
val export = ContactExport.exportContacts(contacts)
print(export.toJsonString(true))
assertThat(ContactExport.importContacts(export), `is`(contacts))
}
@Test
fun `ensure private keys are omitted by default`() {
val contacts = listOf(
BitmessageAddress.chan(1, "test")
)
val export = ContactExport.exportContacts(contacts)
print(export.toJsonString(true))
val import = ContactExport.importContacts(export)
assertThat(import.size, `is`(1))
assertThat(import[0].isChan, `is`(true))
assertThat(import[0].privateKey, `is`(nullValue()))
}
@Test
fun `ensure private keys are exported if flag is set`() {
val contacts = listOf(
BitmessageAddress.chan(1, "test")
)
val export = ContactExport.exportContacts(contacts, true)
print(export.toJsonString(true))
val import = ContactExport.importContacts(export)
assertThat(import.size, `is`(1))
assertThat(import[0].isChan, `is`(true))
assertThat(import[0].privateKey, `is`(contacts[0].privateKey))
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2017 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.exports
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography
import ch.dissem.bitmessage.entity.BitmessageAddress
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.bitmessage.entity.valueobject.Label
import ch.dissem.bitmessage.utils.ConversationServiceTest
import ch.dissem.bitmessage.utils.Singleton
import ch.dissem.bitmessage.utils.TestUtils
import org.hamcrest.CoreMatchers.`is`
import org.junit.Assert.assertThat
import org.junit.Test
class MessageExportTest {
val inbox = Label("Inbox", Label.Type.INBOX, 0x0000ff)
val outbox = Label("Outbox", Label.Type.OUTBOX, 0x00ff00)
val unread = Label("Unread", Label.Type.UNREAD, 0x000000)
val trash = Label("Trash", Label.Type.TRASH, 0x555555)
val labels = listOf(
inbox,
outbox,
unread,
trash
)
val labelMap = MessageExport.createLabelMap(labels)
init {
TestUtils.mockedInternalContext(cryptography = BouncyCryptography())
}
@Test
fun `ensure labels are exported`() {
val export = MessageExport.exportLabels(labels)
print(export.toJsonString(true))
assertThat(MessageExport.importLabels(export), `is`(labels))
}
@Test
fun `ensure messages are exported`() {
val messages = listOf(
Plaintext.Builder(Plaintext.Type.MSG)
.ackData(Singleton.cryptography().randomBytes(32))
.from(BitmessageAddress("BM-2cWJ4UFRTCehWuWNsW8fJkAYMxU4S8jxci"))
.to(BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn"))
.message("Subject", "Message")
.status(Plaintext.Status.RECEIVED)
.labels(listOf(inbox))
.build(),
Plaintext.Builder(Plaintext.Type.BROADCAST)
.from(BitmessageAddress("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
.message("Subject", "Message")
.labels(listOf(inbox, unread))
.status(Plaintext.Status.SENT)
.build(),
Plaintext.Builder(Plaintext.Type.MSG)
.ttl(1)
.message("subject", "message")
.from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
.to(TestUtils.loadContact())
.labels(listOf(trash))
.status(Plaintext.Status.SENT_ACKNOWLEDGED)
.build(),
*ConversationServiceTest.conversation(
TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"),
TestUtils.loadContact()
).toTypedArray()
)
val export = MessageExport.exportMessages(messages)
print(export.toJsonString(true))
assertThat(MessageExport.importMessages(export, labelMap), `is`(messages))
}
}