Skip to content

Commit d4312cc

Browse files
committed
fix: review feedback
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 33c185c commit d4312cc

2 files changed

Lines changed: 44 additions & 10 deletions

File tree

ledger/common/certs.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
769792
func (c PoolRegistrationCertificate) MarshalCBOR() ([]byte, error) {
770793
if cborData := c.Cbor(); cborData != nil {
771794
return cborData, nil

ledger/common/certs_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ func TestLeiosKeyRejectsInvalidLengths(t *testing.T) {
299299
var decoded LeiosKey
300300
_, err = cbor.Decode(encoded, &decoded)
301301
require.Error(t, err)
302+
303+
encoded, err = json.Marshal(struct {
304+
PublicKey []byte `json:"publicKey"`
305+
PossessionProof []byte `json:"possessionProof"`
306+
}{
307+
PublicKey: test.key,
308+
PossessionProof: test.proof,
309+
})
310+
require.NoError(t, err)
311+
err = json.Unmarshal(encoded, &decoded)
312+
require.Error(t, err)
302313
})
303314
}
304315
}

0 commit comments

Comments
 (0)