Java Code Examples for com.btchip.utils.VarintUtils#write()

The following examples show how to use com.btchip.utils.VarintUtils#write() . 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 green_android with GNU General Public License v3.0 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: BitcoinTransaction.java    From GreenBits with GNU General Public License v3.0 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 3
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public void startUntrustedTransction(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(input.isSegwit() ? (byte)0x02 : input.isTrusted() ? (byte)0x01 : (byte)0x00);
		if (input.isTrusted()) {
			// other 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 4
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 5
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 6
Source File: BitcoinTransaction.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public byte[] serializeOutputs() throws BTChipException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    VarintUtils.write(output, outputs.size());
    for (BitcoinOutput outputItem : outputs) {
            outputItem.serialize(output);
    }
    return output.toByteArray();
}
 
Example 7
Source File: BitcoinTransaction.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public byte[] serializeOutputs() throws BTChipException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    VarintUtils.write(output, outputs.size());
    for (BitcoinOutput outputItem : outputs) {
            outputItem.serialize(output);
    }
    return output.toByteArray();
}
 
Example 8
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 9
Source File: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public byte[] serializeOutputs() throws BTChipException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    VarintUtils.write(output, outputs.size());
    for (BitcoinOutput outputItem : outputs) {
            outputItem.serialize(output);
    }
    return output.toByteArray();
}
 
Example 10
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 11
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, prevOut);
	VarintUtils.write(output, script.length);
	BufferUtils.writeBuffer(output, script);
	BufferUtils.writeBuffer(output, sequence);
}
 
Example 12
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);
}
 
Example 13
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 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, amount);
	VarintUtils.write(output, script.length);
	BufferUtils.writeBuffer(output, script);
}
 
Example 15
Source File: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 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 16
Source File: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 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);
}