Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some examples #8

Merged
merged 1 commit into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Editor
.vscode
.idea
17 changes: 0 additions & 17 deletions .vscode/launch.json

This file was deleted.

35 changes: 35 additions & 0 deletions example/createaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"github.com/codemaveric/libra-go/pkg/librawallet"
"log"
"strings"
)

func main() {
// Change the mnemonic to something else,
// or you will get address used by others
mnemonic := "present good satochi coin future media giant"
wallet := librawallet.NewWalletLibrary(mnemonic)

// create a new address
address, childNum, err := wallet.NewAddress()
if err != nil {
log.Fatal(err)
}

fmt.Printf("Adddress: %s\n", address.ToString())

// Generate KeyPair with mnemonic and childNum
keyPair := librawallet.GenerateKeyPair(strings.Split(mnemonic, " "), childNum)

// Create Account from KeyPair
sourceAccount := librawallet.NewAccountFromKeyPair(keyPair)
fmt.Printf("Address from KeyPair: %s\n", sourceAccount.Address.ToString())

// show the private key
fmt.Printf("Private key: %x\n", sourceAccount.KeyPair.PrivateKey.Value)
}


61 changes: 61 additions & 0 deletions example/mintcoin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"github.com/codemaveric/libra-go/pkg/goclient"
"github.com/codemaveric/libra-go/pkg/librawallet"
"log"
"strings"
)

func main() {
// Change the mnemonic to something else,
// or you will get address used by others
mnemonic := "present good satochi coin future media giant"
wallet := librawallet.NewWalletLibrary(mnemonic)

// create a new address
address, childNum, err := wallet.NewAddress()
if err != nil {
log.Fatal(err)
}

fmt.Printf("Adddress: %s\n", address.ToString())

// Generate KeyPair with mnemonic and childNum
keyPair := librawallet.GenerateKeyPair(strings.Split(mnemonic, " "), childNum)

// Create Account from KeyPair
sourceAccount := librawallet.NewAccountFromKeyPair(keyPair)

// Libra Client Configuration
config := goclient.LibraClientConfig{
Host: "ac.testnet.libra.org",
Port: "80",
Network: goclient.TestNet,
}
// Instantiate LibraClient with Configuration
libraClient := goclient.NewLibraClient(config)

// Mint Coin from Test Faucet to account generated
err = libraClient.MintWithFaucetService(sourceAccount.Address.ToString(), 500000000, true)
if err != nil {
log.Fatal(err)
}
fmt.Println("Mint coin successfully.")

// Get Account State
SourceaccState, err := libraClient.GetAccountState(sourceAccount.Address.ToString())
if err != nil {
log.Fatal(err)
}

// The balance will not be 500000000 if you don't change the mnemonic,
// cause the account has been used multiple times
fmt.Printf("balance: %d\n", SourceaccState.Balance)

// Set the current account sequence
sourceAccount.Sequence = SourceaccState.SequenceNumber
}


98 changes: 98 additions & 0 deletions example/transfercoins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"fmt"
"github.com/codemaveric/libra-go/pkg/goclient"
"github.com/codemaveric/libra-go/pkg/librawallet"
"log"
"strings"
)

