Skip to content

Commit 32c9688

Browse files
committed
fix(ledger/byron): removed the first byte checking part from ftsSeed UnmarshalJSON and attempted string, empty object & null
Signed-off-by: Akhil Repala <[email protected]>
1 parent aabfe4c commit 32c9688

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

ledger/byron/genesis.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -200,41 +200,36 @@ func NewByronGenesisFromFile(path string) (ByronGenesis, error) {
200200
}
201201

202202
// UnmarshalJSON accepts: "string", {}, or null
203+
// Tries each expected shape and accepts the first that parses cleanly
203204
func (f *ByronGenesisFtsSeed) UnmarshalJSON(b []byte) error {
204-
var first byte
205-
for _, c := range b {
206-
if c > ' ' {
207-
first = c
208-
break
209-
}
210-
}
211-
switch first {
212-
case '"':
213-
// Normal string case
214-
var s string
215-
if err := json.Unmarshal(b, &s); err != nil {
216-
return err
217-
}
205+
// Try string
206+
var s string
207+
if err := json.Unmarshal(b, &s); err == nil {
218208
f.Value = s
219209
f.IsObject = false
220210
return nil
221-
case '{':
222-
// empty object case
223-
var m map[string]any
224-
if err := json.Unmarshal(b, &m); err != nil {
225-
return err
226-
}
227-
if len(m) > 0 {
228-
return errors.New("ftsSeed: expected empty object or string")
211+
}
212+
213+
// Try empty object
214+
var m map[string]any
215+
if err := json.Unmarshal(b, &m); err == nil {
216+
if len(m) == 0 {
217+
f.Value = ""
218+
f.IsObject = true
219+
return nil
229220
}
221+
return errors.New("ftsSeed: non-empty object not supported")
222+
}
223+
224+
// Try null
225+
var v any
226+
if err := json.Unmarshal(b, &v); err == nil && v == nil {
230227
f.Value = ""
231-
f.IsObject = true
232-
return nil
233-
case 'n':
228+
f.IsObject = false
234229
return nil
235-
default:
236-
return errors.New("ftsSeed: expected string, empty object, or null")
237230
}
231+
232+
return errors.New("ftsSeed: expected string, empty object, or null")
238233
}
239234

240235
func (f ByronGenesisFtsSeed) MarshalJSON() ([]byte, error) {

0 commit comments

Comments
 (0)