package domain import ( "encoding/binary" "errors" "fmt" "git.smesh.lol/tinywallet/pkg/script" "git.smesh.lol/tinywallet/pkg/wallet" ) // Watch limits. A watched script is a contract someone may pay; the list is small on purpose, // because every entry is matched against every filter. const ( // MaxWatches bounds how many extra scripts one chain may watch. MaxWatches = 64 // MaxWatchScript bounds one watched script. MaxWatchScript = 10000 ) // ErrWatch is a script this chain will not watch. var ErrWatch = errors.New("domain: a watched script is empty or too large") // watchKey names one chain's list. func watchKey(name string) string { return "watch/" + name } // PutWatches encodes a watch list: a count, then each script length-prefixed. func PutWatches(scripts [][]byte) []byte { out := binary.LittleEndian.AppendUint16(nil, uint16(len(scripts))) for _, witness := range scripts { out = binary.LittleEndian.AppendUint16(out, uint16(len(witness))) out = append(out, witness...) } return out } // ReadWatches decodes a watch list, refusing anything that does not fit its own lengths. func ReadWatches(raw []byte) ([][]byte, error) { if len(raw) < 2 { return nil, fmt.Errorf("domain: a watch list is %d bytes", len(raw)) } count := int(binary.LittleEndian.Uint16(raw)) at := 2 if count > MaxWatches { return nil, fmt.Errorf("domain: %d watched scripts, the limit is %d", count, MaxWatches) } scripts := make([][]byte, 0, count) for i := 0; i < count; i++ { if at+2 > len(raw) { return nil, fmt.Errorf("domain: a watch list ends inside a length") } length := int(binary.LittleEndian.Uint16(raw[at:])) at += 2 if at+length > len(raw) { return nil, fmt.Errorf("domain: a watched script runs past the list") } scripts = append(scripts, append([]byte(nil), raw[at:at+length]...)) at += length } if at != len(raw) { return nil, fmt.Errorf("domain: %d trailing bytes after the watch list", len(raw)-at) } return scripts, nil } // Watch adds a scriptPubKey to the chain's scan set. // // This is the output script, not a witness script: the filters are built over output scripts, // so a P2WSH contract is watched as its 34-byte OP_0 push. WatchHTLC does that wrapping for a // caller that holds the witness script. // // A swap's HTLC is an output the wallet's own addresses never name, so without a watcher the // filters would match a block and the wallet would still not see the contract it is a party to. // Watching is additive and idempotent: the same script twice is one entry. func (d *Domain) Watch(name string, witnessScript []byte) error { if _, err := d.link(name); err != nil { return err } if len(witnessScript) == 0 || len(witnessScript) > MaxWatchScript { return ErrWatch } watches, err := d.Watches(name) if err != nil { return err } for _, existing := range watches { if string(existing) == string(witnessScript) { return nil } } if len(watches) >= MaxWatches { return fmt.Errorf("domain: %q watches %d scripts, the limit is %d", name, len(watches), MaxWatches) } watches = append(watches, append([]byte(nil), witnessScript...)) return d.store.Put(watchKey(name), PutWatches(watches)) } // WatchHTLC watches a contract given its witness script, wrapping it in P2WSH. func (d *Domain) WatchHTLC(name string, witnessScript []byte) error { if len(witnessScript) == 0 || len(witnessScript) > MaxWatchScript { return ErrWatch } return d.Watch(name, script.P2WSH(witnessScript)) } // Unwatch forgets one script, for a swap that is settled and needs no further scanning. func (d *Domain) Unwatch(name string, witnessScript []byte) error { if _, err := d.link(name); err != nil { return err } watches, err := d.Watches(name) if err != nil { return err } kept := make([][]byte, 0, len(watches)) for _, existing := range watches { if string(existing) != string(witnessScript) { kept = append(kept, existing) } } return d.store.Put(watchKey(name), PutWatches(kept)) } // Watches lists the chain's watched scripts. func (d *Domain) Watches(name string) ([][]byte, error) { raw, ok := d.store.Get(watchKey(name)) if !ok { return nil, nil } return ReadWatches(raw) } // scanScripts is the script set a scan matches: the wallet's own receive and change addresses // over the lookahead, plus every contract the chain is watching. func (d *Domain) scanScripts(name string, derived *wallet.Wallet, meta Meta, lookahead uint32) ([][]byte, error) { watches, err := d.Watches(name) if err != nil { return nil, err } return scriptsFor(derived, meta, lookahead, watches) } // Rewind makes the next sync rescan from a height. // // A watched script can be added after the block that pays it was already // scanned, and the per-height match markers would otherwise skip that block. The // filters are already in the store, so nothing is downloaded again: dropping the // markers is what makes the block be fetched and read for the new script. func (d *Domain) Rewind(name string, height uint32) error { if _, err := d.link(name); err != nil { return err } meta, err := d.meta(name) if err != nil { return err } if height < meta.FilterFrom { meta.FilterFrom = height if err := d.store.Put(metaKey(name), PutMeta(meta)); err != nil { return err } } for _, key := range d.store.Keys(matchPrefix(name)) { matched, ok := parseMatchKey(key) if ok && matched >= height { if err := d.store.Delete(key); err != nil { return err } } } return d.store.Flush() }