|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +pub use alloy_core; |
| 4 | +pub use alloy_rlp; |
| 5 | +pub use alloy_signer; |
| 6 | +pub use alloy_sol_types; |
| 7 | + |
| 8 | +use alloy_core::primitives::Address; |
| 9 | +use alloy_rlp::{RlpDecodable, RlpEncodable}; |
| 10 | +use alloy_signer::Signature; |
| 11 | +use alloy_sol_types::{sol, SolStruct}; |
| 12 | + |
| 13 | +sol! { |
| 14 | + // EIP712 encoded bytes, ABI - ethers |
| 15 | + #[derive(Debug, RlpEncodable, RlpDecodable, PartialEq)] |
| 16 | + struct SignedIndexingAgreementVoucher { |
| 17 | + IndexingAgreementVoucher voucher; |
| 18 | + bytes signature; |
| 19 | + } |
| 20 | + |
| 21 | + #[derive(Debug, RlpEncodable, RlpDecodable, PartialEq)] |
| 22 | + struct IndexingAgreementVoucher { |
| 23 | + // should coincide with signer |
| 24 | + address payer; |
| 25 | + // should coincide with indexer |
| 26 | + address payee; |
| 27 | + // data service that will initiate payment collection |
| 28 | + address service; |
| 29 | + // initial indexing amount max |
| 30 | + uint256 maxInitialAmount; |
| 31 | + uint256 maxOngoingAmountPerEpoch; |
| 32 | + // time to accept the agreement, intended to be on the order |
| 33 | + // of hours or mins |
| 34 | + uint64 deadline; |
| 35 | + uint32 maxEpochsPerCollection; |
| 36 | + uint32 minEpochsPerCollection; |
| 37 | + // after which the agreement is complete |
| 38 | + uint32 durationEpochs; |
| 39 | + bytes metadata; |
| 40 | + } |
| 41 | + |
| 42 | + // the vouchers are generic to each data service, in the case of subgraphs this is an ABI-encoded SubgraphIndexingVoucherMetadata |
| 43 | + #[derive(Debug, RlpEncodable, RlpDecodable, PartialEq)] |
| 44 | + struct SubgraphIndexingVoucherMetadata { |
| 45 | + uint256 pricePerBlock; // wei GRT |
| 46 | + bytes32 protocolNetwork; // eip199:1 format |
| 47 | + // differentiate based on indexed chain |
| 48 | + bytes32 chainId; // eip199:1 format |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl SignedIndexingAgreementVoucher { |
| 53 | + // TODO: Validate all values, maybe return a useful error on failure. |
| 54 | + pub fn is_valid( |
| 55 | + &self, |
| 56 | + expected_payee: &Address, |
| 57 | + allowed_payers: impl AsRef<[Address]>, |
| 58 | + ) -> bool { |
| 59 | + let sig = match Signature::from_str(&self.signature.to_string()) { |
| 60 | + Ok(s) => s, |
| 61 | + Err(_) => return false, |
| 62 | + }; |
| 63 | + |
| 64 | + let payer = sig |
| 65 | + .recover_address_from_msg(self.voucher.eip712_hash_struct()) |
| 66 | + .unwrap(); |
| 67 | + |
| 68 | + if allowed_payers.as_ref().is_empty() |
| 69 | + || !allowed_payers.as_ref().iter().any(|addr| addr.eq(&payer)) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + if !self.voucher.payee.eq(expected_payee) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + true |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod test { |
| 84 | + use alloy_core::primitives::{Address, FixedBytes, U256}; |
| 85 | + use alloy_signer::SignerSync; |
| 86 | + use alloy_signer_local::PrivateKeySigner; |
| 87 | + use alloy_sol_types::SolStruct; |
| 88 | + |
| 89 | + use crate::{ |
| 90 | + IndexingAgreementVoucher, SignedIndexingAgreementVoucher, SubgraphIndexingVoucherMetadata, |
| 91 | + }; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn voucher_signature_verification() { |
| 95 | + let payee = PrivateKeySigner::random(); |
| 96 | + let payee_addr = payee.address(); |
| 97 | + let signer = PrivateKeySigner::random(); |
| 98 | + let pubkey = signer.address(); |
| 99 | + |
| 100 | + let metadata = SubgraphIndexingVoucherMetadata { |
| 101 | + pricePerBlock: U256::from(10000_u64), |
| 102 | + protocolNetwork: FixedBytes::left_padding_from("arbitrum-one".as_bytes()), |
| 103 | + chainId: FixedBytes::left_padding_from("mainnet".as_bytes()), |
| 104 | + }; |
| 105 | + |
| 106 | + let voucher = IndexingAgreementVoucher { |
| 107 | + payer: pubkey.clone(), |
| 108 | + payee: payee.address(), |
| 109 | + service: Address(FixedBytes::ZERO), |
| 110 | + maxInitialAmount: U256::from(10000_u64), |
| 111 | + maxOngoingAmountPerEpoch: U256::from(10000_u64), |
| 112 | + deadline: 1000, |
| 113 | + maxEpochsPerCollection: 1000, |
| 114 | + minEpochsPerCollection: 1000, |
| 115 | + durationEpochs: 1000, |
| 116 | + metadata: metadata.eip712_hash_struct().to_owned().into(), |
| 117 | + }; |
| 118 | + |
| 119 | + let signed = SignedIndexingAgreementVoucher { |
| 120 | + voucher: voucher.clone(), |
| 121 | + signature: signer |
| 122 | + .sign_message_sync(voucher.eip712_hash_struct().as_slice()) |
| 123 | + .unwrap() |
| 124 | + .as_bytes() |
| 125 | + .into(), |
| 126 | + }; |
| 127 | + |
| 128 | + assert_eq!(false, signed.is_valid(&payee_addr, vec![])); |
| 129 | + assert_eq!(true, signed.is_valid(&payee_addr, vec![pubkey.clone()])); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn check_voucher_modified() { |
| 134 | + let payee = PrivateKeySigner::random(); |
| 135 | + let payee_addr = payee.address(); |
| 136 | + let signer = PrivateKeySigner::random(); |
| 137 | + let pubkey = signer.address(); |
| 138 | + |
| 139 | + let metadata = SubgraphIndexingVoucherMetadata { |
| 140 | + pricePerBlock: U256::from(10000_u64), |
| 141 | + protocolNetwork: FixedBytes::left_padding_from("arbitrum-one".as_bytes()), |
| 142 | + chainId: FixedBytes::left_padding_from("mainnet".as_bytes()), |
| 143 | + }; |
| 144 | + |
| 145 | + let voucher = IndexingAgreementVoucher { |
| 146 | + payer: pubkey.clone(), |
| 147 | + payee: payee.address(), |
| 148 | + service: Address(FixedBytes::ZERO), |
| 149 | + maxInitialAmount: U256::from(10000_u64), |
| 150 | + maxOngoingAmountPerEpoch: U256::from(10000_u64), |
| 151 | + deadline: 1000, |
| 152 | + maxEpochsPerCollection: 1000, |
| 153 | + minEpochsPerCollection: 1000, |
| 154 | + durationEpochs: 1000, |
| 155 | + metadata: metadata.eip712_hash_struct().to_owned().into(), |
| 156 | + }; |
| 157 | + |
| 158 | + let mut signed = SignedIndexingAgreementVoucher { |
| 159 | + voucher: voucher.clone(), |
| 160 | + signature: signer |
| 161 | + .sign_message_sync(voucher.eip712_hash_struct().as_slice()) |
| 162 | + .unwrap() |
| 163 | + .as_bytes() |
| 164 | + .into(), |
| 165 | + }; |
| 166 | + signed.voucher.service = Address::repeat_byte(9); |
| 167 | + |
| 168 | + assert_eq!(false, signed.is_valid(&payee_addr, vec![pubkey.clone()])); |
| 169 | + } |
| 170 | +} |
0 commit comments