mpcium icon indicating copy to clipboard operation
mpcium copied to clipboard

How to decode S256 PubKey

Open yl opened this issue 1 year ago • 1 comments

https://github.com/fystack/mpcium/blob/173438eaa3613f9652e09e0205e3f1a3d59e50c1/pkg/encoding/ecdsa.go#L7

I'm trying to derive an Ethereum address from PubKey, but I can't find any decode function.

yl avatar Sep 18 '25 10:09 yl

Try this one

import (
        "github.com/btcsuite/btcd/btcec/v2"
        "crypto/ecdsa"
	"github.com/ethereum/go-ethereum/crypto"
)

func DecodeECDSAPubKey(encodedKey []byte) (*ecdsa.PublicKey, error) {
	xBytes := encodedKey[:32] // 32 bytes for X coordinate
	yBytes := encodedKey[32:] // 32 bytes for Y coordinate
	x := new(big.Int).SetBytes(xBytes)
	y := new(big.Int).SetBytes(yBytes)
	return &ecdsa.PublicKey{Curve: btcec.S256(), X: x, Y: y}, nil
}

func PubkeyToEthAddress(pubkey []byte) (string, error) {
	pubKey, err :=DecodeECDSAPubKey(pubkey)
	if err != nil {
		return "", err
	}

	ethAddress := crypto.PubkeyToAddress(*pubKey)
	return ethAddress.Hex(), nil
}

We also have examples in Typescript https://github.com/fystack/mpcium-client-ts/blob/b76e59ab90df53327a8541fc3dde573bf7bd3d37/examples/generate.ts#L56

anhthii avatar Sep 18 '25 10:09 anhthii