Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keychain and keychainstore services #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PROJECT_NAME := $(shell basename "$(PWD)")
API := api
CONSUMER := consumer
PARSER := parser
KEYCHAIN := keychain
COIN_FILE := coin/coins.yml
COIN_GO_FILE := coin/coins.go
GEN_COIN_FILE := coin/gen.go
Expand Down Expand Up @@ -43,6 +44,7 @@ STDERR := /tmp/.$(PROJECT_NAME)-stderr.txt
PID_API := /tmp/.$(PROJECT_NAME).$(API).pid
PID_CONSUMER := /tmp/.$(PROJECT_NAME).$(CONSUMER).pid
PID_PARSER := /tmp/.$(PROJECT_NAME).$(PARSER).pid
PID_KEYCHAIN := /tmp/.$(PROJECT_NAME).$(KEYCHAIN).pid
PID_MOCKSERVER := /tmp/.$(PROJECT_NAME).mockserver.pid
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent
Expand Down Expand Up @@ -79,13 +81,21 @@ start-consumer: stop
@cat $(PID_CONSUMER) | sed "/^/s/^/ \> consumer PID: /"
@echo " > Error log: $(STDERR)"

## start-keychain: Start keychain in development mode.
start-keychain: stop
@echo " > Starting $(PROJECT_NAME)"
@-$(GOBIN)/$(KEYCHAIN)/keychain -c $(CONFIG_FILE) 2>&1 & echo $$! > $(PID_CONSUMER)
@cat $(PID_CONSUMER) | sed "/^/s/^/ \> consumer PID: /"
@echo " > Error log: $(STDERR)"

## stop: Stop development mode.
stop:
@-touch $(PID_API) $(PID_CONSUMER) $(PID_PARSER)
@-kill `cat $(PID_API)` 2> /dev/null || true
@-kill `cat $(PID_CONSUMER)` 2> /dev/null || true
@-kill `cat $(PID_PARSER)` 2> /dev/null || true
@-rm $(PID_API) $(PID_CONSUMER) $(PID_PARSER)
@-kill `cat $(PID_KEYCHAIN)` 2> /dev/null || true
@-rm $(PID_API) $(PID_CONSUMER) $(PID_PARSER) $(PID_KEYCHAIN)

stop-mockserver:
@-touch $(PID_MOCKSERVER)
Expand Down Expand Up @@ -203,7 +213,7 @@ endif

go-compile: go-build

go-build: go-build-api go-build-consumer go-build-parser
go-build: go-build-api go-build-consumer go-build-parser go-build-keychain

docker-shutdown:
@echo " > Shutdown docker containers..."
Expand All @@ -227,6 +237,10 @@ go-build-parser:
@echo " > Building parser binary..."
GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(PARSER)/parser ./cmd/$(PARSER)

go-build-keychain:
@echo " > Building keychain binary..."
GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(KEYCHAIN)/keychain ./cmd/$(KEYCHAIN)

go-generate:
@echo " > Generating dependency files..."
GOBIN=$(GOBIN) go generate $(generate)
Expand Down
9 changes: 9 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/trustwallet/blockatlas/config"
_ "github.com/trustwallet/blockatlas/docs"
"github.com/trustwallet/blockatlas/platform"
"github.com/trustwallet/blockatlas/services/keychainstore"
"github.com/trustwallet/blockatlas/services/tokenindexer"
)

Expand All @@ -26,6 +27,14 @@ func SetupPlatformAPI(router gin.IRouter) {
RegisterBasicAPI(router)
}

func SetupKeychainStoreAPI(router gin.IRouter, instance keychainstore.Instance) {
RegisterKeychainStoreAPI(router, instance)
}

func SetupKeychainAPI(router gin.IRouter) {
RegisterKeychainAPI(router)
}

func SetupTokensIndexAPI(router gin.IRouter, instance tokenindexer.Instance) {
RegisterTokensIndexAPI(router, instance)
}
Expand Down
32 changes: 32 additions & 0 deletions api/endpoint/keychain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package endpoint

import (
"net/http"

"github.com/gin-gonic/gin"
)

// getEvents(customer-apikey)
// apikey -> keychainID
// customer-apikey -> wallet
func GetKeychainEvents(c *gin.Context) {
c.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"events": []string{"keychain_address"},
})
}

// getAddress(user, customer-apikey, network, asset, addressType (user, memo))
// -> { address, memo }
// apikey -> keychainID
// customer-apikey -> wallet
// network/asset -> address
// generate new memo
// call store api -> (watchAddress/userAddress, memo)
func GetKeychainAddress(c *gin.Context) {
c.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"address": "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
"memo": "UUID",
})
}
48 changes: 48 additions & 0 deletions api/endpoint/keychainstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package endpoint

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/prometheus/common/log"
"github.com/trustwallet/blockatlas/internal"
"github.com/trustwallet/blockatlas/services/keychainstore"
)

func GetKeychainStoreEvents(c *gin.Context, instance keychainstore.Instance) {
c.JSON(http.StatusOK, map[string]interface{}{
"status": true,
"build": internal.Build,
"date": internal.Date,
})
}

