diff --git a/README.md b/README.md index b0606a5..d912042 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g ### Features - **Addresses** - - [Address from PrivateKey (bsvec.PrivateKey)](address.go) + - [Address from PrivateKey (bec.PrivateKey)](address.go) - [Address from Script](address.go) - **Encryption** - [Encrypt With Private Key](encryption.go) @@ -97,7 +97,7 @@ 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) @@ -126,33 +126,36 @@ make help List of all current commands: ```text -all Runs multiple commands -clean Remove previous builds and any test cache data -clean-mods Remove all the Go mod cache -coverage Shows the test coverage -generate Runs the go generate command in the base of the repo -godocs Sync the latest tag with GoDocs -help Show this help message -install Install the application -install-go Install the application (Using Native Go) -lint Run the golangci-lint application (install if not found) -release Full production release (creates release in Github) -release Runs common.release then runs godocs -release-snap Test the full release (build binaries) -release-test Full production test release (everything except deploy) -replace-version Replaces the version in HTML/JS (pre-deploy) -tag Generate a new tag and push (tag version=0.0.0) -tag-remove Remove a tag if found (tag-remove version=0.0.0) -tag-update Update an existing tag to current commit (tag-update version=0.0.0) -test Runs vet, lint and ALL tests -test-ci Runs all tests via CI (exports coverage) -test-ci-no-race Runs all tests via CI (no race) (exports coverage) -test-ci-short Runs unit tests via CI (exports coverage) -test-short Runs vet, lint and tests (excludes integration tests) -test-unit Runs tests and outputs coverage -uninstall Uninstall the application (and remove files) -update-linter Update the golangci-lint package (macOS only) -vet Run the Go vet application +all Runs multiple commands +clean Remove previous builds and any test cache data +clean-mods Remove all the Go mod cache +coverage Shows the test coverage +diff Show the git diff +generate Runs the go generate command in the base of the repo +godocs Sync the latest tag with GoDocs +help Show this help message +install Install the application +install-go Install the application (Using Native Go) +install-releaser Install the GoReleaser application +lint Run the golangci-lint application (install if not found) +release Full production release (creates release in Github) +release Runs common.release then runs godocs +release-snap Test the full release (build binaries) +release-test Full production test release (everything except deploy) +replace-version Replaces the version in HTML/JS (pre-deploy) +tag Generate a new tag and push (tag version=0.0.0) +tag-remove Remove a tag if found (tag-remove version=0.0.0) +tag-update Update an existing tag to current commit (tag-update version=0.0.0) +test Runs lint and ALL tests +test-ci Runs all tests via CI (exports coverage) +test-ci-no-race Runs all tests via CI (no race) (exports coverage) +test-ci-short Runs unit tests via CI (exports coverage) +test-no-lint Runs just tests +test-short Runs vet, lint and tests (excludes integration tests) +test-unit Runs tests and outputs coverage +uninstall Uninstall the application (and remove files) +update-linter Update the golangci-lint package (macOS only) +vet Run the Go vet application ``` diff --git a/address.go b/address.go index 94f97f5..7efa22a 100644 --- a/address.go +++ b/address.go @@ -7,10 +7,9 @@ import ( "errors" "fmt" - "github.com/bitcoinsv/bsvd/bsvec" - "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. @@ -89,13 +88,13 @@ func ValidA58(a58 []byte) (bool, error) { return a.EmbeddedChecksum() == a.ComputeChecksum(), nil } -// GetAddressFromPrivateKey takes a bsvec private key and returns a Bitcoin address -func GetAddressFromPrivateKey(privateKey *bsvec.PrivateKey, compressed bool) (string, error) { +// GetAddressFromPrivateKey takes a bec private key and returns a Bitcoin address +func GetAddressFromPrivateKey(privateKey *bec.PrivateKey, compressed bool) (string, error) { address, err := GetAddressFromPubKey(privateKey.PubKey(), compressed) 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 bsvec.PublicKey -func GetAddressFromPubKey(publicKey *bsvec.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 serializedPublicKey []byte - if compressed { - serializedPublicKey = publicKey.SerializeCompressed() - } else { - serializedPublicKey = publicKey.SerializeUncompressed() + + 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(serializedPublicKey), &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 7ca4d9a..5cf268b 100644 --- a/address_test.go +++ b/address_test.go @@ -4,7 +4,7 @@ import ( "fmt" "testing" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" "github.com/stretchr/testify/assert" ) @@ -97,7 +97,7 @@ func TestGetAddressFromPrivateKey(t *testing.T) { // TestGetAddressFromPrivateKeyCompression will test the method GetAddressFromPrivateKey() func TestGetAddressFromPrivateKeyCompression(t *testing.T) { - privateKey, err := bsvec.NewPrivateKey(bsvec.S256()) + privateKey, err := bec.NewPrivateKey(bec.S256()) assert.NoError(t, err) var addressUncompressed string @@ -109,6 +109,10 @@ func TestGetAddressFromPrivateKeyCompression(t *testing.T) { assert.NoError(t, err) assert.NotEqual(t, addressCompressed, addressUncompressed) + + addressCompressed, err = GetAddressFromPrivateKey(&bec.PrivateKey{}, true) + assert.Error(t, err) + assert.Equal(t, "", addressCompressed) } // ExampleGetAddressFromPrivateKey example using GetAddressFromPrivateKey() @@ -131,7 +135,7 @@ func BenchmarkGetAddressFromPrivateKey(b *testing.B) { } // testGetPublicKeyFromPrivateKey is a helper method for tests -func testGetPublicKeyFromPrivateKey(privateKey string) *bsvec.PublicKey { +func testGetPublicKeyFromPrivateKey(privateKey string) *bec.PublicKey { rawKey, err := PrivateKeyFromString(privateKey) if err != nil { return nil @@ -144,18 +148,18 @@ func TestGetAddressFromPubKey(t *testing.T) { t.Parallel() var tests = []struct { - input *bsvec.PublicKey + input *bec.PublicKey expectedAddress string expectedNil bool expectedError bool }{ - {&bsvec.PublicKey{}, "", true, true}, + {&bec.PublicKey{}, "", true, true}, {testGetPublicKeyFromPrivateKey("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd"), "1DfGxKmgL3ETwUdNnXLBueEvNpjcDGcKgK", false, false}, {testGetPublicKeyFromPrivateKey("000000"), "15wJjXvfQzo3SXqoWGbWZmNYND1Si4siqV", false, false}, {testGetPublicKeyFromPrivateKey("0"), "15wJjXvfQzo3SXqoWGbWZmNYND1Si4siqV", true, true}, } - // todo: add more error cases of invalid *bsvec.PublicKey + // todo: add more error cases of invalid *bec.PublicKey for _, test := range tests { if rawKey, err := GetAddressFromPubKey(test.input, true); err != nil && !test.expectedError { @@ -166,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) } } } @@ -179,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 } @@ -267,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) } } } @@ -280,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/encryption.go b/encryption.go index aae112d..a90f0cd 100644 --- a/encryption.go +++ b/encryption.go @@ -3,14 +3,14 @@ package bitcoin import ( "encoding/hex" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" ) // EncryptWithPrivateKey will encrypt the data using a given private key -func EncryptWithPrivateKey(privateKey *bsvec.PrivateKey, data string) (string, error) { +func EncryptWithPrivateKey(privateKey *bec.PrivateKey, data string) (string, error) { - // Encrypt using bsvec - encryptedData, err := bsvec.Encrypt(privateKey.PubKey(), []byte(data)) + // Encrypt using bec + encryptedData, err := bec.Encrypt(privateKey.PubKey(), []byte(data)) if err != nil { return "", err } @@ -21,7 +21,7 @@ func EncryptWithPrivateKey(privateKey *bsvec.PrivateKey, data string) (string, e // DecryptWithPrivateKey is a wrapper to decrypt the previously encrypted // information, given a corresponding private key -func DecryptWithPrivateKey(privateKey *bsvec.PrivateKey, data string) (string, error) { +func DecryptWithPrivateKey(privateKey *bec.PrivateKey, data string) (string, error) { // Decode the hex encoded string rawData, err := hex.DecodeString(data) @@ -31,7 +31,7 @@ func DecryptWithPrivateKey(privateKey *bsvec.PrivateKey, data string) (string, e // Decrypt the data var decrypted []byte - if decrypted, err = bsvec.Decrypt(privateKey, rawData); err != nil { + if decrypted, err = bec.Decrypt(privateKey, rawData); err != nil { return "", err } return string(decrypted), nil @@ -46,7 +46,7 @@ func EncryptWithPrivateKeyString(privateKey, data string) (string, error) { return "", err } - // Encrypt using bsvec + // Encrypt using bec return EncryptWithPrivateKey(rawPrivateKey, data) } @@ -64,26 +64,26 @@ func DecryptWithPrivateKeyString(privateKey, data string) (string, error) { } // EncryptShared will encrypt data and provide shared keys for decryption -func EncryptShared(user1PrivateKey *bsvec.PrivateKey, user2PubKey *bsvec.PublicKey, data []byte) ( - *bsvec.PrivateKey, *bsvec.PublicKey, []byte, error) { +func EncryptShared(user1PrivateKey *bec.PrivateKey, user2PubKey *bec.PublicKey, data []byte) ( + *bec.PrivateKey, *bec.PublicKey, []byte, error) { // Generate shared keys that can be decrypted by either user sharedPrivKey, sharedPubKey := GenerateSharedKeyPair(user1PrivateKey, user2PubKey) // Encrypt data with shared key - encryptedData, err := bsvec.Encrypt(sharedPubKey, data) + encryptedData, err := bec.Encrypt(sharedPubKey, data) return sharedPrivKey, sharedPubKey, encryptedData, err } // EncryptSharedString will encrypt a string to a hex encoded encrypted payload, and provide shared keys for decryption -func EncryptSharedString(user1PrivateKey *bsvec.PrivateKey, user2PubKey *bsvec.PublicKey, data string) ( - *bsvec.PrivateKey, *bsvec.PublicKey, string, error) { +func EncryptSharedString(user1PrivateKey *bec.PrivateKey, user2PubKey *bec.PublicKey, data string) ( + *bec.PrivateKey, *bec.PublicKey, string, error) { // Generate shared keys that can be decrypted by either user sharedPrivKey, sharedPubKey := GenerateSharedKeyPair(user1PrivateKey, user2PubKey) // Encrypt data with shared key - encryptedData, err := bsvec.Encrypt(sharedPubKey, []byte(data)) + encryptedData, err := bec.Encrypt(sharedPubKey, []byte(data)) return sharedPrivKey, sharedPubKey, hex.EncodeToString(encryptedData), err } diff --git a/encryption_test.go b/encryption_test.go index a2924c8..3061049 100644 --- a/encryption_test.go +++ b/encryption_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" "github.com/stretchr/testify/assert" ) @@ -21,7 +21,7 @@ func TestEncryptWithPrivateKey(t *testing.T) { assert.NotNil(t, privateKey) var tests = []struct { - inputKey *bsvec.PrivateKey + inputKey *bec.PrivateKey inputData string expectedError bool }{ @@ -170,7 +170,7 @@ func TestDecryptWithPrivateKey(t *testing.T) { assert.NotNil(t, privateKey) var tests = []struct { - inputKey *bsvec.PrivateKey + inputKey *bec.PrivateKey inputEncrypted string expectedData string expectedError bool @@ -408,7 +408,7 @@ func TestEncryptShared(t *testing.T) { assert.NotNil(t, privKey1) // User 2's private key - var privKey2 *bsvec.PrivateKey + var privKey2 *bec.PrivateKey privKey2, err = CreatePrivateKey() assert.NoError(t, err) assert.NotNil(t, privKey1) @@ -424,7 +424,7 @@ func TestEncryptShared(t *testing.T) { // User 2 can decrypt using the shared private key var decryptedTestData []byte - decryptedTestData, err = bsvec.Decrypt(user2SharedPrivKey, encryptedData) + decryptedTestData, err = bec.Decrypt(user2SharedPrivKey, encryptedData) assert.NoError(t, err) // Test the result @@ -456,7 +456,7 @@ func TestEncryptSharedString(t *testing.T) { assert.NotNil(t, privKey1) // User 2's private key - var privKey2 *bsvec.PrivateKey + var privKey2 *bec.PrivateKey privKey2, err = CreatePrivateKey() assert.NoError(t, err) assert.NotNil(t, privKey1) @@ -475,7 +475,7 @@ func TestEncryptSharedString(t *testing.T) { decoded, err = hex.DecodeString(encryptedData) assert.NoError(t, err) - decryptedTestData, err = bsvec.Decrypt(user2SharedPrivKey, decoded) + decryptedTestData, err = bec.Decrypt(user2SharedPrivKey, decoded) assert.NoError(t, err) // Test the result diff --git a/examples/calculate_fee_for_tx/calculate_fee_for_tx.go b/examples/calculate_fee_for_tx/calculate_fee_for_tx.go index 790ed6f..944c437 100644 --- a/examples/calculate_fee_for_tx/calculate_fee_for_tx.go +++ b/examples/calculate_fee_for_tx/calculate_fee_for_tx.go @@ -19,5 +19,5 @@ func main() { estimatedFee := bitcoin.CalculateFeeForTx(tx, nil, nil) // Success! - log.Printf("tx id: %s estimated fee: %d satoshis", tx.GetTxID(), estimatedFee) + log.Printf("tx id: %s estimated fee: %d satoshis", tx.TxID(), estimatedFee) } diff --git a/examples/create_tx/create_tx.go b/examples/create_tx/create_tx.go index 038ff04..9b95ecb 100644 --- a/examples/create_tx/create_tx.go +++ b/examples/create_tx/create_tx.go @@ -4,7 +4,7 @@ import ( "log" "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/libsv/go-bt" + "github.com/libsv/go-bt/v2" ) func main() { @@ -48,6 +48,6 @@ func main() { } // Success! - log.Printf("rawTx: %s", rawTx.ToString()) - log.Printf("tx_id: %s", rawTx.GetTxID()) + log.Printf("rawTx: %s", rawTx.String()) + log.Printf("tx_id: %s", rawTx.TxID()) } diff --git a/examples/create_tx_using_wif/create_tx_using_wif.go b/examples/create_tx_using_wif/create_tx_using_wif.go index f020342..ae79ee5 100644 --- a/examples/create_tx_using_wif/create_tx_using_wif.go +++ b/examples/create_tx_using_wif/create_tx_using_wif.go @@ -39,5 +39,5 @@ func main() { } // Success! - log.Printf("rawTx: %s", rawTx.ToString()) + log.Printf("rawTx: %s", rawTx.String()) } diff --git a/examples/create_tx_with_change/create_tx_with_change.go b/examples/create_tx_with_change/create_tx_with_change.go index 5ca802a..20136f2 100644 --- a/examples/create_tx_with_change/create_tx_with_change.go +++ b/examples/create_tx_with_change/create_tx_with_change.go @@ -42,5 +42,5 @@ func main() { } // Success! - log.Printf("rawTx: %s", rawTx.ToString()) + log.Printf("rawTx: %s", rawTx.String()) } diff --git a/examples/encrypt_shared_keys/encrypt_shared_keys.go b/examples/encrypt_shared_keys/encrypt_shared_keys.go index 341cff4..4875681 100644 --- a/examples/encrypt_shared_keys/encrypt_shared_keys.go +++ b/examples/encrypt_shared_keys/encrypt_shared_keys.go @@ -5,7 +5,7 @@ import ( "log" "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" ) func main() { @@ -30,7 +30,7 @@ func main() { // User 2 can decrypt using the shared private key var decryptedTestData []byte - decryptedTestData, err = bsvec.Decrypt(user2SharedPrivKey, encryptedData) + decryptedTestData, err = bec.Decrypt(user2SharedPrivKey, encryptedData) if err != nil { log.Fatalf("failed to decrypt test data %s", err) } diff --git a/examples/encrypt_with_private_key/encrypt_with_private_key.go b/examples/encrypt_with_private_key/encrypt_with_private_key.go index 2b12d5c..5025306 100644 --- a/examples/encrypt_with_private_key/encrypt_with_private_key.go +++ b/examples/encrypt_with_private_key/encrypt_with_private_key.go @@ -14,7 +14,7 @@ func main() { if err != nil { log.Fatalf("error occurred: %s", err.Error()) } - log.Println("private key (used for encryption): ", hex.EncodeToString(privateKey.Serialize())) + log.Println("private key (used for encryption): ", hex.EncodeToString(privateKey.Serialise())) // Encrypt var data string 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/examples/get_private_key_for_path/get_private_key_for_path.go b/examples/get_private_key_for_path/get_private_key_for_path.go index 265f708..1fd34df 100644 --- a/examples/get_private_key_for_path/get_private_key_for_path.go +++ b/examples/get_private_key_for_path/get_private_key_for_path.go @@ -5,7 +5,7 @@ import ( "log" "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" ) func main() { @@ -17,12 +17,12 @@ func main() { } // Get a private key from a specific path (chain/num) - var privateKey *bsvec.PrivateKey + var privateKey *bec.PrivateKey privateKey, err = bitcoin.GetPrivateKeyByPath(hdKey, 10, 2) if err != nil { log.Fatalf("error occurred: %s", err.Error()) } // Success! - log.Printf("private key: %s for chain/path: %d/%d", hex.EncodeToString(privateKey.Serialize()), 10, 2) + log.Printf("private key: %s for chain/path: %d/%d", hex.EncodeToString(privateKey.Serialise()), 10, 2) } diff --git a/examples/get_public_keys_for_path/get_public_keys_for_path.go b/examples/get_public_keys_for_path/get_public_keys_for_path.go index a0c29b6..c213b1b 100644 --- a/examples/get_public_keys_for_path/get_public_keys_for_path.go +++ b/examples/get_public_keys_for_path/get_public_keys_for_path.go @@ -5,7 +5,7 @@ import ( "log" "github.com/bitcoinschema/go-bitcoin/v2" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" ) func main() { @@ -16,13 +16,13 @@ func main() { } // Get keys by path (example showing 5 sets of keys) - var pubKeys []*bsvec.PublicKey + var pubKeys []*bec.PublicKey for i := 1; i <= 5; i++ { if pubKeys, err = bitcoin.GetPublicKeysForPath(hdKey, uint32(i)); err != nil { log.Fatalf("error occurred: %s", err.Error()) } for index, key := range pubKeys { - log.Printf("#%d found at m/%d/%d key: %s", i, index, i, hex.EncodeToString(key.SerializeCompressed())) + log.Printf("#%d found at m/%d/%d key: %s", i, index, i, hex.EncodeToString(key.SerialiseCompressed())) } } } diff --git a/examples/private_key_to_wif/private_key_to_wif.go b/examples/private_key_to_wif/private_key_to_wif.go index d4b5fc3..376ec56 100644 --- a/examples/private_key_to_wif/private_key_to_wif.go +++ b/examples/private_key_to_wif/private_key_to_wif.go @@ -15,11 +15,11 @@ func main() { } // Create a wif - var wif string - if wif, err = bitcoin.PrivateKeyToWifString(privateKey); err != nil { + var privateWif string + if privateWif, err = bitcoin.PrivateKeyToWifString(privateKey); err != nil { log.Fatalf("error occurred: %s", err.Error()) } // Success! - log.Printf("private key: %s converted to wif: %s", privateKey, wif) + log.Printf("private key: %s converted to wif: %s", privateKey, privateWif) } diff --git a/examples/tx_from_hex/tx_from_hex.go b/examples/tx_from_hex/tx_from_hex.go index 1d2d73f..632e50e 100644 --- a/examples/tx_from_hex/tx_from_hex.go +++ b/examples/tx_from_hex/tx_from_hex.go @@ -17,5 +17,5 @@ func main() { return } - log.Printf("tx id: %s", rawTx.GetTxID()) + log.Printf("tx id: %s", rawTx.TxID()) } diff --git a/go.mod b/go.mod index 15d147c..ba19bbe 100644 --- a/go.mod +++ b/go.mod @@ -4,15 +4,15 @@ 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-bt v1.0.4 + 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 ) require ( - github.com/bitcoinsv/bsvlog v0.0.0-20181216181007-cb81b076bf2e // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8 // indirect + golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index 561041f..e102eca 100644 --- a/go.sum +++ b/go.sum @@ -1,33 +1,31 @@ github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= 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= -github.com/libsv/go-bt v1.0.4 h1:2Css5lfomk/J97tM5Gk56Lp+tTK6xWYnmHNc/fGO6lE= -github.com/libsv/go-bt v1.0.4/go.mod h1:AfXoLFYEbY/TvCq/84xTce2xGjPUuC5imokHmcykF2k= +github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c= +github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA= +github.com/libsv/go-bt/v2 v2.1.0-beta.2.0.20211221142324-0d686850c5e0 h1:YGwofFXF3W+eXYb9QTL3yxIlfNrm514xTvEtU1TYTFk= +github.com/libsv/go-bt/v2 v2.1.0-beta.2.0.20211221142324-0d686850c5e0/go.mod h1:KbBf6ugGNMtVwtCSUtlSAHoVhbAie4hu2VM97d1ZL8I= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20201208171446-5f87f3452ae9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8 h1:kACShD3qhmr/3rLmg1yXyt+N4HcwutKyPRB93s54TIU= -golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE= +golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/hd_key.go b/hd_key.go index 0954d79..e961318 100644 --- a/hd_key.go +++ b/hd_key.go @@ -3,10 +3,10 @@ package bitcoin import ( "encoding/hex" - "github.com/bitcoinsv/bsvd/bsvec" - "github.com/bitcoinsv/bsvd/chaincfg" - "github.com/bitcoinsv/bsvutil" - "github.com/bitcoinsv/bsvutil/hdkeychain" + "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 ( @@ -25,8 +25,8 @@ const ( DefaultInternalChain = 1 ) -// GenerateHDKey will create a new master node for use in creating a hierarchical deterministic key chain -func GenerateHDKey(seedLength uint8) (hdKey *hdkeychain.ExtendedKey, err error) { +// GenerateHDKey will create a new master node for use in creating a hierarchical deterministic keychain +func GenerateHDKey(seedLength uint8) (hdKey *bip32.ExtendedKey, err error) { // Missing or invalid seed length if seedLength == 0 { @@ -35,25 +35,25 @@ func GenerateHDKey(seedLength uint8) (hdKey *hdkeychain.ExtendedKey, err error) // Generate a new seed (added extra security from 256 to 512 bits for seed length) var seed []byte - if seed, err = hdkeychain.GenerateSeed(seedLength); err != nil { + if seed, err = bip32.GenerateSeed(seedLength); err != nil { return } // Generate a new master key - return hdkeychain.NewMaster(seed, &chaincfg.MainNetParams) + return bip32.NewMaster(seed, &chaincfg.MainNet) } // GenerateHDKeyFromString will create a new master node for use in creating a -// hierarchical deterministic key chain from an xPrivKey string -func GenerateHDKeyFromString(xPriv string) (hdKey *hdkeychain.ExtendedKey, err error) { - return hdkeychain.NewKeyFromString(xPriv) +// hierarchical deterministic keychain from an xPrivKey string +func GenerateHDKeyFromString(xPriv string) (hdKey *bip32.ExtendedKey, err error) { + return bip32.NewKeyFromString(xPriv) } // GenerateHDKeyPair will generate a new xPub HD master node (xPrivateKey & xPublicKey) func GenerateHDKeyPair(seedLength uint8) (xPrivateKey, xPublicKey string, err error) { // Generate an HD master key - var masterKey *hdkeychain.ExtendedKey + var masterKey *bip32.ExtendedKey if masterKey, err = GenerateHDKey(seedLength); err != nil { return } @@ -69,7 +69,7 @@ func GenerateHDKeyPair(seedLength uint8) (xPrivateKey, xPublicKey string, err er // GetHDKeyByPath gets the corresponding HD key from a chain/num path // Reference: https://en.bitcoin.it/wiki/BIP_0032#The_default_wallet_layout -func GetHDKeyByPath(hdKey *hdkeychain.ExtendedKey, chain, num uint32) (*hdkeychain.ExtendedKey, error) { +func GetHDKeyByPath(hdKey *bip32.ExtendedKey, chain, num uint32) (*bip32.ExtendedKey, error) { // Derive the child key from the chain path childKeyChain, err := GetHDKeyChild(hdKey, chain) @@ -85,14 +85,14 @@ func GetHDKeyByPath(hdKey *hdkeychain.ExtendedKey, chain, num uint32) (*hdkeycha // Note: For a hardened child, start at 0x80000000. (For reference, 0x8000000 = 0') // // Expects hdKey to not be nil (otherwise will panic) -func GetHDKeyChild(hdKey *hdkeychain.ExtendedKey, num uint32) (*hdkeychain.ExtendedKey, error) { +func GetHDKeyChild(hdKey *bip32.ExtendedKey, num uint32) (*bip32.ExtendedKey, error) { return hdKey.Child(num) } // GetPrivateKeyByPath gets the key for a given derivation path (chain/num) // // Expects hdKey to not be nil (otherwise will panic) -func GetPrivateKeyByPath(hdKey *hdkeychain.ExtendedKey, chain, num uint32) (*bsvec.PrivateKey, error) { +func GetPrivateKeyByPath(hdKey *bip32.ExtendedKey, chain, num uint32) (*bec.PrivateKey, error) { // Get the child key from the num & chain childKeyNum, err := GetHDKeyByPath(hdKey, chain, num) @@ -108,7 +108,7 @@ func GetPrivateKeyByPath(hdKey *hdkeychain.ExtendedKey, chain, num uint32) (*bsv // with a given hdKey // // Expects hdKey to not be nil (otherwise will panic) -func GetPrivateKeyFromHDKey(hdKey *hdkeychain.ExtendedKey) (*bsvec.PrivateKey, error) { +func GetPrivateKeyFromHDKey(hdKey *bip32.ExtendedKey) (*bec.PrivateKey, error) { return hdKey.ECPrivKey() } @@ -116,25 +116,25 @@ func GetPrivateKeyFromHDKey(hdKey *hdkeychain.ExtendedKey) (*bsvec.PrivateKey, e // associated with a given hdKey // // Expects hdKey to not be nil (otherwise will panic) -func GetPrivateKeyStringFromHDKey(hdKey *hdkeychain.ExtendedKey) (string, error) { +func GetPrivateKeyStringFromHDKey(hdKey *bip32.ExtendedKey) (string, error) { key, err := GetPrivateKeyFromHDKey(hdKey) if err != nil { return "", err } - return hex.EncodeToString(key.Serialize()), nil + return hex.EncodeToString(key.Serialise()), nil } // GetPublicKeyFromHDKey is a helper function to get the Public Key associated with a given hdKey // // Expects hdKey to not be nil (otherwise will panic) -func GetPublicKeyFromHDKey(hdKey *hdkeychain.ExtendedKey) (*bsvec.PublicKey, error) { +func GetPublicKeyFromHDKey(hdKey *bip32.ExtendedKey) (*bec.PublicKey, error) { return hdKey.ECPubKey() } // 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 *hdkeychain.ExtendedKey) (*bsvutil.LegacyAddressPubKeyHash, error) { +func GetAddressFromHDKey(hdKey *bip32.ExtendedKey) (*bscript.Address, error) { pubKey, err := GetPublicKeyFromHDKey(hdKey) if err != nil { return nil, err @@ -145,27 +145,27 @@ func GetAddressFromHDKey(hdKey *hdkeychain.ExtendedKey) (*bsvutil.LegacyAddressP // GetAddressStringFromHDKey is a helper function to get the Address (string) associated with a given hdKey // // Expects hdKey to not be nil (otherwise will panic) -func GetAddressStringFromHDKey(hdKey *hdkeychain.ExtendedKey) (string, error) { +func GetAddressStringFromHDKey(hdKey *bip32.ExtendedKey) (string, error) { address, err := GetAddressFromHDKey(hdKey) if err != nil { return "", err } - return address.String(), nil + return address.AddressString, nil } // GetPublicKeysForPath gets the PublicKeys for a given derivation path // Uses the standard m/0/0 (external) and m/0/1 (internal) paths // Reference: https://en.bitcoin.it/wiki/BIP_0032#The_default_wallet_layout -func GetPublicKeysForPath(hdKey *hdkeychain.ExtendedKey, num uint32) (pubKeys []*bsvec.PublicKey, err error) { +func GetPublicKeysForPath(hdKey *bip32.ExtendedKey, num uint32) (pubKeys []*bec.PublicKey, err error) { // m/0/x - var childM0x *hdkeychain.ExtendedKey + var childM0x *bip32.ExtendedKey if childM0x, err = GetHDKeyByPath(hdKey, DefaultExternalChain, num); err != nil { return } - // Get the external pubkey from m/0/x - var pubKey *bsvec.PublicKey + // Get the external pubKey from m/0/x + var pubKey *bec.PublicKey if pubKey, err = childM0x.ECPubKey(); err != nil { // Should never error since the previous method ensures a valid hdKey return @@ -173,13 +173,13 @@ func GetPublicKeysForPath(hdKey *hdkeychain.ExtendedKey, num uint32) (pubKeys [] pubKeys = append(pubKeys, pubKey) // m/1/x - var childM1x *hdkeychain.ExtendedKey + var childM1x *bip32.ExtendedKey if childM1x, err = GetHDKeyByPath(hdKey, DefaultInternalChain, num); err != nil { // Should never error since the previous method ensures a valid hdKey return } - // Get the internal pubkey from m/1/x + // Get the internal pubKey from m/1/x if pubKey, err = childM1x.ECPubKey(); err != nil { // Should never error since the previous method ensures a valid hdKey return @@ -191,29 +191,29 @@ func GetPublicKeysForPath(hdKey *hdkeychain.ExtendedKey, num uint32) (pubKeys [] // GetAddressesForPath will get the corresponding addresses for the PublicKeys at the given path m/0/x // Returns 2 keys, first is internal and second is external -func GetAddressesForPath(hdKey *hdkeychain.ExtendedKey, num uint32) (addresses []string, err error) { +func GetAddressesForPath(hdKey *bip32.ExtendedKey, num uint32) (addresses []string, err error) { // Get the public keys for the corresponding chain/num (using default chain) - var pubKeys []*bsvec.PublicKey + var pubKeys []*bec.PublicKey if pubKeys, err = GetPublicKeysForPath(hdKey, num); err != nil { return } // 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 } // GetExtendedPublicKey will get the extended public key (xPub) -func GetExtendedPublicKey(hdKey *hdkeychain.ExtendedKey) (string, error) { +func GetExtendedPublicKey(hdKey *bip32.ExtendedKey) (string, error) { // Neuter the extended public key from hd key pub, err := hdKey.Neuter() @@ -227,6 +227,6 @@ func GetExtendedPublicKey(hdKey *hdkeychain.ExtendedKey) (string, error) { } // GetHDKeyFromExtendedPublicKey will get the hd key from an existing extended public key (xPub) -func GetHDKeyFromExtendedPublicKey(xPublicKey string) (*hdkeychain.ExtendedKey, error) { - return hdkeychain.NewKeyFromString(xPublicKey) +func GetHDKeyFromExtendedPublicKey(xPublicKey string) (*bip32.ExtendedKey, error) { + return bip32.NewKeyFromString(xPublicKey) } diff --git a/hd_key_test.go b/hd_key_test.go index e58ea97..b48cdb7 100644 --- a/hd_key_test.go +++ b/hd_key_test.go @@ -5,9 +5,9 @@ import ( "fmt" "testing" - "github.com/bitcoinsv/bsvd/bsvec" - "github.com/bitcoinsv/bsvutil" - "github.com/bitcoinsv/bsvutil/hdkeychain" + "github.com/libsv/go-bk/bec" + "github.com/libsv/go-bk/bip32" + "github.com/libsv/go-bt/v2/bscript" "github.com/stretchr/testify/assert" ) @@ -139,7 +139,7 @@ func TestGetPrivateKeyByPath(t *testing.T) { } var tests = []struct { - inputHDKey *hdkeychain.ExtendedKey + inputHDKey *bip32.ExtendedKey inputChain uint32 inputNum uint32 expectedNil bool @@ -159,7 +159,7 @@ func TestGetPrivateKeyByPath(t *testing.T) { {validKey, 1<<32 - 1, 1<<32 - 1, false, false}, } - var privateKey *bsvec.PrivateKey + var privateKey *bec.PrivateKey for _, test := range tests { if privateKey, err = GetPrivateKeyByPath(test.inputHDKey, test.inputChain, test.inputNum); err != nil && !test.expectedError { t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and error not expected but got: %s", t.Name(), test.inputHDKey, test.inputChain, test.inputNum, err.Error()) @@ -169,7 +169,7 @@ func TestGetPrivateKeyByPath(t *testing.T) { t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and was nil but not expected", t.Name(), test.inputHDKey, test.inputChain, test.inputNum) } else if privateKey != nil && test.expectedNil { t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.inputHDKey, test.inputChain, test.inputNum) - } else if privateKey != nil && len(hex.EncodeToString(privateKey.Serialize())) == 0 { + } else if privateKey != nil && len(hex.EncodeToString(privateKey.Serialise())) == 0 { t.Fatalf("%s Failed: [%v] [%d] [%d] inputted and should not be empty", t.Name(), test.inputHDKey, test.inputChain, test.inputNum) } } @@ -195,13 +195,13 @@ func ExampleGetPrivateKeyByPath() { } // Get a private key at the path - var privateKey *bsvec.PrivateKey + var privateKey *bec.PrivateKey privateKey, err = GetPrivateKeyByPath(hdKey, 0, 1) if err != nil { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("private key (%d) found at path %d/%d", len(privateKey.Serialize()), 0, 1) + fmt.Printf("private key (%d) found at path %d/%d", len(privateKey.Serialise()), 0, 1) // Output:private key (32) found at path 0/1 } @@ -225,7 +225,7 @@ func TestGetHDKeyByPath(t *testing.T) { // Max depth key /* - var maxKey *hdkeychain.ExtendedKey + var maxKey *bip32.ExtendedKey maxKey, err = GetHDKeyByPath(validKey, 1<<9, 1<<9) if err != nil { t.Fatalf("error occurred: %s", err.Error()) @@ -244,7 +244,7 @@ func TestGetHDKeyByPath(t *testing.T) { */ var tests = []struct { - inputHDKey *hdkeychain.ExtendedKey + inputHDKey *bip32.ExtendedKey inputChain uint32 inputNum uint32 expectedNil bool @@ -298,7 +298,7 @@ func ExampleGetHDKeyByPath() { } // Get a child key - var childKey *hdkeychain.ExtendedKey + var childKey *bip32.ExtendedKey childKey, err = GetHDKeyByPath(hdKey, 0, 1) if err != nil { fmt.Printf("error occurred: %s", err.Error()) @@ -328,7 +328,7 @@ func TestGetHDKeyChild(t *testing.T) { // Max depth key /* - var maxKey *hdkeychain.ExtendedKey + var maxKey *bip32.ExtendedKey maxKey, err = GetHDKeyByPath(validKey, 1<<9, 1<<9) if err != nil { t.Fatalf("error occurred: %s", err.Error()) @@ -353,12 +353,12 @@ func TestGetHDKeyChild(t *testing.T) { */ var tests = []struct { - inputHDKey *hdkeychain.ExtendedKey + inputHDKey *bip32.ExtendedKey inputNum uint32 expectedNil bool expectedError bool }{ - //{nil, 0, true, true}, + // {nil, 0, true, true}, {validKey, 0, false, false}, {validKey, 10, false, false}, {validKey, 100, false, false}, @@ -407,7 +407,7 @@ func ExampleGetHDKeyChild() { } // Get a child key - var childKey *hdkeychain.ExtendedKey + var childKey *bip32.ExtendedKey childKey, err = GetHDKeyChild(hdKey, 0) if err != nil { fmt.Printf("error occurred: %s", err.Error()) @@ -487,12 +487,12 @@ func TestGetPrivateKeyFromHDKey(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey expectedKey string expectedNil bool expectedError bool }{ - {new(hdkeychain.ExtendedKey), "", true, true}, + {new(bip32.ExtendedKey), "", true, true}, {validHdKey, "8511f5e1e35ab748e7639aa68666df71857866af13fda1d081d5917948a6cd34", false, false}, } @@ -505,8 +505,8 @@ func TestGetPrivateKeyFromHDKey(t *testing.T) { t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input) } else if privateKey != nil && test.expectedNil { t.Fatalf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input) - } else if privateKey != nil && hex.EncodeToString(privateKey.Serialize()) != test.expectedKey { - t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(privateKey.Serialize())) + } else if privateKey != nil && hex.EncodeToString(privateKey.Serialise()) != test.expectedKey { + t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(privateKey.Serialise())) } } } @@ -530,13 +530,13 @@ func ExampleGetPrivateKeyFromHDKey() { return } - var privateKey *bsvec.PrivateKey + var privateKey *bec.PrivateKey if privateKey, err = GetPrivateKeyFromHDKey(hdKey); err != nil { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("private key: %s", hex.EncodeToString(privateKey.Serialize())) + fmt.Printf("private key: %s", hex.EncodeToString(privateKey.Serialise())) // Output:private key: 0ccf07f2cbe10dbe6f6034b7efbf62fc83cac3d44f49d67aa22ac8893d294e7a } @@ -557,11 +557,11 @@ func TestGetPrivateKeyStringFromHDKey(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey expectedKey string expectedError bool }{ - {new(hdkeychain.ExtendedKey), "", true}, + {new(bip32.ExtendedKey), "", true}, {validHdKey, "8511f5e1e35ab748e7639aa68666df71857866af13fda1d081d5917948a6cd34", false}, } @@ -623,16 +623,16 @@ func TestGetPublicKeyFromHDKey(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey expectedKey string expectedNil bool expectedError bool }{ - {new(hdkeychain.ExtendedKey), "", true, true}, + {new(bip32.ExtendedKey), "", true, true}, {validHdKey, "02f2a2942b9d1dba033d36ab0c193e680415f5c8c1ff5d854f805c8c42ed9dd1fd", false, false}, } - var publicKey *bsvec.PublicKey + var publicKey *bec.PublicKey for _, test := range tests { if publicKey, err = GetPublicKeyFromHDKey(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()) @@ -642,8 +642,8 @@ func TestGetPublicKeyFromHDKey(t *testing.T) { t.Fatalf("%s Failed: [%v] inputted and was nil but not expected", t.Name(), test.input) } else if publicKey != nil && test.expectedNil { t.Fatalf("%s Failed: [%v] inputted and was NOT nil but expected to be nil", t.Name(), test.input) - } else if publicKey != nil && hex.EncodeToString(publicKey.SerializeCompressed()) != test.expectedKey { - t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(publicKey.SerializeCompressed())) + } else if publicKey != nil && hex.EncodeToString(publicKey.SerialiseCompressed()) != test.expectedKey { + t.Fatalf("%s Failed: [%v] inputted [%s] expected but got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(publicKey.SerialiseCompressed())) } } } @@ -667,13 +667,13 @@ func ExampleGetPublicKeyFromHDKey() { return } - var publicKey *bsvec.PublicKey + var publicKey *bec.PublicKey if publicKey, err = GetPublicKeyFromHDKey(hdKey); err != nil { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("public key: %s", hex.EncodeToString(publicKey.SerializeCompressed())) + fmt.Printf("public key: %s", hex.EncodeToString(publicKey.SerialiseCompressed())) // Output:public key: 03a25f6c10eedcd41eebac22c6bbc5278690fa1aab3afc2bbe8f2277c85e5c5def } @@ -694,16 +694,16 @@ func TestGetAddressFromHDKey(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey expectedAddress string expectedNil bool expectedError bool }{ - {new(hdkeychain.ExtendedKey), "", true, true}, + {new(bip32.ExtendedKey), "", true, true}, {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 } @@ -765,11 +765,11 @@ func TestGetAddressStringFromHDKey(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey expectedAddress string expectedError bool }{ - {new(hdkeychain.ExtendedKey), "", true}, + {new(bip32.ExtendedKey), "", true}, {validHdKey, "13xHrMdZuqa2gpweHf37w8hu6tfv3JrnaW", false}, } @@ -831,21 +831,21 @@ func TestGetPublicKeysForPath(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey inputNum uint32 expectedPubKey1 string expectedPubKey2 string expectedNil bool expectedError bool }{ - {new(hdkeychain.ExtendedKey), 1, "", "", true, true}, + {new(bip32.ExtendedKey), 1, "", "", true, true}, {validHdKey, 1, "03cc3334f0a6f0fae0420d1442ca0ce64fad0da76d652f2cc3b333e7ed95b97259", "02ceb23902f8dcf6fbff656597ee0343e05c907c6dfcdd8aaf6d033e14e85fd955", false, false}, {validHdKey, 2, "020cb908e3b9f3de7c9b40e7bcce63708c5617536d85cf4ab5635e3d3819c02c37", "030007ae60fc6eef98ea17b4f80f9b791e61ea94936e8a9e6ec343eeaa50a875e0", false, false}, {validHdKey, 3, "0342593453c476ac6c78eb1b1e586df00b20352e61c42536fe1b33c9fdf3bfbb6f", "03786a41dbf0b099256da26cb0019e10063628f6ce31b96801703f1bb2e1b17724", false, false}, {validHdKey, 4, "0366dcdebfc8abfd34bffc181ccb54f1706839a80ad4f0842ae5a43f39fdd35c1e", "03a095db29ae9ee0b22c775118b4444b59db40acdea137fd9ecd9c68dacf50a644", false, false}, } - var pubKeys []*bsvec.PublicKey + var pubKeys []*bec.PublicKey for _, test := range tests { if pubKeys, err = GetPublicKeysForPath(test.input, test.inputNum); err != nil && !test.expectedError { t.Fatalf("%s Failed: [%v] [%d] inputted and error not expected but got: %s", t.Name(), test.input, test.inputNum, err.Error()) @@ -855,10 +855,10 @@ func TestGetPublicKeysForPath(t *testing.T) { t.Fatalf("%s Failed: [%v] [%d] inputted and was nil but not expected", t.Name(), test.input, test.inputNum) } else if pubKeys != nil && test.expectedNil { t.Fatalf("%s Failed: [%v] [%d] inputted and was NOT nil but expected to be nil", t.Name(), test.input, test.inputNum) - } else if pubKeys != nil && hex.EncodeToString(pubKeys[0].SerializeCompressed()) != test.expectedPubKey1 { - t.Fatalf("%s Failed: [%v] [%d] inputted key 1 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedPubKey1, hex.EncodeToString(pubKeys[0].SerializeCompressed())) - } else if pubKeys != nil && hex.EncodeToString(pubKeys[1].SerializeCompressed()) != test.expectedPubKey2 { - t.Fatalf("%s Failed: [%v] [%d] inputted key 2 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedPubKey2, hex.EncodeToString(pubKeys[1].SerializeCompressed())) + } else if pubKeys != nil && hex.EncodeToString(pubKeys[0].SerialiseCompressed()) != test.expectedPubKey1 { + t.Fatalf("%s Failed: [%v] [%d] inputted key 1 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedPubKey1, hex.EncodeToString(pubKeys[0].SerialiseCompressed())) + } else if pubKeys != nil && hex.EncodeToString(pubKeys[1].SerialiseCompressed()) != test.expectedPubKey2 { + t.Fatalf("%s Failed: [%v] [%d] inputted key 2 [%s] expected but got: %s", t.Name(), test.input, test.inputNum, test.expectedPubKey2, hex.EncodeToString(pubKeys[1].SerialiseCompressed())) } } } @@ -882,14 +882,14 @@ func ExampleGetPublicKeysForPath() { return } - var publicKeys []*bsvec.PublicKey + var publicKeys []*bec.PublicKey publicKeys, err = GetPublicKeysForPath(hdKey, 5) if err != nil { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("found [%d] keys! Key 1: %s Key 2: %s", len(publicKeys), hex.EncodeToString(publicKeys[0].SerializeCompressed()), hex.EncodeToString(publicKeys[1].SerializeCompressed())) + fmt.Printf("found [%d] keys! Key 1: %s Key 2: %s", len(publicKeys), hex.EncodeToString(publicKeys[0].SerialiseCompressed()), hex.EncodeToString(publicKeys[1].SerialiseCompressed())) // Output:found [2] keys! Key 1: 03f87ac38fb0cfca12988b51a2f1cd3e85bb4aeb1b05f549682190ac8205a67d30 Key 2: 02e78303aeef1acce1347c6493fadc1914e6d85ef3189a8856afb3accd53fbd9c5 } @@ -910,14 +910,14 @@ func TestGetAddressesForPath(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey inputNum uint32 expectedAddress1 string expectedAddress2 string expectedNil bool expectedError bool }{ - {new(hdkeychain.ExtendedKey), 1, "", "", true, true}, + {new(bip32.ExtendedKey), 1, "", "", true, true}, {validHdKey, 1, "1KMxfSfRCkC1jrBAuYaLde4XBzdsWApbdH", "174DL9ZbBWx568ssAg8w2YwW6FTTBwXGEu", false, false}, {validHdKey, 2, "18s3peTU7fMSwgui54avpnqm1126pRVccw", "1KgZZ3NsJDw3v1GPHBj8ASnxutA1kFxo2i", false, false}, } @@ -985,12 +985,12 @@ func TestGetExtendedPublicKey(t *testing.T) { assert.NotNil(t, validHdKey) var tests = []struct { - input *hdkeychain.ExtendedKey + input *bip32.ExtendedKey expectedKey string expectedError bool }{ {validHdKey, "xpub661MyMwAqRbcGjhmJnvR198z2x9XnnDhz2yBtLuTdXQ2VBQj8eJ9RnxmXxKnRPhYy6nLsmabmUfVkbajvP7aZASrrnoZkzmwgyjiNskiefG", false}, - {new(hdkeychain.ExtendedKey), "zeroed extended key", false}, + {new(bip32.ExtendedKey), "zeroed extended key", false}, } var xPub string diff --git a/private_key.go b/private_key.go index 3a6dcf2..1d05cf0 100644 --- a/private_key.go +++ b/private_key.go @@ -6,23 +6,23 @@ import ( "errors" "math/big" - "github.com/bitcoinsv/bsvd/bsvec" - "github.com/bitcoinsv/bsvd/chaincfg" - "github.com/bitcoinsv/bsvutil" + "github.com/libsv/go-bk/bec" + "github.com/libsv/go-bk/chaincfg" + "github.com/libsv/go-bk/wif" ) // GenerateSharedKeyPair creates shared keys that can be used to encrypt/decrypt data // that can be decrypted by yourself (privateKey) and also the owner of the given public key -func GenerateSharedKeyPair(privateKey *bsvec.PrivateKey, - pubKey *bsvec.PublicKey) (*bsvec.PrivateKey, *bsvec.PublicKey) { - return bsvec.PrivKeyFromBytes( - bsvec.S256(), - bsvec.GenerateSharedSecret(privateKey, pubKey), +func GenerateSharedKeyPair(privateKey *bec.PrivateKey, + pubKey *bec.PublicKey) (*bec.PrivateKey, *bec.PublicKey) { + return bec.PrivKeyFromBytes( + bec.S256(), + bec.GenerateSharedSecret(privateKey, pubKey), ) } -// PrivateKeyFromString turns a private key (hex encoded string) into an bsvec.PrivateKey -func PrivateKeyFromString(privateKey string) (*bsvec.PrivateKey, error) { +// PrivateKeyFromString turns a private key (hex encoded string) into an bec.PrivateKey +func PrivateKeyFromString(privateKey string) (*bec.PrivateKey, error) { if len(privateKey) == 0 { return nil, errors.New("privateKey is missing") } @@ -30,18 +30,18 @@ func PrivateKeyFromString(privateKey string) (*bsvec.PrivateKey, error) { if err != nil { return nil, err } - x, y := bsvec.S256().ScalarBaseMult(privateKeyBytes) + x, y := bec.S256().ScalarBaseMult(privateKeyBytes) ecdsaPubKey := ecdsa.PublicKey{ - Curve: bsvec.S256(), + Curve: bec.S256(), X: x, Y: y, } - return &bsvec.PrivateKey{PublicKey: ecdsaPubKey, D: new(big.Int).SetBytes(privateKeyBytes)}, nil + return &bec.PrivateKey{PublicKey: ecdsaPubKey, D: new(big.Int).SetBytes(privateKeyBytes)}, nil } -// CreatePrivateKey will create a new private key (*bsvec.PrivateKey) -func CreatePrivateKey() (*bsvec.PrivateKey, error) { - return bsvec.NewPrivateKey(bsvec.S256()) +// CreatePrivateKey will create a new private key (*bec.PrivateKey) +func CreatePrivateKey() (*bec.PrivateKey, error) { + return bec.NewPrivateKey(bec.S256()) } // CreatePrivateKeyString will create a new private key (hex encoded) @@ -51,12 +51,12 @@ func CreatePrivateKeyString() (string, error) { return "", err } - return hex.EncodeToString(privateKey.Serialize()), nil + return hex.EncodeToString(privateKey.Serialise()), nil } // PrivateAndPublicKeys will return both the private and public key in one method // Expects a hex encoded privateKey -func PrivateAndPublicKeys(privateKey string) (*bsvec.PrivateKey, *bsvec.PublicKey, error) { +func PrivateAndPublicKeys(privateKey string) (*bec.PrivateKey, *bec.PublicKey, error) { // No key? if len(privateKey) == 0 { @@ -70,12 +70,12 @@ func PrivateAndPublicKeys(privateKey string) (*bsvec.PrivateKey, *bsvec.PublicKe } // Get the public and private key from the bytes - rawKey, publicKey := bsvec.PrivKeyFromBytes(bsvec.S256(), privateKeyBytes) + rawKey, publicKey := bec.PrivKeyFromBytes(bec.S256(), privateKeyBytes) return rawKey, publicKey, nil } -// PrivateKeyToWif will convert a private key to a WIF (*bsvutil.WIF) -func PrivateKeyToWif(privateKey string) (*bsvutil.WIF, error) { +// PrivateKeyToWif will convert a private key to a WIF (*wif.WIF) +func PrivateKeyToWif(privateKey string) (*wif.WIF, error) { // Missing private key if len(privateKey) == 0 { @@ -89,32 +89,32 @@ func PrivateKeyToWif(privateKey string) (*bsvutil.WIF, error) { } // Get the private key from bytes - rawKey, _ := bsvec.PrivKeyFromBytes(bsvec.S256(), decodedKey) + rawKey, _ := bec.PrivKeyFromBytes(bec.S256(), decodedKey) // Create a new WIF (error never gets hit since (net) is set correctly) - return bsvutil.NewWIF(rawKey, &chaincfg.MainNetParams, false) + return wif.NewWIF(rawKey, &chaincfg.MainNet, false) } // PrivateKeyToWifString will convert a private key to a WIF (string) func PrivateKeyToWifString(privateKey string) (string, error) { - wif, err := PrivateKeyToWif(privateKey) + privateWif, err := PrivateKeyToWif(privateKey) if err != nil { return "", err } - return wif.String(), nil + return privateWif.String(), nil } -// WifToPrivateKey will convert a WIF to a private key (*bsvec.PrivateKey) -func WifToPrivateKey(wif string) (*bsvec.PrivateKey, error) { +// WifToPrivateKey will convert a WIF to a private key (*bec.PrivateKey) +func WifToPrivateKey(wifKey string) (*bec.PrivateKey, error) { // Missing wif? - if len(wif) == 0 { + if len(wifKey) == 0 { return nil, errors.New("missing wif") } // Decode the wif - decodedWif, err := bsvutil.DecodeWIF(wif) + decodedWif, err := wif.DecodeWIF(wifKey) if err != nil { return nil, err } @@ -133,5 +133,5 @@ func WifToPrivateKeyString(wif string) (string, error) { } // Return the hex (string) version of the private key - return hex.EncodeToString(privateKey.Serialize()), nil + return hex.EncodeToString(privateKey.Serialise()), nil } diff --git a/private_key_test.go b/private_key_test.go index d612422..8e6dc6b 100644 --- a/private_key_test.go +++ b/private_key_test.go @@ -13,7 +13,7 @@ func TestCreatePrivateKey(t *testing.T) { rawKey, err := CreatePrivateKey() assert.NoError(t, err) assert.NotNil(t, rawKey) - assert.Equal(t, 32, len(rawKey.Serialize())) + assert.Equal(t, 32, len(rawKey.Serialise())) } // ExampleCreatePrivateKey example using CreatePrivateKey() @@ -22,7 +22,7 @@ func ExampleCreatePrivateKey() { if err != nil { fmt.Printf("error occurred: %s", err.Error()) return - } else if len(rawKey.Serialize()) > 0 { + } else if len(rawKey.Serialise()) > 0 { fmt.Printf("key created successfully!") } // Output:key created successfully! @@ -89,8 +89,8 @@ func TestPrivateKeyFromString(t *testing.T) { t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input) } else if rawKey != nil && test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input) - } else if rawKey != nil && hex.EncodeToString(rawKey.Serialize()) != test.expectedKey { - t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(rawKey.Serialize())) + } else if rawKey != nil && hex.EncodeToString(rawKey.Serialise()) != test.expectedKey { + t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(rawKey.Serialise())) } } } @@ -102,7 +102,7 @@ func ExamplePrivateKeyFromString() { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("key converted: %s", hex.EncodeToString(key.Serialize())) + fmt.Printf("key converted: %s", hex.EncodeToString(key.Serialise())) // Output:key converted: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd } @@ -142,8 +142,8 @@ func TestPrivateAndPublicKeys(t *testing.T) { t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input) } else if (privateKey != nil || publicKey != nil) && test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input) - } else if privateKey != nil && hex.EncodeToString(privateKey.Serialize()) != test.expectedPrivateKey { - t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedPrivateKey, hex.EncodeToString(privateKey.Serialize())) + } else if privateKey != nil && hex.EncodeToString(privateKey.Serialise()) != test.expectedPrivateKey { + t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedPrivateKey, hex.EncodeToString(privateKey.Serialise())) } } } @@ -155,7 +155,7 @@ func ExamplePrivateAndPublicKeys() { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("private key: %s public key: %s", hex.EncodeToString(privateKey.Serialize()), hex.EncodeToString(publicKey.SerializeCompressed())) + fmt.Printf("private key: %s public key: %s", hex.EncodeToString(privateKey.Serialise()), hex.EncodeToString(publicKey.SerialiseCompressed())) // Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd public key: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f } @@ -188,16 +188,16 @@ func TestPrivateKeyToWif(t *testing.T) { } for _, test := range tests { - if wif, err := PrivateKeyToWif(test.input); err != nil && !test.expectedError { + if privateWif, err := PrivateKeyToWif(test.input); err != nil && !test.expectedError { t.Fatalf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error()) } else if err == nil && test.expectedError { t.Fatalf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input) - } else if wif == nil && !test.expectedNil { + } else if privateWif == nil && !test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input) - } else if wif != nil && test.expectedNil { + } else if privateWif != nil && test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input) - } else if wif != nil && wif.String() != test.expectedWif { - t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, wif.String()) + } else if privateWif != nil && privateWif.String() != test.expectedWif { + t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, privateWif.String()) } } @@ -205,12 +205,12 @@ func TestPrivateKeyToWif(t *testing.T) { // ExamplePrivateKeyToWif example using PrivateKeyToWif() func ExamplePrivateKeyToWif() { - wif, err := PrivateKeyToWif("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd") + privateWif, err := PrivateKeyToWif("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd") if err != nil { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("converted wif: %s", wif.String()) + fmt.Printf("converted wif: %s", privateWif.String()) // Output:converted wif: 5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei } @@ -242,12 +242,12 @@ func TestPrivateKeyToWifString(t *testing.T) { } for _, test := range tests { - if wif, err := PrivateKeyToWifString(test.input); err != nil && !test.expectedError { + if privateWif, err := PrivateKeyToWifString(test.input); err != nil && !test.expectedError { t.Fatalf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error()) } else if err == nil && test.expectedError { t.Fatalf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input) - } else if wif != test.expectedWif { - t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, wif) + } else if privateWif != test.expectedWif { + t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedWif, privateWif) } } @@ -255,12 +255,12 @@ func TestPrivateKeyToWifString(t *testing.T) { // ExamplePrivateKeyToWifString example using PrivateKeyToWifString() func ExamplePrivateKeyToWifString() { - wif, err := PrivateKeyToWifString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd") + privateWif, err := PrivateKeyToWifString("54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd") if err != nil { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("converted wif: %s", wif) + fmt.Printf("converted wif: %s", privateWif) // Output:converted wif: 5JTHas7yTFMBLqgFogxZFf8Vc5uKEbkE7yQAQ2g3xPHo2sNG1Ei } @@ -300,8 +300,8 @@ func TestWifToPrivateKey(t *testing.T) { t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.input) } else if privateKey != nil && test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.input) - } else if privateKey != nil && hex.EncodeToString(privateKey.Serialize()) != test.expectedKey { - t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(privateKey.Serialize())) + } else if privateKey != nil && hex.EncodeToString(privateKey.Serialise()) != test.expectedKey { + t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of keys, got: %s", t.Name(), test.input, test.expectedKey, hex.EncodeToString(privateKey.Serialise())) } } } @@ -313,7 +313,7 @@ func ExampleWifToPrivateKey() { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("private key: %s", hex.EncodeToString(privateKey.Serialize())) + fmt.Printf("private key: %s", hex.EncodeToString(privateKey.Serialise())) // Output:private key: 54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd } diff --git a/pubkey.go b/pubkey.go index 26e44b0..3789ee8 100644 --- a/pubkey.go +++ b/pubkey.go @@ -4,7 +4,7 @@ import ( "encoding/hex" "errors" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" ) // PubKeyFromPrivateKeyString will derive a pubKey (hex encoded) from a given private key @@ -18,16 +18,16 @@ func PubKeyFromPrivateKeyString(privateKey string, compressed bool) (string, err } // PubKeyFromPrivateKey will derive a pubKey (hex encoded) from a given private key -func PubKeyFromPrivateKey(privateKey *bsvec.PrivateKey, compressed bool) string { +func PubKeyFromPrivateKey(privateKey *bec.PrivateKey, compressed bool) string { if compressed { - return hex.EncodeToString(privateKey.PubKey().SerializeCompressed()) + return hex.EncodeToString(privateKey.PubKey().SerialiseCompressed()) } - return hex.EncodeToString(privateKey.PubKey().SerializeUncompressed()) + return hex.EncodeToString(privateKey.PubKey().SerialiseUncompressed()) } -// PubKeyFromString will convert a pubKey (string) into a pubkey (*bsvec.PublicKey) -func PubKeyFromString(pubKey string) (*bsvec.PublicKey, error) { +// PubKeyFromString will convert a pubKey (string) into a pubkey (*bec.PublicKey) +func PubKeyFromString(pubKey string) (*bec.PublicKey, error) { // Invalid pubKey if len(pubKey) == 0 { @@ -41,5 +41,5 @@ func PubKeyFromString(pubKey string) (*bsvec.PublicKey, error) { } // Parse into a pubKey - return bsvec.ParsePubKey(decoded, bsvec.S256()) + return bec.ParsePubKey(decoded, bec.S256()) } diff --git a/pubkey_test.go b/pubkey_test.go index a5598b1..bc90a68 100644 --- a/pubkey_test.go +++ b/pubkey_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - "github.com/bitcoinsv/bsvd/bsvec" + "github.com/libsv/go-bk/bec" "github.com/stretchr/testify/assert" ) @@ -16,15 +16,17 @@ func TestPubKeyFromPrivateKeyString(t *testing.T) { var tests = []struct { inputKey string expectedPubKey string + compressed bool expectedError bool }{ - {"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", false}, - {"0", "", true}, - {"", "", true}, + {"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f", true, false}, + {"54035dd4c7dda99ac473905a3d82f7864322b49bab1ff441cc457183b9bd8abd", "041b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f36e7ef720509250313fcf1b4c5af0dc7c5efa126efe2c3b7008e6f1487c61f31", false, false}, + {"0", "", true, true}, + {"", "", true, true}, } for _, test := range tests { - if pubKey, err := PubKeyFromPrivateKeyString(test.inputKey, true); err != nil && !test.expectedError { + if pubKey, err := PubKeyFromPrivateKeyString(test.inputKey, test.compressed); err != nil && !test.expectedError { t.Fatalf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.inputKey, err.Error()) } else if err == nil && test.expectedError { t.Fatalf("%s Failed: [%s] inputted and error was expected", t.Name(), test.inputKey) @@ -62,7 +64,7 @@ func TestPubKeyFromPrivateKey(t *testing.T) { assert.NotNil(t, priv) var tests = []struct { - inputKey *bsvec.PrivateKey + inputKey *bec.PrivateKey expectedPubKey string expectedError bool }{ @@ -133,8 +135,8 @@ func TestPubKeyFromString(t *testing.T) { t.Fatalf("%s Failed: [%s] inputted and nil was expected", t.Name(), test.inputKey) } else if pubKey == nil && !test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and nil was NOT expected", t.Name(), test.inputKey) - } else if pubKey != nil && hex.EncodeToString(pubKey.SerializeCompressed()) != test.expectedPubKey { - t.Fatalf("%s Failed: [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, hex.EncodeToString(pubKey.SerializeCompressed())) + } else if pubKey != nil && hex.EncodeToString(pubKey.SerialiseCompressed()) != test.expectedPubKey { + t.Fatalf("%s Failed: [%s] inputted and [%s] expected, but got: %s", t.Name(), test.inputKey, test.expectedPubKey, hex.EncodeToString(pubKey.SerialiseCompressed())) } } } @@ -146,7 +148,7 @@ func ExamplePubKeyFromString() { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("pubkey from string: %s", hex.EncodeToString(pubKey.SerializeCompressed())) + fmt.Printf("pubkey from string: %s", hex.EncodeToString(pubKey.SerialiseCompressed())) // Output:pubkey from string: 031b8c93100d35bd448f4646cc4678f278351b439b52b303ea31ec9edb5475e73f } diff --git a/script.go b/script.go index da6f293..d6548b7 100644 --- a/script.go +++ b/script.go @@ -3,7 +3,7 @@ package bitcoin import ( "errors" - "github.com/libsv/go-bt/bscript" + "github.com/libsv/go-bt/v2/bscript" ) // ScriptFromAddress will create an output P2PKH script from an address string @@ -20,5 +20,5 @@ func ScriptFromAddress(address string) (string, error) { } // Return the string version - return rawScript.ToString(), nil + return rawScript.String(), nil } diff --git a/sign.go b/sign.go index 7f956cb..cfba058 100644 --- a/sign.go +++ b/sign.go @@ -5,9 +5,9 @@ import ( "encoding/base64" "errors" - "github.com/bitcoinsv/bsvd/bsvec" "github.com/bitcoinsv/bsvd/chaincfg/chainhash" "github.com/bitcoinsv/bsvd/wire" + "github.com/libsv/go-bk/bec" ) // SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding @@ -31,14 +31,14 @@ func SignMessage(privateKey string, message string, sigRefCompressedKey bool) (s messageHash := chainhash.DoubleHashB(buf.Bytes()) // Get the private key - var ecdsaPrivateKey *bsvec.PrivateKey + var ecdsaPrivateKey *bec.PrivateKey if ecdsaPrivateKey, err = PrivateKeyFromString(privateKey); err != nil { return "", err } // Sign var sigBytes []byte - if sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, messageHash, sigRefCompressedKey); err != nil { + if sigBytes, err = bec.SignCompact(bec.S256(), ecdsaPrivateKey, messageHash, sigRefCompressedKey); err != nil { return "", err } diff --git a/transaction.go b/transaction.go index 0ca9327..2602cad 100644 --- a/transaction.go +++ b/transaction.go @@ -1,12 +1,15 @@ package bitcoin import ( + "context" "errors" "fmt" "strings" - "github.com/bitcoinsv/bsvd/bsvec" - "github.com/libsv/go-bt" + "github.com/libsv/go-bk/bec" + "github.com/libsv/go-bt/v2" + "github.com/libsv/go-bt/v2/bscript" + "github.com/libsv/go-bt/v2/unlocker" ) const ( @@ -30,6 +33,18 @@ type PayToAddress struct { Satoshis uint64 `json:"satoshis"` } +// account is a struct/interface for implementing unlocker +type account struct { + PrivateKey *bec.PrivateKey +} + +// Unlocker get the correct un-locker for a given locking script +func (a *account) Unlocker(context.Context, *bscript.Script) (bt.Unlocker, error) { + return &unlocker.Simple{ + PrivateKey: a.PrivateKey, + }, nil +} + // OpReturnData is the op return data to include in the tx type OpReturnData [][]byte @@ -44,7 +59,7 @@ func TxFromHex(rawHex string) (*bt.Tx, error) { // USE AT YOUR OWN RISK - this will modify a "pay-to" output to accomplish auto-fees func CreateTxWithChange(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns []OpReturnData, changeAddress string, standardRate, dataRate *bt.Fee, - privateKey *bsvec.PrivateKey) (*bt.Tx, error) { + privateKey *bec.PrivateKey) (*bt.Tx, error) { // Missing utxo(s) or change address if len(utxos) == 0 { @@ -156,7 +171,7 @@ func CreateTxWithChange(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns // draftTx is a helper method to create a draft tx and associated fees func draftTx(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns []OpReturnData, - privateKey *bsvec.PrivateKey, standardRate, dataRate *bt.Fee) (uint64, error) { + privateKey *bec.PrivateKey, standardRate, dataRate *bt.Fee) (uint64, error) { // Create the "Draft tx" tx, err := CreateTx(utxos, payToAddresses, opReturns, privateKey) @@ -195,7 +210,7 @@ func CreateTxWithChangeUsingWif(utxos []*Utxo, payToAddresses []*PayToAddress, o // Get the raw hex version: tx.ToString() // Get the tx id: tx.GetTxID() func CreateTx(utxos []*Utxo, addresses []*PayToAddress, - opReturns []OpReturnData, privateKey *bsvec.PrivateKey) (*bt.Tx, error) { + opReturns []OpReturnData, privateKey *bec.PrivateKey) (*bt.Tx, error) { // Start creating a new transaction tx := bt.NewTx() @@ -214,25 +229,29 @@ func CreateTx(utxos []*Utxo, addresses []*PayToAddress, // Loop any pay addresses for _, address := range addresses { - if err = tx.PayTo(address.Address, address.Satoshis); err != nil { + var a *bscript.Script + a, err = bscript.NewP2PKHFromAddress(address.Address) + if err != nil { + return nil, err + } + + if err = tx.PayTo(a, address.Satoshis); err != nil { return nil, err } } // Loop any op returns - var outPut *bt.Output for _, op := range opReturns { - if outPut, err = bt.NewOpReturnPartsOutput(op); err != nil { + if err = tx.AddOpReturnPartsOutput(op); err != nil { return nil, err } - tx.AddOutput(outPut) } // If inputs are supplied, make sure they are sufficient for this transaction - if len(tx.GetInputs()) > 0 { + if len(tx.Inputs) > 0 { // Sanity check - not enough satoshis in utxo(s) to cover all paid amount(s) // They should never be equal, since the fee is the spread between the two amounts - totalOutputSatoshis := tx.GetTotalOutputSatoshis() // Does not work properly + totalOutputSatoshis := tx.TotalOutputSatoshis() // Does not work properly if totalOutputSatoshis > totalSatoshis { return nil, fmt.Errorf("not enough in utxo(s) to cover: %d + (fee) found: %d", totalOutputSatoshis, totalSatoshis) } @@ -240,9 +259,9 @@ func CreateTx(utxos []*Utxo, addresses []*PayToAddress, // Sign the transaction if privateKey != nil { - - signer := bt.InternalSigner{PrivateKey: privateKey, SigHashFlag: 0} - if _, err = tx.SignAuto(&signer); err != nil { + myAccount := &account{PrivateKey: privateKey} + // todo: support context (ctx) + if err = tx.FillAllInputs(context.Background(), myAccount); err != nil { return nil, err } } @@ -271,6 +290,22 @@ func CreateTxUsingWif(utxos []*Utxo, addresses []*PayToAddress, return CreateTx(utxos, addresses, opReturns, privateKey) } +// DefaultStandardFee returns the default standard fees offered by most miners. +// this function is not public anymore in go-bt +func DefaultStandardFee() *bt.Fee { + return &bt.Fee{ + FeeType: bt.FeeTypeStandard, + MiningFee: bt.FeeUnit{ + Satoshis: 5, + Bytes: 10, + }, + RelayFee: bt.FeeUnit{ + Satoshis: 5, + Bytes: 10, + }, + } +} + // CalculateFeeForTx will estimate a fee for the given transaction // // If tx is nil this will panic @@ -285,22 +320,22 @@ func CalculateFeeForTx(tx *bt.Tx, standardRate, dataRate *bt.Fee) uint64 { // Set defaults if not found if standardRate == nil { - standardRate = bt.DefaultStandardFee() + standardRate = DefaultStandardFee() } if dataRate == nil { - dataRate = bt.DefaultStandardFee() + dataRate = DefaultStandardFee() // todo: adjusted to 5/10 for now, since all miners accept that rate dataRate.FeeType = bt.FeeTypeData } // Set the total bytes of the tx - totalBytes := len(tx.ToBytes()) + totalBytes := len(tx.Bytes()) // Loop all outputs and accumulate size (find data related outputs) - for _, out := range tx.GetOutputs() { - outHexString := out.GetLockingScriptHexString() + for _, out := range tx.Outputs { + outHexString := out.LockingScriptHexString() if strings.HasPrefix(outHexString, "006a") || strings.HasPrefix(outHexString, "6a") { - totalDataBytes += len(out.ToBytes()) + totalDataBytes += len(out.Bytes()) } } @@ -320,6 +355,6 @@ func CalculateFeeForTx(tx *bt.Tx, standardRate, dataRate *bt.Fee) uint64 { totalFee = 1 } - // Return the total fee as a uint (easier to use with satoshi values) + // Return the total fee as an uint (easier to use with satoshi values) return uint64(totalFee) } diff --git a/transaction_test.go b/transaction_test.go index b34a829..24dec16 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -4,7 +4,7 @@ import ( "fmt" "testing" - "github.com/libsv/go-bt" + "github.com/libsv/go-bt/v2" "github.com/stretchr/testify/assert" ) @@ -34,8 +34,8 @@ func TestTxFromHex(t *testing.T) { t.Fatalf("%s Failed: [%s] inputted and was nil but not expected", t.Name(), test.inputHex) } else if rawTx != nil && test.expectedNil { t.Fatalf("%s Failed: [%s] inputted and was NOT nil but expected to be nil", t.Name(), test.inputHex) - } else if rawTx != nil && rawTx.GetTxID() != test.expectedTxID { - t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of txIDs, got: %s", t.Name(), test.inputHex, test.expectedTxID, rawTx.GetTxID()) + } else if rawTx != nil && rawTx.TxID() != test.expectedTxID { + t.Fatalf("%s Failed: [%s] inputted [%s] expected but failed comparison of txIDs, got: %s", t.Name(), test.inputHex, test.expectedTxID, rawTx.TxID()) } } } @@ -47,7 +47,7 @@ func ExampleTxFromHex() { fmt.Printf("error occurred: %s", err.Error()) return } - fmt.Printf("txID: %s", tx.GetTxID()) + fmt.Printf("txID: %s", tx.TxID()) // Output:txID: 64cd12102af20195d54a107e0ee5989ac5db3491893a0b9d42e24354732a22a5 } @@ -93,7 +93,7 @@ func TestCreateTx(t *testing.T) { assert.NotNil(t, tx) assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100eea3d606bd1627be6459a9de4860919225db74843d2fc7f4e7caa5e01f42c2d0022017978d9c6a0e934955a70e7dda71d68cb614f7dd89eb7b9d560aea761834ddd4412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff03f4010000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000", - tx.ToString(), + tx.String(), ) }) @@ -106,7 +106,7 @@ func TestCreateTx(t *testing.T) { ) assert.NoError(t, err) assert.NotNil(t, tx) - assert.Equal(t, "01000000000000000000", tx.ToString()) + assert.Equal(t, "01000000000000000000", tx.String()) }) } @@ -150,7 +150,7 @@ func ExampleCreateTx() { return } - fmt.Printf("rawTx: %s", rawTx.ToString()) + fmt.Printf("rawTx: %s", rawTx.String()) // Output:rawTx: 0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100eea3d606bd1627be6459a9de4860919225db74843d2fc7f4e7caa5e01f42c2d0022017978d9c6a0e934955a70e7dda71d68cb614f7dd89eb7b9d560aea761834ddd4412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff03f4010000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000 } @@ -311,8 +311,8 @@ func TestCreateTxErrors(t *testing.T) { t.Fatalf("%s Failed: [%v] [%v] [%v] [%s] inputted and nil was not expected", t.Name(), test.inputUtxos, test.inputAddresses, test.inputOpReturns, test.inputWif) } else if rawTx != nil && test.expectedNil { t.Fatalf("%s Failed: [%v] [%v] [%v] [%s] inputted and nil was expected", t.Name(), test.inputUtxos, test.inputAddresses, test.inputOpReturns, test.inputWif) - } else if rawTx != nil && rawTx.ToString() != test.expectedRawTx { - t.Fatalf("%s Failed: [%v] [%v] [%v] [%s] inputted [%s] expected but failed comparison of scripts, got: %s", t.Name(), test.inputUtxos, test.inputAddresses, test.inputOpReturns, test.inputWif, test.expectedRawTx, rawTx.ToString()) + } else if rawTx != nil && rawTx.String() != test.expectedRawTx { + t.Fatalf("%s Failed: [%v] [%v] [%v] [%s] inputted [%s] expected but failed comparison of scripts, got: %s", t.Name(), test.inputUtxos, test.inputAddresses, test.inputOpReturns, test.inputWif, test.expectedRawTx, rawTx.String()) } } } @@ -407,7 +407,7 @@ func ExampleCreateTxUsingWif() { return } - fmt.Printf("rawTx: %s", rawTx.ToString()) + fmt.Printf("rawTx: %s", rawTx.String()) // Output:rawTx: 0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100eea3d606bd1627be6459a9de4860919225db74843d2fc7f4e7caa5e01f42c2d0022017978d9c6a0e934955a70e7dda71d68cb614f7dd89eb7b9d560aea761834ddd4412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff03f4010000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000 } @@ -628,7 +628,7 @@ func TestCalculateFeeForTxVariousTxs(t *testing.T) { satoshis = CalculateFeeForTx(tx, test.inputStandardRate, test.inputDataRate) assert.Equal(t, test.expectedSatoshis, satoshis, satoshis) - assert.Equal(t, test.expectedTxID, tx.GetTxID()) + assert.Equal(t, test.expectedTxID, tx.TxID()) }) } } @@ -647,7 +647,7 @@ func ExampleCalculateFeeForTx() { // Calculate the fee using default rates estimatedFee := CalculateFeeForTx(tx, nil, nil) - fmt.Printf("tx id: %s estimated fee: %d satoshis", tx.GetTxID(), estimatedFee) + fmt.Printf("tx id: %s estimated fee: %d satoshis", tx.TxID(), estimatedFee) // Output:tx id: e75fa79ee5fbb589201f769c01835e14ca595b7bbfa0e602050a2a90cf28d129 estimated fee: 132 satoshis } @@ -707,8 +707,8 @@ func TestCreateTxWithChange(t *testing.T) { assert.Equal(t, uint64(149), CalculateFeeForTx(rawTx, nil, nil)) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { - if out.GetLockingScriptHexString() == "76a914c9d8699bdea34b131e737447b50a8b1af0b040bf88ac" { + for _, out := range rawTx.Outputs { + if out.LockingScriptHexString() == "76a914c9d8699bdea34b131e737447b50a8b1af0b040bf88ac" { assert.Equal(t, uint64(351), out.Satoshis) } } @@ -809,7 +809,7 @@ func TestCreateTxWithChange(t *testing.T) { ) assert.NoError(t, err) assert.NotNil(t, rawTx) - assert.Equal(t, test.expectedRawTx, rawTx.ToString()) + assert.Equal(t, test.expectedRawTx, rawTx.String()) }) } }) @@ -999,7 +999,7 @@ func ExampleCreateTxWithChange() { return } - fmt.Printf("rawTx: %s", rawTx.ToString()) + fmt.Printf("rawTx: %s", rawTx.String()) // Output:rawTx: 0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100c1dc4a4c5f26a404ff3618013dc63777d0790d0b0ea3371c67ee3f1bb5126c3e02206be8c841918215337f9b6a6a6040bd058596f2bab5c8b8cb27f849b1474b9e4c412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff04f4010000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac5f010000000000001976a914c9d8699bdea34b131e737447b50a8b1af0b040bf88ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000 } @@ -1067,8 +1067,8 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { assert.Equal(t, uint64(149), CalculateFeeForTx(rawTx, nil, nil)) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { - if out.GetLockingScriptHexString() == "76a914c9d8699bdea34b131e737447b50a8b1af0b040bf88ac" { + for _, out := range rawTx.Outputs { + if out.LockingScriptHexString() == "76a914c9d8699bdea34b131e737447b50a8b1af0b040bf88ac" { assert.Equal(t, uint64(351), out.Satoshis) } } @@ -1107,7 +1107,7 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { // Test the right fee assert.Equal(t, uint64(206), CalculateFeeForTx(rawTx, nil, nil)) - assert.Equal(t, "01000000028ee20a442cdbcc9f9f927d9c2c9370e611675ebc24c064e8e94508ec8eca889e000000006b483045022100b79c96d45cb4bb9f2d1887ede3aced860c3d6d8fec169f710ec8a350d16004ad02205413acf2322aa9dbb5ea2a5166af8704ee354b941f4c04b60486e105c5c7ac344121034aaeabc056f33fd960d1e43fc8a0672723af02f275e54c31381af66a334634caffffffff42eaf7bdddc797a0beb97717ff8846f03c963fb5fe15a2b555b9cbd477b0254e000000006b483045022100f1cd918ebbaa7962d700c975cc793bf1351c4a064bf32685da40ac30194f13ea022062ad3e73b2cc96ed0e911f693d0dd844b6eff955f8d7e8f522594882a076f65c4121034aaeabc056f33fd960d1e43fc8a0672723af02f275e54c31381af66a334634caffffffff03a42b0000000000001976a914861c91132b67aec6d1bc111f13523cced19c9f2188ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000", rawTx.ToString()) + assert.Equal(t, "01000000028ee20a442cdbcc9f9f927d9c2c9370e611675ebc24c064e8e94508ec8eca889e000000006b483045022100b79c96d45cb4bb9f2d1887ede3aced860c3d6d8fec169f710ec8a350d16004ad02205413acf2322aa9dbb5ea2a5166af8704ee354b941f4c04b60486e105c5c7ac344121034aaeabc056f33fd960d1e43fc8a0672723af02f275e54c31381af66a334634caffffffff42eaf7bdddc797a0beb97717ff8846f03c963fb5fe15a2b555b9cbd477b0254e000000006b483045022100f1cd918ebbaa7962d700c975cc793bf1351c4a064bf32685da40ac30194f13ea022062ad3e73b2cc96ed0e911f693d0dd844b6eff955f8d7e8f522594882a076f65c4121034aaeabc056f33fd960d1e43fc8a0672723af02f275e54c31381af66a334634caffffffff03a42b0000000000001976a914861c91132b67aec6d1bc111f13523cced19c9f2188ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000", rawTx.String()) }) t.Run("send entire utxo amount", func(t *testing.T) { @@ -1139,9 +1139,9 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { assert.Equal(t, uint64(95), CalculateFeeForTx(rawTx, nil, nil)) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { + for _, out := range rawTx.Outputs { assert.Equal(t, uint64(904), out.Satoshis) - assert.Equal(t, "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac", out.GetLockingScriptHexString()) + assert.Equal(t, "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac", out.LockingScriptHexString()) } }) @@ -1186,8 +1186,8 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { assert.Equal(t, uint64(146), CalculateFeeForTx(rawTx, nil, nil)) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { - if out.GetLockingScriptHexString() == "76a9147dca96f8f7f4c4b400c46663b86a669bfb7e73c188ac" { + for _, out := range rawTx.Outputs { + if out.LockingScriptHexString() == "76a9147dca96f8f7f4c4b400c46663b86a669bfb7e73c188ac" { assert.Equal(t, uint64(103), out.Satoshis) } else { assert.Equal(t, uint64(250), out.Satoshis) @@ -1222,13 +1222,13 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { // Test the right fee assert.Equal(t, uint64(95), CalculateFeeForTx(rawTx, nil, nil)) - assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006a47304402206cb30774f7cd99db0a713ce164577ae94bbb3fde02f5e3b5afafe354458f4afc02204caa9804b352172d2b4260f5c99edb2091b6c4b5f9fbce12280f76d6371c9abc412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0188030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000", rawTx.ToString()) + assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006a47304402206cb30774f7cd99db0a713ce164577ae94bbb3fde02f5e3b5afafe354458f4afc02204caa9804b352172d2b4260f5c99edb2091b6c4b5f9fbce12280f76d6371c9abc412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0188030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000", rawTx.String()) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { - if out.GetLockingScriptHexString() == "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac" { + for _, out := range rawTx.Outputs { + if out.LockingScriptHexString() == "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac" { assert.Equal(t, uint64(904), out.Satoshis) - assert.Equal(t, "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac", out.GetLockingScriptHexString()) + assert.Equal(t, "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac", out.LockingScriptHexString()) } } }) @@ -1260,12 +1260,12 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { // Test the right fee assert.Equal(t, uint64(96), CalculateFeeForTx(rawTx, nil, nil)) - assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100b7311ea368f243bae53913c8fd17a3a5774703fbb49ac1191d3331a1b2ef031d0220059f392cbe75eaa6d43adf45a6822c86c06c50e819a2352e5596f53049470f77412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0187030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000", rawTx.ToString()) + assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100b7311ea368f243bae53913c8fd17a3a5774703fbb49ac1191d3331a1b2ef031d0220059f392cbe75eaa6d43adf45a6822c86c06c50e819a2352e5596f53049470f77412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0187030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000", rawTx.String()) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { + for _, out := range rawTx.Outputs { assert.Equal(t, uint64(903), out.Satoshis) - assert.Equal(t, "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac", out.GetLockingScriptHexString()) + assert.Equal(t, "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac", out.LockingScriptHexString()) } }) @@ -1324,7 +1324,7 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { assert.NotNil(t, rawTx) assert.Equal(t, uint64(132), CalculateFeeForTx(rawTx, nil, nil)) - assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100b8d6cf4362122a00fcc50195b6ecd75e08abf6dc427f1b75039c6355195a265202200c6eaf5c45c192b1fb1e6384b10d2f526d73edd996f03c43e48fe43b17e9869f412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0363030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000", rawTx.ToString()) + assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100b8d6cf4362122a00fcc50195b6ecd75e08abf6dc427f1b75039c6355195a265202200c6eaf5c45c192b1fb1e6384b10d2f526d73edd996f03c43e48fe43b17e9869f412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0363030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000", rawTx.String()) }) t.Run("send entire utxo amount - last pay-to does not cover fee", func(t *testing.T) { @@ -1358,11 +1358,11 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { // Test the right fee assert.Equal(t, uint64(113), CalculateFeeForTx(rawTx, nil, nil)) - assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100ed46aac84c052c4b64c42782e2d09d0ebf679ba23c99cc22922c78d1ba185bc102204814d6d3d470e6c9ab46b4270ff98962dabd9df853d3856e73943433739faddd412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0245030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac32000000000000001976a9147dca96f8f7f4c4b400c46663b86a669bfb7e73c188ac00000000", rawTx.ToString()) + assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100ed46aac84c052c4b64c42782e2d09d0ebf679ba23c99cc22922c78d1ba185bc102204814d6d3d470e6c9ab46b4270ff98962dabd9df853d3856e73943433739faddd412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0245030000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac32000000000000001976a9147dca96f8f7f4c4b400c46663b86a669bfb7e73c188ac00000000", rawTx.String()) // Test that we got the right amount of change (satoshis) - for _, out := range rawTx.GetOutputs() { - if out.GetLockingScriptHexString() == "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac" { + for _, out := range rawTx.Outputs { + if out.LockingScriptHexString() == "76a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac" { assert.Equal(t, uint64(837), out.Satoshis) } else { assert.Equal(t, uint64(50), out.Satoshis) @@ -1501,7 +1501,7 @@ func TestCreateTxWithChangeUsingWif(t *testing.T) { ) assert.NoError(t, err) assert.NotNil(t, rawTx) - assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100f1ab55c2f9fe4afb436ce18d097176eaca8a3d37a38966f606903fa1711f2bbe02202cedc7d854318c15e91fbdd779d427ef78e643e2dd9efa81ba5e245fad385ae9412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0201000000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac0000000000000000fd0107006a4dfc0630343363653730653536663866333763653565303066393932313134353934663234623435613439383234333231663963396665333039636333626334313132643534366538386364663935363934323738616161623932313737623065383161326434626334613166386531386432633238646165333662316635613332626538366631633737313931656635333464363661396533373634373265613132643932653762346530663432383734333130643438313839616439643163383462623966336437613834343032353635373434643537323166363736383662616135356133633262613238363565666661306339313165313536356661383964316466646661313766663432313830343335626431366235353037363734336432336461666335616639666565323061336364646562323862306464383666303538623863343966613032343961353032353333333165653731633434353532393232323265633366333334353062393563306465343265363862616565633762353039303836326334316131396539303363326363313964663833636133353034623131663365373134326230363365643439383232366130363434643630353234373936666662643136636664353061376365336463383161616634313739303335633436643062653030643230306231653930656134366239383630343566323437383461393932626233323364376662643964363934353636626135363630323966363561376564616236356164373162626666633439353330323139613562616139343562613539643365633133613038646162383836363336393466373333626639366536636130376335656663356536353362633062313232373738313262613664346631633831383338343134396237383962313535383339313661323137633162366264393731663632386337306536363036383065646338353330323936623633373366386462316433626632666436316339303937373535343566636331643132376433653034376436373430303735333938666637393161346562613334333031656661393238623661336163653836616633396563346138613762383834303463373634613830333864663831323330626536653032636562383830383136356230613036653530396665393462386136343461313462356537656536333966373335633031323562626336346135323430356466666232336637366534323131656132666238366539656164623564313330656533363339393863376531383537303937636462653866396337623031343339373034303238393266666533333364653664613833313930663665366161626332316261616366623039323465333639306462336438616332383232333264356265376335316632353430396365616466366135663166323731383434616266316665353935326166303661373039656632653361373232613534616363643032393162393236653263323964373939376332653535393839666133643661633261373263343762663230623064376430643537663063653335386564366464366465643634306430343831303862393264366261633736313733346139386466396165363639663064653130666364336634333162613064373838306537373364343863346564613935356236366137613766366666643065376431353161343535393964653131373338383537353465386161326337316161393435316134636465323935366635393864306462383031336162633364343162356163633161393963366665393032313861353635643535633831636136353561633165313936393739663662336165643761366236343335343563656664636664353766303361323464336565396538396132616363326564393233346236323862616566303366376335306439643265346464633230376133333737613931316664346231373034383362643831623632393538373031666466656231646263346234303564313434333762363065323530323533353034353235333162393064663935613231653963626463353039333933363339326238306436386662666530303636373036383537613963666536656561363934346332613534386234613062623335363066633866663033323864363836633864656461363836656463653631306562386161646430366662623463386135346438653933353963303530376637643233393062353133393837343139386636643636323531313233656661653636363036613939666164316338363736333035633666333539306265386639323433393432373636373134623539653636653462386163626364306339336539383862383838633832343361663530356535326530313339373239306237646236623737336336353934396266653030386231356535383200000000", rawTx.ToString()) + assert.Equal(t, "0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100f1ab55c2f9fe4afb436ce18d097176eaca8a3d37a38966f606903fa1711f2bbe02202cedc7d854318c15e91fbdd779d427ef78e643e2dd9efa81ba5e245fad385ae9412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff0201000000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac0000000000000000fd0107006a4dfc0630343363653730653536663866333763653565303066393932313134353934663234623435613439383234333231663963396665333039636333626334313132643534366538386364663935363934323738616161623932313737623065383161326434626334613166386531386432633238646165333662316635613332626538366631633737313931656635333464363661396533373634373265613132643932653762346530663432383734333130643438313839616439643163383462623966336437613834343032353635373434643537323166363736383662616135356133633262613238363565666661306339313165313536356661383964316466646661313766663432313830343335626431366235353037363734336432336461666335616639666565323061336364646562323862306464383666303538623863343966613032343961353032353333333165653731633434353532393232323265633366333334353062393563306465343265363862616565633762353039303836326334316131396539303363326363313964663833636133353034623131663365373134326230363365643439383232366130363434643630353234373936666662643136636664353061376365336463383161616634313739303335633436643062653030643230306231653930656134366239383630343566323437383461393932626233323364376662643964363934353636626135363630323966363561376564616236356164373162626666633439353330323139613562616139343562613539643365633133613038646162383836363336393466373333626639366536636130376335656663356536353362633062313232373738313262613664346631633831383338343134396237383962313535383339313661323137633162366264393731663632386337306536363036383065646338353330323936623633373366386462316433626632666436316339303937373535343566636331643132376433653034376436373430303735333938666637393161346562613334333031656661393238623661336163653836616633396563346138613762383834303463373634613830333864663831323330626536653032636562383830383136356230613036653530396665393462386136343461313462356537656536333966373335633031323562626336346135323430356466666232336637366534323131656132666238366539656164623564313330656533363339393863376531383537303937636462653866396337623031343339373034303238393266666533333364653664613833313930663665366161626332316261616366623039323465333639306462336438616332383232333264356265376335316632353430396365616466366135663166323731383434616266316665353935326166303661373039656632653361373232613534616363643032393162393236653263323964373939376332653535393839666133643661633261373263343762663230623064376430643537663063653335386564366464366465643634306430343831303862393264366261633736313733346139386466396165363639663064653130666364336634333162613064373838306537373364343863346564613935356236366137613766366666643065376431353161343535393964653131373338383537353465386161326337316161393435316134636465323935366635393864306462383031336162633364343162356163633161393963366665393032313861353635643535633831636136353561633165313936393739663662336165643761366236343335343563656664636664353766303361323464336565396538396132616363326564393233346236323862616566303366376335306439643265346464633230376133333737613931316664346231373034383362643831623632393538373031666466656231646263346234303564313434333762363065323530323533353034353235333162393064663935613231653963626463353039333933363339326238306436386662666530303636373036383537613963666536656561363934346332613534386234613062623335363066633866663033323864363836633864656461363836656463653631306562386161646430366662623463386135346438653933353963303530376637643233393062353133393837343139386636643636323531313233656661653636363036613939666164316338363736333035633666333539306265386639323433393432373636373134623539653636653462386163626364306339336539383862383838633832343361663530356535326530313339373239306237646236623737336336353934396266653030386231356535383200000000", rawTx.String()) }) t.Run("send entire utxo using data, too much data", func(t *testing.T) { @@ -1601,7 +1601,7 @@ func ExampleCreateTxWithChangeUsingWif() { return } - fmt.Printf("rawTx: %s", rawTx.ToString()) + fmt.Printf("rawTx: %s", rawTx.String()) // Output:rawTx: 0100000001760595866e99c1ce920197844740f5598b34763878696371d41b3a7c0a65b0b7000000006b483045022100c1dc4a4c5f26a404ff3618013dc63777d0790d0b0ea3371c67ee3f1bb5126c3e02206be8c841918215337f9b6a6a6040bd058596f2bab5c8b8cb27f849b1474b9e4c412102ea87d1fd77d169bd56a71e700628113d0f8dfe57faa0ba0e55a36f9ce8e10be3ffffffff04f4010000000000001976a9147a1980655efbfec416b2b0c663a7b3ac0b6a25d288ac5f010000000000001976a914c9d8699bdea34b131e737447b50a8b1af0b040bf88ac00000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000000000001c006a0770726566697832116d6f7265206578616d706c65206461746100000000 } diff --git a/verify.go b/verify.go index 4cfdfc4..fa83df7 100644 --- a/verify.go +++ b/verify.go @@ -6,10 +6,10 @@ import ( "encoding/hex" "fmt" - "github.com/bitcoinsv/bsvd/bsvec" "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 ( @@ -18,7 +18,7 @@ const ( ) // PubKeyFromSignature gets a publickey for a signature and tells you whether is was compressed -func PubKeyFromSignature(sig, data string) (pubKey *bsvec.PublicKey, wasCompressed bool, err error) { +func PubKeyFromSignature(sig, data string) (pubKey *bec.PublicKey, wasCompressed bool, err error) { var decodedSig []byte if decodedSig, err = base64.StdEncoding.DecodeString(sig); err != nil { @@ -37,7 +37,7 @@ func PubKeyFromSignature(sig, data string) (pubKey *bsvec.PublicKey, wasCompress // Create the hash expectedMessageHash := chainhash.DoubleHashB(buf.Bytes()) - return bsvec.RecoverCompact(bsvec.S256(), decodedSig, expectedMessageHash) + return bec.RecoverCompact(bec.S256(), decodedSig, expectedMessageHash) } // VerifyMessage verifies a string and address against the provided @@ -57,20 +57,20 @@ func VerifyMessage(address, sig, data string) error { } // Get the address - var bsvecAddress *bsvutil.LegacyAddressPubKeyHash - if bsvecAddress, 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 bsvecAddress.String() == address { + if bscriptAddress.AddressString == address { return nil } return fmt.Errorf( "address (%s) not found - compressed: %t\n%s was found instead", address, wasCompressed, - bsvecAddress.EncodeAddress(), + bscriptAddress.AddressString, ) } @@ -90,8 +90,8 @@ func VerifyMessageDER(hash [32]byte, pubKey string, signature string) (verified } // Parse the signature - var sig *bsvec.Signature - if sig, err = bsvec.ParseDERSignature(sigBytes, bsvec.S256()); err != nil { + var sig *bec.Signature + if sig, err = bec.ParseDERSignature(sigBytes, bec.S256()); err != nil { return } @@ -102,8 +102,8 @@ func VerifyMessageDER(hash [32]byte, pubKey string, signature string) (verified } // Parse the pubKey - var rawPubKey *bsvec.PublicKey - if rawPubKey, err = bsvec.ParsePubKey(pubKeyBytes, bsvec.S256()); err != nil { + var rawPubKey *bec.PublicKey + if rawPubKey, err = bec.ParsePubKey(pubKeyBytes, bec.S256()); err != nil { return }