diff --git a/account/priv_account.go b/account/priv_account.go index 23114893a..96367b356 100644 --- a/account/priv_account.go +++ b/account/priv_account.go @@ -34,7 +34,6 @@ func GenPrivAccountFromKey(privKeyBytes [64]byte) *PrivAccount { PubKey: pubKey, PrivKey: privKey, } - } func (privAccount *PrivAccount) Sign(o Signable) Signature { diff --git a/types/tx.go b/types/tx.go index ca98367e8..a36be2295 100644 --- a/types/tx.go +++ b/types/tx.go @@ -91,9 +91,7 @@ func (txIn *TxInput) ValidateBasic() error { } func (txIn *TxInput) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteByteSlice(txIn.Address, w, n, err) - binary.WriteUint64(txIn.Amount, w, n, err) - binary.WriteUvarint(txIn.Sequence, w, n, err) + binary.WriteTo([]byte(Fmt(`{"Address":"%X","Amount":%v,"Sequence":%v}`, txIn.Address, txIn.Amount, txIn.Sequence)), w, n, err) } func (txIn *TxInput) String() string { @@ -118,8 +116,7 @@ func (txOut *TxOutput) ValidateBasic() error { } func (txOut *TxOutput) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteByteSlice(txOut.Address, w, n, err) - binary.WriteUint64(txOut.Amount, w, n, err) + binary.WriteTo([]byte(Fmt(`{"Address":"%X","Amount":%v}`, txOut.Address, txOut.Amount)), w, n, err) } func (txOut *TxOutput) String() string { @@ -134,15 +131,23 @@ type SendTx struct { } func (tx *SendTx) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) - binary.WriteUvarint(uint(len(tx.Inputs)), w, n, err) - for _, in := range tx.Inputs { + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Tx":[%v,{"Inputs":[`, TxTypeSend)), w, n, err) + for i, in := range tx.Inputs { in.WriteSignBytes(w, n, err) + if i != len(tx.Inputs)-1 { + binary.WriteTo([]byte(","), w, n, err) + } } - binary.WriteUvarint(uint(len(tx.Outputs)), w, n, err) - for _, out := range tx.Outputs { + binary.WriteTo([]byte(`],"Outputs":[`), w, n, err) + for i, out := range tx.Outputs { out.WriteSignBytes(w, n, err) + if i != len(tx.Outputs)-1 { + binary.WriteTo([]byte(","), w, n, err) + } } + binary.WriteTo([]byte(`]}]}`), w, n, err) } func (tx *SendTx) String() string { @@ -160,12 +165,12 @@ type CallTx struct { } func (tx *CallTx) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Tx":[%v,{"Address":"%X","Data":"%X"`, TxTypeCall, tx.Address, tx.Data)), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Fee":%v,"GasLimit":%v,"Input":`, tx.Fee, tx.GasLimit)), w, n, err) tx.Input.WriteSignBytes(w, n, err) - binary.WriteByteSlice(tx.Address, w, n, err) - binary.WriteUint64(tx.GasLimit, w, n, err) - binary.WriteUint64(tx.Fee, w, n, err) - binary.WriteByteSlice(tx.Data, w, n, err) + binary.WriteTo([]byte(`}]}`), w, n, err) } func (tx *CallTx) String() string { @@ -181,16 +186,25 @@ type BondTx struct { } func (tx *BondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) - binary.WriteBinary(tx.PubKey, w, n, err) - binary.WriteUvarint(uint(len(tx.Inputs)), w, n, err) - for _, in := range tx.Inputs { + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Tx":[%v,{"Inputs":[`, TxTypeBond)), w, n, err) + for i, in := range tx.Inputs { in.WriteSignBytes(w, n, err) + if i != len(tx.Inputs)-1 { + binary.WriteTo([]byte(","), w, n, err) + } } - binary.WriteUvarint(uint(len(tx.UnbondTo)), w, n, err) - for _, out := range tx.UnbondTo { + binary.WriteTo([]byte(Fmt(`],"PubKey":`)), w, n, err) + binary.WriteTo(binary.JSONBytes(tx.PubKey), w, n, err) + binary.WriteTo([]byte(`,"UnbondTo":[`), w, n, err) + for i, out := range tx.UnbondTo { out.WriteSignBytes(w, n, err) + if i != len(tx.UnbondTo)-1 { + binary.WriteTo([]byte(","), w, n, err) + } } + binary.WriteTo([]byte(`]}]}`), w, n, err) } func (tx *BondTx) String() string { @@ -206,9 +220,9 @@ type UnbondTx struct { } func (tx *UnbondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) - binary.WriteByteSlice(tx.Address, w, n, err) - binary.WriteUvarint(tx.Height, w, n, err) + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Tx":[%v,{"Address":"%X","Height":%v}]}`, TxTypeUnbond, tx.Address, tx.Height)), w, n, err) } func (tx *UnbondTx) String() string { @@ -224,9 +238,9 @@ type RebondTx struct { } func (tx *RebondTx) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) - binary.WriteByteSlice(tx.Address, w, n, err) - binary.WriteUvarint(tx.Height, w, n, err) + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Tx":[%v,{"Address":"%X","Height":%v}]}`, TxTypeRebond, tx.Address, tx.Height)), w, n, err) } func (tx *RebondTx) String() string { diff --git a/types/tx_test.go b/types/tx_test.go new file mode 100644 index 000000000..aa66e3119 --- /dev/null +++ b/types/tx_test.go @@ -0,0 +1,121 @@ +package types + +import ( + "testing" + + "github.com/tendermint/tendermint/account" +) + +func TestSendTxSignable(t *testing.T) { + sendTx := &SendTx{ + Inputs: []*TxInput{ + &TxInput{ + Address: []byte("input1"), + Amount: 12345, + Sequence: 67890, + }, + &TxInput{ + Address: []byte("input2"), + Amount: 111, + Sequence: 222, + }, + }, + Outputs: []*TxOutput{ + &TxOutput{ + Address: []byte("output1"), + Amount: 333, + }, + &TxOutput{ + Address: []byte("output2"), + Amount: 444, + }, + }, + } + signBytes := account.SignBytes(sendTx) + signStr := string(signBytes) + expected := `{"Network":"74656E6465726D696E745F746573746E657433","Tx":[1,{"Inputs":[{"Address":"696E70757431","Amount":12345,"Sequence":67890},{"Address":"696E70757432","Amount":111,"Sequence":222}],"Outputs":[{"Address":"6F757470757431","Amount":333},{"Address":"6F757470757432","Amount":444}]}]}` + if signStr != expected { + t.Errorf("Got unexpected sign string for SendTx") + } +} + +func TestCallTxSignable(t *testing.T) { + callTx := &CallTx{ + Input: &TxInput{ + Address: []byte("input1"), + Amount: 12345, + Sequence: 67890, + }, + Address: []byte("contract1"), + GasLimit: 111, + Fee: 222, + Data: []byte("data1"), + } + signBytes := account.SignBytes(callTx) + signStr := string(signBytes) + expected := `{"Network":"74656E6465726D696E745F746573746E657433","Tx":[2,{"Address":"636F6E747261637431","Data":"6461746131","Fee":222,"GasLimit":111,"Input":{"Address":"696E70757431","Amount":12345,"Sequence":67890}}]}` + if signStr != expected { + t.Errorf("Got unexpected sign string for CallTx") + } +} + +func TestBondTxSignable(t *testing.T) { + privAccount := account.GenPrivAccountFromKey([64]byte{}) + bondTx := &BondTx{ + PubKey: privAccount.PubKey.(account.PubKeyEd25519), + Inputs: []*TxInput{ + &TxInput{ + Address: []byte("input1"), + Amount: 12345, + Sequence: 67890, + }, + &TxInput{ + Address: []byte("input2"), + Amount: 111, + Sequence: 222, + }, + }, + UnbondTo: []*TxOutput{ + &TxOutput{ + Address: []byte("output1"), + Amount: 333, + }, + &TxOutput{ + Address: []byte("output2"), + Amount: 444, + }, + }, + } + signBytes := account.SignBytes(bondTx) + signStr := string(signBytes) + expected := `{"Network":"74656E6465726D696E745F746573746E657433","Tx":[17,{"Inputs":[{"Address":"696E70757431","Amount":12345,"Sequence":67890},{"Address":"696E70757432","Amount":111,"Sequence":222}],"PubKey":[1,"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"],"UnbondTo":[{"Address":"6F757470757431","Amount":333},{"Address":"6F757470757432","Amount":444}]}]}` + if signStr != expected { + t.Errorf("Got unexpected sign string for BondTx") + } +} + +func TestUnbondTxSignable(t *testing.T) { + unbondTx := &UnbondTx{ + Address: []byte("address1"), + Height: 111, + } + signBytes := account.SignBytes(unbondTx) + signStr := string(signBytes) + expected := `{"Network":"74656E6465726D696E745F746573746E657433","Tx":[18,{"Address":"6164647265737331","Height":111}]}` + if signStr != expected { + t.Errorf("Got unexpected sign string for UnbondTx") + } +} + +func TestRebondTxSignable(t *testing.T) { + rebondTx := &RebondTx{ + Address: []byte("address1"), + Height: 111, + } + signBytes := account.SignBytes(rebondTx) + signStr := string(signBytes) + expected := `{"Network":"74656E6465726D696E745F746573746E657433","Tx":[19,{"Address":"6164647265737331","Height":111}]}` + if signStr != expected { + t.Errorf("Got unexpected sign string for RebondTx") + } +} diff --git a/types/vote.go b/types/vote.go index dc68012ba..e8658664c 100644 --- a/types/vote.go +++ b/types/vote.go @@ -47,12 +47,10 @@ const ( ) func (vote *Vote) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) - binary.WriteUvarint(vote.Height, w, n, err) - binary.WriteUvarint(vote.Round, w, n, err) - binary.WriteByte(vote.Type, w, n, err) - binary.WriteByteSlice(vote.BlockHash, w, n, err) - binary.WriteBinary(vote.BlockParts, w, n, err) + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Vote":{"BlockHash":"%X","BlockParts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err) + binary.WriteTo([]byte(Fmt(`,"Height":%v,"Round":%v,"Type":%v}}`, vote.Height, vote.Round, vote.Type)), w, n, err) } func (vote *Vote) Copy() *Vote {