Skip to content

Commit

Permalink
Migrated to go-bt/v2, changed all bsvec keys to bec keys
Browse files Browse the repository at this point in the history
  • Loading branch information
icellan committed Feb 3, 2022
1 parent 6f64f4e commit 00eb4b4
Show file tree
Hide file tree
Showing 27 changed files with 299 additions and 247 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ 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"
)

// A25 is a type for a 25 byte (not base58 encoded) bitcoin address.
Expand Down Expand Up @@ -89,8 +89,8 @@ 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
Expand All @@ -111,21 +111,21 @@ func GetAddressFromPrivateKeyString(privateKey string, compressed bool) (string,
return address.EncodeAddress(), nil
}

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

return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(serializedPublicKey), &chaincfg.MainNetParams)
return bsvutil.NewLegacyAddressPubKeyHash(bsvutil.Hash160(SerialisedPublicKey), &chaincfg.MainNetParams)
}

// GetAddressFromPubKeyString is a convenience function to use a hex string pubKey
Expand Down
12 changes: 6 additions & 6 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/go-bk/bec"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -131,7 +131,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
Expand All @@ -144,18 +144,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 {
Expand Down
26 changes: 13 additions & 13 deletions encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -46,7 +46,7 @@ func EncryptWithPrivateKeyString(privateKey, data string) (string, error) {
return "", err
}

// Encrypt using bsvec
// Encrypt using bec
return EncryptWithPrivateKey(rawPrivateKey, data)
}

Expand All @@ -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
}
14 changes: 7 additions & 7 deletions encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/go-bk/bec"
"github.com/stretchr/testify/assert"
)

Expand All @@ -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
}{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/calculate_fee_for_tx/calculate_fee_for_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions examples/create_tx/create_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion examples/create_tx_using_wif/create_tx_using_wif.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func main() {
}

// Success!
log.Printf("rawTx: %s", rawTx.ToString())
log.Printf("rawTx: %s", rawTx.String())
}
2 changes: 1 addition & 1 deletion examples/create_tx_with_change/create_tx_with_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ func main() {
}

// Success!
log.Printf("rawTx: %s", rawTx.ToString())
log.Printf("rawTx: %s", rawTx.String())
}
4 changes: 2 additions & 2 deletions examples/encrypt_shared_keys/encrypt_shared_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions examples/get_private_key_for_path/get_private_key_for_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
6 changes: 3 additions & 3 deletions examples/get_public_keys_for_path/get_public_keys_for_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()))
}
}
}
2 changes: 1 addition & 1 deletion examples/tx_from_hex/tx_from_hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func main() {
return
}

log.Printf("tx id: %s", rawTx.GetTxID())
log.Printf("tx id: %s", rawTx.TxID())
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,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
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
Expand Down
Loading

0 comments on commit 00eb4b4

Please sign in to comment.