Skip to content

Commit

Permalink
Merge pull request #13 from myndshft/4
Browse files Browse the repository at this point in the history
4
  • Loading branch information
tylerwince authored Apr 2, 2018
2 parents aec463a + 21f52a5 commit 7375f23
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ install:
script:
- go test ./...

# TODO: Is this the right slack channel?
notifications:
slack: myndshfttech:eye5HmlClH9OThrY2xqdn3CN
email: false

matrix:
allow_failures:
- go: tip
11 changes: 10 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 52 additions & 6 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"
)

// AccountInfo describes basic information for an account.
Expand Down Expand Up @@ -119,13 +121,57 @@ func (c Client) GetBatchAccountData(addresses []string) ([]AccountMetadataPair,
return data.Data, nil
}

// AccountInfo gets all information for a given address
func (c Client) AccountInfo(address string) (AccountMetadataPair, error) {
type accountName interface {
isValid() bool
String() string
}

// Address is a Nem account address as a string
type Address string

func (a Address) isValid() bool {
// check if address is valid
return true
}

func (a Address) String() string {
return string(a)
}

// PublicKey is a Nem account public key as a string
type PublicKey string

func (pk PublicKey) isValid() bool {
// check if address is valid
return true
}

func (pk PublicKey) String() string {
return string(pk)
}

// AccountData gets all information for a given address
func (c Client) AccountData(acc accountName) (AccountMetadataPair, error) {
var data AccountMetadataPair
c.url.Path = "/account/get"
req, err := c.buildReq(map[string]string{"address": address}, nil, http.MethodGet)
if err != nil {
return data, err
var req *http.Request
var err error
if acc.isValid() {
switch acc.(type) {
case Address:
c.url.Path = "/account/get"
req, err = c.buildReq(map[string]string{"address": acc.String()}, nil, http.MethodGet)
if err != nil {
return data, err
}
case PublicKey:
c.url.Path = "/account/get/from-public-key"
req, err = c.buildReq(map[string]string{"publicKey": acc.String()}, nil, http.MethodGet)
if err != nil {
return data, err
}
default:
return data, errors.New("Use an Address or PublicKey type")
}
}
body, err := c.request(req)
if err != nil {
Expand Down
40 changes: 39 additions & 1 deletion account_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Myndshft Technologies, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nemgo

import (
Expand Down Expand Up @@ -64,7 +78,31 @@ func TestAccountInfo(t *testing.T) {
RemoteStatus: "ACTIVE",
CosignatoryOf: []AccountInfo{},
Cosignatories: []AccountInfo{}}}
got, err := clientMock.AccountInfo("TBCI2A67UQZAKCR6NS4JWAEICEIGEIM72G3MVW5S")
got, err := clientMock.AccountData(Address("TBCI2A67UQZAKCR6NS4JWAEICEIGEIM72G3MVW5S"))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("\nWanted: %v\n Got: %v", want, got)
}
}

func TestAccountInfoPrivateKey(t *testing.T) {
want := AccountMetadataPair{
Account: AccountInfo{
Address: "TBCI2A67UQZAKCR6NS4JWAEICEIGEIM72G3MVW5S",
Balance: 124446551689680,
VestedBalance: 1041345514976241,
Importance: 0.010263666447108395,
PublicKey: "a11a1a6c17a24252e674d151713cdf51991ad101751e4af02a20c61b59f1fe1a",
Label: "",
HarvestedBlocks: 645},
Meta: AccountMetadata{
Status: "LOCKED",
RemoteStatus: "ACTIVE",
CosignatoryOf: []AccountInfo{},
Cosignatories: []AccountInfo{}}}
got, err := clientMock.AccountData(PublicKey("a11a1a6c17a24252e674d151713cdf51991ad101751e4af02a20c61b59f1fe1a"))
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 6 additions & 9 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,15 @@ func (c Client) Score() (string, error) {
// seconds since the nemesis have elapsed. This common time is called network
// time.
type Block struct {
// BUG(tyler): This should really be an int
// BUG(tyler): All float64 in Block should be int
TimeStamp float64
Signature string
PrevBlockHash string
// BUG(tyler): This should really be an int
Type float64
Transactions []Transaction
// BUG(tyler): This should really be an int
Version float64
Signer string
// BUG(tyler): This should really be an int
Height float64
Type float64
Transactions []Transaction
Version float64
Signer string
Height float64
}

// UnmarshalJSON implements a custom JSON unmarshaller for Blocks
Expand Down
16 changes: 15 additions & 1 deletion headers.go → crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package nemgo
package crypto

// func AccountFromPubKey(pubkey string, network nemgo.Network) (string, error) {
// h := sha3.Sum256([]byte(pubkey))
// r := ripemd160.New()
// _, err := r.Write(h[:])
// if err != nil {
// return "", err
// }
// b := append(network, r.Sum(nil)...)
// h = sha3.Sum256(b)
// a := append(b, h[:4])
// return a

// }
14 changes: 14 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Myndshft Technologies, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package nemgo is a pure golang implementation of an SDK for interacting with the
// NEM blockchain through the NEM Infrastructure Server (NIS) api.
package nemgo
27 changes: 27 additions & 0 deletions examples/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build OMIT

package main

import (
"fmt"
"log"

"github.com/myndshft/nemgo"
)

func main() {
// Connect to the mainnet
client := nemgo.New()
// Obtain account data using address
a, err := client.AccountData(nemgo.Address("NBMBTUB6JIXGBSETDJBMCGLB2GPTI6GMPAYFNH3P"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", a)
// Obtain account data using public key
b, err := client.AccountData(nemgo.PublicKey("4f790ccd43426dcb663a80283294ed3e852a855723d87763a7608f1b45d27e20"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", b)
}
2 changes: 2 additions & 0 deletions examples/stream.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build OMIT

package main

import (
Expand Down
2 changes: 1 addition & 1 deletion namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNamespace(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// BUG(tyler): reflect.DeepEqual shows these are different, but they aren't
// BUG(tyler): reflect.DeepEqual shows Namespaces are different in test incorrectly
if want != got {
t.Fatalf("\nWanted: %v\n Got: %v", want, got)
}
Expand Down
18 changes: 17 additions & 1 deletion nemgo.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Myndshft Technologies, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nemgo

import (
Expand All @@ -9,7 +23,9 @@ import (
)

const (
// Testnet is a helper const to connect to the testnet
Testnet = byte(0x98)
// Mainnet is a helper const to connect to the mainnet
Mainnet = byte(0x68)
)

Expand Down Expand Up @@ -44,7 +60,7 @@ func WithNIS(host string, network byte) Option {
// defaulting to the NEM mainnet
func New(opts ...Option) *Client {
c := &Client{
network: byte(0x68),
network: Mainnet,
url: url.URL{Scheme: "http", Host: "209.126.98.204:7890"},
request: sendReq}

Expand Down
28 changes: 24 additions & 4 deletions nemgo_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Myndshft Technologies, Inc.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nemgo

import (
Expand All @@ -10,17 +24,18 @@ import (

func TestNewWithOptions(t *testing.T) {
want := Client{
network: byte(0x98),
network: Testnet,
url: url.URL{Scheme: "http", Host: "23.228.67.85:7890"},
request: sendReq}
got := New(WithNIS("23.228.67.85:7890", byte(0x98)))
got := New(WithNIS("23.228.67.85:7890", Testnet))
if reflect.DeepEqual(want, got) {
t.Fatalf("\nWanted: %v\n Got: %v", want, got)
}
}

func TestNew(t *testing.T) {
want := Client{
network: Mainnet,
url: url.URL{Scheme: "http", Host: "209.126.98.204:7890"},
request: sendReq}
got := New()
Expand All @@ -29,17 +44,22 @@ func TestNew(t *testing.T) {
}
}

// ExampleNew shows how to create a new NEM client
func ExampleNew() {
c := New()
// use c here
// use c
// you can also create a client using a custom NIS
c = New(WithNIS("YOUR.CUSTOM.NIS.HERE:7890", Mainnet))
// use c
fmt.Println(c)

}

func sendReqMock(req *http.Request) ([]byte, error) {
switch req.URL.Path {
case "/account/batch":
return []byte(accountMetaDataPairNested), nil
case "/account/get", "/account/get/forwarded":
case "/account/get", "/account/get/forwarded", "/account/get/from-public-key":
return []byte(accountMetaDataPair), nil
case "/account/status":
return []byte(accountMetaData), nil
Expand Down
1 change: 1 addition & 0 deletions nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type identity struct {
PublicKey string
}

// Node is a node on the Nem blockchain
type Node struct {
MetaData metadata
Endpoint endpoint
Expand Down
21 changes: 21 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,24 @@ func (c Client) AllTransactions(address string) ([]TransactionMetadataPair, erro
}
return data.Data, nil
}

// TODO make this work on a transaction object
// // WithMosaic is an Option used for CreateTransaction
// func WithMosaic(mosaic string, amt int) Option {
// return func(c *Client) {}
// }

// TODO put together the "sending transaction"
// type SendTransaction struct {
// Name string
// Amount string
// }

// CreateTransaction will initialize a transaction on the nem blockchain
func (c Client) CreateTransaction(toAct string, xemAmt int, opts ...Option) (TransactionMetadataPair, error) {
var data TransactionMetadataPair
// for _, opt := range opts {

// }
return data, nil
}
Loading

0 comments on commit 7375f23

Please sign in to comment.