From de3755fd956517a366b4162da444e576132f21bd Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Wed, 28 Sep 2016 01:48:08 +0200
Subject: [PATCH] client, keys: revert to use of string over []byte

---
 client/client.go                        |  3 +--
 client/core/transaction_factory_util.go |  4 ++--
 keys/key_client.go                      | 14 +++++++-------
 keys/mock/key_client_mock.go            |  7 ++++++-
 4 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/client/client.go b/client/client.go
index 16588f53..1c9c3502 100644
--- a/client/client.go
+++ b/client/client.go
@@ -186,8 +186,7 @@ func (erisNodeClient *ErisNodeClient) GetName(name string) (owner []byte, data s
 		return nil, "", 0, err
 	}
 	// unwrap return results
-	owner = make([]byte, len(entryResult.Owner))
-	copy(owner, entryResult.Owner) 
+	owner = entryResult.Owner
 	data = entryResult.Data
 	expirationBlock = entryResult.Expires
 	return
diff --git a/client/core/transaction_factory_util.go b/client/core/transaction_factory_util.go
index 8463e8e6..01c23826 100644
--- a/client/core/transaction_factory_util.go
+++ b/client/core/transaction_factory_util.go
@@ -38,7 +38,7 @@ import (
 // tx has either one input or we default to the first one (ie for send/bond)
 // TODO: better support for multisig and bonding
 func signTx(keyClient keys.KeyClient, chainID string, tx_ txs.Tx) ([]byte, txs.Tx, error) {
-	signBytes := []byte(fmt.Sprintf("%X", acc.SignBytes(chainID, tx_)))
+	signBytesString := fmt.Sprintf("%X", acc.SignBytes(chainID, tx_))
 	var inputAddr []byte
 	var sigED crypto.SignatureEd25519
 	switch tx := tx_.(type) {
@@ -67,7 +67,7 @@ func signTx(keyClient keys.KeyClient, chainID string, tx_ txs.Tx) ([]byte, txs.T
 		inputAddr = tx.Address
 		defer func(s *crypto.SignatureEd25519) { tx.Signature = *s }(&sigED)
 	}
-	sig, err := keyClient.Sign(signBytes, inputAddr)
+	sig, err := keyClient.Sign(signBytesString, inputAddr)
 	if err != nil {
 		return nil, nil, err
 	}
diff --git a/keys/key_client.go b/keys/key_client.go
index 0372805a..44a60f1f 100644
--- a/keys/key_client.go
+++ b/keys/key_client.go
@@ -17,13 +17,14 @@
 package keys
 
 import (
+	"fmt"
 	"encoding/hex"
 )
 
 type KeyClient interface {
 	// Sign needs to return the signature bytes for given message to sign
 	// and the address to sign it with.
-	Sign(signBytes []byte, signAddress []byte) (signature []byte, err error)
+	Sign(signBytesString string, signAddress []byte) (signature []byte, err error)
 	// PublicKey needs to return the public key associated with a given address
 	PublicKey(address []byte) (publicKey []byte, err error)
 }
@@ -46,11 +47,11 @@ func NewErisKeyClient(rpcString string) *ErisKeyClient {
 
 // Eris-keys client Sign requests the signature from ErisKeysClient over rpc for the given
 // bytes to be signed and the address to sign them with.
-func (erisKeys *ErisKeyClient) Sign(signBytes []byte, signAddress []byte) (signature []byte, err error) {
+func (erisKeys *ErisKeyClient) Sign(signBytesString string, signAddress []byte) (signature []byte, err error) {
 	args := map[string]string{
-		"msg":  string(signBytes),
-		"hash": string(signBytes), // TODO:[ben] backwards compatibility
-		"addr": string(signAddress),
+		"msg":  signBytesString,
+		"hash": signBytesString, // TODO:[ben] backwards compatibility
+		"addr": fmt.Sprintf("%X", signAddress),
 	}
 	sigS, err := RequestResponse(erisKeys.rpcString, "sign", args)
 	if err != nil {
@@ -60,8 +61,7 @@ func (erisKeys *ErisKeyClient) Sign(signBytes []byte, signAddress []byte) (signa
 	if err != nil {
 		return
 	}
-	copy(signature[:], sigBytes)
-	return
+	return sigBytes, err
 }
 
 // Eris-keys client PublicKey requests the public key associated with an address from
diff --git a/keys/mock/key_client_mock.go b/keys/mock/key_client_mock.go
index 67b2ad9c..ef9cca86 100644
--- a/keys/mock/key_client_mock.go
+++ b/keys/mock/key_client_mock.go
@@ -17,6 +17,7 @@
 package mock
 
 import (
+	"encoding/hex"
 	"fmt"
 
 	// for the mock of key server we explicitly import
@@ -56,11 +57,15 @@ func (mock *MockKeyClient) NewKey() (address []byte) {
 	return key.Address
 }
 
-func (mock *MockKeyClient) Sign(signBytes []byte, signAddress []byte) (signature []byte, err error) {
+func (mock *MockKeyClient) Sign(signBytesString string, signAddress []byte) (signature []byte, err error) {
 	key := mock.knownKeys[fmt.Sprintf("%X", signAddress)]
 	if key == nil {
 		return nil, fmt.Errorf("Unknown address (%X)", signAddress)
 	}
+	signBytes, err := hex.DecodeString(signBytesString)
+	if err != nil {
+		return nil, fmt.Errorf("Sign bytes string is invalid hex string: %s", err.Error())
+	}
 	return key.Sign(signBytes)
 }
 
-- 
GitLab