@@ -7,7 +7,7 @@ use nillion_nucs::{
77 k256:: {
88 ecdsa:: { signature:: Signer , Signature , SigningKey } ,
99 sha2:: { Digest , Sha256 } ,
10- PublicKey , SecretKey ,
10+ SecretKey ,
1111 } ,
1212 token:: { Did , ProofHash , TokenBody } ,
1313} ;
@@ -44,7 +44,7 @@ pub trait NilauthClient {
4444 async fn pay_subscription (
4545 & self ,
4646 payments_client : & mut NillionChainClient ,
47- key : & PublicKey ,
47+ key : & SecretKey ,
4848 ) -> Result < TxHash , PaySubscriptionError > ;
4949
5050 /// Get our subscription status.
@@ -88,6 +88,9 @@ pub enum PaySubscriptionError {
8888 #[ error( "fetching subscription cost: {0}" ) ]
8989 Cost ( #[ from] SubscriptionCostError ) ,
9090
91+ #[ error( "fetching subscription status: {0}" ) ]
92+ Status ( #[ from] SubscriptionStatusError ) ,
93+
9194 #[ error( "serde: {0}" ) ]
9295 Serde ( #[ from] serde_json:: Error ) ,
9396
@@ -103,6 +106,9 @@ pub enum PaySubscriptionError {
103106 #[ error( "server could not validate payment: {tx_hash}" ) ]
104107 PaymentValidation { tx_hash : TxHash , payload : String } ,
105108
109+ #[ error( "cannot renew subscription before {0}" ) ]
110+ CannotRenewYet ( DateTime < Utc > ) ,
111+
106112 #[ error( "request: {0:?}" ) ]
107113 Request ( RequestError ) ,
108114}
@@ -279,8 +285,15 @@ impl NilauthClient for DefaultNilauthClient {
279285 async fn pay_subscription (
280286 & self ,
281287 payments_client : & mut NillionChainClient ,
282- key : & PublicKey ,
288+ key : & SecretKey ,
283289 ) -> Result < TxHash , PaySubscriptionError > {
290+ let subscription = self . subscription_status ( key) . await ?;
291+ match subscription. details {
292+ Some ( details) if details. renewable_at > Utc :: now ( ) => {
293+ return Err ( PaySubscriptionError :: CannotRenewYet ( details. renewable_at ) ) ;
294+ }
295+ _ => ( ) ,
296+ } ;
284297 let about = self . about ( ) . await ?;
285298 let cost = self . subscription_cost ( ) . await ?;
286299 let payload = ValidatePaymentRequestPayload { nonce : rand:: random ( ) , service_public_key : about. public_key } ;
@@ -291,7 +304,8 @@ impl NilauthClient for DefaultNilauthClient {
291304 . await
292305 . map_err ( |e| PaySubscriptionError :: Payment ( e. to_string ( ) ) ) ?;
293306
294- let public_key = key. to_sec1_bytes ( ) . as_ref ( ) . try_into ( ) . map_err ( |_| PaySubscriptionError :: InvalidPublicKey ) ?;
307+ let public_key =
308+ key. public_key ( ) . to_sec1_bytes ( ) . as_ref ( ) . try_into ( ) . map_err ( |_| PaySubscriptionError :: InvalidPublicKey ) ?;
295309 let url = self . make_url ( "/api/v1/payments/validate" ) ;
296310 let request =
297311 ValidatePaymentRequest { tx_hash : tx_hash. clone ( ) , payload : payload. as_bytes ( ) . to_vec ( ) , public_key } ;
@@ -511,4 +525,8 @@ pub struct SubscriptionDetails {
511525 /// The timestamp at which the subscription expires.
512526 #[ serde( with = "chrono::serde::ts_seconds" ) ]
513527 pub expires_at : DateTime < Utc > ,
528+
529+ /// The timestamp at which the subscription can be renewed.
530+ #[ serde( with = "chrono::serde::ts_seconds" ) ]
531+ pub renewable_at : DateTime < Utc > ,
514532}
0 commit comments