-
Notifications
You must be signed in to change notification settings - Fork 1
/
sunyata.go
610 lines (514 loc) · 19 KB
/
sunyata.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Package sunyata defines the basic types and functions of the sunyata
// system.
package sunyata
import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"math"
"math/big"
"math/bits"
"strconv"
"time"
)
// EphemeralLeafIndex is used as the LeafIndex of StateElements that are created
// and spent within the same block. Such elements do not require a proof of
// existence. They are, however, assigned a proper index and are incorporated
// into the state accumulator when the block is processed.
const EphemeralLeafIndex = math.MaxUint64
// A Hash256 is a generic 256-bit cryptographic hash.
type Hash256 [32]byte
// HashBytes computes the hash of b using sunyata's hash function.
func HashBytes(b []byte) Hash256 { return sha512.Sum512_256(b) }
// An Address is the hash of a public key.
type Address Hash256
// VoidAddress is an address whose signing key does not exist. Sending coins to
// this address ensures that they will never be recoverable by anyone.
var VoidAddress Address
// A BlockID uniquely identifies a block.
type BlockID Hash256
// MeetsTarget returns true if bid is not greater than t.
func (bid BlockID) MeetsTarget(t BlockID) bool {
return bytes.Compare(bid[:], t[:]) <= 0
}
// A TransactionID uniquely identifies a transaction.
type TransactionID Hash256
// A ChainIndex pairs a block's height with its ID.
type ChainIndex struct {
Height uint64
ID BlockID
}
// A PublicKey is an Ed25519 public key.
type PublicKey [32]byte
// A PrivateKey is an Ed25519 private key.
type PrivateKey []byte
// A Signature is an Ed25519 signature.
type Signature [64]byte
// Address returns the address corresponding to a public key.
func (pk PublicKey) Address() Address { return Address(HashBytes(pk[:])) }
// PublicKey returns the PublicKey corresponding to priv.
func (priv PrivateKey) PublicKey() (pk PublicKey) {
copy(pk[:], priv[32:])
return
}
// NewPrivateKeyFromSeed calculates a private key from a seed.
func NewPrivateKeyFromSeed(seed []byte) PrivateKey {
return PrivateKey(ed25519.NewKeyFromSeed(seed))
}
// GeneratePrivateKey creates a new private key from a secure entropy source.
func GeneratePrivateKey() PrivateKey {
seed := make([]byte, ed25519.SeedSize)
rand.Read(seed)
pk := NewPrivateKeyFromSeed(seed)
for i := range seed {
seed[i] = 0
}
return pk
}
// SignHash signs h with priv, producing a Signature.
func (priv PrivateKey) SignHash(h Hash256) (s Signature) {
copy(s[:], ed25519.Sign(ed25519.PrivateKey(priv), h[:]))
return
}
// VerifyHash verifies that s is a valid signature of h by pk.
func (pk PublicKey) VerifyHash(h Hash256, s Signature) bool {
return ed25519.Verify(pk[:], h[:], s[:])
}
// An Output is a volume of currency that is created and spent as an atomic
// unit.
type Output struct {
Value Currency
Address Address
}
// An ElementID uniquely identifies a StateElement.
type ElementID struct {
Source Hash256 // BlockID or TransactionID
Index uint64
}
// A StateElement is a generic element within the state accumulator.
type StateElement struct {
ID ElementID
LeafIndex uint64
MerkleProof []Hash256
}
// An OutputElement is a volume of currency that is created and spent as an
// atomic unit.
type OutputElement struct {
StateElement
Output
MaturityHeight uint64
}
// An Input spends its parent Output by revealing its public key and signing the
// transaction.
type Input struct {
Parent OutputElement
PublicKey PublicKey
Signature Signature
}
// A Transaction transfers value by consuming existing Outputs and creating new
// Outputs.
type Transaction struct {
Inputs []Input
Outputs []Output
MinerFee Currency
}
// ID returns the "semantic hash" of the transaction, covering all of the
// transaction's effects, but not incidental data such as signatures or Merkle
// proofs. This ensures that the ID will remain stable (i.e. non-malleable).
//
// To hash all of the data in a transaction, use the EncodeTo method.
func (txn *Transaction) ID() TransactionID {
h := hasherPool.Get().(*Hasher)
defer hasherPool.Put(h)
h.Reset()
h.E.WriteString("sunyata/id/transaction")
h.E.WritePrefix(len(txn.Inputs))
for _, in := range txn.Inputs {
in.Parent.ID.EncodeTo(h.E)
}
h.E.WritePrefix(len(txn.Outputs))
for _, out := range txn.Outputs {
out.EncodeTo(h.E)
}
txn.MinerFee.EncodeTo(h.E)
return TransactionID(h.Sum())
}
// DeepCopy returns a copy of txn that does not alias any of its memory.
func (txn *Transaction) DeepCopy() Transaction {
c := *txn
c.Inputs = append([]Input(nil), c.Inputs...)
for i := range c.Inputs {
c.Inputs[i].Parent.MerkleProof = append([]Hash256(nil), c.Inputs[i].Parent.MerkleProof...)
}
c.Outputs = append([]Output(nil), c.Outputs...)
return c
}
// OutputID returns the ID of the output at index i.
func (txn *Transaction) OutputID(i int) ElementID {
return ElementID{
Source: Hash256(txn.ID()),
Index: uint64(i),
}
}
// EphemeralOutputElement returns txn.Outputs[i] as an ephemeral OutputElement.
func (txn *Transaction) EphemeralOutputElement(i int) OutputElement {
return OutputElement{
StateElement: StateElement{
ID: txn.OutputID(0),
LeafIndex: EphemeralLeafIndex,
},
Output: txn.Outputs[0],
}
}
// A BlockHeader contains a Block's non-transaction data.
type BlockHeader struct {
Height uint64
ParentID BlockID
Nonce uint64
Timestamp time.Time
MinerAddress Address
Commitment Hash256
}
// Index returns the header's chain index.
func (h BlockHeader) Index() ChainIndex {
return ChainIndex{
Height: h.Height,
ID: h.ID(),
}
}
// ParentIndex returns the index of the header's parent.
func (h BlockHeader) ParentIndex() ChainIndex {
return ChainIndex{
Height: h.Height - 1,
ID: h.ParentID,
}
}
// ID returns a hash that uniquely identifies a block.
func (h BlockHeader) ID() BlockID {
buf := make([]byte, 16+8+8+32)
copy(buf[0:], "sunyata/id/block")
binary.LittleEndian.PutUint64(buf[16:], h.Nonce)
binary.LittleEndian.PutUint64(buf[24:], uint64(h.Timestamp.Unix()))
copy(buf[32:], h.Commitment[:])
return BlockID(HashBytes(buf))
}
// CurrentTimestamp returns the current time, rounded to the nearest second.
func CurrentTimestamp() time.Time { return time.Now().Round(time.Second) }
// A Block is a set of transactions grouped under a header.
type Block struct {
Header BlockHeader
Transactions []Transaction
}
// ID returns a hash that uniquely identifies a block. It is equivalent to
// b.Header.ID().
func (b *Block) ID() BlockID { return b.Header.ID() }
// Index returns the block's chain index. It is equivalent to b.Header.Index().
func (b *Block) Index() ChainIndex { return b.Header.Index() }
// MinerOutputID returns the output ID of the miner payout.
func (b *Block) MinerOutputID() ElementID {
return ElementID{
Source: Hash256(b.ID()),
Index: 0,
}
}
// Work represents a quantity of work.
type Work struct {
// The representation is the expected number of hashes required to produce a
// given hash, in big-endian order.
NumHashes [32]byte
}
// Add returns w+v, wrapping on overflow.
func (w Work) Add(v Work) Work {
var r Work
var sum, c uint64
for i := 24; i >= 0; i -= 8 {
wi := binary.BigEndian.Uint64(w.NumHashes[i:])
vi := binary.BigEndian.Uint64(v.NumHashes[i:])
sum, c = bits.Add64(wi, vi, c)
binary.BigEndian.PutUint64(r.NumHashes[i:], sum)
}
return r
}
// Sub returns w-v, wrapping on underflow.
func (w Work) Sub(v Work) Work {
var r Work
var sum, c uint64
for i := 24; i >= 0; i -= 8 {
wi := binary.BigEndian.Uint64(w.NumHashes[i:])
vi := binary.BigEndian.Uint64(v.NumHashes[i:])
sum, c = bits.Sub64(wi, vi, c)
binary.BigEndian.PutUint64(r.NumHashes[i:], sum)
}
return r
}
// Mul64 returns w*v, wrapping on overflow.
func (w Work) Mul64(v uint64) Work {
var r Work
var c uint64
for i := 24; i >= 0; i -= 8 {
wi := binary.BigEndian.Uint64(w.NumHashes[i:])
hi, prod := bits.Mul64(wi, v)
prod, cc := bits.Add64(prod, c, 0)
c = hi + cc
binary.BigEndian.PutUint64(r.NumHashes[i:], prod)
}
return r
}
// Div64 returns w/v.
func (w Work) Div64(v uint64) Work {
var r Work
var quo, rem uint64
for i := 0; i < len(w.NumHashes); i += 8 {
wi := binary.BigEndian.Uint64(w.NumHashes[i:])
quo, rem = bits.Div64(rem, wi, v)
binary.BigEndian.PutUint64(r.NumHashes[i:], quo)
}
return r
}
// Cmp compares two work values.
func (w Work) Cmp(v Work) int {
return bytes.Compare(w.NumHashes[:], v.NumHashes[:])
}
// WorkRequiredForHash estimates how much work was required to produce the given
// id. Note that the mapping is not injective; many different ids may require
// the same expected amount of Work.
func WorkRequiredForHash(id BlockID) Work {
if id == (BlockID{}) {
// This should never happen as long as inputs are properly validated and
// the laws of physics are intact.
panic("impossibly good BlockID")
}
// As a special case, this hash requires the maximum possible amount of
// Work. (Otherwise, the division would produce 2^256, which overflows our
// representation.)
if id == ([32]byte{31: 1}) {
return Work{
NumHashes: [32]byte{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
},
}
}
// To get the expected number of hashes required, simply divide 2^256 by id.
//
// TODO: write a zero-alloc uint256 division instead of using big.Int
maxTarget := new(big.Int).Lsh(big.NewInt(1), 256)
idInt := new(big.Int).SetBytes(id[:])
quo := maxTarget.Div(maxTarget, idInt)
var w Work
quo.FillBytes(w.NumHashes[:])
return w
}
// HashRequiringWork returns the best BlockID that the given amount of Work
// would be expected to produce. Note that many different BlockIDs may require
// the same amount of Work; this function returns the lowest of them.
func HashRequiringWork(w Work) BlockID {
if w.NumHashes == ([32]byte{}) {
panic("no hash requires zero work")
}
// As a special case, 1 Work produces this hash. (Otherwise, the division
// would produce 2^256, which overflows our representation.)
if w.NumHashes == ([32]byte{31: 1}) {
return BlockID{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
}
}
maxTarget := new(big.Int).Lsh(big.NewInt(1), 256)
workInt := new(big.Int).SetBytes(w.NumHashes[:])
quo := maxTarget.Div(maxTarget, workInt)
var id BlockID
quo.FillBytes(id[:])
return id
}
// Implementations of fmt.Stringer, encoding.Text(Un)marshaler, and json.(Un)marshaler
func stringerHex(prefix string, data []byte) string {
return prefix + ":" + hex.EncodeToString(data[:])
}
func marshalHex(prefix string, data []byte) ([]byte, error) {
return []byte(stringerHex(prefix, data)), nil
}
func unmarshalHex(dst []byte, prefix string, data []byte) error {
n, err := hex.Decode(dst, bytes.TrimPrefix(data, []byte(prefix+":")))
if n < len(dst) {
err = io.EOF
}
if err != nil {
return fmt.Errorf("decoding %v:<hex> failed: %w", prefix, err)
}
return nil
}
func marshalJSONHex(prefix string, data []byte) ([]byte, error) {
return []byte(`"` + stringerHex(prefix, data) + `"`), nil
}
func unmarshalJSONHex(dst []byte, prefix string, data []byte) error {
return unmarshalHex(dst, prefix, bytes.Trim(data, `"`))
}
// String implements fmt.Stringer.
func (h Hash256) String() string { return stringerHex("h", h[:]) }
// MarshalText implements encoding.TextMarshaler.
func (h Hash256) MarshalText() ([]byte, error) { return marshalHex("h", h[:]) }
// UnmarshalText implements encoding.TextUnmarshaler.
func (h *Hash256) UnmarshalText(b []byte) error { return unmarshalHex(h[:], "h", b) }
// MarshalJSON implements json.Marshaler.
func (h Hash256) MarshalJSON() ([]byte, error) { return marshalJSONHex("h", h[:]) }
// UnmarshalJSON implements json.Unmarshaler.
func (h *Hash256) UnmarshalJSON(b []byte) error { return unmarshalJSONHex(h[:], "h", b) }
// String implements fmt.Stringer.
func (ci ChainIndex) String() string {
// use the 4 least-significant bytes of ID -- in a mature chain, the
// most-significant bytes will be zeros
return fmt.Sprintf("%d::%x", ci.Height, ci.ID[len(ci.ID)-4:])
}
// MarshalText implements encoding.TextMarshaler.
func (ci ChainIndex) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("%d::%x", ci.Height, ci.ID[:])), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (ci *ChainIndex) UnmarshalText(b []byte) (err error) {
parts := bytes.Split(b, []byte("::"))
if len(parts) != 2 {
return fmt.Errorf("decoding <height>::<id> failed: wrong number of separators")
} else if ci.Height, err = strconv.ParseUint(string(parts[0]), 10, 64); err != nil {
return fmt.Errorf("decoding <height>::<id> failed: %w", err)
} else if n, err := hex.Decode(ci.ID[:], parts[1]); err != nil {
return fmt.Errorf("decoding <height>::<id> failed: %w", err)
} else if n < len(ci.ID) {
return fmt.Errorf("decoding <height>::<id> failed: %w", io.EOF)
}
return nil
}
// ParseChainIndex parses a chain index from a string.
func ParseChainIndex(s string) (ci ChainIndex, err error) {
err = ci.UnmarshalText([]byte(s))
return
}
// String implements fmt.Stringer.
func (eid ElementID) String() string {
return fmt.Sprintf("elem:%x:%v", eid.Source[:], eid.Index)
}
// MarshalText implements encoding.TextMarshaler.
func (eid ElementID) MarshalText() ([]byte, error) { return []byte(eid.String()), nil }
// UnmarshalText implements encoding.TextUnmarshaler.
func (eid *ElementID) UnmarshalText(b []byte) (err error) {
parts := bytes.Split(b, []byte(":"))
if len(parts) != 3 {
return fmt.Errorf("decoding <hex>:<index> failed: wrong number of separators")
} else if n, err := hex.Decode(eid.Source[:], parts[1]); err != nil {
return fmt.Errorf("decoding <hex>:<index> failed: %w", err)
} else if n < len(eid.Source) {
return fmt.Errorf("decoding <hex>:<index> failed: %w", io.EOF)
} else if eid.Index, err = strconv.ParseUint(string(parts[2]), 10, 64); err != nil {
return fmt.Errorf("decoding <hex>:<index> failed: %w", err)
}
return nil
}
// String implements fmt.Stringer.
func (a Address) String() string {
checksum := HashBytes(a[:])
return stringerHex("addr", append(a[:], checksum[:6]...))
}
// MarshalText implements encoding.TextMarshaler.
func (a Address) MarshalText() ([]byte, error) { return []byte(a.String()), nil }
// UnmarshalText implements encoding.TextUnmarshaler.
func (a *Address) UnmarshalText(b []byte) (err error) {
withChecksum := make([]byte, 32+6)
n, err := hex.Decode(withChecksum, bytes.TrimPrefix(b, []byte("addr:")))
if err != nil {
err = fmt.Errorf("decoding addr:<hex> failed: %w", err)
} else if n != len(withChecksum) {
err = fmt.Errorf("decoding addr:<hex> failed: %w", io.EOF)
} else if checksum := HashBytes(withChecksum[:32]); !bytes.Equal(checksum[:6], withChecksum[32:]) {
err = errors.New("bad checksum")
}
copy(a[:], withChecksum[:32])
return
}
// MarshalJSON implements json.Marshaler.
func (a Address) MarshalJSON() ([]byte, error) {
checksum := HashBytes(a[:])
return marshalJSONHex("addr", append(a[:], checksum[:6]...))
}
// UnmarshalJSON implements json.Unmarshaler.
func (a *Address) UnmarshalJSON(b []byte) (err error) {
return a.UnmarshalText(bytes.Trim(b, `"`))
}
// ParseAddress parses an address from a prefixed hex encoded string.
func ParseAddress(s string) (a Address, err error) {
err = a.UnmarshalText([]byte(s))
return
}
// String implements fmt.Stringer.
func (bid BlockID) String() string { return stringerHex("bid", bid[:]) }
// MarshalText implements encoding.TextMarshaler.
func (bid BlockID) MarshalText() ([]byte, error) { return marshalHex("bid", bid[:]) }
// UnmarshalText implements encoding.TextUnmarshaler.
func (bid *BlockID) UnmarshalText(b []byte) error { return unmarshalHex(bid[:], "bid", b) }
// MarshalJSON implements json.Marshaler.
func (bid BlockID) MarshalJSON() ([]byte, error) { return marshalJSONHex("bid", bid[:]) }
// UnmarshalJSON implements json.Unmarshaler.
func (bid *BlockID) UnmarshalJSON(b []byte) error { return unmarshalJSONHex(bid[:], "bid", b) }
// String implements fmt.Stringer.
func (pk PublicKey) String() string { return stringerHex("ed25519", pk[:]) }
// MarshalText implements encoding.TextMarshaler.
func (pk PublicKey) MarshalText() ([]byte, error) { return marshalHex("ed25519", pk[:]) }
// UnmarshalText implements encoding.TextUnmarshaler.
func (pk *PublicKey) UnmarshalText(b []byte) error { return unmarshalHex(pk[:], "ed25519", b) }
// MarshalJSON implements json.Marshaler.
func (pk PublicKey) MarshalJSON() ([]byte, error) { return marshalJSONHex("ed25519", pk[:]) }
// UnmarshalJSON implements json.Unmarshaler.
func (pk *PublicKey) UnmarshalJSON(b []byte) error { return unmarshalJSONHex(pk[:], "ed25519", b) }
// String implements fmt.Stringer.
func (tid TransactionID) String() string { return stringerHex("txid", tid[:]) }
// MarshalText implements encoding.TextMarshaler.
func (tid TransactionID) MarshalText() ([]byte, error) { return marshalHex("txid", tid[:]) }
// UnmarshalText implements encoding.TextUnmarshaler.
func (tid *TransactionID) UnmarshalText(b []byte) error { return unmarshalHex(tid[:], "txid", b) }
// MarshalJSON implements json.Marshaler.
func (tid TransactionID) MarshalJSON() ([]byte, error) { return marshalJSONHex("txid", tid[:]) }
// UnmarshalJSON implements json.Unmarshaler.
func (tid *TransactionID) UnmarshalJSON(b []byte) error { return unmarshalJSONHex(tid[:], "txid", b) }
// String implements fmt.Stringer.
func (sig Signature) String() string { return stringerHex("sig", sig[:]) }
// MarshalText implements encoding.TextMarshaler.
func (sig Signature) MarshalText() ([]byte, error) { return marshalHex("sig", sig[:]) }
// UnmarshalText implements encoding.TextUnmarshaler.
func (sig *Signature) UnmarshalText(b []byte) error { return unmarshalHex(sig[:], "sig", b) }
// MarshalJSON implements json.Marshaler.
func (sig Signature) MarshalJSON() ([]byte, error) { return marshalJSONHex("sig", sig[:]) }
// UnmarshalJSON implements json.Unmarshaler.
func (sig *Signature) UnmarshalJSON(b []byte) error { return unmarshalJSONHex(sig[:], "sig", b) }
// String implements fmt.Stringer.
func (w Work) String() string { return new(big.Int).SetBytes(w.NumHashes[:]).String() }
// MarshalText implements encoding.TextMarshaler.
func (w Work) MarshalText() ([]byte, error) {
return new(big.Int).SetBytes(w.NumHashes[:]).MarshalText()
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (w *Work) UnmarshalText(b []byte) error {
i := new(big.Int)
if err := i.UnmarshalText(b); err != nil {
return err
} else if i.Sign() < 0 {
return errors.New("value cannot be negative")
} else if i.BitLen() > 256 {
return errors.New("value overflows Work representation")
}
i.FillBytes(w.NumHashes[:])
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (w *Work) UnmarshalJSON(b []byte) error {
return w.UnmarshalText(bytes.Trim(b, `"`))
}
// MarshalJSON implements json.Marshaler.
func (w Work) MarshalJSON() ([]byte, error) {
js, err := new(big.Int).SetBytes(w.NumHashes[:]).MarshalJSON()
return []byte(`"` + string(js) + `"`), err
}