Skip to content

Commit

Permalink
fix: add support for legacy scalar signer
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Nov 13, 2023
1 parent 9aaadca commit 619a68a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
7 changes: 5 additions & 2 deletions graph-gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,14 @@ impl From<KafkaConfig> for rdkafka::config::ClientConfig {
#[serde_as]
#[derive(Debug, Deserialize)]
pub struct Scalar {
/// Scalar TAP verifier contract chain
pub chain_id: U256,
/// Mnemonic for legacy voucher signing
#[serde_as(as = "Option<DisplayFromStr>")]
pub legacy_signer: Option<Hidden<SecretKey>>,
/// Mnemonic for voucher signing
#[serde_as(as = "DisplayFromStr")]
pub signer: Hidden<SecretKey>,
/// Scalar TAP verifier contract chain
pub chain_id: U256,
/// Scalar TAP verifier contract address
pub verifier: Address,
}
Expand Down
19 changes: 13 additions & 6 deletions graph-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,20 @@ async fn main() {
.collect();
Ptr::new(legacy_indexers)
});
let legacy_signer: &'static SecretKey = Box::leak(Box::new(
config
.scalar
.legacy_signer
.map(|s| s.0)
.unwrap_or(config.scalar.signer.0),
));
let receipt_signer: &'static ReceiptSigner = Box::leak(Box::new(
ReceiptSigner::new(
config.scalar.signer.clone(),
legacy_indexers,
config.scalar.signer.0,
config.scalar.chain_id,
config.scalar.verifier,
legacy_signer,
legacy_indexers,
)
.await,
));
Expand Down Expand Up @@ -352,24 +360,23 @@ async fn main() {
.layer(middleware::from_fn(client_query::legacy_auth_adapter)),
);

let signer: &'static SecretKey = Box::leak(Box::new(config.scalar.signer.0));
let router = Router::new()
.route("/", routing::get(|| async { "Ready to roll!" }))
// This path is required by NGINX ingress controller.
.route("/ready", routing::get(|| async { "Ready" }))
.route(
"/collect-receipts",
routing::post(vouchers::handle_collect_receipts).with_state(signer),
routing::post(vouchers::handle_collect_receipts).with_state(legacy_signer),
)
.route(
"/partial-voucher",
routing::post(vouchers::handle_partial_voucher)
.with_state(signer)
.with_state(legacy_signer)
.layer(DefaultBodyLimit::max(3_000_000)),
)
.route(
"/voucher",
routing::post(vouchers::handle_voucher).with_state(signer),
routing::post(vouchers::handle_voucher).with_state(legacy_signer),
)
// Temporary route. Will be replaced by gateway metadata (GSP).
.route(
Expand Down
17 changes: 9 additions & 8 deletions graph-gateway/src/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ use indexer_selection::{Indexing, SecretKey};
use prelude::GRT;

pub struct ReceiptSigner {
// TODO: When legacy (non-TAP) Scalar is removed, this should contain the only owned reference
// to the SignerKey. This will resolve https://github.com/edgeandnode/graph-gateway/issues/13.
signer_key: SecretKey,
signer: SecretKey,
domain: Eip712Domain,
allocations: RwLock<HashMap<Indexing, Address>>,
legacy_signer: &'static SecretKey,
legacy_indexers: Eventual<Ptr<HashSet<Address>>>,
legacy_pools: RwLock<HashMap<Indexing, Arc<Mutex<ReceiptPool>>>>,
}
Expand Down Expand Up @@ -50,14 +49,15 @@ impl ScalarReceipt {

impl ReceiptSigner {
pub async fn new(
signer_key: SecretKey,
legacy_indexers: Eventual<Ptr<HashSet<Address>>>,
signer: SecretKey,
chain_id: U256,
verifier: Address,
legacy_signer: &'static SecretKey,
legacy_indexers: Eventual<Ptr<HashSet<Address>>>,
) -> Self {
let _ = legacy_indexers.value().await;
Self {
signer_key,
signer,
domain: Eip712Domain {
name: Some("Scalar TAP".into()),
version: Some("1".into()),
Expand All @@ -66,6 +66,7 @@ impl ReceiptSigner {
salt: None,
},
allocations: RwLock::default(),
legacy_signer,
legacy_indexers,
legacy_pools: RwLock::default(),
}
Expand Down Expand Up @@ -101,7 +102,7 @@ impl ReceiptSigner {
value: fee.shift::<0>().as_u256().as_u128(),
};
let wallet =
Wallet::from_bytes(self.signer_key.as_ref()).expect("failed to prepare receipt wallet");
Wallet::from_bytes(self.signer.as_ref()).expect("failed to prepare receipt wallet");
let signed = EIP712SignedMessage::new(&self.domain, receipt, &wallet)
.await
.expect("failed to sign receipt");
Expand Down Expand Up @@ -143,7 +144,7 @@ impl ReceiptSigner {
}
// add allocation, if not already present
if !legacy_pool.contains_allocation(allocation) {
legacy_pool.add_allocation(self.signer_key, *allocation.0);
legacy_pool.add_allocation(*self.legacy_signer, *allocation.0);
}
}

Expand Down
4 changes: 2 additions & 2 deletions graph-gateway/src/vouchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub async fn handle_partial_voucher(
payload: Bytes,
) -> Result<JsonResponse, (StatusCode, String)> {
let _timer = METRICS.partial_voucher.duration.start_timer();
match process_partial_voucher(&signer, &payload) {
match process_partial_voucher(signer, &payload) {
Ok(response) => {
METRICS.partial_voucher.ok.inc();
Ok(response)
Expand Down Expand Up @@ -116,7 +116,7 @@ pub async fn handle_voucher(
payload: Bytes,
) -> Result<JsonResponse, (StatusCode, String)> {
let _timer = METRICS.voucher.duration.start_timer();
match process_voucher(&signer, &payload) {
match process_voucher(signer, &payload) {
Ok(response) => {
METRICS.voucher.ok.inc();
Ok(response)
Expand Down

0 comments on commit 619a68a

Please sign in to comment.