-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
84 lines (63 loc) · 1.98 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package zkwasm
import (
"context"
"crypto/ecdsa"
"strings"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
const (
endpointImage = "/image"
endpointImageBinary = "/imagebinary"
endpointTasks = "/tasks"
endpointProve = "/prove"
endpointSetup = "/setup"
headerSignatureKey = "x-eth-signature"
)
type ZkWasmServiceHelper struct {
zkWasmEndpoint string
ethEndpoint string
wallet *ecdsa.PrivateKey
userAddress common.Address
ethClient *ethclient.Client
verifyContractAddress common.Address
}
func New(zkWasmEndpoint, ethEndpoint, privateKey, contractAddress string) (*ZkWasmServiceHelper, error) {
return NewWithContext(context.Background(), zkWasmEndpoint, ethEndpoint, privateKey, contractAddress)
}
func NewWithContext(ctx context.Context, zkWasmEndpoint, ethEndpoint, privateKey, contractAddress string) (*ZkWasmServiceHelper, error) {
h := &ZkWasmServiceHelper{}
h.zkWasmEndpoint = strings.TrimSuffix(zkWasmEndpoint, "/")
h.ethEndpoint = strings.TrimSuffix(ethEndpoint, "/")
ethC, err := ethclient.DialContext(ctx, h.ethEndpoint)
if err != nil {
return nil, err
}
h.ethClient = ethC
h.verifyContractAddress = common.HexToAddress(contractAddress)
privK, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
h.wallet = privK
h.userAddress = crypto.PubkeyToAddress(*h.wallet.Public().(*ecdsa.PublicKey))
return h, nil
}
func (h *ZkWasmServiceHelper) GetUserAddress() string {
return h.userAddress.Hex()
}
func (h *ZkWasmServiceHelper) signMessage(message string, legacyV bool) (string, error) {
hash := accounts.TextHash([]byte(message))
sign, err := crypto.Sign(hash, h.wallet)
if err != nil {
return "", err
}
// https://github.com/ethereum/go-ethereum/issues/19751
if legacyV {
sign[64] += 27
}
return hexutil.Encode(sign), nil
}