Skip to content

Commit

Permalink
signer, accounts: move eip-712 types to apitypes package. fixes ether…
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Dec 1, 2021
1 parent 721c572 commit 4349c8b
Show file tree
Hide file tree
Showing 14 changed files with 790 additions and 783 deletions.
7 changes: 0 additions & 7 deletions accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ type Account struct {
URL URL `json:"url"` // Optional resource locator within a backend
}

const (
MimetypeDataWithValidator = "data/validator"
MimetypeTypedData = "data/typed"
MimetypeClique = "application/x-clique-header"
MimetypeTextPlain = "text/plain"
)

// Wallet represents a software or hardware wallet that might contain one or more
// accounts (derived from the same seed).
type Wallet interface {
Expand Down
4 changes: 2 additions & 2 deletions accounts/external/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (api *ExternalSigner) SignData(account accounts.Account, mimeType string, d
return nil, err
}
// If V is on 27/28-form, convert to 0/1 for Clique
if mimeType == accounts.MimetypeClique && (res[64] == 27 || res[64] == 28) {
if mimeType == apitypes.MimetypeClique && (res[64] == 27 || res[64] == 28) {
res[64] -= 27 // Transform V from 27/28 to 0/1 for Clique use
}
return res, nil
Expand All @@ -177,7 +177,7 @@ func (api *ExternalSigner) SignText(account accounts.Account, text []byte) ([]by
var signature hexutil.Bytes
var signAddress = common.NewMixedcaseAddress(account.Address)
if err := api.client.Call(&signature, "account_signData",
accounts.MimetypeTextPlain,
apitypes.MimetypeTextPlain,
&signAddress, // Need to use the pointer here, because of how MarshalJSON is defined
hexutil.Encode(text)); err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion accounts/usbwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package usbwallet
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"io"
"math/big"
"sync"
Expand Down Expand Up @@ -528,7 +529,7 @@ func (w *wallet) signHash(account accounts.Account, hash []byte) ([]byte, error)
func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {

// Unless we are doing 712 signing, simply dispatch to signHash
if !(mimeType == accounts.MimetypeTypedData && len(data) == 66 && data[0] == 0x19 && data[1] == 0x01) {
if !(mimeType == apitypes.MimetypeTypedData && len(data) == 66 && data[0] == 0x19 && data[1] == 0x01) {
return w.signHash(account, crypto.Keccak256(data))
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ func testExternalUI(api *core.SignerAPI) {
utils.Fatalf("Should not error: %v", err)
}
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
_, err = api.SignData(ctx, accounts.MimetypeClique, *addr, hexutil.Encode(cliqueRlp))
_, err = api.SignData(ctx, apitypes.MimetypeClique, *addr, hexutil.Encode(cliqueRlp))
expectApprove("signdata - clique header", err)
}
{ // Sign data test - typed data
Expand All @@ -898,7 +898,7 @@ func testExternalUI(api *core.SignerAPI) {
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
data := `{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"test","type":"uint8"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":"1","verifyingContract":"0xCCCcccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","test":"3","wallet":"0xcD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB","test":"2"},"contents":"Hello, Bob!"}}`
//_, err := api.SignData(ctx, accounts.MimetypeTypedData, *addr, hexutil.Encode([]byte(data)))
var typedData core.TypedData
var typedData apitypes.TypedData
json.Unmarshal([]byte(data), &typedData)
_, err := api.SignTypedData(ctx, *addr, typedData)
expectApprove("sign 712 typed data", err)
Expand All @@ -907,14 +907,14 @@ func testExternalUI(api *core.SignerAPI) {
api.UI.ShowInfo("Please approve the next request for signing text")
time.Sleep(delay)
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
_, err := api.SignData(ctx, accounts.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
_, err := api.SignData(ctx, apitypes.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
expectApprove("signdata - text", err)
}
{ // Sign data test - plain text reject
api.UI.ShowInfo("Please deny the next request for signing text")
time.Sleep(delay)
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
_, err := api.SignData(ctx, accounts.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
_, err := api.SignData(ctx, apitypes.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
expectDeny("signdata - text", err)
}
{ // Sign transaction
Expand Down Expand Up @@ -1025,12 +1025,12 @@ func GenDoc(ctx *cli.Context) {
"of the work in canonicalizing and making sense of the data, and it's up to the UI to present" +
"the user with the contents of the `message`"
sighash, msg := accounts.TextAndHash([]byte("hello world"))
messages := []*core.NameValueType{{Name: "message", Value: msg, Typ: accounts.MimetypeTextPlain}}
messages := []*apitypes.NameValueType{{Name: "message", Value: msg, Typ: apitypes.MimetypeTextPlain}}

add("SignDataRequest", desc, &core.SignDataRequest{
Address: common.NewMixedcaseAddress(a),
Meta: meta,
ContentType: accounts.MimetypeTextPlain,
ContentType: apitypes.MimetypeTextPlain,
Rawdata: []byte(msg),
Messages: messages,
Hash: sighash})
Expand Down
3 changes: 2 additions & 1 deletion consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"io"
"math/big"
"math/rand"
Expand Down Expand Up @@ -634,7 +635,7 @@ func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, res
log.Trace("Out-of-turn signing requested", "wiggle", common.PrettyDuration(wiggle))
}
// Sign all the things!
sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeClique, CliqueRLP(header))
sighash, err := signFn(accounts.Account{Address: signer}, apitypes.MimetypeClique, CliqueRLP(header))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -1958,7 +1959,7 @@ func (api *PublicDebugAPI) TestSignCliqueBlock(ctx context.Context, address comm
return common.Address{}, err
}

signature, err := wallet.SignData(account, accounts.MimetypeClique, encoded)
signature, err := wallet.SignData(account, apitypes.MimetypeClique, encoded)
if err != nil {
return common.Address{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions signer/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type ExternalAPI interface {
// SignData - request to sign the given data (plus prefix)
SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error)
// SignTypedData - request to sign the given structured data (plus prefix)
SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error)
SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data apitypes.TypedData) (hexutil.Bytes, error)
// EcRecover - recover public key from given message and signature
EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error)
// Version info about the APIs
Expand Down Expand Up @@ -235,7 +235,7 @@ type (
ContentType string `json:"content_type"`
Address common.MixedcaseAddress `json:"address"`
Rawdata []byte `json:"raw_data"`
Messages []*NameValueType `json:"messages"`
Messages []*apitypes.NameValueType `json:"messages"`
Callinfo []apitypes.ValidationInfo `json:"call_info"`
Hash hexutil.Bytes `json:"hash"`
Meta Metadata `json:"meta"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package core
package apitypes

import (
"bytes"
Expand Down
Loading

0 comments on commit 4349c8b

Please sign in to comment.