Skip to content

Instantly share code, notes, and snippets.

@ilap
Last active November 24, 2022 03:01
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save ilap/3fd57e39520c90f084d25b0ef2b96894 to your computer and use it in GitHub Desktop.
Save ilap/3fd57e39520c90f084d25b0ef2b96894 to your computer and use it in GitHub Desktop.
Extracting Pool Staking keys from Daedalus/Yoroi wallet

Intorduction

DISCLAIMER: NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK

UPDATED: 14:08am AEST 29/Sept/2020, The IDX was incorrectly used. Fixed now

There are two keypairs that are required to register a pool:

  1. reward account (costs and rewards) and
  2. owner stake (pledge) keypair.

Note: these keys can be the same when the owner and the operator are the same.

So, that would be nice if the pool operators/owner could track and controle their pool rewards from any wallet (Daedalus, Yoroi or any other wallet) that support stakings.

The aim of this document is achieve this, by using some Shelley wallet-, address- and key related tools.

The only problem is how to extract the staking key(s) from a wallet. Luckily there is the IOHK's cardano-wallet repo that contains cardano-address that can be used for this as it contains the BIP39, BIP32-ED25519 and master key generation functionalities which are required to achive this.

Create wallet and extract staking keys from it.

The following steps are required for extracting staking keys from a wallet:

  1. Generate a wallet and save the 15-word length mnemonic as usual.
  2. Use the mnemonic /w cardano-address cli to generate the master (root) key.
  3. Derive the staking signing key from the master key using the 1852H/1815H/0H/2/0 (cardano-address derivation format).
  4. generate the staking verification key from the above signing key
  5. Used the extracted keypair or keypairs (if additional wallets is created for owner and operator) for pool registration and or pledge.

To crate a wallet you can use the Daedalus 1.6.0-STN5 for creating wallet(s) as the address structure won't be changed on the mainnet. And write down and save that/those mnemonic.

Downloads

Cardano-wallet binaries

From latest releases https://github.com/input-output-hk/cardano-wallet/releases or from Hydra: https://hydra.iohk.io/build/3662127/download/1/cardano-wallet-shelley-2020.7.28-linux64.tar.gz https://hydra.iohk.io/build/3662151/download/1/cardano-wallet-shelley-2020.7.28-macos64.tar.gz https://hydra.iohk.io/build/3662143/download/1/cardano-wallet-shelley-2020.7.28-win64.zip

Script

#!/bin/bash 

CADDR=${CADDR:=$( which cardano-address )}
[[ -z "$CADDR" ]] && { echo "cardano-address cannot be found, exiting..." >&2 ; exit 127; }

CCLI=${CCLI:=$( which cardano-cli )}
[[ -z "$CCLI" ]] && { echo "cardano-cli cannot be found, exiting..." >&2 ; exit 127; }

BECH32=${BECH32:=$( which cardano-cli )}
[[ -z "$BECH32" ]] && { echo "bech32 cannot be found, exiting..." >&2 ; exit 127; }

# Only 24-word length mnemonic is supported only
[[ "$#" -ne 26 ]] && {
       	echo "usage: `basename $0` <change index e.g. 0/1 external/internal>  <ouptut dir> <24-word length mnemonic>" >&2 
       	exit 127
}

GEN_FILE=${GEN_FILE:="$HOME/conf/shelley-genesis.json"}
[[ ! -f "$GEN_FILE" ]] && { echo "genesis file does not exit, exiting..." >&2 ; exit 127; }

IDX=$1
shift 

OUT_DIR="$1"
[[ -e "$OUT_DIR"  ]] && {
       	echo "The \"$OUT_DIR\" is already exist delete and run again." >&2 
       	exit 127
} || mkdir -p "$OUT_DIR" && pushd "$OUT_DIR" >/dev/null

shift
MNEMONIC="$*"

# Generate the master key from mnemonics and derive the stake account keys 
# as extended private and public keys (xpub, xprv)
echo "$MNEMONIC" |\
"$CADDR" key from-recovery-phrase Shelley > root.prv

cat root.prv |\
"$CADDR" key child 1852H/1815H/0H/2/0 > stake.xprv

cat root.prv |\
"$CADDR" key child 1852H/1815H/0H/$IDX/0 > payment.xprv

