fbsobreira / gotron-sdk

Tron SDK for golang / CLI tool with keystore manager
GNU Lesser General Public License v3.0
322 stars 206 forks source link

undefined: secp256k1.RecoverPubkey #103

Open yangyile1990 opened 1 year ago

yangyile1990 commented 1 year ago

/go/pkg/mod/github.com/fbsobreira/gotron-sdk@v0.0.0-20230907131216-1e824406fe8c/pkg/keystore/recover.go:17:33: undefined: secp256k1.RecoverPubkey

FROM golang:alpine as builder
#FROM golang:1.20 AS builder

#由于是使用了私有的gitlab的代码包,因此在编译时需要用到 git 命令拉代码
RUN apk update && apk add --no-cache git
#RUN apt-get update && apt-get install -y git

WORKDIR /work

ADD . /work

#由于 go module 中使用了私有gitlab的代码包,因此需要拉代码,需要密码
RUN cp ./config/.netrc ~/.netrc && chmod 0600 ~/.netrc

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main cmd/main.go

FROM amd64/alpine:latest
#FROM debian:stable-slim

WORKDIR /work

COPY --from=builder /work/main /work

CMD ["./main", "-path=./config"]
yangyile1990 commented 1 year ago

I knew it. because the:

//go:build !gofuzz && cgo
// +build !gofuzz,cgo

while I close the cgo:

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main cmd/main.go
yangyile1990 commented 1 year ago

but I cannot open the cgo.

unrecognized command-line option '-m64'

because:

在 Alpine Linux 上,GCC 不支持 -m64 选项。

I think you should do some change. to make sure "even user not open cgo, can also use the sdk". thank you.

yangyile1990 commented 1 year ago

I can build with this dockerfile config.

#FROM golang:alpine as builder
#FROM golang:1.20 AS builder
FROM --platform=linux/amd64 golang:1.20-alpine AS builder

# 由于是使用了私有的gitlab的代码包,因此在编译时需要用到 git 命令拉代码
# 更新包并安装 Git
RUN apk update && apk add --no-cache git
#RUN apt-get update && apt-get install -y git

RUN apk add --no-cache gcc
RUN apk add --no-cache build-base

WORKDIR /work

ADD . /work

#由于 go module 中使用了私有gitlab的代码包,因此需要拉代码,需要密码
# 复制 .netrc 文件并设置权限
RUN cp ./config/.netrc ~/.netrc && chmod 0600 ~/.netrc

RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o main cmd/main.go

FROM amd64/alpine:latest
#FROM debian:stable-slim

WORKDIR /work

COPY --from=builder /work/main /work

CMD ["./main", "-path=./config"]

But it takes me about 288.4 seconds.

[+] Building 288.4s (17/17) FINISHED 

So, could you please make some change. to make sure, even not open cgo. can build success.

kslamph commented 1 year ago

https://github.com/fbsobreira/gotron-sdk/pull/107 this is a fallback implementation of secp256k1.RecoverPubkey without cgo

yangyile1990 commented 11 months ago

107 this is a fallback implementation of secp256k1.RecoverPubkey without cgo

waiting for your good news.

pengbotter commented 6 months ago

Use the btcec library to replace it

import ( btcecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/fbsobreira/gotron-sdk/pkg/address" "crypto/ecdsa" )

func Recover(signature, data []byte) (*ecdsa.PublicKey, error) { if len(signature) != 65 { return nil, errors.New("invalid signature length") } // Convert to btcec input format with 'recovery id' v at the beginning. btcsig := make([]byte, 65) btcsig[0] = signature[64] copy(btcsig[1:], signature)

//hash, err := hashWithEthereumPrefix(data)
//if err != nil {
//  return nil, err
//}

pbk, _, err := btcecdsa.RecoverCompact(btcsig, data)
if err != nil {
    return nil, err
}
return pbk.ToECDSA(), err

}

// RecoverPubkey To secp256k1.RecoverPubkey func RecoverPubkey(hash []byte, signature []byte) (address.Address, error) { pubKey1, err := Recover(signature, hash) if err != nil { return nil, err }

addr := address.PubkeyToAddress(*pubKey1)
return addr, nil

}