From c287f6cddf56d2aa7658e92695fad115b9de03d9 Mon Sep 17 00:00:00 2001 From: Apisit Toompakdee Date: Wed, 6 Jun 2018 15:55:01 +0900 Subject: [PATCH] nep6 mobile method --- neoutils/mobile.go | 11 +++++++++++ neoutils/mobile_test.go | 18 ++++++++++++++++++ neoutils/neowallet_test.go | 2 +- neoutils/nep6/nep6.go | 4 ++-- neoutils/nep6/nep6_test.go | 6 +++--- neoutils/utils.go | 23 +++++++++++++++++++++++ neoutils/utils_test.go | 7 +++++++ neoutils/version.go | 5 ++++- 8 files changed, 69 insertions(+), 7 deletions(-) diff --git a/neoutils/mobile.go b/neoutils/mobile.go index bcbb046..a3d8f65 100644 --- a/neoutils/mobile.go +++ b/neoutils/mobile.go @@ -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" ) @@ -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) +} diff --git a/neoutils/mobile_test.go b/neoutils/mobile_test.go index 5350b13..dd7a388 100644 --- a/neoutils/mobile_test.go +++ b/neoutils/mobile_test.go @@ -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" ) @@ -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) + +} diff --git a/neoutils/neowallet_test.go b/neoutils/neowallet_test.go index afa5e61..14d484f 100644 --- a/neoutils/neowallet_test.go +++ b/neoutils/neowallet_test.go @@ -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) diff --git a/neoutils/nep6/nep6.go b/neoutils/nep6/nep6.go index 538b888..e3ed570 100644 --- a/neoutils/nep6/nep6.go +++ b/neoutils/nep6/nep6.go @@ -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, @@ -61,5 +61,5 @@ func NewNEP6WithNEP2EncryptedKey(name string, addressLabel string, address strin }, } nep6.Accounts = append(nep6.Accounts, account) - return nep6 + return &nep6 } diff --git a/neoutils/nep6/nep6_test.go b/neoutils/nep6/nep6_test.go index 82c2d7a..c230c7c 100644 --- a/neoutils/nep6/nep6_test.go +++ b/neoutils/nep6/nep6_test.go @@ -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) { @@ -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 diff --git a/neoutils/utils.go b/neoutils/utils.go index 6fdf787..580aff2 100644 --- a/neoutils/utils.go +++ b/neoutils/utils.go @@ -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 { @@ -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 +} diff --git a/neoutils/utils_test.go b/neoutils/utils_test.go index 124a14a..d49f2ff 100644 --- a/neoutils/utils_test.go +++ b/neoutils/utils_test.go @@ -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) +} diff --git a/neoutils/version.go b/neoutils/version.go index 8bcdcea..f8ead6e 100644 --- a/neoutils/version.go +++ b/neoutils/version.go @@ -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