Skip to content

Commit 60b780c

Browse files
committed
Merge pull request #1217 from tgerring/rpcsign
Fix RPC sign
2 parents 7614851 + 0464118 commit 60b780c

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

rpc/args.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,8 @@ type NewSigArgs struct {
172172
}
173173

174174
func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) {
175-
var obj []json.RawMessage
176-
var ext struct {
177-
From string
178-
Data string
179-
}
175+
var obj []interface{}
180176

181-
// Decode byte slice to array of RawMessages
182177
if err := json.Unmarshal(b, &obj); err != nil {
183178
return NewDecodeParamError(err.Error())
184179
}
@@ -188,21 +183,26 @@ func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) {
188183
return NewInsufficientParamsError(len(obj), 1)
189184
}
190185

191-
// Decode 0th RawMessage to temporary struct
192-
if err := json.Unmarshal(obj[0], &ext); err != nil {
193-
return NewDecodeParamError(err.Error())
186+
from, ok := obj[0].(string)
187+
if !ok {
188+
return NewInvalidTypeError("from", "not a string")
194189
}
190+
args.From = from
195191

196-
if len(ext.From) == 0 {
192+
if len(args.From) == 0 {
197193
return NewValidationError("from", "is required")
198194
}
199195

200-
if len(ext.Data) == 0 {
196+
data, ok := obj[1].(string)
197+
if !ok {
198+
return NewInvalidTypeError("data", "not a string")
199+
}
200+
args.Data = data
201+
202+
if len(args.Data) == 0 {
201203
return NewValidationError("data", "is required")
202204
}
203205

204-
args.From = ext.From
205-
args.Data = ext.Data
206206
return nil
207207
}
208208

rpc/args_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,3 +2508,64 @@ func TestSourceArgsEmpty(t *testing.T) {
25082508
t.Error(str)
25092509
}
25102510
}
2511+
2512+
func TestSigArgs(t *testing.T) {
2513+
input := `["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "0x0"]`
2514+
expected := new(NewSigArgs)
2515+
expected.From = "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"
2516+
expected.Data = "0x0"
2517+
2518+
args := new(NewSigArgs)
2519+
if err := json.Unmarshal([]byte(input), &args); err != nil {
2520+
t.Error(err)
2521+
}
2522+
}
2523+
2524+
func TestSigArgsEmptyData(t *testing.T) {
2525+
input := `["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", ""]`
2526+
2527+
args := new(NewSigArgs)
2528+
str := ExpectValidationError(json.Unmarshal([]byte(input), args))
2529+
if len(str) > 0 {
2530+
t.Error(str)
2531+
}
2532+
}
2533+
2534+
func TestSigArgsDataType(t *testing.T) {
2535+
input := `["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", 13]`
2536+
2537+
args := new(NewSigArgs)
2538+
str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
2539+
if len(str) > 0 {
2540+
t.Error(str)
2541+
}
2542+
}
2543+
2544+
func TestSigArgsEmptyFrom(t *testing.T) {
2545+
input := `["", "0x0"]`
2546+
2547+
args := new(NewSigArgs)
2548+
str := ExpectValidationError(json.Unmarshal([]byte(input), args))
2549+
if len(str) > 0 {
2550+
t.Error(str)
2551+
}
2552+
}
2553+
2554+
func TestSigArgsFromType(t *testing.T) {
2555+
input := `[false, "0x0"]`
2556+
2557+
args := new(NewSigArgs)
2558+
str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
2559+
if len(str) > 0 {
2560+
t.Error(str)
2561+
}
2562+
}
2563+
2564+
func TestSigArgsEmpty(t *testing.T) {
2565+
input := `[]`
2566+
args := new(NewSigArgs)
2567+
str := ExpectInsufficientParamsError(json.Unmarshal([]byte(input), args))
2568+
if len(str) > 0 {
2569+
t.Error(str)
2570+
}
2571+
}

0 commit comments

Comments
 (0)