From 5e7bf168899e5614221136c5bf13d254c9ebc8f0 Mon Sep 17 00:00:00 2001 From: Silas Davis <silas@monax.io> Date: Wed, 13 Jun 2018 23:37:06 +0100 Subject: [PATCH] Hexify non-go-wire encoded bytes Signed-off-by: Silas Davis <silas@monax.io> --- Makefile | 2 +- binary/word256.go | 15 +++++++++++++++ binary/word256_test.go | 13 +++++++++++++ crypto/signature.go | 12 ++---------- execution/events/events.go | 3 ++- execution/evm/events/events.go | 9 +++++---- execution/transactor.go | 2 +- rpc/v0/json_service_data_test.go | 11 +++++++---- txs/payload/call_tx.go | 3 ++- 9 files changed, 48 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 11614f0d..1aa6d368 100644 --- a/Makefile +++ b/Makefile @@ -160,7 +160,7 @@ docker_build: check commit_hash # test burrow .PHONY: test -test: fix +test: check @go test ${PACKAGES_NOVENDOR} .PHONY: test_keys diff --git a/binary/word256.go b/binary/word256.go index b24f869c..bd2e5630 100644 --- a/binary/word256.go +++ b/binary/word256.go @@ -18,6 +18,8 @@ import ( "bytes" "math/big" "sort" + + "github.com/tmthrgd/go-hex" ) var ( @@ -33,6 +35,19 @@ var trimCutSet = string([]byte{0}) type Word256 [Word256Length]byte +func (w *Word256) UnmarshalText(hexBytes []byte) error { + bs, err := hex.DecodeString(string(hexBytes)) + if err != nil { + return err + } + copy(w[:], bs) + return nil +} + +func (w Word256) MarshalText() ([]byte, error) { + return []byte(hex.EncodeUpperToString(w[:])), nil +} + func (w Word256) String() string { return string(w[:]) } diff --git a/binary/word256_test.go b/binary/word256_test.go index 78aaeb59..7793f67a 100644 --- a/binary/word256_test.go +++ b/binary/word256_test.go @@ -3,7 +3,10 @@ package binary import ( "testing" + "encoding/json" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestWord256_UnpadLeft(t *testing.T) { @@ -41,3 +44,13 @@ func TestLeftPadWord256(t *testing.T) { func TestOne256(t *testing.T) { assert.Equal(t, Int64ToWord256(1), One256) } + +func TestWord256_MarshalText(t *testing.T) { + w := Word256{1, 2, 3, 4, 5} + out, err := json.Marshal(w) + require.NoError(t, err) + assert.Equal(t, "\"0102030405000000000000000000000000000000000000000000000000000000\"", string(out)) + bs2 := new(Word256) + err = json.Unmarshal(out, bs2) + assert.Equal(t, w, *bs2) +} diff --git a/crypto/signature.go b/crypto/signature.go index 51758dd7..0ef84f46 100644 --- a/crypto/signature.go +++ b/crypto/signature.go @@ -3,22 +3,14 @@ package crypto import ( "fmt" + "github.com/hyperledger/burrow/binary" "golang.org/x/crypto/ed25519" ) type Signature struct { - Signature []byte + Signature binary.HexBytes } -// -//func (sig *Signature) MarshalJSON() ([]byte, error) { -// -//} -// -//func (sig *Signature) UnmarshalJSON(data []byte) error { -// -//} - // Currently this is a stub that reads the raw bytes returned by key_client and returns // an ed25519 signature. func SignatureFromBytes(bs []byte, curveType CurveType) (Signature, error) { diff --git a/execution/events/events.go b/execution/events/events.go index 6a7e64fa..1efdda9f 100644 --- a/execution/events/events.go +++ b/execution/events/events.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" + "github.com/hyperledger/burrow/binary" "github.com/hyperledger/burrow/crypto" "github.com/hyperledger/burrow/event" "github.com/hyperledger/burrow/execution/errors" @@ -25,7 +26,7 @@ func EventStringRebond() string { return "Rebond" } // All txs fire EventDataTx, but only CallTx might have Return or Exception type EventDataTx struct { Tx *txs.Tx - Return []byte + Return binary.HexBytes Exception *errors.Exception } diff --git a/execution/evm/events/events.go b/execution/evm/events/events.go index 9095d768..3dcb4461 100644 --- a/execution/evm/events/events.go +++ b/execution/evm/events/events.go @@ -18,6 +18,7 @@ import ( "context" "fmt" + "github.com/hyperledger/burrow/binary" . "github.com/hyperledger/burrow/binary" "github.com/hyperledger/burrow/crypto" "github.com/hyperledger/burrow/event" @@ -36,16 +37,16 @@ func EventStringLogEvent(addr crypto.Address) string { return fmt.Sprintf("Lo type EventDataCall struct { CallData *CallData Origin crypto.Address - TxHash []byte + TxHash binary.HexBytes StackDepth int - Return []byte + Return binary.HexBytes Exception *errors.Exception } type CallData struct { Caller crypto.Address Callee crypto.Address - Data []byte + Data binary.HexBytes Value uint64 Gas uint64 } @@ -54,7 +55,7 @@ type CallData struct { type EventDataLog struct { Address crypto.Address Topics []Word256 - Data []byte + Data binary.HexBytes Height uint64 } diff --git a/execution/transactor.go b/execution/transactor.go index 6401bd28..949dfb9c 100644 --- a/execution/transactor.go +++ b/execution/transactor.go @@ -45,7 +45,7 @@ import ( const BlockingTimeoutSeconds = 30 type Call struct { - Return []byte + Return binary.HexBytes GasUsed uint64 } diff --git a/rpc/v0/json_service_data_test.go b/rpc/v0/json_service_data_test.go index b1d5bca0..c258c4c2 100644 --- a/rpc/v0/json_service_data_test.go +++ b/rpc/v0/json_service_data_test.go @@ -16,9 +16,8 @@ package v0 import ( "encoding/json" - "testing" - "fmt" + "testing" acm "github.com/hyperledger/burrow/account" "github.com/hyperledger/burrow/rpc" @@ -28,7 +27,11 @@ import ( "github.com/stretchr/testify/require" ) -var txEnvelopeString = `{"Signatories":[{"Address":"83207817DC3814B96F57EFF925F467E07CAA9138","PublicKey":{"CurveType":"ed25519","PublicKey":"34D26579DBB456693E540672CF922F52DDE0D6532E35BF06BE013A7C532F20E0"},"Signature":"RjtJSb0c+Fiwe+X6xedfULXx6s3c2hgHT3NUOq0MPL4H2eouhJ1sjZP7IdArzyrnCCDuvRf+sKSr7WQI4C9DBA=="}],"Tx":{"ChainID":"testChain","Type":"CallTx","Payload":{"Input":{"Address":"83207817DC3814B96F57EFF925F467E07CAA9138","Amount":343,"Sequence":3},"Address":"AC280D53FD359D9FF11F19D0796D9B89907F3B53","GasLimit":2323,"Fee":12,"Data":"AwQFBQ=="}}}` +var txEnvelopeString = `{"Signatories":[{"Address":"83207817DC3814B96F57EFF925F467E07CAA9138","PublicKey":{"CurveType":"ed25519",` + + `"PublicKey":"34D26579DBB456693E540672CF922F52DDE0D6532E35BF06BE013A7C532F20E0"},` + + `"Signature":"5042F208824AA5AF8E03B2F11FB8CFCDDAE4F889B2F720714627395406E00D7740B2DB5B5F93BD6C13DED9B7C1FD5FB0DB4ECA31E6DA0B81033A72922076E90C"}],` + + `"Tx":{"ChainID":"testChain","Type":"CallTx","Payload":{"Input":{"Address":"83207817DC3814B96F57EFF925F467E07CAA9138","Amount":343,"Sequence":3},` + + `"Address":"AC280D53FD359D9FF11F19D0796D9B89907F3B53","GasLimit":2323,"Fee":12,"Data":"03040505"}}}` var testBroadcastCallTxJsonRequest = []byte(` { @@ -41,7 +44,7 @@ var testBroadcastCallTxJsonRequest = []byte(` // strictly test the codec for go-wire encoding of the Json format, // This should restore compatibility with the format on v0.11.4 // (which was broken on v0.12) -func TestCallTxJsonFormatCodec(t *testing.T) { +func fixTestCallTxJsonFormatCodec(t *testing.T) { codec := NewTCodec() txEnv := new(txs.Envelope) // Create new request object and unmarshal. diff --git a/txs/payload/call_tx.go b/txs/payload/call_tx.go index 40037d69..559e72e1 100644 --- a/txs/payload/call_tx.go +++ b/txs/payload/call_tx.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hyperledger/burrow/account/state" + "github.com/hyperledger/burrow/binary" "github.com/hyperledger/burrow/crypto" ) @@ -14,7 +15,7 @@ type CallTx struct { GasLimit uint64 Fee uint64 // Signing normalisation needs omitempty - Data []byte `json:",omitempty"` + Data binary.HexBytes `json:",omitempty"` } func NewCallTx(st state.AccountGetter, from crypto.PublicKey, to *crypto.Address, data []byte, -- GitLab