v1.0.1 probe

This commit is contained in:
2025-04-29 23:40:33 +02:00
parent 371b44422b
commit 56c57957c8
4 changed files with 152 additions and 42 deletions
+49
View File
@@ -1,13 +1,34 @@
package proof
import (
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"github.com/ethereum/go-ethereum/crypto"
"git.cryptolab.re/foudre/whitepaper_obsero/pkg/geo"
"git.cryptolab.re/foudre/whitepaper_obsero/pkg/probe"
import (
...
shell "github.com/ipfs/go-ipfs-api"
...
)
...
func UploadToIPFS(filepath string) (string, error) {
sh := shell.NewShell("localhost:5001")
cid, err := sh.AddNoPin(shell.NewShellFile(filepath))
if err != nil {
return "", err
}
return cid, nil
}
type Observation struct {
Timestamp string `json:"timestamp"`
Target string `json:"target"`
@@ -17,6 +38,7 @@ type Observation struct {
Error string `json:"error_message"`
Observer ObserverInfo `json:"observer"`
Version string `json:"version"`
Proof ProofMeta `json:"proof"`
}
type ObserverInfo struct {
@@ -27,6 +49,11 @@ type ObserverInfo struct {
ProbeVersion string `json:"probe_version"`
}
type ProofMeta struct {
Hash string `json:"hash"`
Signature string `json:"signature"`
}
func BuildProof(result probe.PingResult, loc geo.GeoInfo) Observation {
return Observation{
Timestamp: result.Timestamp,
@@ -50,3 +77,25 @@ func SaveProof(obs Observation, filename string) {
file, _ := json.MarshalIndent(obs, "", " ")
_ = os.WriteFile(filename, file, 0644)
}
func SignProof(obs *Observation) error {
// Replace with your own private key (for demo only)
privKeyHex := "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce03738e0f7b6267d5bdc25"
privKey, err := crypto.HexToECDSA(privKeyHex)
if err != nil {
return err
}
// Hash relevant fields
data := fmt.Sprintf("%s|%s|%s|%d|%d", obs.Timestamp, obs.Target, obs.Status, obs.HTTPStatus, obs.LatencyMs)
hash := sha256.Sum256([]byte(data))
obs.Proof.Hash = hex.EncodeToString(hash[:])
sigBytes, err := crypto.Sign(hash[:], privKey)
if err != nil {
return err
}
obs.Proof.Signature = hex.EncodeToString(sigBytes)
return nil
}