Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign CCRs as EIP-712 messages #247

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/types/confidential.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type ConfidentialComputeRecord struct {
KettleAddress common.Address
ConfidentialInputsHash common.Hash

// Envelope signals whether this CCR was signed using EIP-712
IsEIP712 bool

ChainID *big.Int
V, R, S *big.Int
}
Expand All @@ -30,6 +33,7 @@ func (tx *ConfidentialComputeRecord) copy() TxData {
Gas: tx.Gas,
KettleAddress: tx.KettleAddress,
ConfidentialInputsHash: tx.ConfidentialInputsHash,
IsEIP712: tx.IsEIP712,

Value: new(big.Int),
GasPrice: new(big.Int),
Expand Down
51 changes: 51 additions & 0 deletions core/types/suave_eip712.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package types

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/signer/core/eip712"
)

func (msg *ConfidentialComputeRecord) EIP712Hash() (common.Hash, error) {
hash, _, err := eip712.TypedDataAndHash(CCREIP712Envelope(msg))

hash32 := common.Hash{}
copy(hash32[:], hash[:])

return hash32, err
}

func CCREIP712Envelope(msg *ConfidentialComputeRecord) eip712.TypedData {
return eip712.TypedData{
Types: eip712.Types{
"EIP712Domain": []eip712.Type{
{Name: "name", Type: "string"},
{Name: "verifyingContract", Type: "address"},
},
"ConfidentialRecord": []eip712.Type{
{Name: "nonce", Type: "uint64"},
{Name: "gasPrice", Type: "uint256"},
{Name: "gas", Type: "uint64"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this uint64 be a uint256?

{Name: "to", Type: "address"},
{Name: "value", Type: "uint256"},
{Name: "data", Type: "bytes"},
{Name: "kettleAddress", Type: "address"},
{Name: "confidentialInputsHash", Type: "bytes32"},
},
},
Domain: eip712.TypedDataDomain{
Name: "ConfidentialRecord",
VerifyingContract: msg.KettleAddress.Hex(),
},
PrimaryType: "ConfidentialRecord",
Message: eip712.TypedDataMessage{
"nonce": msg.Nonce,
"gasPrice": msg.GasPrice,
"gas": msg.Gas,
"to": msg.To,
"value": msg.Value,
"data": msg.Data,
"kettleAddress": msg.KettleAddress,
"confidentialInputsHash": msg.ConfidentialInputsHash,
},
}
}
23 changes: 23 additions & 0 deletions core/types/suave_eip712_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package types

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestCCREIP712(t *testing.T) {
to := common.Address{0x2}

ccr := &ConfidentialComputeRecord{
GasPrice: big.NewInt(0),
To: &to,
Value: big.NewInt(0),
ChainID: big.NewInt(0),
}

_, err := ccr.EIP712Hash()
require.NoError(t, err)
}
5 changes: 5 additions & 0 deletions core/types/transaction_marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type txJSON struct {
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
KettleAddress *common.Address `json:"kettleAddress,omitempty"`
ConfidentialInputsHash *common.Hash `json:"confidentialInputsHash,omitempty"`
IsEIP712 *bool `json:"iseip712,omitempty"`
ConfidentialInputs *hexutil.Bytes `json:"confidentialInputs,omitempty"`
RequestRecord *json.RawMessage `json:"requestRecord,omitempty"`
ConfidentialComputeResult *hexutil.Bytes `json:"confidentialComputeResult,omitempty"`
Expand Down Expand Up @@ -146,6 +147,7 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
enc.V = (*hexutil.Big)(itx.V)
enc.R = (*hexutil.Big)(itx.R)
enc.S = (*hexutil.Big)(itx.S)
enc.IsEIP712 = &itx.IsEIP712

case *SuaveTransaction:
requestRecord, err := NewTx(&itx.ConfidentialComputeRequest).MarshalJSON()
Expand Down Expand Up @@ -498,6 +500,9 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
return errors.New("missing required field 'chainId' in transaction")
}
itx.ChainID = (*big.Int)(dec.ChainID)
if dec.IsEIP712 != nil {
itx.IsEIP712 = *dec.IsEIP712
}
if dec.V == nil {
return errors.New("missing required field 'r' in transaction")
}
Expand Down
24 changes: 24 additions & 0 deletions core/types/transaction_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)

Expand Down Expand Up @@ -358,6 +359,15 @@ func (s suaveSigner) Hash(tx *Transaction) common.Hash {
txdata.ConfidentialComputeResult,
})
case *ConfidentialComputeRequest:
if txdata.IsEIP712 {
// EIP-712 signature used during signing
hash, err := txdata.ConfidentialComputeRecord.EIP712Hash()
if err != nil {
log.Error("failed to calculate EIP712 hash", "err", err)
}
return hash
}

return prefixedRlpHash(
ConfidentialComputeRecordTxType, // Note: this is the same as the Record so that hashes match!
[]interface{}{
Expand All @@ -371,6 +381,16 @@ func (s suaveSigner) Hash(tx *Transaction) common.Hash {
tx.Data(),
})
case *ConfidentialComputeRecord:
if txdata.IsEIP712 {
// EIP-712 signature using during recovery
hash, err := txdata.EIP712Hash()
if err != nil {
log.Error("failed to calculate EIP712 hash", "err", err)
}
return hash
}

// normal txn signature
return prefixedRlpHash(
tx.Type(),
[]interface{}{
Expand Down Expand Up @@ -689,6 +709,10 @@ func (fs FrontierSigner) Hash(tx *Transaction) common.Hash {
})
}

func DecodeSignature(sig []byte) (r, s, v *big.Int) {
return decodeSignature(sig)
}

func decodeSignature(sig []byte) (r, s, v *big.Int) {
if len(sig) != crypto.SignatureLength {
panic(fmt.Sprintf("wrong size for signature: got %d, want %d", len(sig), crypto.SignatureLength))
Expand Down
Loading
Loading