forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
146 lines (108 loc) · 3.15 KB
/
api.go
File metadata and controls
146 lines (108 loc) · 3.15 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package factomapi
import (
"net/http"
"net/url"
"fmt"
"encoding/hex"
"encoding/base64"
"sort"
"github.com/FactomProject/FactomCode/database"
"github.com/FactomProject/FactomCode/notaryapi"
//"github.com/FactomProject/FactomCode/database/ldb"
)
//to be improved:
var serverAddr = "localhost:8083"
var db database.Db // database
func CommitChain(name [][]byte) (*notaryapi.Hash, error) {
c := new(notaryapi.EChain)
c.Name = name
c.GenerateIDFromName()
return c.ChainID, nil
}
func RevealChain(version uint16, c *notaryapi.EChain, e *notaryapi.Entry) error {
bChain,_ := c.MarshalBinary()
data := url.Values {}
data.Set("datatype", "chain")
data.Set("format", "binary")
data.Set("chain", hex.EncodeToString(bChain))
fmt.Println("chain name[0]:%s", string(c.Name[0]))
server := fmt.Sprintf(`http://%s/v1`, serverAddr)
_, err := http.PostForm(server, data)
return err
}
/*
func CommitEntry(cid *notaryapi.Hash) (*notaryapi.Entry, error) {
e := new(notaryapi.Entry)
e.ChainID := cid
e.ExtHashes := h
e.Data := data
return e
}
*/
func RevealEntry(version uint16, e *notaryapi.Entry) error {
bEntry,_ := e.MarshalBinary()
data := url.Values{}
data.Set("format", "binary")
data.Set("entry", hex.EncodeToString(bEntry))
fmt.Println("Entry extid[0]:%s", string(e.ExtIDs[0]))
server := fmt.Sprintf(`http://%s/v1`, serverAddr)
_, err := http.PostForm(server, data)
return err
}
func GetDirectoryBloks(fromBlockHeight uint64, toBlockHeight uint64) (dBlocks []notaryapi.DBlock, err error) {
//needs to be improved ??
dBlocks, _ = db.FetchAllDBlocks()
sort.Sort(byBlockID(dBlocks))
if fromBlockHeight > uint64(len(dBlocks)-1) {
return nil, nil
} else if toBlockHeight > uint64(len(dBlocks)-1) {
toBlockHeight = uint64(len(dBlocks)-1)
}
return dBlocks[fromBlockHeight:toBlockHeight+1], nil
}
func GetDirectoryBlokByHash(dBlockHash *notaryapi.Hash) (dBlock *notaryapi.DBlock, err error) {
dBlock, err = db.FetchDBlockByHash(dBlockHash)
return dBlock, err
}
func GetDirectoryBlokByHashStr(dBlockHashBase64 string) (dBlock *notaryapi.DBlock, err error) {
bytes, err := base64.StdEncoding.DecodeString(dBlockHashBase64)
if err != nil || len(bytes) != notaryapi.HashSize{
return nil, err
}
dBlockHash := new (notaryapi.Hash)
dBlockHash.Bytes = bytes
dBlock, _ = db.FetchDBlockByHash(dBlockHash)
return dBlock, nil
}
// to be removed------------------------------
func SetServerAddr(addr string) error {
serverAddr = addr
return nil
}
func SetDB(database database.Db) error {
db = database
return nil
}
//-=-----------------------------------------
// array sorting implementation
type byBlockID []notaryapi.DBlock
func (f byBlockID) Len() int {
return len(f)
}
func (f byBlockID) Less(i, j int) bool {
return f[i].Header.BlockID > f[j].Header.BlockID
}
func (f byBlockID) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
// array sorting implementation
type byEBlockID []notaryapi.EBlock
func (f byEBlockID) Len() int {
return len(f)
}
func (f byEBlockID) Less(i, j int) bool {
return f[i].Header.BlockID > f[j].Header.BlockID
}
func (f byEBlockID) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}