com.btchip.BitcoinTransaction Java Examples

The following examples show how to use com.btchip.BitcoinTransaction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX1ContactlessSticky() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	// Keycard validation is done while still in the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	byte[] signature = dongle.untrustedHashSign("44'/0'/0'/0/0", pin);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 1, 1 + signature.length);		
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #2
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX1ContactlessNoPIN() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	try {
		dongle.startUntrustedTransaction(
			true, 
			0, 
			new BTChipDongle.BTChipInput[] { input1 }, 
			txin_1.getOutputs().get(1).getScript());
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
	}		
}
 
Example #3
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX1ContactlessUntrustedInput() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	byte[] prevout = Arrays.copyOfRange(input1.getValue(), 4, 4 + 36);
	input1 = dongle.createInput(prevout, false);
	try {
		dongle.startUntrustedTransaction(
			true, 
			0, 
			new BTChipDongle.BTChipInput[] { input1 }, 
			txin_1.getOutputs().get(1).getScript());
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}		
}
 
Example #4
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX2ContactlessNoChange() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	try {
		dongle.finalizeInputFull(txout_1.serializeOutputs());		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}				
}
 
Example #5
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX2ContactlessWrongChange() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	try {
		dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/1");		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}				
}
 
Example #6
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX3Contactless() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_3));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_3));		
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 0);
	byte[] prevout = Arrays.copyOfRange(input1.getValue(), 4, 4 + 36);
	input1 = dongle.createInput(prevout, false);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		TXIN_3_REDEEM_SCRIPT);
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.NONE);		
	byte[] signature = dongle.untrustedHashSign("45'/2147483647/0/0", new byte[0]);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 2, 2 + signature.length);				
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #7
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnect() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	byte[] signature = null;
	try {
		dongle.untrustedHashSign("44'/0'/0'/0/0", pin);
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_CONDITIONS_NOT_SATISFIED);
	}
}
 
Example #8
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnectReconnect() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	dongle.finalizeInputFull(txout_1.serializeOutputs());		
	byte[] signature = dongle.untrustedHashSign("44'/0'/0'/0/0", pin);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 1, 1 + signature.length);		
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #9
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnectReconnectFakeOutputAmount() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	byte[] fullOutput = txout_1.serializeOutputs();
	fullOutput[4]++;
	try {
		dongle.finalizeInputFull(fullOutput);		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}		
}
 
Example #10
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnectReconnectFakeOutputDestination() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	byte[] fullOutput = txout_1.serializeOutputs();
	fullOutput[fullOutput.length - 5] ^= (byte)0x42;
	try {
		dongle.finalizeInputFull(fullOutput);		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}		
}
 
Example #11
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX2ContactlessSticky() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	// Keycard validation is done while still in the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_2_ADDRESS, keycardIndexes);
	byte[] signature = dongle.untrustedHashSign("44'/0'/0'/0/1", pin);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 1, 1 + signature.length);				
	assertTrue(Arrays.equals(signature, originalSignature));
	// Process second input
	dongle.startUntrustedTransaction(
		false, 
		1, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_2.getOutputs().get(0).getScript());
	dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");
	signature = dongle.untrustedHashSign("44'/0'/0'/0/1", pin);
	signature = canonicalizeSignature(signature);
	originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(1).getScript(), 1, 1 + signature.length);				
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #12
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX2ContactlessDisconnect() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	reset();
	// Card is removed from the field		
	byte[] pin = keycardHelper.getPIN(TXOUT_2_ADDRESS, keycardIndexes);
	try {
		dongle.untrustedHashSign("44'/0'/0'/0/1", pin);
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_CONDITIONS_NOT_SATISFIED);
	}
}
 
Example #13
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX2ContactlessDisconnectReconnectSwap() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	// Keycard validation is done while still in the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	reset();
	// Card is removed from the field		
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input2, input1 }, 
		txin_2.getOutputs().get(0).getScript());
	try {
		dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");						
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}				
}
 
Example #14
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testTX2ContactlessDisconnectReconnect() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	// Keycard validation is done while still in the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	reset();
	// Card is removed from the field		
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");						
	byte[] pin = keycardHelper.getPIN(TXOUT_2_ADDRESS, keycardIndexes);
	byte[] signature = dongle.untrustedHashSign("44'/0'/0'/0/1", pin);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 1, 1 + signature.length);				
	assertTrue(Arrays.equals(signature, originalSignature));
	// Process second input
	dongle.startUntrustedTransaction(
		false, 
		1, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_2.getOutputs().get(0).getScript());
	dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/0");
	signature = dongle.untrustedHashSign("44'/0'/0'/0/1", pin);
	signature = canonicalizeSignature(signature);
	originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(1).getScript(), 1, 1 + signature.length);				
	assertTrue(Arrays.equals(signature, originalSignature));
}