Use btcec consts for serialized pubkey lengths.

This commit is contained in:
Josh Rickmar 2014-05-20 08:12:43 -05:00
parent c9b476e940
commit 14a9653d73

View file

@ -187,8 +187,11 @@ func ChainedPrivKey(privkey, pubkey, chaincode []byte) ([]byte, error) {
return nil, fmt.Errorf("invalid chaincode length %d (must be 32)", return nil, fmt.Errorf("invalid chaincode length %d (must be 32)",
len(chaincode)) len(chaincode))
} }
if !(len(pubkey) == 65 || len(pubkey) == 33) { switch n := len(pubkey); n {
return nil, fmt.Errorf("invalid pubkey length %d", len(pubkey)) case btcec.PubKeyBytesLenUncompressed, btcec.PubKeyBytesLenCompressed:
// Correct length
default:
return nil, fmt.Errorf("invalid pubkey length %d", n)
} }
xorbytes := make([]byte, 32) xorbytes := make([]byte, 32)
@ -208,8 +211,15 @@ func ChainedPrivKey(privkey, pubkey, chaincode []byte) ([]byte, error) {
// previous public key and chaincode. pubkey must be 33 or 65 bytes, and // previous public key and chaincode. pubkey must be 33 or 65 bytes, and
// chaincode must be 32 bytes long. // chaincode must be 32 bytes long.
func ChainedPubKey(pubkey, chaincode []byte) ([]byte, error) { func ChainedPubKey(pubkey, chaincode []byte) ([]byte, error) {
if !(len(pubkey) == 65 || len(pubkey) == 33) { var compressed bool
return nil, fmt.Errorf("invalid pubkey length %v", len(pubkey)) switch n := len(pubkey); n {
case btcec.PubKeyBytesLenUncompressed:
compressed = false
case btcec.PubKeyBytesLenCompressed:
compressed = true
default:
// Incorrect serialized pubkey length
return nil, fmt.Errorf("invalid pubkey length %d", n)
} }
if len(chaincode) != 32 { if len(chaincode) != 32 {
return nil, fmt.Errorf("invalid chaincode length %d (must be 32)", return nil, fmt.Errorf("invalid chaincode length %d (must be 32)",
@ -236,11 +246,11 @@ func ChainedPubKey(pubkey, chaincode []byte) ([]byte, error) {
Y: newY, Y: newY,
} }
if len(pubkey) == 65 { if compressed {
return newPk.SerializeUncompressed(), nil
}
return newPk.SerializeCompressed(), nil return newPk.SerializeCompressed(), nil
} }
return newPk.SerializeUncompressed(), nil
}
type version struct { type version struct {
major byte major byte
@ -2088,15 +2098,13 @@ func newBtcAddress(wallet *Wallet, privkey, iv []byte, bs *BlockStamp, compresse
// randomly generated). // randomly generated).
func newBtcAddressWithoutPrivkey(wallet *Wallet, pubkey, iv []byte, bs *BlockStamp) (addr *btcAddress, err error) { func newBtcAddressWithoutPrivkey(wallet *Wallet, pubkey, iv []byte, bs *BlockStamp) (addr *btcAddress, err error) {
var compressed bool var compressed bool
switch len(pubkey) { switch n := len(pubkey); n {
case 33: case btcec.PubKeyBytesLenCompressed:
compressed = true compressed = true
case btcec.PubKeyBytesLenUncompressed:
case 65:
compressed = false compressed = false
default: default:
return nil, errors.New("incorrect pubkey length") return nil, fmt.Errorf("invalid pubkey length %d", n)
} }
if len(iv) == 0 { if len(iv) == 0 {
iv = make([]byte, 16) iv = make([]byte, 16)