com.btchip.BTChipDongle Java Examples

The following examples show how to use com.btchip.BTChipDongle. 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: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public List<String> getXpubs(final GaActivity parent, final List<List<Integer>> paths) {
    final List<String> xpubs = new ArrayList<>(paths.size());
    try {
        for (final List<Integer> path : paths) {
            final String key = Joiner.on("/").join(path);
            if (!mUserXPubs.containsKey(key)) {
                final BTChipDongle.BTChipPublicKey pubKey = mDongle.getWalletPublicKey(path);
                final byte[] compressed = KeyUtils.compressPublicKey(pubKey.getPublicKey());
                final Object hdkey = Wally.bip32_key_init(mNetwork.getVerPublic(), 1 /*FIXME: wally bug*/, 0,
                                                          pubKey.getChainCode(), compressed,null, null, null);
                mUserXPubs.put(key, Wally.bip32_key_to_base58(hdkey, Wally.BIP32_FLAG_KEY_PUBLIC));
                Wally.bip32_key_free(hdkey);
            }
            xpubs.add(mUserXPubs.get(key));
        }
        return xpubs;
    } catch (final Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
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 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 #3
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 #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 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 #6
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 #7
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 #8
Source File: TestGetWalletPublicKey.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testGetWalletPublicKey() throws BTChipException {
	BTChipDongle dongle = prepareDongleRestoreTestnet(false);
	dongle.verifyPin(DEFAULT_PIN);
	BTChipDongle.BTChipPublicKey publicKey = dongle.getWalletPublicKey("44'/0'/0'/0/42");
	assertEquals(publicKey.getAddress(), EXPECTED_ADDRESS_1);
	assertTrue(Arrays.equals(publicKey.getPublicKey(), EXPECTED_PUBLIC_KEY_1));
	assertTrue(Arrays.equals(publicKey.getChainCode(), EXPECTED_CHAINCODE_1));	
}
 
Example #9
Source File: TestSignMessage.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSignMessage() throws BTChipException {
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	dongle.verifyPin(DEFAULT_PIN);
	dongle.signMessagePrepare("13'/0'/0'/0/42", MSG.getBytes());
	BTChipDongle.BTChipSignature signature = dongle.signMessageSign(null);		
	assertTrue(Arrays.equals(canonicalizeSignature(signature.getSignature()), EXPECTED_SIGNATURE));
}
 
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 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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: TestSetup.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSetupRestoreTestnet() throws BTChipException {
	BTChipDongle dongle = prepareDongleRestoreTestnet(false);
	dongle.verifyPin(DEFAULT_PIN);
	BTChipDongle.BTChipPublicKey publicKey = dongle.getWalletPublicKey("");
	assertEquals(publicKey.getAddress(), EXPECTED_ADDRESS_1);
	assertTrue(Arrays.equals(publicKey.getPublicKey(), EXPECTED_PUBLIC_KEY_1));
	assertTrue(Arrays.equals(publicKey.getChainCode(), EXPECTED_CHAINCODE_1));	
}
 
Example #18
Source File: TestSetUserKeycard.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSetUserKeycard() throws BTChipException {
	byte[] newKeycard = new byte[16];
	for (byte i=0; i<16; i++) {
		newKeycard[i] = (byte)(i + (byte)0x20);
	}
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	KeycardHelper keycardHelperNew = new KeycardHelper(newKeycard);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	dongle.verifyPin(DEFAULT_PIN);
	byte[] challenge = dongle.setUserKeycard(DEFAULT_KEYCARD_ADDRESS_SIZE, newKeycard);
	dongle.confirmUserKeycard(keycardHelper.getPIN(challenge));
	challenge = dongle.setUserKeycard(DEFAULT_KEYCARD_ADDRESS_SIZE, DEFAULT_KEYCARD);
	dongle.confirmUserKeycard(keycardHelperNew.getPIN(challenge));
}
 
Example #19
Source File: AbstractTest.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
protected BTChipDongle prepareDongleRestoreTestnet(boolean debug) throws BTChipException {
	BTChipDongle dongle = getDongle(debug);
	dongle.setup(
		new BTChipDongle.OperationMode[] { BTChipDongle.OperationMode.WALLET },
		new BTChipDongle.Feature[] { BTChipDongle.Feature.RFC6979, BTChipDongle.Feature.NO_2FA_P2SH},
		TESTNET_VERSION,
		TESTNET_P2SH_VERSION,
		DEFAULT_PIN,
		null,
		BTChipConstants.QWERTY_KEYMAP,
		DEFAULT_SEED,
		null);
	return dongle;
}
 
Example #20
Source File: AbstractTest.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
protected BTChipDongle getDongle(boolean debug) throws BTChipException {
	this.simulator = prepareSimulator();
	assertTrue(simulator.selectApplet(INSTANCE_AID));		
	JCardSIMTransport transport = new JCardSIMTransport(simulator, debug);
	BTChipDongle dongle = new BTChipDongle(transport);
	dongle.setKeycardSeed(DEFAULT_KEYCARD_ADDRESS_SIZE, DEFAULT_KEYCARD);		
	return dongle;
}
 
Example #21
Source File: BTChipHWWallet.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected ECKey.ECDSASignature signMessage(final String message) {
    try {
        mDongle.signMessagePrepare(getPath(), message.getBytes());
        final BTChipDongle.BTChipSignature sig = mDongle.signMessageSign(new byte[]{0});
        return ECKey.ECDSASignature.decodeFromDER(sig.getSignature());
    } catch (final BTChipException e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #22
Source File: BTChipHWWallet.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private DeterministicKey internalGetPubKey() throws BTChipException {
    if (mCachedPubkey == null) {
        final BTChipDongle.BTChipPublicKey walletKey = mDongle.getWalletPublicKey(getPath());
        final byte[] compressedPubKey = KeyUtils.compressPublicKey(walletKey.getPublicKey());
        mCachedPubkey = HDKey.createMasterKey(walletKey.getChainCode(), compressedPubKey);
    }
    return mCachedPubkey;
}
 
Example #23
Source File: RequestLoginActivity.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void onLedgerConnected(final BTChipDongle dongle, final String pin) {
    final SettableFuture<Integer> pinCB = SettableFuture.create();

    final boolean havePin = !TextUtils.isEmpty(pin);
    Log.d(TAG, "Creating Ledger HW wallet" + (havePin ? " with PIN" : ""));
    final HWDeviceData hwDeviceData = new HWDeviceData("Ledger", false, true,
                                                       HWDeviceData.HWDeviceDataLiquidSupport.Lite);
    mHwWallet = new BTChipHWWallet(dongle, havePin ? pin : null, pinCB, networkData, hwDeviceData);

    doLogin(this);
}
 
Example #24
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String getBlindingNonce(GaActivity parent, String pubkey, String scriptHex) {
    try {
        final byte[] fullPk = Wally.ec_public_key_decompress(Wally.hex_to_bytes(pubkey), null);
        final BTChipDongle.BTChipPublicKey nonce = mDongle.getBlindingNonce(fullPk, Wally.hex_to_bytes(scriptHex));
        return Wally.hex_from_bytes(nonce.getPublicKey());
    } catch (final Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #25
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String getBlindingKey(final GaActivity parent, final String scriptHex) {
    try {
        final BTChipDongle.BTChipPublicKey blindingKey = mDongle.getBlindingKey(Wally.hex_to_bytes(scriptHex));
        final byte[] compressed = KeyUtils.compressPublicKey(blindingKey.getPublicKey());

        return Wally.hex_from_bytes(compressed);
    } catch (final Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #26
Source File: AbstractTest.java    From ledger-javacard with GNU Affero General Public License v3.0 4 votes vote down vote up
protected BTChipDongle getDongle() throws BTChipException {
	return getDongle(false);
}
 
Example #27
Source File: RequestLoginActivity.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private void setupLedgerConnection() {
    showInstructions(R.string.logging_in);
    final String pin = mPin;
    mPin = null;

    final BTChipTransport transport;
    if (mUsb != null) {
        transport = BTChipTransportAndroid.open(mUsbManager, mUsb);
        if (transport == null) {
            showInstructions(R.string.hw_wallet_reconnect);
            return;
        }
    } else if ((transport = getTransport(mTag)) == null) {
        showInstructions(R.string.hw_wallet_headline);

        // Prompt the user to tap
        runOnUiThread(new Runnable() {
            public void run() {
                mNfcWaitDialog = new MaterialDialog.Builder(RequestLoginActivity.this)
                    .title(R.string.btchip).content(R.string.please_tap_card).build();
                mNfcWaitDialog.show();
            }
        });
        return;
    }

    transport.setDebug(BuildConfig.DEBUG);
    try {
        final BTChipFirmware fw = (new BTChipDongle(transport, true)).getFirmwareVersion();
        final int major = fw.getMajor(), minor = fw.getMinor(), patch = fw.getPatch();

        Log.d(TAG, "BTChip/Ledger firmware version " + fw.toString() + '(' +
                major + '.' + minor + '.' + patch + ')');

        boolean isFirmwareOutdated = false;
        if (mVendorId == VENDOR_BTCHIP) {
            isFirmwareOutdated = major < 0x2001 ||
                (major == 0x2001 && minor < 0) || // Just for consistency in checking code
                (major == 0x2001 && minor == 0 && patch < 4);
        } else if (mVendorId == VENDOR_LEDGER) {
            isFirmwareOutdated = major < 0x3001 ||
                (major == 0x3001 && minor < 2) ||
                (major == 0x3001 && minor == 2 && patch < 5);
        }

        if (!isFirmwareOutdated) {
            onLedgerConnected(transport, pin);
            return;
        }

        showFirmwareOutdated(R.string.ledger_firmware_outdated,
                             new Runnable() { public void run() { onLedgerConnected(transport, pin); } });
    } catch (final BTChipException e) {
        if (e.getSW() != BTChipConstants.SW_INS_NOT_SUPPORTED)
            e.printStackTrace();
        // We are in dashboard mode, prompt the user to open the btcoin app.
        showInstructions(R.string.ledger_open_bitcoin_app);
    }
}
 
Example #28
Source File: BTChipHWWallet.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public BTChipDongle getDongle() {
    return mDongle;
}
 
Example #29
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));
}
 
Example #30
Source File: BTChipHWWallet.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private BTChipHWWallet(final BTChipTransport transport, final String pin, final Network network) {
    mDongle = new BTChipDongle(transport);
    mPin = pin;
    mAddrn = new LinkedList<>();
    mNetwork = network;
}