Skip to content
File

Blob: tun/client/acme.go

go60 lines
1package client
2 
3import (
4 "context"
5 "crypto/ed25519"
6 
7 "go.miragespace.co/specter/spec/acme"
8 "go.miragespace.co/specter/spec/pki"
9 "go.miragespace.co/specter/spec/pow"
10 "go.miragespace.co/specter/spec/protocol"
11 "go.miragespace.co/specter/spec/rpc"
12)
13 
14func (c *Client) obtainAcmeProof(hostname string) (*protocol.ProofOfWork, error) {
15 c.configMu.RLock()
16 privKey, err := pki.UnmarshalPrivateKey([]byte(c.Configuration.PrivKey))
17 c.configMu.RUnlock()
18 if err != nil {
19 return nil, err
20 }
21 
22 return pow.GenerateSolution(privKey, pow.Parameters{
23 Difficulty: acme.HashcashDifficulty,
24 Expires: acme.HashcashExpires,
25 GetSubject: func(pubKey ed25519.PublicKey) string {
26 return hostname
27 },
28 })
29}
30 
31func (c *Client) GetAcmeInstruction(ctx context.Context, hostname string) (*protocol.InstructionResponse, error) {
32 proof, err := c.obtainAcmeProof(hostname)
33 if err != nil {
34 return nil, err
35 }
36 
37 return retryRPC(c, ctx, func(node *protocol.Node) (*protocol.InstructionResponse, error) {
38 ctx = rpc.WithNode(ctx, node)
39 return c.tunnelClient.AcmeInstruction(ctx, &protocol.InstructionRequest{
40 Proof: proof,
41 Hostname: hostname,
42 })
43 })
44}
45 
46func (c *Client) RequestAcmeValidation(ctx context.Context, hostname string) (*protocol.ValidateResponse, error) {
47 proof, err := c.obtainAcmeProof(hostname)
48 if err != nil {
49 return nil, err
50 }
51 
52 return retryRPC(c, ctx, func(node *protocol.Node) (*protocol.ValidateResponse, error) {
53 ctx = rpc.WithNode(ctx, node)
54 return c.tunnelClient.AcmeValidate(ctx, &protocol.ValidateRequest{
55 Proof: proof,
56 Hostname: hostname,
57 })
58 })
59}