mirror of https://github.com/zcash/zip32.git
commit
3e762d416e
|
@ -0,0 +1,10 @@
|
|||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: github-actions
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: daily
|
||||
timezone: Etc/UTC
|
||||
open-pull-requests-limit: 10
|
||||
labels:
|
||||
- "A-CI"
|
|
@ -0,0 +1,79 @@
|
|||
name: CI checks
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test on ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Run tests
|
||||
run: cargo test --all-features --verbose
|
||||
- name: Verify working directory is clean
|
||||
run: git diff --exit-code
|
||||
|
||||
build-latest:
|
||||
name: Latest build on ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
id: toolchain
|
||||
- run: rustup override set ${{steps.toolchain.outputs.name}}
|
||||
- name: Remove lockfile to build with latest dependencies
|
||||
run: rm Cargo.lock
|
||||
- name: Build crate
|
||||
run: cargo build --all-targets --all-features --verbose
|
||||
- name: Verify working directory is clean (excluding lockfile)
|
||||
run: git diff --exit-code ':!Cargo.lock'
|
||||
|
||||
build-nodefault:
|
||||
name: Build target ${{ matrix.target }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
target:
|
||||
- wasm32-wasi
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Add target
|
||||
run: rustup target add ${{ matrix.target }}
|
||||
- name: Build crate
|
||||
run: cargo build --no-default-features --verbose --target ${{ matrix.target }}
|
||||
|
||||
clippy:
|
||||
name: Clippy (MSRV)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Run Clippy
|
||||
uses: auguwu/clippy-action@1.3.0
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
working-directory: ${{ inputs.target }}
|
||||
deny: warnings
|
||||
|
||||
doc-links:
|
||||
name: Intra-doc links
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: cargo fetch
|
||||
# Requires #![deny(rustdoc::broken_intra_doc_links)] in crate.
|
||||
- name: Check intra-doc links
|
||||
run: cargo doc --all-features --document-private-items
|
||||
|
||||
fmt:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Check formatting
|
||||
run: cargo fmt -- --check
|
|
@ -1,3 +1,2 @@
|
|||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
|
||||
|
||||
[[package]]
|
||||
name = "assert_matches"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9"
|
||||
|
||||
[[package]]
|
||||
name = "blake2b_simd"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c2f0dc9a68c6317d884f97cc36cf5a3d20ba14ce404227df55e1af708ab04bc"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"arrayvec",
|
||||
"constant_time_eq",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21a53c0a4d288377e7415b53dcfc3c04da5cdc2cc95c8d5ac178b58f0b861ad6"
|
||||
|
||||
[[package]]
|
||||
name = "memuse"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2145869435ace5ea6ea3d35f59be559317ec9a0d04e1812d5f185a87b6d36f1a"
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
|
||||
|
||||
[[package]]
|
||||
name = "zip32"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"assert_matches",
|
||||
"blake2b_simd",
|
||||
"memuse",
|
||||
"subtle",
|
||||
]
|
12
Cargo.toml
12
Cargo.toml
|
@ -5,13 +5,13 @@ authors = [
|
|||
"Jack Grigg <jack@electriccoin.co>",
|
||||
"Kris Nuttycombe <kris@electriccoin.co>",
|
||||
]
|
||||
description = "Library for implementing shielded hierarchical deterministic wallets"
|
||||
description = "Common types for implementing shielded hierarchical deterministic wallets"
|
||||
documentation = "https://docs.rs/zip32/"
|
||||
homepage = "https://github.com/zcash-hackworks/zip32"
|
||||
repository = "https://github.com/zcash-hackworks/zip32"
|
||||
homepage = "https://github.com/zcash/zip32"
|
||||
repository = "https://github.com/zcash/zip32"
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.65"
|
||||
rust-version = "1.60"
|
||||
|
||||
[dependencies]
|
||||
blake2b_simd = "1"
|
||||
|
@ -20,3 +20,7 @@ subtle = "2.2.3"
|
|||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.5"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = []
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
[toolchain]
|
||||
channel = "1.60.0"
|
||||
components = ["clippy", "rustfmt"]
|
|
@ -1,18 +1,21 @@
|
|||
//! Seed Fingerprints according to ZIP 32
|
||||
//!
|
||||
//! Implements section `Seed Fingerprints` of Shielded Hierarchical Deterministic Wallets (ZIP 32)
|
||||
//! Implements section [Seed Fingerprints] of Shielded Hierarchical Deterministic Wallets (ZIP 32).
|
||||
//!
|
||||
//! [Section Seed Fingerprints]: https://zips.z.cash/zip-0032#seed-fingerprints
|
||||
//! [Seed Fingerprints]: https://zips.z.cash/zip-0032#seed-fingerprints
|
||||
use blake2b_simd::Params as Blake2bParams;
|
||||
|
||||
pub const ZIP32_SEED_FP_PERSONALIZATION: &[u8; 16] = b"Zcash_HD_Seed_FP";
|
||||
const ZIP32_SEED_FP_PERSONALIZATION: &[u8; 16] = b"Zcash_HD_Seed_FP";
|
||||
|
||||
/// The fingerprint for a wallet's seed bytes, as defined in [ZIP 32].
|
||||
///
|
||||
/// [ZIP 32]: https://zips.z.cash/zip-0032#seed-fingerprints
|
||||
pub struct SeedFingerprint([u8; 32]);
|
||||
|
||||
impl SeedFingerprint {
|
||||
/// Return the seed fingerprint of the wallet as defined in
|
||||
/// <https://zips.z.cash/zip-0032#seed-fingerprints> or None
|
||||
/// if the length of `seed_bytes` is less than 32 or
|
||||
/// greater than 252.
|
||||
/// Derives the fingerprint of the given seed bytes.
|
||||
///
|
||||
/// Returns `None` if the length of `seed_bytes` is less than 32 or greater than 252.
|
||||
pub fn from_seed(seed_bytes: &[u8]) -> Option<SeedFingerprint> {
|
||||
let seed_len = seed_bytes.len();
|
||||
|
||||
|
@ -44,17 +47,17 @@ impl SeedFingerprint {
|
|||
#[test]
|
||||
fn test_seed_fingerprint() {
|
||||
struct TestVector {
|
||||
root_seed: Vec<u8>,
|
||||
fingerprint: Vec<u8>,
|
||||
root_seed: [u8; 32],
|
||||
fingerprint: [u8; 32],
|
||||
}
|
||||
|
||||
let test_vectors = vec![TestVector {
|
||||
root_seed: vec![
|
||||
let test_vectors = [TestVector {
|
||||
root_seed: [
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
|
||||
0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
|
||||
0x1c, 0x1d, 0x1e, 0x1f,
|
||||
],
|
||||
fingerprint: vec![
|
||||
fingerprint: [
|
||||
0xde, 0xff, 0x60, 0x4c, 0x24, 0x67, 0x10, 0xf7, 0x17, 0x6d, 0xea, 0xd0, 0x2a, 0xa7,
|
||||
0x46, 0xf2, 0xfd, 0x8d, 0x53, 0x89, 0xf7, 0x7, 0x25, 0x56, 0xdc, 0xb5, 0x55, 0xfd,
|
||||
0xbe, 0x5e, 0x3a, 0xe3,
|
||||
|
@ -68,7 +71,7 @@ fn test_seed_fingerprint() {
|
|||
}
|
||||
#[test]
|
||||
fn test_seed_fingerprint_is_none() {
|
||||
let odd_seed = vec![
|
||||
let odd_seed = [
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
||||
0x0f,
|
||||
];
|
||||
|
|
40
src/lib.rs
40
src/lib.rs
|
@ -1,7 +1,15 @@
|
|||
//! Implementation of [ZIP 32] for hierarchical deterministic key management.
|
||||
//! Common types for implementing [ZIP 32] for hierarchical deterministic key management.
|
||||
//!
|
||||
//! [ZIP 32]: https://zips.z.cash/zip-0032
|
||||
|
||||
#![no_std]
|
||||
#![deny(missing_docs)]
|
||||
#![deny(unsafe_code)]
|
||||
#![deny(rustdoc::broken_intra_doc_links)]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
extern crate std;
|
||||
|
||||
use memuse::{self, DynamicUsage};
|
||||
use subtle::{Choice, ConditionallySelectable};
|
||||
|
||||
|
@ -92,6 +100,7 @@ impl ChainCode {
|
|||
}
|
||||
}
|
||||
|
||||
/// The index for a particular diversifier.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct DiversifierIndex(pub [u8; 11]);
|
||||
|
||||
|
@ -116,7 +125,7 @@ impl From<u64> for DiversifierIndex {
|
|||
}
|
||||
|
||||
impl TryFrom<DiversifierIndex> for u32 {
|
||||
type Error = std::num::TryFromIntError;
|
||||
type Error = core::num::TryFromIntError;
|
||||
|
||||
fn try_from(di: DiversifierIndex) -> Result<u32, Self::Error> {
|
||||
let mut u128_bytes = [0u8; 16];
|
||||
|
@ -126,11 +135,13 @@ impl TryFrom<DiversifierIndex> for u32 {
|
|||
}
|
||||
|
||||
impl DiversifierIndex {
|
||||
/// Constructs the zero index.
|
||||
pub fn new() -> Self {
|
||||
DiversifierIndex([0; 11])
|
||||
}
|
||||
|
||||
pub fn increment(&mut self) -> Result<(), ()> {
|
||||
/// Increments this index, failing on overflow.
|
||||
pub fn increment(&mut self) -> Result<(), DiversifierIndexOverflowError> {
|
||||
for k in 0..11 {
|
||||
self.0[k] = self.0[k].wrapping_add(1);
|
||||
if self.0[k] != 0 {
|
||||
|
@ -139,20 +150,31 @@ impl DiversifierIndex {
|
|||
}
|
||||
}
|
||||
// Overflow
|
||||
Err(())
|
||||
Err(DiversifierIndexOverflowError)
|
||||
}
|
||||
}
|
||||
|
||||
/// The error type returned when a [`DiversifierIndex`] increment fails.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct DiversifierIndexOverflowError;
|
||||
|
||||
impl core::fmt::Display for DiversifierIndexOverflowError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "DiversifierIndex increment overflowed")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::error::Error for DiversifierIndexOverflowError {}
|
||||
|
||||
/// The scope of a viewing key or address.
|
||||
///
|
||||
/// A "scope" narrows the visibility or usage to a level below "full".
|
||||
///
|
||||
/// Consistent usage of `Scope` enables the user to provide consistent views over a wallet
|
||||
/// to other people. For example, a user can give an external [SaplingIvk] to a merchant
|
||||
/// terminal, enabling it to only detect "real" transactions from customers and not
|
||||
/// internal transactions from the wallet.
|
||||
///
|
||||
/// [SaplingIvk]: crate::sapling::SaplingIvk
|
||||
/// to other people. For example, a user can give an external incoming viewing key to a
|
||||
/// merchant terminal, enabling it to only detect "real" transactions from customers and
|
||||
/// not internal transactions from the wallet.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Scope {
|
||||
/// A scope used for wallet-external operations, namely deriving addresses to give to
|
||||
|
|
Loading…
Reference in New Issue