Skip to content

Commit

Permalink
Merge pull request #82 from proximax-storage/keypair_generate
Browse files Browse the repository at this point in the history
Added keypair generator with pvkey #80 #77
  • Loading branch information
BramBear authored Oct 14, 2018
2 parents f797c44 + af849db commit c46fa63
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 49 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
4 changes: 2 additions & 2 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 Down
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
2 changes: 1 addition & 1 deletion sdk/mosaic.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,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
2 changes: 1 addition & 1 deletion 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
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
6 changes: 3 additions & 3 deletions sdk/transaction_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (dto *abstractTransactionDTO) toStruct(tInfo *TransactionInfo) (*abstractTr
return nil, err
}

pa, err := NewPublicAccount(dto.Signer, nt)
pa, err := NewAccountFromPublicKey(dto.Signer, nt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1405,7 +1405,7 @@ type aggregateTransactionCosignatureDTO struct {
}

func (dto *aggregateTransactionCosignatureDTO) toStruct(networkType NetworkType) (*AggregateTransactionCosignature, error) {
acc, err := NewPublicAccount(dto.Signer, networkType)
acc, err := NewAccountFromPublicKey(dto.Signer, networkType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1449,7 +1449,7 @@ type multisigCosignatoryModificationDTO struct {
}

func (dto *multisigCosignatoryModificationDTO) toStruct(networkType NetworkType) (*MultisigCosignatoryModification, error) {
acc, err := NewPublicAccount(dto.PublicAccount, networkType)
acc, err := NewAccountFromPublicKey(dto.PublicAccount, networkType)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit c46fa63

Please sign in to comment.