Proper json marshalling/unmarshalling of sendtx with or w/o sig

This commit is contained in:
Ethan Frey 2017-02-23 18:30:57 +01:00
parent c1fa8cb0d2
commit 4ad645f318
4 changed files with 87 additions and 13 deletions

4
glide.lock generated
View File

@ -65,9 +65,9 @@ imports:
- name: github.com/tendermint/go-config
version: e64b424499acd0eb9856b88e10c0dff41628c0d6
- name: github.com/tendermint/go-crypto
version: b6a2c5949f7ea1d064cbb9dc638eb0a3dca9af34
version: 8c9b889ccfe1f891ce8ab36c843f15794ce8f30f
- name: github.com/tendermint/go-data
version: 35a95d275fa845b635e1208c3dd65b19c4eda811
version: f199ef165cd5a50d569b179201702c5ec8899013
- name: github.com/tendermint/go-db
version: 2645626c33d8702739e52a61a55d705c2dfe4530
- name: github.com/tendermint/go-events

View File

@ -6,13 +6,9 @@ import (
"github.com/stretchr/testify/assert"
)
func TestAccount(t *testing.T) {
func TestNilAccount(t *testing.T) {
acc := Account{
PubKey: nil,
Sequence: 0,
Balance: nil,
}
acc := Account{}
//test Copy
accCopy := acc.Copy()

View File

@ -163,9 +163,13 @@ func (tx *SendTx) SignBytes(chainID string) []byte {
}
func (tx *SendTx) SetSignature(addr []byte, sig crypto.Signature) bool {
sigs, ok := sig.(crypto.SignatureS)
if !ok {
sigs = crypto.SignatureS{sig}
}
for i, input := range tx.Inputs {
if bytes.Equal(input.Address, addr) {
tx.Inputs[i].Signature.Signature = sig
tx.Inputs[i].Signature = sigs
return true
}
}
@ -196,7 +200,11 @@ func (tx *AppTx) SignBytes(chainID string) []byte {
}
func (tx *AppTx) SetSignature(sig crypto.Signature) bool {
tx.Input.Signature.Signature = sig
sigs, ok := sig.(crypto.SignatureS)
if !ok {
sigs = crypto.SignatureS{sig}
}
tx.Input.Signature = sigs
return true
}

View File

@ -3,9 +3,11 @@ package types
import (
"testing"
cmn "github.com/tendermint/go-common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-data"
)
var chainID string = "test_chain"
@ -62,5 +64,73 @@ func TestAppTxSignable(t *testing.T) {
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131"
assert.True(t, signBytesHex == expected,
cmn.Fmt("Got unexpected sign string for AppTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
cmn.Fmt("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex))
}
// d'oh, can't use the version in testutils due to circular imports :(
func makePrivAcct() PrivAccount {
privKey := crypto.PrivKeyS{crypto.GenPrivKeyEd25519()}
return PrivAccount{
PrivKeyS: privKey,
Account: Account{
PubKey: crypto.PubKeyS{privKey.PubKey()},
},
}
}
func TestSendTxJSON(t *testing.T) {
chainID := "test_chain_id"
test1PrivAcc := makePrivAcct()
test2PrivAcc := makePrivAcct()
// Construct a SendTx signature
tx := &SendTx{
Gas: 1,
Fee: Coin{"foo", 2},
Inputs: []TxInput{
NewTxInput(test1PrivAcc.PubKey, Coins{{"foo", 10}}, 1),
},
Outputs: []TxOutput{
TxOutput{
Address: test2PrivAcc.PubKey.Address(),
Coins: Coins{{"foo", 8}},
},
},
}
// serialize this as json and back
js, err := data.ToJSON(TxS{tx})
require.Nil(t, err)
// fmt.Println(string(js))
txs := TxS{}
err = data.FromJSON(js, &txs)
require.Nil(t, err)
tx2, ok := txs.Tx.(*SendTx)
require.True(t, ok)
// make sure they are the same!
signBytes := tx.SignBytes(chainID)
signBytes2 := tx2.SignBytes(chainID)
assert.Equal(t, signBytes, signBytes2)
assert.Equal(t, tx, tx2)
// sign this thing
sig := test1PrivAcc.Sign(signBytes)
// we handle both raw sig and wrapped sig the same
tx.SetSignature(test1PrivAcc.PubKey.Address(), sig)
tx2.SetSignature(test1PrivAcc.PubKey.Address(), crypto.SignatureS{sig})
assert.Equal(t, tx, tx2)
// let's marshal / unmarshal this with signature
js, err = data.ToJSON(TxS{tx})
require.Nil(t, err)
// fmt.Println(string(js))
err = data.FromJSON(js, &txs)
require.Nil(t, err)
tx2, ok = txs.Tx.(*SendTx)
require.True(t, ok)
// and make sure the sig is preserved
assert.Equal(t, tx, tx2)
assert.False(t, tx2.Inputs[0].Signature.Empty())
}