PaymentSession: migrate getters to java.time API

This commit is contained in:
Andreas Schildbach 2023-03-10 12:21:39 +01:00
parent 95776f28e3
commit be0b2b46b8
4 changed files with 36 additions and 15 deletions

View file

@ -44,9 +44,11 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.KeyStoreException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/**
@ -253,27 +255,44 @@ public class PaymentSession {
}
/**
* Returns the date that the payment request was generated.
* Returns the time that the payment request was generated.
*/
public Instant getTime() {
return Instant.ofEpochSecond(paymentDetails.getTime());
}
/** @deprecated use {@link #getTime()} */
@Deprecated
public Date getDate() {
return new Date(paymentDetails.getTime() * 1000);
return Date.from(getTime());
}
/**
* Returns the expires time of the payment request, or null if none.
* Returns the expires time of the payment request, or empty if none.
*/
@Nullable public Date getExpires() {
public Optional<Instant> getExpiresInstant() {
if (paymentDetails.hasExpires())
return new Date(paymentDetails.getExpires() * 1000);
return Optional.of(Instant.ofEpochSecond(paymentDetails.getExpires()));
else
return null;
return Optional.empty();
}
/** @deprecated use {@link #getExpiresInstant()} */
@Nullable
@Deprecated
public Date getExpires() {
return getExpiresInstant()
.map(Date::from)
.orElse(null);
}
/**
* This should always be called before attempting to call sendPayment.
*/
public boolean isExpired() {
return paymentDetails.hasExpires() && TimeUtils.currentTimeSeconds() > paymentDetails.getExpires();
return getExpiresInstant()
.map(time -> TimeUtils.currentTime().isAfter(time))
.orElse(false);
}
/**

View file

@ -39,6 +39,8 @@ import org.junit.Test;
import java.io.InputStream;
import java.net.URL;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
@ -59,7 +61,7 @@ public class PaymentSessionTest {
private static final String paymentRequestMemo = "send coinz noa plz kthx";
private static final String paymentMemo = "take ze coinz";
private static final ByteString merchantData = ByteString.copyFromUtf8("merchant data");
private static final long time = TimeUtils.currentTimeSeconds();
private static final Instant time = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
private ECKey serverKey;
private Transaction tx;
private TransactionOutput outputToMe;
@ -80,7 +82,7 @@ public class PaymentSessionTest {
assertEquals(paymentRequestMemo, paymentSession.getMemo());
assertEquals(amount, paymentSession.getValue());
assertEquals(simplePaymentUrl, paymentSession.getPaymentUrl());
assertTrue(new Date(time * 1000L).equals(paymentSession.getDate()));
assertEquals(time, paymentSession.getTime());
assertTrue(paymentSession.getSendRequest().tx.equals(tx));
assertFalse(paymentSession.isExpired());
@ -108,7 +110,7 @@ public class PaymentSessionTest {
Protos.Output.Builder outputBuilder = Protos.Output.newBuilder()
.setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
.setTime(time)
.setTime(time.getEpochSecond())
.addOutputs(outputBuilder)
.build();
Protos.PaymentRequest paymentRequest = Protos.PaymentRequest.newBuilder()
@ -185,7 +187,7 @@ public class PaymentSessionTest {
.setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
.setNetwork(netID)
.setTime(time)
.setTime(time.getEpochSecond())
.setPaymentUrl(simplePaymentUrl)
.addOutputs(outputBuilder)
.setMemo(paymentRequestMemo)
@ -204,8 +206,8 @@ public class PaymentSessionTest {
.setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
Protos.PaymentDetails paymentDetails = Protos.PaymentDetails.newBuilder()
.setNetwork("test")
.setTime(time - 10)
.setExpires(time - 1)
.setTime(time.minusSeconds(10).getEpochSecond())
.setExpires(time.minusSeconds(1).getEpochSecond())
.setPaymentUrl(simplePaymentUrl)
.addOutputs(outputBuilder)
.setMemo(paymentRequestMemo)

View file

@ -71,7 +71,7 @@ public class PaymentProtocolTool {
}
final int version = session.getPaymentRequest().getPaymentDetailsVersion();
StringBuilder output = new StringBuilder(
format("Bitcoin payment request, version %d%nDate: %s%n", version, session.getDate()));
format("Bitcoin payment request, version %d%nDate: %s%n", version, session.getTime()));
PaymentProtocol.PkiVerificationData pki = PaymentProtocol.verifyPaymentRequestPki(
session.getPaymentRequest(), new TrustStoreLoader.DefaultTrustStoreLoader().getKeyStore());
if (pki != null) {

View file

@ -843,7 +843,7 @@ public class WalletTool implements Callable<Integer> {
private void send(PaymentSession session) {
System.out.println("Payment Request");
System.out.println("Coin: " + session.getValue().toFriendlyString());
System.out.println("Date: " + session.getDate());
System.out.println("Date: " + session.getTime());
System.out.println("Memo: " + session.getMemo());
if (session.pkiVerificationData != null) {
System.out.println("Pki-Verified Name: " + session.pkiVerificationData.displayName);