You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/pkg/sign/sign.go

47 lines
1.1 KiB

// Copyright 2022 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
3 years ago
package sign
import (
"crypto/ecdsa"
"crypto/sha256"
"time"
"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
"github.com/rocboss/paopao-ce/pkg/crypto"
"google.golang.org/protobuf/proto"
3 years ago
)
// SignTransaction 签名交易
func SignTransaction(transaction *core.Transaction, key *ecdsa.PrivateKey) ([]byte, error) {
transaction.GetRawData().Timestamp = time.Now().UnixNano() / 1000000
rawData, err := proto.Marshal(transaction.GetRawData())
if err != nil {
return nil, err
}
h256h := sha256.New()
h256h.Write(rawData)
hash := h256h.Sum(nil)
contractList := transaction.GetRawData().GetContract()
for range contractList {
signature, err := crypto.Sign(hash, key)
if err != nil {
return nil, err
}
transaction.Signature = append(transaction.Signature, signature)
}
return hash, nil
}
func TrimLeftZeroes(s []byte) []byte {
idx := 0
for ; idx < len(s); idx++ {
if s[idx] != 48 {
break
}
}
return s[idx:]
}