From 2aa00e47d09d6b3aa29550754d95e37d13402a4e Mon Sep 17 00:00:00 2001 From: Shelly Wei Date: Wed, 27 Nov 2019 13:28:01 -0800 Subject: [PATCH] move sample/demo from go-btfs to go-btfs-common --- ledger/sample/demo.go | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 ledger/sample/demo.go diff --git a/ledger/sample/demo.go b/ledger/sample/demo.go new file mode 100644 index 0000000..a6e24de --- /dev/null +++ b/ledger/sample/demo.go @@ -0,0 +1,98 @@ +package main + +import ( + "context" + "encoding/base64" + "github.com/tron-us/go-btfs-common/ledger" + "log" + + ic "github.com/libp2p/go-libp2p-core/crypto" +) + +const ( + PayerPrivKeyString = "CAISIJFNZZd5ZSvi9OlJP/mz/vvUobvlrr2//QN4DzX/EShP" + ReceiverPrivKeyString = "CAISIDm/qF5f98Jh8FGBUcFUhQvJPU8uEah1SZrR1BrGekC0" +) + +func main() { + ctx := context.Background() + // build connection with ledger + clientConn, err := ledger.LedgerConnection("ledger-dev.bt.co:443", "") + if err != nil { + log.Panic("fail to connect", err) + } + defer ledger.CloseConnection(clientConn) + // new ledger client + ledgerClient := ledger.NewClient(clientConn) + // create payer Account + payerPrivKey, err := convertToPrivKey(PayerPrivKeyString) + if err != nil { + log.Panic(err) + } + payerPubKey := payerPrivKey.GetPublic() + //_, err = ledger.ImportAccount(ctx, payerPubKey, ledgerClient) + _, err = ledger.ImportSignedAccount(ctx, payerPrivKey, payerPubKey, ledgerClient) + if err != nil { + log.Panic(err) + } + // create receiver account + recvPrivKey, err := convertToPrivKey(ReceiverPrivKeyString) + if err != nil { + log.Panic(err) + } + recvPubKey := recvPrivKey.GetPublic() + //_, err = ledger.ImportAccount(ctx, recvPubKey, ledgerClient) + _, err = ledger.ImportSignedAccount(ctx, recvPrivKey, recvPubKey, ledgerClient) + if err != nil { + log.Panic(err) + } + // prepare channel commit + amount := int64(1) + channelCommit, err := ledger.NewChannelCommit(payerPubKey, recvPubKey, amount) + if err != nil { + log.Panic(err) + } + // sign for the channel commit + fromSig, err := ledger.Sign(payerPrivKey, channelCommit) + if err != nil { + log.Panic("fail to sign channel commit", 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) + } + // channel state: transfer money from -> to + fromAcc, err := ledger.NewAccount(payerPubKey, 0) + if err != nil { + log.Panic(err) + } + toAcc, err := ledger.NewAccount(recvPubKey, amount) + if err != nil { + log.Panic(err) + } + channelState := ledger.NewChannelState(channelID, 1, fromAcc, toAcc) + // need permission from both account, get signature from both + fromSigState, err := ledger.Sign(payerPrivKey, channelState) + if err != nil { + log.Panic("error when signing the channel state", err) + } + toSigState, err := ledger.Sign(recvPrivKey, channelState) + if err != nil { + log.Panic("error when signing the channel state", 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) + if err != nil { + return nil, err + } + return ic.UnmarshalPrivateKey(raw) +}