-
Notifications
You must be signed in to change notification settings - Fork 3
/
sign_order.go
102 lines (94 loc) · 3.18 KB
/
sign_order.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
package go_cowswap
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/itsahedge/go-cowswap/util/signature-scheme/eip712"
"github.com/itsahedge/go-cowswap/util/signature-scheme/eth-sign"
"strings"
)
type SignOrderFunc func(*Client, *CounterOrder) (*CounterOrder, error)
var signOrderFuncs = map[string]SignOrderFunc{
"ethsign": SignEthSignOrder,
"eip712": SignEip712Order,
}
func (c *Client) SignOrder(o *CounterOrder) (*CounterOrder, error) {
signFunc, ok := signOrderFuncs[o.SigningScheme]
if !ok {
return nil, fmt.Errorf("invalid signing scheme: %s", o.SigningScheme)
}
return signFunc(c, o)
}
// SignEthSignOrder Signs order with eth_sign signing scheme
func SignEthSignOrder(c *Client, o *CounterOrder) (*CounterOrder, error) {
message := map[string]interface{}{
"sellToken": o.SellToken,
"buyToken": o.BuyToken,
"receiver": o.Receiver,
"sellAmount": o.SellAmount,
"buyAmount": o.BuyAmount,
"validTo": fmt.Sprintf("%d", o.ValidTo),
"appData": common.Hex2Bytes(strings.TrimPrefix(o.AppData, "0x")),
"feeAmount": o.FeeAmount,
"kind": o.Kind,
"partiallyFillable": o.PartiallyFillable,
"sellTokenBalance": o.SellTokenBalance,
"buyTokenBalance": o.BuyTokenBalance,
}
domain := apitypes.TypedDataDomain{
Name: "Gnosis Protocol",
Version: "v2",
ChainId: math.NewHexOrDecimal256(c.ChainId.Int64()), // NETWORK ID
VerifyingContract: GPv2Settlement,
}
typedData := apitypes.TypedData{
Types: Eip712OrderTypes,
PrimaryType: "Order",
Domain: domain,
Message: message,
}
sigBytes, err := eth_sign.SignEthSign(typedData, c.TransactionSigner.PrivateKey)
if err != nil {
return nil, err
}
signature := fmt.Sprintf("0x%s", common.Bytes2Hex(sigBytes))
o.Signature = signature
return o, nil
}
// SignEip712Order Signs order with EIP712 signing scheme
func SignEip712Order(c *Client, o *CounterOrder) (*CounterOrder, error) {
message := map[string]interface{}{
"sellToken": o.SellToken,
"buyToken": o.BuyToken,
"receiver": o.Receiver,
"sellAmount": o.SellAmount,
"buyAmount": o.BuyAmount,
"validTo": fmt.Sprintf("%d", o.ValidTo),
"appData": common.Hex2Bytes(strings.TrimPrefix(o.AppData, "0x")),
"feeAmount": o.FeeAmount,
"kind": o.Kind,
"partiallyFillable": o.PartiallyFillable,
"sellTokenBalance": o.SellTokenBalance,
"buyTokenBalance": o.BuyTokenBalance,
}
domain := apitypes.TypedDataDomain{
Name: "Gnosis Protocol",
Version: "v2",
ChainId: math.NewHexOrDecimal256(c.ChainId.Int64()),
VerifyingContract: GPv2Settlement,
}
typedData := apitypes.TypedData{
Types: Eip712OrderTypes,
PrimaryType: "Order",
Domain: domain,
Message: message,
}
sigBytes, err := eip712.SignTypedData(typedData, c.TransactionSigner.PrivateKey)
if err != nil {
return nil, err
}
signature := fmt.Sprintf("0x%s", common.Bytes2Hex(sigBytes))
o.Signature = signature
return o, nil
}