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

File tree

22 files changed

+192
-147
lines changed

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
}

0 commit comments

Comments
 (0)