Browse Source

Wrap constructor to create xxxS structs and avoid recursion

pull/1782/head
Ethan Frey 8 years ago
parent
commit
49569ac244
4 changed files with 55 additions and 0 deletions
  1. +7
    -0
      priv_key.go
  2. +7
    -0
      pub_key.go
  3. +7
    -0
      signature.go
  4. +34
    -0
      signature_test.go

+ 7
- 0
priv_key.go View File

@ -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)
}


+ 7
- 0
pub_key.go View File

@ -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)
}


+ 7
- 0
signature.go View File

@ -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)
}


+ 34
- 0
signature_test.go View File

@ -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)
}
}

Loading…
Cancel
Save