<commit_before>(ns bitcoin.miniwallet
  (:gen-class :main true)
  (:import java.math.BigInteger
           [org.bitcoinj.core Address ECKey NetworkParameters DumpedPrivateKey])
  (:require [clojure.string :as str]
            [pandect.algo.sha256 :refer :all]))

(defn private-to-address [k]
  (.toString (Address. (NetworkParameters/prodNet) (.getPubKeyHash k))))

(defn private-to-pubkey-string [k]
  (format (if (.isCompressed k) "%066x" "%0130x") (java.math.BigInteger. (.getPubKey k))))

(defn gen-uncompressed-key []
  ;; hack to get the key uncompressed, because bitcoinj makes it compressed by default.
  (.getPrivateKeyEncoded (ECKey. (.getPrivKeyBytes (ECKey.)) nil) (NetworkParameters/prodNet)))

(defn minikey-candidate []
  (let [key-str (.toString (gen-uncompressed-key))]
    (str "S" (subs key-str 2 31))))

(defn minikey-validate [key-str]
  (.startsWith (sha256 (str key-str "?")) "00"))

(defn gen-minikey []
  (loop [testkey (minikey-candidate)]
    (if (minikey-validate testkey)
         testkey
      (recur (minikey-candidate)))))

(defn minikey-to-private [minikey]
  (ECKey/fromPrivate (BigInteger. (sha256 minikey) 16) false))

(defn gen-miniwallet []
  (let [key-str (gen-minikey)
        key (minikey-to-private key-str)]
    (println key-str (private-to-pubkey-string key) (private-to-address key))))

(defn gen-bip38 [passphrase]
  (let [key (ECKey.)
        plain-key (.toString (.getPrivateKeyEncoded key (NetworkParameters/prodNet)))
        key-str (com.fruitcat.bitcoin.BIP38/encryptNoEC passphrase plain-key true)]
    (println key-str (private-to-pubkey-string key) (private-to-address key) passphrase)))<commit_msg>Get rid of ugly hex print hack and use .getPublicKeyAsHex of ECKey<commit_after>(ns bitcoin.miniwallet
  (:gen-class :main true)
  (:import java.math.BigInteger
           [org.bitcoinj.core Address ECKey NetworkParameters DumpedPrivateKey])
  (:require [clojure.string :as str]
            [pandect.algo.sha256 :refer :all]))

(defn private-to-address [k]
  (.toString (Address. (NetworkParameters/prodNet) (.getPubKeyHash k))))

(defn gen-uncompressed-key []
  ;; hack to get the key uncompressed, because bitcoinj makes it compressed by default.
  (.getPrivateKeyEncoded (ECKey. (.getPrivKeyBytes (ECKey.)) nil) (NetworkParameters/prodNet)))

(defn minikey-candidate []
  (let [key-str (.toString (gen-uncompressed-key))]
    (str "S" (subs key-str 2 31))))

(defn minikey-validate [key-str]
  (.startsWith (sha256 (str key-str "?")) "00"))

(defn gen-minikey []
  (loop [testkey (minikey-candidate)]
    (if (minikey-validate testkey)
         testkey
      (recur (minikey-candidate)))))

(defn minikey-to-private [minikey]
  (ECKey/fromPrivate (BigInteger. (sha256 minikey) 16) false))

(defn gen-miniwallet []
  (let [key-str (gen-minikey)
        key (minikey-to-private key-str)]
    (println key-str (.getPublicKeyAsHex key) (private-to-address key))))

(defn gen-bip38 [passphrase]
  (let [key (ECKey.)
        plain-key (.toString (.getPrivateKeyEncoded key (NetworkParameters/prodNet)))
        key-str (com.fruitcat.bitcoin.BIP38/encryptNoEC passphrase plain-key true)]
    (println key-str (.getPublicKeyAsHex key) (private-to-address key) passphrase)))
{'fraction_non_alphanumeric': 0.14866542520173806, 'fraction_numerical': 0.014587212911235258, 'mean_word_length': 3.9281345565749235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4409997', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1108, 'n_words': 272}