var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

func KeychainStoreWSHandler(c *gin.Context, instance keychainstore.Instance) {
w := c.Writer
r := c.Request

conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
log.Warn("Failed to set websocket upgrade: ", err)
return
}

for {
t, msg, err := conn.ReadMessage()
if err != nil {
log.Warn("Failed to read WS message: ", err)
break
}
err = conn.WriteMessage(t, msg)
if err != nil {
log.Warn("Failed to write WS message: ", err)
break
}
}
}
21 changes: 21 additions & 0 deletions api/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/trustwallet/blockatlas/api/endpoint"
"github.com/trustwallet/blockatlas/pkg/blockatlas"
"github.com/trustwallet/blockatlas/platform"
"github.com/trustwallet/blockatlas/services/keychainstore"
"github.com/trustwallet/blockatlas/services/tokenindexer"
"github.com/trustwallet/golibs/network/middleware"
)
Expand Down Expand Up @@ -101,6 +102,26 @@ func RegisterBasicAPI(router gin.IRouter) {
router.GET("/", endpoint.GetStatus)
}

func RegisterKeychainStoreAPI(router gin.IRouter, instance keychainstore.Instance) {
router.GET("/v1/keychain/events", func(c *gin.Context) {
endpoint.GetKeychainStoreEvents(c, instance)
})

router.GET("/v1/keychain/ws", func(c *gin.Context) {
endpoint.KeychainStoreWSHandler(c, instance)
})
}

func RegisterKeychainAPI(router gin.IRouter) {
router.GET("/v1/address", func(c *gin.Context) {
endpoint.GetKeychainAddress(c)
})

router.GET("/v1/events", func(c *gin.Context) {
endpoint.GetKeychainEvents(c)
})
}

func RegisterTokensIndexAPI(router gin.IRouter, instance tokenindexer.Instance) {
router.GET("/v3/tokens/new", func(c *gin.Context) {
endpoint.GetNewTokens(c, instance)
Expand Down
4 changes: 4 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
_ "github.com/trustwallet/blockatlas/docs"
"github.com/trustwallet/blockatlas/internal"
"github.com/trustwallet/blockatlas/platform"
"github.com/trustwallet/blockatlas/services/keychainstore"
"github.com/trustwallet/blockatlas/services/tokenindexer"
)

Expand All @@ -32,6 +33,7 @@ var (
engine *gin.Engine
database *db.Instance
tokenIndexer tokenindexer.Instance
keychainStore keychainstore.Instance
)

func init() {
Expand All @@ -56,12 +58,14 @@ func init() {
metrics.Setup(database)

tokenIndexer = tokenindexer.Init(database)
keychainStore = keychainstore.Init(database)
}

func main() {
api.SetupTokensIndexAPI(engine, tokenIndexer)
api.SetupSwaggerAPI(engine)
api.SetupPlatformAPI(engine)
api.SetupKeychainStoreAPI(engine, keychainStore)
api.SetupMetrics(engine)

golibsGin.SetupGracefulShutdown(ctx, port, engine)
Expand Down
42 changes: 42 additions & 0 deletions cmd/keychain/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"

golibsGin "github.com/trustwallet/golibs/network/gin"

"github.com/gin-gonic/gin"
"github.com/trustwallet/blockatlas/api"
"github.com/trustwallet/blockatlas/config"
_ "github.com/trustwallet/blockatlas/docs"
"github.com/trustwallet/blockatlas/internal"
)

const (
defaultPort = "8430"
defaultConfigPath = "../../config.yml"
)

var (
ctx context.Context
cancel context.CancelFunc
port, confPath string
engine *gin.Engine
)

func init() {
port, confPath = internal.ParseArgs(defaultPort, defaultConfigPath)
ctx, cancel = context.WithCancel(context.Background())

internal.InitConfig(confPath)

engine = internal.InitEngine(config.Default.Gin.Mode)
}

func main() {
api.SetupMetrics(engine)
api.SetupKeychainAPI(engine)

golibsGin.SetupGracefulShutdown(ctx, port, engine)
cancel()
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ require (
github.com/getsentry/raven-go v0.2.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.7
github.com/gorilla/websocket v1.5.0
github.com/itchyny/timefmt-go v0.1.2
github.com/mitchellh/mapstructure v1.5.0
github.com/mr-tron/base58 v1.2.0
github.com/ory/dockertest v3.3.5+incompatible
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v0.9.4
github.com/prometheus/common v0.4.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.7.1
github.com/streadway/amqp v1.0.0
Expand All @@ -39,6 +41,7 @@ require (
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/beorn7/perks v1.0.0 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 // indirect
Expand Down Expand Up @@ -83,7 +86,6 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/common v0.4.1 // indirect
github.com/prometheus/procfs v0.0.2 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
Expand All @@ -99,6 +101,7 @@ require (
golang.org/x/tools v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -179,6 +180,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQZOmDhRqsv5mZf+Jb1RnSLxcqZcI=
github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
Expand Down Expand Up @@ -658,6 +661,7 @@ google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
Loading