Create ECDSA Keypair from Seed Phrase



Published: 2019-07-12 08:34:03 +0000
Categories: BASH,

Language

BASH

Description

I was recently asked to provide the public component of a keypair for access to something. For reasons known only to myself, I decided it would amuse me to have the key derived from something else they'd said, so I knocked together a script to take a seed sentence and derive an ECDSA pair from that.

The script outputs the keypair in a few forms - the private key as a SHA256 hash, long SHA256 public key hash, RIPEMD public key hash (as used by many crypto currencies), and PEM versions of both (for use with OpenSSL et al).

Snippet

#!/bin/bash 
#
# build_keypair.sh
#
# Create a ECDSA keypair for use with crypto currencies
# The key will be derived from whatever seed phrase is entered by the user
#
# Copyright (c) 2019 B Tasker

read -p "Enter a seed sentence: " seedphrase

# Derive a private key
privkey=$(echo "$seedphrase" | openssl sha256 | cut -d\  -f2)

# Get a proper copy of the private key
privkeyfull=$(openssl ec -inform DER -in <(cat <(echo -n "302e0201010420") <(echo -n "$privkey") <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null)

# Now start creating the pub key 
longpub=$(openssl ec -inform DER -text -noout -in <(cat <(echo -n "302e0201010420") <(echo -n "$privkey") <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null  | tail -6 | head -5 | sed 's/[ :]//g' | tr -d '\n' && echo)

# Create the compressed version
compub=$(echo -n "$longpub" | cut -c1-66 | sed 's/^04/02/')

# Now RipeMD it:
hash256=$(echo "$compub" | xxd -r -p | openssl sha256 | cut -d\  -f2)
ripemd=$(echo "$hash256" | xxd -r -p | openssl ripemd160 | cut -d\  -f2)

# Now RipeMD the uncompressed :
hash256=$(echo "$longpub" | xxd -r -p | openssl sha256 | cut -d\  -f2)
ripemdunc=$(echo "$hash256" | xxd -r -p | openssl ripemd160 | cut -d\  -f2)

# And a version we can pass into OpenSSL if we need to
pubkeyfull=$(openssl ec -inform DER -in <(cat <(echo -n "302e0201010420") <(echo -n "e359ae12b3c49fa0d59d0947a97acc9d8595017205909a883501ae09d4ea1888") <(echo -n "a00706052b8104000a") | xxd -r -p) -pubout 2>/dev/null)

cat << EOM
Seed Phrase: 
$seedphrase

Keys:

Private: $privkey
Long public: $longpub
Compressed Public: $compub
RipeMD (Compressed) Public: $ripemd
RipeMD (Uncompressed) Public: $ripemdunc

PEMs:

$privkeyfull
$pubkeyfull

Keep these safe
EOM

Usage Example

$ bash build_keypair.sh
Enter a seed sentence: This is an example seed sentence which you should never actually use in real life. Make your own up, do not copy and paste this
Seed Phrase: 
This is an example seed sentence which you should never actually use in real life. Make your own up, do not copy and paste this

Keys:

Private: 4de575cf2a8a7953f39233c0f8edf97a4b9473316fab8a719842cc67abfe42bf
Long public: 041f38fca1028756e46655135ea553feed52e112eeb69de6e0c02e10088778a8e30657185a2ad87eed06b306e8816b578e7bb03defafbe1b7ce25045dc88a8c03c
Compressed Public: 021f38fca1028756e46655135ea553feed52e112eeb69de6e0c02e10088778a8e3
RipeMD (Compressed) Public: 3150940d52e2179c13be6d125ba0bed252f4456a
RipeMD (Uncompressed) Public: f9041b8c8a14fb7625f5f5fad8acef964d8a538b

PEMs:

-----BEGIN EC PRIVATE KEY-----
MC4CAQEEIE3ldc8qinlT85IzwPjt+XpLlHMxb6uKcZhCzGer/kK/oAcGBSuBBAAK
-----END EC PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE/kNqXAN46B9kMO5F9RoI1BcUquJFxAln
/oyGwvY1wp/O2TW1yg4O1IaXz+zyuoxcAvVG0oZ+5K8ipJFPStPbNA==
-----END PUBLIC KEY-----

Keep these safe

Requires

License

BSD-3-Clause

Keywords

ECDSA, keygen, keypair, btc,

Latest Posts


Copyright © 2022 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion and http://snippets.bentasker.i2p
hit counter