Added exports for messages, labels and adddresses

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

View File

@ -28,6 +28,7 @@ subprojects {
repositories { repositories {
mavenCentral() mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
jcenter()
} }
dependencies { dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7" compile "org.jetbrains.kotlin:kotlin-stdlib-jre7"
@ -142,6 +143,7 @@ subprojects {
dependency 'com.madgag.spongycastle:prov:1.56.0.0' dependency 'com.madgag.spongycastle:prov:1.56.0.0'
dependency 'org.apache.commons:commons-lang3:3.6' dependency 'org.apache.commons:commons-lang3:3.6'
dependency 'org.flywaydb:flyway-core:4.2.0' dependency 'org.flywaydb:flyway-core:4.2.0'
dependency 'com.beust:klaxon:0.31'
dependency 'args4j:args4j:2.33' dependency 'args4j:args4j:2.33'
dependency 'org.ini4j:ini4j:0.5.4' dependency 'org.ini4j:ini4j:0.5.4'

View File

@ -438,7 +438,7 @@ class Plaintext private constructor(
if (this === other) return true if (this === other) return true
if (other !is Plaintext) return false if (other !is Plaintext) return false
return encoding == other.encoding && return encoding == other.encoding &&
from == other.from && from.address == other.from.address &&
Arrays.equals(message, other.message) && Arrays.equals(message, other.message) &&
ackMessage == other.ackMessage && ackMessage == other.ackMessage &&
Arrays.equals(to?.ripe, other.to?.ripe) && Arrays.equals(to?.ripe, other.to?.ripe) &&
@ -655,7 +655,7 @@ class Plaintext private constructor(
return this return this
} }
fun signature(signature: ByteArray): Builder { fun signature(signature: ByteArray?): Builder {
this.signature = signature this.signature = signature
return this return this
} }

View File

@ -34,48 +34,37 @@ import java.util.*
* Represents a private key. Additional information (stream, version, features, ...) is stored in the accompanying * Represents a private key. Additional information (stream, version, features, ...) is stored in the accompanying
* [Pubkey] object. * [Pubkey] object.
*/ */
class PrivateKey : Streamable { data class PrivateKey(
val privateSigningKey: ByteArray,
val privateSigningKey: ByteArray val privateEncryptionKey: ByteArray,
val privateEncryptionKey: ByteArray
val pubkey: Pubkey val pubkey: Pubkey
) : Streamable {
constructor(shorter: Boolean, stream: Long, nonceTrialsPerByte: Long, extraBytes: Long, vararg features: Pubkey.Feature) { constructor(
var privSK: ByteArray shorter: Boolean,
var pubSK: ByteArray stream: Long,
var privEK: ByteArray nonceTrialsPerByte: Long, extraBytes: Long,
var pubEK: ByteArray vararg features: Pubkey.Feature
var ripe: ByteArray ) : this(
do { Builder(version = Pubkey.LATEST_VERSION, stream = stream, shorter = shorter)
privSK = cryptography().randomBytes(PRIVATE_KEY_SIZE) .random()
privEK = cryptography().randomBytes(PRIVATE_KEY_SIZE) .nonceTrialsPerByte(nonceTrialsPerByte)
pubSK = cryptography().createPublicKey(privSK) .extraBytes(extraBytes)
pubEK = cryptography().createPublicKey(privEK) .features(features)
ripe = Pubkey.getRipe(pubSK, pubEK) .generate())
} while (ripe[0].toInt() != 0 || shorter && ripe[1].toInt() != 0)
this.privateSigningKey = privSK
this.privateEncryptionKey = privEK
this.pubkey = cryptography().createPubkey(Pubkey.LATEST_VERSION, stream, privateSigningKey, privateEncryptionKey,
nonceTrialsPerByte, extraBytes, *features)
}
constructor(privateSigningKey: ByteArray, privateEncryptionKey: ByteArray, pubkey: Pubkey) {
this.privateSigningKey = privateSigningKey
this.privateEncryptionKey = privateEncryptionKey
this.pubkey = pubkey
}
constructor(address: BitmessageAddress, passphrase: String) : this(address.version, address.stream, passphrase) constructor(address: BitmessageAddress, passphrase: String) : this(address.version, address.stream, passphrase)
constructor(version: Long, stream: Long, passphrase: String) : this(Builder(version, stream, false).seed(passphrase).generate()) constructor(version: Long, stream: Long, passphrase: String) : this(
Builder(version, stream, false).seed(passphrase).generate()
)
private constructor(builder: Builder) { private constructor(builder: Builder) : this(
this.privateSigningKey = builder.privSK!! builder.privSK!!, builder.privEK!!,
this.privateEncryptionKey = builder.privEK!! Factory.createPubkey(builder.version, builder.stream, builder.pubSK!!, builder.pubEK!!,
this.pubkey = Factory.createPubkey(builder.version, builder.stream, builder.pubSK!!, builder.pubEK!!, builder.nonceTrialsPerByte, builder.extraBytes, *builder.features)
InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE, InternalContext.NETWORK_EXTRA_BYTES) )
}
private class Builder internal constructor(internal val version: Long, internal val stream: Long, internal val shorter: Boolean) { private class Builder internal constructor(internal val version: Long, internal val stream: Long, internal val shorter: Boolean) {
@ -87,6 +76,30 @@ class PrivateKey : Streamable {
internal var pubSK: ByteArray? = null internal var pubSK: ByteArray? = null
internal var pubEK: ByteArray? = null internal var pubEK: ByteArray? = null
internal var nonceTrialsPerByte = InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE
internal var extraBytes = InternalContext.NETWORK_EXTRA_BYTES
internal var features: Array<out Pubkey.Feature> = emptyArray()
internal fun random(): Builder {
seed = cryptography().randomBytes(1024)
return this
}
fun nonceTrialsPerByte(nonceTrialsPerByte: Long): Builder {
this.nonceTrialsPerByte = nonceTrialsPerByte
return this
}
fun extraBytes(extraBytes: Long): Builder {
this.extraBytes = extraBytes
return this
}
fun features(features: Array<out Pubkey.Feature>): Builder {
this.features = features
return this
}
internal fun seed(passphrase: String): Builder { internal fun seed(passphrase: String): Builder {
try { try {
seed = passphrase.toByteArray(charset("UTF-8")) seed = passphrase.toByteArray(charset("UTF-8"))
@ -143,6 +156,13 @@ class PrivateKey : Streamable {
Encode.varBytes(privateEncryptionKey, buffer) Encode.varBytes(privateEncryptionKey, buffer)
} }
override fun equals(other: Any?) = other is PrivateKey
&& Arrays.equals(privateEncryptionKey, other.privateEncryptionKey)
&& Arrays.equals(privateSigningKey, other.privateSigningKey)
&& pubkey == other.pubkey
override fun hashCode() = pubkey.hashCode()
companion object { companion object {
@JvmField val PRIVATE_KEY_SIZE = 32 @JvmField val PRIVATE_KEY_SIZE = 32

View File

@ -36,6 +36,8 @@ class ConversationServiceTest : TestBase() {
private val messageRepository = mock<MessageRepository>() private val messageRepository = mock<MessageRepository>()
private val conversationService = spy(ConversationService(messageRepository)) private val conversationService = spy(ConversationService(messageRepository))
private val conversation = conversation(alice, bob)
@Test @Test
fun `ensure conversation is sorted properly`() { fun `ensure conversation is sorted properly`() {
MockitoKotlin.registerInstanceCreator { UUID.randomUUID() } MockitoKotlin.registerInstanceCreator { UUID.randomUUID() }
@ -46,8 +48,9 @@ class ConversationServiceTest : TestBase() {
assertThat(actual, `is`(expected)) assertThat(actual, `is`(expected))
} }
private val conversation: List<Plaintext> companion object {
get() { private var timer = 2
fun conversation(alice: BitmessageAddress, bob: BitmessageAddress): List<Plaintext> {
val result = LinkedList<Plaintext>() val result = LinkedList<Plaintext>()
val older = plaintext(alice, bob, val older = plaintext(alice, bob,
@ -99,9 +102,7 @@ class ConversationServiceTest : TestBase() {
return result return result
} }
private var timer = 2 fun plaintext(from: BitmessageAddress, to: BitmessageAddress,
private fun plaintext(from: BitmessageAddress, to: BitmessageAddress,
content: ExtendedEncoding, status: Plaintext.Status): Plaintext { content: ExtendedEncoding, status: Plaintext.Status): Plaintext {
val builder = Plaintext.Builder(MSG) val builder = Plaintext.Builder(MSG)
.IV(TestUtils.randomInventoryVector()) .IV(TestUtils.randomInventoryVector())
@ -115,3 +116,4 @@ class ConversationServiceTest : TestBase() {
return builder.build() return builder.build()
} }
} }
}

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))
}
}

View File

@ -1,6 +1,6 @@
#Sun Jul 02 11:22:52 CEST 2017 #Mon Jul 17 06:32:41 CEST 2017
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip

View File

@ -241,7 +241,6 @@ class NetworkHandlerTest {
Thread.sleep(100) Thread.sleep(100)
} catch (ignore: InterruptedException) { } catch (ignore: InterruptedException) {
} }
} while (ctx.isRunning()) } while (ctx.isRunning())
} }

View File

@ -15,3 +15,5 @@ include 'cryptography-sc'
include 'cryptography-bc' include 'cryptography-bc'
include 'extensions' include 'extensions'
include 'exports'