org.stellar.walletsdk.horizon.Account.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wallet-sdk Show documentation
Show all versions of wallet-sdk Show documentation
Kotlin Stellar Wallet SDK
package org.stellar.walletsdk.horizon
import org.stellar.sdk.AbstractTransaction
import org.stellar.sdk.KeyPair
/**
* Stellar account's key pair. It can be either [PublicKeyPair] obtained from public key, or
* [SigningKeyPair], obtained from private key. Existing account in string format can be converted
* to public key pair via calling [toPublicKeyPair] helper function.
*/
sealed interface AccountKeyPair {
val keyPair: KeyPair
val address: String
get() = keyPair.accountId
val publicKey: ByteArray
get() = keyPair.publicKey
}
data class PublicKeyPair(override val keyPair: KeyPair) : AccountKeyPair {
override fun toString() = "${PublicKeyPair::class.simpleName}(address=$address)"
}
data class SigningKeyPair(override val keyPair: KeyPair) : AccountKeyPair {
init {
require(keyPair.canSign()) { "This keypair doesn't have private key and can't sign" }
}
val secretKey: String
get() = keyPair.secretSeed.concatToString()
fun sign(transaction: T): T {
return transaction.sign(this)
}
companion object {
@JvmStatic
fun fromSecret(secret: String): SigningKeyPair {
return SigningKeyPair(KeyPair.fromSecretSeed(secret))
}
}
override fun toString() = "${SigningKeyPair::class.simpleName}(address=$address)"
}
fun T.sign(keyPair: SigningKeyPair): T {
return this.also { this.sign(keyPair.keyPair) }
}
fun String.toPublicKeyPair(): PublicKeyPair {
return PublicKeyPair(KeyPair.fromAccountId(this))
}