Skip to content

Commit

Permalink
Merge pull request #83 from proximax-storage/development
Browse files Browse the repository at this point in the history
Merge from Developmern to Master for 0.0.2 - 14102018
  • Loading branch information
BramBear authored Oct 14, 2018
2 parents 8bcb806 + c46fa63 commit 5bd549c
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 136 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Create a Catapult network configuration

Using the *Testnet* network
```go
conf, err := sdk.LoadTestnetConfig("http://localhost:3000")
conf, err := sdk.NewConfig("http://localhost:3000",sdk.Testnet)
```
Or using the *Mainnet* network
```go
conf, err := sdk.LoadMainnetConfig("http://localhost:3000")
conf, err := sdk.NewConfig("http://localhost:3000",sdk.Mainnet)
```

Construct a new Catapult client
Expand Down
4 changes: 2 additions & 2 deletions crypto/key_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PrivateKey struct {
Raw []byte
}


// NewPrivateKey creates a new private key from []byte
func NewPrivateKey(raw []byte) *PrivateKey {
return &PrivateKey{(&big.Int{}).SetBytes(raw), raw}
Expand Down Expand Up @@ -64,8 +65,7 @@ func NewPrivateKeyfromDecimalString(decimal string) (*PrivateKey, error) {
}

func (ref *PrivateKey) String() string {

return string(ref.Raw)
return hex.EncodeToString(ref.Raw)
}

//PublicKey Represents a public key.
Expand Down
7 changes: 5 additions & 2 deletions examples/blockchain_api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import (
"math/big"
)

const baseUrl = "http://localhost:3000"
const (
baseUrl = "http://localhost:3000"
networkType = sdk.MijinTest
)

// Simple Blockchain API request
func main() {

conf, err := sdk.LoadTestnetConfig(baseUrl)
conf, err := sdk.NewConfig(baseUrl,networkType)
if err != nil {
panic(err)
}
Expand Down
47 changes: 21 additions & 26 deletions examples/websocket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const (
// The notification is received in real time without having to poll the API waiting for a reply.
func main() {

conf, err := sdk.LoadTestnetConfig(baseUrl)
conf, err := sdk.NewConfig(baseUrl,networkType)
if err != nil {
panic(err)
}

acc, err := sdk.NewAccount(privateKey, networkType)
acc, err := sdk.NewAccountFromPrivateKey(privateKey, networkType)

ws, err := sdk.NewConnectWs(baseUrl)
if err != nil {
Expand All @@ -38,41 +38,37 @@ func main() {
// The UnconfirmedAdded channel notifies when a transaction related to an
// address is in unconfirmed state and waiting to be included in a block.
// The message contains the transaction.
a, _ := ws.Subscribe.UnconfirmedAdded(acc.Address.Address)
chUnconfirmedAdded, _ := ws.Subscribe.UnconfirmedAdded(acc.Address.Address)
go func() {
for {
data := <-a.ChIn
ch := data.(sdk.Transaction)
fmt.Printf("UnconfirmedAdded Tx Hash: %v \n", ch.GetAbstractTransaction().Hash)
a.Unsubscribe()
data := <-chUnconfirmedAdded.Ch
fmt.Printf("UnconfirmedAdded Tx Hash: %v \n", data.GetAbstractTransaction().Hash)
chUnconfirmedAdded.Unsubscribe()
}
}()

// The confirmedAdded channel notifies when a transaction related to an
// address is included in a block. The message contains the transaction.
b, _ := ws.Subscribe.ConfirmedAdded(acc.Address.Address)
//
//// The confirmedAdded channel notifies when a transaction related to an
//// address is included in a block. The message contains the transaction.
chConfirmedAdded, _ := ws.Subscribe.ConfirmedAdded(acc.Address.Address)
go func() {
for {
data := <-b.ChIn
ch := data.(sdk.Transaction)
fmt.Printf("ConfirmedAdded Tx Hash: %v \n", ch.GetAbstractTransaction().Hash)
b.Unsubscribe()
data := <-chConfirmedAdded.Ch
fmt.Printf("ConfirmedAdded Tx Hash: %v \n", data.GetAbstractTransaction().Hash)
chConfirmedAdded.Unsubscribe()
fmt.Println("Successful transfer!")

}
}()

//The status channel notifies when a transaction related to an address rises an error.
//The message contains the error message and the transaction hash.
c, _ := ws.Subscribe.Status(acc.Address.Address)
chStatus, _ := ws.Subscribe.Status(acc.Address.Address)

go func() {
for {
data := <-c.ChIn
ch := data.(sdk.StatusInfo)
c.Unsubscribe()
fmt.Printf("Hash: %v \n", ch.Hash)
panic(fmt.Sprint("Status: ", ch.Status))
data := <-chStatus.Ch
chStatus.Unsubscribe()
fmt.Printf("Hash: %v \n", data.Hash)
panic(fmt.Sprint("Status: ", data.Status))
}
}()

Expand Down Expand Up @@ -105,11 +101,10 @@ func main() {

// The block channel notifies for every new block.
// The message contains the block information.
d, _ := ws.Subscribe.Block()
chBlock, _ := ws.Subscribe.Block()

for {
data := <-d.ChIn
ch := data.(*sdk.BlockInfo)
fmt.Printf("Block received with height: %v \n", ch.Height)
data := <-chBlock.Ch
fmt.Printf("Block received with height: %v \n", data.Height)
}
}
2 changes: 2 additions & 0 deletions sdk/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

type AccountService service



func (a *AccountService) GetAccountInfo(ctx context.Context, address *Address) (*AccountInfo, *http.Response, error) {
dto := &accountInfoDTO{}

Expand Down
25 changes: 19 additions & 6 deletions sdk/account_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (dto *multisigAccountInfoDTO) toStruct(networkType NetworkType) (*MultisigA
cs := make([]*PublicAccount, len(dto.Multisig.Cosignatories))
ms := make([]*PublicAccount, len(dto.Multisig.MultisigAccounts))

acc, err := NewPublicAccount(dto.Multisig.Account, networkType)
acc, err := NewAccountFromPublicKey(dto.Multisig.Account, networkType)
if err != nil {
return nil, err
}
Expand All @@ -214,14 +214,14 @@ func (dto *multisigAccountInfoDTO) toStruct(networkType NetworkType) (*MultisigA
go func() {
defer wg.Done()
for i, c := range dto.Multisig.Cosignatories {
cs[i], err = NewPublicAccount(c, networkType)
cs[i], err = NewAccountFromPublicKey(c, networkType)
}
}()

go func() {
defer wg.Done()
for i, m := range dto.Multisig.MultisigAccounts {
ms[i], err = NewPublicAccount(m, networkType)
ms[i], err = NewAccountFromPublicKey(m, networkType)
}
}()

Expand Down Expand Up @@ -285,7 +285,20 @@ func (dto multisigAccountGraphInfoDTOS) toStruct(networkType NetworkType) (*Mult

var addressError = errors.New("wrong address")

func NewAccount(pKey string, networkType NetworkType) (*Account, error) {
func NewAccount(networkType NetworkType) (*Account, error) {
kp, err := crypto.NewKeyPairByEngine(crypto.CryptoEngines.DefaultEngine)
if err != nil {
return nil, err
}

pa, err := NewAccountFromPublicKey(kp.PublicKey.String(), networkType)
if err != nil {
return nil, err
}

return &Account{pa, kp}, nil
}
func NewAccountFromPrivateKey(pKey string, networkType NetworkType) (*Account, error) {
k, err := crypto.NewPrivateKeyfromHexString(pKey)
if err != nil {
return nil, err
Expand All @@ -296,15 +309,15 @@ func NewAccount(pKey string, networkType NetworkType) (*Account, error) {
return nil, err
}

pa, err := NewPublicAccount(kp.PublicKey.String(), networkType)
pa, err := NewAccountFromPublicKey(kp.PublicKey.String(), networkType)
if err != nil {
return nil, err
}

return &Account{pa, kp}, nil
}

func NewPublicAccount(pKey string, networkType NetworkType) (*PublicAccount, error) {
func NewAccountFromPublicKey(pKey string, networkType NetworkType) (*PublicAccount, error) {
ad, err := NewAddressFromPublicKey(pKey, networkType)
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions sdk/account_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ var testAddressesForEncoded = map[NetworkType]string{
MainNet: "NARNASAS2BIAB6LMFA3FPMGBPGIJGK6IJFJKUV32",
}

func TestGenerateNewAccount_NEM(t *testing.T) {
acc, err := NewAccount(MijinTest)
if err != nil {
t.Fatal("Error")
}
a := acc.KeyPair.PrivateKey.String()
t.Log("Private Key: " + a)

assert.NotNil(t,acc.KeyPair.PrivateKey.String(),"Error generating new KeyPair")


}
func TestGenerateEncodedAddress_NEM(t *testing.T) {

for nType, testAddress := range testAddressesForEncoded {
Expand Down
2 changes: 1 addition & 1 deletion sdk/blockchain_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type blockInfoDTO struct {
func (dto *blockInfoDTO) toStruct() (*BlockInfo, error) {
nt := ExtractNetworkType(dto.Block.Version)

pa, err := NewPublicAccount(dto.Block.Signer, nt)
pa, err := NewAccountFromPublicKey(dto.Block.Signer, nt)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var wantBlockInfo *BlockInfo
func init() {
addRouters(bcRouters)

pubAcc, _ := NewPublicAccount("321DE652C4D3362FC2DDF7800F6582F4A10CFEA134B81F8AB6E4BE78BBA4D18E", MijinTest)
pubAcc, _ := NewAccountFromPublicKey("321DE652C4D3362FC2DDF7800F6582F4A10CFEA134B81F8AB6E4BE78BBA4D18E", MijinTest)

wantBlockInfo = &BlockInfo{
NetworkType: MijinTest,
Expand Down
5 changes: 2 additions & 3 deletions sdk/mosaic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type MosaicService service

// mosaics get mosaics Info
// @get /mosaic/{mosaicId}
func (ref *MosaicService) GetMosaic(ctx context.Context, mosaicId MosaicId) (mscInfo *MosaicInfo, resp *http.Response, err error) {
func (ref *MosaicService) GetMosaic(ctx context.Context, mosaicId *MosaicId) (mscInfo *MosaicInfo, resp *http.Response, err error) {

mscInfoDTO := &mosaicInfoDTO{}
resp, err = ref.client.DoNewRequest(ctx, "GET", pathMosaic+mosaicId.toHexString(), nil, mscInfoDTO)
Expand Down Expand Up @@ -80,7 +80,6 @@ func (ref *MosaicService) GetMosaicNames(ctx context.Context, mosaicIds MosaicId
}

// GetMosaicsFromNamespace Get mosaics information from namespaceId (nsId)
// get @/namespaces/{namespaceId}/mosaic/
func (ref *MosaicService) GetMosaicsFromNamespace(ctx context.Context, namespaceId *NamespaceId, mosaicId *MosaicId,
pageSize int) (mscInfo []*MosaicInfo, resp *http.Response, err error) {

Expand Down Expand Up @@ -155,7 +154,7 @@ func (dto mosaicPropertiesDTO) toStruct() *MosaicProperties {

func (ref *mosaicInfoDTO) setMosaicInfo() (*MosaicInfo, error) {

publicAcc, err := NewPublicAccount(ref.Mosaic.Owner, NetworkType(1))
publicAcc, err := NewAccountFromPublicKey(ref.Mosaic.Owner, NetworkType(1))
if err != nil {
return nil, err
}
Expand Down
11 changes: 3 additions & 8 deletions sdk/mosaic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var (

func TestMosaicService_GetMosaic(t *testing.T) {

mscInfo, resp, err := serv.Mosaic.GetMosaic(ctx, *testMosaicId)
mscInfo, resp, err := serv.Mosaic.GetMosaic(ctx, testMosaicId)
if err != nil {
t.Error(err)
} else if validateResp(resp, t) && validateMosaicInfo(mscInfo, t) {
Expand Down Expand Up @@ -137,7 +137,6 @@ func TestMosaicService_GetMosaicNames(t *testing.T) {
}

func TestMosaicService_GetMosaicsFromNamespace(t *testing.T) {

mscInfoArr, resp, err := serv.Mosaic.GetMosaicsFromNamespace(ctx, testNamespaceId, testMosaicId, pageSize)
if err != nil {
t.Error(err)
Expand All @@ -150,6 +149,7 @@ func TestMosaicService_GetMosaicsFromNamespace(t *testing.T) {
if err != nil {
t.Error(err)
} else if validateResp(resp, t) {
t.Log(t)
t.Logf("%v", mscInfoArr)

}
Expand All @@ -159,11 +159,6 @@ func TestMosaicService_GetMosaicsFromNamespace(t *testing.T) {
const iter = 1000

func TestMosaicService_GetMosaicsFromNamespasceExt(t *testing.T) {

cfg, _ := LoadMainnetConfig("http://190.216.224.11:3000")

serv := NewClient(nil, cfg)

h, _, err := serv.Blockchain.GetBlockchainHeight(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -213,7 +208,7 @@ func TestMosaicService_GetMosaicsFromNamespasceExt(t *testing.T) {
t.Log(tran)
continue
}
mscInfo, resp, err := serv.Mosaic.GetMosaic(ctx, *tran.MosaicId)
mscInfo, resp, err := serv.Mosaic.GetMosaic(ctx, tran.MosaicId)
if err != nil {
t.Error(err)
} else if validateResp(resp, t) {
Expand Down
4 changes: 2 additions & 2 deletions sdk/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type namespaceInfoDTO struct {

//getNamespaceInfo create & return new NamespaceInfo from namespaceInfoDTO
func (ref *namespaceInfoDTO) getNamespaceInfo() (*NamespaceInfo, error) {
pubAcc, err := NewPublicAccount(ref.Namespace.Owner, NetworkType(ref.Namespace.Type))
pubAcc, err := NewAccountFromPublicKey(ref.Namespace.Owner, NetworkType(ref.Namespace.Type))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func listNamespaceInfoFromDTO(res []*namespaceInfoDTO, nsList *ListNamespaceInfo
if err != nil {
return err
}
nsList.list = append(nsList.list, nsInfo)
nsList.List = append(nsList.List, nsInfo)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion sdk/namespace_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (ref *NamespaceInfo) String() string {

// ListNamespaceInfo is a list NamespaceInfo
type ListNamespaceInfo struct {
list []*NamespaceInfo
List []*NamespaceInfo
}

// generateNamespaceId create NamespaceId from namespace string name (ex: nem or domain.subdom.subdome)
Expand Down
8 changes: 4 additions & 4 deletions sdk/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func TestNamespaceService_GetNamespacesFromAccount(t *testing.T) {
if err != nil {
t.Error(err)
} else if validateResp(resp, t) {
if len(nsInfoArr.list) != 1 {
if len(nsInfoArr.List) != 1 {
t.Error("return result must have length = 1")
} else {
isValid := true
for _, nsInfo := range nsInfoArr.list {
for _, nsInfo := range nsInfoArr.List {
isValid = isValid && validateNamespaceInfo(nsInfo, t)
}
if isValid {
Expand All @@ -137,11 +137,11 @@ func TestNamespaceService_GetNamespacesFromAccounts(t *testing.T) {
if err != nil {
t.Error(err)
} else if validateResp(resp, t) {
if len(nsInfoArr.list) != 1 {
if len(nsInfoArr.List) != 1 {
t.Error("return result must have length = 1")
} else {
isValid := true
for _, nsInfo := range nsInfoArr.list {
for _, nsInfo := range nsInfoArr.List {
isValid = isValid && validateNamespaceInfo(nsInfo, t)
}
if isValid {
Expand Down
2 changes: 1 addition & 1 deletion sdk/sdk_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package sdk

import (
"encoding/binary"
"github.com/Wondertan/nem2-sdk-go/utils"
"github.com/proximax-storage/nem2-sdk-go/utils"
"math/big"
"strconv"
)
Expand Down
Loading

0 comments on commit 5bd549c

Please sign in to comment.