Update for recent btcec API changes.

This change is introduced so that btcwallet will continue to compile after
issue 6 in btcec is merged.
This commit is contained in:
Jimmy Song 2014-09-27 13:59:18 -05:00 committed by Dave Collins
parent a9cf4a2bf5
commit 8d928ce2e1
3 changed files with 28 additions and 30 deletions

View file

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/ecdsa"
"crypto/rand" "crypto/rand"
"crypto/sha512" "crypto/sha512"
"encoding/binary" "encoding/binary"
@ -2139,7 +2138,7 @@ type PubKeyAddress interface {
// PrivKey returns the private key for the address. // PrivKey returns the private key for the address.
// It can fail if the key store is watching only, the key store is locked, // It can fail if the key store is watching only, the key store is locked,
// or the address doesn't have any keys. // or the address doesn't have any keys.
PrivKey() (*ecdsa.PrivateKey, error) PrivKey() (*btcec.PrivateKey, error)
// ExportPrivKey exports the WIF private key. // ExportPrivKey exports the WIF private key.
ExportPrivKey() (*btcutil.WIF, error) ExportPrivKey() (*btcutil.WIF, error)
} }
@ -2252,20 +2251,20 @@ func (a *btcAddress) verifyKeypairs() error {
return errors.New("private key unavailable") return errors.New("private key unavailable")
} }
privkey := &ecdsa.PrivateKey{ privKey := &btcec.PrivateKey{
PublicKey: *a.pubKey.ToECDSA(), PublicKey: *a.pubKey.ToECDSA(),
D: new(big.Int).SetBytes(a.privKeyCT), D: new(big.Int).SetBytes(a.privKeyCT),
} }
data := "String to sign." data := "String to sign."
r, s, err := ecdsa.Sign(rand.Reader, privkey, []byte(data)) sig, err := privKey.Sign([]byte(data))
if err != nil { if err != nil {
return err return err
} }
ok := ecdsa.Verify(&privkey.PublicKey, []byte(data), r, s) ok := sig.Verify([]byte(data), privKey.PubKey())
if !ok { if !ok {
return errors.New("ecdsa verification failed") return errors.New("pubkey verification failed")
} }
return nil return nil
} }
@ -2568,7 +2567,7 @@ func (a *btcAddress) ExportPubKey() string {
// PrivKey implements PubKeyAddress by returning the private key, or an error // 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. // 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 { if a.store.flags.watchingOnly {
return nil, ErrWatchingOnly return nil, ErrWatchingOnly
} }
@ -2590,7 +2589,7 @@ func (a *btcAddress) PrivKey() (*ecdsa.PrivateKey, error) {
return nil, err return nil, err
} }
return &ecdsa.PrivateKey{ return &btcec.PrivateKey{
PublicKey: *a.pubKey.ToECDSA(), PublicKey: *a.pubKey.ToECDSA(),
D: new(big.Int).SetBytes(privKeyCT), D: new(big.Int).SetBytes(privKeyCT),
}, nil }, nil

View file

