Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expiry type #301

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion concordium-android-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../concordium-android-sdk-examples/app/libs</outputDirectory>
<outputDirectory>${basedir}/../concordium-android-wallet-example/app/libs</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target</directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import com.concordium.sdk.crypto.wallet.identityobject.IdentityObject
import com.concordium.sdk.responses.accountinfo.credential.AttributeType
import com.concordium.sdk.transactions.CredentialRegistrationId
import com.concordium.sdk.transactions.Index
import com.concordium.sdk.transactions.TransactionExpiry
import com.concordium.sdk.types.AccountAddress
import com.concordium.example.wallet.Constants
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
import java.util.Date


class IdentityActivity : ComponentActivity() {
Expand All @@ -55,7 +55,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 +74,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 @@ -11,93 +13,94 @@
*/
@EqualsAndHashCode
public final class Expiry {

public static final int BYTES = UInt64.BYTES;
private final Timestamp timestampInMillis;

private Expiry(Timestamp value) {
this.timestampInMillis = value;
// The expiry in seconds since unix epoch.
@JsonProperty
private final UInt64 expiry;

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

private static final int MILLISECONDS_PER_SECOND = 1000;
private static final int SECONDS_PER_MINUTE = 60;

/**
* Create a new `Expiry` with current offset added the amount of minutes.
*
* @param minutes minutes to add.
* The amount of minutes must be positive.
* The amount of minutes must be strictly positive.
* @return The Expiry with the added minutes.
*/
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(this.expiry.getValue() + (minutes * SECONDS_PER_MINUTE));
}

/**
* Create a new `Expiry` with current offset added the amount of seconds.
*
* @param seconds seconds to add.
* The amount of seconds provided must be positive.
* The amount of seconds provided must be strictly positive.
* @return The Expiry with the added minutes.
*/
public Expiry addSeconds(int seconds) {
if (seconds < 0) {
if (seconds < 1) {
throw new IllegalArgumentException("Seconds must be positive.");
}
return Expiry.from(Timestamp.newMillis(this.timestampInMillis.getMillis() + (long) seconds * MILLISECONDS_PER_SECOND));
return Expiry.from(this.expiry.getValue() + seconds);
}

/**
* Create an `Expiry` from a raw unix timestamp.
*
* Create an `Expiry` from a raw unix timestamp in seconds.
* @param value the raw unix timestamp i.e., seconds since unix epoch.
* @return the Expiry
*/
public static Expiry from(long value) {
if (value == 0) {
throw new IllegalArgumentException("Expiry cannot be zero");
}
return new Expiry(Timestamp.newSeconds(value));
return Expiry.from(Timestamp.newSeconds(value));
}

/**
* Create a new `Expiry` with an offset of the current time.
*
* @return the Expiry
*/
public static Expiry createNew() {
return Expiry.from(Timestamp.newMillis(System.currentTimeMillis()));
}

/**
* Create an `Expiry` from a {@link Date}
*
* Create an `Expiry` from a {@link Date}. Note that there is a loss of precision
* when using this as a {@link Date} holds milliseconds and the internal
* value of a {@link Expiry} is in seconds.
* @param date the date
* @return the expiry
*/
public static Expiry from(Date date) {
return new Expiry(Timestamp.newMillis(date.getTime()));
return Expiry.from(Timestamp.newMillis(date.getTime()));
}

/**
* Create an `Expiry` from a {@link Timestamp}
*
* Create an `Expiry` from a {@link Timestamp}. Note that there is a loss of precision
* when using this as a {@link Timestamp} holds milliseconds and the internal
* value of a {@link Expiry} is in seconds.
* @param timestamp the timestamp
* @return the expiry
*/
public static Expiry from(Timestamp timestamp) {
return new Expiry(timestamp);
return new Expiry(UInt64.from(timestamp.getMillis() / MILLISECONDS_PER_SECOND));
}

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

@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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.concordium.sdk.transactions;

import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Test;

import com.concordium.sdk.types.Timestamp;

public class ExpiryTest {

@Test
public void testExpiryAddMinutes() {
Expiry expiry = Expiry.from(1);
long secondsIn5Minutes = 300;

Expiry result = expiry.addMinutes(5);

assertEquals(expiry.getValue().getValue() + secondsIn5Minutes, result.getValue().getValue());
}

@Test
public void testExpiryAddSeconds() {
Expiry expiry = Expiry.from(1);
int secondsToAdd = 1664115;

Expiry result = expiry.addSeconds(secondsToAdd);

assertEquals(expiry.getValue().getValue() + secondsToAdd, result.getValue().getValue());
}

@Test
public void testExpiryFromSeconds() {
long value = 1513;
Expiry expiry = Expiry.from(value);

assertEquals(value, expiry.getValue().getValue());
}

@Test
public void testExpiryFromDate() {
long valueInSeconds = 15605;
Date date = new Date(valueInSeconds * 1000);

Expiry expiry = Expiry.from(date);

assertEquals(valueInSeconds, expiry.getValue().getValue());
}

@Test
public void testExpiryFromTimestamp() {
long valueInSeconds = 51356;
Timestamp timestamp = Timestamp.newSeconds(valueInSeconds);

Expiry expiry = Expiry.from(timestamp);

assertEquals(valueInSeconds, expiry.getValue().getValue());
}
}
Loading