btcinfo  

Hic inserere motto

Nostr keygen in common lisp

April 09, 2025 — shinohai

It's high time some good, working common lisp tools existed for nostr. As far as I can tell only cl-nosso exists, though it is mostly non-working and far from complete. Here is my attempt at it's grossly inadequate keygen function. This requires the `ironclad` crypto library, available in quicklisp.

(defun make-key-pair ()
  "Creates a nostr keypair with 32-byte private and public keys in hex."
  (multiple-value-bind (private-key public-key)
      (ironclad:generate-key-pair :secp256k1)
    (flet ((bytes-to-hex (bytes)
             (ironclad:byte-array-to-hex-string bytes)))
      (let* ((priv-struct (ironclad:destructure-private-key private-key))
             (pub-struct (ironclad:destructure-public-key public-key))
             (priv-bytes (getf priv-struct :x))
             (pub-point (getf pub-struct :y))
             (pub-x (subseq pub-point 1 33))
             (priv-hex (bytes-to-hex priv-bytes))
             (pub-hex (bytes-to-hex pub-x)))
        (list
         :/private-key priv-hex 
         :/public-key pub-hex))))) 

(progn
  (let ((kp (make-key-pair)))
    (format t "Private key: ~A (~D bytes)~%" 
            (getf kp :/private-key) 
            (/ (length (getf kp :/private-key)) 2))
    (format t "Public key: ~A (~D bytes)~%" 
            (getf kp :/public-key) 
            (/ (length (getf kp :/public-key)) 2))
    (force-output)))

Come find me on nostr if you want to discuss or wish to complain about how this doesn't work for you. I'll probably just laugh at you for the latter, but come anyway!

Tags: Lisp, Linux, Nostr