refactor
This commit is contained in:
46
stamp.go
46
stamp.go
@@ -6,9 +6,15 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.intruders.space/public/opentimestamps/ots"
|
||||
"git.intruders.space/public/opentimestamps/varn"
|
||||
)
|
||||
|
||||
func Stamp(ctx context.Context, calendarUrl string, digest [32]byte) (Sequence, error) {
|
||||
var httpClient = &http.Client{}
|
||||
|
||||
func Stamp(ctx context.Context, calendarUrl string, digest [32]byte) (ots.Sequence, error) {
|
||||
body := bytes.NewBuffer(digest[:])
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", normalizeUrl(calendarUrl)+"/digest", body)
|
||||
if err != nil {
|
||||
@@ -18,7 +24,8 @@ func Stamp(ctx context.Context, calendarUrl string, digest [32]byte) (Sequence,
|
||||
req.Header.Add("User-Agent", "github.com/fiatjaf/opentimestamps")
|
||||
req.Header.Add("Accept", "application/vnd.opentimestamps.v1")
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("'%s' request failed: %w", calendarUrl, err)
|
||||
}
|
||||
@@ -29,7 +36,7 @@ func Stamp(ctx context.Context, calendarUrl string, digest [32]byte) (Sequence,
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
seq, err := parseCalendarServerResponse(newBuffer(full))
|
||||
seq, err := parseCalendarServerResponse(varn.NewBuffer(full))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response from '%s': %w", calendarUrl, err)
|
||||
}
|
||||
@@ -37,11 +44,11 @@ func Stamp(ctx context.Context, calendarUrl string, digest [32]byte) (Sequence,
|
||||
return seq, nil
|
||||
}
|
||||
|
||||
func ReadFromFile(data []byte) (*File, error) {
|
||||
return parseOTSFile(newBuffer(data))
|
||||
func ReadFromFile(data []byte) (*ots.File, error) {
|
||||
return ots.ParseOTSFile(varn.NewBuffer(data))
|
||||
}
|
||||
|
||||
func UpgradeSequence(ctx context.Context, seq Sequence, initial []byte) (Sequence, error) {
|
||||
func UpgradeSequence(ctx context.Context, seq ots.Sequence, initial []byte) (ots.Sequence, error) {
|
||||
result, _ := seq.Compute(initial)
|
||||
attestation := seq.GetAttestation()
|
||||
|
||||
@@ -54,7 +61,8 @@ func UpgradeSequence(ctx context.Context, seq Sequence, initial []byte) (Sequenc
|
||||
req.Header.Add("User-Agent", "github.com/fiatjaf/opentimestamps")
|
||||
req.Header.Add("Accept", "application/vnd.opentimestamps.v1")
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("'%s' request failed: %w", attestation.CalendarServerURL, err)
|
||||
}
|
||||
@@ -69,14 +77,34 @@ func UpgradeSequence(ctx context.Context, seq Sequence, initial []byte) (Sequenc
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
tail, err := parseCalendarServerResponse(newBuffer(body))
|
||||
tail, err := parseCalendarServerResponse(varn.NewBuffer(body))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response from '%s': %w", attestation.CalendarServerURL, err)
|
||||
}
|
||||
|
||||
newSeq := make(Sequence, len(seq)+len(tail)-1)
|
||||
newSeq := make(ots.Sequence, len(seq)+len(tail)-1)
|
||||
copy(newSeq, seq[0:len(seq)-1])
|
||||
copy(newSeq[len(seq)-1:], tail)
|
||||
|
||||
return newSeq, nil
|
||||
}
|
||||
|
||||
func parseCalendarServerResponse(buf varn.Buffer) (ots.Sequence, error) {
|
||||
seqs, err := ots.ParseTimestamp(buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(seqs) != 1 {
|
||||
return nil, fmt.Errorf("invalid number of sequences obtained: %d", len(seqs))
|
||||
}
|
||||
|
||||
return seqs[0], nil
|
||||
}
|
||||
|
||||
func normalizeUrl(u string) string {
|
||||
u = strings.TrimSuffix(u, "/")
|
||||
if !strings.HasPrefix(u, "https://") && !strings.HasPrefix(u, "http://") {
|
||||
u = "http://" + u
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user