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 diff --git a/VERSION b/VERSION index bd8bf88..27f9cd3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.7.0 +1.8.0 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..eab648d 100644 --- a/neo/client_test.go +++ b/neo/client_test.go @@ -175,5 +175,28 @@ 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) + }) + } + }) + + 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) + }) + }) }) } 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", }, } 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"` + } +)