Skip to content

Commit

Permalink
Added AddressFromPubKeyString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 2, 2020
1 parent e7528b8 commit 079adec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ func AddressFromPubKey(publicKey *bsvec.PublicKey) (*bsvutil.LegacyAddressPubKey
return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(publicKey.SerializeCompressed()), &chaincfg.MainNetParams)
}

// AddressFromPubKeyString is a convenience function to use a hex string pubKey
func AddressFromPubKeyString(pubKey string) (*bsvutil.LegacyAddressPubKeyHash, error) {
rawPubKey, err := PubKeyFromString(pubKey)
if err != nil {
return nil, err
}
return AddressFromPubKey(rawPubKey)
}

// AddressFromScript will take an output script and extract a standard bitcoin address
func AddressFromScript(script string) (string, error) {

Expand Down
51 changes: 51 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,54 @@ func BenchmarkAddressFromScript(b *testing.B) {
_, _ = AddressFromScript("76a914b424110292f4ea2ac92beb9e83cf5e6f0fa2996388ac")
}
}

// TestAddressFromPubKeyString will test the method AddressFromPubKeyString()
func TestAddressFromPubKeyString(t *testing.T) {
t.Parallel()

// Create the list of tests
var tests = []struct {
input string
expectedAddress string
expectedNil bool
expectedError bool
}{
{"", "", true, true},
{"0", "", true, true},
{"03ce8a73eb5e4d45966d719ac3ceb431cd0ee203e6395357a167b9abebc4baeacf", "17HeHWVDqDqexLJ31aG4qtVMoX8pKMGSuJ", false, false},
{"0000", "", true, true},
}

// Run tests
for _, test := range tests {
if rawKey, err := AddressFromPubKeyString(test.input); 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)
} else if rawKey == nil && !test.expectedNil {
t.Errorf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if rawKey != nil && test.expectedNil {
t.Errorf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if rawKey != nil && rawKey.EncodeAddress() != test.expectedAddress {
t.Errorf("%s Failed: [%v] inputted [%s] expected but failed comparison of addresses, got: %s", t.Name(), test.input, test.expectedAddress, rawKey.EncodeAddress())
}
}
}

// ExampleAddressFromPubKeyString example using AddressFromPubKeyString()
func ExampleAddressFromPubKeyString() {
rawAddress, err := AddressFromPubKeyString("03ce8a73eb5e4d45966d719ac3ceb431cd0ee203e6395357a167b9abebc4baeacf")
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("address found: %s", rawAddress.EncodeAddress())
// Output:address found: 17HeHWVDqDqexLJ31aG4qtVMoX8pKMGSuJ
}

// BenchmarkAddressFromPubKeyString benchmarks the method AddressFromPubKeyString()
func BenchmarkAddressFromPubKeyString(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = AddressFromPubKeyString("03ce8a73eb5e4d45966d719ac3ceb431cd0ee203e6395357a167b9abebc4baeacf")
}
}

0 comments on commit 079adec

Please sign in to comment.