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
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
import android.os.Bundle
|
2019-10-21 03:17:07 -07:00
|
|
|
import android.view.LayoutInflater
|
2021-10-21 13:05:02 -07:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2020-08-13 18:08:01 -07:00
|
|
|
import cash.z.ecc.android.bip39.Mnemonics
|
|
|
|
import cash.z.ecc.android.bip39.toSeed
|
2020-06-10 00:08:19 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.BaseDemoFragment
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.databinding.FragmentGetPrivateKeyBinding
|
2021-09-04 04:05:41 -07:00
|
|
|
import cash.z.ecc.android.sdk.demoapp.ext.requireApplicationContext
|
|
|
|
import cash.z.ecc.android.sdk.demoapp.util.fromResources
|
2022-08-02 06:29:09 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.ZcashNetwork
|
2020-09-11 00:16:46 -07:00
|
|
|
import cash.z.ecc.android.sdk.tool.DerivationTool
|
2021-10-21 13:05:02 -07:00
|
|
|
import kotlinx.coroutines.launch
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-02-27 00:25:07 -08:00
|
|
|
/**
|
2020-08-13 18:08:01 -07:00
|
|
|
* Displays the viewing key and spending key associated with the seed used during the demo. The
|
|
|
|
* seedPhrase defaults to the value of`DemoConfig.seedWords` but can be set by the user on the
|
|
|
|
* HomeFragment.
|
2020-02-27 00:25:07 -08:00
|
|
|
*/
|
2019-10-21 03:17:07 -07:00
|
|
|
class GetPrivateKeyFragment : BaseDemoFragment<FragmentGetPrivateKeyBinding>() {
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
private lateinit var seedPhrase: String
|
|
|
|
private lateinit var seed: ByteArray
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the required values that would normally live outside the demo but are repeated
|
|
|
|
* here for completeness so that each demo file can serve as a standalone example.
|
|
|
|
*/
|
2020-09-11 00:16:46 -07:00
|
|
|
private fun setup() {
|
2020-08-13 18:08:01 -07:00
|
|
|
// defaults to the value of `DemoConfig.seedWords` but can also be set by the user
|
|
|
|
seedPhrase = sharedViewModel.seedPhrase.value
|
|
|
|
|
|
|
|
// Use a BIP-39 library to convert a seed phrase into a byte array. Most wallets already
|
|
|
|
// have the seed stored
|
|
|
|
seed = Mnemonics.MnemonicCode(seedPhrase).toSeed()
|
|
|
|
}
|
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
private fun displayKeys() {
|
2020-08-13 18:08:01 -07:00
|
|
|
// derive the keys from the seed:
|
|
|
|
// demonstrate deriving spending keys for five accounts but only take the first one
|
2021-10-21 13:05:02 -07:00
|
|
|
lifecycleScope.launchWhenStarted {
|
|
|
|
val spendingKey = DerivationTool.deriveSpendingKeys(
|
|
|
|
seed,
|
|
|
|
ZcashNetwork.fromResources(requireApplicationContext()),
|
|
|
|
5
|
|
|
|
).first()
|
2020-09-11 00:16:46 -07:00
|
|
|
|
2021-10-21 13:05:02 -07:00
|
|
|
// derive the key that allows you to view but not spend transactions
|
|
|
|
val viewingKey = DerivationTool.deriveViewingKey(
|
|
|
|
spendingKey,
|
|
|
|
ZcashNetwork.fromResources(requireApplicationContext())
|
|
|
|
)
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2021-10-21 13:05:02 -07:00
|
|
|
// display the keys in the UI
|
|
|
|
binding.textInfo.setText("Spending Key:\n$spendingKey\n\nViewing Key:\n$viewingKey")
|
|
|
|
}
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
//
|
|
|
|
// Android Lifecycle overrides
|
|
|
|
//
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
setup()
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
displayKeys()
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
//
|
|
|
|
// Base Fragment overrides
|
|
|
|
//
|
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
override fun onActionButtonClicked() {
|
2021-10-21 13:05:02 -07:00
|
|
|
lifecycleScope.launch {
|
|
|
|
copyToClipboard(
|
|
|
|
DerivationTool.deriveUnifiedViewingKeys(
|
|
|
|
seed,
|
|
|
|
ZcashNetwork.fromResources(requireApplicationContext())
|
|
|
|
).first().extpub,
|
|
|
|
"ViewingKey copied to clipboard!"
|
|
|
|
)
|
|
|
|
}
|
2020-09-11 00:16:46 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetPrivateKeyBinding =
|
|
|
|
FragmentGetPrivateKeyBinding.inflate(layoutInflater)
|
2020-06-09 20:28:21 -07:00
|
|
|
}
|