Module neo_crypto

Source
Expand description

§Neo Crypto (v0.1.8)

Cryptographic utilities for the Neo N3 blockchain.

§Overview

The neo_crypto module provides cryptographic primitives and utilities for working with the Neo N3 blockchain. It includes:

  • Key pair generation and management
  • Cryptographic signing and verification
  • Hashing functions (SHA256, RIPEMD160, etc.)
  • Base58 encoding and decoding
  • WIF (Wallet Import Format) utilities
  • Secure random number generation
  • Encryption and decryption utilities

This module forms the cryptographic foundation for wallet management, transaction signing, and secure communication within the Neo N3 ecosystem.

§Examples

§Creating a key pair

use neo3::neo_crypto::KeyPair;

// Generate a new random key pair
let key_pair = KeyPair::new_random();
println!("Public key: {:?}", key_pair.public_key());
println!("Private key: {:?}", key_pair.private_key());

// Create a key pair from a private key (32 bytes)
let private_key_bytes = [1u8; 32]; // Replace with actual private key bytes
let key_pair = KeyPair::from_private_key(&private_key_bytes).unwrap();

§Signing and verifying data

use neo3::neo_crypto::KeyPair;

// Generate a key pair
let key_pair = KeyPair::new_random();

// Data to sign
let data = b"Hello, Neo!";

// Sign the data
let signature = key_pair.sign(data).unwrap();

// Verify the signature
let is_valid = key_pair.verify_signature(data, &signature).unwrap();
assert!(is_valid);

§Working with WIF format

use neo3::neo_crypto::KeyPair;

// Import a private key from WIF format
let wif = "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn";
let key_pair = KeyPair::from_wif(wif).unwrap();

// Export a private key to WIF format
let exported_wif = key_pair.export_as_wif();
assert_eq!(wif, exported_wif);

Re-exports§

pub use hash::HashableForVec;
pub use utils::*;

Modules§

hash
utils

Structs§

KeyPair
Represents an Elliptic Curve Key Pair containing both a private and a public key.
Secp256r1PrivateKey
Secp256r1PublicKey
Secp256r1Signature
Secp256r1SignedMsg

Enums§

CryptoError
Nep2Error
SignError

Traits§

PrivateKeyExtension
PublicKeyExtension

Functions§

base58check_decode
Decodes a base58check string into a byte vector.
base58check_encode
Encodes a byte slice into a base58check string.
calculate_checksum
Calculates the checksum of a byte slice.
private_key_from_wif
Converts a WIF (Wallet Import Format) string into a Secp256r1PrivateKey.
wif_from_private_key
Converts a Secp256r1PrivateKey into a WIF (Wallet Import Format) string.