Skip to content
interpreter.rs 64.9 KiB
Newer Older
		let checker = TransactionSignatureChecker {
			signer: signer,
			input_index: 1,
			input_amount: 0,
		};
		let input: Script = "00483045022015BD0139BCCCF990A6AF6EC5C1C52ED8222E03A0D51C334DF139968525D2FCD20221009F9EFE325476EB64C3958E4713E9EEFE49BF1D820ED58D2112721B134E2A1A53034930460221008431BDFA72BC67F9D41FE72E94C88FB8F359FFA30B33C72C121C5A877D922E1002210089EF5FC22DD8BFC6BF9FFDB01A9862D27687D424D1FEFBAB9E9C7176844A187A014C9052483045022015BD0139BCCCF990A6AF6EC5C1C52ED8222E03A0D51C334DF139968525D2FCD20221009F9EFE325476EB64C3958E4713E9EEFE49BF1D820ED58D2112721B134E2A1A5303210378D430274F8C5EC1321338151E9F27F4C676A008BDF8638D07C0B6BE9AB35C71210378D430274F8C5EC1321338151E9F27F4C676A008BDF8638D07C0B6BE9AB35C7153AE".into();
		let output: Script = "A914D8DACDADB7462AE15CD906F1878706D0DA8660E687".into();

		let flags = VerificationFlags::default()
			.verify_p2sh(true);
		assert_eq!(verify_script(&input, &output, &flags, &checker, SignatureVersion::Base), Ok(()));

	#[test]
	fn test_script_with_forkid_signature() {
		use keys::{KeyPair, Private, Network};
		use sign::UnsignedTransactionInput;
		use chain::{OutPoint, TransactionOutput};

		let key_pair = KeyPair::from_private(Private { network: Network::Mainnet, secret: 1.into(), compressed: false, }).unwrap();
		let redeem_script = Builder::default()
			.push_data(key_pair.public())
			.push_opcode(Opcode::OP_CHECKSIG)
			.into_script();


		let amount = 12345000000000;
		let sighashtype = 0x41; // All + ForkId
		let checker = TransactionSignatureChecker {
			input_index: 0,
			input_amount: amount,
			signer: TransactionInputSigner {
				version: 1,
				inputs: vec![
					UnsignedTransactionInput {
						previous_output: OutPoint {
							hash: 0u8.into(),
							index: 0xffffffff,
						},
						sequence: 0xffffffff,
					},
				],
				outputs: vec![
					TransactionOutput {
						value: amount,
						script_pubkey: redeem_script.to_bytes(),
					},
				],
				lock_time: 0,
			},
		};

		let script_pubkey = redeem_script;
		let flags = VerificationFlags::default();

		// valid signature
		{
			let signed_input = checker.signer.signed_input(&key_pair, 0, amount, &script_pubkey, SignatureVersion::ForkId, sighashtype);
			let script_sig = signed_input.script_sig.into();

			assert_eq!(verify_script(&script_sig, &script_pubkey, &flags, &checker, SignatureVersion::ForkId), Ok(()));
		}

		// signature with wrong amount
		{
			let signed_input = checker.signer.signed_input(&key_pair, 0, amount + 1, &script_pubkey, SignatureVersion::ForkId, sighashtype);
			let script_sig = signed_input.script_sig.into();

			assert_eq!(verify_script(&script_sig, &script_pubkey, &flags, &checker, SignatureVersion::ForkId), Err(Error::EvalFalse));
		}

		// fork-id signature passed when not expected
		{
			let signed_input = checker.signer.signed_input(&key_pair, 0, amount + 1, &script_pubkey, SignatureVersion::ForkId, sighashtype);
			let script_sig = signed_input.script_sig.into();

			assert_eq!(verify_script(&script_sig, &script_pubkey, &flags, &checker, SignatureVersion::Base), Err(Error::EvalFalse));
		}

		// non-fork-id signature passed when expected
		{
			let signed_input = checker.signer.signed_input(&key_pair, 0, amount + 1, &script_pubkey, SignatureVersion::Base, 1);
			let script_sig = signed_input.script_sig.into();

Svyatoslav Nikolsky's avatar
Svyatoslav Nikolsky committed
			assert_eq!(verify_script(&script_sig, &script_pubkey, &flags.verify_strictenc(true), &checker, SignatureVersion::ForkId), Err(Error::SignatureMustUseForkId));