Skip to content

Commit

Permalink
Transfer Coins
Browse files Browse the repository at this point in the history
  • Loading branch information
codemaveric committed Jul 7, 2019
1 parent 4776d0c commit 8ce021b
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -3,6 +3,7 @@ module github.com/codemaveric/libra-go
go 1.12

require (
github.com/gogo/protobuf v1.2.1
github.com/golang/protobuf v1.3.1
github.com/tyler-smith/go-bip39 v1.0.0
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
Expand Down
5 changes: 5 additions & 0 deletions go.sum
@@ -1,13 +1,17 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/tyler-smith/go-bip39 v1.0.0 h1:FOHg9gaQLeBBRbHE/QrTLfEiBHy5pQ/yXzf9JG5pYFM=
github.com/tyler-smith/go-bip39 v1.0.0/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
Expand All @@ -20,6 +24,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
Expand Down
2 changes: 2 additions & 0 deletions pkg/crypto/hash.go
Expand Up @@ -2,6 +2,7 @@ package crypto

import (
"hash"
"log"

"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -36,6 +37,7 @@ func NewCryptoHasher(salt []byte) *CryptoHasher {
hashSuf := []byte(LIBRA_HASH_SUFFIX)
salt = append(salt, hashSuf...)
hash := from_sha3(salt).hash
log.Println(hash)
state.Write(hash[:])
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/goclient/libraclient.go
Expand Up @@ -56,8 +56,10 @@ func (l *LibraClient) TransferCoins(sender *librawallet.Account, receipientAddre
if err != nil {
return err
}
acStatus := res.GetAcStatus()

// If status is not Accepted return error else increase the sequence number of the sender
if acStatus := res.GetAcStatus(); acStatus != gowrapper.AdmissionControlStatus_Accepted {
if acStatus != gowrapper.AdmissionControlStatus_Accepted {
return errors.New(fmt.Sprintf("Transaction failed with status: %s", gowrapper.AdmissionControlStatus_name[int32(acStatus)]))
}
sender.Sequence += 1
Expand Down
9 changes: 5 additions & 4 deletions pkg/goclient/transaction.go
Expand Up @@ -3,16 +3,16 @@ package goclient
import (
"encoding/binary"
"encoding/hex"
"encoding/json"

"github.com/codemaveric/libra-go/gowrapper"
"github.com/codemaveric/libra-go/pkg/crypto"
"github.com/codemaveric/libra-go/pkg/librawallet"
"github.com/golang/protobuf/proto"
)

const GAS_UNIT_PRICE uint64 = 0
const MAX_GAS_AMOUNT uint64 = 10000
const TX_EXPIRATION uint64 = 100
const TX_EXPIRATION int64 = 100

// Const of transaction program
const HEX_PEER_TO_PEER_TRANSFER_CODE = "4c49425241564d0a010007014a00000004000000034e000000060000000c54000000050000000d5900000004000000055d0000002900000004860000002000000007a60000000d00000000000001000200010300020002040203020402063c53454c463e0c4c696272614163636f756e74046d61696e0f7061795f66726f6d5f73656e64657200000000000000000000000000000000000000000000000000000000000000000001020004000c000c01110102"
Expand All @@ -37,14 +37,15 @@ func encodeTransferProgram(receiverAddress string, numCoins uint64) (*gowrapper.

func createSubmitTransactionReq(program *gowrapper.Program, sender *librawallet.Account, gasUnitPrice, maxGasAmount uint64) (*gowrapper.SubmitTransactionRequest, error) {
raw_txn := &gowrapper.RawTransaction{
SequenceNumber: sender.Sequence,
SequenceNumber: 5,
SenderAccount: sender.Address,
MaxGasAmount: maxGasAmount,
GasUnitPrice: gasUnitPrice,
Payload: &gowrapper.RawTransaction_Program{program},
ExpirationTime: 0,
}
txn_bytes, err := json.Marshal(&raw_txn)
proto.Marshal(raw_txn)
txn_bytes, err := proto.Marshal(raw_txn)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/librawallet/account.go
Expand Up @@ -12,14 +12,14 @@ type Account struct {
Sequence uint64
}

func GenerateKeyPair(mnemonic Mnemonic) *crypto.KeyPair {
func GenerateKeyPair(mnemonic Mnemonic, childNum uint64) *crypto.KeyPair {
seed := NewSeed(mnemonic, "")
keyfactory := NewKeyFactory(seed)
priveKey := keyfactory.GenerateKey(0)
priveKey := keyfactory.GenerateKey(childNum)
return crypto.NewKeyPair(priveKey.PrivateKey)
}

func AccountFromSecret(keyPair *crypto.KeyPair) *Account {
func NewAccountFromKeyPair(keyPair *crypto.KeyPair) *Account {
digest := sha3.Sum256(keyPair.PublicKey.Value)
address := digest[:]
return &Account{Address: address, KeyPair: keyPair}
Expand Down
2 changes: 1 addition & 1 deletion pkg/librawallet/mnemonic.go
Expand Up @@ -8,7 +8,7 @@ import (

type Mnemonic []string

func generateMnemonic() Mnemonic {
func GenerateMnemonic() Mnemonic {
entropy, _ := bip39.NewEntropy(256)
mnemonic, _ := bip39.NewMnemonic(entropy)
return strings.Split(mnemonic, " ")
Expand Down
2 changes: 1 addition & 1 deletion pkg/librawallet/walletlibrary.go
Expand Up @@ -17,7 +17,7 @@ type WalletLibrary struct {
func NewWalletLibrary(mnemonicStr string) *WalletLibrary {
var mnemonic Mnemonic
if mnemonicStr == "" {
mnemonic = generateMnemonic()
mnemonic = GenerateMnemonic()
} else {
mnemonic = strings.Split(mnemonicStr, " ")
}
Expand Down

0 comments on commit 8ce021b

Please sign in to comment.