From 405eca4a4489cf7033a2321eb3b46095da6c1f76 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 3 Jan 2014 13:22:28 -0500 Subject: [PATCH] Remove usage of deprecated address encode/decode API. --- rpcserver.go | 28 +++++++++++++--------------- rpcwebsocket.go | 27 +++++++++++++++++---------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index edf72021..d0501a42 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -528,31 +528,29 @@ func createVoutList(mtx *btcwire.MsgTx, net btcwire.BitcoinNet) ([]btcjson.Vout, voutList[i].ScriptPubKey.Asm = disbuf voutList[i].ScriptPubKey.Hex = hex.EncodeToString(v.PkScript) - scriptType, reqSigs, addrHashes := btcscript.CalcPkScriptAddrHashes(v.PkScript) + scriptType, reqSigs, hashes := btcscript.CalcPkScriptAddrHashes(v.PkScript) voutList[i].ScriptPubKey.Type = scriptType.String() voutList[i].ScriptPubKey.ReqSigs = reqSigs - if addrHashes == nil { + if hashes == nil { voutList[i].ScriptPubKey.Addresses = nil } else { - voutList[i].ScriptPubKey.Addresses = make([]string, len(addrHashes)) - for j := 0; j < len(addrHashes); j++ { - var addr string + voutList[i].ScriptPubKey.Addresses = make([]string, len(hashes)) + for j := 0; j < len(hashes); j++ { + var addr btcutil.Address if scriptType == btcscript.ScriptHashTy { - addr, err = btcutil.EncodeScriptHash(addrHashes[j], net) - if err != nil { - continue - } + addr, err = btcutil.NewAddressScriptHash(hashes[j], net) } else { - addr, err = btcutil.EncodeAddress(addrHashes[j], net) - if err != nil { - continue - } + addr, err = btcutil.NewAddressPubKeyHash(hashes[j], net) } - voutList[i].ScriptPubKey.Addresses[j] = addr + if err != nil { + // hash will always be 20 bytes, so the only + // possible error is from an invalid network. + return nil, errors.New("Cannot create address with invalid network.") + } + voutList[i].ScriptPubKey.Addresses[j] = addr.EncodeAddress() } } - } return voutList, nil diff --git a/rpcwebsocket.go b/rpcwebsocket.go index 284d5d93..c794d746 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -340,12 +340,19 @@ func handleNotifyNewTXs(s *rpcServer, cmd btcjson.Cmd, } for _, addr := range notifyCmd.Addresses { - hash, _, err := btcutil.DecodeAddress(addr) + addr, err := btcutil.DecodeAddr(addr) if err != nil { return fmt.Errorf("cannot decode address: %v", err) } - s.ws.AddTxRequest(walletNotification, rc, string(hash), - cmd.Id()) + + // TODO(jrick) Notifing for non-P2PKH addresses is currently + // unsuported. + if _, ok := addr.(*btcutil.AddressPubKeyHash); !ok { + return fmt.Errorf("address is not P2PKH: %v", addr.EncodeAddress()) + } + + s.ws.AddTxRequest(walletNotification, rc, + string(addr.ScriptAddress()), cmd.Id()) } mreply, _ := json.Marshal(reply) @@ -423,13 +430,13 @@ func handleRescan(s *rpcServer, cmd btcjson.Cmd, if st != btcscript.ScriptAddr || err != nil { continue } - txaddr, err := btcutil.EncodeAddress(txaddrhash, s.server.btcnet) + txaddr, err := btcutil.NewAddressPubKeyHash(txaddrhash, s.server.btcnet) if err != nil { - rpcsLog.Errorf("Error encoding address: %v", err) + rpcsLog.Errorf("Error creating address: %v", err) return err } - if _, ok := rescanCmd.Addresses[txaddr]; ok { + if _, ok := rescanCmd.Addresses[txaddr.EncodeAddress()]; ok { // TODO(jrick): This lookup is expensive and can be avoided // if the wallet is sent the previous outpoints for all inputs // of the tx, so any can removed from the utxo set (since @@ -460,7 +467,7 @@ func handleRescan(s *rpcServer, cmd btcjson.Cmd, PkScript string `json:"pkscript"` Spent bool `json:"spent"` }{ - Receiver: txaddr, + Receiver: txaddr.EncodeAddress(), Height: blk.Height(), BlockHash: blkshalist[i].String(), BlockIndex: tx.Index(), @@ -784,9 +791,9 @@ func (s *rpcServer) NotifyForTxOuts(tx *btcutil.Tx, block *btcutil.Block) { for e := idlist.Front(); e != nil; e = e.Next() { ctx := e.Value.(*notificationCtx) - txaddr, err := btcutil.EncodeAddress(txaddrhash, s.server.btcnet) + txaddr, err := btcutil.NewAddressPubKeyHash(txaddrhash, s.server.btcnet) if err != nil { - rpcsLog.Error("Error encoding address; dropping Tx notification.") + rpcsLog.Debugf("Error creating address; dropping Tx notification.") break } @@ -802,7 +809,7 @@ func (s *rpcServer) NotifyForTxOuts(tx *btcutil.Tx, block *btcutil.Block) { Amount int64 `json:"amount"` PkScript string `json:"pkscript"` }{ - Receiver: txaddr, + Receiver: txaddr.EncodeAddress(), TxID: tx.Sha().String(), TxOutIndex: uint32(i), Amount: txout.Value,