dossier_test.go raw

   1  package verify
   2  
   3  import (
   4  	"crypto/sha256"
   5  	"encoding/json"
   6  	"os"
   7  	"testing"
   8  
   9  	"git.smesh.lol/tinywallet/pkg/script"
  10  	"git.smesh.lol/tinywallet/pkg/tx"
  11  )
  12  
  13  func hexOf(b []byte) string {
  14  	const digits = "0123456789abcdef"
  15  
  16  	out := make([]byte, 0, 2*len(b))
  17  
  18  	for _, v := range b {
  19  		out = append(out, digits[v>>4], digits[v&0xf])
  20  	}
  21  
  22  	return string(out)
  23  }
  24  
  25  // TestAdjudicateJSONFromThePage is the exact path the panel uses: one dossier in, one report out,
  26  // with every external fact supplied by two sources and hash-verified.
  27  func TestAdjudicateJSONFromThePage(t *testing.T) {
  28  	terms, _ := testTerms(t)
  29  
  30  	var preimage [32]byte
  31  	preimage[0] = 7
  32  
  33  	hash := sha256.Sum256(preimage[:])
  34  	terms.HashLock = hash
  35  
  36  	claim := tx.New()
  37  	claim.AddInput(tx.Outpoint{Hash: [32]byte{0x11}, Index: 0}, tx.SequenceFinal)
  38  	claim.AddOutput(terms.AmountBTC-terms.FeeBTC, []byte{0x51})
  39  	claim.Inputs[0].Witness = [][]byte{preimage[:], {0x01}}
  40  
  41  	claimRaw, err := claim.SerializeWitness()
  42  
  43  	if err != nil {
  44  		t.Fatal(err)
  45  	}
  46  
  47  	legs := make([]LegJSON, 0, 2)
  48  
  49  	for _, side := range []struct {
  50  		chain    string
  51  		timeout  uint32
  52  		amount   uint64
  53  		fee      uint64
  54  		claimer  []byte
  55  		refunder []byte
  56  	}{
  57  		{"btc", terms.TimeoutBTC, terms.AmountBTC, terms.FeeBTC, terms.ClaimerBTC, terms.RefunderBTC},
  58  		{"xbt", terms.TimeoutXBT, terms.AmountXBT, terms.FeeXBT, terms.ClaimerXBT, terms.RefunderXBT},
  59  	} {
  60  		htlc, err := script.HTLC(side.claimer, side.refunder, hash, side.timeout)
  61  
  62  		if err != nil {
  63  			t.Fatal(err)
  64  		}
  65  
  66  		funding := buildTx(t, 0, tx.SequenceFinal,
  67  			tx.Output{Value: side.amount, Script: htlc},
  68  			tx.Output{Value: 1_000, Script: []byte{0x51}},
  69  		)
  70  
  71  		refund := buildTx(t, side.timeout, 0,
  72  			tx.Output{Value: side.amount - side.fee, Script: []byte{0x51}},
  73  		)
  74  
  75  		legs = append(legs, LegJSON{
  76  			Chain: side.chain, Funding: hexOf(rawOf(t, funding)), Refund: hexOf(rawOf(t, refund)),
  77  			Claim: hexOf(claimRaw), Timeout: side.timeout, Amount: side.amount, Fee: side.fee,
  78  			HashLock: hexOf(hash[:]), Claimer: hexOf(side.claimer), Refunder: hexOf(side.refunder),
  79  			ChangeScripts: []string{hexOf([]byte{0x51})},
  80  		})
  81  	}
  82  
  83  	facts := map[string]FactJSON{}
  84  
  85  	for _, leg := range legs {
  86  		for _, text := range []string{leg.Funding, leg.Refund, leg.Claim} {
  87  			raw, err := decodeHex(text)
  88  
  89  			if err != nil {
  90  				t.Fatal(err)
  91  			}
  92  
  93  			id, err := TxidOf(raw)
  94  
  95  			if err != nil {
  96  				t.Fatal(err)
  97  			}
  98  
  99  			facts[Hash(id)] = FactJSON{Found: true, Hex: text, Height: 962000}
 100  		}
 101  	}
 102  
 103  	body, err := json.Marshal(Dossier{
 104  		Terms: TermsJSON{
 105  			HashLock: hexOf(hash[:]), TimeoutBTC: terms.TimeoutBTC, TimeoutXBT: terms.TimeoutXBT,
 106  			AmountBTC: terms.AmountBTC, AmountXBT: terms.AmountXBT, FeeBTC: terms.FeeBTC, FeeXBT: terms.FeeXBT,
 107  			ClaimerBTC: hexOf(terms.ClaimerBTC), RefunderBTC: hexOf(terms.RefunderBTC),
 108  			ClaimerXBT: hexOf(terms.ClaimerXBT), RefunderXBT: hexOf(terms.RefunderXBT),
 109  			MinTimeoutGap: terms.MinTimeoutGap,
 110  		},
 111  		Legs: legs, Need: 2,
 112  		Sources: []SourceJSON{
 113  			{Name: "a", Facts: facts},
 114  			{Name: "b", Facts: facts},
 115  		},
 116  	})
 117  
 118  	if err != nil {
 119  		t.Fatal(err)
 120  	}
 121  
 122  	if path := os.Getenv("VERIFY_DOSSIER_OUT"); path != "" {
 123  		if err := os.WriteFile(path, body, 0o644); err != nil {
 124  			t.Fatal(err)
 125  		}
 126  	}
 127  
 128  	out, err := AdjudicateJSON(body)
 129  
 130  	if err != nil {
 131  		t.Fatal(err)
 132  	}
 133  
 134  	var report Report
 135  
 136  	if err := json.Unmarshal(out, &report); err != nil {
 137  		t.Fatal(err)
 138  	}
 139  
 140  	if report.Verdict != VerdictPass {
 141  		failed, unverifiable := report.Unverified()
 142  
 143  		for _, check := range append(failed, unverifiable...) {
 144  			t.Logf("%s (%s): %s", check.Name, check.Tier, check.Detail)
 145  		}
 146  
 147  		t.Fatalf("verdict %s, want pass", report.Verdict)
 148  	}
 149  
 150  	if report.Agreement != 2 {
 151  		t.Fatalf("agreement %d, want 2", report.Agreement)
 152  	}
 153  
 154  	if err := report.Gate(); err != nil {
 155  		t.Fatalf("a passing report was refused: %v", err)
 156  	}
 157  }
 158  
 159  func decodeHex(text string) ([]byte, error) {
 160  	const digits = "0123456789abcdef"
 161  	out := make([]byte, 0, len(text)/2)
 162  
 163  	for i := 0; i+1 < len(text); i += 2 {
 164  		hi := indexOf(digits, text[i])
 165  		lo := indexOf(digits, text[i+1])
 166  
 167  		if hi < 0 || lo < 0 {
 168  			return nil, errInvalidHex
 169  		}
 170  
 171  		out = append(out, byte(hi<<4|lo))
 172  	}
 173  
 174  	return out, nil
 175  }
 176  
 177  func indexOf(digits string, c byte) int {
 178  	for i := 0; i < len(digits); i++ {
 179  		if digits[i] == c {
 180  			return i
 181  		}
 182  	}
 183  
 184  	return -1
 185  }
 186  
 187  var errInvalidHex = &hexError{}
 188  
 189  type hexError struct{}
 190  
 191  func (*hexError) Error() string { return "invalid hex" }
 192