2016-03-20 03:00:43 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2017-06-30 09:28:40 -07:00
|
|
|
"encoding/hex"
|
2017-03-23 17:51:50 -07:00
|
|
|
"fmt"
|
2016-03-20 03:00:43 -07:00
|
|
|
"testing"
|
|
|
|
|
2017-02-22 14:30:50 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-23 09:30:57 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-25 22:08:31 -07:00
|
|
|
data "github.com/tendermint/go-wire/data"
|
2016-03-20 03:00:43 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var chainID string = "test_chain"
|
|
|
|
|
|
|
|
func TestSendTxSignable(t *testing.T) {
|
|
|
|
sendTx := &SendTx{
|
2016-04-01 15:19:07 -07:00
|
|
|
Gas: 222,
|
2017-01-15 15:10:51 -08:00
|
|
|
Fee: Coin{"", 111},
|
2016-03-20 03:00:43 -07:00
|
|
|
Inputs: []TxInput{
|
|
|
|
TxInput{
|
|
|
|
Address: []byte("input1"),
|
2016-04-01 15:19:07 -07:00
|
|
|
Coins: Coins{{"", 12345}},
|
2016-03-20 03:00:43 -07:00
|
|
|
Sequence: 67890,
|
|
|
|
},
|
|
|
|
TxInput{
|
|
|
|
Address: []byte("input2"),
|
2016-04-01 15:19:07 -07:00
|
|
|
Coins: Coins{{"", 111}},
|
2016-03-20 03:00:43 -07:00
|
|
|
Sequence: 222,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outputs: []TxOutput{
|
|
|
|
TxOutput{
|
|
|
|
Address: []byte("output1"),
|
2016-04-01 15:19:07 -07:00
|
|
|
Coins: Coins{{"", 333}},
|
2016-03-20 03:00:43 -07:00
|
|
|
},
|
|
|
|
TxOutput{
|
|
|
|
Address: []byte("output2"),
|
2016-04-01 15:19:07 -07:00
|
|
|
Coins: Coins{{"", 444}},
|
2016-03-20 03:00:43 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
signBytes := sendTx.SignBytes(chainID)
|
2017-03-23 17:51:50 -07:00
|
|
|
signBytesHex := fmt.Sprintf("%X", signBytes)
|
2017-01-15 15:10:51 -08:00
|
|
|
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F01020106696E7075743101010000000000000030390301093200000106696E70757432010100000000000000006F01DE0000010201076F757470757431010100000000000000014D01076F75747075743201010000000000000001BC"
|
2017-02-22 14:30:50 -08:00
|
|
|
|
2017-03-23 17:51:50 -07:00
|
|
|
assert.Equal(t, signBytesHex, expected,
|
2017-04-17 16:53:06 -07:00
|
|
|
"Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
|
2016-03-20 03:00:43 -07:00
|
|
|
}
|
|
|
|
|
2016-03-27 12:47:50 -07:00
|
|
|
func TestAppTxSignable(t *testing.T) {
|
|
|
|
callTx := &AppTx{
|
2016-04-01 15:19:07 -07:00
|
|
|
Gas: 222,
|
2017-01-15 15:10:51 -08:00
|
|
|
Fee: Coin{"", 111},
|
2017-01-13 16:10:22 -08:00
|
|
|
Name: "X",
|
2016-03-20 03:00:43 -07:00
|
|
|
Input: TxInput{
|
|
|
|
Address: []byte("input1"),
|
2016-04-01 15:19:07 -07:00
|
|
|
Coins: Coins{{"", 12345}},
|
2016-03-20 03:00:43 -07:00
|
|
|
Sequence: 67890,
|
|
|
|
},
|
2016-03-27 12:47:50 -07:00
|
|
|
Data: []byte("data1"),
|
2016-03-20 03:00:43 -07:00
|
|
|
}
|
|
|
|
signBytes := callTx.SignBytes(chainID)
|
2017-03-23 17:51:50 -07:00
|
|
|
signBytesHex := fmt.Sprintf("%X", signBytes)
|
2017-01-15 15:10:51 -08:00
|
|
|
expected := "010A746573745F636861696E0100000000000000DE00000000000000006F0101580106696E70757431010100000000000000303903010932000001056461746131"
|
2017-02-22 14:30:50 -08:00
|
|
|
|
2017-03-23 17:51:50 -07:00
|
|
|
assert.Equal(t, signBytesHex, expected,
|
2017-04-17 16:53:06 -07:00
|
|
|
"Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signBytesHex)
|
2017-02-23 09:30:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendTxJSON(t *testing.T) {
|
2017-03-23 17:51:50 -07:00
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
2017-02-23 09:30:57 -08:00
|
|
|
chainID := "test_chain_id"
|
2017-02-24 13:24:01 -08:00
|
|
|
test1PrivAcc := PrivAccountFromSecret("sendtx1")
|
|
|
|
test2PrivAcc := PrivAccountFromSecret("sendtx2")
|
2017-02-23 09:30:57 -08:00
|
|
|
|
|
|
|
// 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})
|
2017-03-23 17:51:50 -07:00
|
|
|
require.Nil(err)
|
2017-02-23 09:30:57 -08:00
|
|
|
// fmt.Println(string(js))
|
|
|
|
txs := TxS{}
|
|
|
|
err = data.FromJSON(js, &txs)
|
2017-03-23 17:51:50 -07:00
|
|
|
require.Nil(err)
|
2017-02-23 09:30:57 -08:00
|
|
|
tx2, ok := txs.Tx.(*SendTx)
|
2017-03-23 17:51:50 -07:00
|
|
|
require.True(ok)
|
2017-02-23 09:30:57 -08:00
|
|
|
|
|
|
|
// make sure they are the same!
|
|
|
|
signBytes := tx.SignBytes(chainID)
|
|
|
|
signBytes2 := tx2.SignBytes(chainID)
|
2017-03-23 17:51:50 -07:00
|
|
|
assert.Equal(signBytes, signBytes2)
|
|
|
|
assert.Equal(tx, tx2)
|
2017-02-23 09:30:57 -08:00
|
|
|
|
|
|
|
// sign this thing
|
|
|
|
sig := test1PrivAcc.Sign(signBytes)
|
|
|
|
// we handle both raw sig and wrapped sig the same
|
|
|
|
tx.SetSignature(test1PrivAcc.PubKey.Address(), sig)
|
2017-04-10 07:32:30 -07:00
|
|
|
tx2.SetSignature(test1PrivAcc.PubKey.Address(), sig)
|
2017-03-23 17:51:50 -07:00
|
|
|
assert.Equal(tx, tx2)
|
2017-02-23 09:30:57 -08:00
|
|
|
|
|
|
|
// let's marshal / unmarshal this with signature
|
|
|
|
js, err = data.ToJSON(TxS{tx})
|
2017-03-23 17:51:50 -07:00
|
|
|
require.Nil(err)
|
2017-02-23 09:30:57 -08:00
|
|
|
// fmt.Println(string(js))
|
|
|
|
err = data.FromJSON(js, &txs)
|
2017-03-23 17:51:50 -07:00
|
|
|
require.Nil(err)
|
2017-02-23 09:30:57 -08:00
|
|
|
tx2, ok = txs.Tx.(*SendTx)
|
2017-03-23 17:51:50 -07:00
|
|
|
require.True(ok)
|
2017-02-23 09:30:57 -08:00
|
|
|
|
|
|
|
// and make sure the sig is preserved
|
2017-03-23 17:51:50 -07:00
|
|
|
assert.Equal(tx, tx2)
|
|
|
|
assert.False(tx2.Inputs[0].Signature.Empty())
|
2016-03-20 03:00:43 -07:00
|
|
|
}
|
2017-06-30 09:28:40 -07:00
|
|
|
|
|
|
|
func TestSendTxIBC(t *testing.T) {
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
|
|
|
good, err := hex.DecodeString("1960CA7E170862837AA8F22A947194F41F61860B")
|
|
|
|
require.Nil(err)
|
|
|
|
short, err := hex.DecodeString("1960CA7E170862837AA8F22F947194F41F610B")
|
|
|
|
require.Nil(err)
|
|
|
|
long, err := hex.DecodeString("1960CA7E170862837AA8F22A947194F41F6186120B")
|
|
|
|
require.Nil(err)
|
|
|
|
slash, err := hex.DecodeString("F40ECECEA86F29D0FDF2980EF72F1708687BD4BF")
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
coins := Coins{{"atom", 5}}
|
|
|
|
|
|
|
|
addrs := []struct {
|
|
|
|
addr []byte
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{good, true},
|
|
|
|
{slash, true},
|
|
|
|
{long, false},
|
|
|
|
{short, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixes := []struct {
|
|
|
|
prefix []byte
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{nil, true},
|
|
|
|
{[]byte("chain-1/"), true},
|
|
|
|
{[]byte("chain/with/paths/"), false},
|
|
|
|
{[]byte("no-slash-here"), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range addrs {
|
|
|
|
for j, pc := range prefixes {
|
|
|
|
addr := append(pc.prefix, tc.addr...)
|
|
|
|
output := TxOutput{Address: addr, Coins: coins}
|
|
|
|
res := output.ValidateBasic()
|
|
|
|
|
|
|
|
if tc.valid && pc.valid {
|
|
|
|
assert.True(res.IsOK(), "%d,%d: %s", i, j, res.Log)
|
|
|
|
} else {
|
|
|
|
assert.False(res.IsOK(), "%d,%d: %s", i, j, res.Log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|