Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ZIP 32 arbitrary key derivation #1616

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- `DerivationTool.deriveArbitraryWalletKey`
- `DerivationTool.deriveArbitraryAccountKey`

## [2.2.5] - 2024-10-22

### Added
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.
daira marked this conversation as resolved.
Show resolved Hide resolved
* @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,18 @@ 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, seed, accountIndex = accountIndex, networkId = networkId)

companion object {
suspend fun new(): Derivation {
RustBackend.loadLibrary()
Expand Down Expand Up @@ -79,5 +91,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
Loading