Skip to content

Instantly share code, notes, and snippets.

@jprupp
Last active January 8, 2021 16:38
Show Gist options
  • Save jprupp/f0456453d7154a2e01f7a72c908910c3 to your computer and use it in GitHub Desktop.
Save jprupp/f0456453d7154a2e01f7a72c908910c3 to your computer and use it in GitHub Desktop.
Casascius Minikey Generator
#!/usr/bin/env stack
{- stack
--resolver lts-16.28
--nix
--nix-packages "secp256k1 pkg-config zlib"
script
--package haskoin-core
--package entropy
--package bytestring
--package text
--extra-dep haskoin-core-0.18.0
--extra-dep secp256k1-haskell-0.5.0
-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
import Haskoin
import System.Entropy
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import qualified Data.Text.IO as T
import Data.Maybe
alphabet :: [Char]
alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
genKey :: IO (B.ByteString, SecKeyI)
genKey =
g >>= \case
Nothing -> genKey
Just x -> return x
where
g = do
ent <- B.filter (< (l * 4)) <$> getEntropy 512
let chars = B8.cons 'S'
$ B8.pack
$ take 21
$ map (\x -> alphabet !! fromIntegral (x `mod` l))
$ B.unpack ent
if (B.length chars < 22)
then return Nothing
else case fromMiniKey chars of
Nothing -> return Nothing
Just k -> return (Just (chars, k))
l = fromIntegral $ length alphabet
main :: IO ()
main = do
(char, key) <- genKey
B8.putStrLn char
T.putStrLn (toWif btc key)
T.putStrLn (fromJust (addrToText btc (pubKeyAddr (derivePubKeyI key))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment