derive_test.go raw

   1  package domain
   2  
   3  import (
   4  	"crypto/sha256"
   5  	"encoding/hex"
   6  	"testing"
   7  )
   8  
   9  // The same vector pkg/script pins to the node: the witness script and the address a regtest
  10  // node compiled for these terms. The domain must produce them without a node in the loop.
  11  const (
  12  	deriveClaimer  = "03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd"
  13  	deriveRefunder = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
  14  	deriveHash     = "0000000000000000000000000000000000000000000000000000000000000001"
  15  	deriveLocktime = 500
  16  	deriveScript   = "632103a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bdad82012088a820" +
  17  		"0000000000000000000000000000000000000000000000000000000000000001876702f401b169210279be66" +
  18  		"7ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac68"
  19  	deriveAddress = "bcrt1qvsxuue8tp8gnfw02ynzxv5ns4ecpp8k4kjrx7eav02qv3jgad7tqzur2fy"
  20  )
  21  
  22  // TestDeriveHTLCMatchesTheNode: the wallet derives the contract the node compiled, from the
  23  // terms alone, with no descriptor compiler and no node.
  24  func TestDeriveHTLCMatchesTheNode(t *testing.T) {
  25  	dom, _ := signDomain(t)
  26  
  27  	claimer, err := hex.DecodeString(deriveClaimer)
  28  
  29  	if err != nil {
  30  		t.Fatal(err)
  31  	}
  32  
  33  	refunder, err := hex.DecodeString(deriveRefunder)
  34  
  35  	if err != nil {
  36  		t.Fatal(err)
  37  	}
  38  
  39  	raw, err := hex.DecodeString(deriveHash)
  40  
  41  	if err != nil {
  42  		t.Fatal(err)
  43  	}
  44  
  45  	var secretHash [32]byte
  46  
  47  	copy(secretHash[:], raw)
  48  
  49  	address, witness, program, err := dom.DeriveHTLC("btc", claimer, refunder, secretHash, deriveLocktime)
  50  
  51  	if err != nil {
  52  		t.Fatalf("DeriveHTLC: %v", err)
  53  	}
  54  
  55  	if got := hex.EncodeToString(witness); got != deriveScript {
  56  		t.Fatalf("the script is\n%s\nwant\n%s", got, deriveScript)
  57  	}
  58  
  59  	if address != deriveAddress {
  60  		t.Fatalf("the address is %s, want %s", address, deriveAddress)
  61  	}
  62  
  63  	if hex.EncodeToString(program[:]) == "" || program == ([32]byte{}) {
  64  		t.Fatal("the program is empty")
  65  	}
  66  
  67  	// The program is the SHA-256 of the script, which is what the address encodes.
  68  	if want := sha256Of(witness); program != want {
  69  		t.Fatalf("the program is %x, want %x", program, want)
  70  	}
  71  }
  72  
  73  // TestDeriveHTLCRefusesNonsense: the terms are a contract, not a suggestion.
  74  func TestDeriveHTLCRefusesNonsense(t *testing.T) {
  75  	dom, _ := signDomain(t)
  76  
  77  	claimer, _ := hex.DecodeString(deriveClaimer)
  78  	refunder, _ := hex.DecodeString(deriveRefunder)
  79  
  80  	var secretHash [32]byte
  81  
  82  	if _, _, _, err := dom.DeriveHTLC("btc", claimer[:32], refunder, secretHash, 500); err == nil {
  83  		t.Fatal("a 32-byte claimer key was accepted")
  84  	}
  85  
  86  	if _, _, _, err := dom.DeriveHTLC("btc", claimer, refunder, secretHash, 0); err == nil {
  87  		t.Fatal("a zero locktime was accepted")
  88  	}
  89  
  90  	if _, _, _, err := dom.DeriveHTLC("blake", claimer, refunder, secretHash, 500); err == nil {
  91  		t.Fatal("a chain the wallet does not drive was accepted")
  92  	}
  93  }
  94  
  95  // sha256Of is the hash the P2WSH program is built from.
  96  func sha256Of(raw []byte) [32]byte {
  97  	sum := sha256.Sum256(raw)
  98  
  99  	return sum
 100  }
 101