/
coinjoin.go
423 lines (385 loc) · 9.76 KB
/
coinjoin.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Package coinjoin defines a builder type for creating Decred CoinJoin transactions.
package coinjoin
import (
"bytes"
"context"
"crypto/rand"
"encoding/binary"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
mathrand "math/rand"
"strings"
"sync"
"time"
"github.com/decred/dcrd/wire"
)
var shuffleRand *mathrand.Rand
var shuffleMu sync.Mutex
func init() {
buf := make([]byte, 8)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
seed := int64(binary.LittleEndian.Uint64(buf))
shuffleRand = mathrand.New(mathrand.NewSource(seed))
}
func shuffle(n int, swap func(i, j int)) {
shuffleMu.Lock()
shuffleRand.Shuffle(n, swap)
shuffleMu.Unlock()
}
// Caller performs a RPC method call.
type Caller interface {
Call(ctx context.Context, method string, res interface{}, args ...interface{}) error
}
// Tx is a Decred CoinJoin transaction builder. It is intended for usage by
// CoinJoin servers, not clients.
type Tx struct {
Tx wire.MsgTx
c Caller
sc ScriptClass
inputPids []int
mixValue int64
feeRate int64
txVersion uint16
lockTime uint32
expiry uint32
}
type ScriptClass int
// Recognized script classes
const (
P2PKHv0 ScriptClass = iota
P2SHv0
numScriptClasses
)
func (sc ScriptClass) Match(script []byte, vers uint16) bool {
// Only recognize v0 scripts currently
if vers != 0 {
return false
}
var match bool
switch sc {
case P2PKHv0:
match = len(script) == 25
match = match && script[0] == 0x76 // DUP
match = match && script[1] == 0xa9 // HASH160
match = match && script[2] == 20 // DATA20
match = match && script[23] == 0x88 // EQUALVERIFY
match = match && script[24] == 0xac // CHECKSIG
case P2SHv0:
match = len(script) == 23
match = match && script[0] == 0xa9 // HASH160
match = match && script[1] == 20 // DATA20
match = match && script[22] == 0x87 // EQUAL
}
return match
}
func (sc ScriptClass) script(message []byte) []byte {
switch sc {
case P2PKHv0:
s := []byte{
0: 0x76, // DUP
1: 0xa9, // HASH160
2: 20, // DATA20
23: 0x88, // EQUALVERIFY
24: 0xac, // CHECKSIG
}
copy(s[3:23], message)
return s
case P2SHv0:
s := []byte{
0: 0xa9, // HASH160
1: 20, // DATA20
22: 0x87, // EQUALVERIFY
}
copy(s[2:22], message)
return s
default:
panic("unreachable")
}
}
func (sc ScriptClass) scriptSize() int {
switch sc {
case P2PKHv0:
return 25
case P2SHv0:
return 23
default:
panic("unreachable")
}
}
func (sc ScriptClass) version() uint16 { return 0 }
const descname = "coinjoin-decred"
// EncodeDesc encodes a description defining the parameters for a Decred coinjoin.
func EncodeDesc(sc ScriptClass, amount int64, txVersion uint16, lockTime, expiry uint32) []byte {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
encode := func(value interface{}) {
if err := enc.Encode(value); err != nil {
panic(err)
}
}
encode(descname)
encode(int(sc))
encode(amount)
encode(txVersion)
encode(lockTime)
encode(expiry)
return buf.Bytes()
}
// DecodeDesc decodes a description defining the parameters for a compatible coinjoin.
func DecodeDesc(desc []byte) (sc ScriptClass, amount int64, txVersion uint16, lockTime, expiry uint32, err error) {
buf := bytes.NewBuffer(desc)
dec := gob.NewDecoder(buf)
decode := func(out interface{}) {
if err == nil {
err = dec.Decode(out)
}
}
var ty string
decode(&ty)
if ty != descname {
err = errors.New("incompatible coinjoin")
return
}
decode(&sc)
decode(&amount)
decode(&txVersion)
decode(&lockTime)
decode(&expiry)
return
}
func NewTx(caller Caller, sc ScriptClass, amount, feeRate int64, txVersion uint16,
lockTime, expiry uint32) (*Tx, error) {
if sc >= numScriptClasses {
return nil, errors.New("unknown script class")
}
tx := &Tx{
Tx: wire.MsgTx{Version: txVersion},
c: caller,
sc: sc,
mixValue: amount,
feeRate: feeRate,
txVersion: txVersion,
lockTime: lockTime,
expiry: expiry,
}
return tx, nil
}
// MarshalBinary marshals the transaction in wire encoding.
func (t *Tx) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
buf.Grow(t.Tx.SerializeSize())
err := t.Tx.Serialize(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBinary unmarshals the transaction in wire encoding.
func (t *Tx) UnmarshalBinary(b []byte) error {
return t.Tx.Deserialize(bytes.NewReader(b))
}
func feeForSerializeSize(relayFeePerKb int64, txSerializeSize int) int64 {
fee := relayFeePerKb * int64(txSerializeSize) / 1000
if fee == 0 && relayFeePerKb > 0 {
fee = relayFeePerKb
}
const maxAmount = 21e6 * 1e8
if fee < 0 || fee > maxAmount {
fee = maxAmount
}
return fee
}
func (t *Tx) ValidateUnmixed(unmixed []byte, mcount int) error {
var fee int64
other := new(wire.MsgTx)
err := other.Deserialize(bytes.NewReader(unmixed))
if err != nil {
return err
}
if other.Version != t.txVersion {
return errors.New("coinjoin: different tx versions")
}
for _, out := range other.TxOut {
if !t.sc.Match(out.PkScript, out.Version) {
return errors.New("coinjoin: different script class")
}
fee -= out.Value
}
for _, in := range other.TxIn {
if err := verifyOutput(t.c, &in.PreviousOutPoint, in.ValueIn); err != nil {
var e *blameError
if errors.As(err, &e) {
return errors.New(e.s)
}
return err
}
fee += in.ValueIn
}
fee -= int64(mcount) * t.mixValue
bogusMixedOut := &wire.TxOut{
Value: t.mixValue,
Version: t.sc.version(),
PkScript: make([]byte, t.sc.scriptSize()),
}
for i := 0; i < mcount; i++ {
other.AddTxOut(bogusMixedOut)
}
requiredFee := feeForSerializeSize(t.feeRate, other.SerializeSize())
if fee < requiredFee {
return errors.New("coinjoin: unmixed transaction does not pay enough network fees")
}
return nil
}
// Join adds the inputs and outputs of a transaction in wire encoding to t.
// Inputs and outputs are associated with a peer id for blame assignment.
func (t *Tx) Join(unmixed []byte, pid int) error {
other := new(wire.MsgTx)
err := other.Deserialize(bytes.NewReader(unmixed))
if err != nil {
return err
}
if other.Version != t.txVersion {
return errors.New("coinjoin: different tx versions")
}
for _, out := range other.TxOut {
if !t.sc.Match(out.PkScript, out.Version) {
return errors.New("coinjoin: different script class")
}
}
for _, in := range other.TxIn {
if err := verifyOutput(t.c, &in.PreviousOutPoint, in.ValueIn); err != nil {
var e *blameError
if errors.As(err, &e) {
e.b = []int{pid}
}
return err
}
}
tx := &t.Tx
for _, in := range other.TxIn {
t.inputPids = append(t.inputPids, pid)
tx.TxIn = append(tx.TxIn, in)
}
tx.TxOut = append(tx.TxOut, other.TxOut...)
return nil
}
const rpcTimeout = 10 * time.Second
func verifyOutput(c Caller, outpoint *wire.OutPoint, value int64) error {
ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
defer cancel()
var res struct {
Value float64 `json:"value"`
}
err := c.Call(ctx, "gettxout", &res, outpoint.Hash.String(), outpoint.Index, outpoint.Tree)
if err != nil {
return err
}
if v := atoms(res.Value); v != value {
return &blameError{
fmt.Sprintf("output %v has wrong value %d (expected %d)", outpoint, value, v),
nil,
}
}
return nil
}
func atoms(f float64) int64 {
return int64(f*1e8 + 0.5)
}
// Mix adds an output with m as the PkScript. The output will use the builder's
// output value and script version.
func (t *Tx) Mix(m []byte) {
t.Tx.TxOut = append(t.Tx.TxOut, &wire.TxOut{
Value: t.mixValue,
Version: t.sc.version(),
PkScript: t.sc.script(m),
})
}
// Confirm extracts the signatures from peer pid in confirm and adds them the
// receiver.
//
// confirm must have type *Tx.
func (t *Tx) Confirm(confirm interface{}, pid int) error {
t2, ok := confirm.(*Tx)
if !ok {
return errors.New("coinjoin: confirmation is not a *Tx")
}
if len(t.Tx.TxIn) != len(t2.Tx.TxIn) {
return errors.New("coinjoin: peer added or removed inputs")
}
for i, inputPid := range t.inputPids {
if pid != inputPid {
continue
}
t.Tx.TxIn[i].SignatureScript = t2.Tx.TxIn[i].SignatureScript
}
return nil
}
// Shuffle randomly shuffles the transaction inputs and outputs.
// Randomness is obtained from a cryptographically-seeded PRNG.
// Must only be used before signing.
func (t *Tx) Shuffle() {
tx := &t.Tx
shuffle(len(tx.TxIn), func(i, j int) {
t.inputPids[i], t.inputPids[j] = t.inputPids[j], t.inputPids[i]
tx.TxIn[i], tx.TxIn[j] = tx.TxIn[j], tx.TxIn[i]
})
shuffle(len(tx.TxOut), func(i, j int) {
tx.TxOut[i], tx.TxOut[j] = tx.TxOut[j], tx.TxOut[i]
})
}
// PublishMix publishes a transaction using the dcrd sendrawtransaction RPC.
func (t *Tx) PublishMix(ctx context.Context) error {
b := new(strings.Builder)
err := t.Tx.Serialize(hex.NewEncoder(b))
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, rpcTimeout)
defer cancel()
err = t.c.Call(ctx, "sendrawtransaction", nil, b.String())
if err != nil {
// TODO: try to blame peers of the bad inputs/outputs
return err
}
return nil
}
type report struct {
TxHash string
Denomination int64
TotalInput int64
Fee int64
}
// Report returns an object that can be marshaled with reflection-based encoders
// such as encoding/json and encoding/gob. The object includes details about
// the current CoinJoin transaction.
func (t *Tx) Report() interface{} {
r := &report{
TxHash: t.Tx.TxHash().String(),
Denomination: t.mixValue,
}
var input, output int64
for _, in := range t.Tx.TxIn {
input += in.ValueIn
}
for _, out := range t.Tx.TxOut {
output += out.Value
}
r.TotalInput = input
r.Fee = input - output
return r
}
// Blamer describes misbehaving peer IDs.
type Blamer interface {
Blame() []int
}
type blameError struct {
s string
b []int
}
func (e *blameError) Error() string { return e.s }
func (e *blameError) Blame() []int { return e.b }