Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private Key for Generated Account #123

Closed
rudreshkoranne opened this issue Feb 3, 2025 · 5 comments
Closed

Private Key for Generated Account #123

rudreshkoranne opened this issue Feb 3, 2025 · 5 comments

Comments

@rudreshkoranne
Copy link

Hey there all!!!

I have generated an account using aptos-labs/aptos-go-sdk as such
acc1, _ := aptos.NewEd25519Account()

I want to ask how to get access of the Private Key for this generated Account.

@charlesmarvin
Copy link

I've been using the following which I lifted from aptos.NewEd25519Account() but feels like something that can (should?) be exposed by the sdk api.

pk, err := crypto.GenerateEd25519PrivateKey()
...
acc1, err := aptos.NewAccountFromSigner(pk)
...

@gregnazario
Copy link
Contributor

Would love to make a new function for this, what would you like to see?

Atm, it can fairly complicated, but you can do some crafty stuff here:

acc1, _ := aptos.NewEd25519Account()
pk, ok := acc1.Signer.(*crypto.Ed25519PrivateKey)

Thinking something like:

// ExtractMessageSigner extracts the message signer from the account for
func (account *Account) ExtractMessageSigner() (crypto.MessageSigner, bool) {
	ed25519PrivKey, ok := account.Signer.(*crypto.Ed25519PrivateKey)
	if ok {
		return ed25519PrivKey, ok
	}
	singleSigner, ok := account.Signer.(*crypto.SingleSigner)
	if ok {
		return singleSigner.Signer, ok
	}
	return nil, false
}

Or alternatively can just make an "ExtractPrivateKeyString"

/ ExtractPrivateKeyString extracts the private key string
func (account *Account) ExtractPrivateKeyString() (string, error) {
	// Handle the key by itself
	ed25519PrivateKey, ok := account.Signer.(*crypto.Ed25519PrivateKey)
	if ok {
		return ed25519PrivateKey.ToAIP80()
	}
	
	// Handle key in single signer
	singleSigner, ok := account.Signer.(*crypto.SingleSigner)
	if !ok {
		innerSigner := singleSigner.Signer
		switch innerSigner.(type) {
		case *crypto.Ed25519PrivateKey:
			return innerSigner.(*crypto.Ed25519PrivateKey).ToAIP80()
		case *crypto.Secp256k1PrivateKey:
			return innerSigner.(*crypto.Secp256k1PrivateKey).ToAIP80()
		}
	}

	return "", errors.New("signer is not a private key")
}

@gregnazario
Copy link
Contributor

Here's an attempt at it with some other fixes #125

@charlesmarvin
Copy link

@gregnazario 👋 I was away for a few weeks 👶 Thanks for putting up the PR. I left some comments.

@gregnazario
Copy link
Contributor

I've committed with that feedback! Please reopen if it's not enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants