Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Impl the Confirmation Binding Interface #145

Merged
merged 4 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.clipevery.presist.data
package com.clipevery.dao

import app.cash.sqldelight.db.SqlDriver
import com.clipevery.Database
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.clipevery.dao.store

import org.signal.libsignal.protocol.state.IdentityKeyStore

interface IdentityKeyStoreFactory {

fun createIdentityKeyStore(): IdentityKeyStore
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.clipevery.exception

class ClipException: RuntimeException {

private val errorCode: ErrorCode

constructor(errorCode: ErrorCode) : super() {
this.errorCode = errorCode
}

constructor(errorCode: ErrorCode, message: String) : super(message) {
this.errorCode = errorCode
}

constructor(errorCode: ErrorCode, message: String, cause: Throwable) : super(message, cause) {
this.errorCode = errorCode
}

constructor(errorCode: ErrorCode, cause: Throwable) : super(cause) {
this.errorCode = errorCode
}

fun getErrorCode(): ErrorCode {
return errorCode
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.clipevery.exception

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import java.util.Objects

class ErrorCode @JsonCreator constructor(
@JsonProperty("code") code: Int,
@JsonProperty("name") name: String,
@JsonProperty("type") type: ErrorType) {
@get:JsonProperty
val code: Int

@get:JsonProperty
val name: String
private val type: ErrorType

init {
require(code >= 0) { "code is negative" }
this.code = code
this.name = name
this.type = type
}

@JsonProperty
fun getType(): ErrorType {
return type
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (other == null || javaClass != other.javaClass) {
return false
}
val errorCode = other as ErrorCode
return code == errorCode.code && name == errorCode.name && type === errorCode.type
}

override fun hashCode(): Int {
return Objects.hash(code, name, type)
}
}


enum class ErrorType {
USER_ERROR,
INTERNAL_ERROR,
EXTERNAL
}


interface ErrorCodeSupplier {
fun toErrorCode(): ErrorCode
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.clipevery.exception

enum class StandardErrorCode(code: Int, errorType: ErrorType): ErrorCodeSupplier {
UNKNOWN_ERROR(0, ErrorType.INTERNAL_ERROR),
BOOTSTRAP_ERROR(1, ErrorType.INTERNAL_ERROR),

SYNC_TIMEOUT(1000, ErrorType.USER_ERROR),
SYNC_INVALID(1001, ErrorType.USER_ERROR);

private val errorCode: ErrorCode

init {
errorCode = ErrorCode(code, name, errorType)
}

override fun toErrorCode(): ErrorCode {
return errorCode
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.clipevery.model

import kotlinx.serialization.Serializable

const val AppName: String = "Clipevery"

@Serializable
data class AppInfo(
val appInstanceId: String,
val appVersion: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import java.io.DataOutputStream
data class RequestEndpointInfo(val deviceInfo: DeviceInfo,
val port: Int) {

fun getBase64Encode(salt: Int): String {
fun getBase64Encode(token: Int): String {
val byteStream = ByteArrayOutputStream()
val dataStream = DataOutputStream(byteStream)
encodeDeviceInfo(dataStream)
dataStream.writeInt(port)
val byteArray = byteStream.toByteArray()
val size = byteArray.size
val offset = salt % size
val offset = token % size
val byteArrayRotate = byteArray.rotate(offset)
val saltByteStream = ByteArrayOutputStream()
val saltDataStream = DataOutputStream(saltByteStream)
saltDataStream.write(byteArrayRotate)
saltDataStream.writeInt(salt)
saltDataStream.writeInt(token)
return base64Encode(saltByteStream.toByteArray())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package com.clipevery.model.sync

import com.clipevery.model.AppInfo
import com.clipevery.model.RequestEndpointInfo
import kotlinx.serialization.Serializable

@Serializable
data class RequestSyncInfo(val requestEndpointInfo: RequestEndpointInfo,
val preKeyBundle: ByteArray) {
data class RequestSyncInfo(val appInfo: AppInfo,
val requestEndpointInfo: RequestEndpointInfo,
val preKeyBundle: ByteArray,
val token: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as RequestSyncInfo

if (appInfo != other.appInfo) return false
if (requestEndpointInfo != other.requestEndpointInfo) return false
if (!preKeyBundle.contentEquals(other.preKeyBundle)) return false
if (token != other.token) return false

return true
}

override fun hashCode(): Int {
var result = requestEndpointInfo.hashCode()
var result = appInfo.hashCode()
result = 31 * result + requestEndpointInfo.hashCode()
result = 31 * result + preKeyBundle.contentHashCode()
result = 31 * result + token
return result
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.clipevery.model.sync

import com.clipevery.model.AppInfo
import com.clipevery.model.RequestEndpointInfo

data class ResponseSyncInfo(val appInfo: AppInfo,
val requestEndpointInfo: RequestEndpointInfo
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.clipevery.net

import com.clipevery.exception.ClipException
import com.clipevery.model.SyncInfo
import com.clipevery.model.sync.RequestSyncInfo
import org.signal.libsignal.protocol.state.PreKeyBundle

interface SyncValidator {

fun createToken(): Int

fun getCurrentToken(): Int

fun getRefreshTime(): Long

@Throws(ClipException::class)
suspend fun validate(requestSyncInfo: RequestSyncInfo): SyncInfoWithPreKeyBundle
}

data class SyncInfoWithPreKeyBundle(val syncInfo: SyncInfo, val preKeyBundle: PreKeyBundle)
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
CREATE TABLE identityKey (
app_instance_id TEXT NOT NULL PRIMARY KEY,
registrationId INTEGER NOT NULL,
serialized BLOB NOT NULL
);

select:
SELECT * FROM identityKey WHERE app_instance_id = ?;

tryInit:
INSERT OR FAIL INTO identityKey (app_instance_id, registrationId, serialized) VALUES (?, ?, ?);
selectIndentity:
SELECT serialized FROM identityKey WHERE app_instance_id = ?;

update:
UPDATE identityKey SET serialized = ? WHERE app_instance_id = ? AND registrationId = ?;
UPDATE identityKey SET serialized = ? WHERE app_instance_id = ?;

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE preKey (
id INTEGER PRIMARY KEY,
id INTEGER NOT NULL PRIMARY KEY ,
serialized BLOB NOT NULL
);

Expand Down
22 changes: 22 additions & 0 deletions composeApp/src/commonMain/sqldelight/com/clipevery/data/Session.sq
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE TABLE session (
app_instance_id TEXT NOT NULL PRIMARY KEY,
sessionRecord BLOB NOT NULL
);

selectSessionRecord:
SELECT sessionRecord FROM session WHERE app_instance_id = ?;

selectSessionRecords:
SELECT sessionRecord FROM session WHERE app_instance_id IN ?;

selectSubDevice:
SELECT app_instance_id FROM session WHERE app_instance_id = ?;

updateSessionRecord:
UPDATE session SET sessionRecord = ? WHERE app_instance_id = ?;

count:
SELECT COUNT(1) FROM session WHERE app_instance_id = ?;

delete:
DELETE FROM session WHERE app_instance_id = ?;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE signedPreKey (
id INTEGER PRIMARY KEY,
id INTEGER NOT NULL PRIMARY KEY ,
serialized BLOB NOT NULL
);

Expand Down
37 changes: 5 additions & 32 deletions composeApp/src/commonMain/sqldelight/com/clipevery/data/Sync.sq
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,25 @@ CREATE TABLE syncInfo (
app_instance_id TEXT NOT NULL PRIMARY KEY,
app_version TEXT NOT NULL,
app_user_name TEXT NOT NULL,
device_id TEXT NOT NULL,
device_id TEXT NOT NULL,
device_name TEXT NOT NULL,
device_state TEXT NOT NULL,
sync_state TEXT NOT NULL,
host_address TEXT NOT NULL,
port INTEGER NOT NULL,
platform_name TEXT NOT NULL,
platform_arch TEXT NOT NULL,
platform_bit_mode INTEGER NOT NULL,
platform_version TEXT NOT NULL,
public_key BLOB,
sessionRecord BLOB
platform_version TEXT NOT NULL
);


CREATE INDEX device_state_index ON syncInfo (device_state);

selectAll:
SELECT *
FROM syncInfo;

selectSessionRecord:
SELECT sessionRecord FROM syncInfo WHERE app_instance_id = ?;

selectSessionRecords:
SELECT sessionRecord FROM syncInfo WHERE app_instance_id IN ?;

selectPublicKey:
SELECT public_key FROM syncInfo WHERE app_instance_id = ?;

selectSubDevice:
SELECT app_instance_id FROM syncInfo WHERE app_instance_id = ?;
CREATE INDEX sync_state_index ON syncInfo (sync_state);

insert:
INSERT INTO syncInfo(app_instance_id, app_version, app_user_name,
device_id, device_name, device_state, host_address, port, platform_name,
device_id, device_name, sync_state, host_address, port, platform_name,
platform_arch, platform_bit_mode, platform_version)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

updatePublicKey:
UPDATE syncInfo SET public_key = ? WHERE app_instance_id = ?;

updateSessionRecord:
UPDATE syncInfo SET sessionRecord = ? WHERE app_instance_id = ?;

count:
SELECT COUNT(1) FROM syncInfo WHERE app_instance_id = ?;

delete:
DELETE FROM syncInfo WHERE app_instance_id = ?;
Loading
Loading