Skip to content

Commit

Permalink
Merge pull request #366 from /issues/349
Browse files Browse the repository at this point in the history
Issues/349 - Refactor of keys from String to Key class
  • Loading branch information
cnorburn authored Sep 24, 2024
2 parents 41a50b2 + a390a24 commit 693da34
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public T deserialize(final JsonParser p, final DeserializationContext ctxt) thro
if (strKey.contains("-")) {
try {
//noinspection unchecked
return (T) Key.fromKeyString(strKey);
return (T) Key.create(strKey);
} catch (NoSuchKeyTagException e) {
throw new CasperClientException("No such key: " + strKey, e);
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/casper/sdk/jackson/serializer/KeySerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.casper.sdk.jackson.serializer;

import com.casper.sdk.model.key.Key;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
* Desrializer for {@link Key} types.
*
* @author [email protected]
*/
public class KeySerializer extends JsonSerializer<Key> {
@Override
public void serialize(final Key key, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
gen.writeString(key.toString());
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/casper/sdk/model/account/Account.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.casper.sdk.model.account;

import com.casper.sdk.model.contract.NamedKey;
import com.casper.sdk.model.key.AccountHashKey;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -28,7 +29,7 @@ public class Account {
* account_hash(String) Hex-encoded account hash.
*/
@JsonProperty("account_hash")
private String hash;
private AccountHashKey hash;

/**
* {@link ActionThresholds} that have to be met
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.casper.sdk.model.account;

import com.casper.sdk.model.key.AccountHashKey;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -25,7 +26,7 @@ public class AssociatedKey {
* account_hash(String) Hex-encoded account hash.
*/
@JsonProperty("account_hash")
private String accountHash;
private AccountHashKey accountHash;

/**
* weight(Integer)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/casper/sdk/model/contract/NamedKey.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.casper.sdk.model.contract;

import com.casper.sdk.model.key.Key;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -25,7 +26,7 @@ public class NamedKey {
* key(String) The value of the entry: a casper `Key` type.
*/
@JsonProperty("key")
private String key;
private Key key;

/**
* name(String) The name of the entry.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/casper/sdk/model/deploy/Operation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.casper.sdk.model.deploy;

import com.casper.sdk.model.key.Key;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -23,7 +24,7 @@ public class Operation {
/**
* The formatted string of the `Key`
*/
private String key;
private Key key;

/**
* @see OpKind
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.casper.sdk.model.deploy.executionresult;

import com.casper.sdk.model.deploy.ExecutionEffect;
import com.casper.sdk.model.key.Key;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
Expand Down Expand Up @@ -30,7 +31,7 @@ public class Success {
private ExecutionEffect effect;

/** List of Hex-encoded transfer address. */
private List<String> transfers;
private List<Key> transfers;

/** The cost of executing the deploy. */
@JsonSerialize(using = ToStringSerializer.class)
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/casper/sdk/model/entity/Account.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.casper.sdk.model.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.casper.sdk.exception.NoSuchKeyTagException;
import com.casper.sdk.model.key.AccountHashKey;
import com.casper.sdk.model.key.Key;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.*;

/**
Expand All @@ -13,9 +17,13 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonTypeName("Account")
public class Account implements EntityAddressKind {

@JsonProperty("Account")
private String account;
@JsonValue
private AccountHashKey account;

public Account(final String accountSt) throws NoSuchKeyTagException {
this.account = (AccountHashKey) Key.create(accountSt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ public class AddressableEntity implements StateEntity {
/** The entry points of the addressable entity. */
@JsonProperty("entry_points")
private List<EntryPointValue> entryPoints;

}
9 changes: 4 additions & 5 deletions src/main/java/com/casper/sdk/model/entity/EntityAddr.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.casper.sdk.model.entity;

import com.casper.sdk.exception.NoSuchKeyTagException;
import com.casper.sdk.model.key.KeyTag;
import com.casper.sdk.model.key.Tag;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -34,10 +33,10 @@ public static EntityAddr getByTag(byte tag) throws NoSuchKeyTagException {

public static EntityAddr getByKeyName(final String keyName) throws NoSuchKeyTagException {
// Search in reverse order to get the most specific key eg 'bid-addr-' and 'bid-'
for (final EntityAddr entityAddr: values()) {
if (entityAddr.getKeyName().equals(keyName)) {
return entityAddr;
}
for (final EntityAddr entityAddr : values()) {
if (entityAddr.getKeyName().equals(keyName)) {
return entityAddr;
}
}
throw new NoSuchKeyTagException("No such key name: " + keyName);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/casper/sdk/model/key/AccountHashKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.casper.sdk.model.key;

/**
* A key for an account hash
*
* @author [email protected]
*/
public class AccountHashKey extends Key {
}
3 changes: 2 additions & 1 deletion src/main/java/com/casper/sdk/model/key/ByteCodeKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ protected void fromStringCustom(final String strKey) {
final byte[] key = new byte[33];
final String[] split = strKey.split("-");
key[0] = byteCodeAddr.getByteTag();
System.arraycopy(Hex.decode(split[split.length - 1]), 0, key, 1, 32);
final byte[] decode = Hex.decode(split[split.length - 1]);
System.arraycopy(decode, 0, key, 1, decode.length);
setKey(key);
}

Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/casper/sdk/model/key/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.casper.sdk.exception.NoSuchKeyTagException;
import com.casper.sdk.jackson.deserializer.KeyDeserializer;
import com.casper.sdk.jackson.serializer.KeySerializer;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.syntifi.crypto.key.hash.Blake2b;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.util.ByteUtils;
Expand All @@ -22,6 +24,7 @@
* @since 0.0.1
*/
@JsonDeserialize(using = KeyDeserializer.class)
@JsonSerialize(using = KeySerializer.class)
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Key extends AbstractSerializedKeyTaggedHex<KeyTag> {
Expand All @@ -40,7 +43,8 @@ public static Key deserialize(final DeserializerBuffer deser) throws NoSuchKeyTa
}
}

public static Key fromKeyString(final String strKey) throws NoSuchKeyTagException {
@JsonCreator
public static Key create(final String strKey) throws NoSuchKeyTagException {
final KeyTag keyTag = KeyTag.getByKeyName(strKey);
try {
final Key key = keyTag.getKeyClass().getDeclaredConstructor().newInstance();
Expand All @@ -67,13 +71,6 @@ public String generateAccountHash(final boolean includePrefix) throws IOExceptio
return (includePrefix ? "account-hash-" : "") + ByteUtils.encodeHexString(Blake2b.digest(byteArrayOutputStream.toByteArray(), 32));
}

@JsonCreator
public void createKey(final String key) throws NoSuchKeyTagException {
Key obj = Key.fromTaggedHexString(key);
this.setTag(obj.getTag());
this.setKey(obj.getKey());
}

@Override
public String toString() {
return getTag().getKeyName() + ByteUtils.encodeHexString(this.getKey());
Expand All @@ -87,5 +84,4 @@ protected void fromStringCustom(final String strKey) {
final String[] split = strKey.split("-");
this.setKey(ByteUtils.parseHexString(split[split.length - 1]));
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/casper/sdk/model/key/KeyTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
@Getter
public enum KeyTag implements Tag {
ACCOUNT((byte) 0x00, Key.class, "account-hash-"),
ACCOUNT((byte) 0x00, AccountHashKey.class, "account-hash-"),
HASH((byte) 0x01, Key.class, "hash-", "contract-"),
UREF((byte) 0x02, URefKey.class, "uref-"),
TRANSFER((byte) 0x03, Key.class, "transfer-"),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/casper/sdk/model/key/NamedKeyKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected void fromStringCustom(final String strKey) {
final String[] split = strKey.split("-");
try {
final String baseAddrStr = split[2] + "-" + split[3] + "-" + split[4];
baseAddr = (AddressableEntityKey) Key.fromKeyString(baseAddrStr);
baseAddr = (AddressableEntityKey) Key.create(baseAddrStr);
stringBytes = Hex.decode(split[5]);
refreshKey();
} catch (NoSuchKeyTagException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.casper.sdk.model.transaction.execution;

import com.casper.sdk.model.key.Key;
import com.casper.sdk.model.transaction.kind.Kind;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
Expand All @@ -16,9 +17,8 @@
@Builder
public class Effect {

// TODO convert EntityAddr
@JsonProperty("key")
private String key;
private Key key;
@JsonProperty("kind")
private Kind kind;

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/casper/sdk/model/transfer/TransferV1.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.casper.sdk.model.transfer;

import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
import com.casper.sdk.model.key.AccountHashKey;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
Expand All @@ -25,11 +26,11 @@ public class TransferV1 {

/** Hex-encoded account hash. */
@JsonProperty("to")
private String to;
private AccountHashKey to;

/** Hex-encoded account hash. */
@JsonProperty("from")
private String from;
private AccountHashKey from;

/** Amount transferred */
@JsonIgnore
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/HowTo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.casper.sdk.model.entity.AddressableEntity;
import com.casper.sdk.model.entity.StateEntityResult;
import com.casper.sdk.model.era.EraInfoData;
import com.casper.sdk.model.key.Key;
import com.casper.sdk.model.key.PublicKey;
import com.casper.sdk.model.reward.GetRewardResult;
import com.casper.sdk.model.stateroothash.StateRootHashData;
Expand Down Expand Up @@ -288,8 +289,8 @@ void getStateEntity() {
assert stateEntityPublicKey.getEntity() != null;

//By contract identifier
final String contractKey = ((AddressableEntity) stateEntityPublicKey.getEntity()).getNamedKeys().get(0).getKey();
final StateEntityResult stateEntityContract = casperService.getStateEntity(new EntityAddrIdentifier(contractKey), null);
final Key contractKey = ((AddressableEntity) stateEntityPublicKey.getEntity()).getNamedKeys().get(0).getKey();
final StateEntityResult stateEntityContract = casperService.getStateEntity(new EntityAddrIdentifier(contractKey.toString()), null);
assert stateEntityContract.getEntity() != null;

}
Expand Down
Loading

0 comments on commit 693da34

Please sign in to comment.