Skip to content

Commit

Permalink
Removed bsvutil
Browse files Browse the repository at this point in the history
  • Loading branch information
icellan committed Feb 3, 2022
1 parent 10c1b94 commit c5c2ad9
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 58 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g
<br/>

- [bitcoinsv/bsvd](https://github.com/bitcoinsv/bsvd)
- [bitcoinsv/bsvutil](https://github.com/bitcoinsv/bsvutil)
- [libsv/go-bk](https://github.com/libsv/go-bk)
- [libsv/go-bt](https://github.com/libsv/go-bt)
</details>
Expand Down
54 changes: 26 additions & 28 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"errors"
"fmt"

"github.com/bitcoinsv/bsvd/chaincfg"
"github.com/bitcoinsv/bsvd/txscript"
"github.com/bitcoinsv/bsvutil"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/crypto"
"github.com/libsv/go-bt/v2/bscript"
)

// A25 is a type for a 25 byte (not base58 encoded) bitcoin address.
Expand Down Expand Up @@ -95,7 +94,7 @@ func GetAddressFromPrivateKey(privateKey *bec.PrivateKey, compressed bool) (stri
if err != nil {
return "", err
}
return address.EncodeAddress(), nil
return address.AddressString, nil
}

// GetAddressFromPrivateKeyString takes a private key string and returns a Bitcoin address
Expand All @@ -104,32 +103,39 @@ func GetAddressFromPrivateKeyString(privateKey string, compressed bool) (string,
if err != nil {
return "", err
}
var address *bsvutil.LegacyAddressPubKeyHash
var address *bscript.Address
if address, err = GetAddressFromPubKey(rawKey.PubKey(), compressed); err != nil {
return "", err
}
return address.EncodeAddress(), nil
return address.AddressString, nil
}

// GetAddressFromPubKey gets a bsvutil.LegacyAddressPubKeyHash from a bec.PublicKey
func GetAddressFromPubKey(publicKey *bec.PublicKey, compressed bool) (*bsvutil.LegacyAddressPubKeyHash, error) {
// GetAddressFromPubKey gets a bscript.Address from a bec.PublicKey
func GetAddressFromPubKey(publicKey *bec.PublicKey, compressed bool) (*bscript.Address, 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")
}
var SerialisedPublicKey []byte
if compressed {
SerialisedPublicKey = publicKey.SerialiseCompressed()
} else {
SerialisedPublicKey = publicKey.SerialiseUncompressed()

if !compressed {
// go-bt/v2/bscript does not have a function that exports the uncompressed address
// https://github.com/libsv/go-bt/blob/master/bscript/address.go#L98
hash := crypto.Hash160(publicKey.SerialiseUncompressed())
bb := make([]byte, 1)
// nolint: makezero // we need to set up the array with 1
bb = append(bb, hash...)
return &bscript.Address{
AddressString: bscript.Base58EncodeMissingChecksum(bb),
PublicKeyHash: hex.EncodeToString(hash),
}, nil
}

return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(SerialisedPublicKey), &chaincfg.MainNetParams)
return bscript.NewAddressFromPublicKey(publicKey, true)
}

// GetAddressFromPubKeyString is a convenience function to use a hex string pubKey
func GetAddressFromPubKeyString(pubKey string, compressed bool) (*bsvutil.LegacyAddressPubKeyHash, error) {
func GetAddressFromPubKeyString(pubKey string, compressed bool) (*bscript.Address, error) {
rawPubKey, err := PubKeyFromString(pubKey)
if err != nil {
return nil, err
Expand All @@ -151,9 +157,10 @@ func GetAddressFromScript(script string) (string, error) {
return "", err
}

// Extract the components from the script
var addresses []bsvutil.Address
_, addresses, _, err = txscript.ExtractPkScriptAddrs(scriptBytes, &chaincfg.MainNetParams)
// Extract the addresses from the script
bScript := bscript.NewFromBytes(scriptBytes)
var addresses []string
addresses, err = bScript.Addresses()
if err != nil {
return "", err
}
Expand All @@ -165,15 +172,6 @@ func GetAddressFromScript(script string) (string, error) {
return "", fmt.Errorf("invalid output script, missing an address")
}

// Extract the address from the pubkey hash
var address *bsvutil.LegacyAddressPubKeyHash
if address, err = bsvutil.NewLegacyAddressPubKeyHash(
addresses[0].ScriptAddress(),
&chaincfg.MainNetParams,
); err != nil {
return "", err
}

// Use the encoded version of the address
return address.EncodeAddress(), nil
return addresses[0], nil
}
12 changes: 6 additions & 6 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func TestGetAddressFromPubKey(t *testing.T) {
t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if rawKey != nil && test.expectedNil {
t.Fatalf("%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.Fatalf("%s Failed: [%v] inputted [%s] expected but failed comparison of addresses, got: %s", t.Name(), test.input, test.expectedAddress, rawKey.EncodeAddress())
} else if rawKey != nil && rawKey.AddressString != test.expectedAddress {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but failed comparison of addresses, got: %s", t.Name(), test.input, test.expectedAddress, rawKey.AddressString)
}
}
}
Expand All @@ -183,7 +183,7 @@ func ExampleGetAddressFromPubKey() {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("address found: %s", rawAddress.EncodeAddress())
fmt.Printf("address found: %s", rawAddress.AddressString)
// Output:address found: 1DfGxKmgL3ETwUdNnXLBueEvNpjcDGcKgK
}

Expand Down Expand Up @@ -271,8 +271,8 @@ func TestGetAddressFromPubKeyString(t *testing.T) {
t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if rawKey != nil && test.expectedNil {
t.Fatalf("%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.Fatalf("%s Failed: [%v] inputted [%s] expected but failed comparison of addresses, got: %s", t.Name(), test.input, test.expectedAddress, rawKey.EncodeAddress())
} else if rawKey != nil && rawKey.AddressString != test.expectedAddress {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but failed comparison of addresses, got: %s", t.Name(), test.input, test.expectedAddress, rawKey.AddressString)
}
}
}
Expand All @@ -284,7 +284,7 @@ func ExampleGetAddressFromPubKeyString() {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("address found: %s", rawAddress.EncodeAddress())
fmt.Printf("address found: %s", rawAddress.AddressString)
// Output:address found: 17HeHWVDqDqexLJ31aG4qtVMoX8pKMGSuJ
}

Expand Down
7 changes: 4 additions & 3 deletions examples/get_address_from_hd_key/get_address_from_hd_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package main
import (
"log"

"github.com/libsv/go-bt/v2/bscript"

"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/bitcoinsv/bsvutil"
)

func main() {
Expand All @@ -16,11 +17,11 @@ func main() {
}

// Get an address
var rawAddress *bsvutil.LegacyAddressPubKeyHash
var rawAddress *bscript.Address
if rawAddress, err = bitcoin.GetAddressFromHDKey(hdKey); err != nil {
log.Fatalf("error occurred: %s", err.Error())
}

// Success!
log.Printf("got address: %s", rawAddress.String())
log.Printf("got address: %s", rawAddress.AddressString)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.17

require (
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9
github.com/libsv/go-bk v0.1.6
github.com/libsv/go-bt/v2 v2.1.0-beta.2.0.20211221142324-0d686850c5e0
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGX
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA=
github.com/bitcoinsv/bsvlog v0.0.0-20181216181007-cb81b076bf2e h1:6f+gRvaPE/4h0g39dqTNPr9/P4mikw0aB+dhiExaWN8=
github.com/bitcoinsv/bsvlog v0.0.0-20181216181007-cb81b076bf2e/go.mod h1:WPrWor6cSeuGQZ15qPe+jqFmblJEFrJHYfr5cD7cmyk=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9 h1:hFI8rT84FCA0FFy3cFrkW5Nz4FyNKlIdCvEvvTNySKg=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9/go.mod h1:p44KuNKUH5BC8uX4ONEODaHUR4+ibC8todEAOGQEJAM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
10 changes: 5 additions & 5 deletions hd_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package bitcoin
import (
"encoding/hex"

"github.com/bitcoinsv/bsvutil"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bk/chaincfg"
"github.com/libsv/go-bt/v2/bscript"
)

const (
Expand Down Expand Up @@ -134,7 +134,7 @@ func GetPublicKeyFromHDKey(hdKey *bip32.ExtendedKey) (*bec.PublicKey, error) {
// GetAddressFromHDKey is a helper function to get the Address associated with a given hdKey
//
// Expects hdKey to not be nil (otherwise will panic)
func GetAddressFromHDKey(hdKey *bip32.ExtendedKey) (*bsvutil.LegacyAddressPubKeyHash, error) {
func GetAddressFromHDKey(hdKey *bip32.ExtendedKey) (*bscript.Address, error) {
pubKey, err := GetPublicKeyFromHDKey(hdKey)
if err != nil {
return nil, err
Expand All @@ -150,7 +150,7 @@ func GetAddressStringFromHDKey(hdKey *bip32.ExtendedKey) (string, error) {
if err != nil {
return "", err
}
return address.String(), nil
return address.AddressString, nil
}

// GetPublicKeysForPath gets the PublicKeys for a given derivation path
Expand Down Expand Up @@ -200,13 +200,13 @@ func GetAddressesForPath(hdKey *bip32.ExtendedKey, num uint32) (addresses []stri
}

// Loop, get address and append to results
var address *bsvutil.LegacyAddressPubKeyHash
var address *bscript.Address
for _, key := range pubKeys {
if address, err = GetAddressFromPubKey(key, true); err != nil {
// Should never error if the pubKeys are valid keys
return
}
addresses = append(addresses, address.String())
addresses = append(addresses, address.AddressString)
}

return
Expand Down
12 changes: 6 additions & 6 deletions hd_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"testing"

"github.com/bitcoinsv/bsvutil"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bt/v2/bscript"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -703,7 +703,7 @@ func TestGetAddressFromHDKey(t *testing.T) {
{validHdKey, "13xHrMdZuqa2gpweHf37w8hu6tfv3JrnaW", false, false},
}

var address *bsvutil.LegacyAddressPubKeyHash
var address *bscript.Address
for _, test := range tests {
if address, err = GetAddressFromHDKey(test.input); err != nil && !test.expectedError {
t.Fatalf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
Expand All @@ -713,8 +713,8 @@ func TestGetAddressFromHDKey(t *testing.T) {
t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input)
} else if address != nil && test.expectedNil {
t.Fatalf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input)
} else if address != nil && address.String() != test.expectedAddress {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedAddress, address.String())
} else if address != nil && address.AddressString != test.expectedAddress {
t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedAddress, address.AddressString)
}
}
}
Expand All @@ -738,13 +738,13 @@ func ExampleGetAddressFromHDKey() {
return
}

var address *bsvutil.LegacyAddressPubKeyHash
var address *bscript.Address
if address, err = GetAddressFromHDKey(hdKey); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}

fmt.Printf("address: %s", address.String())
fmt.Printf("address: %s", address.AddressString)
// Output:address: 18G2YRH3nRKRx8pnqVFUM5nAJhTZJ3YA4W
}

Expand Down
2 changes: 1 addition & 1 deletion private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func PrivateAndPublicKeys(privateKey string) (*bec.PrivateKey, *bec.PublicKey, e
return rawKey, publicKey, nil
}

// PrivateKeyToWif will convert a private key to a WIF (*bsvutil.WIF)
// PrivateKeyToWif will convert a private key to a WIF (*wif.WIF)
func PrivateKeyToWif(privateKey string) (*wif.WIF, error) {

// Missing private key
Expand Down
10 changes: 5 additions & 5 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/bitcoinsv/bsvd/chaincfg/chainhash"
"github.com/bitcoinsv/bsvd/wire"
"github.com/bitcoinsv/bsvutil"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bt/v2/bscript"
)

const (
Expand Down Expand Up @@ -57,20 +57,20 @@ func VerifyMessage(address, sig, data string) error {
}

// Get the address
var bsvutilAddress *bsvutil.LegacyAddressPubKeyHash
if bsvutilAddress, err = GetAddressFromPubKey(publicKey, wasCompressed); err != nil {
var bscriptAddress *bscript.Address
if bscriptAddress, err = GetAddressFromPubKey(publicKey, wasCompressed); err != nil {
return err
}

// Return nil if addresses match.
if bsvutilAddress.String() == address {
if bscriptAddress.AddressString == address {
return nil
}
return fmt.Errorf(
"address (%s) not found - compressed: %t\n%s was found instead",
address,
wasCompressed,
bsvutilAddress.EncodeAddress(),
bscriptAddress.AddressString,
)
}

Expand Down

0 comments on commit c5c2ad9

Please sign in to comment.