Browse Source

type Ed25519[Signature|PubKey] struct{[]byte} -> []byte

pull/9/head
Jae Kwon 10 years ago
parent
commit
7a8a0fefc7
11 changed files with 72 additions and 29 deletions
  1. +1
    -3
      account/priv_account.go
  2. +1
    -1
      account/privkey.go
  3. +7
    -8
      account/pubkey.go
  4. +4
    -6
      account/signature.go
  5. +41
    -2
      account/signature_test.go
  6. +1
    -1
      block/block.go
  7. +4
    -2
      cmd/gen_tx.go
  8. +2
    -2
      consensus/pol_test.go
  9. +9
    -2
      rpc/mempool.go
  10. +1
    -1
      state/priv_validator.go
  11. +1
    -1
      state/validator_set_test.go

+ 1
- 3
account/priv_account.go View File

@ -16,9 +16,7 @@ func GenPrivAccount() *PrivAccount {
privKey := CRandBytes(32)
pubKey := ed25519.MakePubKey(privKey)
return &PrivAccount{
PubKeyEd25519{
PubKey: pubKey,
},
PubKeyEd25519(pubKey),
PrivKeyEd25519{
PubKey: pubKey,
PrivKey: privKey,


+ 1
- 1
account/privkey.go View File

@ -59,5 +59,5 @@ func (key PrivKeyEd25519) ValidateBasic() error {
func (key PrivKeyEd25519) Sign(msg []byte) Signature {
signature := ed25519.SignMessage(msg, key.PrivKey, key.PubKey)
return SignatureEd25519{signature}
return SignatureEd25519(signature)
}

+ 7
- 8
account/pubkey.go View File

@ -19,7 +19,7 @@ type PubKey interface {
// Types of PubKey implementations
const (
PubKeyTypeNil = byte(0x00)
PubKeyTypeEd25519 = byte(0x02)
PubKeyTypeEd25519 = byte(0x01)
)
//-------------------------------------
@ -60,16 +60,15 @@ func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
//-------------------------------------
// Implements PubKey
type PubKeyEd25519 struct {
PubKey []byte
}
type PubKeyEd25519 []byte
func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160(key.PubKey) }
// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160([]byte(key)) }
func (key PubKeyEd25519) ValidateBasic() error {
if len(key.PubKey) != ed25519.PublicKeySize {
if len(key) != ed25519.PublicKeySize {
return errors.New("Invalid PubKeyEd25519 key size")
}
return nil
@ -82,8 +81,8 @@ func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
}
v1 := &ed25519.Verify{
Message: msg,
PubKey: key.PubKey,
Signature: []byte(sig.Bytes),
PubKey: key,
Signature: sig,
}
return ed25519.VerifyBatch([]*ed25519.Verify{v1})
}

+ 4
- 6
account/signature.go View File

@ -40,23 +40,21 @@ var _ = RegisterType(&TypeInfo{
//-------------------------------------
// Implements Signature
type SignatureEd25519 struct {
Bytes []byte
}
type SignatureEd25519 []byte
func (sig SignatureEd25519) TypeByte() byte { return SignatureTypeEd25519 }
func (sig SignatureEd25519) ValidateBasic() error {
if len(sig.Bytes) != ed25519.SignatureSize {
if len(sig) != ed25519.SignatureSize {
return errors.New("Invalid SignatureEd25519 signature size")
}
return nil
}
func (sig SignatureEd25519) IsZero() bool {
return len(sig.Bytes) == 0
return len(sig) == 0
}
func (sig SignatureEd25519) String() string {
return fmt.Sprintf("%X", Fingerprint(sig.Bytes))
return fmt.Sprintf("%X", Fingerprint(sig))
}

+ 41
- 2
account/signature_test.go View File

@ -1,8 +1,12 @@
package account
import (
. "github.com/tendermint/tendermint/common"
"bytes"
"testing"
ed25519 "github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
)
func TestSignAndValidate(t *testing.T) {
@ -21,9 +25,44 @@ func TestSignAndValidate(t *testing.T) {
}
// Mutate the signature, just one bit.
sig.(SignatureEd25519).Bytes[0] ^= byte(0x01)
sig.(SignatureEd25519)[0] ^= byte(0x01)
if pubKey.VerifyBytes(msg, sig) {
t.Errorf("Account message signature verification should have failed but passed instead")
}
}
func TestBinaryDecode(t *testing.T) {
privAccount := GenPrivAccount()
pubKey := privAccount.PubKey
privKey := privAccount.PrivKey
msg := CRandBytes(128)
sig := privKey.Sign(msg)
t.Logf("msg: %X, sig: %X", msg, sig)
buf, n, err := new(bytes.Buffer), new(int64), new(error)
WriteBinary(sig, buf, n, err)
if *err != nil {
t.Fatalf("Failed to write Signature: %v", err)
}
if len(buf.Bytes()) != ed25519.SignatureSize+2 {
// 1 byte TypeByte, 1 byte length, 64 bytes signature bytes
t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
}
if buf.Bytes()[0] != SignatureTypeEd25519 {
t.Fatalf("Unexpected signature type byte")
}
sig2, ok := ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
if !ok || *err != nil {
t.Fatalf("Failed to read Signature: %v", err)
}
// Test the signature
if !pubKey.VerifyBytes(msg, sig2) {
t.Errorf("Account message signature verification failed")
}
}

+ 1
- 1
block/block.go View File

@ -169,7 +169,7 @@ func (commit Commit) IsZero() bool {
}
func (commit Commit) String() string {
return fmt.Sprintf("Commit{A:%X R:%v %X}", commit.Address, commit.Round, Fingerprint(commit.Signature.Bytes))
return fmt.Sprintf("Commit{A:%X R:%v %X}", commit.Address, commit.Round, Fingerprint(commit.Signature))
}
//-------------------------------------


+ 4
- 2
cmd/gen_tx.go View File

@ -59,10 +59,11 @@ func gen_tx() {
// Get source pubkey
srcPubKeyBytes := getByteSliceFromBase64("Enter source pubkey: ")
r, n, err := bytes.NewReader(srcPubKeyBytes), new(int64), new(error)
srcPubKey := account_.PubKeyDecoder(r, n, err).(account_.PubKey)
srcPubKey_ := account_.PubKeyDecoder(r, n, err)
if *err != nil {
Exit(Fmt("Invalid PubKey. Error: %v", err))
}
srcPubKey := srcPubKey_.(account_.PubKey)
// Get the state of the account.
var srcAccount *account_.Account
@ -113,10 +114,11 @@ func gen_tx() {
// Get source privkey (for signing)
srcPrivKeyBytes := getByteSliceFromBase64("Enter source privkey (for signing): ")
r, n, err = bytes.NewReader(srcPrivKeyBytes), new(int64), new(error)
srcPrivKey := account_.PrivKeyDecoder(r, n, err).(account_.PrivKey)
srcPrivKey_ := account_.PrivKeyDecoder(r, n, err)
if *err != nil {
Exit(Fmt("Invalid PrivKey. Error: %v", err))
}
srcPrivKey := srcPrivKey_.(account_.PrivKey)
// Sign
tx.Inputs[0].Signature = srcPrivKey.Sign(account_.SignBytes(tx))


+ 2
- 2
consensus/pol_test.go View File

@ -72,7 +72,7 @@ func TestVerifyInvalidVote(t *testing.T) {
}
for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Signature.Bytes[0] += byte(0x01) // mutated!
polVoteSig.Signature[0] += byte(0x01) // mutated!
}
// Check that validation fails.
@ -119,7 +119,7 @@ func TestVerifyInvalidCommits(t *testing.T) {
}
for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Signature.Bytes[0] += byte(0x01)
polVoteSig.Signature[0] += byte(0x01)
}
// Check that validation fails.


+ 9
- 2
rpc/mempool.go View File

@ -2,6 +2,8 @@ package rpc
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/tendermint/tendermint/block"
@ -21,7 +23,6 @@ func MempoolHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
ReturnJSON(API_INVALID_PARAM, Fmt("Invalid tx_bytes: %v", err))
}
// XXX Oops, I need to cast later like this, everywhere.
tx := tx_.(block.Tx)
err = mempoolReactor.BroadcastTx(tx)
@ -29,9 +30,15 @@ func MempoolHandler(w http.ResponseWriter, r *http.Request) {
ReturnJSON(API_ERROR, Fmt("Error broadcasting transaction: %v", err))
}
jsonBytes, err := json.MarshalIndent(tx, "", " ")
if err != nil {
panic(err)
}
fmt.Println(">>", string(jsonBytes))
ReturnJSON(API_OK, Fmt("Broadcasted tx: %X", tx))
}
/*
curl -H 'content-type: text/plain;' http://127.0.0.1:8888/mempool?tx_bytes=0101146070FF17C39B2B0A64CA2BC431328037FA0F47606400000000000000000140D209A7CD4E2E7C5E4B17815AB93029960AF66D3428DE7B085EBDBACD84A31F58562EFF0AC4EC7151B071DE82417110C94FFEE862A3740624D7A8C1874AFCF50402206BD490C212E701A2136EEEA04F06FA4F287EE47E2B7A9B5D62EDD84CD6AD975301146070FF17C39B2B0A64CA2BC431328037FA0F47FF6400000000000000
curl -H 'content-type: text/plain;' http://127.0.0.1:8888/mempool?tx_bytes=0101146070FF17C39B2B0A64CA2BC431328037FA0F4760640000000000000001014025BE0AD9344DA24BC12FCB84903F88AB9B82107F414A0B570048CDEF7EDDD5AC96B34F0405B7A75B761447F8E6C899343F1EDB160B4C4FAAE92D5881D0023A0701206BD490C212E701A2136EEEA04F06FA4F287EE47E2B7A9B5D62EDD84CD6AD975301146070FF17C39B2B0A64CA2BC431328037FA0F47FF6400000000000000
*/

+ 1
- 1
state/priv_validator.go View File

@ -61,7 +61,7 @@ type PrivValidator struct {
func GenPrivValidator() *PrivValidator {
privKeyBytes := CRandBytes(32)
pubKeyBytes := ed25519.MakePubKey(privKeyBytes)
pubKey := PubKeyEd25519{pubKeyBytes}
pubKey := PubKeyEd25519(pubKeyBytes)
privKey := PrivKeyEd25519{pubKeyBytes, privKeyBytes}
return &PrivValidator{
Address: pubKey.Address(),


+ 1
- 1
state/validator_set_test.go View File

@ -11,7 +11,7 @@ import (
func randValidator_() *Validator {
return &Validator{
Address: RandBytes(20),
PubKey: PubKeyEd25519{RandBytes(64)},
PubKey: PubKeyEd25519(RandBytes(64)),
BondHeight: uint(RandUint32()),
VotingPower: RandUint64(),
Accum: int64(RandUint64()),


Loading…
Cancel
Save