/
signed_calls.go
186 lines (162 loc) · 5.42 KB
/
signed_calls.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2023 Migwi Ndung'u
// Copyright (c) 2023 Sapphire-Paratime Authors
// See LICENSE for details.
package sapphire
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
)
const ZeroAddress = "0x0000000000000000000000000000000000000000"
// SignedCallDataPack defines a signed call.
//
// It should be encoded and sent in the `data` field of an Ethereum call.
type SignedCallDataPack struct {
Data Data `json:"data"`
Leash Leash `json:"leash"`
Signature []byte `json:"signature"`
}
// Data is the plain data in the datapack.
type Data struct {
Body []byte `json:"body"`
}
// DataEnvelope is an oasis-sdk `Call` without optional fields.
type DataEnvelope struct {
Body []byte `json:"body"`
Format uint64 `json:"format,omitempty"` // reuse for now, TODO swap later
}
// EncryptedBodyEnvelope is an oasis-sdk `Call` with optional fields.
type EncryptedBodyEnvelope struct {
Body Body `json:"body"`
Format uint64 `json:"format"`
}
type Body struct {
PK []byte `json:"pk"`
Data []byte `json:"data"`
Nonce []byte `json:"nonce"`
}
type Leash struct {
Nonce uint64 `json:"nonce"`
BlockNumber uint64 `json:"block_number"`
BlockHash []byte `json:"block_hash"`
BlockRange uint64 `json:"block_range"`
}
// NewDataPack returns a SignedCallDataPack.
//
// This method does not encrypt `data`, so that should be done afterwards.
func NewDataPack(sign SignerFn, privateKey []byte, chainID uint64, caller, callee []byte, gasLimit uint64, gasPrice, value *big.Int, data []byte, leash Leash) (*SignedCallDataPack, error) {
signable := makeSignableCall(chainID, caller, callee, gasLimit, gasPrice, value, data, leash)
signature, err := signTypedData(sign, privateKey, signable)
if err != nil {
return nil, fmt.Errorf("failed to sign call: %w", err)
}
return &SignedCallDataPack{
Data: Data{Body: data},
Leash: leash,
Signature: signature,
}, nil
}
func (p SignedCallDataPack) Encode() []byte {
return hexutil.Bytes(cbor.Marshal(p.Data.Body))
}
func (p SignedCallDataPack) EncryptEncode(cipher Cipher) []byte {
// Encrypt when data exists
if p.Data.Body != nil {
return cipher.EncryptEncode(p.Data.Body)
}
return p.Encode()
}
func NewLeash(nonce uint64, blockNumber uint64, blockHash []byte, blockRange uint64) Leash {
return Leash{
Nonce: nonce,
BlockNumber: blockNumber,
BlockHash: blockHash,
BlockRange: blockRange,
}
}
func makeSignableCall(chainID uint64, caller, callee []byte, gasLimit uint64, gasPrice *big.Int, value *big.Int, data []byte, leash Leash) apitypes.TypedData {
toAddr := ZeroAddress
// callee should exist except for contract creation
if callee != nil {
toAddr = hex.EncodeToString(callee)
}
if value == nil {
value = big.NewInt(0)
}
valueU256 := math.HexOrDecimal256(*value)
if gasPrice == nil {
gasPrice = big.NewInt(0)
}
gasPriceU256 := math.HexOrDecimal256(*gasPrice)
return apitypes.TypedData{
Types: map[string][]apitypes.Type{
"EIP712Domain": {
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
},
"Call": {
{Name: "from", Type: "address"},
{Name: "to", Type: "address"},
{Name: "gasLimit", Type: "uint64"},
{Name: "gasPrice", Type: "uint256"},
{Name: "value", Type: "uint256"},
{Name: "data", Type: "bytes"},
{Name: "leash", Type: "Leash"},
},
"Leash": {
{Name: "nonce", Type: "uint64"},
{Name: "blockNumber", Type: "uint64"},
{Name: "blockHash", Type: "bytes32"},
{Name: "blockRange", Type: "uint64"},
},
},
PrimaryType: "Call",
Domain: apitypes.TypedDataDomain{
Name: "oasis-runtime-sdk/evm: signed query",
Version: "1.0.0",
ChainId: math.NewHexOrDecimal256(int64(chainID)),
VerifyingContract: "",
Salt: "",
},
Message: map[string]interface{}{
"from": hex.EncodeToString(caller),
"to": toAddr,
"value": &valueU256,
"gasLimit": math.NewHexOrDecimal256(int64(gasLimit)),
"gasPrice": &gasPriceU256,
"data": data,
"leash": map[string]interface{}{
"nonce": math.NewHexOrDecimal256(int64(leash.Nonce)),
"blockNumber": math.NewHexOrDecimal256(int64(leash.BlockNumber)),
"blockHash": leash.BlockHash,
"blockRange": math.NewHexOrDecimal256(int64(leash.BlockRange)),
},
},
}
}
// signTypedData is based on go-ethereum/core/signer but modified to use an in-memory signer.
func signTypedData(sign SignerFn, privateKey []byte, typedData apitypes.TypedData) ([]byte, error) {
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
if err != nil {
return nil, fmt.Errorf("failed to hash EIP721Domain: %w", err)
}
typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
if err != nil {
return nil, fmt.Errorf("failed to hash typed data: %w", err)
}
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
var digest [32]byte
copy(digest[:], crypto.Keccak256Hash(rawData).Bytes())
signature, err := sign(digest, privateKey)
if err != nil {
return nil, fmt.Errorf("failed to sign typed data: %w", err)
}
signature[64] = 28 // Eth wallets use a high recovery ID.
return signature, nil
}