Skip to content

Commit

Permalink
Merge pull request #330 from aergoio/topic/update-btcec
Browse files Browse the repository at this point in the history
update btcec
  • Loading branch information
kroggen authored Jan 18, 2025
2 parents c79cd74 + 2c7cdb4 commit b64c86f
Show file tree
Hide file tree
Showing 25 changed files with 81 additions and 107 deletions.
10 changes: 5 additions & 5 deletions account/key/aergo_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"

crypto "github.com/aergoio/aergo/v2/account/key/crypto"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -24,12 +24,12 @@ func TestSaveAndLoadOnAergo(t *testing.T) {
if nil != err {
assert.FailNow(t, "Could not create storage", err)
}
expected, err := btcec.NewPrivateKey(btcec.S256())
expected, err := btcec.NewPrivateKey()
if nil != err {
assert.FailNow(t, "Could not create private key", err)
}

identity := crypto.GenerateAddress(&expected.PublicKey)
identity := crypto.GenerateAddress(expected.PubKey().ToECDSA())
password := "password"
saved, err := storage.Save(identity, password, expected)
if nil != err {
Expand All @@ -52,12 +52,12 @@ func TestSaveAndListOnAergo(t *testing.T) {
if nil != err {
assert.FailNow(t, "Could not create storage", err)
}
expected, err := btcec.NewPrivateKey(btcec.S256())
expected, err := btcec.NewPrivateKey()
if nil != err {
assert.FailNow(t, "Could not create private key", err)
}

identity := crypto.GenerateAddress(&expected.PublicKey)
identity := crypto.GenerateAddress(expected.PubKey().ToECDSA())
password := "password"
saved, err := storage.Save(identity, password, expected)
if nil != err {
Expand Down
4 changes: 2 additions & 2 deletions account/key/badgerdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)

type BadgerStorage struct {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (ks *BadgerStorage) Load(identity Identity, password string) (*PrivateKey,
return nil, types.ErrWrongAddressOrPassWord
}

privateKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), decrypted)
privateKey, _ := btcec.PrivKeyFromBytes(decrypted)
return privateKey, nil
}

Expand Down
10 changes: 5 additions & 5 deletions account/key/badgerdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"

crypto "github.com/aergoio/aergo/v2/account/key/crypto"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,12 +21,12 @@ func TestSaveAndLoadOnBadger(t *testing.T) {
defer os.RemoveAll(dir)

storage, _ := NewBadgerStorage(dir)
expected, err := btcec.NewPrivateKey(btcec.S256())
expected, err := btcec.NewPrivateKey()
if nil != err {
assert.FailNow(t, "Could not create private key", err)
}

identity := crypto.GenerateAddress(&expected.PublicKey)
identity := crypto.GenerateAddress(expected.PubKey().ToECDSA())
password := "password"
saved, err := storage.Save(identity, password, expected)
if nil != err {
Expand All @@ -47,12 +47,12 @@ func TestSaveAndListOnBadger(t *testing.T) {
defer os.RemoveAll(dir)

storage, _ := NewBadgerStorage(dir)
expected, err := btcec.NewPrivateKey(btcec.S256())
expected, err := btcec.NewPrivateKey()
if nil != err {
assert.FailNow(t, "Could not create private key", err)
}

identity := crypto.GenerateAddress(&expected.PublicKey)
identity := crypto.GenerateAddress(expected.PubKey().ToECDSA())
password := "password"
saved, err := storage.Save(identity, password, expected)
if nil != err {
Expand Down
6 changes: 3 additions & 3 deletions account/key/crypto/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"testing"

"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/assert"
)

func TestGenerateAddress(t *testing.T) {
for i := 0; i < 100; i++ {
key, err := btcec.NewPrivateKey(btcec.S256())
key, err := btcec.NewPrivateKey()
assert.NoError(t, err, "could not create private key")

address := GenerateAddress(&key.PublicKey)
address := GenerateAddress(key.PubKey().ToECDSA())
assert.Equalf(t, types.AddressLength, len(address), "wrong address length : %s", address)
assert.Equal(t, key.PubKey().SerializeCompressed(), address, "wrong address contents")
}
Expand Down
2 changes: 1 addition & 1 deletion account/key/crypto/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package key

import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)

type PrivateKey = btcec.PrivateKey
Expand Down
4 changes: 2 additions & 2 deletions account/key/crypto/v1strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/aergoio/aergo/v2/internal/enc/hex"
"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"golang.org/x/crypto/scrypt"
)

Expand Down Expand Up @@ -179,7 +179,7 @@ func (ks *v1Strategy) Decrypt(encrypted []byte, passphrase string) (*PrivateKey,
return nil, err
}

privateKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), plaintext)
privateKey, _ := btcec.PrivKeyFromBytes(plaintext)

rawAddress := GenerateAddress(&(privateKey.ToECDSA().PublicKey))
encodedAddress := types.EncodeAddress(rawAddress)
Expand Down
4 changes: 2 additions & 2 deletions account/key/crypto/v1strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"testing"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -34,7 +34,7 @@ func TestEncryptAndDecrypt(t *testing.T) {
defer os.RemoveAll(dir)

for i := 0; i < 2; i++ {
expected, err := btcec.NewPrivateKey(btcec.S256())
expected, err := btcec.NewPrivateKey()
if nil != err {
assert.FailNow(t, "Could not create private key", err)
}
Expand Down
17 changes: 6 additions & 11 deletions account/key/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"encoding/binary"

"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
sha256 "github.com/minio/sha256-simd"
)

Expand All @@ -19,20 +20,14 @@ func (ks *Store) Sign(addr Identity, pass string, hash []byte) ([]byte, error) {
if err != nil {
return nil, err
}
sign, err := key.Sign(hash)
if err != nil {
return nil, err
}
sign := ecdsa.Sign(key, hash)
return sign.Serialize(), nil
}

// SignTx return tx signature using stored key
func SignTx(tx *types.Tx, key *aergokey) error {
hash := CalculateHashWithoutSign(tx.Body)
sign, err := key.Sign(hash)
if err != nil {
return err
}
sign := ecdsa.Sign(key, hash)
tx.Body.Sign = sign.Serialize()
tx.Hash = tx.CalculateTxHash()
return nil
Expand All @@ -59,11 +54,11 @@ func VerifyTx(tx *types.Tx) error {
func VerifyTxWithAddress(tx *types.Tx, address []byte) error {
txBody := tx.Body
hash := CalculateHashWithoutSign(txBody)
sign, err := btcec.ParseSignature(txBody.Sign, btcec.S256())
sign, err := ecdsa.ParseSignature(txBody.Sign)
if err != nil {
return err
}
pubkey, err := btcec.ParsePubKey(address, btcec.S256())
pubkey, err := btcec.ParsePubKey(address)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion account/key/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package key

import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)

// Identity is a raw, i.e. decoded address generated from a public key
Expand Down
10 changes: 5 additions & 5 deletions account/key/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

crypto "github.com/aergoio/aergo/v2/account/key/crypto"
"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)

type aergokey = btcec.PrivateKey
Expand Down Expand Up @@ -61,7 +61,7 @@ func (ks *Store) CloseStore() {
// CreateKey make new key in keystore and return it's address
func (ks *Store) CreateKey(pass string) (Identity, error) {
//gen new key
privkey, err := btcec.NewPrivateKey(btcec.S256())
privkey, err := btcec.NewPrivateKey()
if err != nil {
return nil, err
}
Expand All @@ -76,10 +76,10 @@ func (ks *Store) ImportKey(imported []byte, oldpass string, newpass string) (Ide
if err != nil {
return nil, err
}
privkey, _ := btcec.PrivKeyFromBytes(btcec.S256(), key)
privkey, _ := btcec.PrivKeyFromBytes(key)
idendity, err := ks.addKey(privkey, newpass)
if err != nil {
address := crypto.GenerateAddress(&privkey.PublicKey)
address := crypto.GenerateAddress(privkey.PubKey().ToECDSA())
return address, err
}
return idendity, nil
Expand Down Expand Up @@ -164,7 +164,7 @@ func (ks *Store) GetKey(address []byte, pass string) (*aergokey, error) {
}

func (ks *Store) addKey(key *btcec.PrivateKey, pass string) (Identity, error) {
address := crypto.GenerateAddress(&key.PublicKey)
address := crypto.GenerateAddress(key.PubKey().ToECDSA())
return ks.storage.Save(address, pass, key)
}

Expand Down
6 changes: 3 additions & 3 deletions account/key/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

crypto "github.com/aergoio/aergo/v2/account/key/crypto"
"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -67,8 +67,8 @@ func TestImportKey(t *testing.T) {
defer deinitTest()
const testSize = 3
for i := 0; i < testSize; i++ {
key, err := btcec.NewPrivateKey(btcec.S256())
addr := crypto.GenerateAddress(&(key.PublicKey))
key, err := btcec.NewPrivateKey()
addr := crypto.GenerateAddress(key.PubKey().ToECDSA())
if err != nil {
t.Errorf("could not create key : %s", err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions chain/signverifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/aergoio/aergo/v2/account/key"
crypto "github.com/aergoio/aergo/v2/account/key/crypto"
"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -41,12 +41,12 @@ func beforeTest(txCount int) error {
}

for i := 0; i < maxAccount; i++ {
privkey, err := btcec.NewPrivateKey(btcec.S256())
privkey, err := btcec.NewPrivateKey()
if err != nil {
return err
}
//gen new address
accs[i] = crypto.GenerateAddress(&privkey.PublicKey)
accs[i] = crypto.GenerateAddress(privkey.PubKey().ToECDSA())
sign[i] = privkey
recipient[i] = _itobU32(uint32(i))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/aergocli/cmd/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/aergoio/aergo/v2/internal/enc/base64"
"github.com/aergoio/aergo/v2/p2p/p2putil"
"github.com/aergoio/aergo/v2/types"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -137,7 +137,7 @@ func saveFilesFromKeys(priv crypto.PrivKey, pub crypto.PubKey, prefix string) er
if err != nil {
return err
}
_, pubkey := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)
_, pubkey := btcec.PrivKeyFromBytes(pkBytes)
address := keycrypto.GenerateAddress(pubkey.ToECDSA())
addrf.WriteString(types.EncodeAddress(address))
addrf.Sync()
Expand Down
4 changes: 2 additions & 2 deletions cmd/aergocli/cmd/signtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/aergoio/aergo/v2/internal/enc/base58"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/jsonrpc"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ var signCmd = &cobra.Command{
return
}
tx := &types.Tx{Body: param}
signKey, pubkey := btcec.PrivKeyFromBytes(btcec.S256(), rawKey)
signKey, pubkey := btcec.PrivKeyFromBytes(rawKey)
err = key.SignTx(tx, signKey)
if err != nil {
cmd.Printf("Failed: %s\n", err.Error())
Expand Down
9 changes: 5 additions & 4 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import (
"github.com/aergoio/aergo/v2/state/statedb"
"github.com/aergoio/aergo/v2/types"
"github.com/aergoio/aergo/v2/types/dbkey"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
)

var (
Expand Down Expand Up @@ -862,7 +863,7 @@ func luaECVerify(L *LState, service C.int, msg *C.char, sig *C.char, addr *C.cha
if err != nil {
return -1, C.CString("[Contract.LuaEcVerify] invalid aergo address: " + err.Error())
}
pubKey, err = btcec.ParsePubKey(bAddress, btcec.S256())
pubKey, err = btcec.ParsePubKey(bAddress)
if err != nil {
return -1, C.CString("[Contract.LuaEcVerify] error parsing pubKey: " + err.Error())
}
Expand All @@ -877,7 +878,7 @@ func luaECVerify(L *LState, service C.int, msg *C.char, sig *C.char, addr *C.cha
copy(btcsig[1:], bSig)
bSig = btcsig
}
pub, _, err := btcec.RecoverCompact(btcec.S256(), bSig, bMsg)
pub, _, err := ecdsa.RecoverCompact(bSig, bMsg)
if err != nil {
return -1, C.CString("[Contract.LuaEcVerify] error recoverCompact: " + err.Error())
}
Expand All @@ -895,7 +896,7 @@ func luaECVerify(L *LState, service C.int, msg *C.char, sig *C.char, addr *C.cha
verifyResult = bytes.Equal(bAddress, signAddress)
}
} else {
sign, err := btcec.ParseSignature(bSig, btcec.S256())
sign, err := ecdsa.ParseSignature(bSig)
if err != nil {
return -1, C.CString("[Contract.LuaEcVerify] error parsing signature: " + err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/aergoio/etcd v0.0.0-20190429013412-e8b3f96f6399
github.com/anaskhan96/base58check v0.0.0-20181220122047-b05365d494c4
github.com/bluele/gcache v0.0.0-20190518031135-bc40bd653833
github.com/btcsuite/btcd v0.21.0-beta
github.com/btcsuite/btcd/btcec/v2 v2.3.4
github.com/c-bata/go-prompt v0.2.3
github.com/coreos/go-semver v0.3.0
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
Expand Down
Loading

0 comments on commit b64c86f

Please sign in to comment.