Skip to content

Commit

Permalink
add compressed bool
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Nov 1, 2020
1 parent 2f4c489 commit 23fd7c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 11 additions & 4 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,27 @@ func GetAddressFromPrivateKey(privateKey string) (string, error) {
return "", err
}
var address *bsvutil.LegacyAddressPubKeyHash
if address, err = GetAddressFromPubKey(rawKey.PubKey()); err != nil {
if address, err = GetAddressFromPubKey(rawKey.PubKey(), true); err != nil {
return "", err
}
return address.EncodeAddress(), nil
}

// GetAddressFromPubKey gets a bsvutil.LegacyAddressPubKeyHash from a bsvec.PublicKey
func GetAddressFromPubKey(publicKey *bsvec.PublicKey) (*bsvutil.LegacyAddressPubKeyHash, error) {
func GetAddressFromPubKey(publicKey *bsvec.PublicKey, compressed bool) (*bsvutil.LegacyAddressPubKeyHash, error) {
if publicKey == nil {
return nil, fmt.Errorf("publicKey cannot be nil")
} else if publicKey.X == nil {
return nil, fmt.Errorf("publicKey.X cannot be nil")
}
return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(publicKey.SerializeCompressed()), &chaincfg.MainNetParams)
var serializedPublicKey []byte
if compressed {
serializedPublicKey = publicKey.SerializeCompressed()
} else {
serializedPublicKey = publicKey.SerializeUncompressed()
}

return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(serializedPublicKey), &chaincfg.MainNetParams)
}

// GetAddressFromPubKeyString is a convenience function to use a hex string pubKey
Expand All @@ -118,7 +125,7 @@ func GetAddressFromPubKeyString(pubKey string) (*bsvutil.LegacyAddressPubKeyHash
if err != nil {
return nil, err
}
return GetAddressFromPubKey(rawPubKey)
return GetAddressFromPubKey(rawPubKey, true)
}

// GetAddressFromScript will take an output script and extract a standard bitcoin address
Expand Down
6 changes: 3 additions & 3 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestGetAddressFromPubKey(t *testing.T) {

// Run tests
for _, test := range tests {
if rawKey, err := GetAddressFromPubKey(test.input); err != nil && !test.expectedError {
if rawKey, err := GetAddressFromPubKey(test.input, true); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%v] inputted and error was expected", t.Name(), test.input)
Expand All @@ -161,7 +161,7 @@ func TestGetAddressFromPubKey(t *testing.T) {

// ExampleGetAddressFromPubKey example using GetAddressFromPubKey()
func ExampleGetAddressFromPubKey() {
rawAddress, err := GetAddressFromPubKey(testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd"))
rawAddress, err := GetAddressFromPubKey(testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd"), true)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
Expand All @@ -174,7 +174,7 @@ func ExampleGetAddressFromPubKey() {
func BenchmarkGetAddressFromPubKey(b *testing.B) {
pubKey := testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
for i := 0; i < b.N; i++ {
_, _ = GetAddressFromPubKey(pubKey)
_, _ = GetAddressFromPubKey(pubKey, true)
}
}

Expand Down
4 changes: 2 additions & 2 deletions hd_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func GetAddressFromHDKey(hdKey *hdkeychain.ExtendedKey) (*bsvutil.LegacyAddressP
if err != nil {
return nil, err
}
return GetAddressFromPubKey(pubKey)
return GetAddressFromPubKey(pubKey, true)
}

// GetAddressStringFromHDKey is a helper function to get the Address (string) associated with a given hdKey
Expand Down Expand Up @@ -202,7 +202,7 @@ func GetAddressesForPath(hdKey *hdkeychain.ExtendedKey, num uint32) (addresses [
// Loop, get address and append to results
var address *bsvutil.LegacyAddressPubKeyHash
for _, key := range pubKeys {
if address, err = GetAddressFromPubKey(key); err != nil {
if address, err = GetAddressFromPubKey(key, true); err != nil {
// Should never error if the pubKeys are valid keys
return
}
Expand Down

0 comments on commit 23fd7c4

Please sign in to comment.