Browse Source

SNativeTx sign bytes

pull/119/head
Ethan Buchman 9 years ago
parent
commit
d528d002fa
3 changed files with 62 additions and 19 deletions
  1. +15
    -15
      permission/types/snatives.go
  2. +4
    -4
      types/tx.go
  3. +43
    -0
      types/tx_test.go

+ 15
- 15
permission/types/snatives.go View File

@ -64,8 +64,8 @@ var _ = binary.RegisterInterface(
)
type HasBaseArgs struct {
Address []byte
Permission PermFlag
Address []byte `json:"address"`
Permission PermFlag `json:"permission"`
}
func (*HasBaseArgs) PermFlag() PermFlag {
@ -73,9 +73,9 @@ func (*HasBaseArgs) PermFlag() PermFlag {
}
type SetBaseArgs struct {
Address []byte
Permission PermFlag
Value bool
Address []byte `json:"address"`
Permission PermFlag `json:"permission"`
Value bool `json:"value"`
}
func (*SetBaseArgs) PermFlag() PermFlag {
@ -83,8 +83,8 @@ func (*SetBaseArgs) PermFlag() PermFlag {
}
type UnsetBaseArgs struct {
Address []byte
Permission PermFlag
Address []byte `json:"address"`
Permission PermFlag `json:"permission"`
}
func (*UnsetBaseArgs) PermFlag() PermFlag {
@ -92,8 +92,8 @@ func (*UnsetBaseArgs) PermFlag() PermFlag {
}
type SetGlobalArgs struct {
Permission PermFlag
Value bool
Permission PermFlag `json:"permission"`
Value bool `json:"value"`
}
func (*SetGlobalArgs) PermFlag() PermFlag {
@ -101,8 +101,8 @@ func (*SetGlobalArgs) PermFlag() PermFlag {
}
type HasRoleArgs struct {
Address []byte
Role string
Address []byte `json:"address"`
Role string `json:"role"`
}
func (*HasRoleArgs) PermFlag() PermFlag {
@ -110,8 +110,8 @@ func (*HasRoleArgs) PermFlag() PermFlag {
}
type AddRoleArgs struct {
Address []byte
Role string
Address []byte `json:"address"`
Role string `json:"role"`
}
func (*AddRoleArgs) PermFlag() PermFlag {
@ -119,8 +119,8 @@ func (*AddRoleArgs) PermFlag() PermFlag {
}
type RmRoleArgs struct {
Address []byte
Role string
Address []byte `json:"address"`
Role string `json:"role"`
}
func (*RmRoleArgs) PermFlag() PermFlag {


+ 4
- 4
types/tx.go View File

@ -328,13 +328,13 @@ type SNativeTx struct {
SNative ptypes.SNativeArgs `json:"snative"`
}
// TODO: check the tx.SNative encoding ...
func (tx *SNativeTx) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
binary.WriteTo([]byte(Fmt(`{"chain_id":%s`, jsonEscape(chainID))), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"args":%v`, TxTypeSNative, tx.SNative)), w, n, err)
binary.WriteTo([]byte(`,"input":`), w, n, err)
binary.WriteTo([]byte(Fmt(`,"tx":[%v,{"args":"`, TxTypeSNative)), w, n, err)
binary.WriteJSON(tx.SNative, w, n, err)
binary.WriteTo([]byte(`","input":`), w, n, err)
tx.Input.WriteSignBytes(w, n, err)
binary.WriteTo([]byte(Fmt(`,"snative":%s`, tx.SNative)), w, n, err)
binary.WriteTo([]byte(Fmt(`,"snative":%s`, jsonEscape(ptypes.PermFlagToString(tx.SNative.PermFlag())))), w, n, err)
binary.WriteTo([]byte(`}]}`), w, n, err)
}


+ 43
- 0
types/tx_test.go View File

@ -6,6 +6,7 @@ import (
acm "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/common"
_ "github.com/tendermint/tendermint/config/tendermint_test"
ptypes "github.com/tendermint/tendermint/permission/types"
)
var chainID string
@ -69,6 +70,26 @@ func TestCallTxSignable(t *testing.T) {
}
}
func TestNameTxSignable(t *testing.T) {
nameTx := &NameTx{
Input: &TxInput{
Address: []byte("input1"),
Amount: 12345,
Sequence: 250,
},
Name: "google.com",
Data: "secretly.not.google.com",
Fee: 1000,
}
signBytes := acm.SignBytes(chainID, nameTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[3,{"data":"secretly.not.google.com","fee":1000,"input":{"address":"696E70757431","amount":12345,"sequence":250},"name":"google.com"}]}`,
config.GetString("chain_id"))
if signStr != expected {
t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
}
}
func TestBondTxSignable(t *testing.T) {
privKeyBytes := make([]byte, 64)
var privKeyArray [64]byte
@ -135,3 +156,25 @@ func TestRebondTxSignable(t *testing.T) {
t.Errorf("Got unexpected sign string for RebondTx")
}
}
func TestSNativeTxSignable(t *testing.T) {
snativeTx := &SNativeTx{
Input: &TxInput{
Address: []byte("input1"),
Amount: 12345,
Sequence: 250,
},
SNative: &ptypes.SetBaseArgs{
Address: []byte("address1"),
Permission: 1,
Value: true,
},
}
signBytes := acm.SignBytes(chainID, snativeTx)
signStr := string(signBytes)
expected := Fmt(`{"chain_id":"%s","tx":[32,{"args":"[2,{"address":"6164647265737331","permission":1,"value":true}]","input":{"address":"696E70757431","amount":12345,"sequence":250},"snative":"SetBase"}]}`,
config.GetString("chain_id"))
if signStr != expected {
t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
}
}

Loading…
Cancel
Save