|
| 1 | +use anyhow::anyhow; |
| 2 | +use tap_core::signed_message::EIP712SignedMessage; |
| 3 | + |
| 4 | +tonic::include_proto!("tap_aggregator.v1"); |
| 5 | + |
| 6 | +impl TryFrom<Receipt> for tap_core::receipt::Receipt { |
| 7 | + type Error = anyhow::Error; |
| 8 | + fn try_from(receipt: Receipt) -> Result<Self, Self::Error> { |
| 9 | + Ok(Self { |
| 10 | + allocation_id: receipt.allocation_id.as_slice().try_into()?, |
| 11 | + timestamp_ns: receipt.timestamp_ns, |
| 12 | + value: receipt.value.ok_or(anyhow!("Missing value"))?.into(), |
| 13 | + nonce: receipt.nonce, |
| 14 | + }) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl TryFrom<SignedReceipt> for tap_core::receipt::SignedReceipt { |
| 19 | + type Error = anyhow::Error; |
| 20 | + fn try_from(receipt: SignedReceipt) -> Result<Self, Self::Error> { |
| 21 | + Ok(Self { |
| 22 | + signature: receipt.signature.as_slice().try_into()?, |
| 23 | + message: receipt |
| 24 | + .message |
| 25 | + .ok_or(anyhow!("Missing message"))? |
| 26 | + .try_into()?, |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl TryFrom<SignedRav> for EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher> { |
| 32 | + type Error = anyhow::Error; |
| 33 | + fn try_from(voucher: SignedRav) -> Result<Self, Self::Error> { |
| 34 | + Ok(Self { |
| 35 | + signature: voucher.signature.as_slice().try_into()?, |
| 36 | + message: voucher |
| 37 | + .message |
| 38 | + .ok_or(anyhow!("Missing message"))? |
| 39 | + .try_into()?, |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl From<EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher>> for SignedRav { |
| 45 | + fn from(voucher: EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher>) -> Self { |
| 46 | + Self { |
| 47 | + signature: voucher.signature.as_bytes().to_vec(), |
| 48 | + message: Some(voucher.message.into()), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl TryFrom<ReceiptAggregateVoucher> for tap_core::rav::ReceiptAggregateVoucher { |
| 54 | + type Error = anyhow::Error; |
| 55 | + fn try_from(voucher: ReceiptAggregateVoucher) -> Result<Self, Self::Error> { |
| 56 | + Ok(Self { |
| 57 | + allocationId: voucher.allocation_id.as_slice().try_into()?, |
| 58 | + timestampNs: voucher.timestamp_ns, |
| 59 | + valueAggregate: voucher |
| 60 | + .value_aggregate |
| 61 | + .ok_or(anyhow!("Missing Value Aggregate"))? |
| 62 | + .into(), |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl From<tap_core::rav::ReceiptAggregateVoucher> for ReceiptAggregateVoucher { |
| 68 | + fn from(voucher: tap_core::rav::ReceiptAggregateVoucher) -> Self { |
| 69 | + Self { |
| 70 | + allocation_id: voucher.allocationId.to_vec(), |
| 71 | + timestamp_ns: voucher.timestampNs, |
| 72 | + value_aggregate: Some(voucher.valueAggregate.into()), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl From<Uint128> for u128 { |
| 78 | + fn from(Uint128 { high, low }: Uint128) -> Self { |
| 79 | + ((high as u128) << 64) | low as u128 |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl From<u128> for Uint128 { |
| 84 | + fn from(value: u128) -> Self { |
| 85 | + let high = (value >> 64) as u64; |
| 86 | + let low = value as u64; |
| 87 | + Self { high, low } |
| 88 | + } |
| 89 | +} |
0 commit comments