Skip to content

Commit 0700d59

Browse files
Chinwendu20kaloudis
authored andcommitted
walletrpc: Implement Rescan rpc
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
1 parent 372818b commit 0700d59

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

lnrpc/walletrpc/walletkit_server.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"context"
99
"encoding/base64"
1010
"encoding/binary"
11+
"encoding/hex"
1112
"errors"
1213
"fmt"
1314
"math"
1415
"os"
1516
"path/filepath"
1617
"sort"
18+
"strconv"
19+
"strings"
1720
"time"
1821

1922
"github.com/btcsuite/btcd/btcec/v2"
@@ -2770,6 +2773,123 @@ func (w *WalletKit) ImportPublicKey(_ context.Context,
27702773
return &ImportPublicKeyResponse{}, nil
27712774
}
27722775

2776+
func (w *WalletKit) Rescan(_ context.Context,
2777+
req *RescanRequest) (*RescanResponse, error) {
2778+
2779+
hash, err := w.cfg.Chain.GetBlockHash(int64(req.StartHeight))
2780+
if err != nil {
2781+
return nil, err
2782+
}
2783+
2784+
header, err := w.cfg.Chain.GetBlockHeader(hash)
2785+
if err != nil {
2786+
return nil, err
2787+
}
2788+
2789+
startBlock := &waddrmgr.BlockStamp{
2790+
Hash: *hash,
2791+
Height: req.StartHeight,
2792+
Timestamp: header.Timestamp,
2793+
}
2794+
2795+
addresses := req.Addresses
2796+
if len(addresses) == 0 {
2797+
addressLists, err := w.cfg.Wallet.ListAddresses(
2798+
"", true,
2799+
)
2800+
if err != nil {
2801+
return nil, fmt.Errorf(
2802+
"unable to list addresses: %w", err)
2803+
}
2804+
2805+
for _, addressProperty := range addressLists {
2806+
addrFunc := func(addr lnwallet.AddressProperty) string {
2807+
return addr.Address
2808+
}
2809+
addresses = append(
2810+
addresses, fn.Map(addrFunc, addressProperty)...,
2811+
)
2812+
2813+
}
2814+
}
2815+
2816+
// Convert the supplied addresses to btcutil addresses.
2817+
btcutilAddresses := make([]btcutil.Address, 0)
2818+
for _, address := range addresses {
2819+
btcutilAddr, err := btcutil.DecodeAddress(
2820+
address, w.cfg.ChainParams,
2821+
)
2822+
if err != nil {
2823+
return nil, fmt.Errorf(
2824+
"unable to decode address: %w", err)
2825+
}
2826+
2827+
btcutilAddresses = append(btcutilAddresses, btcutilAddr)
2828+
}
2829+
2830+
var outpoints map[wire.OutPoint]btcutil.Address
2831+
for outpoint, address := range req.Outpoints {
2832+
btcutilAddr, err := btcutil.DecodeAddress(
2833+
address, w.cfg.ChainParams,
2834+
)
2835+
if err != nil {
2836+
return nil, fmt.Errorf(
2837+
"unable to decode address: %w", err)
2838+
}
2839+
wireOutpoint, err := stringtoWireOutpoints(outpoint)
2840+
if err != nil {
2841+
return nil, err
2842+
}
2843+
outpoints[*wireOutpoint] = btcutilAddr
2844+
}
2845+
err = w.cfg.Wallet.Rescan(startBlock, btcutilAddresses, outpoints)
2846+
if err != nil {
2847+
return nil, err
2848+
}
2849+
2850+
return &RescanResponse{
2851+
Status: "ok",
2852+
}, nil
2853+
}
2854+
2855+
func NewProtoOutPoint(op string) (*lnrpc.OutPoint, error) {
2856+
parts := strings.Split(op, ":")
2857+
if len(parts) != 2 {
2858+
return nil, errors.New("outpoint should be of the form txid:index")
2859+
}
2860+
txid := parts[0]
2861+
if hex.DecodedLen(len(txid)) != chainhash.HashSize {
2862+
return nil, fmt.Errorf("invalid hex-encoded txid %v", txid)
2863+
}
2864+
outputIndex, err := strconv.Atoi(parts[1])
2865+
if err != nil {
2866+
return nil, fmt.Errorf("invalid output index: %w", err)
2867+
}
2868+
return &lnrpc.OutPoint{
2869+
TxidStr: txid,
2870+
OutputIndex: uint32(outputIndex),
2871+
}, nil
2872+
}
2873+
func stringtoWireOutpoints(outpoint string) (*wire.OutPoint,
2874+
error) {
2875+
2876+
protoOutpoint, err := NewProtoOutPoint(outpoint)
2877+
if err != nil {
2878+
return nil, err
2879+
}
2880+
2881+
hash, err := chainhash.NewHashFromStr(outpoint)
2882+
if err != nil {
2883+
return nil, fmt.Errorf("cannot create chainhash")
2884+
}
2885+
2886+
wireOutpoint := wire.NewOutPoint(
2887+
hash, protoOutpoint.OutputIndex,
2888+
)
2889+
2890+
return wireOutpoint, nil
2891+
}
2892+
27732893
// ImportTapscript imports a Taproot script and internal key and adds the
27742894
// resulting Taproot output key as a watch-only output script into the wallet.
27752895
// For BIP-0086 style Taproot keys (no root hash commitment and no script spend

0 commit comments

Comments
 (0)