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