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

The following examples show how to use com.btchip.utils.BufferUtils#writeBuffer() . 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: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
public byte[] serialize(boolean skipOutputLockTime) throws BTChipException {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(output, version);
	VarintUtils.write(output, inputs.size());
	for (BitcoinInput input : inputs) {
		input.serialize(output);
	}
	if (!skipOutputLockTime) {
		VarintUtils.write(output, outputs.size());
		for (BitcoinOutput outputItem : outputs) {
			outputItem.serialize(output);
		}
		BufferUtils.writeBuffer(output, lockTime);
	}
	return output.toByteArray();
}
 
Example 2
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void startUntrustedTransaction(boolean newTransaction, long inputIndex, BTChipInput usedInputList[], byte[] redeemScript, boolean segwit) throws BTChipException {
	// Start building a fake transaction with the passed inputs
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(data, BitcoinTransaction.DEFAULT_VERSION);
	VarintUtils.write(data, usedInputList.length);
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_INPUT_START, (byte)0x00, (newTransaction ? (segwit ? (byte)0x02 : (byte)0x00) : (byte)0x80), data.toByteArray(), OK);
	// Loop for each input
	long currentIndex = 0;
	for (BTChipInput input : usedInputList) {
		byte[] script = (currentIndex == inputIndex ? redeemScript : new byte[0]);
		data = new ByteArrayOutputStream();
		data.write(segwit ? 0x02 : (input.isTrusted() ? (byte)0x01 : (byte)0x00));
		if (input.isTrusted()) {
			// untrusted inputs have constant length
			data.write(input.getValue().length);
		}
		BufferUtils.writeBuffer(data, input.getValue());
		VarintUtils.write(data, script.length);
		exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_INPUT_START, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
		data = new ByteArrayOutputStream();
		BufferUtils.writeBuffer(data, script);
		BufferUtils.writeBuffer(data, input.getSequence());
		exchangeApduSplit(BTCHIP_CLA, BTCHIP_INS_HASH_INPUT_START, (byte)0x80, (byte)0x00, data.toByteArray(), OK);
		currentIndex++;			
	}				
}
 
Example 3
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BTChipOutput finalizeInput(String outputAddress, String amount, String fees, String changePath) throws BTChipException {
	BTChipOutput result;
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	byte path[] = BIP32Utils.splitPath(changePath);
	data.write(outputAddress.length());
	BufferUtils.writeBuffer(data, outputAddress.getBytes());
	BufferUtils.writeUint64BE(data, CoinFormatUtils.toSatoshi(amount));
	BufferUtils.writeUint64BE(data, CoinFormatUtils.toSatoshi(fees));
	BufferUtils.writeBuffer(data, path);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_INPUT_FINALIZE, (byte)0x02, (byte)0x00, data.toByteArray(), OK);
	result = convertResponseToOutput(response);
	return result;
}
 
Example 4
Source File: BTChipDongle.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public BTChipOutput finalizeInput(String outputAddress, String amount, String fees, final List<Integer> changePath) throws BTChipException {
	BTChipOutput result;
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	byte path[] = pathToByteArray(changePath);
	data.write(outputAddress.length());
	BufferUtils.writeBuffer(data, outputAddress.getBytes());
	BufferUtils.writeUint64BE(data, CoinFormatUtils.toSatoshi(amount));
	BufferUtils.writeUint64BE(data, CoinFormatUtils.toSatoshi(fees));
	BufferUtils.writeBuffer(data, path);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_HASH_INPUT_FINALIZE, (byte)0x02, (byte)0x00, data.toByteArray(), OK);
	result = convertResponseToOutput(response);
	return result;
}
 
