gotron-sdk icon indicating copy to clipboard operation
gotron-sdk copied to clipboard

github.com/btcsuite/btcd需要升级

Open RelicOfTesla opened this issue 3 years ago • 0 comments

最新版的go-ethereum(v1.10.21)使用的是github.com/btcsuite/btcd/btcec/v2 v2.2.0 現在gotron-sdk使用的是 github.com/btcsuite/btcd v0.22.0-beta 两个会有冲突。


import (
	"crypto/sha256"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/fbsobreira/gotron-sdk/pkg/keys"
	"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
	"github.com/golang/protobuf/proto"
	"github.com/pkg/errors"
)

func SignTrxTx(keyword string, txData []byte) ([]byte, string, error) {
	tx, err := Bytes2TrxTx(txData)
	if err != nil {
		return nil, "", err
	}
	rawData, err := proto.Marshal(tx.GetRawData())
	if err != nil {
		return nil, "", err
	}
	priv, _ := keys.FromMnemonicSeedAndPassphrase(keyword, "", 0)

	h256h := sha256.New()
	h256h.Write(rawData)
	hash := h256h.Sum(nil)

	signature, err := crypto.Sign(hash, priv.ToECDSA())    // 会报告 private key curve is not secp256k1
	if err != nil {
		return nil, "", err
	}

	tx.Signature = append(tx.Signature, signature)

	txData, err = TrxTx2Bytes(tx)
	if err != nil {
		return nil, "", err
	}
	hashStr := TrxHashFromBytes(hash).String()
	return txData, hashStr, nil
}

注意需要设置禁止cgo,即CGO_ENABLED=0编译 即两个库(go-ethereum 与 gotron-sdk)使用了不同的btcd库的secp256k1 导致的。

RelicOfTesla avatar Aug 15 '22 07:08 RelicOfTesla