org.bitcoinj.core.VarInt Java Examples

The following examples show how to use org.bitcoinj.core.VarInt. 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: GATx.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private static int estimateElementsSize(final Transaction tx, final Network network) {
    if (!network.isElements())
        return 0;

    final int sjSize = Wally.asset_surjectionproof_size(tx.getInputs().size());
    final int cmtSize = Wally.EC_PUBLIC_KEY_LEN;
    // Estimate the rangeproof len as 160 bytes per 2 bits used to express the
    // output value (currently 32), plus fixed overhead of 128 (this is a slight
    // over-estimate given that only up to +100 has been seen in the wild).
    // FIXME: This assumes 32 bit maximum amounts as per current wally impl.
    final int rpSize = ((32 / 2) * 160) + 128;
    final int singleOutputSize = sjSize + VarInt.sizeOf(sjSize) +
                                 cmtSize + VarInt.sizeOf(cmtSize) +
                                 rpSize + VarInt.sizeOf(rpSize);
    return singleOutputSize * tx.getOutputs().size();
}
 
Example #2
Source File: ElementsTransactionOutput.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
protected void bitcoinSerializeToStream(final OutputStream stream) throws IOException {
    checkNotNull(getScriptBytes());

    stream.write(assetTag);

    if (value != 0) {
        stream.write(new byte[] { 1 });
        final byte[] out = new byte[8];
        Utils.uint64ToByteArrayLE(value, out, 0);
        for (int i = 0; i < 4; ++i) {
            final byte tmp = out[i];
            out[i] = out[7-i];
            out[7-i] = tmp;
        }
        stream.write(out);
    } else {
        stream.write(commitment);
    }

    // TODO: Move script serialization into the Script class, where it belongs.
    stream.write(new VarInt(getScriptBytes().length).encode());
    stream.write(getScriptBytes());
}
 
Example #3
Source File: Identity.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
String encryptDataToIPFS(String originData, long unixtime, byte[] iv) {
  int headerLength = 21;
  byte[] toSign = new byte[headerLength + 32];
  byte version = 0x03;
  toSign[0] = version;
  byte[] timestamp = new byte[4];

  Utils.uint32ToByteArrayLE(unixtime, timestamp, 0);
  System.arraycopy(timestamp, 0, toSign, 1, 4);
  byte[] encryptionKey = NumericUtil.hexToBytes(this.keystore.getEncKey());

  System.arraycopy(iv, 0, toSign, 5, 16);

  byte[] encKey = Arrays.copyOf(encryptionKey, 16);
  byte[] ciphertext = AES.encryptByCBC(originData.getBytes(Charset.forName("UTF-8")), encKey, iv);
  VarInt ciphertextLength = new VarInt(ciphertext.length);

  System.arraycopy(Hash.merkleHash(ciphertext), 0, toSign, headerLength, 32);
  String signature = EthereumSign.sign(NumericUtil.bytesToHex(toSign), encryptionKey);
  byte[] signatureBytes = NumericUtil.hexToBytes(signature);
  int totalLen = (int) (headerLength + ciphertextLength.getSizeInBytes() + ciphertextLength.value + 65);
  byte[] payload = new byte[totalLen];
  int destPos = 0;
  System.arraycopy(toSign, 0, payload, destPos, headerLength);
  destPos += headerLength;
  System.arraycopy(ciphertextLength.encode(), 0, payload, destPos, ciphertextLength.getSizeInBytes());
  destPos += ciphertextLength.getSizeInBytes();
  System.arraycopy(ciphertext, 0, payload, destPos, (int) ciphertextLength.value);
  destPos += (int) ciphertextLength.value;

  System.arraycopy(signatureBytes, 0, payload, destPos, 65);
  return NumericUtil.bytesToHex(payload);

}
 
Example #4
Source File: ElementsTransaction.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void readOutputs() {
    final long numOutputs = readVarInt();
    optimalEncodingMessageSize += VarInt.sizeOf(numOutputs);
    outputs = new ArrayList<>((int) numOutputs);
    for (long i = 0; i < numOutputs; i++) {
        final TransactionOutput output = new ElementsTransactionOutput(params, this, payload, cursor, serializer);
        outputs.add(output);
        final int len = (output.getValue().getValue() != 0) ? 42 : 66;  // 33+9 or 33+33
        final long scriptLen = readVarInt(len);
        optimalEncodingMessageSize += len + VarInt.sizeOf(scriptLen) + scriptLen;
        cursor += scriptLen;
    }
}
 
Example #5
Source File: ElementsTransaction.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
protected void readOutWitness() {
    outWitness = new ArrayList<>(getOutputs().size());
    for (int i = 0; i < getOutputs().size(); i++) {
        final long pushCount = 3;
        outWitness.add(new ArrayList<byte[]>());
        for (int y = 0; y < pushCount; y++) {
            final long pushSize = readVarInt();
            optimalEncodingMessageSize += VarInt.sizeOf(pushSize) + pushSize;
            outWitness.get(outWitness.size() - 1).add(readBytes((int) pushSize));
        }
    }
}
 
