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: networks:
blockd-net: blockd-net:
name: blockd-net name: blockd-net
driver: bridge driver: bridge
syslog:
name: syslog
driver: bridge
volumes: volumes:
blockd-data: {} blockd-data: {}
@ -11,14 +13,19 @@ volumes:
services: services:
blockd: blockd:
container_name: blockd container_name: blockd
image: blockd:latest build:
context: .
dockerfile: ./Dockerfile
ports: ports:
- 8080:8080 - 8080:8080
networks: networks:
- blockd-net - blockd-net
- syslog
depends_on: depends_on:
blockd-db: blockd-db:
condition: service_healthy condition: service_healthy
syslog:
condition: service_started
profiles: [blockd] profiles: [blockd]
blockd-db: blockd-db:
@ -54,32 +61,52 @@ services:
- 6379:6379 - 6379:6379
profiles: [blockd, database, noback] profiles: [blockd, database, noback]
prometheus: # prometheus:
image: prom/prometheus # image: prom/prometheus
container_name: prometheus # container_name: prometheus
command: # command:
- '--config.file=/etc/prometheus/prometheus.yml' # - '--config.file=/etc/prometheus/prometheus.yml'
ports: # ports:
- 9091:9090 # - 9091:9090
restart: unless-stopped # restart: unless-stopped
networks: # networks:
- blockd-net # - blockd-net
volumes: # volumes:
- ./prometheus:/etc/prometheus # - ./prometheus:/etc/prometheus
- prometheus_data:/prometheus # - prometheus_data:/prometheus
profiles: [blockd, metrics, noback] # profiles: [blockd, metrics, noback]
grafana: # grafana:
image: grafana/grafana # image: grafana/grafana
container_name: grafana # container_name: grafana
ports: # ports:
- 3112:3000 # - 3112:3000
restart: unless-stopped # restart: unless-stopped
networks: # networks:
- blockd-net # - blockd-net
environment: # environment:
- GF_SECURITY_ADMIN_USER=admin # - GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=grafana # - GF_SECURITY_ADMIN_PASSWORD=grafana
volumes: # volumes:
- ./grafana:/etc/grafana/provisioning/datasources # - ./grafana:/etc/grafana/provisioning/datasources
profiles: [blockd, metrics, noback] # 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 PK []byte
Bip39Seed []byte Bip39Seed []byte
Mnemonic string
Activated bool Activated bool
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time

View File

@ -3,9 +3,9 @@ package chain
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/ecdsa"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log/slog" "log/slog"
"net/http" "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) { func (i *chainInteractor) PubKey(ctx context.Context, user *models.User) ([]byte, error) {
hex := common.Bytes2Hex(user.Seed()) pubAddr := i.config.ChainAPI.Host + "/address-from-seed"
pubAddr := i.config.ChainAPI.Host + "/address/" + hex
doneCh := make(chan struct{}) doneCh := make(chan struct{})
errCh := make(chan error) 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() { 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 { select {
case err := <-errCh: case err := <-errCh:
return err return nil, err
case <-doneCh: case <-doneCh:
return nil return common.Hex2Bytes(pubKeyStr), nil
case <-ctx.Done(): 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.Name = params.Name
user.Mnemonic = params.Mnemonic
user.Credentails = &models.UserCredentials{ user.Credentails = &models.UserCredentials{
Email: params.Email, Email: params.Email,
@ -89,7 +90,7 @@ func (i *usersInteractor) Create(ctx context.Context, params CreateParams) (*mod
// TODO fetch user PK from chain-api // 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 { if err = i.usersRepo.Create(ctx, user); err != nil {
return nil, fmt.Errorf("error create new user. %w", err) 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)) var users []*models.User = make([]*models.User, 0, len(params.Ids))
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) (err error) { 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"). From("users as u").
PlaceholderFormat(sq.Dollar) PlaceholderFormat(sq.Dollar)
@ -99,6 +99,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
//isAdmin bool //isAdmin bool
createdAt time.Time createdAt time.Time
activatedAt sql.NullTime activatedAt sql.NullTime
mnemonic string
) )
if err = rows.Scan( if err = rows.Scan(
@ -111,6 +112,7 @@ func (r *repositorySQL) Get(ctx context.Context, params GetParams) ([]*models.Us
&createdAt, &createdAt,
&activatedAt, &activatedAt,
&pk, &pk,
&mnemonic,
); err != nil { ); err != nil {
return fmt.Errorf("error scan row. %w", err) 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, Bip39Seed: seed,
PK: pk, PK: pk,
Mnemonic: mnemonic,
//Admin: isAdmin, //Admin: isAdmin,
CreatedAt: createdAt, CreatedAt: createdAt,
Activated: activatedAt.Valid, 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 { func (r *repositorySQL) Create(ctx context.Context, user *models.User) error {
if err := sqltools.Transaction(ctx, r.db, func(ctx context.Context) 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{ values := []any{
user.ID, user.ID,
@ -151,6 +154,7 @@ func (r *repositorySQL) Create(ctx context.Context, user *models.User) error {
user.Credentails.Telegram, user.Credentails.Telegram,
user.Bip39Seed, user.Bip39Seed,
user.PK, user.PK,
user.Mnemonic,
user.CreatedAt, user.CreatedAt,
} }

View File

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