mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-17 07:01:20 +00:00
Use new naming scheme PubKey{PubKeyInner}
This commit is contained in:
@ -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)
|
||||||
|
@ -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()}
|
pubKey := tc.privKey.PubKey()
|
||||||
pub2 := PubKeyS{}
|
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)
|
||||||
|
|
||||||
|
57
priv_key.go
57
priv_key.go
@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PrivKey is part of PrivAccount and state.PrivValidator.
|
// PrivKeyInner is now the interface itself, use PrivKey in all code
|
||||||
type PrivKey interface {
|
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
|
// PrivKey handles all encoding and exposes methods
|
||||||
type PrivKeyS struct {
|
type PrivKey struct {
|
||||||
PrivKey
|
PrivKeyInner
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapPrivKey(pk PrivKey) PrivKeyS {
|
func WrapPrivKey(pk PrivKeyInner) PrivKey {
|
||||||
for ppk, ok := pk.(PrivKeyS); ok; ppk, ok = pk.(PrivKeyS) {
|
if wrap, ok := pk.(PrivKey); ok {
|
||||||
pk = ppk.PrivKey
|
pk = wrap.Unwrap()
|
||||||
}
|
}
|
||||||
return PrivKeyS{pk}
|
return PrivKey{pk}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PrivKeyS) MarshalJSON() ([]byte, error) {
|
func (p PrivKey) Unwrap() PrivKeyInner {
|
||||||
return privKeyMapper.ToJSON(p.PrivKey)
|
pk := p.PrivKeyInner
|
||||||
|
for wrap, ok := pk.(PrivKey); ok; wrap, ok = pk.(PrivKey) {
|
||||||
|
pk = wrap.PrivKeyInner
|
||||||
|
}
|
||||||
|
return pk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error) {
|
func (p PrivKey) MarshalJSON() ([]byte, error) {
|
||||||
|
return privKeyMapper.ToJSON(p.PrivKeyInner)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
func (p PrivKey) Empty() bool {
|
||||||
return p.PrivKey == nil
|
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
|
||||||
|
62
pub_key.go
62
pub_key.go
@ -13,8 +13,8 @@ import (
|
|||||||
"golang.org/x/crypto/ripemd160"
|
"golang.org/x/crypto/ripemd160"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PubKey is part of Account and Validator.
|
// PubKeyInner is now the interface itself, use PubKey struct in all code
|
||||||
type PubKey interface {
|
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
|
// PubKey add json serialization to PubKeyInner
|
||||||
type PubKeyS struct {
|
type PubKey struct {
|
||||||
PubKey
|
PubKeyInner
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapPubKey(pk PubKey) PubKeyS {
|
func WrapPubKey(pk PubKeyInner) PubKey {
|
||||||
for ppk, ok := pk.(PubKeyS); ok; ppk, ok = pk.(PubKeyS) {
|
if wrap, ok := pk.(PubKey); ok {
|
||||||
pk = ppk.PubKey
|
pk = wrap.Unwrap()
|
||||||
}
|
}
|
||||||
return PubKeyS{pk}
|
return PubKey{pk}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PubKeyS) MarshalJSON() ([]byte, error) {
|
func (p PubKey) Unwrap() PubKeyInner {
|
||||||
return pubKeyMapper.ToJSON(p.PubKey)
|
pk := p.PubKeyInner
|
||||||
|
for wrap, ok := pk.(PubKey); ok; wrap, ok = pk.(PubKey) {
|
||||||
|
pk = wrap.PubKeyInner
|
||||||
|
}
|
||||||
|
return pk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error) {
|
func (p PubKey) MarshalJSON() ([]byte, error) {
|
||||||
|
return pubKeyMapper.ToJSON(p.PubKeyInner)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
func (p PubKey) Empty() bool {
|
||||||
return p.PubKey == nil
|
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
|
||||||
|
@ -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()
|
||||||
|
|
||||||
|
49
signature.go
49
signature.go
@ -9,8 +9,9 @@ import (
|
|||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Signature is a part of Txs and consensus Votes.
|
// SignatureInner is now the interface itself.
|
||||||
type Signature interface {
|
// 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
|
// Signature add json serialization to Signature
|
||||||
type SignatureS struct {
|
type Signature struct {
|
||||||
Signature
|
SignatureInner
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapSignature(sig Signature) SignatureS {
|
func WrapSignature(pk SignatureInner) Signature {
|
||||||
for ssig, ok := sig.(SignatureS); ok; ssig, ok = sig.(SignatureS) {
|
if wrap, ok := pk.(Signature); ok {
|
||||||
sig = ssig.Signature
|
pk = wrap.Unwrap()
|
||||||
}
|
}
|
||||||
return SignatureS{sig}
|
return Signature{pk}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p SignatureS) MarshalJSON() ([]byte, error) {
|
func (p Signature) Unwrap() SignatureInner {
|
||||||
return sigMapper.ToJSON(p.Signature)
|
pk := p.SignatureInner
|
||||||
|
for wrap, ok := pk.(Signature); ok; wrap, ok = pk.(Signature) {
|
||||||
|
pk = wrap.SignatureInner
|
||||||
|
}
|
||||||
|
return pk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
|
func (p Signature) MarshalJSON() ([]byte, error) {
|
||||||
|
return sigMapper.ToJSON(p.SignatureInner)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
func (p Signature) Empty() bool {
|
||||||
return p.Signature == nil
|
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
|
||||||
|
@ -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 := sig.Unwrap().(SignatureEd25519)
|
||||||
sigEd[0] ^= byte(0x01)
|
sigEd[7] ^= byte(0x01)
|
||||||
sig = Signature(sigEd)
|
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 := sig.Unwrap().(SignatureSecp256k1)
|
||||||
sigEd[0] ^= byte(0x01)
|
sigEd[3] ^= byte(0x01)
|
||||||
sig = Signature(sigEd)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user