Skip to content

Commit

Permalink
Add support for decentralized treasury.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Sep 21, 2020
1 parent 84962d3 commit f98c13b
Show file tree
Hide file tree
Showing 40 changed files with 1,123 additions and 232 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ vendor
.vscode
.idea
rpc/tools/bin/
*.sw*
29 changes: 29 additions & 0 deletions chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ func (s *Syncer) Run(ctx context.Context) (err error) {
return err
}

// Populate tspends.
tspends, err := s.rpc.GetMempoolTSpends(ctx)
if err != nil {
return err
}
for _, v := range tspends {
s.wallet.AddTSpend(*v)
}
log.Tracef("TSpends in mempool: %v", len(tspends))

// Request notifications for mempool tspennd arrivals.
err = s.rpc.Call(ctx, "notifytspend", nil)
if err != nil {
return err
}

// Fetch new headers and cfilters from the server.
locators, err := s.wallet.BlockLocators(ctx, nil)
if err != nil {
Expand Down Expand Up @@ -576,6 +592,11 @@ func (n *notifier) Notify(method string, params json.RawMessage) error {
if err != nil {
log.Error(errors.E(op, err))
}
case "tspend":
err := s.storeTSpend(ctx, params)
if err != nil {
log.Error(errors.E(op, err))
}
}
return nil
}
Expand Down Expand Up @@ -673,3 +694,11 @@ func (s *Syncer) spentAndMissedTickets(ctx context.Context, params json.RawMessa
}
return s.wallet.RevokeOwnedTickets(ctx, missed)
}

func (s *Syncer) storeTSpend(ctx context.Context, params json.RawMessage) error {
tx, err := dcrd.TSpend(params)
if err != nil {
return err
}
return s.wallet.AddTSpend(*tx)
}
50 changes: 50 additions & 0 deletions cmd/treasurykey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
treasurykey
===========

THIS TOOL IS FOR NETWORK OPERATORS ONLY.

**DO NOT USE**

The `treasurykey` utility is used to generate treasury keys that enable
spending from the treasury account.

The tool takes a single flag to designate which network it is generating a key
for.

It prints out three values:
1. A serialized private key
2. The serialized public key
3. The WIF (Wallet Interchange Format) version of the private key.

This information must be stored in a secure location.

The public key will be hardcoded inside `dcrd` and `dcrwallet`. Both daemons
will use it to identify valid treasury spend transactions.

The WIF must be imported into the dcrwallet that will generate TSPEND
transactions.

For example:

Generate key.
```
$ treasurykey -mainnet
Private key: 9bcf82e4b267585b3f0c54632d7e8eef2fc13407dfdbf7a80398085f5851baa0
Public key: 03f31aa7013b3c3e8568eb6415a895d8662b5ddceff12a62bf77c01e3e451a332f
WIF : PmQeT3VmoxDRjgsfwVJLV13ioNyxmd9XuQmQu86UPiQQAn5XkRfLN
```

Import key into treasury wallet.
```
dcrctl --wallet importprivkey PmQeT3VmoxDRjgsfwVJLV13ioNyxmd9XuQmQu86UPiQQAn5XkRfLN imported false
```

It is suggested to repeat this process so that there are at least two valid
keys.

Why?
====

This approach was chosen in order to generate *independent* random keys. If the
treasury wallet machine were to be compromised the seed would be compromised as
well.
69 changes: 69 additions & 0 deletions cmd/treasurykey/treasurykey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/dcrec"
"github.com/decred/dcrd/dcrec/secp256k1/v3"
"github.com/decred/dcrd/dcrutil/v3"
)

func generateKeys(params *chaincfg.Params) error {
key, err := secp256k1.GeneratePrivateKey()
if err != nil {
return err
}

keyBytes := key.Serialize()
wif, err := dcrutil.NewWIF(keyBytes, params.PrivateKeyID,
dcrec.STSchnorrSecp256k1)
if err != nil {
return err
}

fmt.Printf("Private key: %x\n", keyBytes)
fmt.Printf("Public key: %x\n", key.PubKey().SerializeCompressed())
fmt.Printf("WIF : %s\n", wif)

return nil
}

func main() {
mainnet := flag.Bool("mainnet", false, "use mainnet parameters")
simnet := flag.Bool("simnet", false, "use simnet parameters")
regnet := flag.Bool("regnet", false, "use regnet parameters")
testnet := flag.Bool("testnet", false, "use testnet parameters")
flag.Parse()

var net *chaincfg.Params
flags := 0
if *mainnet {
flags++
net = chaincfg.MainNetParams()
}
if *testnet {
flags++
net = chaincfg.TestNet3Params()
}
if *simnet {
flags++
net = chaincfg.SimNetParams()
}
if *regnet {
flags++
net = chaincfg.RegNetParams()
}
if flags != 1 {
fmt.Println("One and only one flag must be selected")
flag.Usage()
os.Exit(1)
}

if err := generateKeys(net); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
34 changes: 17 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ go 1.14
require (
decred.org/cspp v0.3.0
github.com/decred/dcrd/addrmgr v1.1.0
github.com/decred/dcrd/blockchain/stake/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/blockchain/standalone v1.1.0
github.com/decred/dcrd/blockchain/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/certgen v1.1.0
github.com/decred/dcrd/chaincfg/chainhash v1.0.2
github.com/decred/dcrd/chaincfg/v3 v3.0.0-20200903180623-3c2f8ad6a320
github.com/decred/dcrd/connmgr/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/crypto/blake256 v1.0.0
github.com/decred/dcrd/dcrec v1.0.0
github.com/decred/dcrd/dcrec/secp256k1/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/dcrjson/v3 v3.0.1
github.com/decred/dcrd/dcrutil/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/gcs/v2 v2.0.2-0.20200608124004-b2f67c2dc475
github.com/decred/dcrd/hdkeychain/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/rpc/jsonrpc/types/v2 v2.0.0
github.com/decred/dcrd/txscript/v3 v3.0.0-20200608124004-b2f67c2dc475
github.com/decred/dcrd/wire v1.3.0
github.com/decred/dcrd/blockchain/stake/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/blockchain/standalone/v2 v2.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/blockchain/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/certgen v1.1.1-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/chaincfg/chainhash v1.0.3-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/chaincfg/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/connmgr/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/crypto/blake256 v1.0.1-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/dcrec v1.0.1-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/dcrec/secp256k1/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/dcrjson/v3 v3.0.2-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/dcrutil/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/gcs/v2 v2.0.2-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/hdkeychain/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/rpc/jsonrpc/types/v2 v2.0.1-0.20200921185235-6d75c7ec1199
github.com/decred/dcrd/txscript/v3 v3.0.0-20200921185235-6d75c7ec1199
github.com/decred/dcrd/wire v1.3.1-0.20200921185235-6d75c7ec1199
github.com/decred/go-socks v1.1.0
github.com/decred/slog v1.1.0
github.com/golang/protobuf v1.3.2
Expand Down
Loading

0 comments on commit f98c13b

Please sign in to comment.