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
24 changes: 20 additions & 4 deletions account/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"context"
"fmt"
"math/big"

"github.com/NethermindEth/starknet.go/curve"
Expand All @@ -13,13 +14,23 @@ type Signer struct {
publicKey string
}

func NewSigner(privateKey *big.Int) (*Signer, error) {
func NewSigner(privateKeyHex string, existingKeystore *MemKeystore) (*Signer, error) {
privateKey, ok := new(big.Int).SetString(privateKeyHex, 16)
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return nil, fmt.Errorf("invalid private key format")
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
}

pubKey, err := getPublicKey(privateKey)
if err != nil {
return nil, err
}

keyStore := SetNewMemKeystore(pubKey, privateKey)
var keyStore *MemKeystore
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
if existingKeystore != nil {
keyStore = existingKeystore
} else {
keyStore = SetNewMemKeystore(pubKey, privateKey)
}

return &Signer{
keystore: keyStore,
Expand All @@ -35,8 +46,13 @@ func (s *Signer) MemKeyStore() *MemKeystore {
return s.keystore
}

func (s *Signer) Put(priv *big.Int) {
s.keystore.Put(s.publicKey, priv)
func (s *Signer) Put(priv string) error {
privateKey, ok := new(big.Int).SetString(priv, 16)
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return fmt.Errorf("invalid private key format")
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
}
s.keystore.Put(s.publicKey, privateKey)
return nil
}

func (s *Signer) Sign(ctx context.Context, msgHash *big.Int) (*big.Int, *big.Int, error) {
Expand Down
Loading