All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.stellar.walletsdk.horizon.Account.kt Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
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))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy