diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/apisync/blockchair/BlockchairApi.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/apisync/blockchair/BlockchairApi.kt index f8098293..ec36fc0c 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/apisync/blockchair/BlockchairApi.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/apisync/blockchair/BlockchairApi.kt @@ -1,5 +1,7 @@ package io.horizontalsystems.bitcoincore.apisync.blockchair +import com.eclipsesource.json.Json +import com.eclipsesource.json.JsonObject import io.horizontalsystems.bitcoincore.apisync.model.AddressItem import io.horizontalsystems.bitcoincore.apisync.model.BlockHeaderItem import io.horizontalsystems.bitcoincore.apisync.model.TransactionItem @@ -77,7 +79,14 @@ class BlockchairApi( } fun broadcastTransaction(rawTransactionHex: String) { - apiManager.post("$chainId/push/transaction", "{ \"data\" : \"$rawTransactionHex\"}") + val apiManager = ApiManager("https://api.blockchair.com") + val url = "$chainId/push/transaction" + + val body = JsonObject().apply { + this["data"] = Json.value(rawTransactionHex) + }.toString() + + apiManager.post(url, body) } private fun fetchTransactions( @@ -129,7 +138,7 @@ class BlockchairApi( private fun dateStringToTimestamp(date: String): Long? { return try { - dateFormat.parse(date)?.time?.let { it / 1000 } + dateFormat.parse(date)?.time?.let { it / 1000 } } catch (e: ParseException) { null } diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/ApiManager.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/ApiManager.kt index 602bdc10..47789c6a 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/ApiManager.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/managers/ApiManager.kt @@ -1,10 +1,11 @@ package io.horizontalsystems.bitcoincore.managers -import android.util.Log import com.eclipsesource.json.Json import com.eclipsesource.json.JsonValue +import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.OkHttpClient import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import java.io.BufferedOutputStream import java.io.BufferedWriter import java.io.IOException @@ -31,6 +32,7 @@ class ApiManager(private val host: String) { connectTimeout = 5000 readTimeout = 60000 setRequestProperty("Accept", "application/json") + setRequestProperty("content-type", "application/json") }.getInputStream() } catch (exception: IOException) { throw ApiManagerException.Other("${exception.javaClass.simpleName}: $host") @@ -42,14 +44,12 @@ class ApiManager(private val host: String) { try { val path = "$host/$resource" - Log.e("e", "path: $path") - Log.e("e", "body: $data") - logger.info("Fetching $path") val url = URL(path) val urlConnection = url.openConnection() as HttpURLConnection urlConnection.requestMethod = "POST" + urlConnection.setRequestProperty("Content-Type", "application/json") val out = BufferedOutputStream(urlConnection.outputStream) val writer = BufferedWriter(OutputStreamWriter(out, "UTF-8")) writer.write(data) @@ -99,6 +99,7 @@ class ApiManager(private val host: String) { throw ApiManagerException.Other("${e.javaClass.simpleName}: $host, ${e.localizedMessage}") } } + } sealed class ApiManagerException : Exception() { diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSender.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSender.kt index 7f532304..53a5de4d 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSender.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSender.kt @@ -1,6 +1,5 @@ package io.horizontalsystems.bitcoincore.transactions -import android.util.Log import io.horizontalsystems.bitcoincore.BitcoinCore import io.horizontalsystems.bitcoincore.apisync.blockchair.BlockchairApi import io.horizontalsystems.bitcoincore.core.IInitialDownload @@ -102,16 +101,14 @@ class TransactionSender( } private fun sendViaAPI(transactions: List, blockchairApi: BlockchairApi) { - timer.startIfNotRunning() - transactions.forEach { transaction -> try { - transactionSendStart(transaction) - val hex = transactionSerializer.serialize(transaction).toHexString() blockchairApi.broadcastTransaction(hex) + + transactionSyncer.handleRelayed(listOf(transaction)) } catch (error: Throwable) { - Log.e("e", "API send error", error) + transactionSyncer.handleInvalid(transaction) } } } @@ -172,6 +169,7 @@ class TransactionSender( transactionSendSuccess(task.transaction) true } + else -> false } }