Skip to content

Commit 23fd7c4

Browse files
committed
add compressed bool
1 parent 2f4c489 commit 23fd7c4

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

address.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,27 @@ func GetAddressFromPrivateKey(privateKey string) (string, error) {
9696
return "", err
9797
}
9898
var address *bsvutil.LegacyAddressPubKeyHash
99-
if address, err = GetAddressFromPubKey(rawKey.PubKey()); err != nil {
99+
if address, err = GetAddressFromPubKey(rawKey.PubKey(), true); err != nil {
100100
return "", err
101101
}
102102
return address.EncodeAddress(), nil
103103
}
104104

105105
// GetAddressFromPubKey gets a bsvutil.LegacyAddressPubKeyHash from a bsvec.PublicKey
106-
func GetAddressFromPubKey(publicKey *bsvec.PublicKey) (*bsvutil.LegacyAddressPubKeyHash, error) {
106+
func GetAddressFromPubKey(publicKey *bsvec.PublicKey, compressed bool) (*bsvutil.LegacyAddressPubKeyHash, error) {
107107
if publicKey == nil {
108108
return nil, fmt.Errorf("publicKey cannot be nil")
109109
} else if publicKey.X == nil {
110110
return nil, fmt.Errorf("publicKey.X cannot be nil")
111111
}
112-
return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(publicKey.SerializeCompressed()), &chaincfg.MainNetParams)
112+
var serializedPublicKey []byte
113+
if compressed {
114+
serializedPublicKey = publicKey.SerializeCompressed()
115+
} else {
116+
serializedPublicKey = publicKey.SerializeUncompressed()
117+
}
118+
119+
return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(serializedPublicKey), &chaincfg.MainNetParams)
113120
}
114121

115122
// GetAddressFromPubKeyString is a convenience function to use a hex string pubKey
@@ -118,7 +125,7 @@ func GetAddressFromPubKeyString(pubKey string) (*bsvutil.LegacyAddressPubKeyHash
118125
if err != nil {
119126
return nil, err
120127
}
121-
return GetAddressFromPubKey(rawPubKey)
128+
return GetAddressFromPubKey(rawPubKey, true)
122129
}
123130

124131
// GetAddressFromScript will take an output script and extract a standard bitcoin address

address_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestGetAddressFromPubKey(t *testing.T) {
145145

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

162162
// ExampleGetAddressFromPubKey example using GetAddressFromPubKey()
163163
func ExampleGetAddressFromPubKey() {
164-
rawAddress, err := GetAddressFromPubKey(testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd"))
164+
rawAddress, err := GetAddressFromPubKey(testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd"), true)
165165
if err != nil {
166166
fmt.Printf("error occurred: %s", err.Error())
167167
return
@@ -174,7 +174,7 @@ func ExampleGetAddressFromPubKey() {
174174
func BenchmarkGetAddressFromPubKey(b *testing.B) {
175175
pubKey := testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd")
176176
for i := 0; i < b.N; i++ {
177-
_, _ = GetAddressFromPubKey(pubKey)
177+
_, _ = GetAddressFromPubKey(pubKey, true)
178178
}
179179
}
180180

hd_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func GetAddressFromHDKey(hdKey *hdkeychain.ExtendedKey) (*bsvutil.LegacyAddressP
139139
if err != nil {
140140
return nil, err
141141
}
142-
return GetAddressFromPubKey(pubKey)
142+
return GetAddressFromPubKey(pubKey, true)
143143
}
144144

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

0 commit comments

Comments
 (0)