diff options
author | Sonic <sonic@dexon.org> | 2018-10-20 19:12:59 +0800 |
---|---|---|
committer | Wei-Ning Huang <aitjcize@gmail.com> | 2018-10-20 19:12:59 +0800 |
commit | 6615245b8098daf39be250698e9ff90e0a72936b (patch) | |
tree | 71535eb7715141b98823de93e8403a1bd3eeaada | |
parent | dae117690c0bf58db3f86fe675cf827c90cce7e6 (diff) | |
download | dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.tar dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.tar.gz dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.tar.bz2 dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.tar.lz dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.tar.xz dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.tar.zst dexon-consensus-6615245b8098daf39be250698e9ff90e0a72936b.zip |
crypto: dkg: implement rlp.Encoder, rlp.Decoder for PrivateKey (#235)
-rw-r--r-- | core/crypto/dkg/dkg.go | 14 | ||||
-rw-r--r-- | core/crypto/dkg/dkg_test.go | 12 |
2 files changed, 26 insertions, 0 deletions
diff --git a/core/crypto/dkg/dkg.go b/core/crypto/dkg/dkg.go index 8d0e088..8ca511d 100644 --- a/core/crypto/dkg/dkg.go +++ b/core/crypto/dkg/dkg.go @@ -58,6 +58,20 @@ type PrivateKey struct { publicKey PublicKey } +// EncodeRLP implements rlp.Encoder +func (prv *PrivateKey) EncodeRLP(w io.Writer) error { + return rlp.Encode(w, prv.Bytes()) +} + +// DecodeRLP implements rlp.Decoder +func (prv *PrivateKey) DecodeRLP(s *rlp.Stream) error { + var b []byte + if err := s.Decode(&b); err != nil { + return err + } + return prv.SetBytes(b) +} + // MarshalJSON implements json.Marshaller. func (prv *PrivateKey) MarshalJSON() ([]byte, error) { return json.Marshal(&prv.privateKey) diff --git a/core/crypto/dkg/dkg_test.go b/core/crypto/dkg/dkg_test.go index fe7ec73..44e68d8 100644 --- a/core/crypto/dkg/dkg_test.go +++ b/core/crypto/dkg/dkg_test.go @@ -292,6 +292,18 @@ func (s *DKGTestSuite) TestSignature() { s.False(pubKey.VerifySignature(hash, sig)) } +func (s *DKGTestSuite) TestPrivateKeyRLPEncodeDecode() { + k := NewPrivateKey() + b, err := rlp.EncodeToBytes(k) + s.Require().NoError(err) + + var kk PrivateKey + err = rlp.DecodeBytes(b, &kk) + s.Require().NoError(err) + + s.Require().True(reflect.DeepEqual(*k, kk)) +} + func (s *DKGTestSuite) TestPublicKeySharesRLPEncodeDecode() { p := NewEmptyPublicKeyShares() for i, id := range s.genID(1) { |