@ -18,7 +18,6 @@ package keystore
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/rand" "crypto/rand"
"math/big" "math/big"
"reflect" "reflect"
@ -301,36 +300,36 @@ func TestChaining(t *testing.T) {
t.Errorf("%s: Unable to parse next compressed pubkey: %v", test.name, err) t.Errorf("%s: Unable to parse next compressed pubkey: %v", test.name, err)
return return
} }
privkeyUncompressed := &ecdsa.PrivateKey{ privkeyUncompressed := &btcec.PrivateKey{
PublicKey: *pubkeyUncompressed.ToECDSA(), PublicKey: *pubkeyUncompressed.ToECDSA(),
D: new(big.Int).SetBytes(nextPrivUncompressed), D: new(big.Int).SetBytes(nextPrivUncompressed),
} }
privkeyCompressed := &ecdsa.PrivateKey{ privkeyCompressed := &btcec.PrivateKey{
PublicKey: *pubkeyCompressed.ToECDSA(), PublicKey: *pubkeyCompressed.ToECDSA(),
D: new(big.Int).SetBytes(nextPrivCompressed), D: new(big.Int).SetBytes(nextPrivCompressed),
} }
data := "String to sign." data := "String to sign."
r, s, err := ecdsa.Sign(rand.Reader, privkeyUncompressed, []byte(data)) sig, err := privkeyUncompressed.Sign([]byte(data))
if err != nil { if err != nil {
t.Errorf("%s: Unable to sign data with next private key (chained from uncompressed pubkey): %v", t.Errorf("%s: Unable to sign data with next private key (chained from uncompressed pubkey): %v",
test.name, err) test.name, err)
return return
} }
ok := ecdsa.Verify(&privkeyUncompressed.PublicKey, []byte(data), r, s) ok := sig.Verify([]byte(data), privkeyUncompressed.PubKey())
if !ok { 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) test.name)
return return
} }
r, s, err = ecdsa.Sign(rand.Reader, privkeyCompressed, []byte(data)) sig, err = privkeyCompressed.Sign([]byte(data))
if err != nil { if err != nil {
t.Errorf("%s: Unable to sign data with next private key (chained from compressed pubkey): %v", t.Errorf("%s: Unable to sign data with next private key (chained from compressed pubkey): %v",
test.name, err) test.name, err)
return return
} }
ok = ecdsa.Verify(&privkeyCompressed.PublicKey, []byte(data), r, s) ok = sig.Verify([]byte(data), privkeyCompressed.PubKey())
if !ok { 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) test.name)
return return
} }
@ -440,15 +439,15 @@ func TestWalletPubkeyChaining(t *testing.T) {
// Sign some data with the private key, then verify signature with the pubkey. // Sign some data with the private key, then verify signature with the pubkey.
hash := []byte("hash to sign") hash := []byte("hash to sign")
r, s, err := ecdsa.Sign(rand.Reader, key1, hash) sig, err := key1.Sign(hash)
if err != nil { if err != nil {
t.Errorf("Unable to sign hash with the created private key: %v", err) t.Errorf("Unable to sign hash with the created private key: %v", err)
return return
} }
pubKey := pkinfo.PubKey() pubKey := pkinfo.PubKey()
ok := ecdsa.Verify(pubKey.ToECDSA(), hash, r, s) ok := sig.Verify(hash, pubKey)
if !ok { 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 return
} }
@ -470,17 +469,17 @@ func TestWalletPubkeyChaining(t *testing.T) {
return 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. // 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 { if err != nil {
t.Errorf("Unable to sign hash with the created private key: %v", err) t.Errorf("Unable to sign hash with the created private key: %v", err)
return return
} }
pubKey = nextPkInfo.PubKey() pubKey = nextPkInfo.PubKey()
ok = ecdsa.Verify(pubKey.ToECDSA(), hash, r, s) ok = sig.Verify(hash, pubKey)
if !ok { 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 return
} }
@ -703,7 +702,7 @@ func TestImportPrivateKey(t *testing.T) {
return return
} }
pk, err := ecdsa.GenerateKey(btcec.S256(), rand.Reader) pk, err := btcec.NewPrivateKey(btcec.S256())
if err != nil { if err != nil {
t.Error("Error generating private key: " + err.Error()) t.Error("Error generating private key: " + err.Error())
return return

View file

@ -18,7 +18,6 @@ package main
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/sha256" "crypto/sha256"
"crypto/subtle" "crypto/subtle"
"crypto/tls" "crypto/tls"
@ -2418,13 +2417,14 @@ func SignMessage(w *Wallet, chainSvr *chain.Client, icmd btcjson.Cmd) (interface
} }
pka := ainfo.(keystore.PubKeyAddress) pka := ainfo.(keystore.PubKeyAddress)
privkey, err := pka.PrivKey() tmp, err := pka.PrivKey()
if err != nil { if err != nil {
return nil, err return nil, err
} }
privKey := (*btcec.PrivateKey)(tmp)
fullmsg := "Bitcoin Signed Message:\n" + cmd.Message 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()) btcwire.DoubleSha256([]byte(fullmsg)), ainfo.Compressed())
if err != nil { if err != nil {
return nil, err 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 // Set up our callbacks that we pass to btcscript so it can
// look up the appropriate keys and scripts by address. // look up the appropriate keys and scripts by address.
getKey := btcscript.KeyClosure(func(addr btcutil.Address) ( getKey := btcscript.KeyClosure(func(addr btcutil.Address) (
*ecdsa.PrivateKey, bool, error) { *btcec.PrivateKey, bool, error) {
if len(keys) != 0 { if len(keys) != 0 {
wif, ok := keys[addr.EncodeAddress()] wif, ok := keys[addr.EncodeAddress()]
if !ok { if !ok {
return nil, false, return nil, false,
errors.New("no key for address") 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) address, err := w.KeyStore.Address(addr)
if err != nil { if err != nil {