Skip to content

Instantly share code, notes, and snippets.

@rgruesbeck
Last active November 15, 2018 02:51
Show Gist options
  • Save rgruesbeck/d75fc0f054f42489610e6824acef5358 to your computer and use it in GitHub Desktop.
Save rgruesbeck/d75fc0f054f42489610e6824acef5358 to your computer and use it in GitHub Desktop.
create an offline address by coin toss: requires https://github.com/libbitcoin/libbitcoin-explorer
#!/usr/bin/env bash
length=256
bits=()
prvk=""
pubk=""
mnemonic=""
# collect coin flips
function getBits() {
msg="Start flipping!"
echo $msg
msg="Enter 1 for heads, 0 for tails: "
while [ "${#bits[*]}" -lt "$length" ]; do
read -p "$msg" bit
if [[ $bit =~ ^-?[0-1]$ ]]; then
bits+=($bit)
msg="${#bits[*]} of $length bits: "
else
msg="Enter 1 for heads, 0 for tails: "
fi
done
}
# convert to string
function getBitString() {
local str=$(echo "${bits[@]}")
echo ${str// /}
}
# convert to hex
function getHexString() {
local bitstr=$(getBitString)
echo $(printf '%x\n' "$((2#$bitstr))")
}
function mnemonic() {
mnemonic=$(bx mnemonic-new --SEED $1)
}
function privateKey() {
prvk=$(bx hd-new $1)
}
function publicKey() {
pubk=$(bx hd-public -d $1)
}
function newAddress() {
local seed=$(getHexString)
mnemonic $seed
privateKey $(bx mnemonic-to-seed $mnemonic)
publicKey $prvk
echo ""
echo "You have created a new Bitcoin Address!"
echo "Public Key: $pubk"
echo "Private Key: $prvk"
echo ""
echo "$mnemonic"
}
function main() {
getBits
newAddress
}
main
@rgruesbeck
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment