com.btchip.utils.VarintUtils Java Examples

The following examples show how to use com.btchip.utils.VarintUtils. 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 GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public BitcoinTransaction(ByteArrayInputStream data) throws BTChipException  {		
	inputs = new Vector<BitcoinInput>();
	outputs = new Vector<BitcoinOutput>();
	try {
		version = new byte[4];
		data.read(version);
		long numberItems = VarintUtils.read(data);
		for (long i=0; i<numberItems; i++) {
			inputs.add(new BitcoinInput(data));
		}
		numberItems = VarintUtils.read(data);
		for (long i=0; i<numberItems; i++) {
			outputs.add(new BitcoinOutput(data));
		}
		lockTime = new byte[4];
		data.read(lockTime);			
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}					
}
 
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: 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 #4
Source File: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
public BitcoinTransaction(ByteArrayInputStream data) throws BTChipException  {		
	inputs = new Vector<BitcoinInput>();
	outputs = new Vector<BitcoinOutput>();
	try {
		version = new byte[4];
		data.read(version);
		long numberItems = VarintUtils.read(data);
		for (long i=0; i<numberItems; i++) {
			inputs.add(new BitcoinInput(data));
		}
		numberItems = VarintUtils.read(data);
		for (long i=0; i<numberItems; i++) {
			outputs.add(new BitcoinOutput(data));
		}
		lockTime = new byte[4];
		data.read(lockTime);			
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}					
}
 
Example #5
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 #6
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 #7
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 #8
Source File: BitcoinTransaction.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public BitcoinTransaction(ByteArrayInputStream data) throws BTChipException  {		
	inputs = new Vector<BitcoinInput>();
	outputs = new Vector<BitcoinOutput>();
	try {
		version = new byte[4];
		data.read(version);
		long numberItems = VarintUtils.read(data);
		for (long i=0; i<numberItems; i++) {
			inputs.add(new BitcoinInput(data));
		}
		numberItems = VarintUtils.read(data);
		for (long i=0; i<numberItems; i++) {
			outputs.add(new BitcoinOutput(data));
		}
		lockTime = new byte[4];
		data.read(lockTime);			
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}					
}
 
Example #9
Source File: BitcoinTransaction.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public BitcoinInput(ByteArrayInputStream data) throws BTChipException {	
	try {
		prevOut = new byte[36];
		data.read(prevOut);
		long scriptSize = VarintUtils.read(data);
		script = new byte[(int)scriptSize];
		data.read(script);
		sequence = new byte[4];
		data.read(sequence);
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}			
}
 
Example #10
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 #11
Source File: BitcoinTransaction.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public BitcoinOutput(ByteArrayInputStream data) throws BTChipException {
	try {
		amount = new byte[8];
		data.read(amount);
		long scriptSize = VarintUtils.read(data);
		script = new byte[(int)scriptSize];
		data.read(script);				
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}			
}
 
Example #12
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 #13
Source File: BitcoinTransaction.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public BitcoinInput(ByteArrayInputStream data) throws BTChipException {	
	try {
		prevOut = new byte[36];
		data.read(prevOut);
		long scriptSize = VarintUtils.read(data);
		script = new byte[(int)scriptSize];
		data.read(script);
		sequence = new byte[4];
		data.read(sequence);
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}			
}
 
Example #14
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 #15
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 #16
Source File: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BitcoinInput(ByteArrayInputStream data) throws BTChipException {	
	try {
		prevOut = new byte[36];
		data.read(prevOut);
		long scriptSize = VarintUtils.read(data);
		script = new byte[(int)scriptSize];
		data.read(script);
		sequence = new byte[4];
		data.read(sequence);
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}			
}
 
Example #17
Source File: BitcoinTransaction.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public BitcoinOutput(ByteArrayInputStream data) throws BTChipException {
	try {
		amount = new byte[8];
		data.read(amount);
		long scriptSize = VarintUtils.read(data);
		script = new byte[(int)scriptSize];
		data.read(script);				
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}			
}
 
Example #18
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 #19
Source File: BitcoinTransaction.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BitcoinOutput(ByteArrayInputStream data) throws BTChipException {
	try {
		amount = new byte[8];
		data.read(amount);
		long scriptSize = VarintUtils.read(data);
		script = new byte[(int)scriptSize];
		data.read(script);				
	}
	catch(Exception e) {
		throw new BTChipException("Invalid encoding", e);
	}			
}
 
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, prevOut);
	VarintUtils.write(output, script.length);
	BufferUtils.writeBuffer(output, script);
	BufferUtils.writeBuffer(output, sequence);
}
 
Example #21
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);
}
 
Example #22
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 #23
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 #24
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 #25
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);
}