Skip to content

Commit 1a3504e

Browse files
holimanatif-konasl
authored andcommitted
signer/core: move API JSON types to separate package (ethereum#23275)
This PR moves (some) account types into a standalone package, to avoid depending on signer/core from accounts/external.
1 parent e64ac58 commit 1a3504e

File tree

12 files changed

+52
-46
lines changed

12 files changed

+52
-46
lines changed

accounts/external/backend.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/event"
3030
"github.com/ethereum/go-ethereum/log"
3131
"github.com/ethereum/go-ethereum/rpc"
32-
"github.com/ethereum/go-ethereum/signer/core"
32+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
3333
)
3434

3535
type ExternalBackend struct {
@@ -203,7 +203,7 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
203203
t := common.NewMixedcaseAddress(*tx.To())
204204
to = &t
205205
}
206-
args := &core.SendTxArgs{
206+
args := &apitypes.SendTxArgs{
207207
Data: &data,
208208
Nonce: hexutil.Uint64(tx.Nonce()),
209209
Value: hexutil.Big(*tx.Value()),

cmd/abidump/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"os"
2424
"strings"
2525

26-
"github.com/ethereum/go-ethereum/signer/core"
26+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
2727
"github.com/ethereum/go-ethereum/signer/fourbyte"
2828
)
2929

@@ -41,7 +41,7 @@ func parse(data []byte) {
4141
if err != nil {
4242
die(err)
4343
}
44-
messages := core.ValidationMessages{}
44+
messages := apitypes.ValidationMessages{}
4545
db.ValidateCallData(nil, data, &messages)
4646
for _, m := range messages.Messages {
4747
fmt.Printf("%v: %v\n", m.Typ, m.Message)

cmd/clef/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ import (
5050
"github.com/ethereum/go-ethereum/rlp"
5151
"github.com/ethereum/go-ethereum/rpc"
5252
"github.com/ethereum/go-ethereum/signer/core"
53+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
5354
"github.com/ethereum/go-ethereum/signer/fourbyte"
5455
"github.com/ethereum/go-ethereum/signer/rules"
5556
"github.com/ethereum/go-ethereum/signer/storage"
56-
5757
"github.com/mattn/go-colorable"
5858
"github.com/mattn/go-isatty"
5959
"gopkg.in/urfave/cli.v1"
@@ -923,7 +923,7 @@ func testExternalUI(api *core.SignerAPI) {
923923
time.Sleep(delay)
924924
data := hexutil.Bytes([]byte{})
925925
to := common.NewMixedcaseAddress(a)
926-
tx := core.SendTxArgs{
926+
tx := apitypes.SendTxArgs{
927927
Data: &data,
928928
Nonce: 0x1,
929929
Value: hexutil.Big(*big.NewInt(6)),
@@ -1055,11 +1055,11 @@ func GenDoc(ctx *cli.Context) {
10551055
data := hexutil.Bytes([]byte{0x01, 0x02, 0x03, 0x04})
10561056
add("SignTxRequest", desc, &core.SignTxRequest{
10571057
Meta: meta,
1058-
Callinfo: []core.ValidationInfo{
1058+
Callinfo: []apitypes.ValidationInfo{
10591059
{Typ: "Warning", Message: "Something looks odd, show this message as a warning"},
10601060
{Typ: "Info", Message: "User should see this as well"},
10611061
},
1062-
Transaction: core.SendTxArgs{
1062+
Transaction: apitypes.SendTxArgs{
10631063
Data: &data,
10641064
Nonce: 0x1,
10651065
Value: hexutil.Big(*big.NewInt(6)),
@@ -1075,7 +1075,7 @@ func GenDoc(ctx *cli.Context) {
10751075
add("SignTxResponse - approve", "Response to request to sign a transaction. This response needs to contain the `transaction`"+
10761076
", because the UI is free to make modifications to the transaction.",
10771077
&core.SignTxResponse{Approved: true,
1078-
Transaction: core.SendTxArgs{
1078+
Transaction: apitypes.SendTxArgs{
10791079
Data: &data,
10801080
Nonce: 0x4,
10811081
Value: hexutil.Big(*big.NewInt(6)),

signer/core/api.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/common/hexutil"
3434
"github.com/ethereum/go-ethereum/internal/ethapi"
3535
"github.com/ethereum/go-ethereum/log"
36+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
3637
"github.com/ethereum/go-ethereum/signer/storage"
3738
)
3839

@@ -52,7 +53,7 @@ type ExternalAPI interface {
5253
// New request to create a new account
5354
New(ctx context.Context) (common.Address, error)
5455
// SignTransaction request to sign the specified transaction
55-
SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error)
56+
SignTransaction(ctx context.Context, args apitypes.SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error)
5657
// SignData - request to sign the given data (plus prefix)
5758
SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error)
5859
// SignTypedData - request to sign the given structured data (plus prefix)
@@ -104,7 +105,7 @@ type Validator interface {
104105
// ValidateTransaction does a number of checks on the supplied transaction, and
105106
// returns either a list of warnings, or an error (indicating that the transaction
106107
// should be immediately rejected).
107-
ValidateTransaction(selector *string, tx *SendTxArgs) (*ValidationMessages, error)
108+
ValidateTransaction(selector *string, tx *apitypes.SendTxArgs) (*apitypes.ValidationMessages, error)
108109
}
109110

110111
// SignerAPI defines the actual implementation of ExternalAPI
@@ -220,24 +221,24 @@ func (m Metadata) String() string {
220221
type (
221222
// SignTxRequest contains info about a Transaction to sign
222223
SignTxRequest struct {
223-
Transaction SendTxArgs `json:"transaction"`
224-
Callinfo []ValidationInfo `json:"call_info"`
225-
Meta Metadata `json:"meta"`
224+
Transaction apitypes.SendTxArgs `json:"transaction"`
225+
Callinfo []apitypes.ValidationInfo `json:"call_info"`
226+
Meta Metadata `json:"meta"`
226227
}
227228
// SignTxResponse result from SignTxRequest
228229
SignTxResponse struct {
229230
//The UI may make changes to the TX
230-
Transaction SendTxArgs `json:"transaction"`
231-
Approved bool `json:"approved"`
231+
Transaction apitypes.SendTxArgs `json:"transaction"`
232+
Approved bool `json:"approved"`
232233
}
233234
SignDataRequest struct {
234-
ContentType string `json:"content_type"`
235-
Address common.MixedcaseAddress `json:"address"`
236-
Rawdata []byte `json:"raw_data"`
237-
Messages []*NameValueType `json:"messages"`
238-
Callinfo []ValidationInfo `json:"call_info"`
239-
Hash hexutil.Bytes `json:"hash"`
240-
Meta Metadata `json:"meta"`
235+
ContentType string `json:"content_type"`
236+
Address common.MixedcaseAddress `json:"address"`
237+
Rawdata []byte `json:"raw_data"`
238+
Messages []*NameValueType `json:"messages"`
239+
Callinfo []apitypes.ValidationInfo `json:"call_info"`
240+
Hash hexutil.Bytes `json:"hash"`
241+
Meta Metadata `json:"meta"`
241242
}
242243
SignDataResponse struct {
243244
Approved bool `json:"approved"`
@@ -537,7 +538,7 @@ func (api *SignerAPI) lookupOrQueryPassword(address common.Address, title, promp
537538
}
538539

539540
// SignTransaction signs the given Transaction and returns it both as json and rlp-encoded form
540-
func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
541+
func (api *SignerAPI) SignTransaction(ctx context.Context, args apitypes.SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
541542
var (
542543
err error
543544
result SignTxResponse
@@ -548,7 +549,7 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, meth
548549
}
549550
// If we are in 'rejectMode', then reject rather than show the user warnings
550551
if api.rejectMode {
551-
if err := msgs.getWarnings(); err != nil {
552+
if err := msgs.GetWarnings(); err != nil {
552553
return nil, err
553554
}
554555
}
@@ -585,7 +586,7 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, meth
585586
return nil, err
586587
}
587588
// Convert fields into a real transaction
588-
var unsignedTx = result.Transaction.toTransaction()
589+
var unsignedTx = result.Transaction.ToTransaction()
589590
// Get the password for the transaction
590591
pw, err := api.lookupOrQueryPassword(acc.Address, "Account password",
591592
fmt.Sprintf("Please enter the password for account %s", acc.Address.String()))
@@ -621,7 +622,7 @@ func (api *SignerAPI) SignGnosisSafeTx(ctx context.Context, signerAddress common
621622
}
622623
// If we are in 'rejectMode', then reject rather than show the user warnings
623624
if api.rejectMode {
624-
if err := msgs.getWarnings(); err != nil {
625+
if err := msgs.GetWarnings(); err != nil {
625626
return nil, err
626627
}
627628
}

signer/core/api_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/ethereum/go-ethereum/internal/ethapi"
3636
"github.com/ethereum/go-ethereum/rlp"
3737
"github.com/ethereum/go-ethereum/signer/core"
38+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
3839
"github.com/ethereum/go-ethereum/signer/fourbyte"
3940
"github.com/ethereum/go-ethereum/signer/storage"
4041
)
@@ -223,14 +224,14 @@ func TestNewAcc(t *testing.T) {
223224
}
224225
}
225226

226-
func mkTestTx(from common.MixedcaseAddress) core.SendTxArgs {
227+
func mkTestTx(from common.MixedcaseAddress) apitypes.SendTxArgs {
227228
to := common.NewMixedcaseAddress(common.HexToAddress("0x1337"))
228229
gas := hexutil.Uint64(21000)
229230
gasPrice := (hexutil.Big)(*big.NewInt(2000000000))
230231
value := (hexutil.Big)(*big.NewInt(1e18))
231232
nonce := (hexutil.Uint64)(0)
232233
data := hexutil.Bytes(common.Hex2Bytes("01020304050607080a"))
233-
tx := core.SendTxArgs{
234+
tx := apitypes.SendTxArgs{
234235
From: from,
235236
To: &to,
236237
Gas: gas,

signer/core/types.go signer/core/apitypes/types.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package core
17+
package apitypes
1818

1919
import (
2020
"encoding/json"
@@ -52,7 +52,7 @@ func (vs *ValidationMessages) Info(msg string) {
5252
}
5353

5454
/// getWarnings returns an error with all messages of type WARN of above, or nil if no warnings were present
55-
func (v *ValidationMessages) getWarnings() error {
55+
func (v *ValidationMessages) GetWarnings() error {
5656
var messages []string
5757
for _, msg := range v.Messages {
5858
if msg.Typ == WARN || msg.Typ == CRIT {
@@ -97,7 +97,7 @@ func (args SendTxArgs) String() string {
9797
return err.Error()
9898
}
9999

100-
func (args *SendTxArgs) toTransaction() *types.Transaction {
100+
func (args *SendTxArgs) ToTransaction() *types.Transaction {
101101
txArgs := ethapi.TransactionArgs{
102102
Gas: &args.Gas,
103103
GasPrice: args.GasPrice,

signer/core/auditlog.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/common/hexutil"
2525
"github.com/ethereum/go-ethereum/internal/ethapi"
2626
"github.com/ethereum/go-ethereum/log"
27+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
2728
)
2829

2930
type AuditLogger struct {
@@ -43,7 +44,7 @@ func (l *AuditLogger) New(ctx context.Context) (common.Address, error) {
4344
return l.api.New(ctx)
4445
}
4546

46-
func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
47+
func (l *AuditLogger) SignTransaction(ctx context.Context, args apitypes.SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
4748
sel := "<nil>"
4849
if methodSelector != nil {
4950
sel = *methodSelector

signer/core/gnosis_safe.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/ethereum/go-ethereum/common"
88
"github.com/ethereum/go-ethereum/common/hexutil"
99
"github.com/ethereum/go-ethereum/common/math"
10+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
1011
)
1112

1213
// GnosisSafeTx is a type to parse the safe-tx returned by the relayer,
@@ -76,9 +77,9 @@ func (tx *GnosisSafeTx) ToTypedData() TypedData {
7677

7778
// ArgsForValidation returns a SendTxArgs struct, which can be used for the
7879
// common validations, e.g. look up 4byte destinations
79-
func (tx *GnosisSafeTx) ArgsForValidation() *SendTxArgs {
80+
func (tx *GnosisSafeTx) ArgsForValidation() *apitypes.SendTxArgs {
8081
gp := hexutil.Big(tx.GasPrice)
81-
args := &SendTxArgs{
82+
args := &apitypes.SendTxArgs{
8283
From: tx.Safe,
8384
To: &tx.To,
8485
Gas: hexutil.Uint64(tx.SafeTxGas.Uint64()),

signer/core/signed_data.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/ethereum/go-ethereum/core/types"
3939
"github.com/ethereum/go-ethereum/crypto"
4040
"github.com/ethereum/go-ethereum/rlp"
41+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
4142
)
4243

4344
type SigFormat struct {
@@ -323,7 +324,7 @@ func (api *SignerAPI) SignTypedData(ctx context.Context, addr common.MixedcaseAd
323324
// signTypedData is identical to the capitalized version, except that it also returns the hash (preimage)
324325
// - the signature preimage (hash)
325326
func (api *SignerAPI) signTypedData(ctx context.Context, addr common.MixedcaseAddress,
326-
typedData TypedData, validationMessages *ValidationMessages) (hexutil.Bytes, hexutil.Bytes, error) {
327+
typedData TypedData, validationMessages *apitypes.ValidationMessages) (hexutil.Bytes, hexutil.Bytes, error) {
327328
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
328329
if err != nil {
329330
return nil, nil, err

signer/fourbyte/validation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
"math/big"
2424

2525
"github.com/ethereum/go-ethereum/common"
26-
"github.com/ethereum/go-ethereum/signer/core"
26+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
2727
)
2828

2929
// ValidateTransaction does a number of checks on the supplied transaction, and
3030
// returns either a list of warnings, or an error (indicating that the transaction
3131
// should be immediately rejected).
32-
func (db *Database) ValidateTransaction(selector *string, tx *core.SendTxArgs) (*core.ValidationMessages, error) {
33-
messages := new(core.ValidationMessages)
32+
func (db *Database) ValidateTransaction(selector *string, tx *apitypes.SendTxArgs) (*apitypes.ValidationMessages, error) {
33+
messages := new(apitypes.ValidationMessages)
3434

3535
// Prevent accidental erroneous usage of both 'input' and 'data' (show stopper)
3636
if tx.Data != nil && tx.Input != nil && !bytes.Equal(*tx.Data, *tx.Input) {
@@ -90,7 +90,7 @@ func (db *Database) ValidateTransaction(selector *string, tx *core.SendTxArgs) (
9090

9191
// ValidateCallData checks if the ABI call-data + method selector (if given) can
9292
// be parsed and seems to match.
93-
func (db *Database) ValidateCallData(selector *string, data []byte, messages *core.ValidationMessages) {
93+
func (db *Database) ValidateCallData(selector *string, data []byte, messages *apitypes.ValidationMessages) {
9494
// If the data is empty, we have a plain value transfer, nothing more to do
9595
if len(data) == 0 {
9696
return

signer/fourbyte/validation_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/common/hexutil"
25-
"github.com/ethereum/go-ethereum/signer/core"
25+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
2626
)
2727

2828
func mixAddr(a string) (*common.MixedcaseAddress, error) {
@@ -36,7 +36,7 @@ func toHexUint(h string) hexutil.Uint64 {
3636
b := big.NewInt(0).SetBytes(common.FromHex(h))
3737
return hexutil.Uint64(b.Uint64())
3838
}
39-
func dummyTxArgs(t txtestcase) *core.SendTxArgs {
39+
func dummyTxArgs(t txtestcase) *apitypes.SendTxArgs {
4040
to, _ := mixAddr(t.to)
4141
from, _ := mixAddr(t.from)
4242
n := toHexUint(t.n)
@@ -55,7 +55,7 @@ func dummyTxArgs(t txtestcase) *core.SendTxArgs {
5555
input = &a
5656

5757
}
58-
return &core.SendTxArgs{
58+
return &apitypes.SendTxArgs{
5959
From: *from,
6060
To: to,
6161
Value: value,

signer/rules/rules_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/internal/ethapi"
3030
"github.com/ethereum/go-ethereum/signer/core"
31+
"github.com/ethereum/go-ethereum/signer/core/apitypes"
3132
"github.com/ethereum/go-ethereum/signer/storage"
3233
)
3334

@@ -180,7 +181,7 @@ func TestSignTxRequest(t *testing.T) {
180181
}
181182
t.Logf("to %v", to.Address().String())
182183
resp, err := r.ApproveTx(&core.SignTxRequest{
183-
Transaction: core.SendTxArgs{
184+
Transaction: apitypes.SendTxArgs{
184185
From: *from,
185186
To: to},
186187
Callinfo: nil,
@@ -432,15 +433,15 @@ func dummyTx(value hexutil.Big) *core.SignTxRequest {
432433
gasPrice := hexutil.Big(*big.NewInt(2000000))
433434

434435
return &core.SignTxRequest{
435-
Transaction: core.SendTxArgs{
436+
Transaction: apitypes.SendTxArgs{
436437
From: *from,
437438
To: to,
438439
Value: value,
439440
Nonce: n,
440441
GasPrice: &gasPrice,
441442
Gas: gas,
442443
},
443-
Callinfo: []core.ValidationInfo{
444+
Callinfo: []apitypes.ValidationInfo{
444445
{Typ: "Warning", Message: "All your base are bellong to us"},
445446
},
446447
Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},

0 commit comments

Comments
 (0)