Skip to content

Commit

Permalink
fix: key secp256 marshal issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn-Huang-Tron committed Mar 22, 2023
1 parent 050a5d0 commit ef7ebaf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func Decrypt(key, text []byte) ([]byte, error) {
}

func GetPubKeyFromPeerId(pid string) (ic.PubKey, error) {
peerId, err := peer.IDFromBytes([]byte(pid))
peerId, err := peer.Decode(pid)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion crypto/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func FromIcPrivateKey(privKey ic.PrivKey) (*Keys, error) {
return nil, err
}

pubKeyRaw, err := ic.MarshalPublicKey(pubKey)
pubKeyRaw, err := Secp256k1PublicKeyRaw(pubKey)
if err != nil {
return nil, err
}
Expand Down
21 changes: 16 additions & 5 deletions crypto/tron_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"fmt"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/tron-us/go-common/v2/crypto"
"github.com/tron-us/protobuf/proto"

Expand Down Expand Up @@ -41,7 +43,7 @@ func GetTronPubKeyFromPubkey(pubkeyS string) (*string, error) {
}

func GetTronPubKeyFromPeerIdPretty(peerId string) (*string, error) {
pid, err := peer.IDFromBytes([]byte(peerId))
pid, err := peer.Decode(peerId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -80,12 +82,11 @@ func TronSignRaw(privKey ic.PrivKey, data []byte) ([]byte, error) {
}

func GetTronPubKeyFromIcPubKey(pubkey ic.PubKey) (*string, error) {
pubkeyRaw, err := ic.MarshalPublicKey(pubkey)
rawPubKey, err := Secp256k1PublicKeyRaw(pubkey)
if err != nil {
return nil, err
}

ethPubkey, err := eth.UnmarshalPubkey(pubkeyRaw)
ethPubkey, err := eth.UnmarshalPubkey(rawPubKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -117,7 +118,7 @@ func EcdsaPublicKeyToAddress(p ecdsa.PublicKey) (Address, error) {
}

func GetRawFullFromPeerIdPretty(peerid string) ([]byte, error) {
peerId, err := peer.IDFromBytes([]byte(peerid))
peerId, err := peer.Decode(peerid)
if err != nil {
return nil, err
}
Expand All @@ -127,3 +128,13 @@ func GetRawFullFromPeerIdPretty(peerid string) ([]byte, error) {
}
return pubkey.Raw()
}

// Raw returns the bytes of the key
func Secp256k1PublicKeyRaw(pk ic.PubKey) (res []byte, err error) {
// defer func() { catch.HandlePanic(recover(), &err, "secp256k1 public key marshaling") }()
k, ok := pk.(*ic.Secp256k1PublicKey)
if !ok {
return nil, fmt.Errorf("only secp256k1 keys support full public key bytes")
}
return (*secp256k1.PublicKey)(k).SerializeUncompressed(), nil
}

0 comments on commit ef7ebaf

Please sign in to comment.