From 5f4a2fb0b4a6875e5bd084ece39d2bc36a7f0ec7 Mon Sep 17 00:00:00 2001 From: Abhinav Prakash Date: Thu, 11 Jul 2024 22:00:52 +0530 Subject: [PATCH 1/5] fix::> used crypto package to derive public key from private key --- account/keystore.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/account/keystore.go b/account/keystore.go index 72c8c389..8b6a2078 100644 --- a/account/keystore.go +++ b/account/keystore.go @@ -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 { @@ -38,14 +40,21 @@ func NewMemKeystore() *MemKeystore { // SetNewMemKeystore returns a new instance of MemKeystore and sets the given public key and private key in it. // // Parameters: -// - pub: a string representing the public key // - priv: a pointer to a big.Int representing the private key // Returns: // - *MemKeystore: a pointer to the newly created MemKeystore instance -func SetNewMemKeystore(pub string, priv *big.Int) *MemKeystore { +// - error: if any error occurs during conversion +func SetNewMemKeystore(priv *big.Int) (*MemKeystore, error) { ks := NewMemKeystore() + + pubX, _, err := curve.Curve.PrivateToPoint(priv) + if err != nil { + return nil, err + } + + pub := utils.BigIntToFelt(pubX).String() ks.Put(pub, priv) - return ks + return ks, nil } // Put stores the given key in the keystore for the specified sender address. @@ -126,7 +135,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 @@ -156,3 +167,7 @@ func GetRandomKeys() (*MemKeystore, *felt.Felt, *felt.Felt) { return ks, pubFelt, privFelt } + +// func derivePublicKeyFromPrivateKey(priv *big.Int) (*ecdsa.PublicKey, error) { +// privBytes := priv.Bytes() +// } From d223a45c4f8ec940eb4a08b36a8f374a023845e6 Mon Sep 17 00:00:00 2001 From: Abhinav Prakash Date: Thu, 18 Jul 2024 09:59:15 +0530 Subject: [PATCH 2/5] fix:> Added new Signer.go file and reverted all the changes in keystore.go --- account/keystore.go | 17 +++----------- account/signer.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 account/signer.go diff --git a/account/keystore.go b/account/keystore.go index 8b6a2078..e56606e9 100644 --- a/account/keystore.go +++ b/account/keystore.go @@ -40,21 +40,14 @@ func NewMemKeystore() *MemKeystore { // SetNewMemKeystore returns a new instance of MemKeystore and sets the given public key and private key in it. // // Parameters: +// - pub: a string representing the public key // - priv: a pointer to a big.Int representing the private key // Returns: // - *MemKeystore: a pointer to the newly created MemKeystore instance -// - error: if any error occurs during conversion -func SetNewMemKeystore(priv *big.Int) (*MemKeystore, error) { +func SetNewMemKeystore(pub string, priv *big.Int) *MemKeystore { ks := NewMemKeystore() - - pubX, _, err := curve.Curve.PrivateToPoint(priv) - if err != nil { - return nil, err - } - - pub := utils.BigIntToFelt(pubX).String() ks.Put(pub, priv) - return ks, nil + return ks } // Put stores the given key in the keystore for the specified sender address. @@ -167,7 +160,3 @@ func GetRandomKeys() (*MemKeystore, *felt.Felt, *felt.Felt) { return ks, pubFelt, privFelt } - -// func derivePublicKeyFromPrivateKey(priv *big.Int) (*ecdsa.PublicKey, error) { -// privBytes := priv.Bytes() -// } diff --git a/account/signer.go b/account/signer.go new file mode 100644 index 00000000..fa4634ef --- /dev/null +++ b/account/signer.go @@ -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) { + 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) { + 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 +} From a32470581506315d5badcc6500c42d5e19c5c091 Mon Sep 17 00:00:00 2001 From: Abhinav Prakash Date: Fri, 19 Jul 2024 12:36:57 +0530 Subject: [PATCH 3/5] fix::> updated NewSigner and Put func --- account/signer.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/account/signer.go b/account/signer.go index fa4634ef..08f1b13b 100644 --- a/account/signer.go +++ b/account/signer.go @@ -2,6 +2,7 @@ package account import ( "context" + "fmt" "math/big" "github.com/NethermindEth/starknet.go/curve" @@ -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) + if !ok { + return nil, fmt.Errorf("invalid private key format") + } + pubKey, err := getPublicKey(privateKey) if err != nil { return nil, err } - keyStore := SetNewMemKeystore(pubKey, privateKey) + var keyStore *MemKeystore + if existingKeystore != nil { + keyStore = existingKeystore + } else { + keyStore = SetNewMemKeystore(pubKey, privateKey) + } return &Signer{ keystore: keyStore, @@ -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) + if !ok { + return fmt.Errorf("invalid private key format") + } + s.keystore.Put(s.publicKey, privateKey) + return nil } func (s *Signer) Sign(ctx context.Context, msgHash *big.Int) (*big.Int, *big.Int, error) { From b936430a3eaa56667c242b57fa0efdb9e39e3d7c Mon Sep 17 00:00:00 2001 From: Abhinav Prakash Date: Tue, 23 Jul 2024 22:58:33 +0530 Subject: [PATCH 4/5] fix::> Made required changes inimpl of NewSigner and Put and changed the docs --- account/signer.go | 71 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/account/signer.go b/account/signer.go index 08f1b13b..d1b5722f 100644 --- a/account/signer.go +++ b/account/signer.go @@ -14,23 +14,26 @@ type Signer struct { publicKey string } -func NewSigner(privateKeyHex string, existingKeystore *MemKeystore) (*Signer, error) { - privateKey, ok := new(big.Int).SetString(privateKeyHex, 16) +// NewSigner creates a new Signer instance with the provided private key. +// +// Parameters: +// - privateKey: a string representing the private key (in any format). +// +// Returns: +// - *Signer: a pointer to the created Signer instance. +// - error: an error if the private key is invalid or if the public key cannot be derived. +func NewSigner(privateKey string) (*Signer, error) { + pkBigInt, ok := new(big.Int).SetString(privateKey, 0) if !ok { - return nil, fmt.Errorf("invalid private key format") + return nil, fmt.Errorf("invalid private key value") } - pubKey, err := getPublicKey(privateKey) + pubKey, err := getPublicKey(pkBigInt) if err != nil { return nil, err } - var keyStore *MemKeystore - if existingKeystore != nil { - keyStore = existingKeystore - } else { - keyStore = SetNewMemKeystore(pubKey, privateKey) - } + keyStore := SetNewMemKeystore(pubKey, pkBigInt) return &Signer{ keystore: keyStore, @@ -38,27 +41,69 @@ func NewSigner(privateKeyHex string, existingKeystore *MemKeystore) (*Signer, er }, nil } +// PublicKey returns the public key associated with the Signer. +// +// Returns: +// - string: the public key. func (s *Signer) PublicKey() string { return s.publicKey } +// MemKeyStore returns the keystore used by the Signer. +// +// Returns: +// - *MemKeystore: a pointer to the keystore. func (s *Signer) MemKeyStore() *MemKeystore { return s.keystore } -func (s *Signer) Put(priv string) error { +// Put stores a new private key in the keystore. +// +// Parameters: +// - priv: a string representing the private key in hexadecimal format. +// - existingKeystore: a pointer to an existing MemKeystore to use. If nil, the Signer's keystore is used. +// +// Returns: +// - error: an error if the private key is invalid. +func (s *Signer) Put(priv string, existingKeystore *MemKeystore) error { privateKey, ok := new(big.Int).SetString(priv, 16) if !ok { - return fmt.Errorf("invalid private key format") + return fmt.Errorf("invalid private key value") + } + + var keystoreToUse *MemKeystore + if existingKeystore != nil { + keystoreToUse = existingKeystore + } else { + keystoreToUse = s.keystore } - s.keystore.Put(s.publicKey, privateKey) + + keystoreToUse.Put(s.publicKey, privateKey) return nil } +// Sign signs a message hash using the private key stored in the keystore. +// +// Parameters: +// - ctx: the context.Context object for the signing operation. +// - msgHash: the hash of the message to sign. +// +// Returns: +// - *big.Int: the r part of the signature. +// - *big.Int: the s part of the signature. +// - error: an error if the signing process fails. func (s *Signer) Sign(ctx context.Context, msgHash *big.Int) (*big.Int, *big.Int, error) { return s.keystore.Sign(ctx, s.publicKey, msgHash) } +// getPublicKey derives the public key from the given private key. +// +// Parameters: +// - priv: a big.Int representing the private key. +// +// Returns: +// - string: the derived public key. +// - error: an error if the public key cannot be derived. func getPublicKey(priv *big.Int) (string, error) { pubX, _, err := curve.Curve.PrivateToPoint(priv) if err != nil { From 35aeb91bd3af67d428703134f98c4b800a984a81 Mon Sep 17 00:00:00 2001 From: Abhinav Prakash Date: Thu, 25 Jul 2024 22:37:01 +0530 Subject: [PATCH 5/5] fix::> made changes in Put and SetString --- account/signer.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/account/signer.go b/account/signer.go index d1b5722f..d9d2a3f6 100644 --- a/account/signer.go +++ b/account/signer.go @@ -66,11 +66,18 @@ func (s *Signer) MemKeyStore() *MemKeystore { // Returns: // - error: an error if the private key is invalid. func (s *Signer) Put(priv string, existingKeystore *MemKeystore) error { - privateKey, ok := new(big.Int).SetString(priv, 16) + privateKey, ok := new(big.Int).SetString(priv, 0) if !ok { return fmt.Errorf("invalid private key value") } + pubKey, err := getPublicKey(privateKey) + if err != nil { + return err + } + + s.publicKey = pubKey + var keystoreToUse *MemKeystore if existingKeystore != nil { keystoreToUse = existingKeystore