diff --git a/README.md b/README.md index bec2ff0..d912042 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,6 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g
- [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) diff --git a/address.go b/address.go index b3031df..7efa22a 100644 --- a/address.go +++ b/address.go @@ -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. @@ -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 @@ -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 @@ -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 } @@ -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 } diff --git a/address_test.go b/address_test.go index 6264fc4..5cf268b 100644 --- a/address_test.go +++ b/address_test.go @@ -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) } } } @@ -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 } @@ -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) } } } @@ -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 } diff --git a/examples/get_address_from_hd_key/get_address_from_hd_key.go b/examples/get_address_from_hd_key/get_address_from_hd_key.go index 47695ed..4e2f0e6 100644 --- a/examples/get_address_from_hd_key/get_address_from_hd_key.go +++ b/examples/get_address_from_hd_key/get_address_from_hd_key.go @@ -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() { @@ -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) } diff --git a/go.mod b/go.mod index 2d3dcf0..618068e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a8e2e19..9a977fd 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/hd_key.go b/hd_key.go index 10371e3..e961318 100644 --- a/hd_key.go +++ b/hd_key.go @@ -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 ( @@ -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 @@ -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 @@ -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 diff --git a/hd_key_test.go b/hd_key_test.go index b651941..b48cdb7 100644 --- a/hd_key_test.go +++ b/hd_key_test.go @@ -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" ) @@ -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()) @@ -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) } } } @@ -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 } diff --git a/private_key.go b/private_key.go index b68cfdd..1d05cf0 100644 --- a/private_key.go +++ b/private_key.go @@ -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 diff --git a/verify.go b/verify.go index 2e58426..fa83df7 100644 --- a/verify.go +++ b/verify.go @@ -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 ( @@ -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, ) }