Skip to content

Commit 693da34

Browse files
authored
Merge pull request #366 from /issues/349
Issues/349 - Refactor of keys from String to Key class
2 parents 41a50b2 + a390a24 commit 693da34

22 files changed

+192
-147
lines changed

src/main/java/com/casper/sdk/jackson/deserializer/AbstractSerializedKeyTaggedHexDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public T deserialize(final JsonParser p, final DeserializationContext ctxt) thro
3838
if (strKey.contains("-")) {
3939
try {
4040
//noinspection unchecked
41-
return (T) Key.fromKeyString(strKey);
41+
return (T) Key.create(strKey);
4242
} catch (NoSuchKeyTagException e) {
4343
throw new CasperClientException("No such key: " + strKey, e);
4444
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.casper.sdk.jackson.serializer;
2+
3+
import com.casper.sdk.model.key.Key;
4+
import com.fasterxml.jackson.core.JsonGenerator;
5+
import com.fasterxml.jackson.databind.JsonSerializer;
6+
import com.fasterxml.jackson.databind.SerializerProvider;
7+
8+
import java.io.IOException;
9+
10+
/**
11+
* Desrializer for {@link Key} types.
12+
*
13+
14+
*/
15+
public class KeySerializer extends JsonSerializer<Key> {
16+
@Override
17+
public void serialize(final Key key, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
18+
gen.writeString(key.toString());
19+
}
20+
}

src/main/java/com/casper/sdk/model/account/Account.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.casper.sdk.model.account;
22

33
import com.casper.sdk.model.contract.NamedKey;
4+
import com.casper.sdk.model.key.AccountHashKey;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56
import lombok.AllArgsConstructor;
67
import lombok.Builder;
@@ -28,7 +29,7 @@ public class Account {
2829
* account_hash(String) Hex-encoded account hash.
2930
*/
3031
@JsonProperty("account_hash")
31-
private String hash;
32+
private AccountHashKey hash;
3233

3334
/**
3435
* {@link ActionThresholds} that have to be met

src/main/java/com/casper/sdk/model/account/AssociatedKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.casper.sdk.model.account;
22

3+
import com.casper.sdk.model.key.AccountHashKey;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import lombok.AllArgsConstructor;
56
import lombok.Builder;
@@ -25,7 +26,7 @@ public class AssociatedKey {
2526
* account_hash(String) Hex-encoded account hash.
2627
*/
2728
@JsonProperty("account_hash")
28-
private String accountHash;
29+
private AccountHashKey accountHash;
2930

3031
/**
3132
* weight(Integer)

src/main/java/com/casper/sdk/model/contract/NamedKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.casper.sdk.model.contract;
22

3+
import com.casper.sdk.model.key.Key;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import lombok.AllArgsConstructor;
56
import lombok.Builder;
@@ -25,7 +26,7 @@ public class NamedKey {
2526
* key(String) The value of the entry: a casper `Key` type.
2627
*/
2728
@JsonProperty("key")
28-
private String key;
29+
private Key key;
2930

3031
/**
3132
* name(String) The name of the entry.

src/main/java/com/casper/sdk/model/deploy/Operation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.casper.sdk.model.deploy;
22

3+
import com.casper.sdk.model.key.Key;
34
import lombok.AllArgsConstructor;
45
import lombok.Builder;
56
import lombok.Getter;
@@ -23,7 +24,7 @@ public class Operation {
2324
/**
2425
* The formatted string of the `Key`
2526
*/
26-
private String key;
27+
private Key key;
2728

2829
/**
2930
* @see OpKind

src/main/java/com/casper/sdk/model/deploy/executionresult/Success.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.casper.sdk.model.deploy.executionresult;
22

33
import com.casper.sdk.model.deploy.ExecutionEffect;
4+
import com.casper.sdk.model.key.Key;
45
import com.fasterxml.jackson.annotation.JsonTypeName;
56
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
67
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
@@ -30,7 +31,7 @@ public class Success {
3031
private ExecutionEffect effect;
3132

3233
/** List of Hex-encoded transfer address. */
33-
private List<String> transfers;
34+
private List<Key> transfers;
3435

3536
/** The cost of executing the deploy. */
3637
@JsonSerialize(using = ToStringSerializer.class)

src/main/java/com/casper/sdk/model/entity/Account.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.casper.sdk.model.entity;
22

3-
import com.fasterxml.jackson.annotation.JsonProperty;
3+
import com.casper.sdk.exception.NoSuchKeyTagException;
4+
import com.casper.sdk.model.key.AccountHashKey;
5+
import com.casper.sdk.model.key.Key;
6+
import com.fasterxml.jackson.annotation.JsonTypeName;
7+
import com.fasterxml.jackson.annotation.JsonValue;
48
import lombok.*;
59

610
/**
@@ -13,9 +17,13 @@
1317
@Builder
1418
@NoArgsConstructor
1519
@AllArgsConstructor
20+
@JsonTypeName("Account")
1621
public class Account implements EntityAddressKind {
1722

18-
@JsonProperty("Account")
19-
private String account;
23+
@JsonValue
24+
private AccountHashKey account;
2025

26+
public Account(final String accountSt) throws NoSuchKeyTagException {
27+
this.account = (AccountHashKey) Key.create(accountSt);
28+
}
2129
}

src/main/java/com/casper/sdk/model/entity/AddressableEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ public class AddressableEntity implements StateEntity {
3030
/** The entry points of the addressable entity. */
3131
@JsonProperty("entry_points")
3232
private List<EntryPointValue> entryPoints;
33-
3433
}

src/main/java/com/casper/sdk/model/entity/EntityAddr.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.casper.sdk.model.entity;
22

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

3534
public static EntityAddr getByKeyName(final String keyName) throws NoSuchKeyTagException {
3635
// Search in reverse order to get the most specific key eg 'bid-addr-' and 'bid-'
37-
for (final EntityAddr entityAddr: values()) {
38-
if (entityAddr.getKeyName().equals(keyName)) {
39-
return entityAddr;
40-
}
36+
for (final EntityAddr entityAddr : values()) {
37+
if (entityAddr.getKeyName().equals(keyName)) {
38+
return entityAddr;
39+
}
4140
}
4241
throw new NoSuchKeyTagException("No such key name: " + keyName);
4342
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.casper.sdk.model.key;
2+
3+
/**
4+
* A key for an account hash
5+
*
6+
7+
*/
8+
public class AccountHashKey extends Key {
9+
}

src/main/java/com/casper/sdk/model/key/ByteCodeKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ protected void fromStringCustom(final String strKey) {
3333
final byte[] key = new byte[33];
3434
final String[] split = strKey.split("-");
3535
key[0] = byteCodeAddr.getByteTag();
36-
System.arraycopy(Hex.decode(split[split.length - 1]), 0, key, 1, 32);
36+
final byte[] decode = Hex.decode(split[split.length - 1]);
37+
System.arraycopy(decode, 0, key, 1, decode.length);
3738
setKey(key);
3839
}
3940

src/main/java/com/casper/sdk/model/key/Key.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.casper.sdk.exception.NoSuchKeyTagException;
44
import com.casper.sdk.jackson.deserializer.KeyDeserializer;
5+
import com.casper.sdk.jackson.serializer.KeySerializer;
56
import com.fasterxml.jackson.annotation.JsonCreator;
67
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
8+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
79
import com.syntifi.crypto.key.hash.Blake2b;
810
import dev.oak3.sbs4j.DeserializerBuffer;
911
import dev.oak3.sbs4j.util.ByteUtils;
@@ -22,6 +24,7 @@
2224
* @since 0.0.1
2325
*/
2426
@JsonDeserialize(using = KeyDeserializer.class)
27+
@JsonSerialize(using = KeySerializer.class)
2528
@NoArgsConstructor
2629
@EqualsAndHashCode(callSuper = true)
2730
public class Key extends AbstractSerializedKeyTaggedHex<KeyTag> {
@@ -40,7 +43,8 @@ public static Key deserialize(final DeserializerBuffer deser) throws NoSuchKeyTa
4043
}
4144
}
4245

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

70-
@JsonCreator
71-
public void createKey(final String key) throws NoSuchKeyTagException {
72-
Key obj = Key.fromTaggedHexString(key);
73-
this.setTag(obj.getTag());
74-
this.setKey(obj.getKey());
75-
}
76-
7774
@Override
7875
public String toString() {
7976
return getTag().getKeyName() + ByteUtils.encodeHexString(this.getKey());
@@ -87,5 +84,4 @@ protected void fromStringCustom(final String strKey) {
8784
final String[] split = strKey.split("-");
8885
this.setKey(ByteUtils.parseHexString(split[split.length - 1]));
8986
}
90-
9187
}

src/main/java/com/casper/sdk/model/key/KeyTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
@Getter
1919
public enum KeyTag implements Tag {
20-
ACCOUNT((byte) 0x00, Key.class, "account-hash-"),
20+
ACCOUNT((byte) 0x00, AccountHashKey.class, "account-hash-"),
2121
HASH((byte) 0x01, Key.class, "hash-", "contract-"),
2222
UREF((byte) 0x02, URefKey.class, "uref-"),
2323
TRANSFER((byte) 0x03, Key.class, "transfer-"),

src/main/java/com/casper/sdk/model/key/NamedKeyKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected void fromStringCustom(final String strKey) {
3232
final String[] split = strKey.split("-");
3333
try {
3434
final String baseAddrStr = split[2] + "-" + split[3] + "-" + split[4];
35-
baseAddr = (AddressableEntityKey) Key.fromKeyString(baseAddrStr);
35+
baseAddr = (AddressableEntityKey) Key.create(baseAddrStr);
3636
stringBytes = Hex.decode(split[5]);
3737
refreshKey();
3838
} catch (NoSuchKeyTagException e) {

src/main/java/com/casper/sdk/model/transaction/execution/Effect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.casper.sdk.model.transaction.execution;
22

3+
import com.casper.sdk.model.key.Key;
34
import com.casper.sdk.model.transaction.kind.Kind;
45
import com.fasterxml.jackson.annotation.JsonProperty;
56
import lombok.*;
@@ -16,9 +17,8 @@
1617
@Builder
1718
public class Effect {
1819

19-
// TODO convert EntityAddr
2020
@JsonProperty("key")
21-
private String key;
21+
private Key key;
2222
@JsonProperty("kind")
2323
private Kind kind;
2424

src/main/java/com/casper/sdk/model/transfer/TransferV1.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.casper.sdk.model.transfer;
22

33
import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
4+
import com.casper.sdk.model.key.AccountHashKey;
45
import com.fasterxml.jackson.annotation.JsonIgnore;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67
import lombok.*;
@@ -25,11 +26,11 @@ public class TransferV1 {
2526

2627
/** Hex-encoded account hash. */
2728
@JsonProperty("to")
28-
private String to;
29+
private AccountHashKey to;
2930

3031
/** Hex-encoded account hash. */
3132
@JsonProperty("from")
32-
private String from;
33+
private AccountHashKey from;
3334

3435
/** Amount transferred */
3536
@JsonIgnore

src/test/java/com/HowTo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.casper.sdk.model.entity.AddressableEntity;
2222
import com.casper.sdk.model.entity.StateEntityResult;
2323
import com.casper.sdk.model.era.EraInfoData;
24+
import com.casper.sdk.model.key.Key;
2425
import com.casper.sdk.model.key.PublicKey;
2526
import com.casper.sdk.model.reward.GetRewardResult;
2627
import com.casper.sdk.model.stateroothash.StateRootHashData;
@@ -288,8 +289,8 @@ void getStateEntity() {
288289
assert stateEntityPublicKey.getEntity() != null;
289290

290291
//By contract identifier
291-
final String contractKey = ((AddressableEntity) stateEntityPublicKey.getEntity()).getNamedKeys().get(0).getKey();
292-
final StateEntityResult stateEntityContract = casperService.getStateEntity(new EntityAddrIdentifier(contractKey), null);
292+
final Key contractKey = ((AddressableEntity) stateEntityPublicKey.getEntity()).getNamedKeys().get(0).getKey();
293+
final StateEntityResult stateEntityContract = casperService.getStateEntity(new EntityAddrIdentifier(contractKey.toString()), null);
293294
assert stateEntityContract.getEntity() != null;
294295

295296
}

0 commit comments

Comments
 (0)