Public Keys
Overview
The PublicKey class represents elliptic curve public keys in the BSV TypeScript SDK. Public keys are derived from private keys and are used to verify signatures and generate Bitcoin addresses.
Purpose
This component provides:
Public key derivation from private keys
Signature verification
Address generation (P2PKH)
DER and compressed/uncompressed encoding
Point operations on the secp256k1 curve
Basic Usage
Deriving from Private Key
import { PrivateKey, PublicKey } from '@bsv/sdk'
// Create private key
const privateKey = PrivateKey.fromRandom()
// Derive public key
const publicKey = privateKey.toPublicKey()
console.log('Public Key:', publicKey.toHex())Creating from Existing Data
Key Features
1. Address Generation
Generate Bitcoin addresses from public keys:
2. Signature Verification
Verify ECDSA signatures:
3. Compressed vs Uncompressed
Public keys can be represented in compressed or uncompressed format:
4. DER Encoding
Get DER-encoded representation:
API Reference
Constructor
x: X coordinate of the pointy: Y coordinate of the pointcompressed: Whether to use compressed format (default: true)
Static Methods
fromPrivateKey(privateKey: PrivateKey, compressed?: boolean): PublicKey- Derive from private keyfromHex(hex: string): PublicKey- Import from hex stringfromDER(der: number[]): PublicKey- Import from DER encodingfromString(str: string): PublicKey- Import from hex or DER stringfromPoint(point: Point): PublicKey- Create from secp256k1 point
Instance Methods
toHex(): string- Export to hex stringtoDER(): number[]- Export to DER encodingtoAddress(network?: 'mainnet' | 'testnet'): string- Generate addresstoHash(): number[]- Get Hash160 of public keyverify(hash: number[], signature: Signature): boolean- Verify signatureadd(other: PublicKey): PublicKey- Add another public key (point addition)mul(scalar: BigNumber): PublicKey- Scalar multiplication
Common Patterns
Verify Message Authentication
Multi-Signature Key Aggregation
Address Validation
Security Considerations
1. Public Key Reuse
Be cautious about address reuse:
2. Signature Verification
Always verify signatures properly:
3. Point Operations
Be careful with point arithmetic:
Performance Considerations
Compressed vs Uncompressed
Compressed keys save space and are faster to process:
Caching Public Keys
Cache derived public keys when used multiple times:
Related Components
Private Keys - Generate and manage private keys
Signatures - Create and verify signatures
P2PKH - Pay-to-Public-Key-Hash transactions
HD Wallets - Hierarchical key derivation
Code Examples
Complete Working Examples
See these code features for full implementations:
Example: Complete Address Manager
Best Practices
Use compressed keys (default) for efficiency
Verify all signatures before trusting them
Generate new addresses for each transaction (privacy)
Cache public keys when using repeatedly
Validate address format before using
Store public keys safely but don't treat as secrets
Use testnet for development and testing
Troubleshooting
Common Issues
Invalid public key format:
Point not on curve:
Verification fails:
Further Reading
Status
✅ Complete - Production ready
Last updated
