Skip to main content

Try it Live

Run Address examples in the interactive playground
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.

Factory API

For tree-shakeable, crypto-agnostic implementation, use the factory function:
Note: This method requires TWO crypto dependencies:
  • keccak256 - For hashing the public key
  • derivePublicKey - For deriving public key from private key
This allows complete control over which crypto implementations to use (native, WASM, or custom).

Standard API

The standard API automatically injects crypto dependencies:

C FFI

The C API does not currently expose fromPrivateKey directly. To derive addresses from private keys in C, use the crypto library’s secp256k1 functions to derive the public key first, then use primitives_address_from_public_key. Example workflow:
Note: For production use with C, consider:
  • Using the crypto library directly for secp256k1 operations
  • Computing keccak256 hash of the derived public key
  • Extracting the last 20 bytes for the address
Alternative: Use Zig or TypeScript APIs for key derivation, then pass addresses to C code as hex strings or byte arrays. Defined in: src/primitives.h

Algorithm

The address derivation follows this process:
  1. Derive public key - Multiply generator point G by private key scalar
  2. Extract coordinates - Get x and y coordinates from resulting point
  3. Hash public key - Compute keccak256(x || y)
  4. Extract address - Take last 20 bytes of hash
Pseudocode:

Private Key Requirements

Size: Exactly 32 bytes (256 bits) Valid range: 1 to n-1 where n is the secp256k1 curve order:
Invalid values:
  • All zeros (0x0000…0000)
  • Greater than or equal to curve order

Security Considerations

Never hardcode or log private keys. Store securely and transmit only over encrypted channels. Compromised private keys allow complete control of associated addresses.
Best practices:
  • Generate from cryptographically secure random source
  • Never reuse across different accounts/networks
  • Store encrypted at rest
  • Use hardware wallets for high-value keys
  • Implement key rotation policies

Complete Example

Use Cases

Wallet Generation

Generate new Ethereum accounts:

Key Import

Import existing private keys:

HD Wallets

Derive addresses from HD wallet private keys:

Performance

Cryptographic operations:
  • secp256k1 scalar multiplication (public key derivation)
  • keccak256 hash function
Bundle size impact: When using tree-shakeable imports, including this method adds:
  • secp256k1 implementation (~15-20 KB)
  • keccak256 implementation (~5-10 KB)
Execution time: ~0.5-2ms depending on implementation and hardware.

Error Handling

See Also