Skip to content

Commit

Permalink
Merge pull request #47 from TRON-US/BTFS-1137
Browse files Browse the repository at this point in the history
BTFS-1001: refactor ledger grpc client
  • Loading branch information
taiyangc authored Dec 10, 2019
2 parents 47ad93b + a68d816 commit 45a00f1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 86 deletions.
62 changes: 45 additions & 17 deletions ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import (
"context"
"time"

ic "github.com/libp2p/go-libp2p-core/crypto"
ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger"
"github.com/tron-us/go-btfs-common/utils/grpc"
"github.com/tron-us/protobuf/proto"
"google.golang.org/grpc"

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

func NewClient(conn *grpc.ClientConn) ledgerpb.ChannelsClient {
return ledgerpb.NewChannelsClient(conn)
type Client struct {
addr string
}

func NewClient(addr string) *Client {
return &Client{addr: addr}
}

func NewAccount(pubKey ic.PubKey, amount int64) (*ledgerpb.Account, error) {
Expand Down Expand Up @@ -59,44 +64,67 @@ func NewSignedChannelState(channelState *ledgerpb.ChannelState, fromSig []byte,
}
}

func ImportAccount(ctx context.Context, pubKey ic.PubKey, ledgerClient ledgerpb.ChannelsClient) (*ledgerpb.Account, error) {
func (c *Client) ImportAccount(ctx context.Context, pubKey ic.PubKey) (*ledgerpb.Account, error) {
keyBytes, err := pubKey.Raw()
if err != nil {
return nil, err
}
res, err := ledgerClient.CreateAccount(ctx, &ledgerpb.PublicKey{Key: keyBytes})
var res *ledgerpb.CreateAccountResult
err = grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error {
res, err = client.CreateAccount(ctx, &ledgerpb.PublicKey{Key: keyBytes})
return err
})
if err != nil {
return nil, err
}
return res.GetAccount(), nil
}

func ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubKey, ledgerClient ledgerpb.ChannelsClient) (*ledgerpb.SignedCreateAccountResult, error) {
func (c *Client) ImportSignedAccount(ctx context.Context, privKey ic.PrivKey, pubKey ic.PubKey) (*ledgerpb.SignedCreateAccountResult, error) {
pubKeyBytes, err := pubKey.Raw()
if err != nil {
return nil, err
}
singedPubKey := &ledgerpb.PublicKey{Key: pubKeyBytes}
sigBytes, err := proto.Marshal(singedPubKey)
if err != nil {
return nil, err
}
signature, err := privKey.Sign(sigBytes)
if err != nil {
return nil, err
}
signedPubkey := &ledgerpb.SignedPublicKey{Key: singedPubKey, Signature: signature}
return ledgerClient.SignedCreateAccount(ctx, signedPubkey)

var result *ledgerpb.SignedCreateAccountResult
err = grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error {
result, err = client.SignedCreateAccount(ctx, signedPubkey)
return err
})
if err != nil {
return nil, err
}
return result, nil
}

func CreateChannel(ctx context.Context, ledgerClient ledgerpb.ChannelsClient, channelCommit *ledgerpb.ChannelCommit, sig []byte) (*ledgerpb.ChannelID, error) {
return ledgerClient.CreateChannel(ctx, &ledgerpb.SignedChannelCommit{
Channel: channelCommit,
Signature: sig,
func (c *Client) CreateChannel(ctx context.Context, channelCommit *ledgerpb.ChannelCommit, sig []byte) (*ledgerpb.ChannelID, error) {
var (
channelId *ledgerpb.ChannelID
err error
)
err = grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error {
channelId, err = client.CreateChannel(ctx, &ledgerpb.SignedChannelCommit{
Channel: channelCommit,
Signature: sig,
})
return err
})
return channelId, err
}

func CloseChannel(ctx context.Context, ledgerClient ledgerpb.ChannelsClient, signedChannelState *ledgerpb.SignedChannelState) error {
_, err := ledgerClient.CloseChannel(ctx, signedChannelState)
if err != nil {
func (c *Client) CloseChannel(ctx context.Context, signedChannelState *ledgerpb.SignedChannelState) error {
return grpc.LedgerClient(c.addr).WithContext(ctx, func(ctx context.Context, client ledgerpb.ChannelsClient) error {
_, err := client.CloseChannel(ctx, signedChannelState)
return err
}
return nil
})
}
131 changes: 62 additions & 69 deletions ledger/sample/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package main

import (
"context"

"github.com/tron-us/go-btfs-common/crypto"
"github.com/tron-us/go-btfs-common/ledger"
ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger"
"github.com/tron-us/go-btfs-common/utils/grpc"
"github.com/tron-us/go-common/v2/log"

"go.uber.org/zap"
Expand All @@ -18,72 +15,68 @@ const (
)

func main() {
err := grpc.LedgerClient("https://ledger-dev.bt.co:443").WithContext(context.Background(),
func(ctx context.Context, ledgerClient ledgerpb.ChannelsClient) error {
// create payer Account
payerPrivKey, err := crypto.ToPrivKey(PayerPrivKeyString)
if err != nil {
log.Panic("can not convert to private key", zap.Error(err))
}
payerPubKey := payerPrivKey.GetPublic()
_, err = ledger.ImportSignedAccount(ctx, payerPrivKey, payerPubKey, ledgerClient)
if err != nil {
log.Panic("can not create account on ledger", zap.Error(err))
}
// create receiver account
recvPrivKey, err := crypto.ToPrivKey(ReceiverPrivKeyString)
if err != nil {
log.Panic("can not convert to private key", zap.Error(err))
}
recvPubKey := recvPrivKey.GetPublic()
_, err = ledger.ImportSignedAccount(ctx, recvPrivKey, recvPubKey, ledgerClient)
if err != nil {
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("can not create channel commit", zap.Error(err))
}
// sign for the channel commit
fromSig, err := crypto.Sign(payerPrivKey, channelCommit)
if err != nil {
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", zap.Error(err))
}
// channel state: transfer money from -> to
fromAcc, err := ledger.NewAccount(payerPubKey, 0)
if err != nil {
log.Panic("wrong account on channel", zap.Error(err))
}
toAcc, err := ledger.NewAccount(recvPubKey, amount)
if err != nil {
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 := crypto.Sign(payerPrivKey, channelState)
if err != nil {
log.Panic("error when signing the channel state", zap.Error(err))
}
toSigState, err := crypto.Sign(recvPrivKey, channelState)
if err != nil {
log.Panic("error when signing the channel state", zap.Error(err))
}
signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState)
// close channel
err = ledger.CloseChannel(ctx, ledgerClient, signedChannelState)
if err != nil {
log.Panic("fail to close channel", zap.Error(err))
}
return nil
})
ctx := context.Background()
c := ledger.NewClient("https://ledger-dev.bt.co:443")

// create payer Account
payerPrivKey, err := crypto.ToPrivKey(PayerPrivKeyString)
if err != nil {
log.Panic("can not convert to private key", zap.Error(err))
}
payerPubKey := payerPrivKey.GetPublic()
_, err = c.ImportSignedAccount(ctx, payerPrivKey, payerPubKey)
if err != nil {
log.Panic("can not create account on ledger", zap.Error(err))
}
// create receiver account
recvPrivKey, err := crypto.ToPrivKey(ReceiverPrivKeyString)
if err != nil {
log.Panic("can not convert to private key", zap.Error(err))
}
recvPubKey := recvPrivKey.GetPublic()
_, err = c.ImportSignedAccount(ctx, recvPrivKey, recvPubKey)
if err != nil {
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("can not create channel commit", zap.Error(err))
}
// sign for the channel commit
fromSig, err := crypto.Sign(payerPrivKey, channelCommit)
if err != nil {
log.Panic("fail to sign channel commit", zap.Error(err))
}
// create channel: payer start the channel
channelID, err := c.CreateChannel(ctx, channelCommit, fromSig)
if err != nil {
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("wrong account on channel", zap.Error(err))
}
toAcc, err := ledger.NewAccount(recvPubKey, amount)
if err != nil {
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 := crypto.Sign(payerPrivKey, channelState)
if err != nil {
log.Panic("error when signing the channel state", zap.Error(err))
}
toSigState, err := crypto.Sign(recvPrivKey, channelState)
if err != nil {
log.Panic("error when signing the channel state", zap.Error(err))
}
signedChannelState := ledger.NewSignedChannelState(channelState, fromSigState, toSigState)
// close channel
err = c.CloseChannel(ctx, signedChannelState)
if err != nil {
log.Panic(err.Error())
log.Panic("fail to close channel", zap.Error(err))
}
}

0 comments on commit 45a00f1

Please sign in to comment.