-
Notifications
You must be signed in to change notification settings - Fork 13
/
issue.go
218 lines (179 loc) · 6.2 KB
/
issue.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2014-2018 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/binary"
"encoding/hex"
"fmt"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/keypair"
"github.com/bitmark-inc/bitmarkd/merkle"
"github.com/bitmark-inc/bitmarkd/pay"
"github.com/bitmark-inc/bitmarkd/reservoir"
"github.com/bitmark-inc/bitmarkd/rpc"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/sha3"
"io"
"time"
)
var (
ErrMakeIssueFail = fault.ProcessError("make issue failed")
)
type IssueData struct {
Issuer *keypair.KeyPair
AssetId *transactionrecord.AssetIdentifier
Quantity int
FreeIssue bool
}
// JSON data to output after asset/issue/proof completes
type IssueReply struct {
AssetId transactionrecord.AssetIdentifier `json:"assetId"`
IssueIds []merkle.Digest `json:"issueIds"`
PayId pay.PayId `json:"payId"`
PayNonce reservoir.PayNonce `json:"payNonce"`
Difficulty string `json:"difficulty"`
SubmittedNonce string `json:"submittedNonce"`
ProofStatus reservoir.TrackingStatus `json:"proofStatus"`
Payments map[string]transactionrecord.PaymentAlternative `json:"payments,omitempty"`
Commands map[string]string `json:"commands,omitempty"`
}
func (client *Client) Issue(issueConfig *IssueData) (*IssueReply, error) {
if issueConfig.FreeIssue && 1 != issueConfig.Quantity {
return nil, fmt.Errorf("quantity: %d > 1 is not allowed for free", issueConfig.Quantity)
}
nonce := time.Now().UTC().Unix() * 1000
if issueConfig.FreeIssue {
nonce = 0 // only the zero nonce is allowed for free issue
}
issues := make([]*transactionrecord.BitmarkIssue, issueConfig.Quantity)
for i := 0; i < len(issues); i += 1 {
issue, err := makeIssue(client.testnet, issueConfig, uint64(nonce)+uint64(i))
if nil != err {
return nil, err
}
if nil == issue {
return nil, ErrMakeIssueFail
}
issues[i] = issue
}
client.printJson("Issue Request", issues)
issuesArgs := rpc.CreateArguments{
Assets: nil,
Issues: issues,
}
var issuesReply rpc.CreateReply
if err := client.client.Call("Bitmarks.Create", issuesArgs, &issuesReply); err != nil {
return nil, err
}
client.printJson("Issue Reply", issuesReply)
// make response
response := IssueReply{
AssetId: issues[0].AssetId, // Note: all issues are for the same asset
IssueIds: make([]merkle.Digest, len(issues)),
PayId: issuesReply.PayId,
PayNonce: issuesReply.PayNonce,
Difficulty: issuesReply.Difficulty,
Payments: issuesReply.Payments,
SubmittedNonce: "",
}
if nil != issuesReply.Payments && len(issuesReply.Payments) > 0 {
tpid, err := issuesReply.PayId.MarshalText()
if nil != err {
return nil, err
}
commands := make(map[string]string)
for _, payment := range issuesReply.Payments {
currency := payment[0].Currency
commands[currency.String()] = paymentCommand(client.testnet, currency, string(tpid), payment)
}
response.Commands = commands
} else {
// run proofer to generate local nonce
localNonce := makeProof(issuesReply.PayId, issuesReply.PayNonce, issuesReply.Difficulty, client.verbose, client.handle)
proofArgs := rpc.ProofArguments{
PayId: issuesReply.PayId,
Nonce: localNonce,
}
client.printJson("Proof Request", proofArgs)
var proofReply rpc.ProofReply
if err := client.client.Call("Bitmarks.Proof", &proofArgs, &proofReply); err != nil {
return nil, err
}
client.printJson("Proof Reply", proofReply)
response.SubmittedNonce = proofArgs.Nonce
response.ProofStatus = proofReply.Status
}
for i := 0; i < len(issuesReply.Issues); i++ {
response.IssueIds[i] = issuesReply.Issues[i].TxId
}
return &response, nil
}
// build a properly signed issues
func makeIssue(testnet bool, issueConfig *IssueData, nonce uint64) (*transactionrecord.BitmarkIssue, error) {
_, issue, err := internalMakeIssue(testnet, issueConfig, nonce, false)
return issue, err
}
func makeIssueTxId(testnet bool, issueConfig *IssueData, nonce uint64) (*merkle.Digest, error) {
digest, _, err := internalMakeIssue(testnet, issueConfig, nonce, true)
return digest, err
}
func internalMakeIssue(testnet bool, issueConfig *IssueData, nonce uint64, generateDigest bool) (*merkle.Digest, *transactionrecord.BitmarkIssue, error) {
issuerAddress := makeAddress(issueConfig.Issuer, testnet)
r := transactionrecord.BitmarkIssue{
AssetId: *issueConfig.AssetId,
Owner: issuerAddress,
Nonce: nonce,
Signature: nil,
}
// pack without signature
packed, err := r.Pack(issuerAddress)
if nil == err {
return nil, nil, ErrMakeTransferFail
} else if fault.ErrInvalidSignature != err {
return nil, nil, err
}
// manually sign the record and attach signature
signature := ed25519.Sign(issueConfig.Issuer.PrivateKey, packed)
r.Signature = signature[:]
// check that signature is correct by packing again
pkFull, err := r.Pack(issuerAddress)
if nil != err {
return nil, nil, err
}
if generateDigest {
digest := merkle.NewDigest(pkFull)
return &digest, &r, nil
}
return nil, &r, nil
}
// determine the nonce as a hex string
func makeProof(payId pay.PayId, payNonce reservoir.PayNonce, difficulty string, verbose bool, handle io.Writer) string {
nonce := uint64(12345)
nonceBuffer := make([]byte, 8)
start := time.Now()
hashCount := 0
for {
hashCount += 1
nonce += 113113
binary.BigEndian.PutUint64(nonceBuffer, nonce)
// compute hash
h := sha3.New256()
h.Write(payId[:])
h.Write(payNonce[:])
h.Write(nonceBuffer)
var digest [32]byte
h.Sum(digest[:0])
if 0 == digest[0]|digest[1] {
if verbose {
hps := float64(hashCount) / time.Now().Sub(start).Seconds() / 1.0e6
fmt.Fprintf(handle, "%f MH/s: possible nonce: %x with digest: %x\n", hps, nonceBuffer, digest)
}
hexDigest := hex.EncodeToString(digest[:])
if hexDigest <= difficulty {
return hex.EncodeToString(nonceBuffer)
}
}
}
}