Skip to content

Commit

Permalink
fix: remove depricated elliptic.Marshal function
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa committed Oct 29, 2024
1 parent 272ca6c commit f8dee19
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
21 changes: 12 additions & 9 deletions pkg/accesscontrol/grantee.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ package accesscontrol
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"fmt"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethersphere/bee/v2/pkg/file"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
Expand Down Expand Up @@ -85,7 +85,10 @@ func (g *GranteeListStruct) Add(addList []*ecdsa.PublicKey) error {

// Save saves the grantee list to the underlying storage and returns the reference.
func (g *GranteeListStruct) Save(ctx context.Context) (swarm.Address, error) {
data := serialize(g.grantees)
data, err := serialize(g.grantees)
if err != nil {
return swarm.ZeroAddress, fmt.Errorf("grantee serialize error: %w", err)
}
refBytes, err := g.loadSave.Save(ctx, data)
if err != nil {
return swarm.ZeroAddress, fmt.Errorf("grantee save error: %w", err)
Expand Down Expand Up @@ -140,16 +143,16 @@ func NewGranteeListReference(ctx context.Context, ls file.LoadSaver, reference s
}, nil
}

func serialize(publicKeys []*ecdsa.PublicKey) []byte {
func serialize(publicKeys []*ecdsa.PublicKey) ([]byte, error) {
b := make([]byte, 0, len(publicKeys)*publicKeyLen)
for _, key := range publicKeys {
b = append(b, serializePublicKey(key)...)
// TODO: check if this is the correct way to serialize the public key
// Is this the only curve we support?
// Should we have switch case for different curves?
pubBytes := crypto.S256().Marshal(key.X, key.Y)
b = append(b, pubBytes...)
}
return b
}

func serializePublicKey(pub *ecdsa.PublicKey) []byte {
return elliptic.Marshal(pub.Curve, pub.X, pub.Y)
return b, nil
}

func deserialize(data []byte) []*ecdsa.PublicKey {
Expand Down
3 changes: 2 additions & 1 deletion pkg/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethersphere/bee/v2/pkg/swarm"
"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -114,7 +115,7 @@ func NewEthereumAddress(p ecdsa.PublicKey) ([]byte, error) {
if p.X == nil || p.Y == nil {
return nil, errors.New("invalid public key")
}
pubBytes := elliptic.Marshal(btcec.S256(), p.X, p.Y)
pubBytes := crypto.S256().Marshal(p.X, p.Y)

This comment has been minimized.

Copy link
@gacevicljubisa

gacevicljubisa Oct 31, 2024

Author Contributor

Reason for this is that in go-ethereum/crypto package is using next code to obtain bytes from ecdsa.PublicKey:

func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
	if pub == nil || pub.X == nil || pub.Y == nil {
		return nil
	}
	return S256().Marshal(pub.X, pub.Y)
}

This is the PR used for this changes in go-ethereum ethereum/go-ethereum#28946

pubHash, err := LegacyKeccak256(pubBytes[1:])
if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion pkg/keystore/file/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func encryptKey(k *ecdsa.PrivateKey, password string, edg keystore.EDG) ([]byte,
}
addr = a
case elliptic.P256():

This comment has been minimized.

Copy link
@gacevicljubisa

gacevicljubisa Oct 31, 2024

Author Contributor

ECDH() method will call internal method:

func curveToECDH(c elliptic.Curve) ecdh.Curve {
	switch c {
	case elliptic.P256():
		return ecdh.P256()
	case elliptic.P384():
		return ecdh.P384()
	case elliptic.P521():
		return ecdh.P521()
	default:
		return nil
	}
}

And this means that elliptic.P256() is supported by ESDH()

addr = elliptic.Marshal(elliptic.P256(), k.PublicKey.X, k.PublicKey.Y)
privKey, err := k.ECDH()
if err != nil {
return nil, fmt.Errorf("generate key: %w", err)
}
addr = privKey.PublicKey().Bytes()
default:
return nil, fmt.Errorf("unsupported curve: %v", k.PublicKey.Curve)
}
Expand Down

0 comments on commit f8dee19

Please sign in to comment.