package domain import ( "encoding/hex" "testing" "git.smesh.lol/tinywallet/pkg/script" ) // TestWatchRoundTrip: a watched script survives the store, is idempotent, and can be forgotten. func TestWatchRoundTrip(t *testing.T) { r := newRig(t) if _, err := r.domain.Restore("btc", phrase, "pass"); err != nil { t.Fatal(err) } witness, err := script.HTLC(mustHex(t, deriveClaimer), mustHex(t, deriveRefunder), hashOf(deriveHash), 500) if err != nil { t.Fatal(err) } if err := r.domain.WatchHTLC("btc", witness); err != nil { t.Fatal(err) } // Watching the same contract twice is one entry. if err := r.domain.WatchHTLC("btc", witness); err != nil { t.Fatal(err) } watches, err := r.domain.Watches("btc") if err != nil { t.Fatal(err) } if len(watches) != 1 || string(watches[0]) != string(script.P2WSH(witness)) { t.Fatalf("watches are %d entries", len(watches)) } if err := r.domain.WatchHTLC("nope", witness); err == nil { t.Fatal("a chain the wallet does not drive was watched") } if err := r.domain.WatchHTLC("btc", nil); err == nil { t.Fatal("an empty script was watched") } if err := r.domain.Unwatch("btc", script.P2WSH(witness)); err != nil { t.Fatal(err) } if watches, _ := r.domain.Watches("btc"); len(watches) != 0 { t.Fatalf("%d watched scripts after unwatch", len(watches)) } } // TestAWatchTurnsAnHTLCOutputIntoACoin: the point of the watch set. The wallet's own lookahead // never names a P2WSH contract, so a block paying it is invisible until the contract is watched. func TestAWatchTurnsAnHTLCOutputIntoACoin(t *testing.T) { r := newRig(t) if _, err := r.domain.Restore("btc", phrase, "pass"); err != nil { t.Fatal(err) } witness, err := script.HTLC(mustHex(t, deriveClaimer), mustHex(t, deriveRefunder), hashOf(deriveHash), 500) if err != nil { t.Fatal(err) } chain := r.chains["btc"] chain.syncTo = chaindataPosition(200) chain.matched = []uint32{150} chain.blocks = map[uint32][]byte{150: buildBlock(t, script.P2WSH(witness), 70_000)} // Without the watch the block pays nobody the wallet scans for. if _, err := r.domain.Sync("btc", 0); err != nil { t.Fatal(err) } coins, err := r.domain.Unspent() if err != nil { t.Fatal(err) } if len(coins) != 0 { t.Fatalf("%d coins before the contract was watched", len(coins)) } // The real order is derive, watch, then fund: a contract watched before the block is // scanned is found by the scan that sees it. (A height already marked scanned is not // refetched, which is why the watch has to come first.) second := newRig(t) if _, err := second.domain.Restore("btc", phrase, "pass"); err != nil { t.Fatal(err) } if err := second.domain.WatchHTLC("btc", witness); err != nil { t.Fatal(err) } secondChain := second.chains["btc"] secondChain.syncTo = chaindataPosition(200) secondChain.matched = []uint32{150} secondChain.blocks = map[uint32][]byte{150: buildBlock(t, script.P2WSH(witness), 70_000)} if _, err := second.domain.Sync("btc", 0); err != nil { t.Fatal(err) } coins, err = second.domain.Unspent() if err != nil { t.Fatal(err) } if len(coins) != 1 { t.Fatalf("%d coins after the contract was watched", len(coins)) } if coins[0].Value != 70_000 || coins[0].Height != 150 || coins[0].Chain != "btc" { t.Fatalf("coin %+v", coins[0]) } // The coin remembers the script that paid it, so a spender knows which key signs it. if string(coins[0].Script) != string(script.P2WSH(witness)) { t.Fatalf("coin script %x", coins[0].Script) } // And it belongs to this chain alone. if other, _ := second.domain.UnspentOn("blake"); len(other) != 0 { t.Fatalf("%d coins on the other chain", len(other)) } } // mustHex decodes test hex. func mustHex(t *testing.T, text string) []byte { t.Helper() raw, err := hex.DecodeString(text) if err != nil { t.Fatal(err) } return raw } // hashOf reads a 32-byte hex string. func hashOf(text string) [32]byte { var out [32]byte raw, _ := hex.DecodeString(text) copy(out[:], raw) return out }