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];
}
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);
}
}
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();
}
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;
}
}
}
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);
}
}
Aggregations