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.
This commit is contained in:
Dave Collins 2019-03-25 14:10:37 -05:00
parent 3e33f259aa
commit a347b76ab3
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2
5 changed files with 29 additions and 48 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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
}