Files
atomic-swap/protocol/xmrmaker/recovery.go
Dmitry Holodov adfb0fa602 Test cleanup (#135)
* version independent monerod path with localhost-only binding

* fixed lint target and install script

* created 3rd ETH key constant to avoid race condition in tests

* updated run-integration-tests.sh with the same changes made to run-unit-tests.sh

* new design for paritioning out 2 unique keys to test packages

* restored accidentally deleted tests/alice.key

* removed websocket connection leaks from tests

* made file paths unique between tests with better file cleanup

* fix for the websocket connection leak commit

* reverted increase of GenerateBlocks (didn't mean to commit that)

* fixed maker/taker key that I had reversed

* fixed incorrect zero-balance check

* fixed comment on ClaimOrRefund

* added back sample script for installing go in /usr/local/go

* etchclient creation cleanup using t.Cleanup()

* minor cleanup to ganache_test_keys code

* initial dynamic monero-wallet-rpc implementation for tests

* converted monero tests to use dynamic monero-wallet-rpc services

* unit tests all using dynamic monero-wallet-rpc services

* fixed 2 tests that failed after moving to dynamic monero-wallet-rpc services

* fixed low balance issues in TestSwapState_NotifyClaimed

Co-authored-by: noot <36753753+noot@users.noreply.github.com>
2022-06-29 13:36:03 -04:00

114 lines
2.9 KiB
Go

package xmrmaker
import (
"context"
"errors"
ethcommon "github.com/ethereum/go-ethereum/common"
mcrypto "github.com/noot/atomic-swap/crypto/monero"
"github.com/noot/atomic-swap/dleq"
pcommon "github.com/noot/atomic-swap/protocol"
"github.com/noot/atomic-swap/protocol/backend"
"github.com/noot/atomic-swap/swapfactory"
)
type recoveryState struct {
ss *swapState
}
// NewRecoveryState returns a new *xmrmaker.recoveryState,
// which has methods to either claim ether or reclaim monero from an initiated swap.
func NewRecoveryState(b backend.Backend, basePath string, secret *mcrypto.PrivateSpendKey,
contractAddr ethcommon.Address,
contractSwapID [32]byte, contractSwap swapfactory.SwapFactorySwap) (*recoveryState, error) { //nolint:revive
kp, err := secret.AsPrivateKeyPair()
if err != nil {
return nil, err
}
pubkp := kp.PublicKeyPair()
var sc [32]byte
copy(sc[:], secret.Bytes())
ctx, cancel := context.WithCancel(b.Ctx())
s := &swapState{
ctx: ctx,
cancel: cancel,
Backend: b,
privkeys: kp,
pubkeys: pubkp,
dleqProof: dleq.NewProofWithSecret(sc),
contractSwapID: contractSwapID,
contractSwap: contractSwap,
infoFile: pcommon.GetSwapRecoveryFilepath(basePath),
}
if err := s.setContract(contractAddr); err != nil {
return nil, err
}
s.setTimeouts(contractSwap.Timeout0, contractSwap.Timeout1)
return &recoveryState{
ss: s,
}, nil
}
// RecoveryResult represents the result of a recovery operation.
// If the ether was claimed, Claimed is set to true and the TxHash is set.
// If the monero was recovered, Recovered is set to true and the MoneroAddress is set.
type RecoveryResult struct {
Claimed, Recovered bool
TxHash ethcommon.Hash
MoneroAddress mcrypto.Address
}
// ClaimOrRecover either claims ether or recovers monero by creating a wallet.
// It returns a *RecoveryResult.
func (rs *recoveryState) ClaimOrRecover() (*RecoveryResult, error) {
// check if XMRTaker refunded
skA, err := rs.ss.filterForRefund()
if !errors.Is(err, errNoRefundLogsFound) && err != nil {
return nil, err
}
// if XMRTaker refunded, let's get our monero back
if skA != nil {
kpA, err := skA.AsPrivateKeyPair() //nolint:govet
if err != nil {
return nil, err
}
rs.ss.setXMRTakerPublicKeys(kpA.PublicKeyPair(), nil)
addr, err := rs.ss.reclaimMonero(skA)
if err != nil {
return nil, err
}
return &RecoveryResult{
Recovered: true,
MoneroAddress: addr,
}, nil
}
// otherwise, let's try to claim
txHash, err := rs.ss.tryClaim()
if err != nil {
if errors.Is(err, errPastClaimTime) {
log.Infof(
"Past the time where we can claim the ether, and the counterparty " +
"has not yet refunded. Please try running the recovery module again later " +
"and hopefully the counterparty will have refunded by then.",
)
}
return nil, err
}
return &RecoveryResult{
Claimed: true,
TxHash: txHash,
}, nil
}