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 point

  • y: Y coordinate of the point

  • compressed: Whether to use compressed format (default: true)

Static Methods

  • fromPrivateKey(privateKey: PrivateKey, compressed?: boolean): PublicKey - Derive from private key

  • fromHex(hex: string): PublicKey - Import from hex string

  • fromDER(der: number[]): PublicKey - Import from DER encoding

  • fromString(str: string): PublicKey - Import from hex or DER string

  • fromPoint(point: Point): PublicKey - Create from secp256k1 point

Instance Methods

  • toHex(): string - Export to hex string

  • toDER(): number[] - Export to DER encoding

  • toAddress(network?: 'mainnet' | 'testnet'): string - Generate address

  • toHash(): number[] - Get Hash160 of public key

  • verify(hash: number[], signature: Signature): boolean - Verify signature

  • add(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:

Code Examples

Complete Working Examples

See these code features for full implementations:

Example: Complete Address Manager

Best Practices

  1. Use compressed keys (default) for efficiency

  2. Verify all signatures before trusting them

  3. Generate new addresses for each transaction (privacy)

  4. Cache public keys when using repeatedly

  5. Validate address format before using

  6. Store public keys safely but don't treat as secrets

  7. 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