-
Notifications
You must be signed in to change notification settings - Fork 3
/
record.go
94 lines (77 loc) · 2.27 KB
/
record.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
88
89
90
91
92
93
94
package record
import (
"encoding/hex"
"github.com/irisnet/core-sdk-go/common/codec"
"github.com/irisnet/core-sdk-go/common/codec/types"
sdk "github.com/irisnet/core-sdk-go/types"
)
type recordClient struct {
sdk.BaseClient
codec.Marshaler
}
func NewClient(bc sdk.BaseClient, cdc codec.Marshaler) Client {
return recordClient{
BaseClient: bc,
Marshaler: cdc,
}
}
func (r recordClient) Name() string {
return ModuleName
}
func (r recordClient) RegisterInterfaceTypes(registry types.InterfaceRegistry) {
RegisterInterfaces(registry)
}
func (r recordClient) CreateRecord(request CreateRecordRequest, baseTx sdk.BaseTx) (CreateRecordResp, sdk.Error) {
creator, err := r.QueryAddress(baseTx.From, baseTx.Password)
createRecordResp := CreateRecordResp{}
if err != nil {
return createRecordResp, sdk.Wrap(err)
}
msg := &MsgCreateRecord{
Contents: request.Contents,
Creator: creator.String(),
}
res, err := r.BuildAndSend([]sdk.Msg{msg}, baseTx)
if err != nil {
return createRecordResp, err
}
if res.Events != nil {
recordID, er := res.Events.GetValue(eventTypeCreateRecord, attributeKeyRecordID)
if er != nil {
return createRecordResp, sdk.Wrap(er)
}
createRecordResp.RecordId = recordID
}
createRecordResp.Hash = res.Hash
createRecordResp.Height = res.Height
createRecordResp.GasUsed = res.GasUsed
createRecordResp.GasWanted = res.GasWanted
return createRecordResp, nil
}
func (r recordClient) QueryRecord(request QueryRecordReq) (QueryRecordResp, sdk.Error) {
rID, err := hex.DecodeString(request.RecordID)
if err != nil {
return QueryRecordResp{}, sdk.Wrapf("invalid record id, must be hex encoded string,but got %s", request.RecordID)
}
recordKey := GetRecordKey(rID)
res, err := r.QueryStore(recordKey, ModuleName, request.Height, request.Prove)
if err != nil {
return QueryRecordResp{}, sdk.Wrap(err)
}
var record Record
if err := r.Marshaler.UnmarshalBinaryBare(res.Value, &record); err != nil {
return QueryRecordResp{}, sdk.Wrap(err)
}
result := record.Convert().(QueryRecordResp)
var proof []byte
if request.Prove {
proof = r.MustMarshalJSON(res.ProofOps)
}
result.Proof = sdk.ProofValue{
Proof: proof,
Path: []string{ModuleName, string(recordKey)},
Value: res.Value,
}
result.Height = res.Height
return result, nil
}