Skip to content

Commit

Permalink
Add testing account.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Aug 3, 2021
1 parent 9e74717 commit 8e78605
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
41 changes: 39 additions & 2 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -179,7 +180,6 @@ func NewWallet(assetCFG *asset.WalletConfig, logger dex.Logger, network dex.Netw
log: logger,
tipChange: assetCFG.TipChange,
internalNode: node,
acct: new(accounts.Account),
}, nil
}

Expand All @@ -192,12 +192,18 @@ func (eth *ExchangeWallet) shutdown() {
// Connect connects to the node RPC server. A dex.Connector.
func (eth *ExchangeWallet) Connect(ctx context.Context) (*sync.WaitGroup, error) {
c := rpcclient{}
if err := c.connect(ctx, eth.internalNode, mainnetContractAddr); err != nil {
err := c.connect(ctx, eth.internalNode, mainnetContractAddr)
if err != nil {
return nil, err
}
eth.node = &c
eth.ctx = ctx

eth.acct, err = eth.initAccount()
if err != nil {
return nil, err
}

// Initialize the best block.
bestHash, err := eth.node.bestBlockHash(ctx)
if err != nil {
Expand All @@ -224,6 +230,37 @@ func (eth *ExchangeWallet) Connect(ctx context.Context) (*sync.WaitGroup, error)
return &wg, nil
}

// initAccount checks to see if the internal client has an account. If found
// returns the account. If not it imports the account via private key.
//
// Currently this only imports a test account. However, in the future this will
// need to be created deterministically from the app seed.
func (eth *ExchangeWallet) initAccount() (*accounts.Account, error) {
testAcctAddr := common.HexToAddress("b6de8bb5ed28e6be6d671975cad20c03931be981")
accts := eth.node.accounts()
for _, acct := range accts {
if bytes.Equal(acct.Address[:], testAcctAddr[:]) {
return acct, nil
}
}
testAcctPrivHex := "0695b9347a4dc096ae5c6f1935380ceba550c70b112f1323c211bade4d11651b"
pw := "abc"
privB, err := hex.DecodeString(testAcctPrivHex)
if err != nil {
return nil, err
}
acct, err := eth.node.importAccount(pw, privB)

This comment has been minimized.

Copy link
@chappjc

chappjc Aug 3, 2021

Member

OK, but out of curiosity, is there no rpc to create a new account and return the address and private key?

if err != nil {
return nil, err
}
// core expects an account to be unlocked during initialization.
err = eth.node.unlock(eth.ctx, pw, acct)
if err != nil {
return nil, err
}
return acct, nil
}

// OwnsAddress indicates if an address belongs to the wallet.
//
// In Ethereum, an address is an account.
Expand Down
20 changes: 11 additions & 9 deletions client/asset/eth/rpcclient_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// This test requires that the testnet harness be running and the unix socket
// be located at $HOME/dextest/eth/gamma/node/geth.ipc
//
// These tests are expected to be run in descending as some depend on the tests
// before. They cannot be run in parallel.
// These tests are expected to be run in descending order as some depend on the
// tests before. They cannot be run in parallel.
//
// NOTE: These test reuse a light node that lives in the dextest folders.
// However, when recreating the test database for every test, the nonce used
// for imported accounts is sometimes, randomly, off, which causes transactions
// to not be mined and effectively makes the node unuseable (at least before
// restarting). It also seems to have caused getting balance of an account to
// fail, and sometime the redeem and refund functions to also fail. This could
// fail, and sometimes the redeem and refund functions to also fail. This could
// be a problem in the future if a user restores from seed. Punting on this
// particular problem for now.
//
Expand Down Expand Up @@ -716,7 +716,7 @@ func TestReplayAttack(t *testing.T) {
t.Fatal(err)
}

// Deploy the fund reentry contract.
// Deploy the reentry attack contract.
_, _, reentryContract, err := reentryattack.DeployReentryAttack(txOpts, ethClient.ec)
if err != nil {
t.Fatal(err)
Expand All @@ -732,7 +732,8 @@ func TestReplayAttack(t *testing.T) {

txOpts.Value = amt
var secretHash [32]byte
// Make four swaps that should be locked and refundable and one that is soon refundable.
// Make four swaps that should be locked and refundable and one that is
// soon refundable.
for i := 0; i < 5; i++ {
var secret [32]byte
copy(secret[:], encode.RandomBytes(32))
Expand All @@ -752,7 +753,7 @@ func TestReplayAttack(t *testing.T) {
}

inLocktime := time.Now().Add(-1 * time.Second).Unix()
// Set some variables in the contract use for the exploit. This
// Set some variables in the contract used for the exploit. This
// will fail (silently) due to require(msg.origin == msg.sender)
// in the real contract.
_, err := reentryContract.SetUsUpTheBomb(txOpts, contractAddr, secretHash, big.NewInt(inLocktime), participantAddr)
Expand Down Expand Up @@ -809,15 +810,16 @@ func TestReplayAttack(t *testing.T) {
t.Fatal(err)
}

// If the exploit worked, the test will fail here, with all funds
// drained from the contract.
// If the exploit worked, the test will fail here, with 4 ether we
// shouldn't be able to touch drained from the contract.
if bal.Cmp(wantBal) != 0 {
diff := big.NewInt(0).Sub(bal, wantBal)
t.Fatalf("unexpected balance change of account: want %v got %v "+
"or a difference of %v", wantBal, bal, diff)
}

// The exploit failed and status should not have changed.
// The exploit failed and status should be None because initiation also
// failed.
swap, err := ethClient.swap(ctx, simnetAcct, secretHash)
if err != nil {
t.Fatal(err)
Expand Down
8 changes: 8 additions & 0 deletions client/cmd/dexcctl/simnet-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ LTC_ON=$?
~/dextest/bch/harness-ctl/alpha getblockchaininfo > /dev/null
BCH_ON=$?

~/dextest/eth/harness-ctl/alpha attach --exec 'eth.blockNumber' > /dev/null
ETH_ON=$?

set -e

echo initializing
Expand All @@ -32,6 +35,11 @@ if [ $BCH_ON -eq 0 ]; then
./dexcctl -p abc -p "" --simnet newwallet 145 ~/dextest/bch/alpha/alpha.conf '{"walletname":"gamma"}'
fi

if [ $ETH_ON -eq 0 ]; then
echo configuring Eth wallet
./dexcctl -p abc -p "" --simnet newwallet 60 "" '{"appDir":"~/dextest/eth/testnode"}'
fi

echo registering with DEX
./dexcctl -p abc --simnet register 127.0.0.1:17273 100000000 ~/dextest/dcrdex/rpc.cert

Expand Down
8 changes: 6 additions & 2 deletions dex/testing/eth/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ DELTA_NODE_KEY="725394672587b34bbf15580c59e5199c75c2c7e998ba8df3cb38cc4347d46e2b
DELTA_ENODE="ca414c361d1a38716170923e4900d9dc9203dbaf8fdcaee73e1f861df9fdf20a1453b76fd218c18bc6f3c7e13cbca0b3416af02a53b8e31188faa45aab398d1c"
DELTA_NODE_PORT="30307"

# TESTING_ADDRESS is used by the client's internal node.
TESTING_ADDRESS="b6de8bb5ed28e6be6d671975cad20c03931be981"

ETH_SWAP_V0="608060405234801561001057600080fd5b50610784806100206000396000f3fe60806040526004361061004a5760003560e01c80637249fbb61461004f57806376467cbd14610071578063ae052147146100a7578063b31597ad146100ba578063eb84e7f2146100da575b600080fd5b34801561005b57600080fd5b5061006f61006a36600461057d565b61015c565b005b34801561007d57600080fd5b5061009161008c36600461057d565b61025e565b60405161009e9190610673565b60405180910390f35b61006f6100b53660046105d1565b610349565b3480156100c657600080fd5b5061006f6100d53660046105af565b61040e565b3480156100e657600080fd5b506101486100f536600461057d565b6000602081905290815260409020805460018201546002830154600384015460048501546005860154600687015460079097015495969495939492936001600160a01b0392831693919092169160ff1688565b60405161009e9897969594939291906106e3565b32331461016857600080fd5b80600160008281526020819052604090206007015460ff16600381111561019157610191610738565b1461019b57600080fd5b6000818152602081905260409020600401546001600160a01b031633146101c157600080fd5b600081815260208190526040902060010154428111156101e057600080fd5b60008381526020819052604080822060078101805460ff191660031790556006015490513391908381818185875af1925050503d806000811461023f576040519150601f19603f3d011682016040523d82523d6000602084013e610244565b606091505b509091505060018115151461025857600080fd5b50505050565b6102a36040805161010081018252600080825260208201819052918101829052606081018290526080810182905260a0810182905260c081018290529060e082015290565b6000828152602081815260409182902082516101008101845281548152600182015492810192909252600281015492820192909252600380830154606083015260048301546001600160a01b03908116608084015260058401541660a0830152600683015460c0830152600783015491929160e084019160ff9091169081111561032f5761032f610738565b600381111561034057610340610738565b90525092915050565b826000341161035757600080fd5b6000811161036457600080fd5b32331461037057600080fd5b826000808281526020819052604090206007015460ff16600381111561039857610398610738565b146103a257600080fd5b6000848152602081905260409020438155600180820187905560028201869055600482018054336001600160a01b0319918216179091556005830180549091166001600160a01b0387161790553460068301556007909101805460ff1916828002179055505050505050565b32331461041a57600080fd5b8082600160008381526020819052604090206007015460ff16600381111561044457610444610738565b1461044e57600080fd5b6000828152602081905260409020600501546001600160a01b0316331461047457600080fd5b8160028260405160200161048a91815260200190565b60408051601f19818403018152908290526104a491610638565b602060405180830381855afa1580156104c1573d6000803e3d6000fd5b5050506040513d601f19601f820116820180604052508101906104e49190610596565b146104ee57600080fd5b60008381526020819052604080822060078101805460ff191660021790556006015490513391908381818185875af1925050503d806000811461054d576040519150601f19603f3d011682016040523d82523d6000602084013e610552565b606091505b509091505060018115151461056657600080fd5b505050600090815260208190526040902060030155565b60006020828403121561058f57600080fd5b5035919050565b6000602082840312156105a857600080fd5b5051919050565b600080604083850312156105c257600080fd5b50508035926020909101359150565b6000806000606084860312156105e657600080fd5b833592506020840135915060408401356001600160a01b038116811461060b57600080fd5b809150509250925092565b6004811061063457634e487b7160e01b600052602160045260246000fd5b9052565b6000825160005b81811015610659576020818601810151858301520161063f565b81811115610668576000828501525b509190910192915050565b60006101008201905082518252602083015160208301526040830151604083015260608301516060830152608083015160018060a01b0380821660808501528060a08601511660a0850152505060c083015160c083015260e08301516106dc60e0840182610616565b5092915050565b8881526020810188905260408101879052606081018690526001600160a01b038581166080830152841660a082015260c08101839052610100810161072b60e0830184610616565b9998505050505050505050565b634e487b7160e01b600052602160045260246000fdfea2646970667358221220f5008532d651b31b24a7d133ff5dd0c4461e5f6339f32c9d551cb9cf81107e6b64736f6c63430008060033"

# PASSWORD is the password used to unlock all accounts/wallets/addresses.
Expand Down Expand Up @@ -94,7 +97,7 @@ cat > "${NODES_ROOT}/genesis.json" <<EOF
"extradata": "0x00000000000000000000000000000000000000000000000000000000000000009ebba10a6136607688ca4f27fab70e23938cd0270000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"18d65fb8d60c1199bb1ad381be47aa692b482605": {
"balance": "11000000000000000000000"
"balance": "26000000000000000000000"
},
"4f8ef3892b65ed7fc356ff473a2ef2ae5ec27a06": {
"balance": "11000000000000000000000"
Expand Down Expand Up @@ -199,9 +202,10 @@ echo "Mining some blocks"
"${NODES_ROOT}/harness-ctl/mine-alpha" "2"

SEND_AMT=5000000000000000000000
echo "Sending 5000 eth to delta and gamma."
echo "Sending 5000 eth to delta and gamma and testing."
"${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/send.js --exec send(\"${ALPHA_ADDRESS}\",\"${GAMMA_ADDRESS}\",${SEND_AMT})"
"${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/send.js --exec send(\"${ALPHA_ADDRESS}\",\"${DELTA_ADDRESS}\",${SEND_AMT})"
"${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/send.js --exec send(\"${ALPHA_ADDRESS}\",\"${TESTING_ADDRESS}\",${SEND_AMT})"

echo "Deploying ETHSwapV0 contract."
CONTRACT_HASH=$("${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/deploy.js --exec deploy(\"${ALPHA_ADDRESS}\",\"${ETH_SWAP_V0}\")" | sed 's/"//g')
Expand Down

0 comments on commit 8e78605

Please sign in to comment.