Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notaryd #61

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ path = "src/sample.rs"
name = "civkit-marketd"
path = "src/services/marketd.rs"

[[bin]]
name = "civkit-notaryd"
path = "src/services/notaryd.rs"

[dependencies]
futures-channel = "0.3.28"
futures-util = "0.3.28"
Expand Down
14 changes: 14 additions & 0 deletions src/proto/civkitservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package civkitservice;

service CivkitService {
rpc RegisterService(RegisterRequest) returns (RegisterReply);
rpc FetchServiceEvent(FetchRequest) returns (FetchReply);
rpc SubmitServiceEvent(SubmitRequest) returns (SubmitReply);
}

message RegisterRequest {
Expand All @@ -14,3 +16,15 @@ message RegisterRequest {
message RegisterReply {
uint64 registration_result = 1;
}

message FetchRequest {
}

message FetchReply {
}

message SubmitRequest {
}

message SubmitReply {
}
14 changes: 14 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ impl CivkitService for DummyManager {

Ok(Response::new(civkitservice::RegisterReply { registration_result: 1 }))
}

async fn fetch_service_event(&self, request: Request<civkitservice::FetchRequest>) -> Result<Response<civkitservice::FetchReply>, Status> {

println!("Received fetch service");

Ok(Response::new(civkitservice::FetchReply {}))
}

async fn submit_service_event(&self, request: Request<civkitservice::SubmitRequest>) -> Result<Response<civkitservice::SubmitReply>, Status> {

println!("Submit service");

Ok(Response::new(civkitservice::SubmitReply {}))
}
}

#[derive(Parser, Debug)]
Expand Down
56 changes: 56 additions & 0 deletions src/services/notaryd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

use civkitservice::civkit_service_client::CivkitServiceClient;

use civkitservice::{RegisterRequest, RegisterReply};

use bitcoin::secp256k1::{SecretKey, PublicKey, Secp256k1};
use bitcoin::secp256k1;

pub mod civkitservice {
tonic::include_proto!("civkitservice");
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

let mut civkitd_client = CivkitServiceClient::connect(format!("http://[::1]:{}", 50031)).await?;

let secp_ctx = Secp256k1::new();
let pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42;32]).unwrap());

let request = tonic::Request::new(RegisterRequest {
service_pubkey: pubkey.serialize().to_vec()
});

let response = civkitd_client.register_service(request).await?;

//TODO: step 1 receive queries from Nostr client
// - add new event kind `service payload`
// - add mainstay / opentimestamp attestations in nostr event content

//TODO: step 2 store attestation in local service store
// - add a map

//TODO: step 3 have the notary server counter-sign the attestation (+ maybe add block hash)

//TODO: step 4 build a merkle tree or pay-to-contract with the attestation

//TODO: step 5 commit the notarization anchor in a Bitcoin on-chain transaction when X is reached
// - X is the frequency declared in the "service policy" at register_service

//TODO: step 6 broadcast the on-chain transaction

//TODO: step 7 return the result to the Nostr client ?
// - add pre-notification at step 3 if the client does not want to be dependent
// on blockchain interval

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirming my understanding of the process flow in pseudo-code

fn notarize(service_payload) -> NostrEventContent {
2. local_store.append(service_payload)
3. attestation = sign(service_payload) // pre-notification?
4. build_merkle_tree // unclear as to process here
5. if local_store.length() >= x; btcx = create_btc_transaction(local_store);
6. broadcast(btcx);
7. return NostrEventContent(attestation);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the flow is mostly correct with the following feedback:
4. I don’t know yet if the attestation will be committed with a merkle tree or pay-to-contract (i’m thinking to start to standardize bitcoin notarization with bips or something equivalent to have inter-operability of attestations)
5. rather to have create_btc_transaction committed on the size of the store, X should be a time point (block height or unix epoch) though obviously we might have to flush when the store is full before X

Ok(())
}