Skip to content

Commit

Permalink
Merge pull request #55 from casper-network/develop
Browse files Browse the repository at this point in the history
Merging develop
  • Loading branch information
asladeofgreen authored Sep 28, 2021
2 parents b05d2ef + dd1219b commit 503eb40
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.gradle/
/.idea/
/build/
/target/
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.casper.sdk.service.json.serialize;

import com.casper.sdk.service.serialization.util.ByteUtils;
import com.casper.sdk.types.CLKeyInfo;
import com.casper.sdk.types.CLKeyValue;
import com.casper.sdk.types.CLValue;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand All @@ -15,8 +15,6 @@
*/
public class CLKeyValueJsonSerializer extends JsonSerializer<CLKeyValue> {

private static final String HASH_PREFIX = "hash-";

@Override
public void serialize(final CLKeyValue value,
final JsonGenerator gen,
Expand All @@ -32,21 +30,21 @@ public void serialize(final CLKeyValue value,

@NotNull
private String buildJsonBytes(final CLKeyValue value) {
return "0" + value.getKeyType().getTag() + getValueBytes(value);
return getValueBytes(value);
}

protected String getValueBytes(CLKeyValue value) {
protected String getValueBytes(final CLKeyValue value) {
return value.toHex();
}

private void writeParsed(CLValue value, JsonGenerator gen) throws IOException {
private void writeParsed(final CLKeyValue value, final JsonGenerator gen) throws IOException {

final String strParsed;

if (value.getParsed() != null) {
strParsed = buildParsed(value.getParsed().toString());
} else if (value.getBytes() != null) {
strParsed = buildParsed(ByteUtils.encodeHexString(value.getBytes()));
strParsed = buildParsed(value.getParsed().toString(), value.getKeyType());
} else if (value.getKeyBytes() != null) {
strParsed = buildParsed(ByteUtils.encodeHexString(value.getKeyBytes()), value.getKeyType());
} else {
strParsed = null;
}
Expand All @@ -60,11 +58,11 @@ private void writeParsed(CLValue value, JsonGenerator gen) throws IOException {
}
}

private String buildParsed(final String parsed) {
if (parsed.startsWith(HASH_PREFIX)) {
private String buildParsed(final String parsed, final CLKeyInfo.KeyType keyType) {
if (parsed.contains("-")) {
return parsed;
} else {
return HASH_PREFIX + parsed;
return keyType.getParsedName() + "-" + parsed;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.casper.sdk.service.serialization.cltypes;

import com.casper.sdk.exceptions.ConversionException;
import com.casper.sdk.service.serialization.util.ByteUtils;
import com.casper.sdk.types.*;
import org.apache.commons.lang3.ArrayUtils;

import java.math.BigInteger;

import static com.casper.sdk.service.serialization.util.ByteUtils.toByteArray;

/**
* Builder to help with programmatic conversion of value types.
*/
Expand Down Expand Up @@ -50,21 +54,25 @@ public static CLValue u512(final Object value) {
return buildCLValue(CLType.U512, value);
}

public static CLKeyValue key(final byte[] value) {
if (value.length != 33) {
throw new ConversionException("Missing key type from byte array");
}
return new CLKeyValue(ByteUtils.lastNBytes(value, 32), CLKeyInfo.KeyType.valueOf(value[0]), null);
}

public static CLKeyValue accountKey(final byte[] value) {
return createKey(value, CLKeyInfo.KeyType.ACCOUNT_ID);
return key(ByteUtils.concat(toByteArray(CLKeyInfo.KeyType.ACCOUNT_ID.getTag()), value));
}

public static CLKeyValue hashKey(final byte[] value) {
return createKey(value, CLKeyInfo.KeyType.HASH_ID);
return key(ByteUtils.concat(toByteArray(CLKeyInfo.KeyType.HASH_ID.getTag()), value));
}

public static CLKeyValue uRefKey(final byte[] value) {
return createKey(value, CLKeyInfo.KeyType.UREF_ID);
return key(ByteUtils.concat(toByteArray(CLKeyInfo.KeyType.UREF_ID.getTag()), value));
}

private static CLKeyValue createKey(byte[] value, CLKeyInfo.KeyType keyType) {
return new CLKeyValue(value, keyType, null);
}

private static CLValue buildCLValue(final CLType type, final Object value) {
return new CLValue(TYPES_FACTORY.getInstance(type).serialize(value), type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public static byte[] lastNBytes(byte[] toTruncate, final int length) {
System.arraycopy(toTruncate, start, secretBytes, 0, length);
return secretBytes;
}

/**
* Converts a number to a byte value in a byte array
*
* @param toByteInArray the number to convert
* @return the byte array containing a single byte value
*/
public static byte[] toByteArray(final Number toByteInArray) {
return new byte[]{toByteInArray.byteValue()};
}
}
24 changes: 20 additions & 4 deletions src/main/java/com/casper/sdk/types/CLKeyInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,39 @@ public class CLKeyInfo extends CLTypeInfo {
public enum KeyType implements HasTag {

/** The Account variant */
ACCOUNT_ID(0),
ACCOUNT_ID(0, "account-hash"),
/** The Hash variant */
HASH_ID(1),
HASH_ID(1, "hash"),
/** The URef variant */
UREF_ID(2);
UREF_ID(2, "uref");

private final int tag;
private final String parsedName;

KeyType(int tag) {
KeyType(final int tag, final String parsedName) {
this.tag = tag;
this.parsedName = parsedName;
}

public static KeyType valueOf(byte tag) {
for (KeyType keyType : KeyType.values()) {
if (tag == keyType.tag) {
return keyType;
}
}
throw new IllegalArgumentException("Invalid key type: " + tag);
}

@Override
public int getTag() {
return tag;
}

public String getParsedName() {
return parsedName;
}
}

private final KeyType keyType;

public CLKeyInfo(final KeyType keyType) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/com/casper/sdk/types/CLKeyValue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.casper.sdk.types;

import com.casper.sdk.service.json.serialize.CLKeyValueJsonSerializer;
import com.casper.sdk.service.serialization.util.ByteUtils;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
Expand All @@ -17,7 +18,26 @@ public CLKeyValue(final byte[] bytes, final CLKeyInfo.KeyType keyType, final Obj
super(bytes, new CLKeyInfo(keyType), parsed);
}

/**
* Obtains the bytes including the key type prefix byte
*
* @return the bytes
*/
@Override
public byte[] getBytes() {
return ByteUtils.concat(ByteUtils.toByteArray(getKeyType().getTag()), super.getBytes());
}

/**
* Obtains the bytes without the key type prefix
*
* @return the original key bytes without the key type prefix byte
*/
public byte[] getKeyBytes() {
return super.getBytes();
}

public CLKeyInfo.KeyType getKeyType() {
return ((CLKeyInfo)getCLTypeInfo()).getKeyType();
return ((CLKeyInfo) getCLTypeInfo()).getKeyType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void clAccountKeyToJson() throws IOException {

assertThat(json, hasJsonPath("$.cl_type", is("Key")));
assertThat(json, hasJsonPath("$.bytes", is("00" + KEY_HEX)));
assertThat(json, hasJsonPath("$.parsed.Hash", is("hash-" + KEY_HEX)));
assertThat(json, hasJsonPath("$.parsed.Hash", is("account-hash-" + KEY_HEX)));
}

/**
Expand Down Expand Up @@ -76,6 +76,6 @@ void clURefKeyToJson() throws IOException {

assertThat(json, hasJsonPath("$.cl_type", is("Key")));
assertThat(json, hasJsonPath("$.bytes", is("02" + keyHex)));
assertThat(json, hasJsonPath("$.parsed.Hash", is("hash-" + keyHex)));
assertThat(json, hasJsonPath("$.parsed.Hash", is("uref-" + keyHex)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.casper.sdk.service.serialization.types;

import com.casper.sdk.types.*;
import com.casper.sdk.service.serialization.cltypes.CLValueBuilder;
import com.casper.sdk.service.serialization.cltypes.TypesFactory;
import com.casper.sdk.types.*;
import org.junit.jupiter.api.Test;

import static com.casper.sdk.service.serialization.util.ByteUtils.concat;
Expand Down Expand Up @@ -52,7 +53,6 @@ void u512ValueToBytes() {
assertThat(byteSerializer.toBytes(source), is(expected));
}


@Test
void optionValueToBytes() {

Expand Down Expand Up @@ -90,6 +90,29 @@ void byteOptionArrayKeyValue() {
};

assertThat(byteSerializer.toBytes(optionValue), is(expected));
}


@Test
void keyValueToBytes() {

// Create key value from bytes prefixed with the key type
final CLKeyValue clKeyValue = CLValueBuilder.key(decodeHex("012b177f0739348d33ce868b2f95bb83decf5b5dcc71279d4bec64c87f60b805d5"));

// Assert they key type is a has
assertThat(clKeyValue.getKeyType(), is(CLKeyInfo.KeyType.HASH_ID));

assertThat(clKeyValue.getBytes().length, is(33));
assertThat(clKeyValue.getBytes(), is(decodeHex("012b177f0739348d33ce868b2f95bb83decf5b5dcc71279d4bec64c87f60b805d5")));

assertThat(clKeyValue.getKeyBytes().length, is(32));
assertThat(clKeyValue.getKeyBytes(), is(decodeHex("2b177f0739348d33ce868b2f95bb83decf5b5dcc71279d4bec64c87f60b805d5")));

// Assert that the key can be correctly serialized to bytes
final byte[] expected = decodeHex("21" + // Length of key with prefix = 33 (0x21)
"000000012b177f0739348d33ce868b2f95bb83decf5b5dcc71279d4bec64c87f60b805d5" +
"0B"); // CL Type == 11 (0x0B)

assertThat(byteSerializer.toBytes(clKeyValue), is(expected));
}
}
}

0 comments on commit 503eb40

Please sign in to comment.