From 6c81ce3458b11384902715c41306f89085c81416 Mon Sep 17 00:00:00 2001 From: hopeyen Date: Mon, 26 Feb 2024 17:35:57 -0600 Subject: [PATCH] fix: receipt type and headers --- file-exchange/src/download_client/mod.rs | 21 +++++++++------------ file-exchange/src/download_client/signer.rs | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/file-exchange/src/download_client/mod.rs b/file-exchange/src/download_client/mod.rs index 73226ad..251f2d4 100644 --- a/file-exchange/src/download_client/mod.rs +++ b/file-exchange/src/download_client/mod.rs @@ -18,22 +18,19 @@ use std::sync::{Arc, Mutex as StdMutex}; use std::time::Duration; use tokio::sync::Mutex; -use crate::{config::DownloaderArgs, discover, graphql}; -use crate::{config::OnChainArgs, errors::Error, transaction_manager::TransactionManager}; use crate::{ + config::{DownloaderArgs, OnChainArgs}, discover::{Finder, ServiceEndpoint}, - download_client::signer::Access, -}; -use crate::{ - graphql::escrow_query::escrow_balance, + errors::Error, + graphql::{allocation_id, escrow_query::escrow_balance}, manifest::{ file_hasher::verify_chunk, ipfs::IpfsClient, manifest_fetcher::read_bundle, Bundle, FileManifestMeta, }, + transaction_manager::TransactionManager, + util::build_wallet, }; -use crate::util::build_wallet; - use self::signer::ReceiptSigner; pub mod signer; @@ -303,10 +300,11 @@ impl Downloader { PaymentMethod::PaidQuery(signer) => { let receipt = signer .receipt_signer - .create_receipt(graphql::allocation_id(receiver), &discover::Finder::fees()) + .create_receipt(allocation_id(receiver), &Finder::fees()) .await?; Ok(( - HeaderName::from_str("Scalar-Receipt").unwrap(), + // HeaderName::from_str("Scalar-Receipt").unwrap(), + HeaderName::from_str("scalar-receipt").unwrap(), receipt.serialize(), )) } @@ -489,11 +487,10 @@ impl Downloader { if (estimated_buying_power_in_bytes as u64) < total_bytes { let msg = format!("Balance is not enough to purchase {} bytes, look at the error message to see top-up recommendations for each account", estimated_buying_power_in_bytes); return Err(Error::PricingError(msg)); - } else { - tracing::info!("Balance is enough to purchase the file, go ahead to download"); } }; + tracing::info!("Balance is enough to purchase the file, go ahead to download"); Ok(()) } } diff --git a/file-exchange/src/download_client/signer.rs b/file-exchange/src/download_client/signer.rs index b9d8c21..7c13df1 100644 --- a/file-exchange/src/download_client/signer.rs +++ b/file-exchange/src/download_client/signer.rs @@ -51,7 +51,7 @@ impl ReceiptSigner { &self, allocation_id: Address, fee: &GRT, - ) -> Result { + ) -> Result { let nonce = rand::thread_rng().next_u64(); let timestamp_ns = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) @@ -69,5 +69,24 @@ impl ReceiptSigner { EIP712SignedMessage::new(&self.domain, receipt, &wallet) .await .map_err(|e| Error::ContractError(e.to_string())) + .map(ScalarReceipt::TAP) + } +} + +pub enum ScalarReceipt { + TAP(EIP712SignedMessage), +} + +impl ScalarReceipt { + pub fn allocation(&self) -> Address { + match self { + ScalarReceipt::TAP(receipt) => receipt.message.allocation_id, + } + } + + pub fn serialize(&self) -> String { + match self { + ScalarReceipt::TAP(receipt) => serde_json::to_string(&receipt).unwrap(), + } } }