Example 5
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 6
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 7
Source File: BTChipDongle.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public boolean signMessagePrepare(final List<Integer> path, byte[] message) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(data, pathToByteArray(path));
	if (this.shouldUseNewSigningApi()) {
		// length has two bytes in Ledger 1.0.2+
		data.write((byte)0);
	}
	data.write((byte)message.length);
	BufferUtils.writeBuffer(data, message);
	final byte p2 = (byte) (this.shouldUseNewSigningApi() ? 0x01 : 0x00);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SIGN_MESSAGE, (byte)0x00, p2, data.toByteArray(), OK);
	return (response[0] == (byte)0x01);
}
 
Example 8
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public BTChipSignature signMessageSign(byte[] pin) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	if (pin == null) {
		data.write((byte)0);
	}
	else {
		data.write((byte)pin.length);
		BufferUtils.writeBuffer(data, pin);
	}
	final byte p2 = (byte) (this.shouldUseNewSigningApi() ? 0x01 : 0x00);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SIGN_MESSAGE, (byte)0x80, p2, data.toByteArray(), OK);
	int yParity = (response[0] & 0x0F);
	response[0] = (byte)0x30;
	return new BTChipSignature(response, yParity);
}
 
Example 9
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public boolean signMessagePrepare(String path, byte[] message) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(data, BIP32Utils.splitPath(path));
	if (this.shouldUseNewSigningApi()) {
		// length has two bytes in Ledger 1.0.2+
		data.write((byte)0);
	}
	data.write((byte)message.length);
	BufferUtils.writeBuffer(data, message);
	final byte p2 = (byte) (this.shouldUseNewSigningApi() ? 0x01 : 0x00);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SIGN_MESSAGE, (byte)0x00, p2, data.toByteArray(), OK);
	return (response[0] == (byte)0x01);
}
 
Example 10
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;
}
 
Example 11
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 12
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BTChipSignature signMessageSign(byte[] pin) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	if (pin == null) {
		data.write((byte)0);
	}
	else {
		data.write((byte)pin.length);
		BufferUtils.writeBuffer(data, pin);
	}
	final byte p2 = (byte) (this.shouldUseNewSigningApi() ? 0x01 : 0x00);
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SIGN_MESSAGE, (byte)0x80, p2, data.toByteArray(), OK);
	int yParity = (response[0] & 0x0F);
	response[0] = (byte)0x30;
	return new BTChipSignature(response, yParity);
}
 
Example 13
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 14
Source File: BitcoinTransaction.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public void serialize(ByteArrayOutputStream output) throws BTChipException {
	BufferUtils.writeBuffer(output, prevOut);
	VarintUtils.write(output, script.length);
	BufferUtils.writeBuffer(output, script);
	BufferUtils.writeBuffer(output, sequence);
}
 
Example 15
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public void setKeymapEncoding(byte[] keymapEncoding) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(data, keymapEncoding);
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SET_KEYMAP, (byte)0x00, (byte)0x00, data.toByteArray(), OK_OR_NOT_SUPPORTED);
}
 
Example 16
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public boolean setup(OperationMode supportedOperationModes[], Feature features[], int keyVersion, int keyVersionP2SH, byte[] userPin, byte[] wipePin, byte[] keymapEncoding, byte[] seed, byte[] developerKey) throws BTChipException {
	int operationModeFlags = 0;
	int featuresFlags = 0;
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	for (OperationMode currentOperationMode : supportedOperationModes) {
		operationModeFlags |= currentOperationMode.getValue();
	}
	for (Feature currentFeature : features) {
		featuresFlags |= currentFeature.getValue();
	}
	data.write(operationModeFlags);
	data.write(featuresFlags);
	data.write(keyVersion);
	data.write(keyVersionP2SH);
	if ((userPin.length < 0x04) || (userPin.length > 0x20)) {
		throw new BTChipException("Invalid user PIN length");
	}
	data.write(userPin.length);
	BufferUtils.writeBuffer(data,  userPin);
	if (wipePin != null) {
		if (wipePin.length > 0x04) {
			throw new BTChipException("Invalid wipe PIN length");	
		}
		data.write(wipePin.length);
		BufferUtils.writeBuffer(data,  wipePin);			
	}
	else {
		data.write(0);
	}
	if (seed != null) {
		if ((seed.length < 32) || (seed.length > 64)) {
			throw new BTChipException("Invalid seed length");
		}
		data.write(seed.length);
		BufferUtils.writeBuffer(data, seed);
	}
	else {
		data.write(0);
	}
	if (developerKey != null) {
		if (developerKey.length != 0x10) {
			throw new BTChipException("Invalid developer key");
		}
		data.write(developerKey.length);
		BufferUtils.writeBuffer(data, developerKey);
	}
	else {
		data.write(0);
	}
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SETUP, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	if (keymapEncoding != null) {
		setKeymapEncoding(keymapEncoding);
	}
	return (response[0] == (byte)0x01);
}
 
