2020-06-10 00:08:19 -07:00
|
|
|
package cash.z.ecc.android.sdk.demoapp.demos.getprivatekey
|
2019-10-21 03:17:07 -07:00
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
2020-06-10 00:08:19 -07:00
|
|
|
import cash.z.ecc.android.sdk.Initializer
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.App
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.BaseDemoFragment
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.databinding.FragmentGetPrivateKeyBinding
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-02-27 00:25:07 -08:00
|
|
|
/**
|
|
|
|
* Displays the viewing key and spending key associated with the seed defined by the default config.
|
|
|
|
* To modify the seed that is used, update the `DemoConfig.seedWords` value. This demo takes two
|
|
|
|
* approaches to deriving the seed, one that is stateless and another that is not. In most cases, a
|
|
|
|
* wallet instance will call `new` on an initializer and then store the result.
|
|
|
|
*/
|
2019-10-21 03:17:07 -07:00
|
|
|
class GetPrivateKeyFragment : BaseDemoFragment<FragmentGetPrivateKeyBinding>() {
|
|
|
|
private var seed: ByteArray = App.instance.defaultConfig.seed
|
2019-11-01 13:25:28 -07:00
|
|
|
private val initializer: Initializer = Initializer(App.instance)
|
2020-02-24 11:51:45 -08:00
|
|
|
private val birthday = App.instance.defaultConfig.newWalletBirthday()
|
2019-11-01 13:25:28 -07:00
|
|
|
private lateinit var spendingKeys: Array<String>
|
|
|
|
private lateinit var viewingKeys: Array<String>
|
2019-10-21 03:17:07 -07:00
|
|
|
|
|
|
|
override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetPrivateKeyBinding =
|
|
|
|
FragmentGetPrivateKeyBinding.inflate(layoutInflater)
|
|
|
|
|
|
|
|
override fun resetInBackground() {
|
|
|
|
/*
|
|
|
|
* Initialize with the seed and retrieve one private key for each account specified (by
|
|
|
|
* default, only 1 account is created). In a normal circumstance, a wallet app would then
|
2020-02-27 00:25:07 -08:00
|
|
|
* store these keys in its secure storage for retrieval, later. Spending keys are only
|
|
|
|
* needed when sending funds. Viewing keys can be derived from spending keys. In most cases,
|
|
|
|
* a call to `initializer.new` or `initializer.import` are the only time a wallet passes the
|
|
|
|
* seed to the SDK. From that point forward, only spending or viewing keys are needed.
|
2019-10-21 03:17:07 -07:00
|
|
|
*/
|
2020-02-24 11:51:45 -08:00
|
|
|
spendingKeys = initializer.new(seed, birthday)
|
2019-11-01 13:25:28 -07:00
|
|
|
|
|
|
|
/*
|
2020-02-25 23:43:27 -08:00
|
|
|
* Alternatively, viewing keys can also be derived directly from a seed or spending keys.
|
2019-11-01 13:25:28 -07:00
|
|
|
*/
|
|
|
|
viewingKeys = initializer.deriveViewingKeys(seed)
|
|
|
|
|
|
|
|
// just for demonstration purposes to show that these approaches produce the same result.
|
|
|
|
require(spendingKeys.first() == initializer.deriveSpendingKeys(seed).first())
|
|
|
|
require(viewingKeys.first() == initializer.deriveViewingKey(spendingKeys.first()))
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResetComplete() {
|
2020-07-27 11:39:43 -07:00
|
|
|
binding.textInfo.setText("Spending Key:\n${spendingKeys[0]}\n\nViewing Key:\n${viewingKeys[0]}")
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClear() {
|
2019-11-01 13:25:28 -07:00
|
|
|
initializer.clear()
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:28:21 -07:00
|
|
|
}
|