Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #26 from O3Labs/nep6-mobile
Browse files Browse the repository at this point in the history
nep6 mobile method
  • Loading branch information
apisit committed Jun 6, 2018
2 parents 27a4563 + c287f6c commit c9e1812
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 7 deletions.
11 changes: 11 additions & 0 deletions neoutils/mobile.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package neoutils

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/o3labs/neo-utils/neoutils/nep6"
"github.com/o3labs/neo-utils/neoutils/o3"
"github.com/o3labs/neo-utils/neoutils/smartcontract"
)
Expand Down Expand Up @@ -111,3 +113,12 @@ func MintTokensRawTransactionMobile(network string, scriptHash string, wif strin
rawTransaction.TXID = txIDString
return rawTransaction, nil
}

func GenerateNEP6FromEncryptedKey(walletName, addressLabel, address, encryptedKey string) string {
nep6Wallet := nep6.NewNEP6WithNEP2EncryptedKey(walletName, addressLabel, address, encryptedKey)
b, err := json.Marshal(nep6Wallet)
if err != nil {
return ""
}
return string(b)
}
18 changes: 18 additions & 0 deletions neoutils/mobile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/o3labs/neo-utils/neoutils"
"github.com/o3labs/neo-utils/neoutils/nep2"
"github.com/o3labs/neo-utils/neoutils/smartcontract"
)

Expand All @@ -30,3 +31,20 @@ func TestMintTokensFromMobile(t *testing.T) {
log.Printf("txID =%v", tx.TXID)
log.Printf("tx = %x", tx.Data)
}

func TestNEP6MobileMethod(t *testing.T) {
passphase := "TestingOneTwoThree"
WIF := "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP" //AStZHy8E6StCqYQbzMqi4poH7YNDHQKxvt
encryptedKey, address, err := nep2.NEP2Encrypt(WIF, passphase)
if err != nil {
log.Printf("err %v", err)
return
}
log.Printf("encrypted = %v", encryptedKey)
walletName := "o3wallet"
addressLabel := "spending"

nep6Wallet := neoutils.GenerateNEP6FromEncryptedKey(walletName, addressLabel, address, encryptedKey)
log.Printf("%+v", nep6Wallet)

}
2 changes: 1 addition & 1 deletion neoutils/neowallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestGenKey(t *testing.T) {
}

func TestGenFromWIF(t *testing.T) {
wif := "L4Ns4Uh4WegsHxgDG49hohAYxuhj41hhxG6owjjTWg95GSrRRbLL"
wif := ""
wallet, err := neoutils.GenerateFromWIF(wif)
if err != nil {
log.Printf("%+v", err)
Expand Down
4 changes: 2 additions & 2 deletions neoutils/nep6/nep6.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type NEP6Wallet struct {
Extra interface{} `json:"extra"`
}

func NewNEP6WithNEP2EncryptedKey(name string, addressLabel string, address string, encryptedKey string) NEP6Wallet {
func NewNEP6WithNEP2EncryptedKey(name string, addressLabel string, address string, encryptedKey string) *NEP6Wallet {
account := NEP6Account{
Address: address,
Label: addressLabel,
Expand All @@ -61,5 +61,5 @@ func NewNEP6WithNEP2EncryptedKey(name string, addressLabel string, address strin
},
}
nep6.Accounts = append(nep6.Accounts, account)
return nep6
return &nep6
}
6 changes: 3 additions & 3 deletions neoutils/nep6/nep6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
"testing"

"github.com/o3labs/neo-utils/neoutils"
"github.com/o3labs/neo-utils/neoutils/nep2"
"github.com/o3labs/neo-utils/neoutils/nep6"
)

func TestNEWNEP6Wallet(t *testing.T) {
Expand All @@ -21,10 +21,10 @@ func TestNEWNEP6Wallet(t *testing.T) {
walletName := "o3wallet"
addressLabel := "spending"

nep6Wallet := nep6.NewNEP6WithNEP2EncryptedKey(walletName, addressLabel, address, encryptedKey)
nep6Wallet := neoutils.GenerateNEP6FromEncryptedKey(walletName, addressLabel, address, encryptedKey)
log.Printf("%+v", nep6Wallet)

b, err := json.Marshal(nep6Wallet.Accounts[0])
b, err := json.Marshal(nep6Wallet)
if err != nil {
t.Fail()
return
Expand Down
23 changes: 23 additions & 0 deletions neoutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

nep9 "github.com/o3labs/NEP9-go/nep9"
"github.com/o3labs/neo-utils/neoutils/btckey"
"golang.org/x/crypto/ripemd160"
)

func ReverseBytes(b []byte) []byte {
Expand Down Expand Up @@ -124,3 +125,25 @@ func Hash256(b []byte) []byte {
hash = sha256.Sum256(hash[:])
return hash[:]
}

func PublicKeyToNEOAddress(publicKeyBytes []byte) string {
publicKeyBytes = append([]byte{0x21}, publicKeyBytes...)
publicKeyBytes = append(publicKeyBytes, 0xAC)

/* SHA256 Hash */
sha256_h := sha256.New()
sha256_h.Reset()
sha256_h.Write(publicKeyBytes)
pub_hash_1 := sha256_h.Sum(nil)

/* RIPEMD-160 Hash */
ripemd160_h := ripemd160.New()
ripemd160_h.Reset()
ripemd160_h.Write(pub_hash_1)
pub_hash_2 := ripemd160_h.Sum(nil)

program_hash := pub_hash_2

address := btckey.B58checkencodeNEO(0x17, program_hash)
return address
}
7 changes: 7 additions & 0 deletions neoutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ func TestHash256(t *testing.T) {
t.Fail()
}
}

func TestPublicKeyToNEOAddress(t *testing.T) {
publicKey := "02ac3cecf6a5909c199fb5d6840577680c232fe3d5594c6514cc7897c4dd384728"
b, _ := hex.DecodeString(publicKey)
address := PublicKeyToNEOAddress(b)
log.Printf("%v", address)
}
5 changes: 4 additions & 1 deletion neoutils/version.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package neoutils

const (
VERSION = "1.0.7"
VERSION = "1.0.8"
)

//RELEASE NOTES

// V. 1.0.8
// - Added Generate NEP6 wallet for mobile

// V. 1.0.7
// - Make sure to round to fixed 8 decimals in output

Expand Down

0 comments on commit c9e1812

Please sign in to comment.