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

chore::> Derive public key from private key using crypto package #594

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions account/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type MemKeystore struct {
// NewMemKeystore initializes and returns a new instance of MemKeystore.
//
// Parameters:
// none
//
// none
//
// Returns:
// - *MemKeystore: a pointer to MemKeystore.
func NewMemKeystore() *MemKeystore {
Expand Down Expand Up @@ -126,7 +128,9 @@ func sign(ctx context.Context, msgHash *big.Int, key *big.Int) (x *big.Int, y *b
// GetRandomKeys gets a random set of pub-priv keys.
// Note: This should be used for testing purposes only, do NOT send real funds to these addresses.
// Parameters:
// none
//
// none
//
// Returns:
// - *MemKeystore: a pointer to a MemKeystore instance
// - *felt.Felt: a pointer to a public key as a felt.Felt
Expand Down
54 changes: 54 additions & 0 deletions account/signer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package account

import (
"context"
"math/big"

"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/utils"
)

type Signer struct {
keystore *MemKeystore
publicKey string
}

func NewSigner(privateKey *big.Int) (*Signer, error) {
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
pubKey, err := getPublicKey(privateKey)
if err != nil {
return nil, err
}

keyStore := SetNewMemKeystore(pubKey, privateKey)

return &Signer{
keystore: keyStore,
publicKey: pubKey,
}, nil
}

func (s *Signer) PublicKey() string {
return s.publicKey
}

func (s *Signer) MemKeyStore() *MemKeystore {
return s.keystore
}

func (s *Signer) Put(priv *big.Int) {
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
s.keystore.Put(s.publicKey, priv)
}

func (s *Signer) Sign(ctx context.Context, msgHash *big.Int) (*big.Int, *big.Int, error) {
return s.keystore.Sign(ctx, s.publicKey, msgHash)
}

func getPublicKey(priv *big.Int) (string, error) {
pubX, _, err := curve.Curve.PrivateToPoint(priv)
if err != nil {
return "", err
}

pub := utils.BigIntToFelt(pubX).String()
return pub, nil
}
Loading