chain interactor init

This commit is contained in:
r8zavetr8v 2024-05-25 02:39:32 +03:00
parent 43dbe185ea
commit 8ae32f0235
6 changed files with 101 additions and 43 deletions

View File

@ -1,8 +1,10 @@
version: '3'
networks:
blockd-net:
name: blockd-net
driver: bridge
syslog:
name: syslog
driver: bridge
volumes:
blockd-data: {}
@ -11,14 +13,19 @@ volumes:
services:
blockd:
container_name: blockd
image: blockd:latest
build:
context: .
dockerfile: ./Dockerfile
ports:
- 8080:8080
networks:
- blockd-net
- syslog
depends_on:
blockd-db:
condition: service_healthy
syslog:
condition: service_started
profiles: [blockd]
blockd-db:
@ -54,32 +61,52 @@ services:
- 6379:6379
profiles: [blockd, database, noback]
prometheus:
image: prom/prometheus
container_name: prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
ports:
- 9091:9090
restart: unless-stopped
networks:
- blockd-net
volumes:
- ./prometheus:/etc/prometheus
- prometheus_data:/prometheus
profiles: [blockd, metrics, noback]
# prometheus:
# image: prom/prometheus
# container_name: prometheus
# command:
# - '--config.file=/etc/prometheus/prometheus.yml'
# ports:
# - 9091:9090
# restart: unless-stopped
# networks:
# - blockd-net
# volumes:
# - ./prometheus:/etc/prometheus
# - prometheus_data:/prometheus
# profiles: [blockd, metrics, noback]
grafana:
image: grafana/grafana
container_name: grafana
ports:
- 3112:3000
restart: unless-stopped
networks:
- blockd-net
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=grafana
volumes:
- ./grafana:/etc/grafana/provisioning/datasources
profiles: [blockd, metrics, noback]
# grafana:
# image: grafana/grafana
# container_name: grafana
# ports:
# - 3112:3000
# restart: unless-stopped
# networks:
# - blockd-net
# environment:
# - GF_SECURITY_ADMIN_USER=admin
# - GF_SECURITY_ADMIN_PASSWORD=grafana
# volumes:
# - ./grafana:/etc/grafana/provisioning/datasources
# profiles: [blockd, metrics, noback]
# syslog:
# image: linuxserver/syslog-ng:3.36.1
# container_name: syslog-ng
# environment:
# - PUID=0
# - PGID=0
# - TZ=UTC
# volumes:
# - /srv/syslog/config:/config
# - /srv/syslog/logs:/var/log
# ports:
# - 514:5514/udp
# - 601:6601/tcp
# - 6514:6514/tcp
# restart: unless-stopped
# networks:
# - syslog
# logging:
# driver: "json-file"

View File

@ -20,6 +20,7 @@ type User struct {
PK []byte
Bip39Seed []byte
Mnemonic string
Activated bool
CreatedAt time.Time
UpdatedAt time.Time

View File

@ -3,9 +3,9 @@ package chain
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
@ -104,25 +104,49 @@ func (i *chainInteractor) NewMultisig(ctx context.Context, params NewMultisigPar
}
}
func (i *chainInteractor) PubKey(ctx context.Context, user *models.User) (*ecdsa.PublicKey, error) {
hex := common.Bytes2Hex(user.Seed())
pubAddr := i.config.ChainAPI.Host + "/address/" + hex
func (i *chainInteractor) PubKey(ctx context.Context, user *models.User) ([]byte, error) {
pubAddr := i.config.ChainAPI.Host + "/address-from-seed"
doneCh := make(chan struct{})
errCh := make(chan error)
requestBody, err := json.Marshal(map[string]any{
"seedPhrase": user.Mnemonic,
})
if err != nil {
return nil, fmt.Errorf("error marshal request body. %w", err)
}
body := bytes.NewBuffer(requestBody)
var pubKeyStr string
go func() {
resp, err := http.Get(pubAddr)
resp, err := http.Post(pubAddr, "application/json", body)
if err != nil {
errCh <- fmt.Errorf("error fetch pub address. %w", err)
return
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
errCh <- fmt.Errorf("error read resp body. %w", err)
return
}
pubKeyStr = string(respBody)
doneCh <- struct{}{}
}()
select {
case err := <-errCh:
return err
return nil, err
case <-doneCh:
return nil
return common.Hex2Bytes(pubKeyStr), nil
case <-ctx.Done():
return ctx.Err()
return nil, ctx.Err()
}
}

View File

@ -80,6 +80,7 @@ func (i *usersInteractor) Create(ctx context.Context, params CreateParams) (*mod
)
user.Name = params.Name
user.Mnemonic = params.Mnemonic
user.Credentails = &models.UserCredentials{
Email: params.Email,
@ -89,7 +90,7 @@ func (i *usersInteractor) Create(ctx context.Context, params CreateParams) (*mod
// TODO fetch user PK from chain-api
user.PK = []byte{0x01}
user.PK = []byte{0x01} // todo remove
if err = i.usersRepo.Create(ctx, user); err != nil {
return nil, fmt.Errorf("error create new user. %w", err)

View File

@ -50,7 +50,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
var users []*models.User = make([]*models.User, 0, len(params.Ids))
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) (err error) {
query := sq.Select("u.id, u.name, u.email, u.phone, u.tg, u.seed, u.created_at, u.activated_at, u.public_key").
query := sq.Select("u.id, u.name, u.email, u.phone, u.tg, u.seed, u.created_at, u.activated_at, u.public_key, u.mnemonic").
From("users as u").
PlaceholderFormat(sq.Dollar)
@ -99,6 +99,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
//isAdmin bool
createdAt time.Time
activatedAt sql.NullTime
mnemonic string
)
if err = rows.Scan(
@ -111,6 +112,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
&createdAt,
&activatedAt,
&pk,
&mnemonic,
); err != nil {
return fmt.Errorf("error scan row. %w", err)
}
@ -125,6 +127,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
},
Bip39Seed: seed,
PK: pk,
Mnemonic: mnemonic,
//Admin: isAdmin,
CreatedAt: createdAt,
Activated: activatedAt.Valid,
@ -141,7 +144,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
func (r *repositorySQL) Create(ctx context.Context, user *models.User) error {
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) error {
columns := []string{"id", "name", "email", "phone", "tg", "seed", "public_key", "created_at"}
columns := []string{"id", "name", "email", "phone", "tg", "seed", "public_key", "mnemonic", "created_at"}
values := []any{
user.ID,
@ -151,6 +154,7 @@ func (r *repositorySQL) Create(ctx context.Context, user *models.User) error {
user.Credentails.Telegram,
user.Bip39Seed,
user.PK,
user.Mnemonic,
user.CreatedAt,
}

View File

@ -5,6 +5,7 @@ create table if not exists users (
phone varchar(16),
tg varchar(200),
public_key bytea not null unique,
mnemonic varchar(500) not null,
seed bytea not null unique,
created_at timestamp default current_timestamp,
activated_at timestamp default null