Skip to content

Instantly share code, notes, and snippets.

@sambacha
Created May 31, 2025 17:21
Show Gist options
  • Save sambacha/fddf349cacb0cc0989c4222fc06baa07 to your computer and use it in GitHub Desktop.
Save sambacha/fddf349cacb0cc0989c4222fc06baa07 to your computer and use it in GitHub Desktop.
web authn private key to keystore format
#!usr/bin/env bash
# !ACHTUNG: Use at your own risk, AI slop generated
# You can export web authn private key using devtools web authn thingy
# Play stupid games, win retarded prizes
# Avoid contact with eyes, apply directly to forehead
#
PEM_FILE="private_key.pem"
KEYSTORE_NAME="my_eth_wallet"
echo "Extracting private key from PEM file..."
if openssl ec -in "$PEM_FILE" -noout 2>/dev/null; then
# Extract private key as hex
PRIVATE_KEY=$(openssl ec -in "$PEM_FILE" -noout -text 2>/dev/null | \
sed -n '/priv:/,/pub:/p' | \
grep -v pub: | \
tr -d ' \n:' | \
sed 's/priv://')
# Ensure it starts with 0x
if [[ ! $PRIVATE_KEY == 0x* ]]; then
PRIVATE_KEY="0x$PRIVATE_KEY"
fi
# There should probably be a bytes size check too but hey chatgpt free plan you know
# Man this is sketchy
echo "Private key extracted: ${PRIVATE_KEY:0:10}..."
#
# Import into cast keystore
echo "Importing into cast keystore..."
cast wallet import "$KEYSTORE_NAME" --private-key "$PRIVATE_KEY"
echo "Wallet imported successfully! Use with:"
echo "cast send --account $KEYSTORE_NAME --rpc-url \$RPC_URL ..."
else
echo "Error: PEM file doesn't contain an EC private key suitable for Ethereum"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment