Skip to content
interpreter.rs 66 KiB
Newer Older
Marek Kotewicz's avatar
Marek Kotewicz committed
	}

	#[test]
	fn test_skipping_sequencetimeverify() {
		let script = Builder::default()
			.push_opcode(Opcode::OP_1)
			.push_opcode(Opcode::OP_NOP1)
			.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
			.push_opcode(Opcode::OP_CHECKSEQUENCEVERIFY)
			.push_opcode(Opcode::OP_NOP4)
			.push_opcode(Opcode::OP_NOP5)
			.push_opcode(Opcode::OP_NOP6)
			.push_opcode(Opcode::OP_NOP7)
			.push_opcode(Opcode::OP_NOP8)
			.push_opcode(Opcode::OP_NOP9)
			.push_opcode(Opcode::OP_NOP10)
			.push_opcode(Opcode::OP_1)
			.push_opcode(Opcode::OP_EQUAL)
			.into_script();
		let result = Ok(true);
		basic_test(&script, result, vec![vec![1].into()].into());
	}

	// https://webbtc.com/tx/5df1375ffe61ac35ca178ebb0cab9ea26dedbd0e96005dfcee7e379fa513232f
	#[test]
	fn test_transaction_find_and_delete() {
		let tx: Transaction = "0100000002f9cbafc519425637ba4227f8d0a0b7160b4e65168193d5af39747891de98b5b5000000006b4830450221008dd619c563e527c47d9bd53534a770b102e40faa87f61433580e04e271ef2f960220029886434e18122b53d5decd25f1f4acb2480659fea20aabd856987ba3c3907e0121022b78b756e2258af13779c1a1f37ea6800259716ca4b7f0b87610e0bf3ab52a01ffffffff42e7988254800876b69f24676b3e0205b77be476512ca4d970707dd5c60598ab00000000fd260100483045022015bd0139bcccf990a6af6ec5c1c52ed8222e03a0d51c334df139968525d2fcd20221009f9efe325476eb64c3958e4713e9eefe49bf1d820ed58d2112721b134e2a1a53034930460221008431bdfa72bc67f9d41fe72e94c88fb8f359ffa30b33c72c121c5a877d922e1002210089ef5fc22dd8bfc6bf9ffdb01a9862d27687d424d1fefbab9e9c7176844a187a014c9052483045022015bd0139bcccf990a6af6ec5c1c52ed8222e03a0d51c334df139968525d2fcd20221009f9efe325476eb64c3958e4713e9eefe49bf1d820ed58d2112721b134e2a1a5303210378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71210378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c7153aeffffffff01a08601000000000017a914d8dacdadb7462ae15cd906f1878706d0da8660e68700000000".into();
		let signer: TransactionInputSigner = tx.into();
		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, &ScriptWitness, &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, &ScriptWitness, &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, &ScriptWitness, &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, &ScriptWitness, &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();

			assert_eq!(verify_script(&script_sig, &script_pubkey, &ScriptWitness, &flags.verify_strictenc(true), &checker, SignatureVersion::ForkId), Err(Error::SignatureMustUseForkId));