2020-09-11 00:33:25 -07:00
|
|
|
package cash.z.ecc.android.sdk.tool
|
|
|
|
|
2023-05-18 04:36:15 -07:00
|
|
|
import cash.z.ecc.android.sdk.internal.Derivation
|
|
|
|
import cash.z.ecc.android.sdk.internal.SuspendingLazy
|
|
|
|
import cash.z.ecc.android.sdk.internal.TypesafeDerivationToolImpl
|
|
|
|
import cash.z.ecc.android.sdk.internal.jni.RustDerivationTool
|
2022-10-06 10:44:34 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.Account
|
2023-04-10 07:35:40 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.UnifiedFullViewingKey
|
2022-09-29 10:04:00 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.UnifiedSpendingKey
|
2022-08-02 06:29:09 -07:00
|
|
|
import cash.z.ecc.android.sdk.model.ZcashNetwork
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2023-05-18 04:36:15 -07:00
|
|
|
interface DerivationTool {
|
2022-11-24 02:15:03 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a seed and a number of accounts, return the associated Unified Full Viewing Keys.
|
|
|
|
*
|
|
|
|
* @param seed the seed from which to derive viewing keys.
|
|
|
|
* @param numberOfAccounts the number of accounts to use. Multiple accounts are not fully
|
|
|
|
* supported so the default value of 1 is recommended.
|
|
|
|
*
|
|
|
|
* @return the UFVKs derived from the seed, encoded as Strings.
|
|
|
|
*/
|
2023-05-18 04:36:15 -07:00
|
|
|
suspend fun deriveUnifiedFullViewingKeys(
|
2022-11-24 02:15:03 -08:00
|
|
|
seed: ByteArray,
|
|
|
|
network: ZcashNetwork,
|
|
|
|
numberOfAccounts: Int
|
2023-05-18 04:36:15 -07:00
|
|
|
): List<UnifiedFullViewingKey>
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2022-11-24 02:15:03 -08:00
|
|
|
/**
|
|
|
|
* Given a unified spending key, return the associated unified full viewing key.
|
|
|
|
*
|
|
|
|
* @param usk the key from which to derive the viewing key.
|
|
|
|
*
|
|
|
|
* @return a unified full viewing key.
|
|
|
|
*/
|
2023-05-18 04:36:15 -07:00
|
|
|
suspend fun deriveUnifiedFullViewingKey(
|
2022-11-24 02:15:03 -08:00
|
|
|
usk: UnifiedSpendingKey,
|
|
|
|
network: ZcashNetwork
|
2023-05-18 04:36:15 -07:00
|
|
|
): UnifiedFullViewingKey
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2022-11-24 02:15:03 -08:00
|
|
|
/**
|
|
|
|
* Derives and returns a unified spending key from the given seed for the given account ID.
|
|
|
|
*
|
|
|
|
* Returns the newly created [ZIP 316] account identifier, along with the binary encoding
|
|
|
|
* of the [`UnifiedSpendingKey`] for the newly created account. The caller should store
|
|
|
|
* the returned spending key in a secure fashion.
|
|
|
|
*
|
|
|
|
* @param seed the seed from which to derive spending keys.
|
|
|
|
* @param account the account to derive.
|
|
|
|
*
|
|
|
|
* @return the unified spending key for the account.
|
|
|
|
*/
|
2023-05-18 04:36:15 -07:00
|
|
|
suspend fun deriveUnifiedSpendingKey(
|
2022-11-24 02:15:03 -08:00
|
|
|
seed: ByteArray,
|
|
|
|
network: ZcashNetwork,
|
|
|
|
account: Account
|
2023-05-18 04:36:15 -07:00
|
|
|
): UnifiedSpendingKey
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2022-11-24 02:15:03 -08:00
|
|
|
/**
|
|
|
|
* Given a seed and account index, return the associated Unified Address.
|
|
|
|
*
|
|
|
|
* @param seed the seed from which to derive the address.
|
2023-05-18 04:36:15 -07:00
|
|
|
* @param account the index of the account to use for deriving the address.
|
2022-11-24 02:15:03 -08:00
|
|
|
*
|
|
|
|
* @return the address that corresponds to the seed and account index.
|
|
|
|
*/
|
2023-05-18 04:36:15 -07:00
|
|
|
suspend fun deriveUnifiedAddress(seed: ByteArray, network: ZcashNetwork, account: Account): String
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2022-11-24 02:15:03 -08:00
|
|
|
/**
|
|
|
|
* Given a Unified Full Viewing Key string, return the associated Unified Address.
|
|
|
|
*
|
|
|
|
* @param viewingKey the viewing key to use for deriving the address. The viewing key is tied to
|
|
|
|
* a specific account so no account index is required.
|
|
|
|
*
|
|
|
|
* @return the address that corresponds to the viewing key.
|
|
|
|
*/
|
2023-05-18 04:36:15 -07:00
|
|
|
suspend fun deriveUnifiedAddress(
|
2022-11-24 02:15:03 -08:00
|
|
|
viewingKey: String,
|
|
|
|
network: ZcashNetwork
|
2023-05-18 04:36:15 -07:00
|
|
|
): String
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2023-05-18 04:36:15 -07:00
|
|
|
companion object {
|
|
|
|
const val DEFAULT_NUMBER_OF_ACCOUNTS = Derivation.DEFAULT_NUMBER_OF_ACCOUNTS
|
2020-09-11 00:33:25 -07:00
|
|
|
|
2023-05-18 04:36:15 -07:00
|
|
|
private val instance = SuspendingLazy<Unit, DerivationTool> {
|
|
|
|
TypesafeDerivationToolImpl(RustDerivationTool.new())
|
|
|
|
}
|
|
|
|
suspend fun getInstance() = instance.getInstance(Unit)
|
2020-09-11 00:33:25 -07:00
|
|
|
}
|
2021-03-10 10:10:03 -08:00
|
|
|
}
|