zcash-android-wallet-sdk/samples/demo-app/app/src/main/java/cash/z/ecc/android/sdk/demoapp/demos/getprivatekey/GetPrivateKeyFragment.kt

55 lines
2.6 KiB
Kotlin
Raw Normal View History

package cash.z.ecc.android.sdk.demoapp.demos.getprivatekey
import android.view.LayoutInflater
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
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.
*/
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>
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.
*/
2020-02-24 11:51:45 -08:00
spendingKeys = initializer.new(seed, birthday)
2019-11-01 13:25:28 -07: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()))
}
override fun onResetComplete() {
binding.textInfo.setText("Spending Key:\n${spendingKeys[0]}\n\nViewing Key:\n${viewingKeys[0]}")
}
override fun onClear() {
2019-11-01 13:25:28 -07:00
initializer.clear()
}
2020-06-09 20:28:21 -07:00
}