Files
whitepaper_obsero/probe/pkg/proof/format.go
T
2025-04-29 23:40:33 +02:00

102 lines
2.7 KiB
Go

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"`
Status string `json:"status"`
HTTPStatus int `json:"http_status_code"`
LatencyMs int64 `json:"latency_ms"`
Error string `json:"error_message"`
Observer ObserverInfo `json:"observer"`
Version string `json:"version"`
Proof ProofMeta `json:"proof"`
}
type ObserverInfo struct {
Address string `json:"address"`
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
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,
Target: result.Target,
Status: result.Status,
HTTPStatus: result.StatusCode,
LatencyMs: result.LatencyMs,
Error: result.ErrorMsg,
Version: "0.1",
Observer: ObserverInfo{
Address: "0xABCD...1234",
Country: loc.Country,
Region: loc.Region,
City: loc.City,
ProbeVersion: "0.1",
},
}
}
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
}