Overview
NEAR supports two elliptic curve cryptography algorithms:- Ed25519: Fast, secure, recommended for most use cases
- Secp256k1: Used for Bitcoin and Ethereum compatibility
curve:base58 strings.
Public Keys
PublicKey
Union type for all supported public key formats.Usage
Private Keys
PrivateKey
Union type for all supported private key formats.Signatures
Signature
Union type for all supported signature formats.Curve Types
Curve
CurveString
Generic format for curve-prefixed base58 strings.Native Types
Internal binary representations used for serialization.NativePublicKey
NativeSignature
Message Types
Hex
Data for signing can be either raw bytes or hexadecimal string.Key Pair Types
See Key Pair Helpers for key pair creation functions.KeyPair
Sign Functions
Format Specifications
Base58 Encoding
NEAR uses Bitcoin’s Base58 alphabet:0, O, I, and l are excluded to avoid confusion.
Key Lengths
Ed25519:- Private key: 64 bytes (32 bytes secret + 32 bytes public)
- Public key: 32 bytes
- Signature: 64 bytes
- Private key: 64 bytes (32 bytes secret + 32 bytes public)
- Public key: 64 bytes (uncompressed without 0x04 header)
- Signature: 64 bytes
Examples
Working with Keys
Signing Messages
Type Guards
Extracting Curve from Key
Algorithm Comparison
| Feature | Ed25519 | Secp256k1 |
|---|---|---|
| Speed | Faster | Slower |
| Signature Size | 64 bytes | 64 bytes |
| Public Key Size | 32 bytes | 64 bytes |
| Security | High | High |
| Use Case | Default | Ethereum compatibility |
Best Practices
- Use Ed25519 by default: It’s faster and produces smaller keys
- Never expose private keys: Store securely, never commit to version control
- Use safe variants: Prefer
safeSign()for better error handling - Validate key format: Always validate curve prefix when accepting keys from external sources
- Key rotation: Consider rotating keys periodically for enhanced security