-
Notifications
You must be signed in to change notification settings - Fork 126
/
assetQuotationEIP712Signer.go
116 lines (97 loc) · 2.9 KB
/
assetQuotationEIP712Signer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package utils
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
)
type AssetQuotationSigner struct {
privateKey string
domain TypedDataDomain
types Types
}
func NewAssetQuotationSigner(privateKey string) *AssetQuotationSigner {
domain := TypedDataDomain{
Name: "DiaData",
Version: "1.0.0",
ChainId: math.NewHexOrDecimal256(1),
VerifyingContract: common.HexToAddress("0x0000000000000000000000000000000000000000").Hex(),
}
types := Types{
"Message": []Type{
{Name: "Symbol", Type: "string"},
{Name: "Address", Type: "address"},
{Name: "Blockchain", Type: "string"},
{Name: "Price", Type: "uint256"},
{Name: "Time", Type: "uint256"},
},
"EIP712Domain": []Type{
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
{Name: "verifyingContract", Type: "address"},
},
}
return &AssetQuotationSigner{privateKey: privateKey, domain: domain, types: types}
}
func (aqs *AssetQuotationSigner) Sign(symbol, address, blockchain string, price float64, time time.Time) (string, error) {
message := map[string]interface{}{
"Symbol": symbol,
"Address": common.HexToAddress(address).Hex(),
"Blockchain": blockchain,
"Price": big.NewInt(int64(price * 100000000)).String(),
"Time": big.NewInt(time.Unix()).String(),
}
typedData := TypedData{
Types: aqs.types,
PrimaryType: "Message",
Domain: aqs.domain,
Message: message,
}
typedDataHash, _, err := TypedDataAndHash(typedData)
if err != nil {
return "", err
}
pk, err := hexToPrivateKey(aqs.privateKey)
if err != nil {
return "", err
}
signature, err := crypto.Sign(typedDataHash, pk)
if err != nil {
return "", err
}
var buf bytes.Buffer
buf.Write(signature)
// buf.WriteByte(1) // recovery ID
sigData := hexutil.Encode(buf.Bytes())
// fmt.Println("sigdata", sigData)
return sigData, nil
}
func hexToPrivateKey(hexKey string) (*ecdsa.PrivateKey, error) {
// Decode the hex string into a byte slice
keyBytes, err := hex.DecodeString(hexKey)
if err != nil {
return nil, err
}
// Parse the byte slice as a big integer
keyBigInt := new(big.Int).SetBytes(keyBytes)
// Generate a new secp256k1 elliptic curve
curve := elliptic.P256()
// Convert the big integer to a private key using the elliptic curve
privateKey := new(ecdsa.PrivateKey)
privateKey.PublicKey.Curve = curve
privateKey.D = keyBigInt
privateKey.PublicKey.X, privateKey.PublicKey.Y = curve.ScalarBaseMult(keyBytes)
// Verify that the private key is valid
if !curve.IsOnCurve(privateKey.PublicKey.X, privateKey.PublicKey.Y) {
return nil, fmt.Errorf("invalid private key")
}
return privateKey, nil
}