diff --git a/.gitignore b/.gitignore index d9bbe08..18ee06b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,4 @@ package-lock.json ipld Dockerfile demo.txt - +*.dat diff --git a/cmd/get.go b/cmd/get.go index 3fd23cd..bee4299 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -14,11 +14,10 @@ import ( // MakeGet ... func MakeGet() *cobra.Command { var command = &cobra.Command{ - Use: "get", - Short: "Get chappe artifacts", - Long: `Retrieve and optionally decrypt a specific item from the data store`, - Example: ` chappe get - chappe get --cid Qmah6HuF5kw1HF9yNfTxxoWBjCRYddestuMYn7RFF4RHnS`, + Use: "get", + Short: "Get chappe artifacts", + Long: `Retrieve and optionally decrypt a specific item from the data store`, + Example: ` chappe get --cid Qmah6HuF5kw1HF9yNfTxxoWBjCRYddestuMYn7RFF4RHnS`, SilenceUsage: false, } diff --git a/cmd/publish.go b/cmd/publish.go index 29f7a84..0006874 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -9,13 +9,12 @@ import ( "github.com/eosio-enterprise/chappe/internal/encryption" "github.com/eosio-enterprise/chappe/pkg" + "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" ) -// var c = cache.New(time.Hour) - // MakePublish ... func MakePublish() *cobra.Command { var command = &cobra.Command{ @@ -41,12 +40,13 @@ func MakePublish() *cobra.Command { } for { - privatePayload := pkg.GetFakePrivatePayload() + privatePayload := pkg.GetLedgerPayload() msg := pkg.NewMessage() msg.Payload["BlockchainMemo"] = []byte(readableMemo) payload, _ := json.MarshalIndent(privatePayload, "", " ") - log.Println("Publishing: \n", string(payload)) + color.Green("Publishing:") + color.Green(string(payload)) if encryptFlag { aesKey := encryption.NewAesEncryptionKey() diff --git a/cmd/subscribe.go b/cmd/subscribe.go index 9e1d4fe..b6d7377 100644 --- a/cmd/subscribe.go +++ b/cmd/subscribe.go @@ -6,7 +6,6 @@ import ( "github.com/eosio-enterprise/chappe/pkg" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // MakeSubscribe ... @@ -14,24 +13,20 @@ func MakeSubscribe() *cobra.Command { var command = &cobra.Command{ Use: "subscribe", Short: "Subscribe to a channel", - Example: ` chappe subscribe --channel-name MyChannel`, + Example: ` chappe subscribe --channel-name MyChannel --send-receipts --ledgerFile my_ledger_file.dat`, SilenceUsage: false, } - var channelName string + var channelName, ledgerFile string var sendReceipts bool command.Flags().StringVarP(&channelName, "channel-name", "n", "", "channel name") command.Flags().BoolVarP(&sendReceipts, "send-receipts", "r", false, "send device-specific receipts back to hub") + command.Flags().StringVarP(&ledgerFile, "ledger-file", "l", "", "record received transactions to a ledger file") command.Run = func(cmd *cobra.Command, args []string) { go func() { - - if viper.GetString("Dfuse.Protocol") == "WebSocket" { - pkg.StreamWS(channelName, sendReceipts) - } else { - pkg.StreamMessages(context.TODO(), channelName, sendReceipts) - } + pkg.StreamMessages(context.TODO(), channelName, ledgerFile, sendReceipts) }() sigs := make(chan os.Signal, 1) diff --git a/configs/example-config.yaml b/configs/example-config.yaml index a88e4a7..5908d5d 100644 --- a/configs/example-config.yaml +++ b/configs/example-config.yaml @@ -5,11 +5,12 @@ Eosio: PublishAccount: messengerbus PublishPrivateKey: 5KAP1zytghuvowgprSPLNasajibZcxf4KMgdgNbrNj98xhcGAUa Dfuse: - Protocol: GraphQL # only GraphQL supported now # WebSocket will use WSEndpoint; else it defaults to GraphQL + Protocol: GraphQL # only GraphQL supported now WSEndpoint: wss://kylin.eos.dfuse.io/v1/stream # only GraphQL supported now Origin: github.com/eosio-enterprise/chappe ApiKey: <> GraphQLEndpoint: kylin.eos.dfuse.io:443 + AuthEndpoint: https://auth.dfuse.io/v1/auth/issue KeyDirectory: channels/ PublishInterval: 5s # Go Duration object DeviceRSAPrivateKey: mykey # uniquely represents the device (used for receipts) diff --git a/go.mod b/go.mod index 645a368..aaf9f52 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,13 @@ module github.com/eosio-enterprise/chappe go 1.13 require ( - github.com/bxcodec/faker v2.0.1+incompatible + github.com/bxcodec/faker/v3 v3.3.0 github.com/dfuse-io/eosws-go v0.0.0-20191011181529-0eb3d4ce8743 github.com/eoscanada/eos-go v0.9.1-0.20200316040626-bf09fb15dea8 + github.com/fatih/color v1.3.0 github.com/golang/protobuf v1.3.3 github.com/ipfs/go-ipfs-api v0.0.3 + github.com/leekchan/accounting v0.0.0-20191218023648-17a4ce5f94d4 github.com/multiformats/go-multihash v0.0.10 // indirect github.com/spf13/cobra v0.0.6 github.com/spf13/viper v1.6.2 diff --git a/go.sum b/go.sum index d0a8b19..8c15d82 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,8 @@ github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVa github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= -github.com/bxcodec/faker v2.0.1+incompatible h1:P0KUpUw5w6WJXwrPfv35oc91i4d8nf40Nwln+M/+faA= -github.com/bxcodec/faker v2.0.1+incompatible/go.mod h1:BNzfpVdTwnFJ6GtfYTcQu6l6rHShT+veBxNCnjCx5XM= +github.com/bxcodec/faker/v3 v3.3.0 h1:w/GnJDXnn8b49hLqU948jZj1pqcb8y7sTE822KzO+xc= +github.com/bxcodec/faker/v3 v3.3.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -49,6 +49,8 @@ github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgk github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/cloudflare-go v0.10.2-0.20190916151808-a80f83b9add9/go.mod h1:1MxXX1Ux4x6mqPmjkUgTP1CdXIBXKX7T+Jk9Gxrmx+U= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= +github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -76,6 +78,7 @@ github.com/eoscanada/eos-go v0.8.5/go.mod h1:RKrm2XzZEZWxSMTRqH5QOyJ1fb/qKEjs2ix github.com/eoscanada/eos-go v0.9.1-0.20200316040626-bf09fb15dea8 h1:E79CwwnFq70bWq52l7qthwR2uY1EpVSUUojwUVbEgLs= github.com/eoscanada/eos-go v0.9.1-0.20200316040626-bf09fb15dea8/go.mod h1:6RuJFiRU1figWZ39M33o2cERU2MdL6VllElYLHTZNeo= github.com/ethereum/go-ethereum v1.9.9/go.mod h1:a9TqabFudpDu1nucId+k9S8R9whYaHnGBLKFouA5EAo= +github.com/fatih/color v1.3.0 h1:YehCCcyeQ6Km0D6+IapqPinWBK6y+0eB5umvZXK9WPs= github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= @@ -153,6 +156,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/leekchan/accounting v0.0.0-20191218023648-17a4ce5f94d4 h1:KZzDAtJ7ZLm0zSWVhN/zgyB8Ksx5H+P9irwbTcJ9FwI= +github.com/leekchan/accounting v0.0.0-20191218023648-17a4ce5f94d4/go.mod h1:3timm6YPhY3YDaGxl0q3eaflX0eoSx3FXn7ckHe4tO0= +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/libp2p/go-flow-metrics v0.0.1 h1:0gxuFd2GuK7IIP5pKljLwps6TvcuYgvG7Atqi3INF5s= github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8= github.com/libp2p/go-libp2p-core v0.0.1 h1:HSTZtFIq/W5Ue43Zw+uWZyy2Vl5WtF0zDjKN8/DT/1I= @@ -166,9 +173,11 @@ github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMg github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o= github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= +github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035 h1:USWjF42jDCSEeikX/G1g40ZWnsPXN5WkZ4jMHZWyBK4= github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= @@ -239,6 +248,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= diff --git a/pkg/dfuse-graphql.go b/pkg/dfuse-graphql.go index ac8d7d1..1dfcccd 100644 --- a/pkg/dfuse-graphql.go +++ b/pkg/dfuse-graphql.go @@ -22,7 +22,7 @@ import ( func getToken(apiKey string) (token string, expiration time.Time, err error) { reqBody := bytes.NewBuffer([]byte(fmt.Sprintf(`{"api_key":"%s"}`, apiKey))) - resp, err := http.Post("https://auth.dfuse.io/v1/auth/issue", "application/json", reqBody) + resp, err := http.Post(viper.GetString("Dfuse.AuthEndpoint"), "application/json", reqBody) if err != nil { err = fmt.Errorf("unable to obtain token: %s", err) return @@ -86,7 +86,7 @@ type eosioDocument struct { } // StreamMessages ... -func StreamMessages(ctx context.Context, channelName string, sendReceipts bool) { +func StreamMessages(ctx context.Context, channelName, ledgerFile string, sendReceipts bool) { /* The client can be re-used for all requests, cache it at the appropriate level */ client := createClient(viper.GetString("Dfuse.GraphQLEndpoint")) executor, err := client.Execute(ctx, &pb.Request{Query: operationEOS}) @@ -131,7 +131,7 @@ func StreamMessages(ctx context.Context, channelName string, sendReceipts bool) if payload["message_type"] == "receipt" { log.Println("Received receipt, ignoring.") } else { - message, err := receiveGQL(channelName, payload) + message, err := receiveGQL(channelName, ledgerFile, payload) if err == nil && sendReceipts { SendReceipt(channelName, message) } @@ -141,13 +141,12 @@ func StreamMessages(ctx context.Context, channelName string, sendReceipts bool) } } -func receiveGQL(channelName string, payload map[string]string) (Message, error) { - // log.Println(payload) +func receiveGQL(channelName, ledgerFile string, payload map[string]string) (Message, error) { var msg Message cid, cidExists := payload["cid"] if cidExists { log.Println("Received notification of new message: ", payload["cid"], "; memo: ", payload["memo"]) - msg, err := Load(channelName, cid) + msg, err := Load(channelName, ledgerFile, cid) if err != nil { log.Println("Error loading message: ", err) return msg, err diff --git a/pkg/dfuse-websocket.go b/pkg/dfuse-websocket.go deleted file mode 100644 index f3f4d37..0000000 --- a/pkg/dfuse-websocket.go +++ /dev/null @@ -1,92 +0,0 @@ -package pkg - -import ( - "fmt" - "log" - - "github.com/dfuse-io/eosws-go" - "github.com/spf13/viper" - "github.com/tidwall/gjson" -) - -// StreamWS ... -func StreamWS(channelName string, sendReceipts bool) { - client := getClient() - err := client.Send(getActionTraces()) - if err != nil { - log.Fatalf("Failed to send request to dfuse: %s", err) - } - - for { - msg, err := client.Read() - if err != nil { - log.Fatalf("Cannot read from dfuse client: %s", err) - } - - switch m := msg.(type) { - case *eosws.ActionTrace: - message, err := receiveWS(channelName, m) - if err == nil && sendReceipts { - SendReceipt(channelName, message) - } - case *eosws.Progress: - fmt.Print(".") // poor man's progress bar, using print not log - case *eosws.Listening: - log.Println("Received Listening Message ...") - default: - log.Println("Received Unsupported Message", m) - } - } -} - -func receiveWS(channelName string, dfuseMessage *eosws.ActionTrace) (Message, error) { - ipfsHash := gjson.Get(string(dfuseMessage.Data.Trace), "act.data.ipfs_hash") - memo := gjson.Get(string(dfuseMessage.Data.Trace), "act.data.memo") - fmt.Println() - log.Println("Received notification of new message: ", ipfsHash, "; memo: ", memo) - - msg, err := Load(channelName, ipfsHash.String()) - if err != nil { - log.Println("Error loading message: ", err) - return msg, err - } - - return msg, nil -} - -// GetClient ... -func getClient() *eosws.Client { - apiKey := viper.GetString("Dfuse.ApiKey") - if apiKey == "" { - log.Fatalf("Missing Dfuse.ApiKey in config") - } - - jwt, _, err := eosws.Auth(apiKey) - if err != nil { - log.Fatalf("cannot get auth token: %s", err.Error()) - } - - // var dfuseEndpoint = viper.GetString("Dfuse.WSEndpoint") - var origin = viper.GetString("Dfuse.Origin") - - client, err := eosws.New(viper.GetString("Dfuse.WSEndpoint"), jwt, origin) - if err != nil { - log.Fatalf("cannot connect to dfuse endpoint: %s", err.Error()) - } - return client -} - -// GetActionTraces ... -func getActionTraces() *eosws.GetActionTraces { - ga := &eosws.GetActionTraces{} - ga.ReqID = "chappe" - ga.StartBlock = -300 - ga.Listen = true - ga.WithProgress = 3 - ga.IrreversibleOnly = false - ga.Data.Accounts = viper.GetString("Eosio.PublishAccount") - ga.Data.ActionNames = "pub" - fmt.Printf("Connecting... %s::%s\n", ga.Data.Accounts, ga.Data.ActionNames) - ga.Data.WithInlineTraces = true - return ga -} diff --git a/pkg/message.go b/pkg/message.go index 7608cf0..ee85bb4 100644 --- a/pkg/message.go +++ b/pkg/message.go @@ -4,13 +4,25 @@ import ( "bytes" "encoding/json" "log" + "os" - "github.com/bxcodec/faker" + "github.com/bxcodec/faker/v3" "github.com/eosio-enterprise/chappe/internal/encryption" + "github.com/fatih/color" shell "github.com/ipfs/go-ipfs-api" + "github.com/leekchan/accounting" "github.com/spf13/viper" ) +// LedgerTrx ... +type LedgerTrx struct { + TrxDate string `faker:"date"` + Memo string `faker:"sentence"` + Account1 string `faker:"-"` + Account2 string `faker:"-"` + Amount string `faker:"-"` +} + // Message ... type Message struct { Payload map[string][]byte @@ -34,7 +46,7 @@ func NewMessage() Message { } // Load ... -func Load(channelName, ipfsHash string) (Message, error) { +func Load(channelName, ledgerFile, ipfsHash string) (Message, error) { var msg Message sh := shell.NewShell(viper.GetString("IPFS.Endpoint")) reader, err := sh.Cat(ipfsHash) @@ -66,14 +78,26 @@ func Load(channelName, ipfsHash string) (Message, error) { log.Println("Decrypted message from Channel: ", channelName, "\n", string(plaintext)) - // We could also unmarshal the text (which is JSON) into an object - // var receivedPrivatePayload FakePrivatePayload - // err = json.Unmarshal(plaintext, &receivedPrivatePayload) + var receivedLedgerTrx LedgerTrx + err = json.Unmarshal(plaintext, &receivedLedgerTrx) + + textTrx := receivedLedgerTrx.TrxDate + "\t" + receivedLedgerTrx.Memo + "\n" + textTrx = textTrx + "\t\t" + receivedLedgerTrx.Account1 + "\t\t" + receivedLedgerTrx.Amount + "\n" + textTrx = textTrx + "\t\t" + receivedLedgerTrx.Account2 + "\n\n" - // indentedPayload, err := json.MarshalIndent(receivedPrivatePayload, "", " ") - // if err != nil { - // log.Printf("Error from marshall indent: %s\n", err) - // } + log.Println("Adding new transaction to ledger: ") + color.Cyan(textTrx) + + f, err := os.OpenFile(ledgerFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Fatal(err) + } + if _, err := f.Write([]byte(textTrx)); err != nil { + log.Fatal(err) + } + if err := f.Close(); err != nil { + log.Fatal(err) + } return msg, nil } @@ -94,10 +118,74 @@ type FakePrivatePayload struct { LastScan string `faker:"timestamp"` } +// Components ... +type Components struct { + AccountIndex1 int `faker:"boundary_start=0, boundary_end=29"` + AccountIndex2 int `faker:"boundary_start=0, boundary_end=29"` + Dollars int `faker:"boundary_start=10000, boundary_end=10000000"` + Cents int `faker:"boundary_start=0, boundary_end=100"` +} + +// GetLedgerPayload ... +func GetLedgerPayload() LedgerTrx { + var ledgerPayload LedgerTrx + err := faker.FakeData(&ledgerPayload) + if err != nil { + log.Println("Cannot generate fake data: ", err) + } + + var components Components + err = faker.FakeData(&components) + if err != nil { + log.Println("Cannot generate fake data: ", err) + } + + var accounts [30]string + accounts[0] = "Assets:Checking Account" + accounts[1] = "Assets:Reserve Account" + accounts[2] = "Assets:Land" + accounts[3] = "Equity:Builder" + accounts[4] = "Equity:Fund Limited Partners" + accounts[5] = "Equity:Private Equity Partners" + accounts[6] = "Liabilities:Bank One Loan" + accounts[7] = "Liabilities:PNC Line of Credit" + accounts[8] = "Expenses:Financing:Appraisal Fee" + accounts[9] = "Expenses:Financing:Mortgage Reg Tax" + accounts[10] = "Expenses:Construction Contract" + accounts[11] = "Expenses:Financing:Financing Fee" + accounts[12] = "Expenses:Development:Development Fee" + accounts[13] = "Expenses:Diligence:Title Insurance" + accounts[14] = "Expenses:Builders Insurance" + accounts[15] = "Expenses:Diligence:Traffic Study" + accounts[16] = "Expenses:Diligence:Survey" + accounts[17] = "Expenses:Diligence:Environmental" + accounts[18] = "Expenses:Diligence:GeoTech" + accounts[19] = "Expenses:Design Costs:Architectural & Engineering" + accounts[20] = "Expenses:Finishing:Furniture" + accounts[21] = "Expenses:Finishing:Flooring" + accounts[22] = "Expenses:Finishing:Walls" + accounts[23] = "Expenses:Office:Document Processing" + accounts[24] = "Expenses:Office:Legal Fees" + accounts[25] = "Expenses:Bank One Loan:Interest" + accounts[26] = "Expenses:PNC Line of Credit:Interest" + accounts[27] = "Income:Condo Sales" + accounts[28] = "Income:Single Family Sales" + accounts[29] = "Income:Rent" + + ledgerPayload.Account1 = accounts[components.AccountIndex1] + ledgerPayload.Account2 = accounts[components.AccountIndex2] + + ac := accounting.Accounting{Symbol: "$", Precision: 2} + ledgerPayload.Amount = ac.FormatMoney(float64(components.Dollars) + (float64(components.Cents) / float64(100))) + + return ledgerPayload +} + // GetFakePrivatePayload ... func GetFakePrivatePayload() FakePrivatePayload { var privatePayload FakePrivatePayload err := faker.FakeData(&privatePayload) + if err != nil { log.Println("Cannot generate fake data: ", err) } diff --git a/pkg/publish.go b/pkg/publish.go index 15fca4f..c713242 100644 --- a/pkg/publish.go +++ b/pkg/publish.go @@ -13,20 +13,6 @@ import ( "github.com/spf13/viper" ) -// func newPub(ipfsHash, memo string) *eos.Action { -// return &eos.Action{ -// Account: eos.AN(viper.GetString("Eosio.PublishAccount")), -// Name: eos.ActN("pub"), -// Authorization: []eos.PermissionLevel{ -// {Actor: eos.AN(viper.GetString("Eosio.PublishAccount")), Permission: eos.PN("active")}, -// }, -// ActionData: eos.NewActionData(PubActionPayload{ -// IpfsHash: ipfsHash, -// Memo: memo, -// }), -// } -// } - // PubMapActionPayload ... type PubMapActionPayload struct { PayloadMap map[string]string `json:"payload"` @@ -45,12 +31,6 @@ func newPubMap(payload map[string]string) *eos.Action { } } -// // PubActionPayload ... -// type PubActionPayload struct { -// IpfsHash string `json:"ipfs_hash"` -// Memo string `json:"memo"` -// } - // PublishMapToBlockchain ... func PublishMapToBlockchain(payload map[string]string) (string, error) { ctx := context.Background() @@ -84,39 +64,6 @@ func PublishMapToBlockchain(payload map[string]string) (string, error) { return hex.EncodeToString(response.Processed.ID), nil } -// // AddToEosio ... -// func AddToEosio(cid string, readableMemo string) (string, error) { -// ctx := context.Background() -// api := eos.New(viper.GetString("Eosio.Endpoint")) - -// keyBag := &eos.KeyBag{} -// err := keyBag.ImportPrivateKey(ctx, viper.GetString("Eosio.PublishPrivateKey")) -// if err != nil { -// log.Panicf("import private key: %s", err) -// } -// api.SetSigner(keyBag) - -// txOpts := &eos.TxOptions{} -// if err := txOpts.FillFromChain(ctx, api); err != nil { -// log.Printf("Error filling tx opts: %s", err) -// return "error", err -// } - -// tx := eos.NewTransaction([]*eos.Action{newPub(cid, readableMemo)}, txOpts) -// _, packedTx, err := api.SignTransaction(ctx, tx, txOpts.ChainID, eos.CompressionNone) -// if err != nil { -// log.Printf("Error signing transaction: %s", err) -// return "error", err -// } - -// response, err := api.PushTransaction(ctx, packedTx) -// if err != nil { -// log.Printf("Error pushing transaction: %s", err) -// return "error", err -// } -// return hex.EncodeToString(response.Processed.ID), nil -// } - func addToIpfs(payload Message) string { sh := shell.NewShell(viper.GetString("IPFS.Endpoint")) jsonPayloadNode, err := json.Marshal(payload) diff --git a/pkg/receipt.go b/pkg/receipt.go index 9efdf29..ced7558 100644 --- a/pkg/receipt.go +++ b/pkg/receipt.go @@ -16,7 +16,7 @@ func SendReceipt(channelName string, msg Message) error { log.Println("Cannot send receipt: ", err) } receiptStr := hex.EncodeToString(receiptSignature) - log.Println("Sending receipt: ", receiptStr) + //log.Println("Sending receipt: ", receiptStr) receiptMap := make(map[string]string) receiptMap["receipt"] = receiptStr