diff --git a/keystore/keystore.go b/keystore/keystore.go index f6da3c9..6459b66 100644 --- a/keystore/keystore.go +++ b/keystore/keystore.go @@ -20,7 +20,6 @@ import ( "bytes" "crypto/aes" "crypto/cipher" - "crypto/ecdsa" "crypto/rand" "crypto/sha512" "encoding/binary" @@ -2139,7 +2138,7 @@ type PubKeyAddress interface { // PrivKey returns the private key for the address. // It can fail if the key store is watching only, the key store is locked, // or the address doesn't have any keys. - PrivKey() (*ecdsa.PrivateKey, error) + PrivKey() (*btcec.PrivateKey, error) // ExportPrivKey exports the WIF private key. ExportPrivKey() (*btcutil.WIF, error) } @@ -2252,20 +2251,20 @@ func (a *btcAddress) verifyKeypairs() error { return errors.New("private key unavailable") } - privkey := &ecdsa.PrivateKey{ + privKey := &btcec.PrivateKey{ PublicKey: *a.pubKey.ToECDSA(), D: new(big.Int).SetBytes(a.privKeyCT), } data := "String to sign." - r, s, err := ecdsa.Sign(rand.Reader, privkey, []byte(data)) + sig, err := privKey.Sign([]byte(data)) if err != nil { return err } - ok := ecdsa.Verify(&privkey.PublicKey, []byte(data), r, s) + ok := sig.Verify([]byte(data), privKey.PubKey()) if !ok { - return errors.New("ecdsa verification failed") + return errors.New("pubkey verification failed") } return nil } @@ -2568,7 +2567,7 @@ func (a *btcAddress) ExportPubKey() string { // PrivKey implements PubKeyAddress by returning the private key, or an error // if the key store is locked, watching only or the private key is missing. -func (a *btcAddress) PrivKey() (*ecdsa.PrivateKey, error) { +func (a *btcAddress) PrivKey() (*btcec.PrivateKey, error) { if a.store.flags.watchingOnly { return nil, ErrWatchingOnly } @@ -2590,7 +2589,7 @@ func (a *btcAddress) PrivKey() (*ecdsa.PrivateKey, error) { return nil, err } - return &ecdsa.PrivateKey{ + return &btcec.PrivateKey{ PublicKey: *a.pubKey.ToECDSA(), D: new(big.Int).SetBytes(privKeyCT), }, nil diff --git a/keystore/keystore_test.go b/keystore/keystore_test.go index cf48d77..658daa4 100644 --- a/keystore/keystore_test.go +++ b/keystore/keystore_test.go @@ -18,7 +18,6 @@ package keystore import ( "bytes" - "crypto/ecdsa" "crypto/rand" "math/big" "reflect" @@ -301,36 +300,36 @@ func TestChaining(t *testing.T) { t.Errorf("%s: Unable to parse next compressed pubkey: %v", test.name, err) return } - privkeyUncompressed := &ecdsa.PrivateKey{ + privkeyUncompressed := &btcec.PrivateKey{ PublicKey: *pubkeyUncompressed.ToECDSA(), D: new(big.Int).SetBytes(nextPrivUncompressed), } - privkeyCompressed := &ecdsa.PrivateKey{ + privkeyCompressed := &btcec.PrivateKey{ PublicKey: *pubkeyCompressed.ToECDSA(), D: new(big.Int).SetBytes(nextPrivCompressed), } data := "String to sign." - r, s, err := ecdsa.Sign(rand.Reader, privkeyUncompressed, []byte(data)) + sig, err := privkeyUncompressed.Sign([]byte(data)) if err != nil { t.Errorf("%s: Unable to sign data with next private key (chained from uncompressed pubkey): %v", test.name, err) return } - ok := ecdsa.Verify(&privkeyUncompressed.PublicKey, []byte(data), r, s) + ok := sig.Verify([]byte(data), privkeyUncompressed.PubKey()) if !ok { - t.Errorf("%s: ecdsa verification failed for next keypair (chained from uncompressed pubkey).", + t.Errorf("%s: btcec signature verification failed for next keypair (chained from uncompressed pubkey).", test.name) return } - r, s, err = ecdsa.Sign(rand.Reader, privkeyCompressed, []byte(data)) + sig, err = privkeyCompressed.Sign([]byte(data)) if err != nil { t.Errorf("%s: Unable to sign data with next private key (chained from compressed pubkey): %v", test.name, err) return } - ok = ecdsa.Verify(&privkeyCompressed.PublicKey, []byte(data), r, s) + ok = sig.Verify([]byte(data), privkeyCompressed.PubKey()) if !ok { - t.Errorf("%s: ecdsa verification failed for next keypair (chained from compressed pubkey).", + t.Errorf("%s: btcec signature verification failed for next keypair (chained from compressed pubkey).", test.name) return } @@ -440,15 +439,15 @@ func TestWalletPubkeyChaining(t *testing.T) { // Sign some data with the private key, then verify signature with the pubkey. hash := []byte("hash to sign") - r, s, err := ecdsa.Sign(rand.Reader, key1, hash) + sig, err := key1.Sign(hash) if err != nil { t.Errorf("Unable to sign hash with the created private key: %v", err) return } pubKey := pkinfo.PubKey() - ok := ecdsa.Verify(pubKey.ToECDSA(), hash, r, s) + ok := sig.Verify(hash, pubKey) if !ok { - t.Errorf("ECDSA verification failed; address's pubkey mismatches the privkey.") + t.Errorf("btcec signature verification failed; address's pubkey mismatches the privkey.") return } @@ -470,17 +469,17 @@ func TestWalletPubkeyChaining(t *testing.T) { return } - // Do an ECDSA signature check here as well, this time for the next + // Do a signature check here as well, this time for the next // address after the one made without the private key. - r, s, err = ecdsa.Sign(rand.Reader, nextKey, hash) + sig, err = nextKey.Sign(hash) if err != nil { t.Errorf("Unable to sign hash with the created private key: %v", err) return } pubKey = nextPkInfo.PubKey() - ok = ecdsa.Verify(pubKey.ToECDSA(), hash, r, s) + ok = sig.Verify(hash, pubKey) if !ok { - t.Errorf("ECDSA verification failed; next address's keypair does not match.") + t.Errorf("btcec signature verification failed; next address's keypair does not match.") return } @@ -703,7 +702,7 @@ func TestImportPrivateKey(t *testing.T) { return } - pk, err := ecdsa.GenerateKey(btcec.S256(), rand.Reader) + pk, err := btcec.NewPrivateKey(btcec.S256()) if err != nil { t.Error("Error generating private key: " + err.Error()) return diff --git a/rpcserver.go b/rpcserver.go index 2f44ead..5a0666d 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -18,7 +18,6 @@ package main import ( "bytes" - "crypto/ecdsa" "crypto/sha256" "crypto/subtle" "crypto/tls" @@ -2418,13 +2417,14 @@ func SignMessage(w *Wallet, chainSvr *chain.Client, icmd btcjson.Cmd) (interface } pka := ainfo.(keystore.PubKeyAddress) - privkey, err := pka.PrivKey() + tmp, err := pka.PrivKey() if err != nil { return nil, err } + privKey := (*btcec.PrivateKey)(tmp) fullmsg := "Bitcoin Signed Message:\n" + cmd.Message - sigbytes, err := btcec.SignCompact(btcec.S256(), privkey, + sigbytes, err := btcec.SignCompact(btcec.S256(), privKey, btcwire.DoubleSha256([]byte(fullmsg)), ainfo.Compressed()) if err != nil { return nil, err @@ -2684,14 +2684,14 @@ func SignRawTransaction(w *Wallet, chainSvr *chain.Client, icmd btcjson.Cmd) (in // Set up our callbacks that we pass to btcscript so it can // look up the appropriate keys and scripts by address. getKey := btcscript.KeyClosure(func(addr btcutil.Address) ( - *ecdsa.PrivateKey, bool, error) { + *btcec.PrivateKey, bool, error) { if len(keys) != 0 { wif, ok := keys[addr.EncodeAddress()] if !ok { return nil, false, errors.New("no key for address") } - return wif.PrivKey.ToECDSA(), wif.CompressPubKey, nil + return wif.PrivKey, wif.CompressPubKey, nil } address, err := w.KeyStore.Address(addr) if err != nil {