rpcserver: check for compatible network in SendCoins

lnd_test: adding address validation for send coins

The commit adds a test that checks that when a user calls sendcoins, the
receiving address is validated according to the current network. If the
address is not compatible with the current network, it will return an
error to the user.

rpcserver: adding a check for compatible network in SendCoins

This commit adds a check in SendCoins that checks whether the receiving
address is compatible with the current network.

Fixes #2677.
This commit is contained in:
ccdle12 authored and Olaoluwa Osuntokun committed 2019-03-28 16:42:44 -07:00
1 parent 341f5e4329
commit abfbdf6aec
2 files changed
+46 -13

No files matched your search

+16 -11
View File
@@ -880,6 +880,20 @@ func (r *rpcServer) SendCoins(ctx context.Context,
in.Addr, btcutil.Amount(in.Amount), int64(feePerKw),
in.SendAll)
// Decode the address receiving the coins, we need to check whether the
// address is valid for this network.
targetAddr, err := btcutil.DecodeAddress(in.Addr, activeNetParams.Params)
if err != nil {
return nil, err
}
// Make the check on the decoded address according to the active network.
if !targetAddr.IsForNet(activeNetParams.Params) {
return nil, fmt.Errorf("address: %v is not valid for this "+
"network: %v", targetAddr.String(),
activeNetParams.Params.Name)
}
var txid *chainhash.Hash
wallet := r.server.cc.wallet
@@ -896,15 +910,6 @@ func (r *rpcServer) SendCoins(ctx context.Context,
"active")
}
// Additionally, we'll need to convert the sweep address passed
// into a useable struct, and also query for the latest block
// height so we can properly construct the transaction.
sweepAddr, err := btcutil.DecodeAddress(
in.Addr, activeNetParams.Params,
)
if err != nil {
return nil, err
}
_, bestHeight, err := r.server.cc.chainIO.GetBestBlock()
if err != nil {
return nil, err
@@ -915,7 +920,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
// single transaction. This will be generated in a concurrent
// safe manner, so no need to worry about locking.
sweepTxPkg, err := sweep.CraftSweepAllTx(
feePerKw, uint32(bestHeight), sweepAddr, wallet,
feePerKw, uint32(bestHeight), targetAddr, wallet,
wallet.WalletController, wallet.WalletController,
r.server.cc.feeEstimator, r.server.cc.signer,
)
@@ -945,7 +950,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
// coin selection synchronization method to ensure that no coin
// selection (funding, sweep alls, other sends) can proceed
// while we instruct the wallet to send this transaction.
paymentMap := map[string]int64{in.Addr: in.Amount}
paymentMap := map[string]int64{targetAddr.String(): in.Amount}
err := wallet.WithCoinSelectLock(func() error {
newTXID, err := r.sendCoinsOnChain(paymentMap, feePerKw)
if err != nil {