mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-20 16:36:31 +00:00
Cleanup; Implement .Wrap()
This commit is contained in:
9
crypto.go
Normal file
9
crypto.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
// Types of implementations
|
||||||
|
const (
|
||||||
|
TypeEd25519 = byte(0x01)
|
||||||
|
TypeSecp256k1 = byte(0x02)
|
||||||
|
NameEd25519 = "ed25519"
|
||||||
|
NameSecp256k1 = "secp256k1"
|
||||||
|
)
|
102
embed_test.go
102
embed_test.go
@ -9,50 +9,13 @@ import (
|
|||||||
data "github.com/tendermint/go-data"
|
data "github.com/tendermint/go-data"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Foo struct {
|
type PubName struct {
|
||||||
Name string
|
PubNameInner
|
||||||
}
|
|
||||||
|
|
||||||
func (f Foo) Greet() string {
|
|
||||||
return "Foo: " + f.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
type Bar struct {
|
|
||||||
Age int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b Bar) Greet() string {
|
|
||||||
return fmt.Sprintf("Bar #%d", b.Age)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PubNameInner interface {
|
type PubNameInner interface {
|
||||||
Greet() string
|
AssertIsPubNameInner()
|
||||||
}
|
String() string
|
||||||
|
|
||||||
type privNameInner interface {
|
|
||||||
Greet() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Greeter interface {
|
|
||||||
Greet() string
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
pubNameMapper, privNameMapper data.Mapper
|
|
||||||
)
|
|
||||||
|
|
||||||
// register both public key types with go-data (and thus go-wire)
|
|
||||||
func init() {
|
|
||||||
pubNameMapper = data.NewMapper(PubName{}).
|
|
||||||
RegisterImplementation(Foo{}, "foo", 1).
|
|
||||||
RegisterImplementation(Bar{}, "bar", 2)
|
|
||||||
privNameMapper = data.NewMapper(PrivName{}).
|
|
||||||
RegisterImplementation(Foo{}, "foo", 1).
|
|
||||||
RegisterImplementation(Bar{}, "bar", 2)
|
|
||||||
}
|
|
||||||
|
|
||||||
type PubName struct {
|
|
||||||
PubNameInner
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PubName) MarshalJSON() ([]byte, error) {
|
func (p PubName) MarshalJSON() ([]byte, error) {
|
||||||
@ -67,62 +30,61 @@ func (p *PubName) UnmarshalJSON(data []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type PrivName struct {
|
var pubNameMapper = data.NewMapper(PubName{}).
|
||||||
privNameInner
|
RegisterImplementation(PubNameFoo{}, "foo", 1).
|
||||||
|
RegisterImplementation(PubNameBar{}, "bar", 2)
|
||||||
|
|
||||||
|
func (f PubNameFoo) AssertIsPubNameInner() {}
|
||||||
|
func (f PubNameBar) AssertIsPubNameInner() {}
|
||||||
|
|
||||||
|
//----------------------------------------
|
||||||
|
|
||||||
|
type PubNameFoo struct {
|
||||||
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PrivName) MarshalJSON() ([]byte, error) {
|
func (f PubNameFoo) String() string { return "Foo: " + f.Name }
|
||||||
return privNameMapper.ToJSON(p.privNameInner)
|
|
||||||
|
type PubNameBar struct {
|
||||||
|
Age int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PrivName) UnmarshalJSON(data []byte) error {
|
func (b PubNameBar) String() string { return fmt.Sprintf("Bar #%d", b.Age) }
|
||||||
parsed, err := privNameMapper.FromJSON(data)
|
|
||||||
if err == nil && parsed != nil {
|
//----------------------------------------
|
||||||
p.privNameInner = parsed.(privNameInner)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestEncodeDemo tries the various strategies to encode the objects
|
// TestEncodeDemo tries the various strategies to encode the objects
|
||||||
func TestEncodeDemo(t *testing.T) {
|
func TestEncodeDemo(t *testing.T) {
|
||||||
assert, require := assert.New(t), require.New(t)
|
assert, require := assert.New(t), require.New(t)
|
||||||
// assert := assert.New(t)
|
|
||||||
// require := require.New(t)
|
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
in, out Greeter
|
in, out PubNameInner
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{PubName{Foo{"pub-foo"}}, &PubName{}, "Foo: pub-foo"},
|
{PubName{PubNameFoo{"pub-foo"}}, &PubName{}, "Foo: pub-foo"},
|
||||||
{PubName{Bar{7}}, &PubName{}, "Bar #7"},
|
{PubName{PubNameBar{7}}, &PubName{}, "Bar #7"},
|
||||||
|
|
||||||
// Note these fail - if you can figure a solution here, I'll buy you a beer :)
|
|
||||||
// (ebuchman is right, you must either break the reflection system, or modify go-wire)
|
|
||||||
// but such a mod would let us make REALLY sure that no one could construct like this
|
|
||||||
|
|
||||||
// {PrivName{Foo{"priv-foo"}}, &PrivName{}, "Foo: priv-foo"},
|
|
||||||
// {PrivName{Bar{9}}, &PrivName{}, "Bar #9"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tc := range cases {
|
for i, tc := range cases {
|
||||||
// make sure it is proper to start
|
|
||||||
require.Equal(tc.expected, tc.in.Greet())
|
|
||||||
|
|
||||||
// now, try to encode as binary
|
// Make sure it is proper to start
|
||||||
|
require.Equal(tc.expected, tc.in.String())
|
||||||
|
|
||||||
|
// Try to encode as binary
|
||||||
b, err := data.ToWire(tc.in)
|
b, err := data.ToWire(tc.in)
|
||||||
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
||||||
err := data.FromWire(b, tc.out)
|
err := data.FromWire(b, tc.out)
|
||||||
if assert.Nil(err) {
|
if assert.Nil(err) {
|
||||||
assert.Equal(tc.expected, tc.out.Greet())
|
assert.Equal(tc.expected, tc.out.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to encode it as json
|
// Try to encode it as json
|
||||||
j, err := data.ToJSON(tc.in)
|
j, err := data.ToJSON(tc.in)
|
||||||
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
if assert.Nil(err, "%d: %#v", i, tc.in) {
|
||||||
err := data.FromJSON(j, tc.out)
|
err := data.FromJSON(j, tc.out)
|
||||||
if assert.Nil(err) {
|
if assert.Nil(err) {
|
||||||
assert.Equal(tc.expected, tc.out.Greet())
|
assert.Equal(tc.expected, tc.out.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,12 +69,12 @@ func TestKeyEncodings(t *testing.T) {
|
|||||||
keyName string
|
keyName string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
privKey: WrapPrivKey(GenPrivKeyEd25519()),
|
privKey: GenPrivKeyEd25519().Wrap(),
|
||||||
keyType: TypeEd25519,
|
keyType: TypeEd25519,
|
||||||
keyName: NameEd25519,
|
keyName: NameEd25519,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
privKey: WrapPrivKey(GenPrivKeySecp256k1()),
|
privKey: GenPrivKeySecp256k1().Wrap(),
|
||||||
keyType: TypeSecp256k1,
|
keyType: TypeSecp256k1,
|
||||||
keyName: NameSecp256k1,
|
keyName: NameSecp256k1,
|
||||||
},
|
},
|
||||||
|
95
priv_key.go
95
priv_key.go
@ -11,59 +11,26 @@ import (
|
|||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
|
||||||
DO NOT USE this interface.
|
err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
|
||||||
|
return
|
||||||
It is public by necessity but should never be used directly
|
|
||||||
outside of this package.
|
|
||||||
|
|
||||||
Only use the PrivKey, never the PrivKeyInner
|
|
||||||
*/
|
|
||||||
type PrivKeyInner interface {
|
|
||||||
Bytes() []byte
|
|
||||||
Sign(msg []byte) Signature
|
|
||||||
PubKey() PubKey
|
|
||||||
Equals(PrivKey) bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Types of implementations
|
//----------------------------------------
|
||||||
const (
|
|
||||||
TypeEd25519 = byte(0x01)
|
|
||||||
TypeSecp256k1 = byte(0x02)
|
|
||||||
NameEd25519 = "ed25519"
|
|
||||||
NameSecp256k1 = "secp256k1"
|
|
||||||
)
|
|
||||||
|
|
||||||
var privKeyMapper data.Mapper
|
|
||||||
|
|
||||||
// register both private key types with go-data (and thus go-wire)
|
|
||||||
func init() {
|
|
||||||
privKeyMapper = data.NewMapper(PrivKey{}).
|
|
||||||
RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
|
|
||||||
RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrivKey should be used instead of an interface in all external packages
|
|
||||||
// unless you demand a concrete implementation, then use that directly.
|
|
||||||
type PrivKey struct {
|
type PrivKey struct {
|
||||||
PrivKeyInner `json:"unwrap"`
|
PrivKeyInner `json:"unwrap"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapPrivKey goes from concrete implementation to "interface" struct
|
// DO NOT USE THIS INTERFACE.
|
||||||
func WrapPrivKey(pk PrivKeyInner) PrivKey {
|
// You probably want to use PubKey
|
||||||
if wrap, ok := pk.(PrivKey); ok {
|
type PrivKeyInner interface {
|
||||||
pk = wrap.Unwrap()
|
AssertIsPrivKeyInner()
|
||||||
}
|
Bytes() []byte
|
||||||
return PrivKey{pk}
|
Sign(msg []byte) Signature
|
||||||
}
|
PubKey() PubKey
|
||||||
|
Equals(PrivKey) bool
|
||||||
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
|
Wrap() 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) {
|
func (p PrivKey) MarshalJSON() ([]byte, error) {
|
||||||
@ -78,20 +45,30 @@ func (p *PrivKey) UnmarshalJSON(data []byte) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
|
||||||
|
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) Empty() bool {
|
func (p PrivKey) Empty() bool {
|
||||||
return p.PrivKeyInner == nil
|
return p.PrivKeyInner == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
|
var privKeyMapper = data.NewMapper(PrivKey{}).
|
||||||
err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
|
RegisterImplementation(PrivKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||||
return
|
RegisterImplementation(PrivKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements PrivKey
|
// Implements PrivKey
|
||||||
type PrivKeyEd25519 [64]byte
|
type PrivKeyEd25519 [64]byte
|
||||||
|
|
||||||
|
func (privKey PrivKeyEd25519) AssertIsPrivKeyInner() {}
|
||||||
|
|
||||||
func (privKey PrivKeyEd25519) Bytes() []byte {
|
func (privKey PrivKeyEd25519) Bytes() []byte {
|
||||||
return wire.BinaryBytes(PrivKey{privKey})
|
return wire.BinaryBytes(PrivKey{privKey})
|
||||||
}
|
}
|
||||||
@ -99,13 +76,13 @@ func (privKey PrivKeyEd25519) Bytes() []byte {
|
|||||||
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 WrapSignature(SignatureEd25519(*signatureBytes))
|
return SignatureEd25519(*signatureBytes).Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (privKey PrivKeyEd25519) PubKey() PubKey {
|
func (privKey PrivKeyEd25519) PubKey() PubKey {
|
||||||
privKeyBytes := [64]byte(privKey)
|
privKeyBytes := [64]byte(privKey)
|
||||||
pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
|
pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
|
||||||
return WrapPubKey(PubKeyEd25519(pubBytes))
|
return PubKeyEd25519(pubBytes).Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
|
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
|
||||||
@ -149,6 +126,10 @@ func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
|
|||||||
return PrivKeyEd25519(newKey)
|
return PrivKeyEd25519(newKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (privKey PrivKeyEd25519) Wrap() PrivKey {
|
||||||
|
return PrivKey{privKey}
|
||||||
|
}
|
||||||
|
|
||||||
func GenPrivKeyEd25519() PrivKeyEd25519 {
|
func GenPrivKeyEd25519() PrivKeyEd25519 {
|
||||||
privKeyBytes := new([64]byte)
|
privKeyBytes := new([64]byte)
|
||||||
copy(privKeyBytes[:32], CRandBytes(32))
|
copy(privKeyBytes[:32], CRandBytes(32))
|
||||||
@ -171,6 +152,8 @@ func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
|
|||||||
// Implements PrivKey
|
// Implements PrivKey
|
||||||
type PrivKeySecp256k1 [32]byte
|
type PrivKeySecp256k1 [32]byte
|
||||||
|
|
||||||
|
func (privKey PrivKeySecp256k1) AssertIsPrivKeyInner() {}
|
||||||
|
|
||||||
func (privKey PrivKeySecp256k1) Bytes() []byte {
|
func (privKey PrivKeySecp256k1) Bytes() []byte {
|
||||||
return wire.BinaryBytes(PrivKey{privKey})
|
return wire.BinaryBytes(PrivKey{privKey})
|
||||||
}
|
}
|
||||||
@ -181,14 +164,14 @@ func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
PanicSanity(err)
|
PanicSanity(err)
|
||||||
}
|
}
|
||||||
return WrapSignature(SignatureSecp256k1(sig__.Serialize()))
|
return SignatureSecp256k1(sig__.Serialize()).Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
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 WrapPubKey(pub)
|
return pub.Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
|
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
|
||||||
@ -214,6 +197,10 @@ func (privKey PrivKeySecp256k1) String() string {
|
|||||||
return Fmt("PrivKeySecp256k1{*****}")
|
return Fmt("PrivKeySecp256k1{*****}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (privKey PrivKeySecp256k1) Wrap() PrivKey {
|
||||||
|
return PrivKey{privKey}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Deterministically generates new priv-key bytes from key.
|
// Deterministically generates new priv-key bytes from key.
|
||||||
func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
|
func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
|
||||||
|
85
pub_key.go
85
pub_key.go
@ -13,80 +13,65 @@ import (
|
|||||||
"golang.org/x/crypto/ripemd160"
|
"golang.org/x/crypto/ripemd160"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
|
||||||
DO NOT USE this interface.
|
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
It is public by necessity but should never be used directly
|
//----------------------------------------
|
||||||
outside of this package.
|
|
||||||
|
|
||||||
Only use the PubKey, never the PubKeyInner
|
type PubKey struct {
|
||||||
*/
|
PubKeyInner `json:"unwrap"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DO NOT USE THIS INTERFACE.
|
||||||
|
// You probably want to use PubKey
|
||||||
type PubKeyInner interface {
|
type PubKeyInner interface {
|
||||||
|
AssertIsPubKeyInner()
|
||||||
Address() []byte
|
Address() []byte
|
||||||
Bytes() []byte
|
Bytes() []byte
|
||||||
KeyString() string
|
KeyString() string
|
||||||
VerifyBytes(msg []byte, sig Signature) bool
|
VerifyBytes(msg []byte, sig Signature) bool
|
||||||
Equals(PubKey) bool
|
Equals(PubKey) bool
|
||||||
|
Wrap() PubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
var pubKeyMapper data.Mapper
|
func (pk PubKey) MarshalJSON() ([]byte, error) {
|
||||||
|
return pubKeyMapper.ToJSON(pk.PubKeyInner)
|
||||||
// register both public key types with go-data (and thus go-wire)
|
|
||||||
func init() {
|
|
||||||
pubKeyMapper = data.NewMapper(PubKey{}).
|
|
||||||
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
|
|
||||||
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PubKey should be used instead of an interface in all external packages
|
func (pk *PubKey) UnmarshalJSON(data []byte) (err error) {
|
||||||
// unless you demand a concrete implementation, then use that directly.
|
parsed, err := pubKeyMapper.FromJSON(data)
|
||||||
type PubKey struct {
|
if err == nil && parsed != nil {
|
||||||
PubKeyInner `json:"unwrap"`
|
pk.PubKeyInner = parsed.(PubKeyInner)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
// WrapPubKey goes from concrete implementation to "interface" struct
|
|
||||||
func WrapPubKey(pk PubKeyInner) PubKey {
|
|
||||||
if wrap, ok := pk.(PubKey); ok {
|
|
||||||
pk = wrap.Unwrap()
|
|
||||||
}
|
|
||||||
return PubKey{pk}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
|
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
|
||||||
func (p PubKey) Unwrap() PubKeyInner {
|
func (pk PubKey) Unwrap() PubKeyInner {
|
||||||
pk := p.PubKeyInner
|
pkI := pk.PubKeyInner
|
||||||
for wrap, ok := pk.(PubKey); ok; wrap, ok = pk.(PubKey) {
|
for wrap, ok := pkI.(PubKey); ok; wrap, ok = pkI.(PubKey) {
|
||||||
pk = wrap.PubKeyInner
|
pkI = wrap.PubKeyInner
|
||||||
}
|
}
|
||||||
return pk
|
return pkI
|
||||||
}
|
|
||||||
|
|
||||||
func (p PubKey) MarshalJSON() ([]byte, error) {
|
|
||||||
return pubKeyMapper.ToJSON(p.PubKeyInner)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PubKey) UnmarshalJSON(data []byte) (err error) {
|
|
||||||
parsed, err := pubKeyMapper.FromJSON(data)
|
|
||||||
if err == nil && parsed != nil {
|
|
||||||
p.PubKeyInner = parsed.(PubKeyInner)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PubKey) Empty() bool {
|
func (p PubKey) Empty() bool {
|
||||||
return p.PubKeyInner == nil
|
return p.PubKeyInner == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
|
var pubKeyMapper = data.NewMapper(PubKey{}).
|
||||||
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
|
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
|
||||||
return
|
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements PubKeyInner
|
// Implements PubKeyInner
|
||||||
type PubKeyEd25519 [32]byte
|
type PubKeyEd25519 [32]byte
|
||||||
|
|
||||||
|
func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
|
||||||
|
|
||||||
func (pubKey PubKeyEd25519) Address() []byte {
|
func (pubKey PubKeyEd25519) Address() []byte {
|
||||||
w, n, err := new(bytes.Buffer), new(int), new(error)
|
w, n, err := new(bytes.Buffer), new(int), new(error)
|
||||||
wire.WriteBinary(pubKey[:], w, n, err)
|
wire.WriteBinary(pubKey[:], w, n, err)
|
||||||
@ -155,6 +140,10 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pubKey PubKeyEd25519) Wrap() PubKey {
|
||||||
|
return PubKey{pubKey}
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements PubKey.
|
// Implements PubKey.
|
||||||
@ -162,6 +151,8 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
|
|||||||
// prefixed with 0x02 or 0x03, depending on the y-cord.
|
// prefixed with 0x02 or 0x03, depending on the y-cord.
|
||||||
type PubKeySecp256k1 [33]byte
|
type PubKeySecp256k1 [33]byte
|
||||||
|
|
||||||
|
func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
|
||||||
|
|
||||||
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
|
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
|
||||||
func (pubKey PubKeySecp256k1) Address() []byte {
|
func (pubKey PubKeySecp256k1) Address() []byte {
|
||||||
hasherSHA256 := sha256.New()
|
hasherSHA256 := sha256.New()
|
||||||
@ -223,3 +214,7 @@ func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pubKey PubKeySecp256k1) Wrap() PubKey {
|
||||||
|
return PubKey{pubKey}
|
||||||
|
}
|
||||||
|
107
signature.go
107
signature.go
@ -9,79 +9,64 @@ import (
|
|||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
|
||||||
DO NOT USE this interface.
|
err = wire.ReadBinaryBytes(sigBytes, &sig)
|
||||||
|
return
|
||||||
It is public by necessity but should never be used directly
|
|
||||||
outside of this package.
|
|
||||||
|
|
||||||
Only use the Signature, never the SignatureInner
|
|
||||||
*/
|
|
||||||
type SignatureInner interface {
|
|
||||||
Bytes() []byte
|
|
||||||
IsZero() bool
|
|
||||||
String() string
|
|
||||||
Equals(Signature) bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var sigMapper data.Mapper
|
//----------------------------------------
|
||||||
|
|
||||||
// register both public key types with go-data (and thus go-wire)
|
|
||||||
func init() {
|
|
||||||
sigMapper = data.NewMapper(Signature{}).
|
|
||||||
RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
|
|
||||||
RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Signature should be used instead of an interface in all external packages
|
|
||||||
// unless you demand a concrete implementation, then use that directly.
|
|
||||||
type Signature struct {
|
type Signature struct {
|
||||||
SignatureInner `json:"unwrap"`
|
SignatureInner `json:"unwrap"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapSignature goes from concrete implementation to "interface" struct
|
// DO NOT USE THIS INTERFACE.
|
||||||
func WrapSignature(pk SignatureInner) Signature {
|
// You probably want to use Signature.
|
||||||
if wrap, ok := pk.(Signature); ok {
|
type SignatureInner interface {
|
||||||
pk = wrap.Unwrap()
|
AssertIsSignatureInner()
|
||||||
|
Bytes() []byte
|
||||||
|
IsZero() bool
|
||||||
|
String() string
|
||||||
|
Equals(Signature) bool
|
||||||
|
Wrap() Signature
|
||||||
}
|
}
|
||||||
return Signature{pk}
|
|
||||||
|
func (sig Signature) MarshalJSON() ([]byte, error) {
|
||||||
|
return sigMapper.ToJSON(sig.SignatureInner)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sig *Signature) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
parsed, err := sigMapper.FromJSON(data)
|
||||||
|
if err == nil && parsed != nil {
|
||||||
|
sig.SignatureInner = parsed.(SignatureInner)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
|
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
|
||||||
func (p Signature) Unwrap() SignatureInner {
|
func (sig Signature) Unwrap() SignatureInner {
|
||||||
pk := p.SignatureInner
|
pk := sig.SignatureInner
|
||||||
for wrap, ok := pk.(Signature); ok; wrap, ok = pk.(Signature) {
|
for wrap, ok := pk.(Signature); ok; wrap, ok = pk.(Signature) {
|
||||||
pk = wrap.SignatureInner
|
pk = wrap.SignatureInner
|
||||||
}
|
}
|
||||||
return pk
|
return pk
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Signature) MarshalJSON() ([]byte, error) {
|
func (sig Signature) Empty() bool {
|
||||||
return sigMapper.ToJSON(p.SignatureInner)
|
return sig.SignatureInner == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Signature) UnmarshalJSON(data []byte) (err error) {
|
var sigMapper = data.NewMapper(Signature{}).
|
||||||
parsed, err := sigMapper.FromJSON(data)
|
RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
|
||||||
if err == nil && parsed != nil {
|
RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
|
||||||
p.SignatureInner = parsed.(SignatureInner)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Signature) Empty() bool {
|
|
||||||
return p.SignatureInner == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
|
|
||||||
err = wire.ReadBinaryBytes(sigBytes, &sig)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements Signature
|
// Implements Signature
|
||||||
type SignatureEd25519 [64]byte
|
type SignatureEd25519 [64]byte
|
||||||
|
|
||||||
|
func (sig SignatureEd25519) AssertIsSignatureInner() {}
|
||||||
|
|
||||||
func (sig SignatureEd25519) Bytes() []byte {
|
func (sig SignatureEd25519) Bytes() []byte {
|
||||||
return wire.BinaryBytes(Signature{sig})
|
return wire.BinaryBytes(Signature{sig})
|
||||||
}
|
}
|
||||||
@ -98,22 +83,28 @@ func (sig SignatureEd25519) Equals(other Signature) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p SignatureEd25519) MarshalJSON() ([]byte, error) {
|
func (sig SignatureEd25519) MarshalJSON() ([]byte, error) {
|
||||||
return data.Encoder.Marshal(p[:])
|
return data.Encoder.Marshal(sig[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
|
func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
|
||||||
var ref []byte
|
var ref []byte
|
||||||
err := data.Encoder.Unmarshal(&ref, enc)
|
err := data.Encoder.Unmarshal(&ref, enc)
|
||||||
copy(p[:], ref)
|
copy(sig[:], ref)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sig SignatureEd25519) Wrap() Signature {
|
||||||
|
return Signature{sig}
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements Signature
|
// Implements Signature
|
||||||
type SignatureSecp256k1 []byte
|
type SignatureSecp256k1 []byte
|
||||||
|
|
||||||
|
func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
|
||||||
|
|
||||||
func (sig SignatureSecp256k1) Bytes() []byte {
|
func (sig SignatureSecp256k1) Bytes() []byte {
|
||||||
return wire.BinaryBytes(Signature{sig})
|
return wire.BinaryBytes(Signature{sig})
|
||||||
}
|
}
|
||||||
@ -129,10 +120,14 @@ func (sig SignatureSecp256k1) Equals(other Signature) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (p SignatureSecp256k1) MarshalJSON() ([]byte, error) {
|
func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
|
||||||
return data.Encoder.Marshal(p)
|
return data.Encoder.Marshal(sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
|
func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
|
||||||
return data.Encoder.Unmarshal((*[]byte)(p), enc)
|
return data.Encoder.Unmarshal((*[]byte)(sig), enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sig SignatureSecp256k1) Wrap() Signature {
|
||||||
|
return Signature{sig}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ func TestSignAndValidateEd25519(t *testing.T) {
|
|||||||
// Mutate the signature, just one bit.
|
// Mutate the signature, just one bit.
|
||||||
sigEd := sig.Unwrap().(SignatureEd25519)
|
sigEd := sig.Unwrap().(SignatureEd25519)
|
||||||
sigEd[7] ^= byte(0x01)
|
sigEd[7] ^= byte(0x01)
|
||||||
sig = WrapSignature(sigEd)
|
sig = sigEd.Wrap()
|
||||||
|
|
||||||
assert.False(t, pubKey.VerifyBytes(msg, sig))
|
assert.False(t, pubKey.VerifyBytes(msg, sig))
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ func TestSignAndValidateSecp256k1(t *testing.T) {
|
|||||||
// Mutate the signature, just one bit.
|
// Mutate the signature, just one bit.
|
||||||
sigEd := sig.Unwrap().(SignatureSecp256k1)
|
sigEd := sig.Unwrap().(SignatureSecp256k1)
|
||||||
sigEd[3] ^= byte(0x01)
|
sigEd[3] ^= byte(0x01)
|
||||||
sig = WrapSignature(sigEd)
|
sig = sigEd.Wrap()
|
||||||
|
|
||||||
assert.False(t, pubKey.VerifyBytes(msg, sig))
|
assert.False(t, pubKey.VerifyBytes(msg, sig))
|
||||||
}
|
}
|
||||||
@ -54,13 +54,13 @@ func TestSignatureEncodings(t *testing.T) {
|
|||||||
sigName string
|
sigName string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
privKey: WrapPrivKey(GenPrivKeyEd25519()),
|
privKey: GenPrivKeyEd25519().Wrap(),
|
||||||
sigSize: ed25519.SignatureSize,
|
sigSize: ed25519.SignatureSize,
|
||||||
sigType: TypeEd25519,
|
sigType: TypeEd25519,
|
||||||
sigName: NameEd25519,
|
sigName: NameEd25519,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
privKey: WrapPrivKey(GenPrivKeySecp256k1()),
|
privKey: GenPrivKeySecp256k1().Wrap(),
|
||||||
sigSize: 0, // unknown
|
sigSize: 0, // unknown
|
||||||
sigType: TypeSecp256k1,
|
sigType: TypeSecp256k1,
|
||||||
sigName: NameSecp256k1,
|
sigName: NameSecp256k1,
|
||||||
@ -119,10 +119,10 @@ func TestWrapping(t *testing.T) {
|
|||||||
|
|
||||||
// do some wrapping
|
// do some wrapping
|
||||||
pubs := []PubKey{
|
pubs := []PubKey{
|
||||||
WrapPubKey(nil),
|
PubKey{nil},
|
||||||
WrapPubKey(pub),
|
pub.Wrap(),
|
||||||
WrapPubKey(WrapPubKey(WrapPubKey(WrapPubKey(pub)))),
|
pub.Wrap().Wrap().Wrap(),
|
||||||
WrapPubKey(PubKey{PubKey{PubKey{pub}}}),
|
PubKey{PubKey{PubKey{pub}}}.Wrap(),
|
||||||
}
|
}
|
||||||
for _, p := range pubs {
|
for _, p := range pubs {
|
||||||
_, ok := p.PubKeyInner.(PubKey)
|
_, ok := p.PubKeyInner.(PubKey)
|
||||||
@ -130,10 +130,10 @@ func TestWrapping(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sigs := []Signature{
|
sigs := []Signature{
|
||||||
WrapSignature(nil),
|
Signature{nil},
|
||||||
WrapSignature(sig),
|
sig.Wrap(),
|
||||||
WrapSignature(WrapSignature(WrapSignature(WrapSignature(sig)))),
|
sig.Wrap().Wrap().Wrap(),
|
||||||
WrapSignature(Signature{Signature{Signature{sig}}}),
|
Signature{Signature{Signature{sig}}}.Wrap(),
|
||||||
}
|
}
|
||||||
for _, s := range sigs {
|
for _, s := range sigs {
|
||||||
_, ok := s.SignatureInner.(Signature)
|
_, ok := s.SignatureInner.(Signature)
|
||||||
|
Reference in New Issue
Block a user