Example #6
Source File: Identity.java    From token-core-android with Apache License 2.0 4 votes vote down vote up
public String decryptDataFromIPFS(String encryptedData) {
  int headerLength = 21;

  byte[] payload = NumericUtil.hexToBytes(encryptedData);

  byte version = payload[0];
  if (version != 0x03) {
    throw new TokenException(Messages.UNSUPPORT_ENCRYPTION_DATA_VERSION);
  }
  int srcPos = 1;
  byte[] toSign = new byte[headerLength + 32];
  System.arraycopy(payload, 0, toSign, 0, headerLength);

  byte[] timestamp = new byte[4];
  System.arraycopy(payload, srcPos, timestamp, 0, 4);
  srcPos += 4;

  byte[] encryptionKey = NumericUtil.hexToBytes(this.keystore.getEncKey());
  byte[] iv = new byte[16];
  System.arraycopy(payload, srcPos, iv, 0, 16);
  srcPos += 16;
  VarInt ciphertextLength = new VarInt(payload, srcPos);
  srcPos += ciphertextLength.getSizeInBytes();
  byte[] ciphertext = new byte[(int) ciphertextLength.value];
  System.arraycopy(payload, srcPos, ciphertext, 0, (int) ciphertextLength.value);
  System.arraycopy(Hash.merkleHash(ciphertext), 0, toSign, headerLength, 32);
  srcPos += ciphertextLength.value;
  byte[] encKey = Arrays.copyOf(encryptionKey, 16);
  String content = new String(AES.decryptByCBC(ciphertext, encKey, iv), Charset.forName("UTF-8"));

  byte[] signature = new byte[65];
  System.arraycopy(payload, srcPos, signature, 0, 65);
  try {
    BigInteger pubKey = EthereumSign.ecRecover(NumericUtil.bytesToHex(toSign), NumericUtil.bytesToHex(signature));
    ECKey ecKey = ECKey.fromPublicOnly(ByteUtil.concat(new byte[]{0x04}, NumericUtil.bigIntegerToBytesWithZeroPadded(pubKey, 64)));
    String recoverIpfsID = new Multihash(Multihash.Type.sha2_256, Hash.sha256(ecKey.getPubKey())).toBase58();

    if (!this.keystore.getIpfsId().equals(recoverIpfsID)) {
      throw new TokenException(Messages.INVALID_ENCRYPTION_DATA_SIGNATURE);
    }

  } catch (SignatureException e) {
    throw new TokenException(Messages.INVALID_ENCRYPTION_DATA_SIGNATURE);
  }
  return content;
}
 
Example #7
Source File: BTChipHWWallet.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
private ByteArrayOutputStream putVarInt(final ByteArrayOutputStream os, final long v) {
    final byte[] buf = new VarInt(v).encode();
    os.write(buf, 0, buf.length);
    return os;
}
 
Example #8
Source File: ElementsTransaction.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void bitcoinSerializeToStream(final OutputStream stream, final int transactionOptions) throws IOException {
    final boolean witSupported = (protocolVersion >= NetworkParameters.ProtocolVersion.WITNESS_VERSION.getBitcoinProtocolVersion())
            && (transactionOptions & TransactionOptions.WITNESS) != 0;
    final boolean serializeWit = hasWitness() && witSupported;
    uint32ToByteStreamLE(getVersion(), stream);
    if (serializeWit) {
        stream.write(new byte[]{0, (byte) (!outWitness.isEmpty() ? 3 : 1)});
    } else if (!outWitness.isEmpty() && (transactionOptions & TransactionOptions.WITNESS) != 0) {
        stream.write(new byte[]{0, 2});
    }
    stream.write(new VarInt(getInputs().size()).encode());
    for (final TransactionInput in : getInputs())
        in.bitcoinSerialize(stream);
    stream.write(new VarInt(outputs.size()).encode());
    for (final TransactionOutput out : outputs)
        out.bitcoinSerialize(stream);

    if (serializeWit) {
        for (int i = 0; i < getInputs().size(); i++) {
            final TransactionWitness witness = getWitness(i);
            stream.write(new VarInt(witness.getPushCount()).encode());
            for (int y = 0; y < witness.getPushCount(); y++) {
                final byte[] push = witness.getPush(y);
                stream.write(new VarInt(push.length).encode());
                stream.write(push);
            }
        }
    }

    if ((transactionOptions & TransactionOptions.WITNESS) != 0) {
        for (final List<byte[]> outwit : outWitness) {
            for (final byte[] ow : outwit) {
                stream.write(new VarInt(ow.length).encode());
                stream.write(ow);
            }
        }
    }

    uint32ToByteStreamLE(getLockTime(), stream);
}