Skip to content

Commit

Permalink
accounts, signer: move mimetypes back to accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Dec 3, 2021
1 parent 8b39206 commit b3c4166
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 28 deletions.
7 changes: 7 additions & 0 deletions accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ 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 == apitypes.MimetypeClique && (res[64] == 27 || res[64] == 28) {
if mimeType == accounts.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",
apitypes.MimetypeTextPlain,
accounts.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: 1 addition & 2 deletions accounts/usbwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/karalabe/usb"
)

Expand Down Expand Up @@ -529,7 +528,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 == apitypes.MimetypeTypedData && len(data) == 66 && data[0] == 0x19 && data[1] == 0x01) {
if !(mimeType == accounts.MimetypeTypedData && len(data) == 66 && data[0] == 0x19 && data[1] == 0x01) {
return w.signHash(account, crypto.Keccak256(data))
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/checkpoint-admin/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"gopkg.in/urfave/cli.v1"
)

Expand Down Expand Up @@ -217,7 +217,7 @@ func sign(ctx *cli.Context) error {
p["message"] = hexutil.Encode(append(buf, chash.Bytes()...))

fmt.Println("Sending signing request to Clef...")
if err := clef.Call(&signature, "account_signData", apitypes.MimetypeDataWithValidator, signer, p); err != nil {
if err := clef.Call(&signature, "account_signData", accounts.MimetypeDataWithValidator, signer, p); err != nil {
utils.Fatalf("Failed to sign checkpoint, err %v", err)
}
fmt.Printf("Signer => %s\n", signer)
Expand Down
10 changes: 5 additions & 5 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, apitypes.MimetypeClique, *addr, hexutil.Encode(cliqueRlp))
_, err = api.SignData(ctx, accounts.MimetypeClique, *addr, hexutil.Encode(cliqueRlp))
expectApprove("signdata - clique header", err)
}
{ // Sign data test - typed data
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, apitypes.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
_, err := api.SignData(ctx, accounts.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, apitypes.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world")))
_, err := api.SignData(ctx, accounts.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 := []*apitypes.NameValueType{{Name: "message", Value: msg, Typ: apitypes.MimetypeTextPlain}}
messages := []*apitypes.NameValueType{{Name: "message", Value: msg, Typ: accounts.MimetypeTextPlain}}

add("SignDataRequest", desc, &core.SignDataRequest{
Address: common.NewMixedcaseAddress(a),
Meta: meta,
ContentType: apitypes.MimetypeTextPlain,
ContentType: accounts.MimetypeTextPlain,
Rawdata: []byte(msg),
Messages: messages,
Hash: sighash})
Expand Down
3 changes: 1 addition & 2 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/ethereum/go-ethereum/trie"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
Expand Down Expand Up @@ -635,7 +634,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}, apitypes.MimetypeClique, CliqueRLP(header))
sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeClique, CliqueRLP(header))
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -1959,7 +1958,7 @@ func (api *PublicDebugAPI) TestSignCliqueBlock(ctx context.Context, address comm
return common.Address{}, err
}

signature, err := wallet.SignData(account, apitypes.MimetypeClique, encoded)
signature, err := wallet.SignData(account, accounts.MimetypeClique, encoded)
if err != nil {
return common.Address{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion signer/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ type (
ContentType string `json:"content_type"`
Address common.MixedcaseAddress `json:"address"`
Rawdata []byte `json:"raw_data"`
Messages []*apitypes.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
16 changes: 5 additions & 11 deletions signer/core/apitypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,14 @@ import (
"unicode"
"unicode/utf8"

"github.com/ethereum/go-ethereum/accounts"
"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/core/types"
"github.com/ethereum/go-ethereum/crypto"
)

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

var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Z](\w*)(\[\])?$`)

type ValidationInfo struct {
Expand Down Expand Up @@ -181,19 +175,19 @@ type SigFormat struct {

var (
IntendedValidator = SigFormat{
MimetypeDataWithValidator,
accounts.MimetypeDataWithValidator,
0x00,
}
DataTyped = SigFormat{
MimetypeTypedData,
accounts.MimetypeTypedData,
0x01,
}
ApplicationClique = SigFormat{
MimetypeClique,
accounts.MimetypeClique,
0x02,
}
TextPlain = SigFormat{
MimetypeTextPlain,
accounts.MimetypeTextPlain,
0x45,
}
)
Expand Down
2 changes: 1 addition & 1 deletion signer/core/signed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (api *SignerAPI) determineSignatureFormat(ctx context.Context, contentType
messages := []*apitypes.NameValueType{
{
Name: "message",
Typ: apitypes.MimetypeTextPlain,
Typ: accounts.MimetypeTextPlain,
Value: msg,
},
}
Expand Down

0 comments on commit b3c4166

Please sign in to comment.