Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Added support to convert 1handle to [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Sep 17, 2020
1 parent 1f88d16 commit b97bc1d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
const (

// version is the current version
version = "v0.4.2"
version = "v0.4.3"

// defaultUserAgent is the default user agent for all requests
defaultUserAgent string = "go-polynym: " + version
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNewClient(t *testing.T) {
func ExampleNewClient() {
client := NewClient(nil)
fmt.Println(client.UserAgent)
// Output:go-polynym: v0.4.2
// Output:go-polynym: v0.4.3
}

// BenchmarkNewClient benchmarks the NewClient method
Expand Down
2 changes: 1 addition & 1 deletion mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (m *mockHTTP) Do(req *http.Request) (*http.Response, error) {
// Invalid paymail
resp.Body = invalidResponse("PayMail not found", req.URL.String(), resp.StatusCode)

} else if strings.Contains(req.URL.String(), "/1mrz") {
} else if strings.Contains(req.URL.String(), "/[email protected]") {

// Valid 1handle
resp.StatusCode = http.StatusOK
Expand Down
26 changes: 24 additions & 2 deletions polynym.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ type GetAddressResponse struct {
// GetAddress returns the address of a given 1handle, $handcash, paymail, Twetch user id or BitcoinSV address
func GetAddress(client Client, handleOrPaymail string) (response *GetAddressResponse, err error) {

// Convert handle to paymail if detected
if strings.Contains(handleOrPaymail, "$") {
handleOrPaymail = HandCashConvert(handleOrPaymail, false)
} else if strings.HasPrefix(handleOrPaymail, "1") && len(handleOrPaymail) < 25 {
handleOrPaymail = RelayXConvert(handleOrPaymail)
}

// Set the API url
// todo: beta is temporary, and only used via the method directly
reqURL := fmt.Sprintf("%s/%s/%s", apiEndpoint, "getAddress", HandCashConvert(handleOrPaymail, false))
reqURL := fmt.Sprintf("%s/%s/%s", apiEndpoint, "getAddress", handleOrPaymail)

// Store for debugging purposes
response = &GetAddressResponse{
Expand Down Expand Up @@ -71,7 +78,10 @@ func GetAddress(client Client, handleOrPaymail string) (response *GetAddressResp

// Cleanup
defer func() {
_ = resp.Body.Close()
if resp.Body != nil {
_ = resp.Body.Close()
}

}()

// Set the status
Expand All @@ -82,6 +92,10 @@ func GetAddress(client Client, handleOrPaymail string) (response *GetAddressResp

// Decode the error message
if resp.StatusCode == http.StatusBadRequest {
if resp.Body == nil {
err = fmt.Errorf("no response body found")
return
}
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return
}
Expand Down Expand Up @@ -112,3 +126,11 @@ func HandCashConvert(handle string, isBeta bool) string {
}
return handle
}

// RelayXConvert now converts 1handle to paymail: [email protected]
func RelayXConvert(handle string) string {
if strings.HasPrefix(handle, "1") && len(handle) < 25 {
return strings.ToLower(strings.Replace(handle, "1", "", -1)) + "@relayx.io"
}
return handle
}
40 changes: 40 additions & 0 deletions polynym_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,43 @@ func BenchmarkHandCashConvert(b *testing.B) {
_ = HandCashConvert("$mr-z", false)
}
}

// TestRelayXConvert will test the RelayXConvert() method
func TestRelayXConvert(t *testing.T) {
t.Parallel()

// Create the list of tests
var tests = []struct {
input string
expected string
}{
{"1mr-z", "[email protected]"},
{"invalid1mr-z", "invalid1mr-z"},
{"1", "@relayx.io"},
{"1mrz", "[email protected]"},
{"1handle", "[email protected]"},
{"1misterz", "[email protected]"},
{"[email protected]", "[email protected]"},
}

// Test all
for _, test := range tests {
if output := RelayXConvert(test.input); output != test.expected {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expected, output)
}
}
}

// ExampleHandCashConvert example using RelayXConvert()
func ExampleRelayXConvert() {
paymail := RelayXConvert("1mr-z")
fmt.Println(paymail)
// Output:[email protected]
}

// BenchmarkHandCashConvert benchmarks the RelayXConvert method
func BenchmarkRelayXConvert(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = RelayXConvert("1mr-z")
}
}

0 comments on commit b97bc1d

Please sign in to comment.