github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/accountmerge.go (about)

     1  package stellar
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/stellar1"
     8  	"github.com/keybase/client/go/stellar/stellarcommon"
     9  	"github.com/keybase/stellarnet"
    10  )
    11  
    12  func AccountMerge(mctx libkb.MetaContext, walletState *WalletState, arg stellar1.AccountMergeCLILocalArg) (res stellarnet.SignResult, err error) {
    13  	baseFee := walletState.BaseFee(mctx)
    14  	sp, unlock := NewClaimSeqnoProvider(mctx, walletState)
    15  	defer unlock()
    16  	tb, err := getTimeboundsForSending(mctx, walletState)
    17  	if err != nil {
    18  		return res, err
    19  	}
    20  
    21  	if arg.FromSecretKey == nil {
    22  		bundleEntry, accountBundle, err := LookupSender(mctx, arg.FromAccountID)
    23  		if err != nil {
    24  			return res, fmt.Errorf("account merge error looking up <from>: %v", err)
    25  		}
    26  		if len(accountBundle.Signers) == 0 {
    27  			return res, fmt.Errorf("secret key not found for %s", arg.FromAccountID)
    28  		}
    29  		if len(accountBundle.Signers) > 1 {
    30  			return res, fmt.Errorf("do not know how to handle multiple secret keys found for %s", arg.FromAccountID)
    31  		}
    32  		arg.FromSecretKey = &accountBundle.Signers[0]
    33  		arg.FromAccountID = bundleEntry.AccountID
    34  		mctx.Debug("account merge <from> lookup complete: %s", arg.FromAccountID)
    35  	}
    36  	fromSeed := stellarnet.SeedStr(arg.FromSecretKey.SecureNoLogString())
    37  	mctx.Debug("account merge <from> seed lookup complete")
    38  
    39  	recipient, err := LookupRecipient(mctx, stellarcommon.RecipientInput(arg.To), true /* isCLI */)
    40  	if err != nil {
    41  		return res, fmt.Errorf("account merge error looking up <to>: %v", err)
    42  	}
    43  	toAddr := recipient.AccountID
    44  	if toAddr == nil {
    45  		return res, fmt.Errorf("cannot merge into a non-existing account")
    46  	}
    47  	mctx.Debug("account merge <to> lookup complete: %s", toAddr)
    48  
    49  	signedTx, err := stellarnet.AccountMergeTransaction(fromSeed, *toAddr, sp, tb, baseFee)
    50  	if err != nil {
    51  		return res, err
    52  	}
    53  	return signedTx, nil
    54  }