Files
ark/server/internal/infrastructure/db/sqlite/entity_repo.go
Louis Singer ff96524f22 Ark Notes (#379)
* ark credits

* rename "ecash" --> "ark credit"

* rework note_test.go

* NewFromString

* create several notes

* note repo: rename "push" to "add"

* RegisterInputsForNextRoundRequest: move "notes" to field #3

* use uint64 as note ID

* rename to voucher

* add nostr notification

* nostr notification test and fixes

* bump badger to 4.3

* allow npub to be registered

* rename poolTxID

* add default relays

* Update server/internal/config/config.go

Co-authored-by: Marco Argentieri <3596602+tiero@users.noreply.github.com>

* fix RedeemVouchers test

* notification = voucher

* WASM wrappers

* fix arkd voucher cmd

* test_utils.go ignore gosec rule G101

* fix permissions

* rename ALL to notes

* add URI prefix

* note.go : fix signature encoding

* fix decode note.Data

* Update server/internal/infrastructure/notifier/nostr/nostr.go

Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>

* Update pkg/client-sdk/wasm/browser/wrappers.go

Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>

* Update server/internal/infrastructure/notifier/nostr/nostr.go

Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>

* rework note and entity db + sqlite implementations

* NOTIFICATION_PREFIX -> NOTE_URI_PREFIX

* validate NOTE_URI_PREFIX

* Update defaults to convenant-less mainnet (#2)

* config: defaults to convenant-less tx builder

* Drop env var for blockchain scanner

---------

Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>

* add // before URI prefix

* add URI prefix in admin CreateNote

* Fixes

* rework nonces encoding (#4)

* rework nonces encoding

* add a check in Musig2Nonce decode function

* musig2_test: increase number of signers to 20

* musig2.json: add a test case with a 35 leaves tree

* GetEventStream REST rework

* fix round phases time intervals

* [SDK] Use server-side streams in rest client

* Fix history

* make the URI optional

* Updates

* Fix settled txs in history

* fix e2e test

* go work sync in sdk unit test

* fix signMessage in btc and liquid sdk wallets

---------

Co-authored-by: Marco Argentieri <3596602+tiero@users.noreply.github.com>
Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
2024-11-15 19:07:33 +01:00

101 lines
2.4 KiB
Go

package sqlitedb
import (
"context"
"database/sql"
"fmt"
"github.com/ark-network/ark/server/internal/core/domain"
"github.com/ark-network/ark/server/internal/infrastructure/db/sqlite/sqlc/queries"
)
type entityRepository struct {
db *sql.DB
querier *queries.Queries
}
func NewEntityRepository(config ...interface{}) (domain.EntityRepository, error) {
if len(config) != 1 {
return nil, fmt.Errorf("invalid config")
}
db, ok := config[0].(*sql.DB)
if !ok {
return nil, fmt.Errorf("cannot open entity repository: invalid config, expected db at 0")
}
return &entityRepository{
db: db,
querier: queries.New(db),
}, nil
}
func (r *entityRepository) Close() {
_ = r.db.Close()
}
func (r *entityRepository) Add(ctx context.Context, data domain.Entity, vtxoKeys []domain.VtxoKey) error {
id, err := r.querier.UpsertEntity(ctx, data.NostrRecipient)
if err != nil {
return err
}
for _, vtxoKey := range vtxoKeys {
if err := r.querier.UpsertEntityVtxo(ctx, queries.UpsertEntityVtxoParams{
EntityID: id,
VtxoTxid: vtxoKey.Txid,
VtxoVout: int64(vtxoKey.VOut),
}); err != nil {
return err
}
}
return nil
}
func (r *entityRepository) Delete(ctx context.Context, vtxoKeys []domain.VtxoKey) error {
for _, vtxoKey := range vtxoKeys {
entities, err := r.querier.SelectEntitiesByVtxo(ctx, queries.SelectEntitiesByVtxoParams{
VtxoTxid: sql.NullString{String: vtxoKey.Txid, Valid: true},
VtxoVout: sql.NullInt64{Int64: int64(vtxoKey.VOut), Valid: true},
})
if err != nil {
return err
}
for _, entity := range entities {
if err := r.querier.DeleteEntity(ctx, entity.EntityVw.ID); err != nil {
return err
}
if err := r.querier.DeleteEntityVtxo(ctx, entity.EntityVw.ID); err != nil {
return err
}
}
}
return nil
}
func (r *entityRepository) Get(ctx context.Context, vtxoKey domain.VtxoKey) ([]domain.Entity, error) {
entities, err := r.querier.SelectEntitiesByVtxo(ctx, queries.SelectEntitiesByVtxoParams{
VtxoTxid: sql.NullString{String: vtxoKey.Txid, Valid: true},
VtxoVout: sql.NullInt64{Int64: int64(vtxoKey.VOut), Valid: true},
})
if err != nil {
return nil, err
}
if len(entities) == 0 {
return nil, fmt.Errorf("no entities found")
}
result := make([]domain.Entity, 0, len(entities))
for _, entity := range entities {
result = append(result, domain.Entity{
NostrRecipient: entity.EntityVw.NostrRecipient,
})
}
return result, nil
}