-
Notifications
You must be signed in to change notification settings - Fork 19
/
raw.go
63 lines (53 loc) · 1.41 KB
/
raw.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
// 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/hex"
"encoding/json"
)
// RawData is a simple hex encoded byte string
type RawData struct {
Data string `json:"data"`
}
func (r *RawData) GetDataBytes() ([]byte, error) {
return hex.DecodeString(r.Data)
}
// GetRaw requests the raw data for any binary block kept in the factomd
// database.
func GetRaw(keymr string) ([]byte, error) {
params := hashRequest{Hash: keymr}
req := NewJSON2Request("raw-data", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
raw := new(RawData)
if err := json.Unmarshal(resp.JSONResult(), raw); err != nil {
return nil, err
}
return raw.GetDataBytes()
}
// SendRawMsg sends a raw hex encoded byte string for factomd to send as a
// binary message on the Factom Netwrork.
func SendRawMsg(message string) (string, error) {
param := messageRequest{Message: message}
req := NewJSON2Request("send-raw-message", APICounter(), param)
resp, err := factomdRequest(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", resp.Error
}
status := new(struct {
Message string `json:"message"`
})
if err := json.Unmarshal(resp.JSONResult(), status); err != nil {
return "", err
}
return status.Message, nil
}