Skip to content

Commit

Permalink
multi: changes for adding verifySeed jsonrpc call.
Browse files Browse the repository at this point in the history
A function deriveCoinTypeKey is created within methods.go that is used in another a new method of the jsonrpc serve VerifySeed.  All necessary changes to autogenerate the help for this rpc call are included as well.
  • Loading branch information
githubsands committed Apr 20, 2020
1 parent 2e37852 commit d62e8e5
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 2 deletions.
220 changes: 220 additions & 0 deletions dcrwallet.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
Sending build context to Docker daemon 125.4MB
Step 1/11 : FROM golang:1.13-alpine as builder
---> 3024b4e742b0
Step 2/11 : WORKDIR /go/src/github.com/decred/dcrwallet
---> Using cache
---> d46b40396d01
Step 3/11 : COPY . .
---> fca7fb3f4849
Step 4/11 : RUN apk add --no-cache --update alpine-sdk && mkdir -p go/bin && go install
---> Running in d2e681a9b021
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/37) Installing fakeroot (1.23-r0)
(2/37) Installing sudo (1.8.27-r0)
(3/37) Installing libcap (2.27-r0)
(4/37) Installing pax-utils (1.2.3-r0)
(5/37) Installing openssl (1.1.1d-r0)
(6/37) Installing libattr (2.4.48-r0)
(7/37) Installing attr (2.4.48-r0)
(8/37) Installing tar (1.32-r0)
(9/37) Installing pkgconf (1.6.1-r1)
(10/37) Installing patch (2.7.6-r6)
(11/37) Installing libgcc (8.3.0-r0)
(12/37) Installing libstdc++ (8.3.0-r0)
(13/37) Installing lzip (1.21-r0)
(14/37) Installing nghttp2-libs (1.39.2-r0)
(15/37) Installing libcurl (7.66.0-r0)
(16/37) Installing curl (7.66.0-r0)
(17/37) Installing abuild (3.4.0-r0)
Executing abuild-3.4.0-r0.pre-install
(18/37) Installing binutils (2.32-r0)
(19/37) Installing libmagic (5.37-r1)
(20/37) Installing file (5.37-r1)
(21/37) Installing gmp (6.1.2-r1)
(22/37) Installing isl (0.18-r0)
(23/37) Installing libgomp (8.3.0-r0)
(24/37) Installing libatomic (8.3.0-r0)
(25/37) Installing mpfr3 (3.1.5-r1)
(26/37) Installing mpc1 (1.1.0-r0)
(27/37) Installing gcc (8.3.0-r0)
(28/37) Installing musl-dev (1.1.22-r3)
(29/37) Installing libc-dev (0.7.1-r0)
(30/37) Installing g++ (8.3.0-r0)
(31/37) Installing make (4.2.1-r2)
(32/37) Installing fortify-headers (1.1-r0)
(33/37) Installing build-base (0.5-r1)
(34/37) Installing expat (2.2.8-r0)
(35/37) Installing pcre2 (10.33-r0)
(36/37) Installing git (2.22.0-r0)
(37/37) Installing alpine-sdk (1.0-r0)
Executing busybox-1.30.1-r2.trigger
OK: 182 MiB in 52 packages
go: downloading github.com/decred/dcrd/dcrec v1.0.0
go: downloading github.com/decred/dcrd/wire v1.3.0
go: downloading google.golang.org/grpc v1.22.0
go: downloading github.com/decred/dcrd/blockchain/standalone v1.1.0
go: downloading github.com/decred/dcrd/connmgr v1.0.2
go: downloading github.com/decred/go-socks v1.0.1-0.20191001171050-775b73941e73
go: downloading github.com/jrick/logrotate v1.0.0
go: downloading decred.org/cspp v0.1.3
go: downloading github.com/decred/dcrd/addrmgr v1.0.2
go: extracting github.com/decred/dcrd/dcrec v1.0.0
go: downloading github.com/decred/dcrd/chaincfg v1.5.2
go: extracting github.com/decred/dcrd/wire v1.3.0
go: extracting github.com/decred/dcrd/addrmgr v1.0.2
go: downloading github.com/decred/dcrd/txscript v1.1.0
go: downloading github.com/decred/dcrd/gcs v1.1.0
go: extracting github.com/jrick/logrotate v1.0.0
go: extracting github.com/decred/dcrd/connmgr v1.0.2
go: extracting decred.org/cspp v0.1.3
go: downloading golang.org/x/sync v0.0.0-20190423024810-112230192c58
go: extracting github.com/decred/dcrd/blockchain/standalone v1.1.0
go: extracting github.com/decred/go-socks v1.0.1-0.20191001171050-775b73941e73
go: downloading github.com/decred/dcrd/rpc/jsonrpc/types v1.0.1
go: extracting github.com/decred/dcrd/chaincfg v1.5.2
go: downloading github.com/decred/dcrd/chaincfg/chainhash v1.0.2
go: downloading github.com/decred/dcrd/chaincfg/v2 v2.3.0
go: downloading github.com/decred/dcrd/blockchain/stake v1.2.1
go: extracting github.com/decred/dcrd/gcs v1.1.0
go: extracting golang.org/x/sync v0.0.0-20190423024810-112230192c58
go: downloading github.com/decred/dcrd/connmgr/v2 v2.0.0
go: downloading github.com/decred/dcrd/hdkeychain/v2 v2.1.0
go: extracting github.com/decred/dcrd/chaincfg/chainhash v1.0.2
go: extracting github.com/decred/dcrd/rpc/jsonrpc/types v1.0.1
go: downloading github.com/decred/dcrd/dcrec/secp256k1 v1.0.2
go: downloading golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
go: extracting github.com/decred/dcrd/txscript v1.1.0
go: downloading github.com/decred/dcrd/txscript/v2 v2.1.0
go: extracting github.com/decred/dcrd/chaincfg/v2 v2.3.0
go: extracting google.golang.org/grpc v1.22.0
go: downloading github.com/decred/dcrd/certgen v1.1.0
go: downloading github.com/jessevdk/go-flags v1.4.0
go: extracting github.com/decred/dcrd/connmgr/v2 v2.0.0
go: downloading github.com/gorilla/websocket v1.4.1
go: downloading github.com/decred/dcrd/crypto/blake256 v1.0.0
go: downloading github.com/dchest/blake256 v1.0.0
go: extracting github.com/decred/dcrd/hdkeychain/v2 v2.1.0
go: downloading go.etcd.io/bbolt v1.3.3
go: extracting github.com/decred/dcrd/blockchain/stake v1.2.1
go: extracting github.com/decred/dcrd/dcrec/secp256k1 v1.0.2
go: downloading github.com/decred/dcrd/blockchain/stake/v2 v2.0.2
go: extracting github.com/decred/dcrd/txscript/v2 v2.1.0
go: extracting github.com/decred/dcrd/certgen v1.1.0
go: downloading github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0
go: downloading github.com/dchest/siphash v1.2.1
go: extracting github.com/jessevdk/go-flags v1.4.0
go: downloading github.com/jrick/wsrpc/v2 v2.1.5
go: extracting github.com/decred/dcrd/crypto/blake256 v1.0.0
go: extracting github.com/dchest/blake256 v1.0.0
go: downloading github.com/decred/base58 v1.0.1
go: downloading github.com/decred/dcrd/dcrjson/v3 v3.0.1
go: extracting github.com/gorilla/websocket v1.4.1
go: extracting go.etcd.io/bbolt v1.3.3
go: downloading github.com/decred/dcrd/dcrec/edwards v1.0.0
go: downloading github.com/jrick/bitset v1.0.0
go: extracting golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
go: extracting github.com/decred/dcrd/blockchain/stake/v2 v2.0.2
go: downloading github.com/decred/slog v1.0.0
go: extracting github.com/dchest/siphash v1.2.1
go: downloading github.com/decred/dcrd/database v1.1.0
go: extracting github.com/jrick/wsrpc/v2 v2.1.5
go: downloading github.com/decred/dcrd/dcrutil v1.4.0
go: downloading github.com/golang/protobuf v1.3.2
go: extracting github.com/decred/base58 v1.0.1
go: extracting github.com/decred/slog v1.0.0
go: extracting github.com/decred/dcrd/dcrutil v1.4.0
go: downloading golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
go: extracting github.com/decred/dcrd/dcrjson/v3 v3.0.1
go: extracting github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0
go: extracting github.com/jrick/bitset v1.0.0
go: downloading github.com/decred/dcrd/dcrutil/v2 v2.0.1
go: extracting github.com/decred/dcrd/dcrec/edwards v1.0.0
go: downloading golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7
go: downloading google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8
go: downloading github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0
go: extracting github.com/decred/dcrd/database v1.1.0
go: downloading github.com/decred/dcrd/database/v2 v2.0.1
go: downloading github.com/decred/dcrd/crypto/ripemd160 v1.0.0
go: extracting golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
go: extracting github.com/decred/dcrd/dcrutil/v2 v2.0.1
go: extracting github.com/golang/protobuf v1.3.2
go: downloading golang.org/x/sys v0.0.0-20191010194322-b09406accb47
go: extracting github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0
go: extracting github.com/decred/dcrd/crypto/ripemd160 v1.0.0
go: downloading github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412
go: extracting github.com/decred/dcrd/database/v2 v2.0.1
go: extracting github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412
go: extracting google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8
go: extracting golang.org/x/sys v0.0.0-20191010194322-b09406accb47
go: extracting golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7
go: downloading golang.org/x/text v0.3.2
go: extracting golang.org/x/text v0.3.2
go: finding github.com/decred/dcrd/certgen v1.1.0
go: finding github.com/decred/dcrd/chaincfg/v2 v2.3.0
go: finding github.com/decred/dcrd/dcrutil/v2 v2.0.1
go: finding github.com/decred/dcrd/chaincfg/chainhash v1.0.2
go: finding golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
go: finding github.com/decred/dcrd/addrmgr v1.0.2
go: finding github.com/decred/dcrd/hdkeychain/v2 v2.1.0
go: finding github.com/decred/slog v1.0.0
go: finding github.com/decred/dcrd/wire v1.3.0
go: finding github.com/decred/base58 v1.0.1
go: finding github.com/decred/dcrd/connmgr v1.0.2
go: finding github.com/decred/dcrd/crypto/blake256 v1.0.0
go: finding github.com/decred/dcrd/connmgr/v2 v2.0.0
go: finding golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
go: finding github.com/decred/dcrd/crypto/ripemd160 v1.0.0
go: finding github.com/decred/dcrd/dcrec v1.0.0
go: finding go.etcd.io/bbolt v1.3.3
go: finding decred.org/cspp v0.1.3
go: finding github.com/decred/dcrd/dcrec/edwards/v2 v2.0.0
go: finding golang.org/x/sys v0.0.0-20191010194322-b09406accb47
go: finding github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0
go: finding github.com/decred/dcrd/chaincfg v1.5.2
go: finding github.com/decred/dcrd/blockchain/stake/v2 v2.0.2
go: finding github.com/decred/dcrd/blockchain/standalone v1.1.0
go: finding github.com/decred/dcrd/dcrjson/v3 v3.0.1
go: finding github.com/decred/dcrd/txscript/v2 v2.1.0
go: finding github.com/decred/dcrd/rpc/jsonrpc/types v1.0.1
go: finding github.com/decred/dcrd/gcs v1.1.0
go: finding github.com/decred/dcrd/database/v2 v2.0.1
go: finding github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412
go: finding github.com/decred/go-socks v1.0.1-0.20191001171050-775b73941e73
go: finding github.com/dchest/blake256 v1.0.0
go: finding github.com/jrick/wsrpc/v2 v2.1.5
go: finding github.com/dchest/siphash v1.2.1
go: finding github.com/jessevdk/go-flags v1.4.0
go: finding golang.org/x/sync v0.0.0-20190423024810-112230192c58
go: finding github.com/jrick/logrotate v1.0.0
go: finding github.com/golang/protobuf v1.3.2
go: finding google.golang.org/grpc v1.22.0
go: finding github.com/gorilla/websocket v1.4.1
go: finding github.com/jrick/bitset v1.0.0
go: finding google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8
go: finding golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7
go: finding golang.org/x/text v0.3.2
Removing intermediate container d2e681a9b021
---> 6e51e003b75b
Step 5/11 : FROM alpine as final
---> 965ea09ff2eb
Step 6/11 : VOLUME /root/.dcrwallet
---> Using cache
---> f12d959d3206
Step 7/11 : COPY --from=builder /go/src/github.com/decred/dcrwallet/start-dcrwallet.sh /bin/
---> Using cache
---> d34e7c3d71ef
Step 8/11 : COPY --from=builder /go/bin/dcrwallet /bin/
---> Using cache
---> 8e56e25816c3
Step 9/11 : EXPOSE 9735 10009
---> Using cache
---> e8f8f5f5e7c1
Step 10/11 : RUN cd bin
---> Using cache
---> c0d6498d0c5a
Step 11/11 : ENTRYPOINT [./dcrwallet]
---> Using cache
---> 48e812d23c1e
Successfully built 48e812d23c1e
Successfully tagged dcrwallet:latest
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ require (
github.com/decred/dcrd/rpc/jsonrpc/types v1.0.1
github.com/decred/dcrd/txscript/v3 v3.0.0-20200311044114-143c1884e4c8
github.com/decred/dcrd/wire v1.3.0
github.com/decred/dcrwallet/errors/v2 v2.0.0
github.com/decred/dcrwallet/p2p/v2 v2.0.0
github.com/decred/dcrwallet/rpc/client/dcrd v1.0.0
github.com/decred/dcrwallet/rpc/jsonrpc/types v1.4.0
github.com/decred/dcrwallet/version v1.0.5
github.com/decred/dcrwallet/wallet/v3 v3.2.1
github.com/decred/dcrwallet/walletseed v1.0.3
github.com/decred/go-socks v1.1.0
github.com/decred/slog v1.0.0
github.com/golang/protobuf v1.3.2
Expand All @@ -32,9 +39,7 @@ require (
github.com/jrick/wsrpc/v2 v2.3.2
go.etcd.io/bbolt v1.3.4
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/text v0.3.2 // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
google.golang.org/grpc v1.25.1
)
91 changes: 91 additions & 0 deletions internal/rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"decred.org/dcrwallet/wallet/txauthor"
"decred.org/dcrwallet/wallet/txrules"
"decred.org/dcrwallet/wallet/udb"
"decred.org/dcrwallet/walletseed"
"github.com/decred/dcrd/blockchain/stake/v3"
blockchain "github.com/decred/dcrd/blockchain/standalone"
"github.com/decred/dcrd/chaincfg/chainhash"
Expand Down Expand Up @@ -132,6 +133,7 @@ var handlers = map[string]handler{
"validateaddress": {fn: (*Server).validateAddress},
"validatepredcp0005cf": {fn: (*Server).validatePreDCP0005CF},
"verifymessage": {fn: (*Server).verifyMessage},
"verifyseed": {fn: (*Server).verifySeed},
"version": {fn: (*Server).version},
"walletinfo": {fn: (*Server).walletInfo},
"walletlock": {fn: (*Server).walletLock},
Expand Down Expand Up @@ -3763,6 +3765,95 @@ WrongAddrKind:
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "address must be secp256k1 P2PK or P2PKH")
}

