mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 04:04:21 +01:00
* 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>
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
package badgerdb
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"github.com/ark-network/ark/server/internal/core/domain"
|
|
"github.com/dgraph-io/badger/v4"
|
|
"github.com/timshannon/badgerhold/v4"
|
|
)
|
|
|
|
const entityStoreDir = "entity"
|
|
|
|
type entityRepository struct {
|
|
store *badgerhold.Store
|
|
}
|
|
|
|
type entities struct {
|
|
Entities []domain.Entity
|
|
}
|
|
|
|
func NewEntityRepository(config ...interface{}) (domain.EntityRepository, error) {
|
|
if len(config) != 2 {
|
|
return nil, fmt.Errorf("invalid config")
|
|
}
|
|
|
|
baseDir, ok := config[0].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid base directory")
|
|
}
|
|
var logger badger.Logger
|
|
if config[1] != nil {
|
|
logger, ok = config[1].(badger.Logger)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid logger")
|
|
}
|
|
}
|
|
|
|
var dir string
|
|
if len(baseDir) > 0 {
|
|
dir = filepath.Join(baseDir, entityStoreDir)
|
|
}
|
|
store, err := createDB(dir, logger)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open entity store: %s", err)
|
|
}
|
|
|
|
return &entityRepository{store}, nil
|
|
}
|
|
|
|
func (r *entityRepository) Add(ctx context.Context, entity domain.Entity, vtxoKeys []domain.VtxoKey) error {
|
|
if len(vtxoKeys) == 0 {
|
|
return fmt.Errorf("vtxo keys are required")
|
|
}
|
|
|
|
for _, vtxoKey := range vtxoKeys {
|
|
var entities entities
|
|
err := r.store.Get(vtxoKey.String(), &entities)
|
|
if err == badgerhold.ErrNotFound {
|
|
entities.Entities = []domain.Entity{entity}
|
|
} else if err != nil {
|
|
return fmt.Errorf("failed to get entity: %w", err)
|
|
} else {
|
|
// check if entity already exists
|
|
for _, e := range entities.Entities {
|
|
if e.NostrRecipient == entity.NostrRecipient {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
entities.Entities = append(entities.Entities, entity)
|
|
}
|
|
|
|
err = r.store.Upsert(vtxoKey.String(), entities)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to upsert entity: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *entityRepository) Get(ctx context.Context, key domain.VtxoKey) ([]domain.Entity, error) {
|
|
var entities entities
|
|
err := r.store.Get(key.String(), &entities)
|
|
if err == badgerhold.ErrNotFound {
|
|
return nil, fmt.Errorf("entities not found for key: %s", key)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get entity: %w", err)
|
|
}
|
|
return entities.Entities, nil
|
|
}
|
|
|
|
func (r *entityRepository) Delete(ctx context.Context, vtxoKeys []domain.VtxoKey) error {
|
|
if len(vtxoKeys) == 0 {
|
|
return fmt.Errorf("vtxo keys are required")
|
|
}
|
|
|
|
for _, vtxoKey := range vtxoKeys {
|
|
err := r.store.Delete(vtxoKey.String(), &entities{})
|
|
if err == badgerhold.ErrNotFound {
|
|
return fmt.Errorf("entities not found for key: %s", vtxoKey)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete entity: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|