|
| 1 | +// Copyright 2023-, Semiotic AI, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use anyhow::anyhow; |
| 5 | +use tap_core::signed_message::EIP712SignedMessage; |
| 6 | + |
| 7 | +tonic::include_proto!("tap_aggregator.v1"); |
| 8 | + |
| 9 | +impl TryFrom<Receipt> for tap_core::receipt::Receipt { |
| 10 | + type Error = anyhow::Error; |
| 11 | + fn try_from(receipt: Receipt) -> Result<Self, Self::Error> { |
| 12 | + Ok(Self { |
| 13 | + allocation_id: receipt.allocation_id.as_slice().try_into()?, |
| 14 | + timestamp_ns: receipt.timestamp_ns, |
| 15 | + value: receipt.value.ok_or(anyhow!("Missing value"))?.into(), |
| 16 | + nonce: receipt.nonce, |
| 17 | + }) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl TryFrom<SignedReceipt> for tap_core::receipt::SignedReceipt { |
| 22 | + type Error = anyhow::Error; |
| 23 | + fn try_from(receipt: SignedReceipt) -> Result<Self, Self::Error> { |
| 24 | + Ok(Self { |
| 25 | + signature: receipt.signature.as_slice().try_into()?, |
| 26 | + message: receipt |
| 27 | + .message |
| 28 | + .ok_or(anyhow!("Missing message"))? |
| 29 | + .try_into()?, |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl From<tap_core::receipt::Receipt> for Receipt { |
| 35 | + fn from(value: tap_core::receipt::Receipt) -> Self { |
| 36 | + Self { |
| 37 | + allocation_id: value.allocation_id.as_slice().to_vec(), |
| 38 | + timestamp_ns: value.timestamp_ns, |
| 39 | + nonce: value.nonce, |
| 40 | + value: Some(value.value.into()), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl From<tap_core::receipt::SignedReceipt> for SignedReceipt { |
| 46 | + fn from(value: tap_core::receipt::SignedReceipt) -> Self { |
| 47 | + Self { |
| 48 | + message: Some(value.message.into()), |
| 49 | + signature: value.signature.as_bytes().to_vec(), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl TryFrom<SignedRav> for EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher> { |
| 55 | + type Error = anyhow::Error; |
| 56 | + fn try_from(voucher: SignedRav) -> Result<Self, Self::Error> { |
| 57 | + Ok(Self { |
| 58 | + signature: voucher.signature.as_slice().try_into()?, |
| 59 | + message: voucher |
| 60 | + .message |
| 61 | + .ok_or(anyhow!("Missing message"))? |
| 62 | + .try_into()?, |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl From<EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher>> for SignedRav { |
| 68 | + fn from(voucher: EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher>) -> Self { |
| 69 | + Self { |
| 70 | + signature: voucher.signature.as_bytes().to_vec(), |
| 71 | + message: Some(voucher.message.into()), |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl TryFrom<ReceiptAggregateVoucher> for tap_core::rav::ReceiptAggregateVoucher { |
| 77 | + type Error = anyhow::Error; |
| 78 | + fn try_from(voucher: ReceiptAggregateVoucher) -> Result<Self, Self::Error> { |
| 79 | + Ok(Self { |
| 80 | + allocationId: voucher.allocation_id.as_slice().try_into()?, |
| 81 | + timestampNs: voucher.timestamp_ns, |
| 82 | + valueAggregate: voucher |
| 83 | + .value_aggregate |
| 84 | + .ok_or(anyhow!("Missing Value Aggregate"))? |
| 85 | + .into(), |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl From<tap_core::rav::ReceiptAggregateVoucher> for ReceiptAggregateVoucher { |
| 91 | + fn from(voucher: tap_core::rav::ReceiptAggregateVoucher) -> Self { |
| 92 | + Self { |
| 93 | + allocation_id: voucher.allocationId.to_vec(), |
| 94 | + timestamp_ns: voucher.timestampNs, |
| 95 | + value_aggregate: Some(voucher.valueAggregate.into()), |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl From<Uint128> for u128 { |
| 101 | + fn from(Uint128 { high, low }: Uint128) -> Self { |
| 102 | + ((high as u128) << 64) | low as u128 |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl From<u128> for Uint128 { |
| 107 | + fn from(value: u128) -> Self { |
| 108 | + let high = (value >> 64) as u64; |
| 109 | + let low = value as u64; |
| 110 | + Self { high, low } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl RavRequest { |
| 115 | + pub fn new( |
| 116 | + receipts: Vec<tap_core::receipt::SignedReceipt>, |
| 117 | + previous_rav: Option<tap_core::rav::SignedRAV>, |
| 118 | + ) -> Self { |
| 119 | + Self { |
| 120 | + receipts: receipts.into_iter().map(Into::into).collect(), |
| 121 | + previous_rav: previous_rav.map(Into::into), |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl RavResponse { |
| 127 | + pub fn signed_rav(mut self) -> anyhow::Result<tap_core::rav::SignedRAV> { |
| 128 | + let signed_rav: tap_core::rav::SignedRAV = self |
| 129 | + .rav |
| 130 | + .take() |
| 131 | + .ok_or(anyhow!("Couldn't find rav"))? |
| 132 | + .try_into()?; |
| 133 | + Ok(signed_rav) |
| 134 | + } |
| 135 | +} |
0 commit comments