3 Commits

Author SHA1 Message Date
fiatjaf
1b0ecd993e UpgradeSequence() instead of seq.Upgrade() and correct behavior. 2023-10-02 19:45:27 -03:00
fiatjaf
57291497e6 Sequence.GetAttestation() 2023-10-02 08:45:39 -03:00
fiatjaf
4f3422212a add license so pkg.go.dev works. 2023-09-30 14:52:54 -03:00
4 changed files with 28 additions and 6 deletions

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -16,8 +16,8 @@ func main () {
hash := sha256.Sum256([]byte{1,2,3,4,5,6}) hash := sha256.Sum256([]byte{1,2,3,4,5,6})
seq, _ := opentimestamps.Stamp(context.Background(), "https://alice.btc.calendar.opentimestamps.org/", hash) 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) // you can just call UpgradeSequence() to get the upgraded sequence (or an error if not yet available)
upgradedSeq, err := seq.Upgrade(context.Background(), hash[:]) upgradedSeq, err := opentimestamps.UpgradeSequence(context.Background(), seq, hash[:])
if err != nil { if err != nil {
fmt.Println("wait more") fmt.Println("wait more")
} }
@@ -43,7 +43,7 @@ func main () {
fmt.Println(hex.EncodeToString(seq[2].Argument)) // "c40fe258f9b828a0b5a7" fmt.Println(hex.EncodeToString(seq[2].Argument)) // "c40fe258f9b828a0b5a7"
// all these instructions can be executed in order, starting from the initial hash // 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 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" // each sequence always ends in an "attestation"

11
ots.go
View File

@@ -71,6 +71,17 @@ type Instruction struct {
type Sequence []Instruction type Sequence []Instruction
func (seq Sequence) GetAttestation() Attestation {
if len(seq) == 0 {
return Attestation{}
}
att := seq[len(seq)-1]
if att.Attestation == nil {
return Attestation{}
}
return *att.Attestation
}
func (seq Sequence) Compute(initial []byte) []byte { func (seq Sequence) Compute(initial []byte) []byte {
current := initial current := initial
for _, inst := range seq { for _, inst := range seq {

View File

@@ -41,9 +41,9 @@ func ReadFromFile(data []byte) (*File, error) {
return parseOTSFile(newBuffer(data)) 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) result := seq.Compute(initial)
attestation := seq[len(seq)-1] attestation := seq.GetAttestation()
url := fmt.Sprintf("%s/timestamp/%x", normalizeUrl(attestation.CalendarServerURL), result) url := fmt.Sprintf("%s/timestamp/%x", normalizeUrl(attestation.CalendarServerURL), result)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil) 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() resp.Body.Close()
newSeq, err := parseCalendarServerResponse(newBuffer(body)) tail, err := parseCalendarServerResponse(newBuffer(body))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse response from '%s': %w", attestation.CalendarServerURL, err) 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 return newSeq, nil
} }