Skip to content

Commit

Permalink
Expose ZIP 32 arbitrary key derivation (#1616)
Browse files Browse the repository at this point in the history
* rust: Reorder FFI methods to group by Kotlin class

* Add ZIP 32 Arbitrary Key derivation to `DerivationTool`

* Fix Ktlint warning

* Unit tests added for DerivationToolImplTesttestDerivedArbitraryAccountKey & DerivationToolImplTest.deriveArbitraryWalletKey

---------

Co-authored-by: Honza <[email protected]>
Co-authored-by: Milan Cerovsky <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent bc2d168 commit 5e8e2c8
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 172 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- `DerivationTool.deriveArbitraryWalletKey`
- `DerivationTool.deriveArbitraryAccountKey`
- `Synchronizer.getTransactionOutputs` API has been added. It enables to fetch all transaction outputs from database.

## [2.2.5] - 2024-10-22
Expand Down
10 changes: 6 additions & 4 deletions backend-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ zcash_client_backend = { version = "0.14", features = ["orchard", "tor", "transp
zcash_client_sqlite = { version = "0.12.2", features = ["orchard", "transparent-inputs", "unstable"] }
zcash_primitives = "0.19"
zcash_proofs = "0.19"
zip32 = "0.1.2"

# Infrastructure
prost = "0.13"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ interface Derivation {
numberOfAccounts: Int
): Array<String>

/**
* Derives a ZIP 32 Arbitrary Key from the given seed at the "wallet level", i.e.
* directly from the seed with no ZIP 32 path applied.
*
* The resulting key will be the same across all networks (Zcash mainnet, Zcash
* testnet, OtherCoin mainnet, and so on). You can think of it as a context-specific
* seed fingerprint that can be used as (static) key material.
*
* @param contextString a globally-unique non-empty sequence of at most 252 bytes that
* identifies the desired context.
* @return an array of 32 bytes.
*/
fun deriveArbitraryWalletKey(
contextString: ByteArray,
seed: ByteArray
): ByteArray

/**
* Derives a ZIP 32 Arbitrary Key from the given seed at the account level.
*
* @param contextString a globally-unique non-empty sequence of at most 252 bytes that
* identifies the desired context.
* @return an array of 32 bytes.
*/
fun deriveArbitraryAccountKey(
contextString: ByteArray,
seed: ByteArray,
networkId: Int,
accountIndex: Int
): ByteArray

companion object {
const val DEFAULT_NUMBER_OF_ACCOUNTS = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ class RustDerivationTool private constructor() : Derivation {
networkId: Int
): String = deriveUnifiedAddressFromViewingKey(viewingKey, networkId = networkId)

override fun deriveArbitraryWalletKey(
contextString: ByteArray,
seed: ByteArray
): ByteArray = deriveArbitraryWalletKeyFromSeed(contextString, seed)

override fun deriveArbitraryAccountKey(
contextString: ByteArray,
seed: ByteArray,
networkId: Int,
accountIndex: Int
): ByteArray =
deriveArbitraryAccountKeyFromSeed(
contextString = contextString,
seed = seed,
accountIndex = accountIndex,
networkId = networkId
)

companion object {
suspend fun new(): Derivation {
RustBackend.loadLibrary()
Expand Down Expand Up @@ -79,5 +97,19 @@ class RustDerivationTool private constructor() : Derivation {
key: String,
networkId: Int
): String

@JvmStatic
private external fun deriveArbitraryWalletKeyFromSeed(
contextString: ByteArray,
seed: ByteArray
): ByteArray

@JvmStatic
private external fun deriveArbitraryAccountKeyFromSeed(
contextString: ByteArray,
seed: ByteArray,
accountIndex: Int,
networkId: Int
): ByteArray
}
}
Loading

0 comments on commit 5e8e2c8

Please sign in to comment.