Skip to content

Commit

Permalink
- added trim function for the ethereum private key
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Nov 18, 2021
1 parent 7afe63e commit 706e0e9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion bridge/eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func NewClient(args ArgsClient) (*client, error) {
if err != nil {
return nil, err
}
privateKey, err := crypto.HexToECDSA(string(privateKeyBytes))
privateKeyString := core.TrimWhiteSpaceCharacters(string(privateKeyBytes))
privateKey, err := crypto.HexToECDSA(privateKeyString)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions bridge/eth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
}
)

const TestPrivateKey = "60f3849d7c8d93dfce1947d17c34be3e4ea974e74e15ce877f0df34d7192efab"
const TestPrivateKey = " 60f3849d7c8d93dfce1947d17c34be3e4ea974e74e15ce877f0df34d7192efab\n\t "
const GasLimit = uint64(400000)

func TestGetPending(t *testing.T) {
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestSign(t *testing.T) {
},
}

sk, err := crypto.HexToECDSA(TestPrivateKey)
sk, err := crypto.HexToECDSA(core.TrimWhiteSpaceCharacters(TestPrivateKey))
require.Nil(t, err)

pk := sk.Public()
Expand Down Expand Up @@ -624,7 +624,7 @@ func TestClient_GetTransactionsStatuses(t *testing.T) {
func privateKey(t *testing.T) *ecdsa.PrivateKey {
t.Helper()

sk, err := crypto.HexToECDSA(TestPrivateKey)
sk, err := crypto.HexToECDSA(core.TrimWhiteSpaceCharacters(TestPrivateKey))
if err != nil {
t.Fatal(err)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/bridge/config/ethereum.sk
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9bb971db41e3815a669a71c3f1bcb24e0b81f21e04bf11faa7a34b9b40e7cfb1
9bb971db41e3815a669a71c3f1bcb24e0b81f21e04bf11faa7a34b9b40e7cfb1
9 changes: 9 additions & 0 deletions core/converters.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core

import "strings"

// TODO - move this as a method in AddressHandler

// ConvertFromByteSliceToArray will convert the provided buffer to its [32]byte representation
Expand All @@ -9,3 +11,10 @@ func ConvertFromByteSliceToArray(buff []byte) [32]byte {

return result
}

// TrimWhiteSpaceCharacters will remove the white spaces from the input string
func TrimWhiteSpaceCharacters(input string) string {
cutset := "\n\t "

return strings.Trim(input, cutset)
}
18 changes: 18 additions & 0 deletions core/converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,21 @@ func TestConvertFromByteSliceToArray(t *testing.T) {
result := ConvertFromByteSliceToArray(buff)
assert.Equal(t, buff, result[:])
}

func TestTrimWhiteSpaceCharacters(t *testing.T) {
t.Parallel()

data := "aaII139HSAh32q782!$#*$(nc"

input := " " + data
assert.Equal(t, data, TrimWhiteSpaceCharacters(input))

input = "\t " + data
assert.Equal(t, data, TrimWhiteSpaceCharacters(input))

input = "\t " + data + "\n"
assert.Equal(t, data, TrimWhiteSpaceCharacters(input))

input = "\t\n " + data + "\n\n\n\n\t"
assert.Equal(t, data, TrimWhiteSpaceCharacters(input))
}

0 comments on commit 706e0e9

Please sign in to comment.