Skip to content

Commit

Permalink
Merge pull request kin-labs#24 from kin-labs/optimizations
Browse files Browse the repository at this point in the history
Optimizations
  • Loading branch information
samdowd authored Jan 26, 2023
2 parents 1271e7b + 995eaf0 commit cba7778
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 84 deletions.
76 changes: 74 additions & 2 deletions api-swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"tags": ["account"]
}
},
"/api/account/info/{environment}/{index}/{accountId}": {
"/api/account/info/{environment}/{index}/{accountId}/{mint}": {
"get": {
"operationId": "getAccountInfo",
"summary": "",
Expand Down Expand Up @@ -90,6 +90,14 @@
"type": "string"
}
},
{
"name": "mint",
"required": true,
"in": "path",
"schema": {
"type": "string"
}
},
{
"name": "commitment",
"required": true,
Expand Down Expand Up @@ -399,6 +407,70 @@
"tags": ["app"]
}
},
"/api/transaction/kinetic-transaction/{environment}/{index}": {
"get": {
"operationId": "getKineticTransaction",
"summary": "",
"parameters": [
{
"name": "environment",
"required": true,
"in": "path",
"schema": {
"type": "string"
}
},
{
"name": "index",
"required": true,
"in": "path",
"schema": {
"type": "integer"
}
},
{
"name": "referenceId",
"required": true,
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "referenceType",
"required": true,
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "signature",
"required": true,
"in": "query",
"schema": {
"type": "string"
}
}
],
"responses": {
"default": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Transaction"
}
}
}
}
}
},
"tags": ["transaction"]
}
},
"/api/transaction/latest-blockhash/{environment}/{index}": {
"get": {
"operationId": "getLatestBlockhash",
Expand Down Expand Up @@ -568,7 +640,7 @@
"info": {
"title": "@kin-kinetic/api",
"description": "The OpenAPI definition of the Kinetic API",
"version": "1.0.0-rc.12",
"version": "1.0.0-rc.14",
"contact": {}
},
"tags": [
Expand Down
54 changes: 38 additions & 16 deletions app/src/main/java/org/kin/kineticdemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ import kotlinx.coroutines.*
import org.kin.kinetic.KineticSdkConfig

class MainActivity : AppCompatActivity() {
private lateinit var getConfigButton: Button
private lateinit var serverConfigText: TextView
private lateinit var getBalanceButton: Button
private lateinit var kinBalanceText: TextView
private lateinit var getTokenAccountsButton: Button
private lateinit var tokenAccountsText: TextView
private lateinit var getAccountHistoryButton: Button
private lateinit var accountHistoryText: TextView
private lateinit var airdropButton: Button
private lateinit var airdropText: TextView
private lateinit var closeAccountButton: Button
private lateinit var closeAccountText: TextView
private lateinit var createAccountButton: Button
private lateinit var createAccountText: TextView
private lateinit var getAccountHistoryButton: Button
private lateinit var getAccountInfoButton: Button
private lateinit var getBalanceButton: Button
private lateinit var getConfigButton: Button
private lateinit var getTokenAccountsButton: Button
private lateinit var getTransactionButton: Button
private lateinit var kinAccountInfoText: TextView
private lateinit var kinBalanceText: TextView
private lateinit var makeTransferButton: Button
private lateinit var makeTransferText: TextView
private lateinit var getTransactionButton: Button
private lateinit var serverConfigText: TextView
private lateinit var tokenAccountsText: TextView
private lateinit var transactionText: TextView

private var kinetic: KineticSdk? = null
Expand Down Expand Up @@ -63,21 +67,25 @@ class MainActivity : AppCompatActivity() {
}
}

getConfigButton = findViewById(R.id.get_config_button)
serverConfigText = findViewById(R.id.server_config_text)
getBalanceButton = findViewById(R.id.get_balance_button)
kinBalanceText = findViewById(R.id.kin_balance_text)
getTokenAccountsButton = findViewById(R.id.get_token_accounts_button)
tokenAccountsText = findViewById(R.id.token_accounts_text)
getAccountHistoryButton = findViewById(R.id.get_account_history_button)
accountHistoryText = findViewById(R.id.account_history_text)
airdropButton = findViewById(R.id.airdrop_button)
airdropText = findViewById(R.id.airdrop_text)
closeAccountButton = findViewById(R.id.close_account_button)
closeAccountText = findViewById(R.id.close_account_text)
createAccountButton = findViewById(R.id.create_account_button)
createAccountText = findViewById(R.id.create_account_text)
getAccountHistoryButton = findViewById(R.id.get_account_history_button)
getAccountInfoButton = findViewById(R.id.get_account_info_button)
getBalanceButton = findViewById(R.id.get_balance_button)
getConfigButton = findViewById(R.id.get_config_button)
getTokenAccountsButton = findViewById(R.id.get_token_accounts_button)
getTransactionButton = findViewById(R.id.get_transaction_button)
kinAccountInfoText = findViewById(R.id.kin_account_info_text)
kinBalanceText = findViewById(R.id.kin_balance_text)
makeTransferButton = findViewById(R.id.make_transfer_button)
makeTransferText = findViewById(R.id.make_transfer_text)
getTransactionButton = findViewById(R.id.get_transaction_button)
serverConfigText = findViewById(R.id.server_config_text)
tokenAccountsText = findViewById(R.id.token_accounts_text)
transactionText = findViewById(R.id.transaction_text)

getConfigButton.setOnClickListener {
Expand All @@ -87,6 +95,13 @@ class MainActivity : AppCompatActivity() {
}
}

getAccountInfoButton.setOnClickListener {
kineticNetworkScope.launch {
val res = kinetic?.getAccountInfo(account!!.publicKey)
runOnUiThread { kinAccountInfoText.text = res.toString() }
}
}

getBalanceButton.setOnClickListener {
kineticNetworkScope.launch {
val res = kinetic?.getBalance(account!!.publicKey)
Expand Down Expand Up @@ -115,6 +130,13 @@ class MainActivity : AppCompatActivity() {
}
}

closeAccountButton.setOnClickListener {
kineticNetworkScope.launch {
val res = kinetic?.closeAccount(account!!.publicKey)
runOnUiThread { closeAccountText.text = res.toString() }
}
}

createAccountButton.setOnClickListener {
kineticNetworkScope.launch {
val res = kinetic?.createAccount(owner = account!!)
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@
android:textAlignment="center"
android:textSize="9sp" />

<Button
android:id="@+id/get_account_info_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Account Info" />

<TextView
android:id="@+id/kin_account_info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="9sp" />

<Button
android:id="@+id/get_balance_button"
android:layout_width="match_parent"
Expand Down Expand Up @@ -119,6 +132,19 @@
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="9sp" />

<Button
android:id="@+id/close_account_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close Account" />

<TextView
android:id="@+id/close_account_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="9sp" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
12 changes: 10 additions & 2 deletions kinetic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ plugins {
id 'de.undercouch.download'
}

ext {
LIBRARY_NAME = '"Kinetic Android SDK"'
LIBRARY_VERSION = '"1.0.0-rc.12"'
}

android {
namespace = "org.kin.kinetic"

Expand All @@ -15,6 +20,9 @@ android {
minSdk 26
targetSdk 33

buildConfigField 'String', 'LIBRARY_NAME', LIBRARY_NAME
buildConfigField 'String', 'LIBRARY_VERSION', LIBRARY_VERSION

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
Expand Down Expand Up @@ -53,7 +61,7 @@ dependencies {

implementation "com.squareup.okhttp3:okhttp:4.9.3"

api 'com.github.metaplex-foundation.SolanaKT:solana:1.2'
api 'com.github.metaplex-foundation.SolanaKT:solana:786b4a9'

implementation 'com.google.code.gson:gson:2.8.6'
}
Expand Down Expand Up @@ -100,7 +108,7 @@ publishing {
maven(MavenPublication) {
groupId = "org.kin"
artifactId = "kinetic"
version = "1.0.0-rc.12"
version = LIBRARY_VERSION

afterEvaluate {
from components.release
Expand Down
6 changes: 6 additions & 0 deletions kinetic/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-keep class org.kin.kinetic.**{*;}
-keep class org.kin.kinetic.**$* {*;}
-keep class com.solana.**{*;}

-keep class org.bitcoinj.**{*;}
-keep class org.bitcoin.**{*;}
4 changes: 0 additions & 4 deletions kinetic/src/main/java/org/kin/kinetic/Constants.kt

This file was deleted.

30 changes: 21 additions & 9 deletions kinetic/src/main/java/org/kin/kinetic/Keypair.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.bitcoinj.crypto.MnemonicCode
import org.bitcoinj.script.Script
import org.bitcoinj.wallet.Wallet


class Keypair {
private var keyPair: TweetNaclFast.Signature.KeyPair
var mnemonic: List<String>? = null
Expand Down Expand Up @@ -49,18 +48,31 @@ class Keypair {
}

fun fromMnemonic(mnemonic: List<String>): Keypair {
val seed = MnemonicCode.toSeed(mnemonic, "")
val privateKey = SolanaBip44().getPrivateKeyFromSeed(seed, DerivableType.BIP44CHANGE)
var keypair = TweetNaclFast.Signature.keyPair_fromSeed(privateKey)
return fromMnemonicSet(mnemonic)[0]
}

fun fromMnemonicSet(mnemonic: List<String>, from: Int = 0, to: Int = 10): List<Keypair> {
// Always start with zero as minimum
val from = if (from < 0) 0 else from
// Always generate at least 1
val to = if (to <= from) from + 1 else to

return Keypair(keypair).apply {
this.mnemonic = mnemonic
val seed = MnemonicCode.toSeed(mnemonic, "")
var keys: List<Keypair> = emptyList()
for (i in from until to) {
var kp = derive(seed, i)
kp.mnemonic = mnemonic
keys += kp
}

return keys
}

// TODO: Not implemented, need update to SolanaKT to allow passing in account number
// fun fromMnemonicSet(mnemonic: List<String>, from: Int = 0, to: Int = 10): List<Keypair> {
// }
fun derive(seed: ByteArray, walletIndex: Int): Keypair {
val privateKey = SolanaBip44().getPrivateKeyFromSeed(seed, DerivableType.BIP44CHANGE, walletIndex.toLong())
var kp = Keypair(TweetNaclFast.Signature.keyPair_fromSeed(privateKey))
return kp
}

fun fromSecretKey(secretKey: String): Keypair {
return Keypair(secretKey)
Expand Down
24 changes: 22 additions & 2 deletions kinetic/src/main/java/org/kin/kinetic/KineticSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.kin.kinetic

import org.kin.kinetic.helpers.getSolanaRPCEndpoint
import com.solana.Solana
import com.solana.networking.OkHttpNetworkingRouter
import com.solana.networking.HttpNetworkingRouter
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import org.kin.kinetic.generated.api.model.*
Expand Down Expand Up @@ -37,6 +37,22 @@ class KineticSdk {
return this.sdkConfig.solanaRpcEndpoint
}

suspend fun closeAccount(
account: String,
commitment: Commitment? = null,
mint: String? = null,
referenceId: String? = null,
referenceType: String? = null
): Transaction {
return internal.closeAccount(
account,
commitment,
mint,
referenceId,
referenceType
)
}

suspend fun createAccount(
owner: Keypair,
commitment: Commitment? = null,
Expand All @@ -53,6 +69,10 @@ class KineticSdk {
)
}

suspend fun getAccountInfo(account: String, commitment: Commitment? = null, mint: String? = null): AccountInfo {
return internal.getAccountInfo(account, commitment, mint)
}

suspend fun getBalance(account: String, commitment: Commitment? = null): BalanceResponse {
return internal.getBalance(account, commitment)
}
Expand Down Expand Up @@ -115,7 +135,7 @@ class KineticSdk {
val config = internal.getAppConfig(sdkConfig.environment, sdkConfig.index)
val rpcEndpoint = if (sdkConfig.solanaRpcEndpoint != null) getSolanaRPCEndpoint(sdkConfig.solanaRpcEndpoint)
else getSolanaRPCEndpoint(config.environment.cluster.endpoint)
val networkingRouter = OkHttpNetworkingRouter(rpcEndpoint)
val networkingRouter = HttpNetworkingRouter(rpcEndpoint)
solana = Solana(networkingRouter)
return config
}
Expand Down
Loading

0 comments on commit cba7778

Please sign in to comment.