func main() {
// Change the mnemonic to something else,
// or you will get address used by others
mnemonic := "present good satochi coinfddw future media giant"
wallet := librawallet.NewWalletLibrary(mnemonic)

// create a new address
address1, childNum, err := wallet.NewAddress()
if err != nil {
log.Fatal(err)
}

fmt.Printf("Adddress1: %s\n", address1.ToString())

// Generate KeyPair with mnemonic and childNum
keyPair := librawallet.GenerateKeyPair(strings.Split(mnemonic, " "), childNum)

// Create Account from KeyPair
sourceAccount := librawallet.NewAccountFromKeyPair(keyPair)

// Libra Client Configuration
config := goclient.LibraClientConfig{
Host: "ac.testnet.libra.org",
Port: "80",
Network: goclient.TestNet,
}
// Instantiate LibraClient with Configuration
libraClient := goclient.NewLibraClient(config)

// Mint Coin from Test Faucet to account generated
err = libraClient.MintWithFaucetService(sourceAccount.Address.ToString(), 500000000, true)
if err != nil {
log.Fatal(err)
}
fmt.Println("Mint coin successfully.")

// Get Account State
SourceaccState, err := libraClient.GetAccountState(sourceAccount.Address.ToString())
if err != nil {
log.Fatal(err)
}

// Set the current account sequence
sourceAccount.Sequence = SourceaccState.SequenceNumber

// The balance will not be 500000000 if you don't change the mnemonic,
// cause the account has been used multiple times
fmt.Printf("Address1 balance: %d\n", SourceaccState.Balance)

// Create another address
address2, childNum, err := wallet.NewAddress()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Adddress2: %s\n", address2.ToString())

// Generate KeyPair with mnemonic and childNum
keyPair = librawallet.GenerateKeyPair(strings.Split(mnemonic, " "), childNum)

// Create Account from KeyPair
destAccount := librawallet.NewAccountFromKeyPair(keyPair)

var amount uint64 = 100000000
// Transfer Coins from source account to destination address
err = libraClient.TransferCoins(sourceAccount, destAccount.Address.ToString(), amount, 0, 10000, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Transfer %d coins from account1 to account2\n", amount)

// Get Account State
SourceaccState, err = libraClient.GetAccountState(sourceAccount.Address.ToString())
if err != nil {
log.Fatal(err)
}

// The balance should be 400000000 now
fmt.Printf("Address1 balance: %d\n", SourceaccState.Balance)

// Get Account State
destaccState, err := libraClient.GetAccountState(destAccount.Address.ToString())
if err != nil {
log.Fatal(err)
}

// The balance should be 100000000 now
fmt.Printf("Address2 balance: %d\n", destaccState.Balance)
}
6 changes: 2 additions & 4 deletions pkg/crypto/hash.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package crypto

import (
"hash"
"log"

"golang.org/x/crypto/sha3"
"hash"
)

const HASH_LENGTH = 32
Expand Down Expand Up @@ -41,7 +39,7 @@ func NewCryptoHasher(salt []byte) *CryptoHasher {
hashSuf := []byte(LIBRA_HASH_SUFFIX)
salt = append(salt, hashSuf...)
hash := from_sha3(salt).hash
log.Println(hash)
// log.Println(hash)
state.Write(hash[:])
}
return &CryptoHasher{state: state}
Expand Down
4 changes: 1 addition & 3 deletions pkg/goclient/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package goclient

import (
"encoding/hex"
"fmt"

"github.com/codemaveric/libra-go/pkg/common"
)

Expand Down Expand Up @@ -55,7 +53,7 @@ func decodeAccountStateBlob(blob []byte) map[string][]byte {
for k := 0; k < valueLen; k++ {
valueBuffer[k] = canonicalSerializer.Read8()
}
fmt.Println(hex.EncodeToString(keyBuffer))
// fmt.Println(hex.EncodeToString(keyBuffer))
state[hex.EncodeToString(keyBuffer)] = valueBuffer[:]
}
return state
Expand Down
8 changes: 5 additions & 3 deletions pkg/librawallet/keyfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ func (k *KeyFactory) GenerateKey(childNumber uint64) *ExtendedPrivKey {
r := hkdf.Expand(sha3.New256, k.Master, info)

seed := make([]byte, 32)
n, _ := io.ReadFull(r, seed)
log.Println(seed)
log.Println(n)
_, err := io.ReadFull(r, seed)
if err != nil {
log.Fatal(err)
}

privateKey := ed25519.NewKeyFromSeed(seed)
return &ExtendedPrivKey{ChildNumber: childNumber, PrivateKey: privateKey}
}
Expand Down