-
Notifications
You must be signed in to change notification settings - Fork 19
/
receipt.go
87 lines (77 loc) · 2.55 KB
/
receipt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"encoding/json"
"fmt"
)
// Receipt is the Merkel proof that a given Entry and its metadata (such as the
// Entry Block timestamp) have been written to the Factom Blockchain and
// possibly anchored into Bitcoin, Etherium, or other blockchains.
//
// The data from the reciept may be used to reconstruct the Merkel proof for the
// requested Entry thus cryptographically proving the Entry is represented by a
// known Factom Directory Block.
type Receipt struct {
Entry struct {
Raw string `json:"raw,omitempty"`
EntryHash string `json:"entryhash,omitempty"`
Json string `json:"json,omitempty"`
} `json:"entry,omitempty"`
MerkleBranch []struct {
Left string `json:"left,omitempty"`
Right string `json:"right,omitempty"`
Top string `json:"top,omitempty"`
} `json:"merklebranch,omitempty"`
EntryBlockKeyMR string `json:"entryblockkeymr,omitempty"`
DirectoryBlockKeyMR string `json:"directoryblockkeymr,omitempty"`
BitcoinTransactionHash string `json:"bitcointransactionhash,omitempty"`
BitcoinBlockHash string `json:"bitcoinblockhash,omitempty"`
}
func (r *Receipt) String() string {
var s string
if r.Entry.Raw != "" {
s += fmt.Sprintln("Raw:", r.Entry.Raw)
}
if r.Entry.EntryHash != "" {
s += fmt.Sprintln("EntryHash:", r.Entry.EntryHash)
}
if r.Entry.Json != "" {
s += fmt.Sprintln("JSON:", r.Entry.Json)
}
s += fmt.Sprintln("DirectoryBlockKeyMR:", r.DirectoryBlockKeyMR)
s += fmt.Sprintln("EntryBlockKeyMR:", r.EntryBlockKeyMR)
s += fmt.Sprintln("BitcoinTransactionHash:", r.BitcoinTransactionHash)
s += fmt.Sprintln("BitcoinBlockHash:", r.BitcoinBlockHash)
s += fmt.Sprintln("MerkelBranch [")
for _, b := range r.MerkleBranch {
s += fmt.Sprintln(" {")
s += fmt.Sprintln(" left:", b.Left)
s += fmt.Sprintln(" right:", b.Right)
s += fmt.Sprintln(" top:", b.Top)
s += fmt.Sprintln(" }")
}
s += fmt.Sprintln("]")
return s
}
// GetReceipt requests a Receipt for a given Factom Entry.
func GetReceipt(hash string) (*Receipt, error) {
type receiptResponse struct {
Receipt *Receipt `json:"receipt"`
}
params := hashRequest{Hash: hash}
req := NewJSON2Request("receipt", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
rec := new(receiptResponse)
if err := json.Unmarshal(resp.JSONResult(), rec); err != nil {
return nil, err
}
return rec.Receipt, nil
}