func deriveCoinTypeKey(seed []byte, coinType uint32, params *chaincfg.Params) (*hdkeychain.ExtendedKey, error) {
// Create new root from the inputted seed and the current net
root, err := hdkeychain.NewMaster(seed[:], params)
if err != nil {
return nil, err
}

// BIP0032 hierarchy: m/<purpose>'/
// Where purpose = 44 and the ' indicates hardening with the HardenedKeyStart 0x80000000
purpose, err := root.Child(44 + hdkeychain.HardenedKeyStart)
if err != nil {
return nil, err
}
defer purpose.Zero()

// BIP0044 hierarchy: m/<purpose>'/<coin type>'
// Where coin type is either the legacy coin type, 20, or the coin type described in SLIP0044, 44. Note these parameters
// are only appropraite for main net.
coinTypePrivKey, err := purpose.Child(coinType + hdkeychain.HardenedKeyStart)
if err != nil {
return nil, err
}

return coinTypePrivKey, nil
}

// verifySeed checks if a user inputted seed is equivalent to the running wallets.
func (s *Server) verifySeed(ctx context.Context, icmd interface{}) (interface{}, error) {
cmd := icmd.(*types.VerifySeedCmd)
w, ok := s.walletLoader.LoadedWallet()
if !ok {
return nil, errUnloadedWallet
}

// obtain the wallet public key to check against the wallet derived seed
account := 0
if cmd.Account != nil {
account = int(*cmd.Account)
}

// snag the coin type from the running wallet
coinType, err := w.CoinType(ctx)
if err != nil {
return nil, err
}

decodedSeed, err := walletseed.DecodeUserInput(cmd.Seed)
if err != nil {
return nil, err
}

// derive the coin type using the user inputted seed, the wallets coin type, and the current chains parameters.
coinTypePrivKey, err := deriveCoinTypeKey(decodedSeed, coinType, w.ChainParams())
if err != nil {
return nil, err
}
defer coinTypePrivKey.Zero()

// both derivedAccountKey and walletDerivedAccountKey use the BIP044 hierachy: m/44'/<coin type>'/<account>'
accountKey, err := coinTypePrivKey.Child(uint32(account) + hdkeychain.HardenedKeyStart)
if err != nil {
return nil, err
}
defer accountKey.Zero()

// to be matched with walletxPubKey
seedPubKey := accountKey.Neuter()

// get the wallet coin type key from the wallet and take the account # if non 0 to get the wallet's public key

// TODO: get the cointype key instead
walletCoinTypePrivKey, err := w.CoinTypePrivKey(ctx)
if err != nil {
return nil, err
}

walletPubKey, err := walletCoinTypePrivKey.Child(uint32(account) + hdkeychain.HardenedKeyStart)
if err != nil {
return nil, err
}
defer accountKey.Zero()

// compare the wallets public key to the inputted seeds public key, and return the results
return &types.VerifySeedResult{
Result: walletPubKey.String() == seedPubKey.String(),
CoinType: coinType,
}, nil
}

// version handles the version command by returning the RPC API versions of the
// wallet and, optionally, the consensus RPC server as well if it is associated
// with the server. The chainClient is optional, and this is simply a helper
Expand Down
Loading

0 comments on commit d62e8e5

Please sign in to comment.