2020-06-10 00:08:19 -07:00
|
|
|
package cash.z.ecc.android.sdk.demoapp.demos.getaddress
|
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.FragmentGetAddressBinding
|
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-09-04 04:05:41 -07:00
|
|
|
import cash.z.ecc.android.sdk.type.UnifiedViewingKey
|
2021-10-21 13:05:02 -07:00
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.runBlocking
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-02-27 00:25:07 -08:00
|
|
|
/**
|
|
|
|
* Displays the address associated with the seed defined by the default config. To modify the seed
|
|
|
|
* that is used, update the `DemoConfig.seedWords` value.
|
|
|
|
*/
|
2019-10-21 03:17:07 -07:00
|
|
|
class GetAddressFragment : BaseDemoFragment<FragmentGetAddressBinding>() {
|
|
|
|
|
2021-09-04 04:05:41 -07:00
|
|
|
private lateinit var viewingKey: UnifiedViewingKey
|
2020-08-13 18:08:01 -07:00
|
|
|
private lateinit var seed: ByteArray
|
2020-02-25 23:43:27 -08:00
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
/**
|
|
|
|
* 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
|
2021-09-04 04:05:41 -07:00
|
|
|
val seedPhrase = sharedViewModel.seedPhrase.value
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
// 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()
|
2019-10-21 03:17:07 -07:00
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
// the derivation tool can be used for generating keys and addresses
|
2021-10-21 13:05:02 -07:00
|
|
|
viewingKey = runBlocking { DerivationTool.deriveUnifiedViewingKeys(seed, ZcashNetwork.fromResources(requireApplicationContext())).first() }
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-09-11 00:16:46 -07:00
|
|
|
private fun displayAddress() {
|
2021-09-04 04:05:41 -07:00
|
|
|
// a full fledged app would just get the address from the synchronizer
|
2021-10-21 13:05:02 -07:00
|
|
|
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
|
|
|
|
val zaddress = DerivationTool.deriveShieldedAddress(seed, ZcashNetwork.fromResources(requireApplicationContext()))
|
|
|
|
val taddress = DerivationTool.deriveTransparentAddress(seed, ZcashNetwork.fromResources(requireApplicationContext()))
|
|
|
|
binding.textInfo.text = "z-addr:\n$zaddress\n\n\nt-addr:\n$taddress"
|
|
|
|
}
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
// TODO: show an example with the synchronizer
|
|
|
|
|
|
|
|
//
|
|
|
|
// Android Lifecycle overrides
|
|
|
|
//
|
|
|
|
|
2021-09-04 04:05:41 -07:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2020-08-13 18:08:01 -07:00
|
|
|
setup()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
displayAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Base Fragment overrides
|
|
|
|
//
|
|
|
|
|
2020-02-25 23:43:27 -08:00
|
|
|
override fun onActionButtonClicked() {
|
2021-10-21 13:05:02 -07:00
|
|
|
viewLifecycleOwner.lifecycleScope.launch {
|
|
|
|
copyToClipboard(
|
|
|
|
DerivationTool.deriveShieldedAddress(
|
|
|
|
viewingKey.extfvk,
|
|
|
|
ZcashNetwork.fromResources(requireApplicationContext())
|
|
|
|
),
|
|
|
|
"Shielded address copied to clipboard!"
|
|
|
|
)
|
|
|
|
}
|
2019-10-21 03:17:07 -07:00
|
|
|
}
|
2020-02-25 23:43:27 -08:00
|
|
|
|
2020-08-13 18:08:01 -07:00
|
|
|
override fun inflateBinding(layoutInflater: LayoutInflater): FragmentGetAddressBinding =
|
|
|
|
FragmentGetAddressBinding.inflate(layoutInflater)
|
2020-06-09 20:28:21 -07:00
|
|
|
}
|