31
31
32
32
BaseAccountSig = []byte ("bacc" )
33
33
ModuleAccountSig = []byte ("macc" )
34
+
35
+ PubKeyTypeSecp256k1 = byte (1 )
36
+ PubKeyTypeEd25519 = byte (2 )
37
+ PubKeyTypeMultisig = byte (3 )
34
38
)
35
39
36
40
// NewBaseAccount creates a new BaseAccount object
@@ -407,10 +411,14 @@ type GenesisAccount interface {
407
411
408
412
// custom json marshaler for BaseAccount & ModuleAccount
409
413
414
+ type PubKeyJSON struct {
415
+ Type byte `json:"type"`
416
+ Key []byte `json:"key"`
417
+ }
410
418
type BaseAccountJSON struct {
411
- Address string `json:"address"`
412
- PubKey json. RawMessage `json:"pub_key"`
413
- Sequence string `json:"sequence"`
419
+ Address string `json:"address"`
420
+ PubKey PubKeyJSON `json:"pub_key"`
421
+ Sequence string `json:"sequence"`
414
422
}
415
423
416
424
func (acc BaseAccount ) MarshalJSONPB (m * jsonpb.Marshaler ) ([]byte , error ) {
@@ -420,19 +428,22 @@ func (acc BaseAccount) MarshalJSONPB(m *jsonpb.Marshaler) ([]byte, error) {
420
428
bi .Sequence = strconv .FormatUint (acc .Sequence , 10 )
421
429
var bz []byte
422
430
var err error
423
- if acc .Ed25519PubKey != nil {
424
- bz , err = codec .ProtoMarshalJSON (acc .Ed25519PubKey , m .AnyResolver )
425
- }
426
431
if acc .Secp256K1PubKey != nil {
427
- bz , err = codec .ProtoMarshalJSON (acc .Secp256K1PubKey , m .AnyResolver )
432
+ bi .PubKey .Type = PubKeyTypeSecp256k1
433
+ bz , err = acc .Secp256K1PubKey .Marshal ()
434
+ }
435
+ if acc .Ed25519PubKey != nil {
436
+ bi .PubKey .Type = PubKeyTypeEd25519
437
+ bz , err = acc .Ed25519PubKey .Marshal ()
428
438
}
429
439
if acc .MultisigPubKey != nil {
430
- bz , err = codec .ProtoMarshalJSON (acc .MultisigPubKey , m .AnyResolver )
440
+ bi .PubKey .Type = PubKeyTypeMultisig
441
+ bz , err = acc .MultisigPubKey .Marshal ()
431
442
}
432
443
if err != nil {
433
444
return nil , err
434
445
}
435
- bi .PubKey = bz
446
+ bi .PubKey . Key = bz
436
447
return json .Marshal (bi )
437
448
}
438
449
@@ -443,42 +454,32 @@ func (acc *BaseAccount) UnmarshalJSONPB(m *jsonpb.Unmarshaler, bz []byte) error
443
454
if err != nil {
444
455
return err
445
456
}
446
- /* TODO: do we need to validate address format here
447
- err = sdk.ValidateAccAddress(bi.Address)
448
- if err != nil {
449
- return err
450
- }
451
- */
452
457
453
458
acc .Address = bi .Address
454
459
acc .Sequence , err = strconv .ParseUint (bi .Sequence , 10 , 64 )
455
460
if err != nil {
456
461
return err
457
462
}
458
463
459
- done := false
460
- if ! done {
464
+ switch bi . PubKey . Type {
465
+ case PubKeyTypeSecp256k1 :
461
466
pk := new (secp256k1.PubKey )
462
- any , _ := codectypes .NewAnyWithValue (pk )
463
- if m .Unmarshal (strings .NewReader (string (bi .PubKey )), any ) == nil {
464
- acc .SetPubKey (pk )
465
- done = true
467
+ if err := pk .Unmarshal (bi .PubKey .Key ); err != nil {
468
+ return err
466
469
}
467
- }
468
- if ! done {
469
- pk := new (ed25519.PubKey )
470
- any , _ := codectypes .NewAnyWithValue (pk )
471
- if m .Unmarshal (strings .NewReader (string (bi .PubKey )), any ) == nil {
472
- acc .SetPubKey (pk )
473
- done = true
470
+ acc .SetPubKey (pk )
471
+ case PubKeyTypeEd25519 :
472
+ pk := new (secp256k1.PubKey )
473
+ if err := pk .Unmarshal (bi .PubKey .Key ); err != nil {
474
+ return err
474
475
}
475
- }
476
- if ! done {
476
+ acc . SetPubKey ( pk )
477
+ case PubKeyTypeMultisig :
477
478
pk := new (multisig.LegacyAminoPubKey )
478
- any , _ := codectypes .NewAnyWithValue (pk )
479
- if m .Unmarshal (strings .NewReader (string (bi .PubKey )), any ) == nil {
480
- acc .SetPubKey (pk )
479
+ if err := pk .Unmarshal (bi .PubKey .Key ); err != nil {
480
+ return err
481
481
}
482
+ acc .SetPubKey (pk )
482
483
}
483
484
return nil
484
485
}
0 commit comments