From 1b0ecd993e8a261c42508b1006aa9af7f762cb0a Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 2 Oct 2023 19:45:27 -0300 Subject: [PATCH] UpgradeSequence() instead of seq.Upgrade() and correct behavior. --- README.md | 6 +++--- stamp.go | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6acfe28..dfb94ac 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ func main () { hash := sha256.Sum256([]byte{1,2,3,4,5,6}) seq, _ := opentimestamps.Stamp(context.Background(), "https://alice.btc.calendar.opentimestamps.org/", hash) - // you can just call .Upgrade() to get the upgraded sequence (or an error if not yet available) - upgradedSeq, err := seq.Upgrade(context.Background(), hash[:]) + // you can just call UpgradeSequence() to get the upgraded sequence (or an error if not yet available) + upgradedSeq, err := opentimestamps.UpgradeSequence(context.Background(), seq, hash[:]) if err != nil { fmt.Println("wait more") } @@ -43,7 +43,7 @@ func main () { fmt.Println(hex.EncodeToString(seq[2].Argument)) // "c40fe258f9b828a0b5a7" // all these instructions can be executed in order, starting from the initial hash - result := seq.Compute(hash) // this is the value we send to the calendar server in order to get the upgraded sequence on .Upgrade() + result := seq.Compute(hash) // this is the value we send to the calendar server in order to get the upgraded sequence finalResult := upgradedSeq.Compute(hash) // this should be the merkle root of a bitcoin block if this sequence is upgraded // each sequence always ends in an "attestation" diff --git a/stamp.go b/stamp.go index 2c2971d..f3c471c 100644 --- a/stamp.go +++ b/stamp.go @@ -41,9 +41,9 @@ func ReadFromFile(data []byte) (*File, error) { return parseOTSFile(newBuffer(data)) } -func (seq Sequence) Upgrade(ctx context.Context, initial []byte) (Sequence, error) { +func UpgradeSequence(ctx context.Context, seq Sequence, initial []byte) (Sequence, error) { result := seq.Compute(initial) - attestation := seq[len(seq)-1] + attestation := seq.GetAttestation() url := fmt.Sprintf("%s/timestamp/%x", normalizeUrl(attestation.CalendarServerURL), result) req, err := http.NewRequestWithContext(ctx, "GET", url, nil) @@ -69,10 +69,14 @@ func (seq Sequence) Upgrade(ctx context.Context, initial []byte) (Sequence, erro } resp.Body.Close() - newSeq, err := parseCalendarServerResponse(newBuffer(body)) + tail, err := parseCalendarServerResponse(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) + copy(newSeq, seq[0:len(seq)-1]) + copy(newSeq[len(seq)-1:], tail) + return newSeq, nil }