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>
This commit is contained in:
Louis Singer
2024-11-15 19:07:33 +01:00
committed by GitHub
parent 963f5d89e6
commit ff96524f22
94 changed files with 6377 additions and 1230 deletions

View File

@@ -6,9 +6,11 @@ import (
"sync"
"time"
"github.com/ark-network/ark/common/note"
"github.com/ark-network/ark/common/tree"
"github.com/ark-network/ark/server/internal/core/domain"
"github.com/ark-network/ark/server/internal/core/ports"
nostr_notifier "github.com/ark-network/ark/server/internal/infrastructure/notifier/nostr"
log "github.com/sirupsen/logrus"
)
@@ -22,6 +24,8 @@ type sweeper struct {
builder ports.TxBuilder
scheduler ports.SchedulerService
noteUriPrefix string
// cache of scheduled tasks, avoid scheduling the same sweep event multiple times
locker sync.Locker
scheduledTasks map[string]struct{}
@@ -32,12 +36,14 @@ func newSweeper(
repoManager ports.RepoManager,
builder ports.TxBuilder,
scheduler ports.SchedulerService,
noteUriPrefix string,
) *sweeper {
return &sweeper{
wallet,
repoManager,
builder,
scheduler,
noteUriPrefix,
&sync.Mutex{},
make(map[string]struct{}),
}
@@ -255,6 +261,8 @@ func (s *sweeper) createTask(
}
log.Debugf("%d vtxos swept", len(vtxoKeys))
go s.createAndSendNotes(ctx, vtxoKeys)
}
}
@@ -311,6 +319,63 @@ func (s *sweeper) updateVtxoExpirationTime(
return s.repoManager.Vtxos().UpdateExpireAt(context.Background(), vtxos, expirationTime)
}
func (s *sweeper) createAndSendNotes(ctx context.Context, vtxosKeys []domain.VtxoKey) {
vtxos, err := s.repoManager.Vtxos().GetVtxos(ctx, vtxosKeys)
if err != nil {
log.Error(fmt.Errorf("error while getting vtxos: %w", err))
return
}
entitiesRepo := s.repoManager.Entities()
notifier := nostr_notifier.New()
for _, vtxo := range vtxos {
if !vtxo.Swept || vtxo.Redeemed || vtxo.Spent {
continue
}
// get the nostr recipients
entities, err := entitiesRepo.Get(ctx, vtxo.VtxoKey)
if err != nil {
log.Debugf("no entity found for vtxo %s", vtxo.VtxoKey)
continue
}
if len(entities) == 0 {
log.Debugf("no nostr recipient found for vtxo %s:%d, skipping note creation", vtxo.Txid, vtxo.VOut)
continue
}
// if vtxo is not redeemed or spent and is swept, create a note for it
noteData, err := note.New(uint32(vtxo.Amount))
if err != nil {
log.Error(fmt.Errorf("error while creating note data: %w", err))
continue
}
signature, err := s.wallet.SignMessage(ctx, noteData.Hash())
if err != nil {
log.Error(fmt.Errorf("error while signing note data: %w", err))
continue
}
note := noteData.ToNote(signature)
notification := note.String()
if len(s.noteUriPrefix) > 0 {
notification = fmt.Sprintf("%s://%s", s.noteUriPrefix, note)
}
for _, entity := range entities {
log.Debugf("sending note notification to %s", entity.NostrRecipient)
if err := notifier.Notify(ctx, entity.NostrRecipient, notification); err != nil {
log.Error(fmt.Errorf("error while sending note notification: %w", err))
}
}
}
}
func computeSubTrees(congestionTree tree.CongestionTree, inputs []ports.SweepInput) ([]tree.CongestionTree, error) {
subTrees := make(map[string]tree.CongestionTree, 0)