Java Code Examples for com.btchip.utils.BufferUtils#writeUint32BE()

The following examples show how to use com.btchip.utils.BufferUtils#writeUint32BE() . 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: BTChipDongle.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public byte[] untrustedHashSign(final List<Integer> privateKeyPath, String pin, long lockTime, byte sigHashType) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	byte path[] = pathToByteArray(privateKeyPath);
	BufferUtils.writeBuffer(data, path);
	data.write(pin.length());
	BufferUtils.writeBuffer(data, pin.getBytes());
	BufferUtils.writeUint32BE(data, lockTime);
	data.write(sigHashType);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_SIGN, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	response[0] = (byte)0x30;
	return response;
}
 
Example 2
Source File: BTChipDongle.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public byte[] untrustedLiquidHashSign(final List<Integer> privateKeyPath, long lockTime, byte sigHashType) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	byte path[] = pathToByteArray(privateKeyPath);
	BufferUtils.writeBuffer(data, path);
	//data.write(pin.length());
	data.write((byte)0x00);
	//BufferUtils.writeBuffer(data, pin.getBytes());
	BufferUtils.writeUint32BE(data, lockTime);
	data.write(sigHashType);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_SIGN, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	response[0] = (byte)0x30;
	return response;
}
 
Example 3
Source File: BTChipDongle.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public String getGreenAddress(final boolean csv, final long subaccount, final long branch, final long pointer, final long csvBlocks) throws BTChipException {
	final ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeUint32BE(data, subaccount);
	BufferUtils.writeUint32BE(data, branch);
	BufferUtils.writeUint32BE(data, pointer);
	if (csv)
		BufferUtils.writeUint32BE(data, csvBlocks);

	final byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_LIQUID_GREEN_ADDRESS, (byte)0x01, csv ? (byte)0x00 : (byte)0x01, data.toByteArray(), OK_OR_NOT_SUPPORTED);
	return  BTChipDongle.readString(response, 0, response.length);
}
 
Example 4
Source File: BTChipDongle.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private byte[] pathToByteArray(final List<Integer> path) throws BTChipException {
	final int len = path.size();
	if (len == 0) {
		return new byte[] { 0 };
	}
	if (len > 10) {
		throw new BTChipException("Path too long");
	}
	ByteArrayOutputStream result = new ByteArrayOutputStream();
	result.write((byte)len);
	for (final Integer element : path) {
		BufferUtils.writeUint32BE(result, element);
	}
	return result.toByteArray();
}
 
Example 5
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public BTChipInput getTrustedInput(BitcoinTransaction transaction, long index, long sequence) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	// Header
	BufferUtils.writeUint32BE(data, index);
	BufferUtils.writeBuffer(data, transaction.getVersion());
	VarintUtils.write(data, transaction.getInputs().size());
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	// Each input
	for (BitcoinTransaction.BitcoinInput input : transaction.getInputs()) {
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, input.getPrevOut());
		VarintUtils.write(data, input.getScript().length);
		exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, input.getScript());			
		exchangeApduSplit2(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), input.getSequence(), OK);			
	}
	// Number of outputs
	data = new ByteArrayOutputStream();
	VarintUtils.write(data, transaction.getOutputs().size());
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
	// Each output
	for (BitcoinTransaction.BitcoinOutput output : transaction.getOutputs()) {
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, output.getAmount());
		VarintUtils.write(data, output.getScript().length);
		exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, output.getScript());
		exchangeApduSplit(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);						
	}
	// Locktime
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, transaction.getLockTime(), OK);
	ByteArrayOutputStream sequenceBuf = new ByteArrayOutputStream();
	BufferUtils.writeUint32LE(sequenceBuf, sequence);
	return new BTChipInput(response, sequenceBuf.toByteArray(), true, false);
}
 
Example 6
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public byte[] untrustedHashSign(String privateKeyPath, String pin, long lockTime, byte sigHashType) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	byte path[] = BIP32Utils.splitPath(privateKeyPath);
	BufferUtils.writeBuffer(data, path);
	data.write(pin.length());
	BufferUtils.writeBuffer(data, pin.getBytes());
	BufferUtils.writeUint32BE(data, lockTime);
	data.write(sigHashType);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_SIGN, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	response[0] = (byte)0x30;
	return response;
}
 
Example 7
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BTChipInput getTrustedInput(BitcoinTransaction transaction, long index, long sequence) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	// Header
	BufferUtils.writeUint32BE(data, index);
	BufferUtils.writeBuffer(data, transaction.getVersion());
	VarintUtils.write(data, transaction.getInputs().size());
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	// Each input
	for (BitcoinTransaction.BitcoinInput input : transaction.getInputs()) {
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, input.getPrevOut());
		VarintUtils.write(data, input.getScript().length);
		exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, input.getScript());			
		exchangeApduSplit2(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), input.getSequence(), OK);			
	}
	// Number of outputs
	data = new ByteArrayOutputStream();
	VarintUtils.write(data, transaction.getOutputs().size());
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
	// Each output
	for (BitcoinTransaction.BitcoinOutput output : transaction.getOutputs()) {
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, output.getAmount());
		VarintUtils.write(data, output.getScript().length);
		exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, output.getScript());
		exchangeApduSplit(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.toByteArray(), OK);						
	}
	// Locktime
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, transaction.getLockTime(), OK);
	ByteArrayOutputStream sequenceBuf = new ByteArrayOutputStream();
	BufferUtils.writeUint32LE(sequenceBuf, sequence);
	return new BTChipInput(response, sequenceBuf.toByteArray(), true);
}
 
Example 8
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public byte[] untrustedHashSign(String privateKeyPath, String pin, long lockTime, byte sigHashType) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	byte path[] = BIP32Utils.splitPath(privateKeyPath);
	BufferUtils.writeBuffer(data, path);
	data.write(pin.length());
	BufferUtils.writeBuffer(data, pin.getBytes());
	BufferUtils.writeUint32BE(data, lockTime);
	data.write(sigHashType);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_SIGN, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	response[0] = (byte)0x30;
	return response;
}