mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Add structs for go-data support
This commit is contained in:
parent
4b11d62bdb
commit
e6d35ee641
9
Makefile
Normal file
9
Makefile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
.PHONY: docs
|
||||||
|
REPO:=github.com/tendermint/go-crypto
|
||||||
|
|
||||||
|
docs:
|
||||||
|
@go get github.com/davecheney/godoc2md
|
||||||
|
godoc2md $(REPO) > README.md
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test ./...
|
56
priv_key.go
56
priv_key.go
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/tendermint/ed25519"
|
"github.com/tendermint/ed25519"
|
||||||
"github.com/tendermint/ed25519/extra25519"
|
"github.com/tendermint/ed25519/extra25519"
|
||||||
. "github.com/tendermint/go-common"
|
. "github.com/tendermint/go-common"
|
||||||
|
data "github.com/tendermint/go-data"
|
||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +23,35 @@ type PrivKey interface {
|
|||||||
const (
|
const (
|
||||||
PrivKeyTypeEd25519 = byte(0x01)
|
PrivKeyTypeEd25519 = byte(0x01)
|
||||||
PrivKeyTypeSecp256k1 = byte(0x02)
|
PrivKeyTypeSecp256k1 = byte(0x02)
|
||||||
|
PrivKeyNameEd25519 = "ed25519"
|
||||||
|
PrivKeyNameSecp256k1 = "secp256k1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// for wire.readReflect
|
var privKeyMapper data.Mapper
|
||||||
var _ = wire.RegisterInterface(
|
|
||||||
struct{ PrivKey }{},
|
// register both private key types with go-data (and thus go-wire)
|
||||||
wire.ConcreteType{PrivKeyEd25519{}, PrivKeyTypeEd25519},
|
func init() {
|
||||||
wire.ConcreteType{PrivKeySecp256k1{}, PrivKeyTypeSecp256k1},
|
privKeyMapper = data.NewMapper(PrivKeyS{}).
|
||||||
)
|
RegisterInterface(PrivKeyEd25519{}, PrivKeyNameEd25519, PrivKeyTypeEd25519).
|
||||||
|
RegisterInterface(PrivKeySecp256k1{}, PrivKeyNameSecp256k1, PrivKeyTypeSecp256k1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivKeyS add json serialization to PrivKey
|
||||||
|
type PrivKeyS struct {
|
||||||
|
PrivKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PrivKeyS) MarshalJSON() ([]byte, error) {
|
||||||
|
return privKeyMapper.ToJSON(p.PrivKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PrivKeyS) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
parsed, err := privKeyMapper.FromJSON(data)
|
||||||
|
if err == nil {
|
||||||
|
p.PrivKey = parsed.(PrivKey)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
|
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
|
||||||
err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
|
err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
|
||||||
@ -64,6 +86,17 @@ func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
|
||||||
|
return data.Encoder.Marshal(p[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
|
||||||
|
var ref []byte
|
||||||
|
err := data.Encoder.Unmarshal(&ref, enc)
|
||||||
|
copy(p[:], ref)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
|
func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
|
||||||
keyCurve25519 := new([32]byte)
|
keyCurve25519 := new([32]byte)
|
||||||
privKeyBytes := [64]byte(privKey)
|
privKeyBytes := [64]byte(privKey)
|
||||||
@ -136,6 +169,17 @@ func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error) {
|
||||||
|
return data.Encoder.Marshal(p[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error {
|
||||||
|
var ref []byte
|
||||||
|
err := data.Encoder.Unmarshal(&ref, enc)
|
||||||
|
copy(p[:], ref)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (privKey PrivKeySecp256k1) String() string {
|
func (privKey PrivKeySecp256k1) String() string {
|
||||||
return Fmt("PrivKeySecp256k1{*****}")
|
return Fmt("PrivKeySecp256k1{*****}")
|
||||||
}
|
}
|
||||||
|
56
pub_key.go
56
pub_key.go
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/tendermint/ed25519"
|
"github.com/tendermint/ed25519"
|
||||||
"github.com/tendermint/ed25519/extra25519"
|
"github.com/tendermint/ed25519/extra25519"
|
||||||
. "github.com/tendermint/go-common"
|
. "github.com/tendermint/go-common"
|
||||||
|
data "github.com/tendermint/go-data"
|
||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
"golang.org/x/crypto/ripemd160"
|
"golang.org/x/crypto/ripemd160"
|
||||||
)
|
)
|
||||||
@ -24,14 +25,35 @@ type PubKey interface {
|
|||||||
const (
|
const (
|
||||||
PubKeyTypeEd25519 = byte(0x01)
|
PubKeyTypeEd25519 = byte(0x01)
|
||||||
PubKeyTypeSecp256k1 = byte(0x02)
|
PubKeyTypeSecp256k1 = byte(0x02)
|
||||||
|
PubKeyNameEd25519 = "ed25519"
|
||||||
|
PubKeyNameSecp256k1 = "secp256k1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// for wire.readReflect
|
var pubKeyMapper data.Mapper
|
||||||
var _ = wire.RegisterInterface(
|
|
||||||
struct{ PubKey }{},
|
// register both public key types with go-data (and thus go-wire)
|
||||||
wire.ConcreteType{PubKeyEd25519{}, PubKeyTypeEd25519},
|
func init() {
|
||||||
wire.ConcreteType{PubKeySecp256k1{}, PubKeyTypeSecp256k1},
|
pubKeyMapper = data.NewMapper(PubKeyS{}).
|
||||||
)
|
RegisterInterface(PubKeyEd25519{}, PubKeyNameEd25519, PubKeyTypeEd25519).
|
||||||
|
RegisterInterface(PubKeySecp256k1{}, PubKeyNameSecp256k1, PubKeyTypeSecp256k1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PubKeyS add json serialization to PubKey
|
||||||
|
type PubKeyS struct {
|
||||||
|
PubKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PubKeyS) MarshalJSON() ([]byte, error) {
|
||||||
|
return pubKeyMapper.ToJSON(p.PubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
parsed, err := pubKeyMapper.FromJSON(data)
|
||||||
|
if err == nil {
|
||||||
|
p.PubKey = parsed.(PubKey)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
|
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
|
||||||
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
|
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
|
||||||
@ -70,6 +92,17 @@ func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
|
|||||||
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
|
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
|
||||||
|
return data.Encoder.Marshal(p[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
|
||||||
|
var ref []byte
|
||||||
|
err := data.Encoder.Unmarshal(&ref, enc)
|
||||||
|
copy(p[:], ref)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// For use with golang/crypto/nacl/box
|
// For use with golang/crypto/nacl/box
|
||||||
// If error, returns nil.
|
// If error, returns nil.
|
||||||
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
|
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
|
||||||
@ -137,6 +170,17 @@ func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
|
|||||||
return sig__.Verify(Sha256(msg), pub__)
|
return sig__.Verify(Sha256(msg), pub__)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
|
||||||
|
return data.Encoder.Marshal(p[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
|
||||||
|
var ref []byte
|
||||||
|
err := data.Encoder.Unmarshal(&ref, enc)
|
||||||
|
copy(p[:], ref)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (pubKey PubKeySecp256k1) String() string {
|
func (pubKey PubKeySecp256k1) String() string {
|
||||||
return Fmt("PubKeySecp256k1{%X}", pubKey[:])
|
return Fmt("PubKeySecp256k1{%X}", pubKey[:])
|
||||||
}
|
}
|
||||||
|
47
signature.go
47
signature.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
. "github.com/tendermint/go-common"
|
. "github.com/tendermint/go-common"
|
||||||
|
data "github.com/tendermint/go-data"
|
||||||
"github.com/tendermint/go-wire"
|
"github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,14 +21,35 @@ type Signature interface {
|
|||||||
const (
|
const (
|
||||||
SignatureTypeEd25519 = byte(0x01)
|
SignatureTypeEd25519 = byte(0x01)
|
||||||
SignatureTypeSecp256k1 = byte(0x02)
|
SignatureTypeSecp256k1 = byte(0x02)
|
||||||
|
SignatureNameEd25519 = "ed25519"
|
||||||
|
SignatureNameSecp256k1 = "secp256k1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// for wire.readReflect
|
var sigMapper data.Mapper
|
||||||
var _ = wire.RegisterInterface(
|
|
||||||
struct{ Signature }{},
|
// register both public key types with go-data (and thus go-wire)
|
||||||
wire.ConcreteType{SignatureEd25519{}, SignatureTypeEd25519},
|
func init() {
|
||||||
wire.ConcreteType{SignatureSecp256k1{}, SignatureTypeSecp256k1},
|
sigMapper = data.NewMapper(SignatureS{}).
|
||||||
)
|
RegisterInterface(SignatureEd25519{}, SignatureNameEd25519, SignatureTypeEd25519).
|
||||||
|
RegisterInterface(SignatureSecp256k1{}, SignatureNameSecp256k1, SignatureTypeSecp256k1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignatureS add json serialization to Signature
|
||||||
|
type SignatureS struct {
|
||||||
|
Signature
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p SignatureS) MarshalJSON() ([]byte, error) {
|
||||||
|
return sigMapper.ToJSON(p.Signature)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
parsed, err := sigMapper.FromJSON(data)
|
||||||
|
if err == nil {
|
||||||
|
p.Signature = parsed.(Signature)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
|
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
|
||||||
err = wire.ReadBinaryBytes(sigBytes, &sig)
|
err = wire.ReadBinaryBytes(sigBytes, &sig)
|
||||||
@ -55,10 +77,21 @@ func (sig SignatureEd25519) Equals(other Signature) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p SignatureEd25519) MarshalJSON() ([]byte, error) {
|
||||||
|
return data.Encoder.Marshal(p[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
|
||||||
|
var ref []byte
|
||||||
|
err := data.Encoder.Unmarshal(&ref, enc)
|
||||||
|
copy(p[:], ref)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
|
|
||||||
// Implements Signature
|
// Implements Signature
|
||||||
type SignatureSecp256k1 []byte
|
type SignatureSecp256k1 data.Bytes
|
||||||
|
|
||||||
func (sig SignatureSecp256k1) Bytes() []byte {
|
func (sig SignatureSecp256k1) Bytes() []byte {
|
||||||
return wire.BinaryBytes(struct{ Signature }{sig})
|
return wire.BinaryBytes(struct{ Signature }{sig})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user