Search in sources :

Example 6 with Base32

use of org.apache.commons.codec.binary.Base32 in project nem2-sdk-java by nemtech.

the class TransferTransaction method generateBytes.

byte[] generateBytes() {
    FlatBufferBuilder builder = new FlatBufferBuilder();
    BigInteger deadlineBigInt = BigInteger.valueOf(getDeadline().getInstant());
    int[] fee = new int[] { 0, 0 };
    int version = (int) Long.parseLong(Integer.toHexString(getNetworkType().getValue()) + "0" + Integer.toHexString(getVersion()), 16);
    // Create Message
    byte[] bytePayload = message.getPayload().getBytes(StandardCharsets.UTF_8);
    int payload = MessageBuffer.createPayloadVector(builder, bytePayload);
    MessageBuffer.startMessageBuffer(builder);
    MessageBuffer.addType(builder, message.getType());
    MessageBuffer.addPayload(builder, payload);
    int message = MessageBuffer.endMessageBuffer(builder);
    // Create Mosaics
    int[] mosaicBuffers = new int[mosaics.size()];
    for (int i = 0; i < mosaics.size(); ++i) {
        Mosaic mosaic = mosaics.get(i);
        int id = MosaicBuffer.createIdVector(builder, UInt64.fromBigInteger(mosaic.getId().getId()));
        int amount = MosaicBuffer.createAmountVector(builder, UInt64.fromBigInteger(mosaic.getAmount()));
        MosaicBuffer.startMosaicBuffer(builder);
        MosaicBuffer.addId(builder, id);
        MosaicBuffer.addAmount(builder, amount);
        mosaicBuffers[i] = MosaicBuffer.endMosaicBuffer(builder);
    }
    byte[] address = new Base32().decode(getRecipient().plain().getBytes(StandardCharsets.UTF_8));
    // Create Vectors
    int signatureVector = TransferTransactionBuffer.createSignatureVector(builder, new byte[64]);
    int signerVector = TransferTransactionBuffer.createSignerVector(builder, new byte[32]);
    int deadlineVector = TransferTransactionBuffer.createDeadlineVector(builder, UInt64.fromBigInteger(deadlineBigInt));
    int feeVector = TransferTransactionBuffer.createFeeVector(builder, fee);
    int recipientVector = TransferTransactionBuffer.createRecipientVector(builder, address);
    int mosaicsVector = TransferTransactionBuffer.createMosaicsVector(builder, mosaicBuffers);
    // replace by the all numbers sum or add a comment explaining this
    int fixSize = 149;
    TransferTransactionBuffer.startTransferTransactionBuffer(builder);
    TransferTransactionBuffer.addSize(builder, fixSize + (16 * mosaics.size()) + bytePayload.length);
    TransferTransactionBuffer.addSignature(builder, signatureVector);
    TransferTransactionBuffer.addSigner(builder, signerVector);
    TransferTransactionBuffer.addVersion(builder, version);
    TransferTransactionBuffer.addType(builder, getType().getValue());
    TransferTransactionBuffer.addFee(builder, feeVector);
    TransferTransactionBuffer.addDeadline(builder, deadlineVector);
    TransferTransactionBuffer.addRecipient(builder, recipientVector);
    TransferTransactionBuffer.addNumMosaics(builder, mosaics.size());
    TransferTransactionBuffer.addMessageSize(builder, bytePayload.length + 1);
    TransferTransactionBuffer.addMessage(builder, message);
    TransferTransactionBuffer.addMosaics(builder, mosaicsVector);
    int codedTransfer = TransferTransactionBuffer.endTransferTransactionBuffer(builder);
    builder.finish(codedTransfer);
    return schema.serialize(builder.sizedByteArray());
}
Also used : FlatBufferBuilder(com.google.flatbuffers.FlatBufferBuilder) BigInteger(java.math.BigInteger) Base32(org.apache.commons.codec.binary.Base32) Mosaic(io.nem.sdk.model.mosaic.Mosaic)

Example 7 with Base32

use of org.apache.commons.codec.binary.Base32 in project nem2-sdk-java by nemtech.

the class Base32Encoder method getString.

/**
 * Converts a byte array to a Base32 string.
 *
 * @param bytes The input byte array.
 * @return The output Base32 string.
 */
public static String getString(final byte[] bytes) {
    final Base32 codec = new Base32();
    final byte[] decodedBytes = codec.encode(bytes);
    return StringEncoder.getString(decodedBytes);
}
Also used : Base32(org.apache.commons.codec.binary.Base32)

Aggregations

Base32 (org.apache.commons.codec.binary.Base32)7 FlatBufferBuilder (com.google.flatbuffers.FlatBufferBuilder)2 BigInteger (java.math.BigInteger)2 Mosaic (io.nem.sdk.model.mosaic.Mosaic)1 SecureRandom (java.security.SecureRandom)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1