Contracts: replace the deprecated chrono calls

NaiveDateTime::from_timestamp_opt and DateTime::from_utc are deprecated
in favour of DateTime::from_timestamp.

Two call sites in contract::proofs converted a timestamp to a
NaiveDateTime and immediately back to the same i64; those now use the
value directly, which also removes an unwrap.
This commit is contained in:
jwinterm
2026-07-26 08:39:20 -04:00
parent 6c7026604a
commit 1f035e3ab8
5 changed files with 15 additions and 28 deletions
+2 -5
View File
@@ -43,7 +43,7 @@ use crate::{
SlatepackAddress, Slatepacker, SlatepackerArgs, TxLogEntryType, ViewWallet, WalletBackend,
WalletInitStatus, WalletInst, WalletLCProvider,
};
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::prelude::DateTime;
use ed25519_dalek::SigningKey as DalekSecretKey;
use ed25519_dalek::Verifier;
use ed25519_dalek::VerifyingKey as DalekPublicKey;
@@ -691,10 +691,7 @@ where
sender_address: Some(sender_address.to_ed25519()?),
receiver_address: a.pub_key,
promise_signature: None,
timestamp: DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
),
timestamp: DateTime::from_timestamp(0, 0).unwrap(),
memo: None,
});
+5 -9
View File
@@ -32,7 +32,7 @@ use crate::slate_versions::ser as dalek_ser;
use crate::types::{Context, NodeClient};
use crate::{address, Error};
use byteorder::{BigEndian, ByteOrder};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use ed25519_dalek::Signature as DalekSignature;
use ed25519_dalek::SigningKey as DalekSecretKey;
use ed25519_dalek::VerifyingKey as DalekPublicKey;
@@ -224,9 +224,7 @@ impl InvoiceProof {
};
let timestamp = match slate.payment_proof.as_ref() {
Some(p) => NaiveDateTime::from_timestamp_opt(p.timestamp.timestamp(), 0)
.unwrap()
.timestamp(),
Some(p) => p.timestamp.timestamp(),
None => 0,
};
@@ -380,8 +378,8 @@ where
// work tied to the early-payment-proof RFC.
let (invoice_proof, promise_signature, receiver_address) =
generate_invoice_signature(wallet, keychain_mask, slate, context, proof_args)?;
let timestamp = NaiveDateTime::from_timestamp_opt(Utc::now().timestamp(), 0).unwrap();
let timestamp = DateTime::<Utc>::from_utc(timestamp, Utc);
// Whole seconds only; the sub-second component is not serialized.
let timestamp = DateTime::from_timestamp(Utc::now().timestamp(), 0).unwrap();
let proof = PaymentInfo {
sender_address: proof_args.sender_address.clone(),
@@ -418,9 +416,7 @@ where
let recp_key =
address::address_from_derivation_path(&keychain, &parent_key_id, derivation_index)?;
invoice_proof.timestamp = NaiveDateTime::from_timestamp_opt(Utc::now().timestamp(), 0)
.unwrap()
.timestamp();
invoice_proof.timestamp = Utc::now().timestamp();
let (sig, addr) = invoice_proof.sign(&recp_key)?;
Ok((invoice_proof, sig, addr))
}
+2 -5
View File
@@ -28,7 +28,7 @@ use crate::grin_util::secp::key::{PublicKey, SecretKey};
use crate::grin_util::secp::pedersen::Commitment;
use crate::grin_util::secp::Signature;
use crate::grin_util::{secp, static_secp_instance};
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::prelude::{DateTime, Utc};
use ed25519_dalek::Signature as DalekSignature;
use ed25519_dalek::VerifyingKey as DalekPublicKey;
use serde::ser::{Serialize, Serializer};
@@ -1600,10 +1600,7 @@ impl From<&PaymentInfoV4> for PaymentInfo {
sender_address: Some(sender_address),
receiver_address,
promise_signature: receiver_signature,
timestamp: DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
),
timestamp: DateTime::from_timestamp(0, 0).unwrap(),
memo: None,
}
}
+2 -3
View File
@@ -155,7 +155,7 @@ pub mod tests {
use crate::{
slate, Error, Slate, Slatepacker, SlatepackerArgs, VersionedBinSlate, VersionedSlate,
};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use ed25519_dalek::Signature as DalekSignature;
use ed25519_dalek::VerifyingKey as DalekPublicKey;
use grin_core::global::{set_local_chain_type, ChainTypes};
@@ -229,8 +229,7 @@ pub mod tests {
let b = <&[u8; 32]>::try_from(b.as_slice()).unwrap();
let d_pkey = DalekPublicKey::from_bytes(b).unwrap();
// Need to remove milliseconds component for comparison. Won't be serialized
let ts = NaiveDateTime::from_timestamp_opt(Utc::now().timestamp(), 0).unwrap();
let ts = DateTime::<Utc>::from_utc(ts, Utc);
let ts = DateTime::from_timestamp(Utc::now().timestamp(), 0).unwrap();
let pm = PaymentMemo {
memo_type: 1,
memo: [9; 32],
+4 -6
View File
@@ -21,7 +21,7 @@ use crate::grin_keychain::BlindingFactor;
use crate::grin_util::secp::key::PublicKey;
use crate::grin_util::secp::pedersen::{Commitment, RangeProof};
use crate::grin_util::secp::Signature;
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
use ed25519_dalek::Signature as DalekSignature;
use ed25519_dalek::VerifyingKey as DalekPublicKey;
use std::convert::{TryFrom, TryInto};
@@ -259,11 +259,10 @@ impl Readable for ProofWrap {
Ok(v) => v,
Err(_) => return Err(grin_ser::Error::CorruptedData),
};
let ts_opt = match NaiveDateTime::from_timestamp_opt(ts_raw, 0) {
Some(o) => o,
let ts = match DateTime::from_timestamp(ts_raw, 0) {
Some(ts) => ts,
None => return Err(grin_ser::Error::CorruptedData),
};
let ts = DateTime::<Utc>::from_utc(ts_opt, Utc);
let psig = match reader.read_u8()? {
0 => None,
1 | _ => Some(
@@ -506,8 +505,7 @@ fn slate_v5_serialize_deserialize() {
let b = <&[u8; 32]>::try_from(b.as_slice()).unwrap();
let d_pkey = DalekPublicKey::from_bytes(b).unwrap();
// Need to remove milliseconds component for comparison. Won't be serialized
let ts = NaiveDateTime::from_timestamp_opt(Utc::now().timestamp(), 0).unwrap();
let ts = DateTime::<Utc>::from_utc(ts, Utc);
let ts = DateTime::from_timestamp(Utc::now().timestamp(), 0).unwrap();
let pm = PaymentMemoV5 {
memo_type: 0,
memo: [9; 32],