com.btchip.utils.BufferUtils Java Examples

The following examples show how to use com.btchip.utils.BufferUtils. 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 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 #2
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 #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: 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 #5
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 #6
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private byte[] inputBytes(final InputOutputData in, final boolean isSegwit) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream(32 + (isSegwit ? 12 : 4));
    final byte[] txid = in.getTxid();
    os.write(txid, 0, txid.length);
    BufferUtils.writeUint32LE(os, in.getPtIdx());
    if (isSegwit)
        BufferUtils.writeUint64LE(os, in.getSatoshi());
    return os.toByteArray();
}
 
Example #7
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 #8
Source File: BTChipDongle.java    From WalletCordova with GNU Lesser General Public License v2.1 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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: BTChipDongle.java    From GreenBits with GNU General Public License v3.0 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 #16
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 #17
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 #18
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private byte[] outputBytes(final List<InputOutputData> outputs) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream(outputs.size() * (8 + 256));
    putVarInt(os, outputs.size());
    for (final InputOutputData out : outputs) {
        BufferUtils.writeUint64LE(os, out.getSatoshi()); // int64ToByteStreamLE in bitcoinj
        final byte[] script = Wally.hex_to_bytes(out.getScript());
        putVarInt(os, script.length).write(script, 0, script.length);
    }
    return os.toByteArray();
}
 
Example #19
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 #20
Source File: BTChipDongle.java    From green_android 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 #21
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 #22
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 #23
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private byte[] inputLiquidBytes(final InputOutputData in) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream(32 + 4 + 33);
    final byte[] txid = in.getTxid();
    os.write(txid, 0, txid.length);
    BufferUtils.writeUint32LE(os, in.getPtIdx());
    os.write(in.getCommitmentBytes(), 0, in.getCommitmentBytes().length);
    return os.toByteArray();
}
 
Example #24
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 #25
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 #26
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 #27
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 #28
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 #29
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
private byte[] sequenceBytes(final InputOutputData in) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream(4);
    BufferUtils.writeUint32LE(os, in.getSequence());
    return os.toByteArray();
}
 
Example #30
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);
}