Skip to content

Commit

Permalink
change due to review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
ShellyWEI committed Nov 28, 2019
1 parent 2aa00e4 commit f3f1bd7
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions ledger/sample/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"context"
"encoding/base64"

"github.com/tron-us/go-btfs-common/crypto"
"github.com/tron-us/go-btfs-common/ledger"
"log"
"github.com/tron-us/go-common/v2/log"

ic "github.com/libp2p/go-libp2p-core/crypto"
"go.uber.org/zap"
)

const (
Expand All @@ -19,80 +20,70 @@ func main() {
// build connection with ledger
clientConn, err := ledger.LedgerConnection("ledger-dev.bt.co:443", "")
if err != nil {
log.Panic("fail to connect", err)
log.Panic("fail to connect", zap.Error(err))
}
defer ledger.CloseConnection(clientConn)
// new ledger client
ledgerClient := ledger.NewClient(clientConn)
// create payer Account
payerPrivKey, err := convertToPrivKey(PayerPrivKeyString)
payerPrivKey, err := crypto.ToPrivKey(PayerPrivKeyString)
if err != nil {
log.Panic(err)
log.Panic("can not convert to private key", zap.Error(err))
}
payerPubKey := payerPrivKey.GetPublic()
//_, err = ledger.ImportAccount(ctx, payerPubKey, ledgerClient)
_, err = ledger.ImportSignedAccount(ctx, payerPrivKey, payerPubKey, ledgerClient)
if err != nil {
log.Panic(err)
log.Panic("can not create account on ledger", zap.Error(err))
}
// create receiver account
recvPrivKey, err := convertToPrivKey(ReceiverPrivKeyString)
recvPrivKey, err := crypto.ToPrivKey(ReceiverPrivKeyString)
if err != nil {
log.Panic(err)
log.Panic("can not convert to private key", zap.Error(err))
}
recvPubKey := recvPrivKey.GetPublic()
//_, err = ledger.ImportAccount(ctx, recvPubKey, ledgerClient)
_, err = ledger.ImportSignedAccount(ctx, recvPrivKey, recvPubKey, ledgerClient)
if err != nil {
log.Panic(err)
log.Panic("can not create account on ledger", zap.Error(err))
}
// prepare channel commit
amount := int64(1)
channelCommit, err := ledger.NewChannelCommit(payerPubKey, recvPubKey, amount)
if err != nil {
log.Panic(err)
log.Panic("can not create channel commit", zap.Error(err))
}
// sign for the channel commit
fromSig, err := ledger.Sign(payerPrivKey, channelCommit)
fromSig, err := crypto.Sign(payerPrivKey, channelCommit)
if err != nil {
log.Panic("fail to sign channel commit", err)
log.Panic("fail to sign channel commit", zap.Error(err))
}
// create channel: payer start the channel
channelID, err := ledger.CreateChannel(ctx, ledgerClient, channelCommit, fromSig)
if err != nil {
log.Panic("fail to create channel", err)
log.Panic("fail to create channel", zap.Error(err))
}
// channel state: transfer money from -> to
fromAcc, err := ledger.NewAccount(payerPubKey, 0)
if err != nil {
log.Panic(err)
log.Panic("wrong account on channel", zap.Error(err))
}
toAcc, err := ledger.NewAccount(recvPubKey, amount)
if err != nil {
log.Panic(err)
log.Panic("wrong account on channel", zap.Error(err))
}
channelState := ledger.NewChannelState(channelID, 1, fromAcc, toAcc)
// need permission from both account, get signature from both
fromSigState, err := ledger.Sign(payerPrivKey, channelState)
fromSigState, err := crypto.Sign(payerPrivKey, channelState)
if err != nil {
log.Panic("error when signing the channel state", err)
log.Panic("error when signing the channel state", zap.Error(err))
}
toSigState, err := ledger.Sign(recvPrivKey, channelState)
toSigState, err := crypto.Sign(recvPrivKey, channelState)
if err != nil {
log.Panic("error when signing the channel state", err)
log.Panic("error when signing the channel state", zap.Error(err))
}
signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState)
// close channel
err = ledger.CloseChannel(ctx, signedChannelState)
if err != nil {
log.Panic("fail to close channel", err)
}
}

func convertToPrivKey(key string) (ic.PrivKey, error) {
raw, err := base64.StdEncoding.DecodeString(key)
err = ledger.CloseChannel(ctx, ledgerClient, signedChannelState)
if err != nil {
return nil, err
log.Panic("fail to close channel", zap.Error(err))
}
return ic.UnmarshalPrivateKey(raw)
}

0 comments on commit f3f1bd7

Please sign in to comment.