-
Notifications
You must be signed in to change notification settings - Fork 13
/
grant.go
155 lines (126 loc) · 4.14 KB
/
grant.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
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
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpccalls
import (
"encoding/hex"
"golang.org/x/crypto/ed25519"
"github.com/bitmark-inc/bitmarkd/account"
"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/rpc/share"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
)
// GrantData - data for a grant request
type GrantData struct {
ShareId string
Quantity uint64
Owner *configuration.Private
Recipient *account.Account
BeforeBlock uint64
}
// GrantCountersignData - data to be countersigned
type GrantCountersignData struct {
Grant string
Recipient *configuration.Private
}
// GrantReply - JSON data to output after grant completes
type GrantReply struct {
GrantId merkle.Digest `json:"grantId"`
PayId pay.PayId `json:"payId"`
Payments map[string]transactionrecord.PaymentAlternative `json:"payments"`
Commands map[string]string `json:"commands,omitempty"`
}
// GrantSingleSignedReply - result from single signature
type GrantSingleSignedReply struct {
Identity string `json:"identity"`
Grant string `json:"grant"`
}
// Grant - perform the grant request
func (client *Client) Grant(grantConfig *GrantData) (*GrantSingleSignedReply, error) {
var shareId merkle.Digest
err := shareId.UnmarshalText([]byte(grantConfig.ShareId))
if nil != err {
return nil, err
}
if 0 == grantConfig.BeforeBlock {
info, err := client.GetBitmarkInfo()
if nil != err {
return nil, err
}
grantConfig.BeforeBlock = info.Block.Height + 100 // allow plenty of time to mine
}
packed, grant, err := makeGrantOneSignature(client.testnet, shareId, grantConfig.Quantity, grantConfig.Owner, grantConfig.Recipient, grantConfig.BeforeBlock)
if nil != err {
return nil, err
}
if nil == grant {
return nil, fault.MakeGrantFailed
}
client.printJson("Grant Request", grant)
response := GrantSingleSignedReply{
Identity: grant.Owner.String(),
Grant: hex.EncodeToString(packed),
}
return &response, nil
}
// CountersignGrant - perform the countersignature
func (client *Client) CountersignGrant(grant *transactionrecord.ShareGrant) (*GrantReply, error) {
client.printJson("Grant Request", grant)
var reply share.GrantReply
err := client.client.Call("Share.Grant", grant, &reply)
if nil != err {
return nil, err
}
tpid, err := reply.PayId.MarshalText()
if nil != err {
return nil, err
}
commands := make(map[string]string)
for _, payment := range reply.Payments {
currency := payment[0].Currency
commands[currency.String()] = paymentCommand(client.testnet, currency, string(tpid), payment)
}
client.printJson("Grant Reply", reply)
// make response
response := GrantReply{
GrantId: reply.TxId,
PayId: reply.PayId,
Payments: reply.Payments,
Commands: commands,
}
return &response, nil
}
func makeGrantOneSignature(testnet bool, shareId merkle.Digest, quantity uint64, owner *configuration.Private, recipient *account.Account, beforeBlock uint64) ([]byte, *transactionrecord.ShareGrant, error) {
ownerAccount := owner.PrivateKey.Account()
r := transactionrecord.ShareGrant{
ShareId: shareId,
Quantity: quantity,
Owner: ownerAccount,
Recipient: recipient,
BeforeBlock: beforeBlock,
Signature: nil,
Countersignature: nil,
}
// pack without signature
packed, err := r.Pack(ownerAccount)
if nil == err {
return nil, nil, fault.MakeGrantFailed
} else if fault.InvalidSignature != err {
return nil, nil, err
}
// attach signature
signature := ed25519.Sign(owner.PrivateKey.PrivateKeyBytes(), packed)
r.Signature = signature[:]
// include first signature by packing again
packed, err = r.Pack(ownerAccount)
if nil == err {
return nil, nil, fault.MakeGrantFailed
} else if fault.InvalidSignature != err {
return nil, nil, err
}
return packed, &r, nil
}