# XPrv/XPub conversion to normal private and public key, keep in mind the 
# keypars are not a valind Ed25519 signing keypairs.
NW=$(jq '.networkId' -r "$GEN_FILE")
NW_ID=$(jq '.networkMagic' -r "$GEN_FILE")

echo "Generating $NW wallet..."
if [ "$NW" == "Testnet" ]; then
  NETWORK=0
  MAGIC="--testnet-magic $NW_ID"
  CONV="bech32 | bech32 addr_test"
else
  NETWORK=1
  MAGIC="--mainnet"
  CONV="cat"
fi

cat payment.xprv |\
"$CADDR" key public | tee payment.xpub |\
"$CADDR" address payment --network-tag $NETWORK |\
"$CADDR" address delegation $(cat stake.xprv | "$CADDR" key public | tee stake.xpub) |\
tee base.addr_candidate |\
"$CADDR" address inspect
echo "Generated from 1852H/1815H/0H/$IDX/0"
if [  "$NW" == "Testnet" ]; then
  cat base.addr_candidate | bech32 | bech32 addr_test > base.addr_candidate_test
  mv base.addr_candidate_test base.addr_candidate
fi
cat base.addr_candidate 
echo


SESKEY=$( cat stake.xprv | bech32 | cut -b -128 )$( cat stake.xpub | bech32)
PESKEY=$( cat payment.xprv | bech32 | cut -b -128 )$( cat payment.xpub | bech32)

cat << EOF > stake.skey
{
    "type": "StakeExtendedSigningKeyShelley_ed25519_bip32",
    "description": "",
    "cborHex": "5880$SESKEY"
}
EOF

cat << EOF > payment.skey
{
    "type": "PaymentExtendedSigningKeyShelley_ed25519_bip32",
    "description": "Payment Signing Key",
    "cborHex": "5880$PESKEY"
}
EOF

"$CCLI" shelley key verification-key --signing-key-file stake.skey --verification-key-file stake.evkey
"$CCLI" shelley key verification-key --signing-key-file payment.skey --verification-key-file payment.evkey

"$CCLI" shelley key non-extended-key --extended-verification-key-file payment.evkey --verification-key-file payment.vkey
"$CCLI" shelley key non-extended-key --extended-verification-key-file stake.evkey --verification-key-file stake.vkey


"$CCLI" shelley stake-address build --stake-verification-key-file stake.vkey $MAGIC > stake.addr
"$CCLI" shelley address build --payment-verification-key-file payment.vkey $MAGIC > payment.addr
"$CCLI" shelley address build \
    --payment-verification-key-file payment.vkey \
    --stake-verification-key-file stake.vkey \
    $MAGIC > base.addr

echo "Important the base.addr and the base.addr_candidate must be the same"
diff -s base.addr base.addr_candidate
cat base.addr base.addr_candidate

popd >/dev/null
@MarcelKlammer
Copy link

Since I use @gitmachtl scripts, I named the files like the scripts do, maybe it helps someone: https://gist.github.com/MarcelKlammer/2e44e64a0f0a743ce7400c1b863ced93

Write down the 24 words. Put them in Daedalus. The payment.addr is the very last addr. shown in the Receive section. If this addr matches the one generated, you are fine.

Keep in mind, that you must send ADA to that very last addr. Also, whenever you send ADA from that addr via Daedalus, send ALL ADA. Refund the last addr if necessary.

Good luck.

@Omegastaking
Copy link

Hi MarcelKlammer,

With your code example are you able to successfully register your stakepool using the stake address. Then signing the transaction with both the payment and stake secret keys and then sending the signed transaction. If so, what specific steps are you following to complete this?

@thiscaspar
Copy link

thiscaspar commented Aug 6, 2020

Is this also working for testnet? When trying that the candidate.addr is different from the addr.

Example output:
addr1qpnvjr678ja9dxhqvf38r3j0a3ex94n02ak4n3062smzn82p9ps3606y9v35kpcma02p4uu6plrt9kl52ck8ms5krajqmphqqg
addr_test1qpnvjr678ja9dxhqvf38r3j0a3ex94n02ak4n3062smzn82p9ps3606y9v35kpcma02p4uu6plrt9kl52ck8ms5krajqfpr872

