@@ -413,26 +413,46 @@ type LeiosKey struct {
413413 PossessionProof []byte `json:"possessionProof"`
414414}
415415
416- func (k * LeiosKey ) UnmarshalCBOR (data []byte ) error {
417- type tmpLeiosKey LeiosKey
418- var tmp tmpLeiosKey
419- if _ , err := cbor .Decode (data , & tmp ); err != nil {
420- return err
421- }
422- if len (tmp .PublicKey ) != LeiosBlsPublicKeySize {
416+ func (k LeiosKey ) validate () error {
417+ if len (k .PublicKey ) != LeiosBlsPublicKeySize {
423418 return fmt .Errorf (
424419 "invalid Leios BLS public key length: expected %d, got %d" ,
425420 LeiosBlsPublicKeySize ,
426- len (tmp .PublicKey ),
421+ len (k .PublicKey ),
427422 )
428423 }
429- if len (tmp .PossessionProof ) != LeiosBlsPossessionProofSize {
424+ if len (k .PossessionProof ) != LeiosBlsPossessionProofSize {
430425 return fmt .Errorf (
431426 "invalid Leios BLS possession proof length: expected %d, got %d" ,
432427 LeiosBlsPossessionProofSize ,
433- len (tmp .PossessionProof ),
428+ len (k .PossessionProof ),
434429 )
435430 }
431+ return nil
432+ }
433+
434+ func (k * LeiosKey ) UnmarshalCBOR (data []byte ) error {
435+ type tmpLeiosKey LeiosKey
436+ var tmp tmpLeiosKey
437+ if _ , err := cbor .Decode (data , & tmp ); err != nil {
438+ return err
439+ }
440+ if err := LeiosKey (tmp ).validate (); err != nil {
441+ return err
442+ }
443+ * k = LeiosKey (tmp )
444+ return nil
445+ }
446+
447+ func (k * LeiosKey ) UnmarshalJSON (data []byte ) error {
448+ type tmpLeiosKey LeiosKey
449+ var tmp tmpLeiosKey
450+ if err := json .Unmarshal (data , & tmp ); err != nil {
451+ return err
452+ }
453+ if err := LeiosKey (tmp ).validate (); err != nil {
454+ return err
455+ }
436456 * k = LeiosKey (tmp )
437457 return nil
438458}
@@ -766,6 +786,9 @@ func (c *PoolRegistrationCertificate) UnmarshalCBOR(cborData []byte) error {
766786 return nil
767787}
768788
789+ // NOTE: UnmarshalCBOR caches the original CBOR bytes, and MarshalCBOR returns
790+ // those bytes for both 10- and 11-field variants. Call SetCbor(nil) before
791+ // marshaling mutated fields.
769792func (c PoolRegistrationCertificate ) MarshalCBOR () ([]byte , error ) {
770793 if cborData := c .Cbor (); cborData != nil {
771794 return cborData , nil
0 commit comments