mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-07 13:55:17 +00:00
Wrap constructor to create xxxS structs and avoid recursion
This commit is contained in:
@@ -41,6 +41,13 @@ type PrivKeyS struct {
|
||||
PrivKey
|
||||
}
|
||||
|
||||
func WrapPrivKey(pk PrivKey) PrivKeyS {
|
||||
for ppk, ok := pk.(PrivKeyS); ok; ppk, ok = pk.(PrivKeyS) {
|
||||
pk = ppk.PrivKey
|
||||
}
|
||||
return PrivKeyS{pk}
|
||||
}
|
||||
|
||||
func (p PrivKeyS) MarshalJSON() ([]byte, error) {
|
||||
return privKeyMapper.ToJSON(p.PrivKey)
|
||||
}
|
||||
|
||||
@@ -36,6 +36,13 @@ type PubKeyS struct {
|
||||
PubKey
|
||||
}
|
||||
|
||||
func WrapPubKey(pk PubKey) PubKeyS {
|
||||
for ppk, ok := pk.(PubKeyS); ok; ppk, ok = pk.(PubKeyS) {
|
||||
pk = ppk.PubKey
|
||||
}
|
||||
return PubKeyS{pk}
|
||||
}
|
||||
|
||||
func (p PubKeyS) MarshalJSON() ([]byte, error) {
|
||||
return pubKeyMapper.ToJSON(p.PubKey)
|
||||
}
|
||||
|
||||
@@ -31,6 +31,13 @@ type SignatureS struct {
|
||||
Signature
|
||||
}
|
||||
|
||||
func WrapSignature(sig Signature) SignatureS {
|
||||
for ssig, ok := sig.(SignatureS); ok; ssig, ok = sig.(SignatureS) {
|
||||
sig = ssig.Signature
|
||||
}
|
||||
return SignatureS{sig}
|
||||
}
|
||||
|
||||
func (p SignatureS) MarshalJSON() ([]byte, error) {
|
||||
return sigMapper.ToJSON(p.Signature)
|
||||
}
|
||||
|
||||
@@ -107,3 +107,37 @@ func TestSignatureEncodings(t *testing.T) {
|
||||
assert.True(t, strings.HasPrefix(text, tc.sigName))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapping(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// construct some basic constructs
|
||||
msg := CRandBytes(128)
|
||||
priv := GenPrivKeyEd25519()
|
||||
pub := priv.PubKey()
|
||||
sig := priv.Sign(msg)
|
||||
|
||||
// do some wrapping
|
||||
pubs := []PubKeyS{
|
||||
WrapPubKey(nil),
|
||||
WrapPubKey(pub),
|
||||
WrapPubKey(WrapPubKey(WrapPubKey(WrapPubKey(pub)))),
|
||||
WrapPubKey(PubKeyS{PubKeyS{PubKeyS{pub}}}),
|
||||
}
|
||||
for _, p := range pubs {
|
||||
_, ok := p.PubKey.(PubKeyS)
|
||||
assert.False(ok)
|
||||
}
|
||||
|
||||
sigs := []SignatureS{
|
||||
WrapSignature(nil),
|
||||
WrapSignature(sig),
|
||||
WrapSignature(WrapSignature(WrapSignature(WrapSignature(sig)))),
|
||||
WrapSignature(SignatureS{SignatureS{SignatureS{sig}}}),
|
||||
}
|
||||
for _, s := range sigs {
|
||||
_, ok := s.Signature.(SignatureS)
|
||||
assert.False(ok)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user