Overview
NEAR supports two elliptic curve cryptography algorithms:- Ed25519: Default and recommended for most use cases
- Secp256k1: Used for Bitcoin and Ethereum compatibility
Result types) and “throwable” variants (throw errors) for all functions.
Functions
keyPair()
Creates a KeyPair from an existing private key string.privateKey(string): Private key in curve:base58 format (e.g.,"ed25519:..."or"secp256k1:...")
KeyPair
Safe variant: safeKeyPair()
randomEd25519KeyPair()
Generates a new random Ed25519 key pair.KeyPair
Safe variant: safeRandomEd25519KeyPair()
randomSecp256k1KeyPair()
Generates a new random Secp256k1 key pair.KeyPair
Safe variant: safeRandomSecp256k1KeyPair()
KeyPair Object
TheKeyPair object provides the following properties and methods:
Properties
publicKey(PublicKey): Public key in curve:base58 formatprivateKey(PrivateKey): Private key in curve:base58 format
Methods
sign()
Signs a message and returns the signature.message(Hex): Message to sign (Uint8Array or hex string)
SignOutput
signature(Signature): Signature in curve:base58 formatcurve(Curve): Curve type ("ed25519"or"secp256k1")u8Signature(Uint8Array): Raw signature bytes
safeSign()
Type Definitions
KeyPair
PublicKey & PrivateKey
SignOutput
Examples
Creating Key Pairs
Signing Messages
Safe Operations
Using with Memory Signer
Key Storage
Security Considerations
- Never expose private keys: Private keys should be stored securely and never committed to version control
- Use Ed25519 by default: Ed25519 is faster and produces smaller signatures
- Key rotation: Consider rotating keys periodically for enhanced security
- Secure storage: Use hardware security modules (HSMs) or secure enclaves for production keys
Error Handling
Common errors:- Invalid private key format: Key must be in
curve:base58format - Invalid curve: Only
ed25519andsecp256k1are supported - Malformed base58: The base58 portion must be valid