Browse Source

Use new naming scheme PubKey{PubKeyInner}

pull/1782/head
Ethan Frey 8 years ago
parent
commit
66ecd7705f
7 changed files with 124 additions and 107 deletions
  1. +0
    -1
      embed_test.go
  2. +11
    -11
      encode_test.go
  3. +33
    -24
      priv_key.go
  4. +31
    -31
      pub_key.go
  5. +1
    -1
      pub_key_test.go
  6. +29
    -20
      signature.go
  7. +19
    -19
      signature_test.go

+ 0
- 1
embed_test.go View File

@ -103,7 +103,6 @@ func TestEncodeDemo(t *testing.T) {
for i, tc := range cases { for i, tc := range cases {
// make sure it is proper to start // make sure it is proper to start
require.Equal(tc.expected, tc.in.Greet()) require.Equal(tc.expected, tc.in.Greet())
fmt.Println(tc.expected)
// now, try to encode as binary // now, try to encode as binary
b, err := data.ToWire(tc.in) b, err := data.ToWire(tc.in)


+ 11
- 11
encode_test.go View File

@ -50,17 +50,17 @@ func checkJSON(t *testing.T, in interface{}, reader interface{}, typ string) {
func TestKeyEncodings(t *testing.T) { func TestKeyEncodings(t *testing.T) {
cases := []struct { cases := []struct {
privKey PrivKeyS
privKey PrivKey
keyType byte keyType byte
keyName string keyName string
}{ }{
{ {
privKey: PrivKeyS{GenPrivKeyEd25519()},
privKey: WrapPrivKey(GenPrivKeyEd25519()),
keyType: TypeEd25519, keyType: TypeEd25519,
keyName: NameEd25519, keyName: NameEd25519,
}, },
{ {
privKey: PrivKeyS{GenPrivKeySecp256k1()},
privKey: WrapPrivKey(GenPrivKeySecp256k1()),
keyType: TypeSecp256k1, keyType: TypeSecp256k1,
keyName: NameSecp256k1, keyName: NameSecp256k1,
}, },
@ -68,19 +68,19 @@ func TestKeyEncodings(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
// check (de/en)codings of private key // check (de/en)codings of private key
priv2 := PrivKeyS{}
priv2 := PrivKey{}
checkWire(t, tc.privKey, &priv2, tc.keyType) checkWire(t, tc.privKey, &priv2, tc.keyType)
assert.EqualValues(t, tc.privKey, priv2) assert.EqualValues(t, tc.privKey, priv2)
priv3 := PrivKeyS{}
priv3 := PrivKey{}
checkJSON(t, tc.privKey, &priv3, tc.keyName) checkJSON(t, tc.privKey, &priv3, tc.keyName)
assert.EqualValues(t, tc.privKey, priv3) assert.EqualValues(t, tc.privKey, priv3)
// check (de/en)codings of public key // check (de/en)codings of public key
pubKey := PubKeyS{tc.privKey.PubKey()}
pub2 := PubKeyS{}
pubKey := tc.privKey.PubKey()
pub2 := PubKey{}
checkWire(t, pubKey, &pub2, tc.keyType) checkWire(t, pubKey, &pub2, tc.keyType)
assert.EqualValues(t, pubKey, pub2) assert.EqualValues(t, pubKey, pub2)
pub3 := PubKeyS{}
pub3 := PubKey{}
checkJSON(t, pubKey, &pub3, tc.keyName) checkJSON(t, pubKey, &pub3, tc.keyName)
assert.EqualValues(t, pubKey, pub3) assert.EqualValues(t, pubKey, pub3)
} }
@ -95,17 +95,17 @@ func toFromJSON(t *testing.T, in interface{}, recvr interface{}) {
func TestNilEncodings(t *testing.T) { func TestNilEncodings(t *testing.T) {
// make sure sigs are okay with nil // make sure sigs are okay with nil
a, b := SignatureS{}, SignatureS{}
a, b := Signature{}, Signature{}
toFromJSON(t, a, &b) toFromJSON(t, a, &b)
assert.EqualValues(t, a, b) assert.EqualValues(t, a, b)
// make sure sigs are okay with nil // make sure sigs are okay with nil
c, d := PubKeyS{}, PubKeyS{}
c, d := PubKey{}, PubKey{}
toFromJSON(t, c, &d) toFromJSON(t, c, &d)
assert.EqualValues(t, c, d) assert.EqualValues(t, c, d)
// make sure sigs are okay with nil // make sure sigs are okay with nil
e, f := PrivKeyS{}, PrivKeyS{}
e, f := PrivKey{}, PrivKey{}
toFromJSON(t, e, &f) toFromJSON(t, e, &f)
assert.EqualValues(t, e, f) assert.EqualValues(t, e, f)


+ 33
- 24
priv_key.go View File

@ -11,8 +11,8 @@ import (
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
) )
// PrivKey is part of PrivAccount and state.PrivValidator.
type PrivKey interface {
// PrivKeyInner is now the interface itself, use PrivKey in all code
type PrivKeyInner interface {
Bytes() []byte Bytes() []byte
Sign(msg []byte) Signature Sign(msg []byte) Signature
PubKey() PubKey PubKey() PubKey
@ -31,37 +31,45 @@ var privKeyMapper data.Mapper
// register both private key types with go-data (and thus go-wire) // register both private key types with go-data (and thus go-wire)
func init() { func init() {
privKeyMapper = data.NewMapper(PrivKeyS{}).
privKeyMapper = data.NewMapper(PrivKey{}).
RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519). RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
} }
// PrivKeyS add json serialization to PrivKey
type PrivKeyS struct {
PrivKey
// PrivKey handles all encoding and exposes methods
type PrivKey struct {
PrivKeyInner
} }
func WrapPrivKey(pk PrivKey) PrivKeyS {
for ppk, ok := pk.(PrivKeyS); ok; ppk, ok = pk.(PrivKeyS) {
pk = ppk.PrivKey
func WrapPrivKey(pk PrivKeyInner) PrivKey {
if wrap, ok := pk.(PrivKey); ok {
pk = wrap.Unwrap()
} }
return PrivKeyS{pk}
return PrivKey{pk}
} }
func (p PrivKeyS) MarshalJSON() ([]byte, error) {
return privKeyMapper.ToJSON(p.PrivKey)
func (p PrivKey) Unwrap() PrivKeyInner {
pk := p.PrivKeyInner
for wrap, ok := pk.(PrivKey); ok; wrap, ok = pk.(PrivKey) {
pk = wrap.PrivKeyInner
}
return pk
}
func (p PrivKey) MarshalJSON() ([]byte, error) {
return privKeyMapper.ToJSON(p.PrivKeyInner)
} }
func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error) {
func (p *PrivKey) UnmarshalJSON(data []byte) (err error) {
parsed, err := privKeyMapper.FromJSON(data) parsed, err := privKeyMapper.FromJSON(data)
if err == nil && parsed != nil { if err == nil && parsed != nil {
p.PrivKey = parsed.(PrivKey)
p.PrivKeyInner = parsed.(PrivKeyInner)
} }
return return
} }
func (p PrivKeyS) Empty() bool {
return p.PrivKey == nil
func (p PrivKey) Empty() bool {
return p.PrivKeyInner == nil
} }
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) { func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
@ -75,22 +83,23 @@ func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
type PrivKeyEd25519 [64]byte type PrivKeyEd25519 [64]byte
func (privKey PrivKeyEd25519) Bytes() []byte { func (privKey PrivKeyEd25519) Bytes() []byte {
return wire.BinaryBytes(struct{ PrivKey }{privKey})
return wire.BinaryBytes(PrivKey{privKey})
} }
func (privKey PrivKeyEd25519) Sign(msg []byte) Signature { func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
privKeyBytes := [64]byte(privKey) privKeyBytes := [64]byte(privKey)
signatureBytes := ed25519.Sign(&privKeyBytes, msg) signatureBytes := ed25519.Sign(&privKeyBytes, msg)
return SignatureEd25519(*signatureBytes)
return WrapSignature(SignatureEd25519(*signatureBytes))
} }
func (privKey PrivKeyEd25519) PubKey() PubKey { func (privKey PrivKeyEd25519) PubKey() PubKey {
privKeyBytes := [64]byte(privKey) privKeyBytes := [64]byte(privKey)
return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes))
pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
return WrapPubKey(PubKeyEd25519(pubBytes))
} }
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool { func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
if otherEd, ok := other.(PrivKeyEd25519); ok {
if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok {
return bytes.Equal(privKey[:], otherEd[:]) return bytes.Equal(privKey[:], otherEd[:])
} else { } else {
return false return false
@ -153,7 +162,7 @@ func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
type PrivKeySecp256k1 [32]byte type PrivKeySecp256k1 [32]byte
func (privKey PrivKeySecp256k1) Bytes() []byte { func (privKey PrivKeySecp256k1) Bytes() []byte {
return wire.BinaryBytes(struct{ PrivKey }{privKey})
return wire.BinaryBytes(PrivKey{privKey})
} }
func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature { func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
@ -162,18 +171,18 @@ func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
if err != nil { if err != nil {
PanicSanity(err) PanicSanity(err)
} }
return SignatureSecp256k1(sig__.Serialize())
return WrapSignature(SignatureSecp256k1(sig__.Serialize()))
} }
func (privKey PrivKeySecp256k1) PubKey() PubKey { func (privKey PrivKeySecp256k1) PubKey() PubKey {
_, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) _, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
var pub PubKeySecp256k1 var pub PubKeySecp256k1
copy(pub[:], pub__.SerializeCompressed()) copy(pub[:], pub__.SerializeCompressed())
return pub
return WrapPubKey(pub)
} }
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool { func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
if otherSecp, ok := other.(PrivKeySecp256k1); ok {
if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok {
return bytes.Equal(privKey[:], otherSecp[:]) return bytes.Equal(privKey[:], otherSecp[:])
} else { } else {
return false return false


+ 31
- 31
pub_key.go View File

@ -13,8 +13,8 @@ import (
"golang.org/x/crypto/ripemd160" "golang.org/x/crypto/ripemd160"
) )
// PubKey is part of Account and Validator.
type PubKey interface {
// PubKeyInner is now the interface itself, use PubKey struct in all code
type PubKeyInner interface {
Address() []byte Address() []byte
Bytes() []byte Bytes() []byte
KeyString() string KeyString() string
@ -26,37 +26,45 @@ var pubKeyMapper data.Mapper
// register both public key types with go-data (and thus go-wire) // register both public key types with go-data (and thus go-wire)
func init() { func init() {
pubKeyMapper = data.NewMapper(PubKeyS{}).
pubKeyMapper = data.NewMapper(PubKey{}).
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519). RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1) RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
} }
// PubKeyS add json serialization to PubKey
type PubKeyS struct {
PubKey
// PubKey add json serialization to PubKeyInner
type PubKey struct {
PubKeyInner
} }
func WrapPubKey(pk PubKey) PubKeyS {
for ppk, ok := pk.(PubKeyS); ok; ppk, ok = pk.(PubKeyS) {
pk = ppk.PubKey
func WrapPubKey(pk PubKeyInner) PubKey {
if wrap, ok := pk.(PubKey); ok {
pk = wrap.Unwrap()
} }
return PubKeyS{pk}
return PubKey{pk}
} }
func (p PubKeyS) MarshalJSON() ([]byte, error) {
return pubKeyMapper.ToJSON(p.PubKey)
func (p PubKey) Unwrap() PubKeyInner {
pk := p.PubKeyInner
for wrap, ok := pk.(PubKey); ok; wrap, ok = pk.(PubKey) {
pk = wrap.PubKeyInner
}
return pk
}
func (p PubKey) MarshalJSON() ([]byte, error) {
return pubKeyMapper.ToJSON(p.PubKeyInner)
} }
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error) {
func (p *PubKey) UnmarshalJSON(data []byte) (err error) {
parsed, err := pubKeyMapper.FromJSON(data) parsed, err := pubKeyMapper.FromJSON(data)
if err == nil && parsed != nil { if err == nil && parsed != nil {
p.PubKey = parsed.(PubKey)
p.PubKeyInner = parsed.(PubKeyInner)
} }
return return
} }
func (p PubKeyS) Empty() bool {
return p.PubKey == nil
func (p PubKey) Empty() bool {
return p.PubKeyInner == nil
} }
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) { func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
@ -66,7 +74,7 @@ func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
//------------------------------------- //-------------------------------------
// Implements PubKey
// Implements PubKeyInner
type PubKeyEd25519 [32]byte type PubKeyEd25519 [32]byte
func (pubKey PubKeyEd25519) Address() []byte { func (pubKey PubKeyEd25519) Address() []byte {
@ -83,16 +91,12 @@ func (pubKey PubKeyEd25519) Address() []byte {
} }
func (pubKey PubKeyEd25519) Bytes() []byte { func (pubKey PubKeyEd25519) Bytes() []byte {
return wire.BinaryBytes(struct{ PubKey }{pubKey})
return wire.BinaryBytes(PubKey{pubKey})
} }
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool { func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
// unwrap if needed
if wrap, ok := sig_.(SignatureS); ok {
sig_ = wrap.Signature
}
// make sure we use the same algorithm to sign // make sure we use the same algorithm to sign
sig, ok := sig_.(SignatureEd25519)
sig, ok := sig_.Unwrap().(SignatureEd25519)
if !ok { if !ok {
return false return false
} }
@ -134,7 +138,7 @@ func (pubKey PubKeyEd25519) KeyString() string {
} }
func (pubKey PubKeyEd25519) Equals(other PubKey) bool { func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
if otherEd, ok := other.(PubKeyEd25519); ok {
if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
return bytes.Equal(pubKey[:], otherEd[:]) return bytes.Equal(pubKey[:], otherEd[:])
} else { } else {
return false return false
@ -160,16 +164,12 @@ func (pubKey PubKeySecp256k1) Address() []byte {
} }
func (pubKey PubKeySecp256k1) Bytes() []byte { func (pubKey PubKeySecp256k1) Bytes() []byte {
return wire.BinaryBytes(struct{ PubKey }{pubKey})
return wire.BinaryBytes(PubKey{pubKey})
} }
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool { func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
// unwrap if needed
if wrap, ok := sig_.(SignatureS); ok {
sig_ = wrap.Signature
}
// and assert same algorithm to sign and verify // and assert same algorithm to sign and verify
sig, ok := sig_.(SignatureSecp256k1)
sig, ok := sig_.Unwrap().(SignatureSecp256k1)
if !ok { if !ok {
return false return false
} }
@ -207,7 +207,7 @@ func (pubKey PubKeySecp256k1) KeyString() string {
} }
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool { func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
if otherSecp, ok := other.(PubKeySecp256k1); ok {
if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
return bytes.Equal(pubKey[:], otherSecp[:]) return bytes.Equal(pubKey[:], otherSecp[:])
} else { } else {
return false return false


+ 1
- 1
pub_key_test.go View File

@ -31,7 +31,7 @@ func TestPubKeySecp256k1Address(t *testing.T) {
var priv PrivKeySecp256k1 var priv PrivKeySecp256k1
copy(priv[:], privB) copy(priv[:], privB)
pubT := priv.PubKey().(PubKeySecp256k1)
pubT := priv.PubKey().Unwrap().(PubKeySecp256k1)
pub := pubT[:] pub := pubT[:]
addr := priv.PubKey().Address() addr := priv.PubKey().Address()


+ 29
- 20
signature.go View File

@ -9,8 +9,9 @@ import (
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
) )
// Signature is a part of Txs and consensus Votes.
type Signature interface {
// SignatureInner is now the interface itself.
// Use Signature in all code
type SignatureInner interface {
Bytes() []byte Bytes() []byte
IsZero() bool IsZero() bool
String() string String() string
@ -21,37 +22,45 @@ var sigMapper data.Mapper
// register both public key types with go-data (and thus go-wire) // register both public key types with go-data (and thus go-wire)
func init() { func init() {
sigMapper = data.NewMapper(SignatureS{}).
sigMapper = data.NewMapper(Signature{}).
RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519). RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1) RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
} }
// SignatureS add json serialization to Signature
type SignatureS struct {
Signature
// Signature add json serialization to Signature
type Signature struct {
SignatureInner
} }
func WrapSignature(sig Signature) SignatureS {
for ssig, ok := sig.(SignatureS); ok; ssig, ok = sig.(SignatureS) {
sig = ssig.Signature
func WrapSignature(pk SignatureInner) Signature {
if wrap, ok := pk.(Signature); ok {
pk = wrap.Unwrap()
} }
return SignatureS{sig}
return Signature{pk}
} }
func (p SignatureS) MarshalJSON() ([]byte, error) {
return sigMapper.ToJSON(p.Signature)
func (p Signature) Unwrap() SignatureInner {
pk := p.SignatureInner
for wrap, ok := pk.(Signature); ok; wrap, ok = pk.(Signature) {
pk = wrap.SignatureInner
}
return pk
}
func (p Signature) MarshalJSON() ([]byte, error) {
return sigMapper.ToJSON(p.SignatureInner)
} }
func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
func (p *Signature) UnmarshalJSON(data []byte) (err error) {
parsed, err := sigMapper.FromJSON(data) parsed, err := sigMapper.FromJSON(data)
if err == nil && parsed != nil { if err == nil && parsed != nil {
p.Signature = parsed.(Signature)
p.SignatureInner = parsed.(SignatureInner)
} }
return return
} }
func (p SignatureS) Empty() bool {
return p.Signature == nil
func (p Signature) Empty() bool {
return p.SignatureInner == nil
} }
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) { func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
@ -65,7 +74,7 @@ func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
type SignatureEd25519 [64]byte type SignatureEd25519 [64]byte
func (sig SignatureEd25519) Bytes() []byte { func (sig SignatureEd25519) Bytes() []byte {
return wire.BinaryBytes(struct{ Signature }{sig})
return wire.BinaryBytes(Signature{sig})
} }
func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 } func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
@ -73,7 +82,7 @@ func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) } func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
func (sig SignatureEd25519) Equals(other Signature) bool { func (sig SignatureEd25519) Equals(other Signature) bool {
if otherEd, ok := other.(SignatureEd25519); ok {
if otherEd, ok := other.Unwrap().(SignatureEd25519); ok {
return bytes.Equal(sig[:], otherEd[:]) return bytes.Equal(sig[:], otherEd[:])
} else { } else {
return false return false
@ -97,7 +106,7 @@ func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
type SignatureSecp256k1 []byte type SignatureSecp256k1 []byte
func (sig SignatureSecp256k1) Bytes() []byte { func (sig SignatureSecp256k1) Bytes() []byte {
return wire.BinaryBytes(struct{ Signature }{sig})
return wire.BinaryBytes(Signature{sig})
} }
func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 } func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
@ -105,7 +114,7 @@ func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) } func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
func (sig SignatureSecp256k1) Equals(other Signature) bool { func (sig SignatureSecp256k1) Equals(other Signature) bool {
if otherEd, ok := other.(SignatureSecp256k1); ok {
if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
return bytes.Equal(sig[:], otherEd[:]) return bytes.Equal(sig[:], otherEd[:])
} else { } else {
return false return false


+ 19
- 19
signature_test.go View File

@ -22,9 +22,9 @@ func TestSignAndValidateEd25519(t *testing.T) {
assert.True(t, pubKey.VerifyBytes(msg, sig)) assert.True(t, pubKey.VerifyBytes(msg, sig))
// Mutate the signature, just one bit. // Mutate the signature, just one bit.
sigEd := sig.(SignatureEd25519)
sigEd[0] ^= byte(0x01)
sig = Signature(sigEd)
sigEd := sig.Unwrap().(SignatureEd25519)
sigEd[7] ^= byte(0x01)
sig = WrapSignature(sigEd)
assert.False(t, pubKey.VerifyBytes(msg, sig)) assert.False(t, pubKey.VerifyBytes(msg, sig))
} }
@ -39,28 +39,28 @@ func TestSignAndValidateSecp256k1(t *testing.T) {
assert.True(t, pubKey.VerifyBytes(msg, sig)) assert.True(t, pubKey.VerifyBytes(msg, sig))
// Mutate the signature, just one bit. // Mutate the signature, just one bit.
sigEd := sig.(SignatureSecp256k1)
sigEd[0] ^= byte(0x01)
sig = Signature(sigEd)
sigEd := sig.Unwrap().(SignatureSecp256k1)
sigEd[3] ^= byte(0x01)
sig = WrapSignature(sigEd)
assert.False(t, pubKey.VerifyBytes(msg, sig)) assert.False(t, pubKey.VerifyBytes(msg, sig))
} }
func TestSignatureEncodings(t *testing.T) { func TestSignatureEncodings(t *testing.T) {
cases := []struct { cases := []struct {
privKey PrivKeyS
privKey PrivKey
sigSize int sigSize int
sigType byte sigType byte
sigName string sigName string
}{ }{
{ {
privKey: PrivKeyS{GenPrivKeyEd25519()},
privKey: WrapPrivKey(GenPrivKeyEd25519()),
sigSize: ed25519.SignatureSize, sigSize: ed25519.SignatureSize,
sigType: TypeEd25519, sigType: TypeEd25519,
sigName: NameEd25519, sigName: NameEd25519,
}, },
{ {
privKey: PrivKeyS{GenPrivKeySecp256k1()},
privKey: WrapPrivKey(GenPrivKeySecp256k1()),
sigSize: 0, // unknown sigSize: 0, // unknown
sigType: TypeSecp256k1, sigType: TypeSecp256k1,
sigName: NameSecp256k1, sigName: NameSecp256k1,
@ -69,10 +69,10 @@ func TestSignatureEncodings(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
// note we embed them from the beginning.... // note we embed them from the beginning....
pubKey := PubKeyS{tc.privKey.PubKey()}
pubKey := tc.privKey.PubKey()
msg := CRandBytes(128) msg := CRandBytes(128)
sig := SignatureS{tc.privKey.Sign(msg)}
sig := tc.privKey.Sign(msg)
// store as wire // store as wire
bin, err := data.ToWire(sig) bin, err := data.ToWire(sig)
@ -83,7 +83,7 @@ func TestSignatureEncodings(t *testing.T) {
assert.Equal(t, tc.sigType, bin[0]) assert.Equal(t, tc.sigType, bin[0])
// and back // and back
sig2 := SignatureS{}
sig2 := Signature{}
err = data.FromWire(bin, &sig2) err = data.FromWire(bin, &sig2)
require.Nil(t, err, "%+v", err) require.Nil(t, err, "%+v", err)
assert.EqualValues(t, sig, sig2) assert.EqualValues(t, sig, sig2)
@ -95,7 +95,7 @@ func TestSignatureEncodings(t *testing.T) {
assert.True(t, strings.Contains(string(js), tc.sigName)) assert.True(t, strings.Contains(string(js), tc.sigName))
// and back // and back
sig3 := SignatureS{}
sig3 := Signature{}
err = data.FromJSON(js, &sig3) err = data.FromJSON(js, &sig3)
require.Nil(t, err, "%+v", err) require.Nil(t, err, "%+v", err)
assert.EqualValues(t, sig, sig3) assert.EqualValues(t, sig, sig3)
@ -118,25 +118,25 @@ func TestWrapping(t *testing.T) {
sig := priv.Sign(msg) sig := priv.Sign(msg)
// do some wrapping // do some wrapping
pubs := []PubKeyS{
pubs := []PubKey{
WrapPubKey(nil), WrapPubKey(nil),
WrapPubKey(pub), WrapPubKey(pub),
WrapPubKey(WrapPubKey(WrapPubKey(WrapPubKey(pub)))), WrapPubKey(WrapPubKey(WrapPubKey(WrapPubKey(pub)))),
WrapPubKey(PubKeyS{PubKeyS{PubKeyS{pub}}}),
WrapPubKey(PubKey{PubKey{PubKey{pub}}}),
} }
for _, p := range pubs { for _, p := range pubs {
_, ok := p.PubKey.(PubKeyS)
_, ok := p.PubKeyInner.(PubKey)
assert.False(ok) assert.False(ok)
} }
sigs := []SignatureS{
sigs := []Signature{
WrapSignature(nil), WrapSignature(nil),
WrapSignature(sig), WrapSignature(sig),
WrapSignature(WrapSignature(WrapSignature(WrapSignature(sig)))), WrapSignature(WrapSignature(WrapSignature(WrapSignature(sig)))),
WrapSignature(SignatureS{SignatureS{SignatureS{sig}}}),
WrapSignature(Signature{Signature{Signature{sig}}}),
} }
for _, s := range sigs { for _, s := range sigs {
_, ok := s.Signature.(SignatureS)
_, ok := s.SignatureInner.(Signature)
assert.False(ok) assert.False(ok)
} }


Loading…
Cancel
Save