From a347b76ab3aa7eaf52eae3fe0ccf074eedc8fa9e Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 25 Mar 2019 14:10:37 -0500 Subject: [PATCH] hdkeychain: Remove Address method. This removes the Address method from the ExtendedKey struct since ultimately there will be multiple address types and therefore it no longer makes sense to return an address which probably isn't the one the caller actually needs. Instead, the caller can make use of the ECPubKey method to obtain the public key and create the desired address from there. It also removes the related tests, updates the example to show the address conversion process for a standard pay-to-pubkey-hash address, and updates the README.md and doc.go files accordingly. --- hdkeychain/README.md | 7 +++---- hdkeychain/doc.go | 6 +++--- hdkeychain/example_test.go | 24 ++++++++++++++++++++++-- hdkeychain/extendedkey.go | 10 +--------- hdkeychain/extendedkey_test.go | 30 ------------------------------ 5 files changed, 29 insertions(+), 48 deletions(-) diff --git a/hdkeychain/README.md b/hdkeychain/README.md index e90d4e27..542b43ad 100644 --- a/hdkeychain/README.md +++ b/hdkeychain/README.md @@ -23,10 +23,9 @@ report. - Easy serialization and deserialization for both private and public extended keys - Support for custom networks by registering them with chaincfg -- Obtaining the underlying EC pubkeys, EC privkeys, and associated decred - addresses ties in seamlessly with existing secp256k1 and dcrutil types which - provide powerful tools for working with them to do things like sign - transactions and generate payment scripts +- Obtaining the underlying EC pubkeys and EC privkeys ties in seamlessly with + existing secp256k1 types which provide powerful tools for working with them to + do things like sign transactions and generate payment scripts - Uses the highly-optimized secp256k1 package - Code examples including: - Generating a cryptographically secure random seed and deriving a master node diff --git a/hdkeychain/doc.go b/hdkeychain/doc.go index d143f05e..113f5a1b 100644 --- a/hdkeychain/doc.go +++ b/hdkeychain/doc.go @@ -1,5 +1,5 @@ // Copyright (c) 2014 The btcsuite developers -// Copyright (c) 2015-2016 The Decred developers +// Copyright (c) 2015-2019 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -26,8 +26,8 @@ Transaction Signing Keys and Payment Addresses In order to create and sign transactions, or provide others with addresses to send funds to, the underlying key and address material must be accessible. This -package provides the ECPubKey, ECPrivKey, and Address functions for this -purpose. +package provides the ECPubKey and ECPrivKey functions for this purpose. The +caller may then create the desired address types. The Master Node diff --git a/hdkeychain/example_test.go b/hdkeychain/example_test.go index 4d3bb14e..8aa4571f 100644 --- a/hdkeychain/example_test.go +++ b/hdkeychain/example_test.go @@ -9,6 +9,8 @@ import ( "fmt" "github.com/decred/dcrd/chaincfg" + "github.com/decred/dcrd/dcrec" + "github.com/decred/dcrd/dcrutil" "github.com/decred/dcrd/hdkeychain" ) @@ -64,6 +66,7 @@ func Example_defaultWalletLayout() { // Start by getting an extended key instance for the master node. // This gives the path: // m + net := &chaincfg.MainNetParams masterKey, err := hdkeychain.NewKeyFromString(master) if err != nil { fmt.Println(err) @@ -119,12 +122,29 @@ func Example_defaultWalletLayout() { // Get and show the address associated with the extended keys for the // main Decred network. - acct0ExtAddr, err := acct0Ext10.Address(&chaincfg.MainNetParams) + // + // pubKeyHashAddr is a convenience function to convert an extended + // pubkey to a standard pay-to-pubkey-hash address. + pubKeyHashAddr := func(extKey *hdkeychain.ExtendedKey) (string, error) { + ecPubKey, err := extKey.ECPubKey() + if err != nil { + return "", err + } + pkHash := dcrutil.Hash160(ecPubKey.SerializeCompressed()) + addr, err := dcrutil.NewAddressPubKeyHash(pkHash, net, + dcrec.STEcdsaSecp256k1) + if err != nil { + fmt.Println(err) + return "", err + } + return addr.String(), nil + } + acct0ExtAddr, err := pubKeyHashAddr(acct0Ext10) if err != nil { fmt.Println(err) return } - acct0IntAddr, err := acct0Int0.Address(&chaincfg.MainNetParams) + acct0IntAddr, err := pubKeyHashAddr(acct0Int0) if err != nil { fmt.Println(err) return diff --git a/hdkeychain/extendedkey.go b/hdkeychain/extendedkey.go index c03e77e4..f2e0e1c1 100644 --- a/hdkeychain/extendedkey.go +++ b/hdkeychain/extendedkey.go @@ -1,5 +1,5 @@ // Copyright (c) 2014-2016 The btcsuite developers -// Copyright (c) 2015-2016 The Decred developers +// Copyright (c) 2015-2019 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -22,7 +22,6 @@ import ( "github.com/decred/base58" "github.com/decred/dcrd/chaincfg" "github.com/decred/dcrd/chaincfg/chainhash" - "github.com/decred/dcrd/dcrec" "github.com/decred/dcrd/dcrec/secp256k1" "github.com/decred/dcrd/dcrutil" ) @@ -355,13 +354,6 @@ func (k *ExtendedKey) ECPrivKey() (*secp256k1.PrivateKey, error) { return privKey, nil } -// Address converts the extended key to a standard Decred pay-to-pubkey-hash -// address for the passed network. -func (k *ExtendedKey) Address(net *chaincfg.Params) (*dcrutil.AddressPubKeyHash, error) { - pkHash := dcrutil.Hash160(k.pubKeyBytes()) - return dcrutil.NewAddressPubKeyHash(pkHash, net, dcrec.STEcdsaSecp256k1) -} - // paddedAppend appends the src byte slice to dst, returning the new slice. // If the length of the source is smaller than the passed size, leading zero // bytes are appended to the dst slice before appending src. diff --git a/hdkeychain/extendedkey_test.go b/hdkeychain/extendedkey_test.go index ca5c2a37..e2a13503 100644 --- a/hdkeychain/extendedkey_test.go +++ b/hdkeychain/extendedkey_test.go @@ -541,7 +541,6 @@ func TestExtendedKeyAPI(t *testing.T) { privKey string privKeyErr error pubKey string - address string }{ { name: "test vector 1 master node private", @@ -550,7 +549,6 @@ func TestExtendedKeyAPI(t *testing.T) { parentFP: 0, privKey: "33a63922ea4e6686c9fc31daf136888297537f66c1aabe3363df06af0b8274c7", pubKey: "039f2e1d7b50b8451911c64cf745f9ba16193b319212a64096e5679555449d8f37", - address: "Dsk8SfRLF2hssYuLcb6Gu4zh19rg2QBEDGs", }, { name: "test vector 2 chain m/0/2147483647/1/2147483646/2", @@ -559,7 +557,6 @@ func TestExtendedKeyAPI(t *testing.T) { parentFP: 4220580796, privKeyErr: ErrNotPrivExtKey, pubKey: "03dceb0b07698ec3d6ac08ae7297e7f5e63d7fda99d3fce1ded31d36badcdd4d36", - address: "DsZcjfdSKUrEQxoyjkWEo7dM4YZKhma8wCa", }, } @@ -623,19 +620,6 @@ func TestExtendedKeyAPI(t *testing.T) { pubKeyStr) continue } - - addr, err := key.Address(&chaincfg.MainNetParams) - if err != nil { - t.Errorf("Address #%d (%s): unexpected error: %v", i, - test.name, err) - continue - } - if addr.EncodeAddress() != test.address { - t.Errorf("Address #%d (%s): mismatched address -- want "+ - "%s, got %s", i, test.name, test.address, - addr.EncodeAddress()) - continue - } } } @@ -938,20 +922,6 @@ func TestZero(t *testing.T) { return false } - wantAddr := "DsWuefL3Rgj6NXoMFqqBzxY2nmh87RZyPkv" - addr, err := key.Address(&chaincfg.MainNetParams) - if err != nil { - t.Errorf("Address #%d (%s): unexpected error: %v", i, - testName, err) - return false - } - if addr.EncodeAddress() != wantAddr { - t.Errorf("Address #%d (%s): mismatched address -- want "+ - "%s, got %s", i, testName, wantAddr, - addr.EncodeAddress()) - return false - } - return true }