Strangely enough the first part is different, and the last 7 characters, the rest is the same. The code works fine for mainnet.
Also, the first output is correct and matches Daedalus' last receive key.

@ThaiTheo
Copy link

ThaiTheo commented Aug 7, 2020

@coostendorp, it works for testnet too.
But you have to change the variables which default to mainnet and you have to adjust the testnet-magic number.

@thiscaspar
Copy link

@coostendorp, it works for testnet too.
But you have to change the variables which default to mainnet and you have to adjust the testnet-magic number.

Somehow it does not work for me.

Changed network:

TESTNET=0
MAINNET=1
NETWORK=$TESTNET

Changed MAGIC:

TESTNET_MAGIC="--testnet-magic 1097911063"
MAINNET_MAGIC="--mainnet"
MAGIC="$TESTNET_MAGIC"

I probably missed something obvious if it works for everyone else, but right now cannot imagine what.

@MarcelKlammer
Copy link

Didn't work for me either. It produced addr instead of addr_test prefixes and the last 4 hex values were different.

Not sure about the results.

Maybe just change the addr to addr_test and test those on testnet? Didn't test that.

@ThaiTheo
Copy link

ThaiTheo commented Aug 8, 2020

@MarcelKlammer then you didn't change the networks, like coostendorp did.

@ThaiTheo
Copy link

ThaiTheo commented Aug 8, 2020

@coostendorp it could be the Ubuntu version because its not a LTS version, but I don't know that for sure, I have tested it with Debian 10.

@ThaiTheo
Copy link

ThaiTheo commented Aug 8, 2020

