_sign_test.go raw

   1  package txscript
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  	"github.com/p9c/p9/pkg/btcaddr"
   7  	"github.com/p9c/p9/pkg/chaincfg"
   8  	"testing"
   9  	
  10  	"github.com/p9c/p9/pkg/chainhash"
  11  	ec "github.com/p9c/p9/pkg/ecc"
  12  	"github.com/p9c/p9/pkg/wire"
  13  )
  14  
  15  type addressToKey struct {
  16  	key        *ec.PrivateKey
  17  	compressed bool
  18  }
  19  
  20  func mkGetKey(keys map[string]addressToKey) KeyDB {
  21  	if keys == nil {
  22  		return KeyClosure(
  23  			func(addr btcaddr.Address) (
  24  				*ec.PrivateKey,
  25  				bool, error,
  26  			) {
  27  				return nil, false, errors.New("nope")
  28  			},
  29  		)
  30  	}
  31  	return KeyClosure(
  32  		func(addr btcaddr.Address) (
  33  			*ec.PrivateKey,
  34  			bool, error,
  35  		) {
  36  			a2k, ok := keys[addr.EncodeAddress()]
  37  			if !ok {
  38  				return nil, false, errors.New("nope")
  39  			}
  40  			return a2k.key, a2k.compressed, nil
  41  		},
  42  	)
  43  }
  44  func mkGetScript(scripts map[string][]byte) ScriptDB {
  45  	if scripts == nil {
  46  		return ScriptClosure(
  47  			func(addr btcaddr.Address) ([]byte, error) {
  48  				return nil, errors.New("nope")
  49  			},
  50  		)
  51  	}
  52  	return ScriptClosure(
  53  		func(addr btcaddr.Address) ([]byte, error) {
  54  			script, ok := scripts[addr.EncodeAddress()]
  55  			if !ok {
  56  				return nil, errors.New("nope")
  57  			}
  58  			return script, nil
  59  		},
  60  	)
  61  }
  62  func checkScripts(msg string, tx *wire.MsgTx, idx int, inputAmt int64, sigScript, pkScript []byte) (e error) {
  63  	tx.TxIn[idx].SignatureScript = sigScript
  64  	vm, e := NewEngine(
  65  		pkScript, tx, idx,
  66  		ScriptBip16|ScriptVerifyDERSignatures, nil, nil, inputAmt,
  67  	)
  68  	if e != nil {
  69  		return fmt.Errorf(
  70  			"failed to make script engine for %s: %v",
  71  			msg, e,
  72  		)
  73  	}
  74  	e = vm.Execute()
  75  	if e != nil {
  76  		return fmt.Errorf(
  77  			"invalid script signature for %s: %v", msg,
  78  			e,
  79  		)
  80  	}
  81  	return nil
  82  }
  83  func signAndCheck(
  84  	msg string, tx *wire.MsgTx, idx int, inputAmt int64, pkScript []byte,
  85  	hashType SigHashType, kdb KeyDB, sdb ScriptDB,
  86  	previousScript []byte,
  87  ) (e error) {
  88  	sigScript, e := SignTxOutput(
  89  		&chaincfg.TestNet3Params, tx, idx,
  90  		pkScript, hashType, kdb, sdb, nil,
  91  	)
  92  	if e != nil {
  93  		return fmt.Errorf("failed to sign output %s: %v", msg, e)
  94  	}
  95  	return checkScripts(msg, tx, idx, inputAmt, sigScript, pkScript)
  96  }
  97  func TestSignTxOutput(t *testing.T) {
  98  	t.Parallel()
  99  	// make key
 100  	// make script based on key.
 101  	// sign with magic pixie dust.
 102  	hashTypes := []SigHashType{
 103  		SigHashOld, // no longer used but should act like all
 104  		SigHashAll,
 105  		SigHashNone,
 106  		SigHashSingle,
 107  		SigHashAll | SigHashAnyOneCanPay,
 108  		SigHashNone | SigHashAnyOneCanPay,
 109  		SigHashSingle | SigHashAnyOneCanPay,
 110  	}
 111  	inputAmounts := []int64{5, 10, 15}
 112  	tx := &wire.MsgTx{
 113  		Version: 1,
 114  		TxIn: []*wire.TxIn{
 115  			{
 116  				PreviousOutPoint: wire.OutPoint{
 117  					Hash:  chainhash.Hash{},
 118  					Index: 0,
 119  				},
 120  				Sequence: 4294967295,
 121  			},
 122  			{
 123  				PreviousOutPoint: wire.OutPoint{
 124  					Hash:  chainhash.Hash{},
 125  					Index: 1,
 126  				},
 127  				Sequence: 4294967295,
 128  			},
 129  			{
 130  				PreviousOutPoint: wire.OutPoint{
 131  					Hash:  chainhash.Hash{},
 132  					Index: 2,
 133  				},
 134  				Sequence: 4294967295,
 135  			},
 136  		},
 137  		TxOut: []*wire.TxOut{
 138  			{
 139  				Value: 1,
 140  			},
 141  			{
 142  				Value: 2,
 143  			},
 144  			{
 145  				Value: 3,
 146  			},
 147  		},
 148  		LockTime: 0,
 149  	}
 150  	// Pay to Pubkey Hash (uncompressed)
 151  	for _, hashType := range hashTypes {
 152  		for i := range tx.TxIn {
 153  			msg := fmt.Sprintf("%d:%d", hashType, i)
 154  			key, e := ec.NewPrivateKey(ec.S256())
 155  			if e != nil {
 156  				t.Errorf(
 157  					"failed to make privKey for %s: %v",
 158  					msg, e,
 159  				)
 160  				break
 161  			}
 162  			pk := (*ec.PublicKey)(&key.PublicKey).
 163  				SerializeUncompressed()
 164  			address, e := btcaddr.NewPubKeyHash(
 165  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 166  			)
 167  			if e != nil {
 168  				t.Errorf(
 169  					"failed to make address for %s: %v",
 170  					msg, e,
 171  				)
 172  				break
 173  			}
 174  			pkScript, e := PayToAddrScript(address)
 175  			if e != nil {
 176  				t.Errorf(
 177  					"failed to make pkscript "+
 178  						"for %s: %v", msg, e,
 179  				)
 180  			}
 181  			if e := signAndCheck(
 182  				msg, tx, i, inputAmounts[i], pkScript, hashType,
 183  				mkGetKey(
 184  					map[string]addressToKey{
 185  						address.EncodeAddress(): {key, false},
 186  					},
 187  				), mkGetScript(nil), nil,
 188  			); E.Chk(e) {
 189  				break
 190  			}
 191  		}
 192  	}
 193  	// Pay to Pubkey Hash (uncompressed) (merging with correct)
 194  	for _, hashType := range hashTypes {
 195  		for i := range tx.TxIn {
 196  			msg := fmt.Sprintf("%d:%d", hashType, i)
 197  			key, e := ec.NewPrivateKey(ec.S256())
 198  			if e != nil {
 199  				t.Errorf(
 200  					"failed to make privKey for %s: %v",
 201  					msg, e,
 202  				)
 203  				break
 204  			}
 205  			pk := (*ec.PublicKey)(&key.PublicKey).
 206  				SerializeUncompressed()
 207  			address, e := btcaddr.NewPubKeyHash(
 208  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 209  			)
 210  			if e != nil {
 211  				t.Errorf(
 212  					"failed to make address for %s: %v",
 213  					msg, e,
 214  				)
 215  				break
 216  			}
 217  			pkScript, e := PayToAddrScript(address)
 218  			if e != nil {
 219  				t.Errorf(
 220  					"failed to make pkscript "+
 221  						"for %s: %v", msg, e,
 222  				)
 223  			}
 224  			sigScript, e := SignTxOutput(
 225  				&chaincfg.TestNet3Params,
 226  				tx, i, pkScript, hashType,
 227  				mkGetKey(
 228  					map[string]addressToKey{
 229  						address.EncodeAddress(): {key, false},
 230  					},
 231  				), mkGetScript(nil), nil,
 232  			)
 233  			if e != nil {
 234  				t.Errorf(
 235  					"failed to sign output %s: %v", msg,
 236  					e,
 237  				)
 238  				break
 239  			}
 240  			// by the above loop, this should be valid, now sign again and merge.
 241  			sigScript, e = SignTxOutput(
 242  				&chaincfg.TestNet3Params,
 243  				tx, i, pkScript, hashType,
 244  				mkGetKey(
 245  					map[string]addressToKey{
 246  						address.EncodeAddress(): {key, false},
 247  					},
 248  				), mkGetScript(nil), sigScript,
 249  			)
 250  			if e != nil {
 251  				t.Errorf(
 252  					"failed to sign output %s a "+
 253  						"second time: %v", msg, e,
 254  				)
 255  				break
 256  			}
 257  			e = checkScripts(msg, tx, i, inputAmounts[i], sigScript, pkScript)
 258  			if e != nil {
 259  				t.Errorf(
 260  					"twice signed script invalid for "+
 261  						"%s: %v", msg, e,
 262  				)
 263  				break
 264  			}
 265  		}
 266  	}
 267  	// Pay to Pubkey Hash (compressed)
 268  	for _, hashType := range hashTypes {
 269  		for i := range tx.TxIn {
 270  			msg := fmt.Sprintf("%d:%d", hashType, i)
 271  			key, e := ec.NewPrivateKey(ec.S256())
 272  			if e != nil {
 273  				t.Errorf(
 274  					"failed to make privKey for %s: %v",
 275  					msg, e,
 276  				)
 277  				break
 278  			}
 279  			pk := (*ec.PublicKey)(&key.PublicKey).
 280  				SerializeCompressed()
 281  			address, e := btcaddr.NewPubKeyHash(
 282  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 283  			)
 284  			if e != nil {
 285  				t.Errorf(
 286  					"failed to make address for %s: %v",
 287  					msg, e,
 288  				)
 289  				break
 290  			}
 291  			pkScript, e := PayToAddrScript(address)
 292  			if e != nil {
 293  				t.Errorf(
 294  					"failed to make pkscript "+
 295  						"for %s: %v", msg, e,
 296  				)
 297  			}
 298  			if e := signAndCheck(
 299  				msg, tx, i, inputAmounts[i],
 300  				pkScript, hashType,
 301  				mkGetKey(
 302  					map[string]addressToKey{
 303  						address.EncodeAddress(): {key, true},
 304  					},
 305  				), mkGetScript(nil), nil,
 306  			); E.Chk(e) {
 307  				break
 308  			}
 309  		}
 310  	}
 311  	// Pay to Pubkey Hash (compressed) with duplicate merge
 312  	for _, hashType := range hashTypes {
 313  		for i := range tx.TxIn {
 314  			msg := fmt.Sprintf("%d:%d", hashType, i)
 315  			key, e := ec.NewPrivateKey(ec.S256())
 316  			if e != nil {
 317  				t.Errorf(
 318  					"failed to make privKey for %s: %v",
 319  					msg, e,
 320  				)
 321  				break
 322  			}
 323  			pk := (*ec.PublicKey)(&key.PublicKey).
 324  				SerializeCompressed()
 325  			address, e := btcaddr.NewPubKeyHash(
 326  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 327  			)
 328  			if e != nil {
 329  				t.Errorf(
 330  					"failed to make address for %s: %v",
 331  					msg, e,
 332  				)
 333  				break
 334  			}
 335  			pkScript, e := PayToAddrScript(address)
 336  			if e != nil {
 337  				t.Errorf(
 338  					"failed to make pkscript "+
 339  						"for %s: %v", msg, e,
 340  				)
 341  			}
 342  			sigScript, e := SignTxOutput(
 343  				&chaincfg.TestNet3Params,
 344  				tx, i, pkScript, hashType,
 345  				mkGetKey(
 346  					map[string]addressToKey{
 347  						address.EncodeAddress(): {key, true},
 348  					},
 349  				), mkGetScript(nil), nil,
 350  			)
 351  			if e != nil {
 352  				t.Errorf(
 353  					"failed to sign output %s: %v", msg,
 354  					e,
 355  				)
 356  				break
 357  			}
 358  			// by the above loop, this should be valid, now sign again and merge.
 359  			sigScript, e = SignTxOutput(
 360  				&chaincfg.TestNet3Params,
 361  				tx, i, pkScript, hashType,
 362  				mkGetKey(
 363  					map[string]addressToKey{
 364  						address.EncodeAddress(): {key, true},
 365  					},
 366  				), mkGetScript(nil), sigScript,
 367  			)
 368  			if e != nil {
 369  				t.Errorf(
 370  					"failed to sign output %s a "+
 371  						"second time: %v", msg, e,
 372  				)
 373  				break
 374  			}
 375  			e = checkScripts(
 376  				msg, tx, i, inputAmounts[i],
 377  				sigScript, pkScript,
 378  			)
 379  			if e != nil {
 380  				t.Errorf(
 381  					"twice signed script invalid for "+
 382  						"%s: %v", msg, e,
 383  				)
 384  				break
 385  			}
 386  		}
 387  	}
 388  	// Pay to PubKey (uncompressed)
 389  	for _, hashType := range hashTypes {
 390  		for i := range tx.TxIn {
 391  			msg := fmt.Sprintf("%d:%d", hashType, i)
 392  			key, e := ec.NewPrivateKey(ec.S256())
 393  			if e != nil {
 394  				t.Errorf(
 395  					"failed to make privKey for %s: %v",
 396  					msg, e,
 397  				)
 398  				break
 399  			}
 400  			pk := (*ec.PublicKey)(&key.PublicKey).
 401  				SerializeUncompressed()
 402  			address, e := btcaddr.NewPubKey(
 403  				pk,
 404  				&chaincfg.TestNet3Params,
 405  			)
 406  			if e != nil {
 407  				t.Errorf(
 408  					"failed to make address for %s: %v",
 409  					msg, e,
 410  				)
 411  				break
 412  			}
 413  			pkScript, e := PayToAddrScript(address)
 414  			if e != nil {
 415  				t.Errorf(
 416  					"failed to make pkscript "+
 417  						"for %s: %v", msg, e,
 418  				)
 419  			}
 420  			if e := signAndCheck(
 421  				msg, tx, i, inputAmounts[i],
 422  				pkScript, hashType,
 423  				mkGetKey(
 424  					map[string]addressToKey{
 425  						address.EncodeAddress(): {key, false},
 426  					},
 427  				), mkGetScript(nil), nil,
 428  			); E.Chk(e) {
 429  				break
 430  			}
 431  		}
 432  	}
 433  	// Pay to PubKey (uncompressed)
 434  	for _, hashType := range hashTypes {
 435  		for i := range tx.TxIn {
 436  			msg := fmt.Sprintf("%d:%d", hashType, i)
 437  			key, e := ec.NewPrivateKey(ec.S256())
 438  			if e != nil {
 439  				t.Errorf(
 440  					"failed to make privKey for %s: %v",
 441  					msg, e,
 442  				)
 443  				break
 444  			}
 445  			pk := (*ec.PublicKey)(&key.PublicKey).
 446  				SerializeUncompressed()
 447  			address, e := btcaddr.NewPubKey(
 448  				pk,
 449  				&chaincfg.TestNet3Params,
 450  			)
 451  			if e != nil {
 452  				t.Errorf(
 453  					"failed to make address for %s: %v",
 454  					msg, e,
 455  				)
 456  				break
 457  			}
 458  			pkScript, e := PayToAddrScript(address)
 459  			if e != nil {
 460  				t.Errorf(
 461  					"failed to make pkscript "+
 462  						"for %s: %v", msg, e,
 463  				)
 464  			}
 465  			sigScript, e := SignTxOutput(
 466  				&chaincfg.TestNet3Params,
 467  				tx, i, pkScript, hashType,
 468  				mkGetKey(
 469  					map[string]addressToKey{
 470  						address.EncodeAddress(): {key, false},
 471  					},
 472  				), mkGetScript(nil), nil,
 473  			)
 474  			if e != nil {
 475  				t.Errorf(
 476  					"failed to sign output %s: %v", msg,
 477  					e,
 478  				)
 479  				break
 480  			}
 481  			// by the above loop, this should be valid, now sign again and merge.
 482  			sigScript, e = SignTxOutput(
 483  				&chaincfg.TestNet3Params,
 484  				tx, i, pkScript, hashType,
 485  				mkGetKey(
 486  					map[string]addressToKey{
 487  						address.EncodeAddress(): {key, false},
 488  					},
 489  				), mkGetScript(nil), sigScript,
 490  			)
 491  			if e != nil {
 492  				t.Errorf(
 493  					"failed to sign output %s a "+
 494  						"second time: %v", msg, e,
 495  				)
 496  				break
 497  			}
 498  			e = checkScripts(msg, tx, i, inputAmounts[i], sigScript, pkScript)
 499  			if e != nil {
 500  				t.Errorf(
 501  					"twice signed script invalid for "+
 502  						"%s: %v", msg, e,
 503  				)
 504  				break
 505  			}
 506  		}
 507  	}
 508  	// Pay to PubKey (compressed)
 509  	for _, hashType := range hashTypes {
 510  		for i := range tx.TxIn {
 511  			msg := fmt.Sprintf("%d:%d", hashType, i)
 512  			key, e := ec.NewPrivateKey(ec.S256())
 513  			if e != nil {
 514  				t.Errorf(
 515  					"failed to make privKey for %s: %v",
 516  					msg, e,
 517  				)
 518  				break
 519  			}
 520  			pk := (*ec.PublicKey)(&key.PublicKey).
 521  				SerializeCompressed()
 522  			address, e := btcaddr.NewPubKey(
 523  				pk,
 524  				&chaincfg.TestNet3Params,
 525  			)
 526  			if e != nil {
 527  				t.Errorf(
 528  					"failed to make address for %s: %v",
 529  					msg, e,
 530  				)
 531  				break
 532  			}
 533  			pkScript, e := PayToAddrScript(address)
 534  			if e != nil {
 535  				t.Errorf(
 536  					"failed to make pkscript "+
 537  						"for %s: %v", msg, e,
 538  				)
 539  			}
 540  			if e := signAndCheck(
 541  				msg, tx, i, inputAmounts[i],
 542  				pkScript, hashType,
 543  				mkGetKey(
 544  					map[string]addressToKey{
 545  						address.EncodeAddress(): {key, true},
 546  					},
 547  				), mkGetScript(nil), nil,
 548  			); E.Chk(e) {
 549  				break
 550  			}
 551  		}
 552  	}
 553  	// Pay to PubKey (compressed) with duplicate merge
 554  	for _, hashType := range hashTypes {
 555  		for i := range tx.TxIn {
 556  			msg := fmt.Sprintf("%d:%d", hashType, i)
 557  			key, e := ec.NewPrivateKey(ec.S256())
 558  			if e != nil {
 559  				t.Errorf(
 560  					"failed to make privKey for %s: %v",
 561  					msg, e,
 562  				)
 563  				break
 564  			}
 565  			pk := (*ec.PublicKey)(&key.PublicKey).
 566  				SerializeCompressed()
 567  			address, e := btcaddr.NewPubKey(
 568  				pk,
 569  				&chaincfg.TestNet3Params,
 570  			)
 571  			if e != nil {
 572  				t.Errorf(
 573  					"failed to make address for %s: %v",
 574  					msg, e,
 575  				)
 576  				break
 577  			}
 578  			pkScript, e := PayToAddrScript(address)
 579  			if e != nil {
 580  				t.Errorf(
 581  					"failed to make pkscript "+
 582  						"for %s: %v", msg, e,
 583  				)
 584  			}
 585  			sigScript, e := SignTxOutput(
 586  				&chaincfg.TestNet3Params,
 587  				tx, i, pkScript, hashType,
 588  				mkGetKey(
 589  					map[string]addressToKey{
 590  						address.EncodeAddress(): {key, true},
 591  					},
 592  				), mkGetScript(nil), nil,
 593  			)
 594  			if e != nil {
 595  				t.Errorf(
 596  					"failed to sign output %s: %v", msg,
 597  					e,
 598  				)
 599  				break
 600  			}
 601  			// by the above loop, this should be valid, now sign again and merge.
 602  			sigScript, e = SignTxOutput(
 603  				&chaincfg.TestNet3Params,
 604  				tx, i, pkScript, hashType,
 605  				mkGetKey(
 606  					map[string]addressToKey{
 607  						address.EncodeAddress(): {key, true},
 608  					},
 609  				), mkGetScript(nil), sigScript,
 610  			)
 611  			if e != nil {
 612  				t.Errorf(
 613  					"failed to sign output %s a "+
 614  						"second time: %v", msg, e,
 615  				)
 616  				break
 617  			}
 618  			e = checkScripts(
 619  				msg, tx, i, inputAmounts[i],
 620  				sigScript, pkScript,
 621  			)
 622  			if e != nil {
 623  				t.Errorf(
 624  					"twice signed script invalid for "+
 625  						"%s: %v", msg, e,
 626  				)
 627  				break
 628  			}
 629  		}
 630  	}
 631  	// As before, but with p2sh now. Pay to Pubkey Hash (uncompressed)
 632  	for _, hashType := range hashTypes {
 633  		for i := range tx.TxIn {
 634  			msg := fmt.Sprintf("%d:%d", hashType, i)
 635  			key, e := ec.NewPrivateKey(ec.S256())
 636  			if e != nil {
 637  				t.Errorf(
 638  					"failed to make privKey for %s: %v",
 639  					msg, e,
 640  				)
 641  				break
 642  			}
 643  			pk := (*ec.PublicKey)(&key.PublicKey).
 644  				SerializeUncompressed()
 645  			var ad *btcaddr.PubKeyHash
 646  			ad, e = btcaddr.NewPubKeyHash(
 647  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 648  			)
 649  			if e != nil {
 650  				t.Errorf(
 651  					"failed to make address for %s: %v",
 652  					msg, e,
 653  				)
 654  				break
 655  			}
 656  			pkScript, e := PayToAddrScript(ad)
 657  			if e != nil {
 658  				t.Errorf(
 659  					"failed to make pkscript "+
 660  						"for %s: %v", msg, e,
 661  				)
 662  				break
 663  			}
 664  			scriptAddr, e := NewAddressScriptHash(
 665  				pkScript, &chaincfg.TestNet3Params,
 666  			)
 667  			if e != nil {
 668  				t.Errorf(
 669  					"failed to make p2sh addr for %s: %v",
 670  					msg, e,
 671  				)
 672  				break
 673  			}
 674  			scriptPkScript, e := PayToAddrScript(
 675  				scriptAddr,
 676  			)
 677  			if e != nil {
 678  				t.Errorf(
 679  					"failed to make script pkscript for "+
 680  						"%s: %v", msg, e,
 681  				)
 682  				break
 683  			}
 684  			if e := signAndCheck(
 685  				msg, tx, i, inputAmounts[i],
 686  				scriptPkScript, hashType,
 687  				mkGetKey(
 688  					map[string]addressToKey{
 689  						address.EncodeAddress(): {key, false},
 690  					},
 691  				), mkGetScript(
 692  					map[string][]byte{
 693  						scriptAddr.EncodeAddress(): pkScript,
 694  					},
 695  				), nil,
 696  			); E.Chk(e) {
 697  				break
 698  			}
 699  		}
 700  	}
 701  	// Pay to Pubkey Hash (uncompressed) with duplicate merge
 702  	for _, hashType := range hashTypes {
 703  		for i := range tx.TxIn {
 704  			msg := fmt.Sprintf("%d:%d", hashType, i)
 705  			key, e := ec.NewPrivateKey(ec.S256())
 706  			if e != nil {
 707  				t.Errorf(
 708  					"failed to make privKey for %s: %v",
 709  					msg, e,
 710  				)
 711  				break
 712  			}
 713  			pk := (*ec.PublicKey)(&key.PublicKey).
 714  				SerializeUncompressed()
 715  			address, e := btcaddr.NewPubKeyHash(
 716  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 717  			)
 718  			if e != nil {
 719  				t.Errorf(
 720  					"failed to make address for %s: %v",
 721  					msg, e,
 722  				)
 723  				break
 724  			}
 725  			pkScript, e := PayToAddrScript(address)
 726  			if e != nil {
 727  				t.Errorf(
 728  					"failed to make pkscript "+
 729  						"for %s: %v", msg, e,
 730  				)
 731  				break
 732  			}
 733  			scriptAddr, e := address.NewAddressScriptHash(
 734  				pkScript, &chaincfg.TestNet3Params,
 735  			)
 736  			if e != nil {
 737  				t.Errorf(
 738  					"failed to make p2sh addr for %s: %v",
 739  					msg, e,
 740  				)
 741  				break
 742  			}
 743  			scriptPkScript, e := PayToAddrScript(
 744  				scriptAddr,
 745  			)
 746  			if e != nil {
 747  				t.Errorf(
 748  					"failed to make script pkscript for "+
 749  						"%s: %v", msg, e,
 750  				)
 751  				break
 752  			}
 753  			sigScript, e := SignTxOutput(
 754  				&chaincfg.TestNet3Params,
 755  				tx, i, scriptPkScript, hashType,
 756  				mkGetKey(
 757  					map[string]addressToKey{
 758  						address.EncodeAddress(): {key, false},
 759  					},
 760  				), mkGetScript(
 761  					map[string][]byte{
 762  						scriptAddr.EncodeAddress(): pkScript,
 763  					},
 764  				), nil,
 765  			)
 766  			if e != nil {
 767  				t.Errorf(
 768  					"failed to sign output %s: %v", msg,
 769  					e,
 770  				)
 771  				break
 772  			}
 773  			// by the above loop, this should be valid, now sign again and merge.
 774  			sigScript, e = SignTxOutput(
 775  				&chaincfg.TestNet3Params,
 776  				tx, i, scriptPkScript, hashType,
 777  				mkGetKey(
 778  					map[string]addressToKey{
 779  						address.EncodeAddress(): {key, false},
 780  					},
 781  				), mkGetScript(
 782  					map[string][]byte{
 783  						scriptAddr.EncodeAddress(): pkScript,
 784  					},
 785  				), nil,
 786  			)
 787  			if e != nil {
 788  				t.Errorf(
 789  					"failed to sign output %s a "+
 790  						"second time: %v", msg, e,
 791  				)
 792  				break
 793  			}
 794  			e = checkScripts(
 795  				msg, tx, i, inputAmounts[i],
 796  				sigScript, scriptPkScript,
 797  			)
 798  			if e != nil {
 799  				t.Errorf(
 800  					"twice signed script invalid for "+
 801  						"%s: %v", msg, e,
 802  				)
 803  				break
 804  			}
 805  		}
 806  	}
 807  	// Pay to Pubkey Hash (compressed)
 808  	for _, hashType := range hashTypes {
 809  		for i := range tx.TxIn {
 810  			msg := fmt.Sprintf("%d:%d", hashType, i)
 811  			key, e := ec.NewPrivateKey(ec.S256())
 812  			if e != nil {
 813  				t.Errorf(
 814  					"failed to make privKey for %s: %v",
 815  					msg, e,
 816  				)
 817  				break
 818  			}
 819  			pk := (*ec.PublicKey)(&key.PublicKey).
 820  				SerializeCompressed()
 821  			address, e := btcaddr.NewPubKeyHash(
 822  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 823  			)
 824  			if e != nil {
 825  				t.Errorf(
 826  					"failed to make address for %s: %v",
 827  					msg, e,
 828  				)
 829  				break
 830  			}
 831  			pkScript, e := PayToAddrScript(address)
 832  			if e != nil {
 833  				t.Errorf(
 834  					"failed to make pkscript "+
 835  						"for %s: %v", msg, e,
 836  				)
 837  			}
 838  			scriptAddr, e := address.NewAddressScriptHash(
 839  				pkScript, &chaincfg.TestNet3Params,
 840  			)
 841  			if e != nil {
 842  				t.Errorf(
 843  					"failed to make p2sh addr for %s: %v",
 844  					msg, e,
 845  				)
 846  				break
 847  			}
 848  			scriptPkScript, e := PayToAddrScript(
 849  				scriptAddr,
 850  			)
 851  			if e != nil {
 852  				t.Errorf(
 853  					"failed to make script pkscript for "+
 854  						"%s: %v", msg, e,
 855  				)
 856  				break
 857  			}
 858  			if e := signAndCheck(
 859  				msg, tx, i, inputAmounts[i],
 860  				scriptPkScript, hashType,
 861  				mkGetKey(
 862  					map[string]addressToKey{
 863  						address.EncodeAddress(): {key, true},
 864  					},
 865  				), mkGetScript(
 866  					map[string][]byte{
 867  						scriptAddr.EncodeAddress(): pkScript,
 868  					},
 869  				), nil,
 870  			); E.Chk(e) {
 871  				break
 872  			}
 873  		}
 874  	}
 875  	// Pay to Pubkey Hash (compressed) with duplicate merge
 876  	for _, hashType := range hashTypes {
 877  		for i := range tx.TxIn {
 878  			msg := fmt.Sprintf("%d:%d", hashType, i)
 879  			key, e := ec.NewPrivateKey(ec.S256())
 880  			if e != nil {
 881  				t.Errorf(
 882  					"failed to make privKey for %s: %v",
 883  					msg, e,
 884  				)
 885  				break
 886  			}
 887  			pk := (*ec.PublicKey)(&key.PublicKey).
 888  				SerializeCompressed()
 889  			address, e := btcaddr.NewPubKeyHash(
 890  				btcaddr.Hash160(pk), &chaincfg.TestNet3Params,
 891  			)
 892  			if e != nil {
 893  				t.Errorf(
 894  					"failed to make address for %s: %v",
 895  					msg, e,
 896  				)
 897  				break
 898  			}
 899  			pkScript, e := PayToAddrScript(address)
 900  			if e != nil {
 901  				t.Errorf(
 902  					"failed to make pkscript "+
 903  						"for %s: %v", msg, e,
 904  				)
 905  			}
 906  			scriptAddr, e := address.NewAddressScriptHash(
 907  				pkScript, &chaincfg.TestNet3Params,
 908  			)
 909  			if e != nil {
 910  				t.Errorf(
 911  					"failed to make p2sh addr for %s: %v",
 912  					msg, e,
 913  				)
 914  				break
 915  			}
 916  			scriptPkScript, e := PayToAddrScript(
 917  				scriptAddr,
 918  			)
 919  			if e != nil {
 920  				t.Errorf(
 921  					"failed to make script pkscript for "+
 922  						"%s: %v", msg, e,
 923  				)
 924  				break
 925  			}
 926  			sigScript, e := SignTxOutput(
 927  				&chaincfg.TestNet3Params,
 928  				tx, i, scriptPkScript, hashType,
 929  				mkGetKey(
 930  					map[string]addressToKey{
 931  						address.EncodeAddress(): {key, true},
 932  					},
 933  				), mkGetScript(
 934  					map[string][]byte{
 935  						scriptAddr.EncodeAddress(): pkScript,
 936  					},
 937  				), nil,
 938  			)
 939  			if e != nil {
 940  				t.Errorf(
 941  					"failed to sign output %s: %v", msg,
 942  					e,
 943  				)
 944  				break
 945  			}
 946  			// by the above loop, this should be valid, now sign again and merge.
 947  			sigScript, e = SignTxOutput(
 948  				&chaincfg.TestNet3Params,
 949  				tx, i, scriptPkScript, hashType,
 950  				mkGetKey(
 951  					map[string]addressToKey{
 952  						address.EncodeAddress(): {key, true},
 953  					},
 954  				), mkGetScript(
 955  					map[string][]byte{
 956  						scriptAddr.EncodeAddress(): pkScript,
 957  					},
 958  				), nil,
 959  			)
 960  			if e != nil {
 961  				t.Errorf(
 962  					"failed to sign output %s a "+
 963  						"second time: %v", msg, e,
 964  				)
 965  				break
 966  			}
 967  			e = checkScripts(
 968  				msg, tx, i, inputAmounts[i],
 969  				sigScript, scriptPkScript,
 970  			)
 971  			if e != nil {
 972  				t.Errorf(
 973  					"twice signed script invalid for "+
 974  						"%s: %v", msg, e,
 975  				)
 976  				break
 977  			}
 978  		}
 979  	}
 980  	// Pay to PubKey (uncompressed)
 981  	for _, hashType := range hashTypes {
 982  		for i := range tx.TxIn {
 983  			msg := fmt.Sprintf("%d:%d", hashType, i)
 984  			key, e := ec.NewPrivateKey(ec.S256())
 985  			if e != nil {
 986  				t.Errorf(
 987  					"failed to make privKey for %s: %v",
 988  					msg, e,
 989  				)
 990  				break
 991  			}
 992  			pk := (*ec.PublicKey)(&key.PublicKey).
 993  				SerializeUncompressed()
 994  			address, e := btcaddr.NewPubKey(
 995  				pk,
 996  				&chaincfg.TestNet3Params,
 997  			)
 998  			if e != nil {
 999  				t.Errorf(
1000  					"failed to make address for %s: %v",
1001  					msg, e,
1002  				)
1003  				break
1004  			}
1005  			pkScript, e := PayToAddrScript(address)
1006  			if e != nil {
1007  				t.Errorf(
1008  					"failed to make pkscript "+
1009  						"for %s: %v", msg, e,
1010  				)
1011  			}
1012  			scriptAddr, e := address.NewAddressScriptHash(
1013  				pkScript, &chaincfg.TestNet3Params,
1014  			)
1015  			if e != nil {
1016  				t.Errorf(
1017  					"failed to make p2sh addr for %s: %v",
1018  					msg, e,
1019  				)
1020  				break
1021  			}
1022  			scriptPkScript, e := PayToAddrScript(
1023  				scriptAddr,
1024  			)
1025  			if e != nil {
1026  				t.Errorf(
1027  					"failed to make script pkscript for "+
1028  						"%s: %v", msg, e,
1029  				)
1030  				break
1031  			}
1032  			if e := signAndCheck(
1033  				msg, tx, i, inputAmounts[i],
1034  				scriptPkScript, hashType,
1035  				mkGetKey(
1036  					map[string]addressToKey{
1037  						address.EncodeAddress(): {key, false},
1038  					},
1039  				), mkGetScript(
1040  					map[string][]byte{
1041  						scriptAddr.EncodeAddress(): pkScript,
1042  					},
1043  				), nil,
1044  			); E.Chk(e) {
1045  				break
1046  			}
1047  		}
1048  	}
1049  	// Pay to PubKey (uncompressed) with duplicate merge
1050  	for _, hashType := range hashTypes {
1051  		for i := range tx.TxIn {
1052  			msg := fmt.Sprintf("%d:%d", hashType, i)
1053  			key, e := ec.NewPrivateKey(ec.S256())
1054  			if e != nil {
1055  				t.Errorf(
1056  					"failed to make privKey for %s: %v",
1057  					msg, e,
1058  				)
1059  				break
1060  			}
1061  			pk := (*ec.PublicKey)(&key.PublicKey).
1062  				SerializeUncompressed()
1063  			address, e := btcaddr.NewPubKey(
1064  				pk,
1065  				&chaincfg.TestNet3Params,
1066  			)
1067  			if e != nil {
1068  				t.Errorf(
1069  					"failed to make address for %s: %v",
1070  					msg, e,
1071  				)
1072  				break
1073  			}
1074  			pkScript, e := PayToAddrScript(address)
1075  			if e != nil {
1076  				t.Errorf(
1077  					"failed to make pkscript "+
1078  						"for %s: %v", msg, e,
1079  				)
1080  			}
1081  			scriptAddr, e := address.NewAddressScriptHash(
1082  				pkScript, &chaincfg.TestNet3Params,
1083  			)
1084  			if e != nil {
1085  				t.Errorf(
1086  					"failed to make p2sh addr for %s: %v",
1087  					msg, e,
1088  				)
1089  				break
1090  			}
1091  			scriptPkScript, e := PayToAddrScript(scriptAddr)
1092  			if e != nil {
1093  				t.Errorf(
1094  					"failed to make script pkscript for "+
1095  						"%s: %v", msg, e,
1096  				)
1097  				break
1098  			}
1099  			sigScript, e := SignTxOutput(
1100  				&chaincfg.TestNet3Params,
1101  				tx, i, scriptPkScript, hashType,
1102  				mkGetKey(
1103  					map[string]addressToKey{
1104  						address.EncodeAddress(): {key, false},
1105  					},
1106  				), mkGetScript(
1107  					map[string][]byte{
1108  						scriptAddr.EncodeAddress(): pkScript,
1109  					},
1110  				), nil,
1111  			)
1112  			if e != nil {
1113  				t.Errorf(
1114  					"failed to sign output %s: %v", msg,
1115  					e,
1116  				)
1117  				break
1118  			}
1119  			// by the above loop, this should be valid, now sign again and merge.
1120  			sigScript, e = SignTxOutput(
1121  				&chaincfg.TestNet3Params,
1122  				tx, i, scriptPkScript, hashType,
1123  				mkGetKey(
1124  					map[string]addressToKey{
1125  						address.EncodeAddress(): {key, false},
1126  					},
1127  				), mkGetScript(
1128  					map[string][]byte{
1129  						scriptAddr.EncodeAddress(): pkScript,
1130  					},
1131  				), nil,
1132  			)
1133  			if e != nil {
1134  				t.Errorf(
1135  					"failed to sign output %s a "+
1136  						"second time: %v", msg, e,
1137  				)
1138  				break
1139  			}
1140  			e = checkScripts(
1141  				msg, tx, i, inputAmounts[i],
1142  				sigScript, scriptPkScript,
1143  			)
1144  			if e != nil {
1145  				t.Errorf(
1146  					"twice signed script invalid for "+
1147  						"%s: %v", msg, e,
1148  				)
1149  				break
1150  			}
1151  		}
1152  	}
1153  	// Pay to PubKey (compressed)
1154  	for _, hashType := range hashTypes {
1155  		for i := range tx.TxIn {
1156  			msg := fmt.Sprintf("%d:%d", hashType, i)
1157  			key, e := ec.NewPrivateKey(ec.S256())
1158  			if e != nil {
1159  				t.Errorf(
1160  					"failed to make privKey for %s: %v",
1161  					msg, e,
1162  				)
1163  				break
1164  			}
1165  			pk := (*ec.PublicKey)(&key.PublicKey).
1166  				SerializeCompressed()
1167  			address, e := btcaddr.NewPubKey(
1168  				pk,
1169  				&chaincfg.TestNet3Params,
1170  			)
1171  			if e != nil {
1172  				t.Errorf(
1173  					"failed to make address for %s: %v",
1174  					msg, e,
1175  				)
1176  				break
1177  			}
1178  			pkScript, e := PayToAddrScript(address)
1179  			if e != nil {
1180  				t.Errorf(
1181  					"failed to make pkscript "+
1182  						"for %s: %v", msg, e,
1183  				)
1184  			}
1185  			scriptAddr, e := address.NewAddressScriptHash(
1186  				pkScript, &chaincfg.TestNet3Params,
1187  			)
1188  			if e != nil {
1189  				t.Errorf(
1190  					"failed to make p2sh addr for %s: %v",
1191  					msg, e,
1192  				)
1193  				break
1194  			}
1195  			scriptPkScript, e := PayToAddrScript(scriptAddr)
1196  			if e != nil {
1197  				t.Errorf(
1198  					"failed to make script pkscript for "+
1199  						"%s: %v", msg, e,
1200  				)
1201  				break
1202  			}
1203  			if e := signAndCheck(
1204  				msg, tx, i, inputAmounts[i],
1205  				scriptPkScript, hashType,
1206  				mkGetKey(
1207  					map[string]addressToKey{
1208  						address.EncodeAddress(): {key, true},
1209  					},
1210  				), mkGetScript(
1211  					map[string][]byte{
1212  						scriptAddr.EncodeAddress(): pkScript,
1213  					},
1214  				), nil,
1215  			); E.Chk(e) {
1216  				break
1217  			}
1218  		}
1219  	}
1220  	// Pay to PubKey (compressed)
1221  	for _, hashType := range hashTypes {
1222  		for i := range tx.TxIn {
1223  			msg := fmt.Sprintf("%d:%d", hashType, i)
1224  			key, e := ec.NewPrivateKey(ec.S256())
1225  			if e != nil {
1226  				t.Errorf(
1227  					"failed to make privKey for %s: %v",
1228  					msg, e,
1229  				)
1230  				break
1231  			}
1232  			pk := (*ec.PublicKey)(&key.PublicKey).
1233  				SerializeCompressed()
1234  			address, e := btcaddr.NewPubKey(
1235  				pk,
1236  				&chaincfg.TestNet3Params,
1237  			)
1238  			if e != nil {
1239  				t.Errorf(
1240  					"failed to make address for %s: %v",
1241  					msg, e,
1242  				)
1243  				break
1244  			}
1245  			pkScript, e := PayToAddrScript(address)
1246  			if e != nil {
1247  				t.Errorf(
1248  					"failed to make pkscript "+
1249  						"for %s: %v", msg, e,
1250  				)
1251  			}
1252  			scriptAddr, e := address.NewAddressScriptHash(
1253  				pkScript, &chaincfg.TestNet3Params,
1254  			)
1255  			if e != nil {
1256  				t.Errorf(
1257  					"failed to make p2sh addr for %s: %v",
1258  					msg, e,
1259  				)
1260  				break
1261  			}
1262  			scriptPkScript, e := PayToAddrScript(scriptAddr)
1263  			if e != nil {
1264  				t.Errorf(
1265  					"failed to make script pkscript for "+
1266  						"%s: %v", msg, e,
1267  				)
1268  				break
1269  			}
1270  			sigScript, e := SignTxOutput(
1271  				&chaincfg.TestNet3Params,
1272  				tx, i, scriptPkScript, hashType,
1273  				mkGetKey(
1274  					map[string]addressToKey{
1275  						address.EncodeAddress(): {key, true},
1276  					},
1277  				), mkGetScript(
1278  					map[string][]byte{
1279  						scriptAddr.EncodeAddress(): pkScript,
1280  					},
1281  				), nil,
1282  			)
1283  			if e != nil {
1284  				t.Errorf(
1285  					"failed to sign output %s: %v", msg,
1286  					e,
1287  				)
1288  				break
1289  			}
1290  			// by the above loop, this should be valid, now sign again and merge.
1291  			sigScript, e = SignTxOutput(
1292  				&chaincfg.TestNet3Params,
1293  				tx, i, scriptPkScript, hashType,
1294  				mkGetKey(
1295  					map[string]addressToKey{
1296  						address.EncodeAddress(): {key, true},
1297  					},
1298  				), mkGetScript(
1299  					map[string][]byte{
1300  						scriptAddr.EncodeAddress(): pkScript,
1301  					},
1302  				), nil,
1303  			)
1304  			if e != nil {
1305  				t.Errorf(
1306  					"failed to sign output %s a "+
1307  						"second time: %v", msg, e,
1308  				)
1309  				break
1310  			}
1311  			e = checkScripts(
1312  				msg, tx, i, inputAmounts[i],
1313  				sigScript, scriptPkScript,
1314  			)
1315  			if e != nil {
1316  				t.Errorf(
1317  					"twice signed script invalid for "+
1318  						"%s: %v", msg, e,
1319  				)
1320  				break
1321  			}
1322  		}
1323  	}
1324  	// Basic Multisig
1325  	for _, hashType := range hashTypes {
1326  		for i := range tx.TxIn {
1327  			msg := fmt.Sprintf("%d:%d", hashType, i)
1328  			key1, e := ec.NewPrivateKey(ec.S256())
1329  			if e != nil {
1330  				t.Errorf(
1331  					"failed to make privKey for %s: %v",
1332  					msg, e,
1333  				)
1334  				break
1335  			}
1336  			pk1 := (*ec.PublicKey)(&key1.PublicKey).
1337  				SerializeCompressed()
1338  			address1, e := btcaddr.NewPubKey(
1339  				pk1,
1340  				&chaincfg.TestNet3Params,
1341  			)
1342  			if e != nil {
1343  				t.Errorf(
1344  					"failed to make address for %s: %v",
1345  					msg, e,
1346  				)
1347  				break
1348  			}
1349  			key2, e := ec.NewPrivateKey(ec.S256())
1350  			if e != nil {
1351  				t.Errorf(
1352  					"failed to make privKey 2 for %s: %v",
1353  					msg, e,
1354  				)
1355  				break
1356  			}
1357  			pk2 := (*ec.PublicKey)(&key2.PublicKey).
1358  				SerializeCompressed()
1359  			address2, e := btcaddr.NewPubKey(
1360  				pk2,
1361  				&chaincfg.TestNet3Params,
1362  			)
1363  			if e != nil {
1364  				t.Errorf(
1365  					"failed to make address 2 for %s: %v",
1366  					msg, e,
1367  				)
1368  				break
1369  			}
1370  			pkScript, e := MultiSigScript(
1371  				[]*btcaddr.PubKey{address1, address2},
1372  				2,
1373  			)
1374  			if e != nil {
1375  				t.Errorf(
1376  					"failed to make pkscript "+
1377  						"for %s: %v", msg, e,
1378  				)
1379  			}
1380  			scriptAddr, e := btcaddr.NewScriptHash(
1381  				pkScript, &chaincfg.TestNet3Params,
1382  			)
1383  			if e != nil {
1384  				t.Errorf(
1385  					"failed to make p2sh addr for %s: %v",
1386  					msg, e,
1387  				)
1388  				break
1389  			}
1390  			scriptPkScript, e := PayToAddrScript(scriptAddr)
1391  			if e != nil {
1392  				t.Errorf(
1393  					"failed to make script pkscript for "+
1394  						"%s: %v", msg, e,
1395  				)
1396  				break
1397  			}
1398  			if e := signAndCheck(
1399  				msg, tx, i, inputAmounts[i],
1400  				scriptPkScript, hashType,
1401  				mkGetKey(
1402  					map[string]addressToKey{
1403  						address1.EncodeAddress(): {key1, true},
1404  						address2.EncodeAddress(): {key2, true},
1405  					},
1406  				), mkGetScript(
1407  					map[string][]byte{
1408  						scriptAddr.EncodeAddress(): pkScript,
1409  					},
1410  				), nil,
1411  			); E.Chk(e) {
1412  				break
1413  			}
1414  		}
1415  	}
1416  	// Two part multisig, sign with one key then the other.
1417  	for _, hashType := range hashTypes {
1418  		for i := range tx.TxIn {
1419  			msg := fmt.Sprintf("%d:%d", hashType, i)
1420  			key1, e := ec.NewPrivateKey(ec.S256())
1421  			if e != nil {
1422  				t.Errorf(
1423  					"failed to make privKey for %s: %v",
1424  					msg, e,
1425  				)
1426  				break
1427  			}
1428  			pk1 := (*ec.PublicKey)(&key1.PublicKey).
1429  				SerializeCompressed()
1430  			address1, e := btcaddr.NewPubKey(
1431  				pk1,
1432  				&chaincfg.TestNet3Params,
1433  			)
1434  			if e != nil {
1435  				t.Errorf(
1436  					"failed to make address for %s: %v",
1437  					msg, e,
1438  				)
1439  				break
1440  			}
1441  			key2, e := ec.NewPrivateKey(ec.S256())
1442  			if e != nil {
1443  				t.Errorf(
1444  					"failed to make privKey 2 for %s: %v",
1445  					msg, e,
1446  				)
1447  				break
1448  			}
1449  			pk2 := (*ec.PublicKey)(&key2.PublicKey).
1450  				SerializeCompressed()
1451  			address2, e := btcaddr.NewPubKey(
1452  				pk2,
1453  				&chaincfg.TestNet3Params,
1454  			)
1455  			if e != nil {
1456  				t.Errorf(
1457  					"failed to make address 2 for %s: %v",
1458  					msg, e,
1459  				)
1460  				break
1461  			}
1462  			pkScript, e := MultiSigScript(
1463  				[]*btcaddr.PubKey{address1, address2},
1464  				2,
1465  			)
1466  			if e != nil {
1467  				t.Errorf(
1468  					"failed to make pkscript "+
1469  						"for %s: %v", msg, e,
1470  				)
1471  			}
1472  			scriptAddr, e := btcaddr.NewScriptHash(
1473  				pkScript, &chaincfg.TestNet3Params,
1474  			)
1475  			if e != nil {
1476  				t.Errorf(
1477  					"failed to make p2sh addr for %s: %v",
1478  					msg, e,
1479  				)
1480  				break
1481  			}
1482  			scriptPkScript, e := PayToAddrScript(scriptAddr)
1483  			if e != nil {
1484  				t.Errorf(
1485  					"failed to make script pkscript for "+
1486  						"%s: %v", msg, e,
1487  				)
1488  				break
1489  			}
1490  			sigScript, e := SignTxOutput(
1491  				&chaincfg.TestNet3Params,
1492  				tx, i, scriptPkScript, hashType,
1493  				mkGetKey(
1494  					map[string]addressToKey{
1495  						address1.EncodeAddress(): {key1, true},
1496  					},
1497  				), mkGetScript(
1498  					map[string][]byte{
1499  						scriptAddr.EncodeAddress(): pkScript,
1500  					},
1501  				), nil,
1502  			)
1503  			if e != nil {
1504  				t.Errorf(
1505  					"failed to sign output %s: %v", msg,
1506  					e,
1507  				)
1508  				break
1509  			}
1510  			// Only 1 out of 2 signed, this *should* fail.
1511  			if checkScripts(
1512  				msg, tx, i, inputAmounts[i], sigScript,
1513  				scriptPkScript,
1514  			) == nil {
1515  				t.Errorf("part signed script valid for %s", msg)
1516  				break
1517  			}
1518  			// Sign with the other key and merge
1519  			sigScript, e = SignTxOutput(
1520  				&chaincfg.TestNet3Params,
1521  				tx, i, scriptPkScript, hashType,
1522  				mkGetKey(
1523  					map[string]addressToKey{
1524  						address2.EncodeAddress(): {key2, true},
1525  					},
1526  				), mkGetScript(
1527  					map[string][]byte{
1528  						scriptAddr.EncodeAddress(): pkScript,
1529  					},
1530  				), sigScript,
1531  			)
1532  			if e != nil {
1533  				t.Errorf("failed to sign output %s: %v", msg, e)
1534  				break
1535  			}
1536  			e = checkScripts(
1537  				msg, tx, i, inputAmounts[i], sigScript,
1538  				scriptPkScript,
1539  			)
1540  			if e != nil {
1541  				t.Errorf(
1542  					"fully signed script invalid for "+
1543  						"%s: %v", msg, e,
1544  				)
1545  				break
1546  			}
1547  		}
1548  	}
1549  	// Two part multisig, sign with one key then both, check key dedup correctly.
1550  	for _, hashType := range hashTypes {
1551  		for i := range tx.TxIn {
1552  			msg := fmt.Sprintf("%d:%d", hashType, i)
1553  			key1, e := ec.NewPrivateKey(ec.S256())
1554  			if e != nil {
1555  				t.Errorf(
1556  					"failed to make privKey for %s: %v",
1557  					msg, e,
1558  				)
1559  				break
1560  			}
1561  			pk1 := (*ec.PublicKey)(&key1.PublicKey).
1562  				SerializeCompressed()
1563  			address1, e := btcaddr.NewPubKey(
1564  				pk1,
1565  				&chaincfg.TestNet3Params,
1566  			)
1567  			if e != nil {
1568  				t.Errorf(
1569  					"failed to make address for %s: %v",
1570  					msg, e,
1571  				)
1572  				break
1573  			}
1574  			key2, e := ec.NewPrivateKey(ec.S256())
1575  			if e != nil {
1576  				t.Errorf(
1577  					"failed to make privKey 2 for %s: %v",
1578  					msg, e,
1579  				)
1580  				break
1581  			}
1582  			pk2 := (*ec.PublicKey)(&key2.PublicKey).
1583  				SerializeCompressed()
1584  			address2, e := btcaddr.NewPubKey(
1585  				pk2,
1586  				&chaincfg.TestNet3Params,
1587  			)
1588  			if e != nil {
1589  				t.Errorf(
1590  					"failed to make address 2 for %s: %v",
1591  					msg, e,
1592  				)
1593  				break
1594  			}
1595  			pkScript, e := MultiSigScript(
1596  				[]*btcaddr.PubKey{address1, address2},
1597  				2,
1598  			)
1599  			if e != nil {
1600  				t.Errorf(
1601  					"failed to make pkscript "+
1602  						"for %s: %v", msg, e,
1603  				)
1604  			}
1605  			scriptAddr, e := btcaddr.NewScriptHash(
1606  				pkScript, &chaincfg.TestNet3Params,
1607  			)
1608  			if e != nil {
1609  				t.Errorf(
1610  					"failed to make p2sh addr for %s: %v",
1611  					msg, e,
1612  				)
1613  				break
1614  			}
1615  			scriptPkScript, e := PayToAddrScript(scriptAddr)
1616  			if e != nil {
1617  				t.Errorf(
1618  					"failed to make script pkscript for "+
1619  						"%s: %v", msg, e,
1620  				)
1621  				break
1622  			}
1623  			sigScript, e := SignTxOutput(
1624  				&chaincfg.TestNet3Params,
1625  				tx, i, scriptPkScript, hashType,
1626  				mkGetKey(
1627  					map[string]addressToKey{
1628  						address1.EncodeAddress(): {key1, true},
1629  					},
1630  				), mkGetScript(
1631  					map[string][]byte{
1632  						scriptAddr.EncodeAddress(): pkScript,
1633  					},
1634  				), nil,
1635  			)
1636  			if e != nil {
1637  				t.Errorf(
1638  					"failed to sign output %s: %v", msg,
1639  					e,
1640  				)
1641  				break
1642  			}
1643  			// Only 1 out of 2 signed, this *should* fail.
1644  			if checkScripts(
1645  				msg, tx, i, inputAmounts[i], sigScript,
1646  				scriptPkScript,
1647  			) == nil {
1648  				t.Errorf("part signed script valid for %s", msg)
1649  				break
1650  			}
1651  			// Sign with the other key and merge
1652  			sigScript, e = SignTxOutput(
1653  				&chaincfg.TestNet3Params,
1654  				tx, i, scriptPkScript, hashType,
1655  				mkGetKey(
1656  					map[string]addressToKey{
1657  						address1.EncodeAddress(): {key1, true},
1658  						address2.EncodeAddress(): {key2, true},
1659  					},
1660  				), mkGetScript(
1661  					map[string][]byte{
1662  						scriptAddr.EncodeAddress(): pkScript,
1663  					},
1664  				), sigScript,
1665  			)
1666  			if e != nil {
1667  				t.Errorf("failed to sign output %s: %v", msg, e)
1668  				break
1669  			}
1670  			// Now we should pass.
1671  			e = checkScripts(
1672  				msg, tx, i, inputAmounts[i],
1673  				sigScript, scriptPkScript,
1674  			)
1675  			if e != nil {
1676  				t.Errorf(
1677  					"fully signed script invalid for "+
1678  						"%s: %v", msg, e,
1679  				)
1680  				break
1681  			}
1682  		}
1683  	}
1684  }
1685  
1686  type tstInput struct {
1687  	txout              *wire.TxOut
1688  	sigscriptGenerates bool
1689  	inputValidates     bool
1690  	indexOutOfRange    bool
1691  }
1692  type tstSigScript struct {
1693  	name               string
1694  	inputs             []tstInput
1695  	hashType           SigHashType
1696  	compress           bool
1697  	scriptAtWrongIndex bool
1698  }
1699  
1700  var coinbaseOutPoint = &wire.OutPoint{
1701  	Index: (1 << 32) - 1,
1702  }
1703  
1704  // Pregenerated private key, with associated public key and pkScripts for the uncompressed and compressed hash160.
1705  var (
1706  	privKeyD = []byte{
1707  		0x6b, 0x0f, 0xd8, 0xda, 0x54, 0x22, 0xd0, 0xb7,
1708  		0xb4, 0xfc, 0x4e, 0x55, 0xd4, 0x88, 0x42, 0xb3, 0xa1, 0x65,
1709  		0xac, 0x70, 0x7f, 0x3d, 0xa4, 0x39, 0x5e, 0xcb, 0x3b, 0xb0,
1710  		0xd6, 0x0e, 0x06, 0x92,
1711  	}
1712  	// pubkeyX = []byte{0xb2, 0x52, 0xf0, 0x49, 0x85, 0x78, 0x03, 0x03, 0xc8,
1713  	// 	0x7d, 0xce, 0x51, 0x7f, 0xa8, 0x69, 0x0b, 0x91, 0x95, 0xf4,
1714  	// 	0xf3, 0x5c, 0x26, 0x73, 0x05, 0x05, 0xa2, 0xee, 0xbc, 0x09,
1715  	// 	0x38, 0x34, 0x3a}
1716  	// pubkeyY = []byte{0xb7, 0xc6, 0x7d, 0xb2, 0xe1, 0xff, 0xc8, 0x43, 0x1f,
1717  	// 	0x63, 0x32, 0x62, 0xaa, 0x60, 0xc6, 0x83, 0x30, 0xbd, 0x24,
1718  	// 	0x7e, 0xef, 0xdb, 0x6f, 0x2e, 0x8d, 0x56, 0xf0, 0x3c, 0x9f,
1719  	// 	0x6d, 0xb6, 0xf8}
1720  	uncompressedPkScript = []byte{
1721  		0x76, 0xa9, 0x14, 0xd1, 0x7c, 0xb5,
1722  		0xeb, 0xa4, 0x02, 0xcb, 0x68, 0xe0, 0x69, 0x56, 0xbf, 0x32,
1723  		0x53, 0x90, 0x0e, 0x0a, 0x86, 0xc9, 0xfa, 0x88, 0xac,
1724  	}
1725  	compressedPkScript = []byte{
1726  		0x76, 0xa9, 0x14, 0x27, 0x4d, 0x9f, 0x7f,
1727  		0x61, 0x7e, 0x7c, 0x7a, 0x1c, 0x1f, 0xb2, 0x75, 0x79, 0x10,
1728  		0x43, 0x65, 0x68, 0x27, 0x9d, 0x86, 0x88, 0xac,
1729  	}
1730  	shortPkScript = []byte{
1731  		0x76, 0xa9, 0x14, 0xd1, 0x7c, 0xb5,
1732  		0xeb, 0xa4, 0x02, 0xcb, 0x68, 0xe0, 0x69, 0x56, 0xbf, 0x32,
1733  		0x53, 0x90, 0x0e, 0x0a, 0x88, 0xac,
1734  	}
1735  	// uncompressedAddrStr = "1L6fd93zGmtzkK6CsZFVVoCwzZV3MUtJ4F"
1736  	// compressedAddrStr   = "14apLppt9zTq6cNw8SDfiJhk9PhkZrQtYZ"
1737  )
1738  
1739  // Pretend output amounts.
1740  const coinbaseVal = 2500000000
1741  const fee = 5000000
1742  
1743  var sigScriptTests = []tstSigScript{
1744  	{
1745  		name: "one input uncompressed",
1746  		inputs: []tstInput{
1747  			{
1748  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1749  				sigscriptGenerates: true,
1750  				inputValidates:     true,
1751  				indexOutOfRange:    false,
1752  			},
1753  		},
1754  		hashType:           SigHashAll,
1755  		compress:           false,
1756  		scriptAtWrongIndex: false,
1757  	},
1758  	{
1759  		name: "two inputs uncompressed",
1760  		inputs: []tstInput{
1761  			{
1762  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1763  				sigscriptGenerates: true,
1764  				inputValidates:     true,
1765  				indexOutOfRange:    false,
1766  			},
1767  			{
1768  				txout:              wire.NewTxOut(coinbaseVal+fee, uncompressedPkScript),
1769  				sigscriptGenerates: true,
1770  				inputValidates:     true,
1771  				indexOutOfRange:    false,
1772  			},
1773  		},
1774  		hashType:           SigHashAll,
1775  		compress:           false,
1776  		scriptAtWrongIndex: false,
1777  	},
1778  	{
1779  		name: "one input compressed",
1780  		inputs: []tstInput{
1781  			{
1782  				txout:              wire.NewTxOut(coinbaseVal, compressedPkScript),
1783  				sigscriptGenerates: true,
1784  				inputValidates:     true,
1785  				indexOutOfRange:    false,
1786  			},
1787  		},
1788  		hashType:           SigHashAll,
1789  		compress:           true,
1790  		scriptAtWrongIndex: false,
1791  	},
1792  	{
1793  		name: "two inputs compressed",
1794  		inputs: []tstInput{
1795  			{
1796  				txout:              wire.NewTxOut(coinbaseVal, compressedPkScript),
1797  				sigscriptGenerates: true,
1798  				inputValidates:     true,
1799  				indexOutOfRange:    false,
1800  			},
1801  			{
1802  				txout:              wire.NewTxOut(coinbaseVal+fee, compressedPkScript),
1803  				sigscriptGenerates: true,
1804  				inputValidates:     true,
1805  				indexOutOfRange:    false,
1806  			},
1807  		},
1808  		hashType:           SigHashAll,
1809  		compress:           true,
1810  		scriptAtWrongIndex: false,
1811  	},
1812  	{
1813  		name: "hashType SigHashNone",
1814  		inputs: []tstInput{
1815  			{
1816  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1817  				sigscriptGenerates: true,
1818  				inputValidates:     true,
1819  				indexOutOfRange:    false,
1820  			},
1821  		},
1822  		hashType:           SigHashNone,
1823  		compress:           false,
1824  		scriptAtWrongIndex: false,
1825  	},
1826  	{
1827  		name: "hashType SigHashSingle",
1828  		inputs: []tstInput{
1829  			{
1830  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1831  				sigscriptGenerates: true,
1832  				inputValidates:     true,
1833  				indexOutOfRange:    false,
1834  			},
1835  		},
1836  		hashType:           SigHashSingle,
1837  		compress:           false,
1838  		scriptAtWrongIndex: false,
1839  	},
1840  	{
1841  		name: "hashType SigHashAnyoneCanPay",
1842  		inputs: []tstInput{
1843  			{
1844  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1845  				sigscriptGenerates: true,
1846  				inputValidates:     true,
1847  				indexOutOfRange:    false,
1848  			},
1849  		},
1850  		hashType:           SigHashAnyOneCanPay,
1851  		compress:           false,
1852  		scriptAtWrongIndex: false,
1853  	},
1854  	{
1855  		name: "hashType non-standard",
1856  		inputs: []tstInput{
1857  			{
1858  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1859  				sigscriptGenerates: true,
1860  				inputValidates:     true,
1861  				indexOutOfRange:    false,
1862  			},
1863  		},
1864  		hashType:           0x04,
1865  		compress:           false,
1866  		scriptAtWrongIndex: false,
1867  	},
1868  	{
1869  		name: "invalid compression",
1870  		inputs: []tstInput{
1871  			{
1872  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1873  				sigscriptGenerates: true,
1874  				inputValidates:     false,
1875  				indexOutOfRange:    false,
1876  			},
1877  		},
1878  		hashType:           SigHashAll,
1879  		compress:           true,
1880  		scriptAtWrongIndex: false,
1881  	},
1882  	{
1883  		name: "short PkScript",
1884  		inputs: []tstInput{
1885  			{
1886  				txout:              wire.NewTxOut(coinbaseVal, shortPkScript),
1887  				sigscriptGenerates: false,
1888  				indexOutOfRange:    false,
1889  			},
1890  		},
1891  		hashType:           SigHashAll,
1892  		compress:           false,
1893  		scriptAtWrongIndex: false,
1894  	},
1895  	{
1896  		name: "valid script at wrong index",
1897  		inputs: []tstInput{
1898  			{
1899  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1900  				sigscriptGenerates: true,
1901  				inputValidates:     true,
1902  				indexOutOfRange:    false,
1903  			},
1904  			{
1905  				txout:              wire.NewTxOut(coinbaseVal+fee, uncompressedPkScript),
1906  				sigscriptGenerates: true,
1907  				inputValidates:     true,
1908  				indexOutOfRange:    false,
1909  			},
1910  		},
1911  		hashType:           SigHashAll,
1912  		compress:           false,
1913  		scriptAtWrongIndex: true,
1914  	},
1915  	{
1916  		name: "index out of range",
1917  		inputs: []tstInput{
1918  			{
1919  				txout:              wire.NewTxOut(coinbaseVal, uncompressedPkScript),
1920  				sigscriptGenerates: true,
1921  				inputValidates:     true,
1922  				indexOutOfRange:    false,
1923  			},
1924  			{
1925  				txout:              wire.NewTxOut(coinbaseVal+fee, uncompressedPkScript),
1926  				sigscriptGenerates: true,
1927  				inputValidates:     true,
1928  				indexOutOfRange:    false,
1929  			},
1930  		},
1931  		hashType:           SigHashAll,
1932  		compress:           false,
1933  		scriptAtWrongIndex: true,
1934  	},
1935  }
1936  
1937  // Test the sigscript generation for valid and invalid inputs, all hashTypes, and with and without compression.  This test creates sigscripts to spend fake coinbase inputs, as sigscripts cannot be created for the MsgTxs in txTests, since they come from the blockchain and we don't have the private keys.
1938  func TestSignatureScript(t *testing.T) {
1939  	t.Parallel()
1940  	privKey, _ := ec.PrivKeyFromBytes(ec.S256(), privKeyD)
1941  nexttest:
1942  	for i := range sigScriptTests {
1943  		tx := wire.NewMsgTx(wire.TxVersion)
1944  		output := wire.NewTxOut(500, []byte{OP_RETURN})
1945  		tx.AddTxOut(output)
1946  		for range sigScriptTests[i].inputs {
1947  			txin := wire.NewTxIn(coinbaseOutPoint, nil, nil)
1948  			tx.AddTxIn(txin)
1949  		}
1950  		var script []byte
1951  		var e error
1952  		for j := range tx.TxIn {
1953  			var idx int
1954  			if sigScriptTests[i].inputs[j].indexOutOfRange {
1955  				t.Errorf("at test %v", sigScriptTests[i].name)
1956  				idx = len(sigScriptTests[i].inputs)
1957  			} else {
1958  				idx = j
1959  			}
1960  			script, e = SignatureScript(
1961  				tx, idx,
1962  				sigScriptTests[i].inputs[j].txout.PkScript,
1963  				sigScriptTests[i].hashType, privKey,
1964  				sigScriptTests[i].compress,
1965  			)
1966  			if (e == nil) != sigScriptTests[i].inputs[j].sigscriptGenerates {
1967  				if e == nil {
1968  					t.Errorf(
1969  						"passed test '%v' incorrectly",
1970  						sigScriptTests[i].name,
1971  					)
1972  				} else {
1973  					t.Errorf(
1974  						"failed test '%v': %v",
1975  						sigScriptTests[i].name, e,
1976  					)
1977  				}
1978  				continue nexttest
1979  			}
1980  			if !sigScriptTests[i].inputs[j].sigscriptGenerates {
1981  				// done with this test
1982  				continue nexttest
1983  			}
1984  			tx.TxIn[j].SignatureScript = script
1985  		}
1986  		// If testing using a correct sigscript but for an incorrect index, use last input script for first input.  Requires > 0 inputs for test.
1987  		if sigScriptTests[i].scriptAtWrongIndex {
1988  			tx.TxIn[0].SignatureScript = script
1989  			sigScriptTests[i].inputs[0].inputValidates = false
1990  		}
1991  		// Validate tx input scripts
1992  		scriptFlags := ScriptBip16 | ScriptVerifyDERSignatures
1993  		for j := range tx.TxIn {
1994  			vm, e := NewEngine(
1995  				sigScriptTests[i].
1996  					inputs[j].txout.PkScript, tx, j, scriptFlags, nil, nil, 0,
1997  			)
1998  			if e != nil {
1999  				t.Errorf(
2000  					"cannot create script vm for test %v: %v",
2001  					sigScriptTests[i].name, e,
2002  				)
2003  				continue nexttest
2004  			}
2005  			e = vm.Execute()
2006  			if (e == nil) != sigScriptTests[i].inputs[j].inputValidates {
2007  				if e == nil {
2008  					t.Errorf(
2009  						"passed test '%v' validation incorrectly: %v",
2010  						sigScriptTests[i].name, e,
2011  					)
2012  				} else {
2013  					t.Errorf(
2014  						"failed test '%v' validation: %v",
2015  						sigScriptTests[i].name, e,
2016  					)
2017  				}
2018  				continue nexttest
2019  			}
2020  		}
2021  	}
2022  }
2023