Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

fix: abort if the private key is different from existed account #265

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions cmd/ronin/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
ArgsUsage: "",
Category: "ACCOUNT COMMANDS",
Description: `
geth wallet import /path/to/my/presale.wallet
ronin wallet import /path/to/my/presale.wallet

will prompt for your password and imports your ether presale account.
It can be used non-interactively with the --password option taking a
Expand All @@ -55,7 +55,7 @@ passwordfile as argument containing the wallet password in plaintext.`,
utils.LightKDFFlag,
},
Description: `
geth wallet [options] /path/to/my/presale.wallet
ronin wallet [options] /path/to/my/presale.wallet

will prompt for your password and imports your ether presale account.
It can be used non-interactively with the --password option taking a
Expand Down Expand Up @@ -111,7 +111,7 @@ Print a short summary of all accounts`,
utils.LightKDFFlag,
},
Description: `
geth account new
ronin account new

Creates a new account and prints the address.

Expand All @@ -136,7 +136,7 @@ password to file or expose in any other way.
utils.LightKDFFlag,
},
Description: `
geth account update <address>
ronin account update <address>

Update an existing account.

Expand All @@ -148,7 +148,7 @@ format to the newest format or change the password for an account.

For non-interactive use the password can be specified with the --password flag:

geth account update [options] <address>
ronin account update [options] <address>

Since only one password can be given, only format update can be performed,
changing your password is only possible interactively.
Expand All @@ -166,7 +166,7 @@ changing your password is only possible interactively.
},
ArgsUsage: "<keyFile>",
Description: `
geth account import <keyfile>
ronin account import <keyfile>

Imports an unencrypted private key from <keyfile> and creates a new account.
Prints the address.
Expand All @@ -179,12 +179,29 @@ You must remember this password to unlock your account in the future.

For non-interactive use the password can be specified with the -password flag:

geth account import [options] <keyfile>
ronin account import [options] <keyfile>

Note:
As you can directly copy your encrypted accounts to another ethereum instance,
this import mechanism is not needed when you transfer an account between
nodes.
`,
},
{
Name: "check",
Usage: "Check if the account corresponding to private key exists",
Action: utils.MigrateFlags(accountCheck),
Flags: []cli.Flag{
utils.DataDirFlag,
utils.KeyStoreDirFlag,
},
ArgsUsage: "<keyFile>",
Description: `
ronin account check <keyfile>

Check if the account corresponding to the private key exists in keystore.

The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
`,
},
},
Expand Down Expand Up @@ -357,3 +374,27 @@ func accountImport(ctx *cli.Context) error {
fmt.Printf("Address: {%x}\n", acct.Address)
return nil
}

func accountCheck(ctx *cli.Context) error {
keyfile := ctx.Args().First()
if len(keyfile) == 0 {
utils.Fatalf("keyfile must be given as argument")
}
key, err := crypto.LoadECDSA(keyfile)
if err != nil {
utils.Fatalf("Failed to load the private key: %v", err)
}
address := crypto.PubkeyToAddress(key.PublicKey)

stack, _ := makeConfigNode(ctx)
for _, wallet := range stack.AccountManager().Wallets() {
for _, account := range wallet.Accounts() {
if account.Address == address {
fmt.Printf("Found account %x\n", address)
return nil
}
}
}
utils.Fatalf("Account %x not found", address)
return nil
}
34 changes: 24 additions & 10 deletions docker/chainnode/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,40 @@ accountsCount=$(
)

# private key
if [[ $accountsCount -le 0 ]]; then
echo "No accounts found"
if [[ ! -z $PRIVATE_KEY ]]; then
if [[ ! -z $PRIVATE_KEY ]]; then
echo "$PRIVATE_KEY" > ./private_key
if [[ $accountsCount -le 0 ]]; then
echo "No accounts found"
echo "Creating account from private key"
echo "$PRIVATE_KEY" > ./private_key
ronin account import ./private_key \
--datadir $datadir \
--keystore $KEYSTORE_DIR \
--password $PASSWORD_FILE
rm ./private_key
else
echo "Creating new account"
ronin account new \
set +e
ronin account check ./private_key \
--datadir $datadir \
--keystore $KEYSTORE_DIR \
--password $PASSWORD_FILE
--keystore $KEYSTORE_DIR 2> /dev/null
exitCode=$?
if [[ $exitCode -ne 0 ]]; then
echo "An account with different address already exists in $KEYSTORE_DIR"
echo "Please consider remove account in keystore" \
"or unset PRIVATE_KEY environment variable"
exit 1
fi
set -e
fi
rm ./private_key
unset PRIVATE_KEY
fi

if [[ ! -z $KEYSTORE_DIR ]]; then
accountsCount=$(
ronin account list --datadir $datadir --keystore $KEYSTORE_DIR \
2> /dev/null \
| wc -l
)

if [[ $accountsCount -gt 0 ]]; then
account=$(
ronin account list --datadir $datadir --keystore $KEYSTORE_DIR \
2> /dev/null \
Expand Down