Search in sources :

Example 1 with VarInt

use of io.nuls.core.crypto.VarInt in project nuls by nuls-io.

the class AccountBody method serialize.

public final byte[] serialize() {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        if (contents != null) {
            for (AccountKeyValue keyValuePair : contents) {
                byte[] keyValue = keyValuePair.toByte();
                bos.write(new VarInt(keyValue.length).encode());
                bos.write(keyValue);
            }
        }
        return bos.toByteArray();
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            Log.error(e);
        } finally {
        }
    }
    return new byte[0];
}
Also used : VarInt(io.nuls.core.crypto.VarInt) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with VarInt

use of io.nuls.core.crypto.VarInt in project nuls by nuls-io.

the class NulsByteBuffer method readVarInt.

public long readVarInt(int offset) throws NulsException {
    try {
        VarInt varint = new VarInt(payload, cursor + offset);
        cursor += offset + varint.getOriginalSizeInBytes();
        return varint.value;
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new NulsException(ErrorCode.DATA_PARSE_ERROR, e);
    }
}
Also used : VarInt(io.nuls.core.crypto.VarInt) NulsException(io.nuls.core.exception.NulsException)

Example 3 with VarInt

use of io.nuls.core.crypto.VarInt in project nuls by nuls-io.

the class KeyValue method toByte.

public byte[] toByte() {
    ByteArrayTool byteArray = new ByteArrayTool();
    try {
        byte[] codeBytes = code.getBytes(CHARSET);
        byteArray.append(codeBytes.length);
        byteArray.append(codeBytes);
        byte[] nameBytes = name.getBytes(CHARSET);
        byteArray.append(nameBytes.length);
        byteArray.append(nameBytes);
        byteArray.append(new VarInt(value.length).encode());
        byteArray.append(value);
    } catch (UnsupportedEncodingException e) {
        Log.error(e);
    }
    return byteArray.toArray();
}
Also used : ByteArrayTool(io.nuls.core.utils.crypto.ByteArrayTool) VarInt(io.nuls.core.crypto.VarInt) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with VarInt

use of io.nuls.core.crypto.VarInt in project nuls by nuls-io.

the class AccountBody method parse.

public void parse(byte[] content) {
    if (content == null || content.length == 0) {
        return;
    }
    int cursor = 0;
    contents = new ArrayList<AccountKeyValue>();
    while (true) {
        VarInt varint = new VarInt(content, cursor);
        cursor += varint.getOriginalSizeInBytes();
        AccountKeyValue keyValuePair = new AccountKeyValue(Arrays.copyOfRange(content, cursor, cursor + (int) varint.value));
        contents.add(keyValuePair);
        cursor += varint.value;
        if (cursor >= content.length) {
            break;
        }
    }
}
Also used : VarInt(io.nuls.core.crypto.VarInt)

Example 5 with VarInt

use of io.nuls.core.crypto.VarInt in project nuls by nuls-io.

the class Utils method formatMessageForSigning.

/**
 * Given a textual message, returns a byte buffer formatted as follows:</p>
 *
 * <tt>[24] "Bitcoin Signed Message:\n" [message.length as a varint] message</p></tt>
 */
public static byte[] formatMessageForSigning(String message) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(SIGNED_MESSAGE_HEADER_BYTES.length);
        bos.write(SIGNED_MESSAGE_HEADER_BYTES);
        byte[] messageBytes = message.getBytes(CHARSET);
        VarInt size = new VarInt(messageBytes.length);
        bos.write(size.encode());
        bos.write(messageBytes);
        return bos.toByteArray();
    } catch (IOException e) {
        // Cannot happen.
        throw new RuntimeException(e);
    }
}
Also used : NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) VarInt(io.nuls.core.crypto.VarInt) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

VarInt (io.nuls.core.crypto.VarInt)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 NulsException (io.nuls.core.exception.NulsException)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 ByteArrayTool (io.nuls.core.utils.crypto.ByteArrayTool)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1