Skip to content

Commit

Permalink
hdkeychain: Use direct hashes and remove dcrutil dep
Browse files Browse the repository at this point in the history
This change replaces usage of chainhash (which is intended solely for
transaction and blocks) and dcrutil.Hash160 (used in scripts for
hashing redeem scripts and pubkeys) with direct calls to each hashing
function from dcrd's crypto subpackages.  This improves clarity and
shortens the transitive package imports.  Notably, dcrutil and
everything it depends on is no longer transitively imported when
importing the hdkeychain package.
  • Loading branch information
jrick committed Feb 17, 2020
1 parent b2cef20 commit 80b6ff8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
25 changes: 20 additions & 5 deletions hdkeychain/extendedkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"math/big"

"github.com/decred/base58"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/crypto/blake256"
"github.com/decred/dcrd/crypto/ripemd160"
"github.com/decred/dcrd/dcrec/secp256k1/v3"
"github.com/decred/dcrd/dcrutil/v3"
)

const (
Expand Down Expand Up @@ -197,6 +197,21 @@ func (k *ExtendedKey) ParentFingerprint() uint32 {
return binary.BigEndian.Uint32(k.parentFP)
}

// hash160 returns RIPEMD160(BLAKE256(v)).
func hash160(v []byte) []byte {
blake256Hash := blake256.Sum256(v)
h := ripemd160.New()
h.Write(blake256Hash[:])
return h.Sum(nil)
}

// doubleBlake256Cksum returns the first four bytes of BLAKE256(BLAKE256(v)).
func doubleBlake256Cksum(v []byte) []byte {
first := blake256.Sum256(v)
second := blake256.Sum256(first[:])
return second[:4]
}

// Child returns a derived child extended key at the given index. When this
// extended key is a private extended key (as determined by the IsPrivate
// function), a private extended key will be derived. Otherwise, the derived
Expand Down Expand Up @@ -332,7 +347,7 @@ func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) {

// The fingerprint of the parent for the derived child is the first 4
// bytes of the RIPEMD160(BLAKE256(parentPubKey)).
parentFP := dcrutil.Hash160(k.pubKeyBytes())[:4]
parentFP := hash160(k.pubKeyBytes())[:4]
return newExtendedKey(k.privVer, k.pubVer, childKey, childChainCode,
parentFP, k.depth+1, i, isPrivate), nil
}
Expand Down Expand Up @@ -419,7 +434,7 @@ func (k *ExtendedKey) String() string {
serializedBytes = append(serializedBytes, k.pubKeyBytes()...)
}

checkSum := chainhash.HashB(chainhash.HashB(serializedBytes))[:4]
checkSum := doubleBlake256Cksum(serializedBytes)
serializedBytes = append(serializedBytes, checkSum...)
return base58.Encode(serializedBytes)
}
Expand Down Expand Up @@ -503,7 +518,7 @@ func NewKeyFromString(key string, net NetworkParams) (*ExtendedKey, error) {
// Split the payload and checksum up and ensure the checksum matches.
payload := decoded[:len(decoded)-4]
checkSum := decoded[len(decoded)-4:]
expectedCheckSum := chainhash.HashB(chainhash.HashB(payload))[:4]
expectedCheckSum := doubleBlake256Cksum(payload)
if !bytes.Equal(checkSum, expectedCheckSum) {
return nil, ErrBadChecksum
}
Expand Down
3 changes: 2 additions & 1 deletion hdkeychain/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.11

require (
github.com/decred/base58 v1.0.2
github.com/decred/dcrd/chaincfg/chainhash v1.0.2
github.com/decred/dcrd/chaincfg/v3 v3.0.0-20200215031403-6b2ce76f0986
github.com/decred/dcrd/crypto/blake256 v1.0.0
github.com/decred/dcrd/crypto/ripemd160 v1.0.0
github.com/decred/dcrd/dcrec v1.0.0
github.com/decred/dcrd/dcrec/secp256k1/v3 v3.0.0-20200215031403-6b2ce76f0986
github.com/decred/dcrd/dcrutil/v3 v3.0.0-20200215031403-6b2ce76f0986
Expand Down

0 comments on commit 80b6ff8

Please sign in to comment.