Example 17
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 4 votes vote down vote up
public boolean setup(OperationMode supportedOperationModes[], Feature features[], int keyVersion, int keyVersionP2SH, byte[] userPin, byte[] wipePin, byte[] keymapEncoding, byte[] seed, byte[] developerKey) throws BTChipException {
	int operationModeFlags = 0;
	int featuresFlags = 0;
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	for (OperationMode currentOperationMode : supportedOperationModes) {
		operationModeFlags |= currentOperationMode.getValue();
	}
	for (Feature currentFeature : features) {
		featuresFlags |= currentFeature.getValue();
	}
	data.write(operationModeFlags);
	data.write(featuresFlags);
	data.write(keyVersion);
	data.write(keyVersionP2SH);
	if ((userPin.length < 0x04) || (userPin.length > 0x20)) {
		throw new BTChipException("Invalid user PIN length");
	}
	data.write(userPin.length);
	BufferUtils.writeBuffer(data,  userPin);
	if (wipePin != null) {
		if (wipePin.length > 0x04) {
			throw new BTChipException("Invalid wipe PIN length");	
		}
		data.write(wipePin.length);
		BufferUtils.writeBuffer(data,  wipePin);			
	}
	else {
		data.write(0);
	}
	if (seed != null) {
		if ((seed.length < 32) || (seed.length > 64)) {
			throw new BTChipException("Invalid seed length");
		}
		data.write(seed.length);
		BufferUtils.writeBuffer(data, seed);
	}
	else {
		data.write(0);
	}
	if (developerKey != null) {
		if (developerKey.length != 0x10) {
			throw new BTChipException("Invalid developer key");
		}
		data.write(developerKey.length);
		BufferUtils.writeBuffer(data, developerKey);
	}
	else {
		data.write(0);
	}
	byte[] response = exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SETUP, (byte)0x00, (byte)0x00, data.toByteArray(), OK);
	if (keymapEncoding != null) {
		setKeymapEncoding(keymapEncoding);
	}
	return (response[0] == (byte)0x01);
}
 
Example 18
Source File: BTChipDongle.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public void setKeymapEncoding(byte[] keymapEncoding) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(data, keymapEncoding);
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SET_KEYMAP, (byte)0x00, (byte)0x00, data.toByteArray(), OK_OR_NOT_SUPPORTED);
}
 
Example 19
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void setKeymapEncoding(byte[] keymapEncoding) throws BTChipException {
	ByteArrayOutputStream data = new ByteArrayOutputStream();
	BufferUtils.writeBuffer(data, keymapEncoding);
	exchangeApdu(BTCHIP_CLA, BTCHIP_INS_SET_KEYMAP, (byte)0x00, (byte)0x00, data.toByteArray(), OK);		
}
 
Example 20
Source File: BitcoinTransaction.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public void serialize(ByteArrayOutputStream output) throws BTChipException {
	BufferUtils.writeBuffer(output, amount);
	VarintUtils.write(output, script.length);
	BufferUtils.writeBuffer(output, script);
}