diff --git a/client/client.go b/client/client.go index 6cfa68fd3ebedd64be77439173fb194cbee8df92..afcd29b923270eb2362df13410acea074e1e1d62 100644 --- a/client/client.go +++ b/client/client.go @@ -17,6 +17,8 @@ package client import ( + "fmt" + "github.com/tendermint/go-rpc/client" "github.com/eris-ltd/eris-db/account" @@ -27,12 +29,12 @@ import ( type NodeClient interface{ Broadcast(transaction txs.Tx) (*txs.Receipt, error) - GetAccount(address []byte) (account account.Account, error) + GetAccount(address []byte) (*account.Account, error) } // NOTE [ben] Compiler check to ensure ErisClient successfully implements // eris-db/client.Client -var _ NodeClient = (*ErisClient)(nil) +var _ NodeClient = (*ErisNodeClient)(nil) // Eris-Client is a simple struct exposing the client rpc methods @@ -42,8 +44,8 @@ type ErisNodeClient struct { // ErisKeyClient.New returns a new eris-keys client for provided rpc location // Eris-keys connects over http request-responses -func New(rpcString string) *ErisNodeClient{ - return &ErisClient{ +func NewErisNodeClient(rpcString string) *ErisNodeClient{ + return &ErisNodeClient{ broadcastRPC: rpcString, } } @@ -62,7 +64,7 @@ func (erisClient *ErisNodeClient) Broadcast(tx txs.Tx) (*txs.Receipt, error) { return &receipt, nil } -func (erisClient *ErisNodeClient) GetAccount(address []byte) (account account.Account, error) { +func (erisClient *ErisNodeClient) GetAccount(address []byte) (*account.Account, error) { // fetch nonce from node client := rpcclient.NewClientURI(erisClient.broadcastRPC) account, err := tendermint_client.GetAccount(client, address) @@ -72,12 +74,11 @@ func (erisClient *ErisNodeClient) GetAccount(address []byte) (account account.Ac return nil, err } if account == nil { - err = fmt.Errorf("Unknown account %X at node (%s)", addrBytes, erisClient.broadcastRPC) + err = fmt.Errorf("Unknown account %X at node (%s)", address, erisClient.broadcastRPC) return nil, err } - return account, nil + return account.Copy(), nil } - diff --git a/client/client_mock_test.go b/client/client_mock_test.go index c1e4963256625e7f1383ded7b426373f6f36cf66..fbd5bc956bad71ad56dda184b0f439978c886ec4 100644 --- a/client/client_mock_test.go +++ b/client/client_mock_test.go @@ -30,10 +30,17 @@ import ( // eris-db/client.NodeClient var _ NodeClient = (*ErisMockClient)(nil) -type MockNodeClient { - accounts +type MockNodeClient struct { + accounts map[string]*account.Account } +func NewMockNodeClient() *MockNodeClient { + return &MockNodeClient{ + accounts: make(map[string]*account.Account) + } +} + + func (mock *MockNodeClient) Broadcast(transaction txs.Tx) (*txs.Receipt, error) { // make zero transaction receipt txReceipt := &txs.Receipt{ @@ -44,7 +51,7 @@ func (mock *MockNodeClient) Broadcast(transaction txs.Tx) (*txs.Receipt, error) return txReceipt, nil } -func (mock *MockNodeClient) GetAccount(address []byte) (account *account.Account, error) { +func (mock *MockNodeClient) GetAccount(address []byte) (*account.Account, error) { // make zero account var zero [32]byte copyAddressBytes := make([]byte, len(address), len(address)) @@ -61,5 +68,6 @@ func (mock *MockNodeClient) GetAccount(address []byte) (account *account.Account } func (mock *MockNodeClient) MockAddAccount(account *account.Account) { - + addressString := string(account.Address[:]) + mock.accounts[addressString] := account.Copy() } \ No newline at end of file diff --git a/client/cmd/transaction.go b/client/cmd/transaction.go index 02c21edad1df1f3049a1719d583a46a4b8063b39..f6c8e0b1c17b3f10594a3e73c444d36706e05480 100644 --- a/client/cmd/transaction.go +++ b/client/cmd/transaction.go @@ -60,7 +60,7 @@ func buildTransactionCommand() { Short: "eris-client tx name --amt <amt> --name <name> --data <data>", Long: "eris-client tx name --amt <amt> --name <name> --data <data>", Run: func(cmd *cobra.Command, args []string) { - transaction.Name(clientDo) + // transaction.Name(clientDo) }, PreRun: assertParameters, } diff --git a/client/core/transaction_factory.go b/client/core/transaction_factory.go index badd221a680246b6326a6151674fafca107dd264..2e85dcbbaf244f44ca3c4760803157de1237cf78 100644 --- a/client/core/transaction_factory.go +++ b/client/core/transaction_factory.go @@ -17,27 +17,18 @@ package core import ( - "bytes" "encoding/hex" - "encoding/json" "fmt" - "io/ioutil" - "net/http" "strconv" // "strings" // "time" - "github.com/tendermint/go-crypto" - "github.com/tendermint/go-rpc/client" - // ptypes "github.com/eris-ltd/permission/types" - log "github.com/eris-ltd/eris-logger" + // log "github.com/eris-ltd/eris-logger" - "github.com/eris-ltd/eris-db/account" "github.com/eris-ltd/eris-db/client" "github.com/eris-ltd/eris-db/keys" - tendermint_client "github.com/eris-ltd/eris-db/rpc/tendermint/client" "github.com/eris-ltd/eris-db/txs" ) @@ -49,7 +40,7 @@ var ( // core functions with string args. // validates strings and forms transaction -func Send(nodeClient *client.NodeClient, keyClient *keys.KeyClient, pubkey, addr, toAddr, amtS, nonceS string) (*txs.SendTx, error) { +func Send(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, addr, toAddr, amtS, nonceS string) (*txs.SendTx, error) { pub, amt, nonce, err := checkCommon(nodeClient, keyClient, pubkey, addr, amtS, nonceS) if err != nil { return nil, err @@ -71,7 +62,7 @@ func Send(nodeClient *client.NodeClient, keyClient *keys.KeyClient, pubkey, addr return tx, nil } -func Call(nodeClient *client.NodeClient, keyClient *keys.KeyClient, pubkey, addr, toAddr, amtS, nonceS, gasS, feeS, data string) (*txs.CallTx, error) { +func Call(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, addr, toAddr, amtS, nonceS, gasS, feeS, data string) (*txs.CallTx, error) { pub, amt, nonce, err := checkCommon(nodeClient, keyClient, pubkey, addr, amtS, nonceS) if err != nil { return nil, err @@ -101,8 +92,8 @@ func Call(nodeClient *client.NodeClient, keyClient *keys.KeyClient, pubkey, addr return tx, nil } -func Name(nodeClient *client.NodeClient, keyClient *keys.KeyClient, pubkey, addr, amtS, nonceS, feeS, name, data string) (*txs.NameTx, error) { - pub, amt, nonce, err := checkCommon(nodeAddr, signAddr, pubkey, addr, amtS, nonceS) +func Name(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, addr, amtS, nonceS, feeS, name, data string) (*txs.NameTx, error) { + pub, amt, nonce, err := checkCommon(nodeClient, keyClient, pubkey, addr, amtS, nonceS) if err != nil { return nil, err } @@ -299,72 +290,6 @@ func coreNewAccount(nodeAddr, pubkey, chainID string) (*types.NewAccountTx, erro // }, nil // } - - -//------------------------------------------------------------------------------------ -// utils for talking to the key server - -// type HTTPResponse struct { -// Response string -// Error string -// } - -// func RequestResponse(addr, method string, args map[string]string) (string, error) { -// b, err := json.Marshal(args) -// if err != nil { -// return "", err -// } -// endpoint := fmt.Sprintf("%s/%s", addr, method) -// log.WithFields(log.Fields{ -// "key server endpoint": endpoint, -// "request body": string(b), -// }).Debugf("Sending request body to key server") -// req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(b)) -// if err != nil { -// return "", err -// } -// req.Header.Add("Content-Type", "application/json") -// res, errS, err := requestResponse(req) -// if err != nil { -// return "", fmt.Errorf("Error calling eris-keys at %s: %s", endpoint, err.Error()) -// } -// if errS != "" { -// return "", fmt.Errorf("Error (string) calling eris-keys at %s: %s", endpoint, errS) -// } -// log.WithFields(log.Fields{ -// "endpoint": endpoint, -// "request body": string(b), -// "response": res, -// }).Debugf("Received response from key server") -// return res, nil -// } - -// func requestResponse(req *http.Request) (string, string, error) { -// client := new(http.Client) -// resp, err := client.Do(req) -// if err != nil { -// return "", "", err -// } -// if resp.StatusCode >= 400 { -// return "", "", fmt.Errorf(resp.Status) -// } -// return unpackResponse(resp) -// } - -// func unpackResponse(resp *http.Response) (string, string, error) { -// b, err := ioutil.ReadAll(resp.Body) -// if err != nil { -// return "", "", err -// } -// r := new(HTTPResponse) -// if err := json.Unmarshal(b, r); err != nil { -// return "", "", err -// } -// return r.Response, r.Error, nil -// } - - - type TxResult struct { BlockHash []byte // all txs get in a block Hash []byte // all txs get a hash @@ -378,13 +303,11 @@ type TxResult struct { // can differentiate mempool errors from other } -func Sign() - // Preserve -func SignAndBroadcast(chainID, nodeAddr, signAddr string, tx txs.Tx, sign, broadcast, wait bool) (txResult *TxResult, err error) { +func SignAndBroadcast(chainID string, nodeClient client.NodeClient, keyClient keys.KeyClient, tx txs.Tx, sign, broadcast, wait bool) (txResult *TxResult, err error) { // var inputAddr []byte if sign { - _, tx, err = signTx(signAddr, chainID, tx) + _, tx, err = signTx(keyClient, chainID, tx) if err != nil { return nil, err } @@ -418,7 +341,7 @@ func SignAndBroadcast(chainID, nodeAddr, signAddr string, tx txs.Tx, sign, broad // } // } var receipt *txs.Receipt - receipt, err = Broadcast(tx, nodeAddr) + receipt, err = nodeClient.Broadcast(tx) if err != nil { return nil, err } @@ -535,8 +458,3 @@ type Msg struct { // }() // return resultChan, nil // } - -//------------------------------------------------------------------------------------ -// convenience function - - diff --git a/client/core/transaction_factory_test.go b/client/core/transaction_factory_test.go index 4fb88e3ff74db4f8d07850a13f3bb61deb2bfdbb..b46bdc7f83d05b6f7d4ee6423ef18fb29a0fd59f 100644 --- a/client/core/transaction_factory_test.go +++ b/client/core/transaction_factory_test.go @@ -21,7 +21,7 @@ import ( "github.com/eris-ltd/eris-keys/crypto" - "github.com?eris-ltd/eris-db/client" + "github.com/eris-ltd/eris-db/client" "github.com/eris-ltd/eris-db/keys" ) @@ -34,7 +34,7 @@ func TestTransactionFactory(t *testing.T) { t.Run("ExtractInputAddress from transaction", func (t *testing.T) { t.Run("SendTransaction", testTransactionFactorySend) // t.Run("NameTransaction", ) - t.Run("CallTransaction", ) + // t.Run("CallTransaction", ) // t.Run("PermissionTransaction", ) // t.Run("BondTransaction", ) // t.Run("UnbondTransaction", ) @@ -43,11 +43,28 @@ func TestTransactionFactory(t *testing.T) { } func testTransactionFactorySend(t *testing.T) { - mockKeyClient := new(keys.MockKeyClient) - mockNodeClient := new(client.MockNodeClient) + mockKeyClient := keys.NewMockKeyClient() + mockNodeClient := client.NewMockNodeClient() - key := + // generate an ED25519 key and ripemd160 address + address := mockKeyClient.NewKey() + // Public key can be queried from mockKeyClient.PublicKey(address) + // but here we let the transaction factory retrieve the public key + // which will then also overwrite the address we provide the function. + // As a result we will assert whether address generated above, is identical + // to address in generated transation. + publicKey := "" + // generate an additional address to send amount to + toAddress := mockKeyClient.NewKey() + // set an amount to transfer + amount := "1000" + // set nonce to unset + nonce := "" - Send(mockNodeClient, mockKeyClient, ) + tx, err := Send(mockNodeClient, mockKeyClient, publicKey, string(address), + string(toAddress), amount, nonce) + if err != nil { + t.Fail() + } } diff --git a/client/core/transaction_factory_util.go b/client/core/transaction_factory_util.go index bfd7e1d35d50792ac01d65bcd474cd98619cf0d2..f119ef9a3391ddfbdce43ce592198e99c5fb3a2e 100644 --- a/client/core/transaction_factory_util.go +++ b/client/core/transaction_factory_util.go @@ -17,7 +17,15 @@ package core import ( - + "fmt" + "encoding/hex" + "strconv" + + log "github.com/eris-ltd/eris-logger" + + "github.com/tendermint/go-crypto" + + acc "github.com/eris-ltd/eris-db/account" "github.com/eris-ltd/eris-db/client" "github.com/eris-ltd/eris-db/keys" "github.com/eris-ltd/eris-db/txs" @@ -28,8 +36,8 @@ 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(signAddr, chainID string, tx_ txs.Tx) ([]byte, txs.Tx, error) { - signBytes := fmt.Sprintf("%X", account.SignBytes(chainID, tx_)) +func signTx(keyClient keys.KeyClient, chainID string, tx_ txs.Tx) ([]byte, txs.Tx, error) { + signString := fmt.Sprintf("%X", acc.SignBytes(chainID, tx_)) var inputAddr []byte var sigED crypto.SignatureEd25519 switch tx := tx_.(type) { @@ -58,61 +66,65 @@ func signTx(signAddr, chainID string, tx_ txs.Tx) ([]byte, txs.Tx, error) { inputAddr = tx.Address defer func(s *crypto.SignatureEd25519) { tx.Signature = *s }(&sigED) } - addrHex := fmt.Sprintf("%X", inputAddr) - sig, err := Sign(signBytes, addrHex, signAddr) + sig, err := keyClient.Sign(signString, inputAddr) if err != nil { return nil, nil, err } - sigED = crypto.SignatureEd25519(sig) + // because this codebase has been written with a total neglect for the type + // system, this is an intermediate step before we clean out the full set of + // canonical bytes. + var sig64 [64]byte + copy(sig64[:], sig) + sigED = crypto.SignatureEd25519(sig64) log.WithFields(log.Fields{ - "transaction sign bytes": signBytes, - "account address": addrHex, - "signature": fmt.Sprintf("%X", sig), + "transaction sign bytes": signString, + "account address": fmt.Sprintf("%X", inputAddr), + "signature": fmt.Sprintf("%X", sig64), }).Debug("Signed transaction") return inputAddr, tx_, nil } -// readInputAddressFromTransaction returns the hexadecimal string form of the -func readInputAddressFromTransaction(tx_ txs.Tx) (addressHex string) { - // signBytes := fmt.Sprintf("%X", account.SignBytes(chainID, tx_)) - var inputAddr []byte - // var sigED crypto.SignatureEd25519 - switch tx := tx_.(type) { - case *txs.SendTx: - inputAddr = tx.Inputs[0].Address - // defer func(s *crypto.SignatureEd25519) { tx.Inputs[0].Signature = *s }(&sigED) - case *txs.NameTx: - inputAddr = tx.Input.Address - // defer func(s *crypto.SignatureEd25519) { tx.Input.Signature = *s }(&sigED) - case *txs.CallTx: - inputAddr = tx.Input.Address - // defer func(s *crypto.SignatureEd25519) { tx.Input.Signature = *s }(&sigED) - case *txs.PermissionsTx: - inputAddr = tx.Input.Address - // defer func(s *crypto.SignatureEd25519) { tx.Input.Signature = *s }(&sigED) - case *txs.BondTx: - inputAddr = tx.Inputs[0].Address - // defer func(s *crypto.SignatureEd25519) { - // tx.Signature = *s - // tx.Inputs[0].Signature = *s - // }(&sigED) - case *txs.UnbondTx: - inputAddr = tx.Address - // defer func(s *crypto.SignatureEd25519) { tx.Signature = *s }(&sigED) - case *txs.RebondTx: - inputAddr = tx.Address - // defer func(s *crypto.SignatureEd25519) { tx.Signature = *s }(&sigED) - } - addressHex := fmt.Sprintf("%X", inputAddr) - // sig, err := Sign(signBytes, addrHex, signAddr) - // if err != nil { - // return nil, nil, err - // } - // sigED = crypto.SignatureEd25519(sig) - return addressHex -} - -func checkCommon(nodeClient client.Client, keyClient keys.KeyClient, pubkey, addr, amtS, nonceS string) (pub crypto.PubKey, amt int64, nonce int64, err error) { +// readInputAddressFromTransacIm not tion returns the hexadecimal string form of the +// func readInputAddressFromTransaction(tx_ txs.Tx) (addressHex string) { +// // signBytes := fmt.Sprintf("%X", account.SignBytes(chainID, tx_)) +// var inputAddr []byte +// // var sigED crypto.SignatureEd25519 +// switch tx := tx_.(type) { +// case *txs.SendTx: +// inputAddr = tx.Inputs[0].Address +// // defer func(s *crypto.SignatureEd25519) { tx.Inputs[0].Signature = *s }(&sigED) +// case *txs.NameTx: +// inputAddr = tx.Input.Address +// // defer func(s *crypto.SignatureEd25519) { tx.Input.Signature = *s }(&sigED) +// case *txs.CallTx: +// inputAddr = tx.Input.Address +// // defer func(s *crypto.SignatureEd25519) { tx.Input.Signature = *s }(&sigED) +// case *txs.PermissionsTx: +// inputAddr = tx.Input.Address +// // defer func(s *crypto.SignatureEd25519) { tx.Input.Signature = *s }(&sigED) +// case *txs.BondTx: +// inputAddr = tx.Inputs[0].Address +// // defer func(s *crypto.SignatureEd25519) { +// // tx.Signature = *s +// // tx.Inputs[0].Signature = *s +// // }(&sigED) +// case *txs.UnbondTx: +// inputAddr = tx.Address +// // defer func(s *crypto.SignatureEd25519) { tx.Signature = *s }(&sigED) +// case *txs.RebondTx: +// inputAddr = tx.Address +// // defer func(s *crypto.SignatureEd25519) { tx.Signature = *s }(&sigED) +// } +// addressHex := fmt.Sprintf("%X", inputAddr) +// // sig, err := Sign(signBytes, addrHex, signAddr) +// // if err != nil { +// // return nil, nil, err +// // } +// // sigED = crypto.SignatureEd25519(sig) +// return addressHex +// } + +func checkCommon(nodeClient client.NodeClient, keyClient keys.KeyClient, pubkey, addr, amtS, nonceS string) (pub crypto.PubKey, amt int64, nonce int64, err error) { if amtS == "" { err = fmt.Errorf("input must specify an amount with the --amt flag") return @@ -165,9 +177,9 @@ func checkCommon(nodeClient client.Client, keyClient keys.KeyClient, pubkey, add return } // fetch nonce from node - account, err :=nodeClient.GetAccount(addrBytes) - if err != nil { - return + account, err2 := nodeClient.GetAccount(addrBytes) + if err2 != nil { + return pub, amt, nonce, err2 } nonce = int64(account.Sequence) + 1 log.WithFields(log.Fields{ diff --git a/client/transaction/transaction.go b/client/transaction/transaction.go index 6665aafe6deb617568339fd3ccf2e56d5ab1ed96..c25a48a0a0200f6e421e22fcfe17c9b6d8411f11 100644 --- a/client/transaction/transaction.go +++ b/client/transaction/transaction.go @@ -31,8 +31,8 @@ import ( func Send(do *definitions.ClientDo) { // construct two clients to call out to keys server and // blockchain node. - erisKeyClient := keys.ErisKeyClient.New(do.SignAddrFlag) - erisNodeClient := client.ErisClient.New(do.NodeAddrFlag) + erisKeyClient := keys.NewErisKeyClient(do.SignAddrFlag) + erisNodeClient := client.NewErisNodeClient(do.NodeAddrFlag) // form the send transaction sendTransaction, err := core.Send(erisNodeClient, erisKeyClient, do.PubkeyFlag, do.AddrFlag, do.ToFlag, do.AmtFlag, do.NonceFlag) @@ -44,15 +44,15 @@ func Send(do *definitions.ClientDo) { // as we move away from and deprecate the api that allows sending unsigned // transactions and relying on (our) receiving node to sign it. unpackSignAndBroadcast( - core.SignAndBroadcast(do.ChainidFlag, do.NodeAddrFlag, - do.SignAddrFlag, sendTransaction, true, do.BroadcastFlag, do.WaitFlag)) + core.SignAndBroadcast(do.ChainidFlag, erisNodeClient, + erisKeyClient, sendTransaction, true, do.BroadcastFlag, do.WaitFlag)) } func Call(do *definitions.ClientDo) { // construct two clients to call out to keys server and // blockchain node. - erisKeyClient := keys.ErisKeyClient.New(do.SignAddrFlag) - erisNodeClient := client.ErisClient.New(do.NodeAddrFlag) + erisKeyClient := keys.NewErisKeyClient(do.SignAddrFlag) + erisNodeClient := client.NewErisNodeClient(do.NodeAddrFlag) // form the call transaction callTransaction, err := core.Call(erisNodeClient, erisKeyClient, do.PubkeyFlag, do.AddrFlag, do.ToFlag, do.AmtFlag, do.NonceFlag, @@ -65,8 +65,8 @@ func Call(do *definitions.ClientDo) { // as we move away from and deprecate the api that allows sending unsigned // transactions and relying on (our) receiving node to sign it. unpackSignAndBroadcast( - core.SignAndBroadcast(do.ChainidFlag, do.NodeAddrFlag, - do.SignAddrFlag, callTransaction, true, do.BroadcastFlag, do.WaitFlag)) + core.SignAndBroadcast(do.ChainidFlag, erisNodeClient, + erisKeyClient, callTransaction, true, do.BroadcastFlag, do.WaitFlag)) } //---------------------------------------------------------------------- diff --git a/keys/key_client.go b/keys/key_client.go index 7ebb17ca408af600389539558b0b8efe975f945f..a91f74918350f775269145b62494e2e323434369 100644 --- a/keys/key_client.go +++ b/keys/key_client.go @@ -18,28 +18,27 @@ package keys import ( "encoding/hex" - "fmt" ) type KeyClient interface{ // Sign needs to return the signature bytes for given message to sign // and the address to sign it with. - Sign(signBytes, signAddress []byte) (signature []byte, err error) + Sign(signString 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) + PublicKey(addressString string) (publicKey []byte, err error) } // NOTE [ben] Compiler check to ensure ErisKeyClient successfully implements // eris-db/keys.KeyClient var _ KeyClient = (*ErisKeyClient)(nil) -struct ErisKeyClient{ +type ErisKeyClient struct { rpcString string } // ErisKeyClient.New returns a new eris-keys client for provided rpc location // Eris-keys connects over http request-responses -func New(rpcString string) *ErisKeyClient{ +func NewErisKeyClient(rpcString string) *ErisKeyClient{ return &ErisKeyClient{ rpcString: rpcString, } @@ -47,10 +46,10 @@ func New(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, signAddress []byte) (signature []byte, err error) { +func (erisKeys *ErisKeyClient) Sign(signString string, signAddress []byte) (signature []byte, err error) { args := map[string]string{ - "msg": string(signBytes), - "hash": string(signBytes), // TODO:[ben] backwards compatibility + "msg": signString, + "hash": signString, // TODO:[ben] backwards compatibility "addr": string(signAddress), } sigS, err := RequestResponse(erisKeys.rpcString, "sign", args) @@ -67,9 +66,9 @@ func (erisKeys *ErisKeyClient) Sign(signBytes, signAddress []byte) (signature [] // Eris-keys client PublicKey requests the public key associated with an address from // the eris-keys server. -func (erisKeys *ErisKeyClient) PublicKey(address []byte) (publicKey []byte, err error) { +func (erisKeys *ErisKeyClient) PublicKey(addressString string) (publicKey []byte, err error) { args := map[string]string{ - "addr": address, + "addr": addressString, } pubS, err := RequestResponse(erisKeys.rpcString, "pub", args) if err != nil { @@ -78,4 +77,5 @@ func (erisKeys *ErisKeyClient) PublicKey(address []byte) (publicKey []byte, err // TODO: [ben] assert that received public key results in // address return hex.DecodeString(pubS) -} \ No newline at end of file +} + diff --git a/keys/key_client_mock_test.go b/keys/key_client_mock_test.go index a95fad704a3291393955124c714042f18186da39..24b0500d4d9d679dbe8bb857b7aac80c0739b414 100644 --- a/keys/key_client_mock_test.go +++ b/keys/key_client_mock_test.go @@ -28,7 +28,7 @@ import ( //--------------------------------------------------------------------- // Mock client for replacing signing done by eris-keys -// NOTE [ben] Compiler check to ensure MockKeysClient successfully implements +// NOTE [ben] Compiler check to ensure MockKeyClient successfully implements // eris-db/keys.KeyClient var _ KeyClient = (*MockKeyClient)(nil) @@ -36,7 +36,13 @@ type MockKeyClient struct{ knownKeys map[string]*crypto.Key } -func (mock *MockKeyCLient) NewKey() (address []byte) { +func NewMockKeyClient() *MockKeyClient { + return &MockKeyClient{ + knownKeys: make(map[string]*crypto.Key) + } +} + +func (mock *MockKeyClient) NewKey() (address []byte) { // Only tests ED25519 curve and ripemd160. keyType := crypto.KeyType{ crypto.CurveTypeEd25519, AddrTypeRipemd160 } @@ -44,7 +50,7 @@ func (mock *MockKeyCLient) NewKey() (address []byte) { } -func (mock *MockKeyClient) Sign(signBytes, signAddress []byte) (signature []byte, err error) { +func (mock *MockKeyClient) Sign(signString string, signAddress []byte) (signature []byte, err error) { key := mock.knownKeys[string(signAddress)] if key.PrivateKey == nil { return nil, fmt.Errorf("Unknown address (%X)", signAddress) @@ -59,5 +65,3 @@ func (mock *MockKeyClient) PublicKey(address []byte) (publicKey []byte, err erro } return key.PubKey() } - -func (mock *MockKeyClient) \ No newline at end of file