github.com/decred/dcrlnd@v0.7.6/input/signdescriptor_test.go (about)

     1  package input
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
     9  	"github.com/decred/dcrd/txscript/v4"
    10  	"github.com/decred/dcrd/wire"
    11  	"github.com/decred/dcrlnd/keychain"
    12  )
    13  
    14  func TestSignDescriptorSerialization(t *testing.T) {
    15  	keys := [][]byte{
    16  		{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    17  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    18  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    19  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    20  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    21  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    22  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    23  			0xb4, 0x12, 0xa3,
    24  		},
    25  		{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    26  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    27  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    28  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    29  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    30  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    31  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    32  			0xb4, 0x12, 0xa3,
    33  		},
    34  	}
    35  
    36  	signDescriptors := []SignDescriptor{
    37  		{
    38  			SingleTweak: []byte{
    39  				0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
    40  				0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
    41  				0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
    42  				0x02, 0x02, 0x02, 0x02, 0x02,
    43  			},
    44  			WitnessScript: []byte{
    45  				0x00, 0x14, 0xee, 0x91, 0x41, 0x7e, 0x85, 0x6c, 0xde,
    46  				0x10, 0xa2, 0x91, 0x1e, 0xdc, 0xbd, 0xbd, 0x69, 0xe2,
    47  				0xef, 0xb5, 0x71, 0x48,
    48  			},
    49  			Output: &wire.TxOut{
    50  				Value: 5000000000,
    51  				PkScript: []byte{
    52  					0x41, // OP_DATA_65
    53  					0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
    54  					0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
    55  					0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
    56  					0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
    57  					0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
    58  					0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
    59  					0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
    60  					0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
    61  					0xa6, // 65-byte signature
    62  					0xac, // OP_CHECKSIG
    63  				},
    64  			},
    65  			HashType: txscript.SigHashAll,
    66  		},
    67  
    68  		// Test serializing a SignDescriptor with a nil-valued PrivateTweak
    69  		{
    70  			SingleTweak: nil,
    71  			WitnessScript: []byte{
    72  				0x00, 0x14, 0xee, 0x91, 0x41, 0x7e, 0x85, 0x6c, 0xde,
    73  				0x10, 0xa2, 0x91, 0x1e, 0xdc, 0xbd, 0xbd, 0x69, 0xe2,
    74  				0xef, 0xb5, 0x71, 0x48,
    75  			},
    76  			Output: &wire.TxOut{
    77  				Value: 5000000000,
    78  				PkScript: []byte{
    79  					0x41, // OP_DATA_65
    80  					0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
    81  					0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
    82  					0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
    83  					0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
    84  					0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
    85  					0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
    86  					0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
    87  					0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
    88  					0xa6, // 65-byte signature
    89  					0xac, // OP_CHECKSIG
    90  				},
    91  			},
    92  			HashType: txscript.SigHashAll,
    93  		},
    94  	}
    95  
    96  	for i := 0; i < len(signDescriptors); i++ {
    97  		// Parse pubkeys for each sign descriptor.
    98  		sd := &signDescriptors[i]
    99  		pubkey, err := secp256k1.ParsePubKey(keys[i])
   100  		if err != nil {
   101  			t.Fatalf("unable to parse pubkey: %v", err)
   102  		}
   103  		sd.KeyDesc = keychain.KeyDescriptor{
   104  			KeyLocator: keychain.KeyLocator{
   105  				Family: 50,
   106  				Index:  99,
   107  			},
   108  			PubKey: pubkey,
   109  		}
   110  
   111  		// Test that serialize -> deserialize yields same result as original.
   112  		var buf bytes.Buffer
   113  		if err := WriteSignDescriptor(&buf, sd); err != nil {
   114  			t.Fatalf("unable to serialize sign descriptor[%v]: %v", i, sd)
   115  		}
   116  
   117  		desSd := &SignDescriptor{}
   118  		if err := ReadSignDescriptor(&buf, desSd); err != nil {
   119  			t.Fatalf("unable to deserialize sign descriptor[%v]: %v", i, sd)
   120  		}
   121  
   122  		if !reflect.DeepEqual(sd, desSd) {
   123  			t.Fatalf("original and deserialized sign descriptors not equal:\n"+
   124  				"original     : %+v\n"+
   125  				"deserialized : %+v\n",
   126  				sd, desSd)
   127  		}
   128  	}
   129  }