From 8889973118cc78622f2b54b0faf7179bc6ea7d44 Mon Sep 17 00:00:00 2001 From: revett Date: Fri, 27 Oct 2017 21:57:12 +0100 Subject: [PATCH 1/5] New RPC function: .ValidateAddress() --- neo/client.go | 38 +++++++++++++++++++++++++++++++ neo/client_test.go | 15 ++++++++++++ neo/models/response/string_map.go | 11 +++++++++ 3 files changed, 64 insertions(+) create mode 100644 neo/models/response/string_map.go diff --git a/neo/client.go b/neo/client.go index 2310118..0fba90e 100644 --- a/neo/client.go +++ b/neo/client.go @@ -184,3 +184,41 @@ func (c Client) Ping() bool { return true } + +// ValidateAddress takes a public NEO address and checks if it is valid. +func (c Client) ValidateAddress(address string) (bool, error) { + requestBodyParams := []interface{}{ + address, + } + var response response.StringMap + + err := executeRequest("validateaddress", requestBodyParams, c.NodeURI, &response) + if err != nil { + return false, err + } + + if _, ok := response.Result["address"]; !ok { + return false, nil + } + + if _, ok := response.Result["address"].(string); !ok { + return false, nil + } + + if _, ok := response.Result["isvalid"]; !ok { + return false, nil + } + + if _, ok := response.Result["isvalid"].(bool); !ok { + return false, nil + } + + returnedAddress := response.Result["address"].(string) + valid := response.Result["isvalid"].(bool) + + if address == returnedAddress && valid { + return true, nil + } + + return false, nil +} diff --git a/neo/client_test.go b/neo/client_test.go index 4218c64..384d79e 100644 --- a/neo/client_test.go +++ b/neo/client_test.go @@ -175,5 +175,20 @@ func TestClient(t *testing.T) { }) } }) + + t.Run(".ValidateAddress()", func(t *testing.T) { + t.Run("HappyCase", func(t *testing.T) { + client := neo.NewClient(nodeURI) + + for _, testAccount := range testAccounts { + t.Run(testAccount.publicAddress, func(t *testing.T) { + isValid, err := client.ValidateAddress(testAccount.publicAddress) + + assert.NoError(t, err) + assert.True(t, isValid) + }) + } + }) + }) }) } diff --git a/neo/models/response/string_map.go b/neo/models/response/string_map.go new file mode 100644 index 0000000..b1644bc --- /dev/null +++ b/neo/models/response/string_map.go @@ -0,0 +1,11 @@ +package response + +type ( + // StringMap represents the JSON schema of a response from a NEO node, where the + // expected result is an object where the keys are strings. + StringMap struct { + ID int `json:"id"` + JSONRPC string `json:"jsonrpc"` + Result map[string]interface{} `json:"result"` + } +) From f266f1b5a327ad7110101986253a218d1d2e5bd4 Mon Sep 17 00:00:00 2001 From: revett Date: Fri, 27 Oct 2017 21:57:28 +0100 Subject: [PATCH 2/5] 1.8.0 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index bd8bf88..27f9cd3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.7.0 +1.8.0 From 7a18a908e7a0f776a0f805008efcb88834a986f2 Mon Sep 17 00:00:00 2001 From: revett Date: Fri, 27 Oct 2017 22:02:43 +0100 Subject: [PATCH 3/5] Update fixtures --- neo/fixtures_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo/fixtures_test.go b/neo/fixtures_test.go index df4170d..36c4195 100644 --- a/neo/fixtures_test.go +++ b/neo/fixtures_test.go @@ -72,10 +72,10 @@ var ( }{ { asset: "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", - hash: "0xc515c4d2db27e06fd2305a5c5378f820d2c4cc04477ebe40ffa40b956eb4f8b5", + hash: "0xd30e2fe0bc3ecb1fde65f611a7f827268535ab19d2d2647d9984f35719185945", id: "1", index: 0, - value: "96", + value: "6", }, } From 49c631f4e391c368a83406b88a3fc1fc49c25779 Mon Sep 17 00:00:00 2001 From: revett Date: Fri, 27 Oct 2017 22:04:04 +0100 Subject: [PATCH 4/5] Sad testcase for new function --- neo/client_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/neo/client_test.go b/neo/client_test.go index 384d79e..eab648d 100644 --- a/neo/client_test.go +++ b/neo/client_test.go @@ -189,6 +189,14 @@ func TestClient(t *testing.T) { }) } }) + + t.Run("SadCase", func(t *testing.T) { + client := neo.NewClient(nodeURI) + isValid, err := client.ValidateAddress("wake-up-neo") + + assert.NoError(t, err) + assert.False(t, isValid) + }) }) }) } From 9359d3ac32baef707046aac392fae0c8bc39baaa Mon Sep 17 00:00:00 2001 From: revett Date: Fri, 27 Oct 2017 22:05:14 +0100 Subject: [PATCH 5/5] CHANGELOG --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38f1312..258e33a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## v1.8.0 - 2017-10-27 + +### Added + +- New function to validate a public NEO address. + +```golang +isValid, err := client.ValidateAddress("ARnLq3m1jsrZU7SS7jLHAvNm37GmaZbsPy") +``` + ## v1.7.0 - 2017-10-27 ### Added