Skip to content

Commit

Permalink
Fix usage of wrong expiry type in credential deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
orhoj committed Jan 24, 2024
1 parent 8b22391 commit 8f4eabd
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import com.concordium.example.wallet.Requests
import com.concordium.example.wallet.Storage
import com.concordium.example.wallet.services.ConcordiumClientService
import com.concordium.example.wallet.ui.Container
import com.concordium.sdk.transactions.CredentialDeploymentTransaction
import com.concordium.sdk.transactions.Expiry
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.apache.commons.codec.binary.Hex
import java.util.Collections
Expand Down Expand Up @@ -55,7 +57,7 @@ class IdentityActivity : ComponentActivity() {
ipIdentity,
)

val expiry = TransactionExpiry.fromLong(Date().time + 360)
val expiry = Expiry.createNew().addMinutes(5)

val credentialDeploymentSignDigest = Credential.getCredentialDeploymentSignDigest(
CredentialDeploymentDetails(
Expand All @@ -74,8 +76,9 @@ class IdentityActivity : ComponentActivity() {
Collections.singletonMap(Index.from(0), Hex.encodeHexString(signature))
)
val credentialPayload = Credential.serializeCredentialDeploymentPayload(context)
val credentialDeploymentTransaction = CredentialDeploymentTransaction.from(expiry, credentialPayload)
ConcordiumClientService.getClient()
.sendCredentialDeploymentTransaction(expiry, credentialPayload)
.sendCredentialDeploymentTransaction(credentialDeploymentTransaction)

// Save the address of the account
val address =
Expand Down
4 changes: 2 additions & 2 deletions concordium-sdk/src/main/java/com/concordium/sdk/ClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ public Hash sendTransaction(final AccountTransaction accountTransaction) {
* @param rawPayload the serialized bytes of the credential deployment transaction, including its signatures
* @return Transaction {@link Hash}.
*/
public Hash sendCredentialDeploymentTransaction(TransactionExpiry expiry, byte[] rawPayload) {
val req = ClientV2MapperExtensions.to(expiry, rawPayload);
public Hash sendCredentialDeploymentTransaction(CredentialDeploymentTransaction credentialDeploymentTransaction) {
val req = ClientV2MapperExtensions.to(credentialDeploymentTransaction);
val grpcOutput = this.server().sendBlockItem(req);

return to(grpcOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import com.concordium.sdk.transactions.TransferPayload;
import com.concordium.sdk.transactions.TransferWithMemoPayload;
import com.concordium.sdk.transactions.*;
import com.concordium.sdk.transactions.UpdateContract;
import com.concordium.sdk.transactions.smartcontracts.WasmModule;
import com.concordium.sdk.transactions.smartcontracts.WasmModuleVersion;
import com.concordium.sdk.types.Timestamp;
Expand Down Expand Up @@ -871,14 +870,14 @@ static SendBlockItemRequest to(AccountTransaction accountTransaction) {
.build();
}

static SendBlockItemRequest to(TransactionExpiry expiry, byte[] payload) {
TransactionTime time = to(expiry.getExpiry());
static SendBlockItemRequest to(CredentialDeploymentTransaction credentialDeploymentTransaction) {
TransactionTime time = to(credentialDeploymentTransaction.getExpiry().getValue());

return SendBlockItemRequest.newBuilder()
.setCredentialDeployment(
CredentialDeployment.newBuilder()
.setMessageExpiry(time)
.setRawPayload(ByteString.copyFrom(payload))
.setRawPayload(ByteString.copyFrom(credentialDeploymentTransaction.getPayloadBytes()))
.build()
)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.concordium.sdk.crypto.wallet.credential;

import com.concordium.sdk.transactions.TransactionExpiry;
import com.concordium.sdk.transactions.Expiry;
import com.fasterxml.jackson.annotation.JsonUnwrapped;

import lombok.AllArgsConstructor;
Expand All @@ -16,6 +16,6 @@ public class CredentialDeploymentDetails {

@NonNull
@JsonUnwrapped
private final TransactionExpiry expiry;
private final Expiry expiry;

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public static CredentialDeploymentTransaction from(final UInt64 expiry, final by
Arrays.copyOf(payloadBytes, payloadBytes.length));
}

/**
* Creates an instance of {@link CredentialDeploymentTransaction}.
*
* @param expiry Indicates when this transaction should expire.
* @param payloadBytes Payload serialized as byte array.
* @return Instance of {@link CredentialDeploymentTransaction}.
*/
public static CredentialDeploymentTransaction from(final Expiry expiry, final byte[] payloadBytes) {
return new CredentialDeploymentTransaction(
expiry,
Arrays.copyOf(payloadBytes, payloadBytes.length));
}

@Override
@UnstableApi
byte[] getBlockItemBytes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.concordium.sdk.types.Timestamp;
import com.concordium.sdk.types.UInt64;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.EqualsAndHashCode;

import java.util.Date;
Expand All @@ -12,10 +14,11 @@
@EqualsAndHashCode
public final class Expiry {
public static final int BYTES = UInt64.BYTES;
private final Timestamp timestampInMillis;

private final Timestamp expiry;

private Expiry(Timestamp value) {
this.timestampInMillis = value;
this.expiry = value;
}

private static final int MILLISECONDS_PER_SECOND = 1000;
Expand All @@ -32,7 +35,7 @@ public Expiry addMinutes(int minutes) {
if (minutes < 1) {
throw new IllegalArgumentException("Minutes must be positive.");
}
return Expiry.from(Timestamp.newMillis(this.timestampInMillis.getMillis() + ((long) minutes * MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE)));
return Expiry.from(Timestamp.newMillis(this.expiry.getMillis() + ((long) minutes * MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE)));
}

/**
Expand All @@ -46,7 +49,7 @@ public Expiry addSeconds(int seconds) {
if (seconds < 0) {
throw new IllegalArgumentException("Seconds must be positive.");
}
return Expiry.from(Timestamp.newMillis(this.timestampInMillis.getMillis() + (long) seconds * MILLISECONDS_PER_SECOND));
return Expiry.from(Timestamp.newMillis(this.expiry.getMillis() + (long) seconds * MILLISECONDS_PER_SECOND));
}

/**
Expand Down Expand Up @@ -91,13 +94,14 @@ public static Expiry from(Timestamp timestamp) {
return new Expiry(timestamp);
}

UInt64 getValue() {
return UInt64.from(timestampInMillis.getMillis() / 1000);
@JsonProperty("expiry")
public UInt64 getValue() {
return UInt64.from(expiry.getMillis() / 1000);
}

@Override
public String toString() {
return timestampInMillis.toString();
return expiry.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.concordium.sdk.Constants;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import lombok.EqualsAndHashCode;
import lombok.Getter;

Expand All @@ -16,6 +18,7 @@
public class Timestamp {

@Getter
@JsonValue
private final long millis;

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -25,8 +24,8 @@
import com.concordium.sdk.responses.cryptographicparameters.CryptographicParameters;
import com.concordium.sdk.serializing.JsonMapper;
import com.concordium.sdk.transactions.CredentialPublicKeys;
import com.concordium.sdk.transactions.Expiry;
import com.concordium.sdk.transactions.Index;
import com.concordium.sdk.transactions.TransactionExpiry;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;

Expand Down Expand Up @@ -91,7 +90,7 @@ public void createUnsignedCredential() throws Exception {
.build();

UnsignedCredentialDeploymentInfoWithRandomness result = Credential.createUnsignedCredential(input);
TransactionExpiry expiry = TransactionExpiry.fromLong(new Date().getTime() + 360);
Expiry expiry = Expiry.createNew().addMinutes(5);

byte[] credentialDeploymentSignDigest = Credential.getCredentialDeploymentSignDigest(new CredentialDeploymentDetails(result.getUnsignedCdi(), expiry));
ED25519SecretKey signingKey = wallet.getAccountSigningKey(0, 0, credentialCounter);
Expand Down

0 comments on commit 8f4eabd

Please sign in to comment.