signer.mx raw

   1  // Package signer defines server for management of signatures, used to
   2  // abstract the signature algorithm from the usage.
   3  package signer
   4  
   5  // I is an interface for a key pair for signing, created to abstract between a CGO fast BIP-340
   6  // signature library and the slower btcec library.
   7  type I interface {
   8  	// Generate creates a fresh new key pair from system entropy, and ensures it is even (so
   9  	// ECDH works).
  10  	Generate() (err error)
  11  	// InitSec initialises the secret (signing) key from the raw bytes, and also
  12  	// derives the public key because it can.
  13  	InitSec(sec []byte) (err error)
  14  	// InitPub initializes the public (verification) key from raw bytes, this is
  15  	// expected to be an x-only 32 byte pubkey.
  16  	InitPub(pub []byte) (err error)
  17  	// Sec returns the secret key bytes.
  18  	Sec() []byte
  19  	// Pub returns the public key bytes (x-only schnorr pubkey).
  20  	Pub() []byte
  21  	// Sign creates a signature using the stored secret key.
  22  	Sign(msg []byte) (sig []byte, err error)
  23  	// Verify checks a message hash and signature match the stored public key.
  24  	Verify(msg, sig []byte) (valid bool, err error)
  25  	// Zero wipes the secret key to prevent memory leaks.
  26  	Zero()
  27  	// ECDH returns a shared secret derived using Elliptic Curve Diffie-Hellman on
  28  	// the I secret and provided pubkey. Returns the 32-byte x-coordinate of the shared point.
  29  	ECDH(pub []byte) (secret []byte, err error)
  30  	// ECDHRaw returns the raw shared secret point (x-coordinate only, 32 bytes) without hashing.
  31  	// This is needed for protocols like NIP-44 that do their own key derivation.
  32  	ECDHRaw(pub []byte) (sharedX []byte, err error)
  33  }
  34  
  35  // Gen is an interface for nostr BIP-340 key generation.
  36  type Gen interface {
  37  	// Generate gathers entropy and derives pubkey bytes for matching, this returns the 33 byte
  38  	// compressed form for checking the oddness of the Y coordinate.
  39  	Generate() (pubBytes []byte, err error)
  40  	// Negate flips the public key Y coordinate between odd and even.
  41  	Negate()
  42  	// KeyPairBytes returns the raw bytes of the secret and public key, this returns the 32 byte
  43  	// X-only pubkey.
  44  	KeyPairBytes() (secBytes, cmprPubBytes []byte)
  45  }
  46