github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lnutil/lnlib_test.go (about)

     1  package lnutil
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	// pubkey from bitcoin blockchain tx
    10  	// adfa661e05b2221a1425190265f2ab397ff5e00380b33c35b57142575defff14
    11  	pubKeyCmpd0 = [33]byte{
    12  		0x03, 0x70, 0xed, 0xd2, 0xa2, 0x47, 0x90, 0x76,
    13  		0x6d, 0xdc, 0xbc, 0x25, 0x98, 0x00, 0xd6, 0x7e,
    14  		0x83, 0x6d, 0x5b, 0xed, 0xd0, 0xa2, 0x74, 0x3c,
    15  		0x5f, 0x8c, 0x67, 0xd2, 0x6c, 0x9d, 0x90, 0xf6,
    16  		0x35}
    17  	// pubkey from bitcoin blockchain tx
    18  	// 23df526af42b7546987ebe7c2dd712faa146f78c7ae2162b3515fd861c7b045f
    19  	pubKeyCmpd1 = [33]byte{
    20  		0x03, 0xc1, 0xa3, 0xb5, 0xdc, 0x62, 0x60, 0xff,
    21  		0xdd, 0xd0, 0x6c, 0x6e, 0x52, 0x8c, 0x34, 0x40,
    22  		0x84, 0xe6, 0x96, 0xb2, 0x11, 0x14, 0x3f, 0xac,
    23  		0x15, 0xf6, 0x1f, 0x85, 0xe2, 0x45, 0x89, 0x29,
    24  		0x5a}
    25  )
    26  
    27  // CommitScript
    28  func TestScriptInCommitScript(t *testing.T) {
    29  	// test for a normal situation(blackbox test)
    30  	// input: inRKey, revocable key
    31  	// input: inTKey, timeout key
    32  	// input: inDelay
    33  	// want: wantB, byte slice
    34  	//
    35  	// blackbox test
    36  	inRKey := [33]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    37  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    38  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    39  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    40  		0x01}
    41  	inTKey := [33]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    42  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    43  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    44  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    45  		0x02}
    46  	var inDelay uint16 = 2
    47  
    48  	wantB := []byte{0x63, 0x21}
    49  	wantB = append(wantB, inRKey[:]...)
    50  	wantB = append(wantB, 0x67)
    51  	wantB = append(wantB, 0x52) // it is related to inDelay
    52  	wantB = append(wantB, []byte{0xb2, 0x75, 0x21}...)
    53  	wantB = append(wantB, inTKey[:]...)
    54  	wantB = append(wantB, []byte{0x68, 0xac}...)
    55  
    56  	if !bytes.Equal(CommitScript(inRKey, inTKey, inDelay), wantB) {
    57  		t.Fatalf("it needs to be equal")
    58  	}
    59  }
    60  
    61  // FundTxScript
    62  func TestFundTxScript(t *testing.T) {
    63  	// test for a normal situation
    64  	// input1: pubKeyCmpd0, 33 bytes array
    65  	// input2: pubKeyCmpd1, 33 bytes array
    66  	//
    67  	// test for swapped value
    68  	// this tests three patterns, normal-order, inverse-order and the same
    69  	_, swappedTrue, _ := FundTxScript(pubKeyCmpd0, pubKeyCmpd1)
    70  	if swappedTrue != true {
    71  		t.Fatalf("wrong swapped value")
    72  	}
    73  
    74  	_, swappedFalse, _ := FundTxScript(pubKeyCmpd1, pubKeyCmpd0)
    75  	if swappedFalse != false {
    76  		t.Fatalf("wrong swapped value")
    77  	}
    78  
    79  	_, swappedSame, _ := FundTxScript(pubKeyCmpd0, pubKeyCmpd0)
    80  	if swappedSame != false {
    81  		t.Fatalf("wrong swapped value")
    82  	}
    83  
    84  	// test for a normal situation(blackbox test)
    85  	// input1: pubKeyCmpd0, 33 bytes array
    86  	// input2: pubKeyCmpd1, 33 bytes array
    87  	// want: wantB, byte slice
    88  	script, _, _ := FundTxScript(pubKeyCmpd0, pubKeyCmpd1)
    89  
    90  	wantB := []byte{0x52, 0x21}
    91  	wantB = append(wantB, pubKeyCmpd1[:]...)
    92  	wantB = append(wantB, 0x21)
    93  	wantB = append(wantB, pubKeyCmpd0[:]...)
    94  	wantB = append(wantB, []byte{0x52, 0xae}...)
    95  
    96  	if !bytes.Equal(script, wantB) {
    97  		t.Fatalf("it needs to be equal")
    98  	}
    99  }
   100  
   101  // FundTxOut
   102  func TestFundTxOut(t *testing.T) {
   103  	// test for a normal situation(blackbox test)
   104  	// input1: pubKeyCmpd0, 33 bytes array
   105  	// input2: pubKeyCmpd1, 33 bytes array
   106  	// input3: inI, int64
   107  	// want1: wantI, int64
   108  	// want2: wantB, byte slice
   109  	var inI int64 = 2
   110  	txOut, _ := FundTxOut(pubKeyCmpd0, pubKeyCmpd1, inI)
   111  
   112  	var wantI int64 = 2
   113  	wantB := []byte{0x00, 0x20, 0x98, 0x5e, 0x92, 0x74, 0x25, 0x91,
   114  		0x16, 0xf0, 0x26, 0x7a, 0x69, 0x96, 0x84, 0x26,
   115  		0x7a, 0x6e, 0x26, 0xb8, 0xc0, 0x9b, 0x73, 0xd8,
   116  		0x6f, 0x37, 0x60, 0x45, 0xec, 0x6f, 0xe2, 0xd6,
   117  		0x03, 0xda}
   118  
   119  	if txOut.Value != wantI {
   120  		t.Fatalf("it needs to be equal")
   121  	}
   122  	if !bytes.Equal(txOut.PkScript, wantB) {
   123  		t.Fatalf("it needs to be equal")
   124  	}
   125  
   126  	// test for an anomaly situation
   127  	// input1: pubKeyCmpd0, 33 bytes array
   128  	// input2: pubKeyCmpd1, 33 bytes array
   129  	// input3: inIMinus, int64
   130  	// want: txOut is nil, err is not nil
   131  	//
   132  	// test for amt in case that amt is minus
   133  	var inIMinus int64 = -2
   134  	txOutNil, errNotNil := FundTxOut(pubKeyCmpd0, pubKeyCmpd1, inIMinus)
   135  
   136  	if txOutNil != nil {
   137  		t.Fatalf("it needs to be nil")
   138  	}
   139  	if errNotNil == nil {
   140  		t.Fatalf("it needs not to be nil")
   141  	}
   142  
   143  }