Important notices to all who want to use this kind of wallet!

  1. The following command only sees the money which is send to the base address:
    cardano-cli shelley query utxo --address $(cat payment.addr) --mainnet
    So I suggest you only transfer the amount for the keyDeposit and the poolDeposit and some small amount of extra ADA because of the transaction fee's to the base address.
  2. Make sure you use a recovery phrase of 24 words to create a wallet. Recovery phrases which have either less or more words are not Shelley wallets.
  3. Be aware that besides sending ADA through the Daedalus wallet, you can now send ADA also with the payment.skey, so keep it in a safe place. (remember the payment.skey doesn't ask for a password for obvious reasons)
  4. The Windows version of the Daedalus wallet also comes with cardano-cli, so you can create keys and sign transactions on a Windows computer too if necessary.

@ilap
Copy link
Author

ilap commented Aug 9, 2020

Important notices to all who want to use this kind of wallet!

  1. The following command only sees the money which is send to the base address:
    cardano-cli shelley query utxo --address $(cat payment.addr) --mainnet
    So I suggest you only transfer the amount for the keyDeposit and the poolDeposit and some small amount of extra ADA because of the transaction fee's to the base address.
  2. Make sure you use a recovery phrase of 24 words to create a wallet. Recovery phrases which have either less or more words are not Shelley wallets.
  3. Be aware that besides sending ADA through the Daedalus wallet, you can now send ADA also with the payment.skey, so keep it in a safe place. (remember the payment.skey doesn't ask for a password for obvious reasons)
  4. The Windows version of the Daedalus wallet also comes with cardano-cli, so you can create keys and sign transactions on a Windows computer too if necessary.

Thx, also I have made it work /w Ledger Nano X/S hardware wallets. I will update this gist or create a new one.
But, it's a bit more complicated, so a different gist would be better to this.

@ilap
Copy link
Author

ilap commented Aug 10, 2020

Here comes the gist for Ledger's as pledge/reward keys. Be very carefull, and do everyting only offline. NO ANY online process should be involved.
https://gist.github.com/ilap/5af151351dcf30a2954685b6edc0039b

@staking4ada
Copy link

Hi, I am trying to get this done in Windows, in order for a partner to create a pledge address which he can manage himself. However, I cannot get past the bech32 conversion. Any chance someone can give a hint how to get past this step:

SESKEY=$( cat stake.xprv | bech32 | cut -b -128 )$( cat stake.xpub | bech32)
PESKEY=$( cat payment.xprv | bech32 | cut -b -128 )$( cat payment.xpub | bech32)

Is there any sign of IOHK working on an upcoming release which incorporates ilap's script functionality?

@ThaiTheo
Copy link

ThaiTheo commented Aug 31, 2020

Hi, I am trying to get this done in Windows
Is there any sign of IOHK working on an upcoming release which incorporates ilap's script functionality?

The script will not work in windows. Too many commands in the script that windows doesn't understand.
IOHK has more important things to do with the Daedulus wallet like the 1 to many delegation and hardware integration, so it will probably not be in there for a long time.

@staking4ada
Copy link

no problem for everything else. I already successfully got to the bech32 part, and I think the part after it shouldn't be a problem either, so I probably only need an equivalent for the bech32 conversion.

@AndrewWestberg
Copy link

no problem for everything else. I already successfully got to the bech32 part, and I think the part after it shouldn't be a problem either, so I probably only need an equivalent for the bech32 conversion.

I sent you a DM on telegram for usage under Windows.

@jbpin
Copy link

jbpin commented Feb 27, 2021

I find a way to generate payment.skey and stake.skey that do not use bech32 on Osx for the one who need it.

cardano-cli key convert-cardano-address-key --shelley-payment-key --signing-key-file payment.xprv --out-file payment.skey
cardano-cli key convert-cardano-address-key --shelley-stake-key --signing-key-file stake.xprv --out-file stake.skey

@monkey-jsun
Copy link

Thanks a lot! It works!

The first argument says "<change index e.g. 0/1 external/internal>". What does this mean? I think I just picked 0 and it worked.

@dertin
Copy link

dertin commented Apr 27, 2021

I have the following error using the wallet version "cardano-wallet-2021.4.8-linux64.tar.gz":

Generating Testnet wallet...
Usage: cardano-address key public (--without-chain-code | --with-chain-code)
  Get the public counterpart of a private key

Available options:
  -h,--help                Show this help text

The private key is read from stdin.To get extended public key pass '--with-chain-code'.To get public key pass '--without-chain-code'.
Usage: cardano-address key public (--without-chain-code | --with-chain-code)
  Get the public counterpart of a private key

Available options:
  -h,--help                Show this help text

The private user error (Bech32 error: string is too short)
key is read from stdin.To get extended public key pass '--with-chain-code'.To get public key pass '--without-chain-code'.
Usage: cardano-address address delegation KEY || SCRIPT HASH
  Create a delegation address

Available options:
  -h,--help                Show this help text
  KEY || SCRIPT HASH       An extended stake public key or a script hash.

The payment address is read from stdin.

Example:
  $ cardano-address recovery-phrase generate --size 15 \
    | cardano-address key from-recovery-phrase Shelley > root.prv

  $ cat root.prv \
    | cardano-address key child 1852H/1815H/0H/2/0 > stake.prv

  $ cat root.prv \
    | cardano-address key child 1852H/1815H/0H/0/0 > addr.prv

  $ cat addr.prv \
    | cardano-address key public --with-chain-code \
    | cardano-address address payment --network-tag testnet \
    | cardano-address address delegation $(cat stake.prv | cardano-address key public --with-chain-code)
  addr1qpj2d4dqzds5p3mmlu95v9pex2d72cdvyjh2u3dtj4yqesv27k...
Error: Wrong input size of 0
Generated from 1852H/1815H/0H/0/0
bech32: user error (StringToDecodeTooShort)
bech32: user error (Unable to detect input encoding. Neither Base16, Bech32 nor Base58.)

bech32: user error (StringToDecodeTooShort)
bech32: user error (StringToDecodeTooShort)
Command failed: key verification-key  Error: stake.skey: Invalid key.
Command failed: key verification-key  Error: payment.skey: Invalid key.
Command failed: key non-extended-key  Error: payment.evkey: payment.evkey: openBinaryFile: does not exist (No such file or directory)
Command failed: key non-extended-key  Error: stake.evkey: stake.evkey: openBinaryFile: does not exist (No such file or directory)
Command failed: stake-address build  Error: stake.vkey: stake.vkey: openBinaryFile: does not exist (No such file or directory)
Command failed: address build  Error: payment.vkey: payment.vkey: openBinaryFile: does not exist (No such file or directory)
Command failed: address build  Error: payment.vkey: payment.vkey: openBinaryFile: does not exist (No such file or directory)
Important the base.addr and the base.addr_candidate must be the same
Files base.addr and base.addr_candidate are identical

Can it work with that version of wallet?

cardano-address version

3.3.0 @ 7e062438fae15cf1025ab780adbe6ed26ac3e6e6

cardano-cli version

cardano-cli 1.26.2 - linux-x86_64 - ghc-8.10
git rev 3531289c9f79eab7ac5d3272ce6e6821504fec4c

@rgoerwit
Copy link

Same issue:

Error: Wrong input size of 0
Generated from 1852H/1815H/0H/0/0

cardano-address --version -> 3.4.0

CNTools v8.2.1

@ClecknerT
Copy link

Same issue:

Error: Wrong input size of 0
Generated from 1852H/1815H/0H/0/0

cardano-address --version -> 3.4.0

CNTools v8.2.1

@den-is
Copy link

den-is commented May 22, 2021

FIX for

Error: Wrong input size of 0
Generated from 1852H/1815H/0H/0/0

Make sure to add --with-chain-code. to every appearance of "$CADDR" key public in the script (two places at the moment of writing)

I have pushed updated/reworked version of the script here https://gist.github.com/den-is/0fb758b07cdb68abc968b3dea689fa9b.
Supports cardano-cli v1.27+ only

@ilap
Copy link
Author

ilap commented Jun 13, 2021

FIX for

Error: Wrong input size of 0
Generated from 1852H/1815H/0H/0/0

Make sure to add --with-chain-code. to every appearance of "$CADDR" key public in the script (two places at the moment of writing)

I have pushed updated/reworked version of the script here https://gist.github.com/den-is/0fb758b07cdb68abc968b3dea689fa9b.
Supports cardano-cli v1.27+ only

Cool thx for that.

@adamsthws
Copy link

Thanks for the awesome tool! Quick question if I may...
I'm attempting to use the 24 word mnemonic from a ledger hw wallet, however, the script's generated address doesn't match any of my ledger addresses shown in Daedalus... Are the ledger mnemonics compatible?

@ilap
Copy link
Author

ilap commented Jul 28, 2021

Thanks for the awesome tool! Quick question if I may...
I'm attempting to use the 24 word mnemonic from a ledger hw wallet, however, the script's generated address doesn't match any of my ledger addresses shown in Daedalus... Are the ledger mnemonics compatible?

For ledger use this, as the master key generation is different.
But, I think it needs revised and updated, also be very careful do everything in air gapped device after you downloaded everything.
https://gist.github.com/ilap/5af151351dcf30a2954685b6edc0039b

@adamsthws
Copy link

Thanks for the awesome tool! Quick question if I may...
I'm attempting to use the 24 word mnemonic from a ledger hw wallet, however, the script's generated address doesn't match any of my ledger addresses shown in Daedalus... Are the ledger mnemonics compatible?

For ledger use this, as the master key generation is different.
But, I think it needs revised and updated, also be very careful do everything in air gapped device after you downloaded everything.
https://gist.github.com/ilap/5af151351dcf30a2954685b6edc0039b

Lovely, thankyou for the guidance, I'll let you know how it goes.
-Adam

@adamsthws
Copy link

adamsthws commented Aug 8, 2021

Thanks for the awesome tool! Quick question if I may...
I'm attempting to use the 24 word mnemonic from a ledger hw wallet, however, the script's generated address doesn't match any of my ledger addresses shown in Daedalus... Are the ledger mnemonics compatible?

For ledger use this, as the master key generation is different.
But, I think it needs revised and updated, also be very careful do everything in air gapped device after you downloaded everything.
https://gist.github.com/ilap/5af151351dcf30a2954685b6edc0039b

Lovely, thankyou for the guidance, I'll let you know how it goes.
-Adam

I'm happy to report this worked perfectly for the ledger method, thankyou ilap, you're a star!

@ilap
Copy link
Author

ilap commented Aug 8, 2021

@adamsthws 👌

@00TIA
Copy link

00TIA commented Jan 18, 2022

During the process of creating the stake pool at the command cardano-cli query utxo \ --address $(cat payment.addr) \ --mainnet I get this output.
any help is appreciated
Schermata 2022-01-18 alle 21 54 54

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