Skip to content

Commit 5f84e20

Browse files
committed
feat: add proto definitions
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent a6c5193 commit 5f84e20

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
syntax = "proto3";
2+
package tap_aggregator.v1;
3+
4+
message Receipt {
5+
bytes allocation_id = 1;
6+
uint64 timestamp_ns = 2;
7+
uint64 nonce = 3;
8+
Uint128 value = 4;
9+
}
10+
11+
message SignedReceipt {
12+
Receipt message = 1;
13+
bytes signature = 2;
14+
}
15+
16+
message ReceiptAggregateVoucher {
17+
bytes allocation_id = 1;
18+
uint64 timestamp_ns = 2;
19+
Uint128 value_aggregate = 3;
20+
}
21+
22+
message SignedRav {
23+
ReceiptAggregateVoucher message = 1;
24+
bytes signature = 2;
25+
}
26+
27+
message RavRequest {
28+
repeated SignedReceipt receipts = 1;
29+
optional SignedRav previous_rav = 2;
30+
}
31+
32+
message RavResponse {
33+
SignedRav rav = 1;
34+
}
35+
36+
service TapAggregator {
37+
rpc AggregateReceipts(RavRequest) returns (RavResponse);
38+
}
39+
40+
message Uint128 {
41+
// Highest 64 bits of a 128 bit number.
42+
uint64 high = 1;
43+
// Lowest 64 bits of a 128 bit number.
44+
uint64 low = 2;
45+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)