package sign import ( "crypto/ecdsa" "crypto/sha256" "encoding/hex" "errors" "fmt" "github.com/ethereum/go-ethereum/crypto" "github.com/fbsobreira/gotron-sdk/pkg/proto/core" "google.golang.org/protobuf/proto" ) // SignTransaction // // @Description: // @param transaction // @param privateKey // @return *core.Transaction // @return error func SignTransaction(transaction *core.Transaction, privateKey string) (*core.Transaction, error) { privateBytes, err := hex.DecodeString(privateKey) if err != nil { return nil, errors.New(fmt.Errorf("hex decode private key error:%v", err).Error()) } prIv := crypto.ToECDSAUnsafe(privateBytes) defer zeroKey(prIv) rawData, err := proto.Marshal(transaction.GetRawData()) if err != nil { return nil, errors.New(fmt.Errorf("proto marshal tx raw data error:%v", err).Error()) } h256h := sha256.New() h256h.Write(rawData) hash := h256h.Sum(nil) signature, err := crypto.Sign(hash, prIv) if err != nil { return nil, errors.New(err.Error()) } transaction.Signature = append(transaction.Signature, signature) return transaction, nil } // zeroKey // // @Description: // @param k func zeroKey(k *ecdsa.PrivateKey) { b := k.D.Bits() for i := range b { b[i] = 0 } }