Skip to content

Instantly share code, notes, and snippets.

@plowsof
Created November 10, 2023 01:38
Show Gist options
  • Save plowsof/ae27643a3eb52950758a5599eb28abc4 to your computer and use it in GitHub Desktop.
Save plowsof/ae27643a3eb52950758a5599eb28abc4 to your computer and use it in GitHub Desktop.
restore a wallet from 0 with sync off / reuse password / custom password and compare balances
from monerorpc.authproxy import AuthServiceProxy, JSONRPCException
import os, requests, time
#./monero-wallet-rpc --disable-rpc-login --rpc-bind-port 18082 --stagenet --wallet-dir /home/human/monero/build/Linux/view_sync/release/bin --daemon-host node2.monerodevs.org:38089 --trusted --no-initial-sync
wallet_dir = "/home/human/monero/build/Linux/view_sync/release/bin"
daemon_address = "node2.monerodevs.org:38089"
rpc_url = "http://127.0.0.1:18082/json_rpc"
test_wallet = "stager"
rpc_connection = AuthServiceProxy(service_url=rpc_url)
original_balance = 0
def is_synced():
current_height = 0
target_height= 1
while current_height != target_height:
try:
response = requests.post(
'http://localhost:18082/json_rpc',
json={'jsonrpc': '2.0', 'id': '0', 'method': 'get_height'}
)
current_height = response.json()['result']['height']
response = requests.post(
'http://node2.monerodevs.org:38089/json_rpc',
json={'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'}
)
target_height = response.json()["result"]["height"]
print(f"Current height:{current_height}")
print(f"Target height:{target_height}")
time.sleep(10)
except JSONRPCException as e:
print(f"Failed to get height from RPC: {str(e)}")
sys.exit(1)
print("we are fully synced")
def restore_wallet(fname,seed,sync_mode=False):
global wallet_dir, rpc_connection, original_balance
params = {
"filename": fname,
"seed": seed,
"password": "",
"restore_height": 1460978
}
print(f"Creating wallet: {fname}")
make_new = rpc_connection.restore_deterministic_wallet(params)
if sync_mode:
match sync_mode:
case 1:
print("Sync mode: reuse-wallet-password")
sync_mode = "reuse-wallet-password"
case 2:
print("Sync mode: custom-background-password")
sync_mode = "custom-background-password"
params = {
"background_sync_type": sync_mode,
"background_cache_password":"lol"
}
rpc_connection.setup_background_sync(params)
rpc_connection.start_background_sync()
else:
print("Sync mode: off")
is_synced()
balance = rpc_connection.get_balance()["balance"]
if sync_mode:
print(f"Balance before sync disabled: {balance}")
else:
original_balance = balance
print(f"Original balance: {balance}")
if sync_mode:
rpc_connection.stop_background_sync({"wallet_password":""})
balance = rpc_connection.get_balance()["balance"]
print(f"Balance after sync disabled: {balance}")
assert balance == original_balance
def main(gui=False):
global wallet_dir,rpc_url,test_wallet,daemon_address,rpc_connection
try: rpc_connection.close_wallet()
except: pass
rpc_connection.open_wallet({"filename": test_wallet,"password": ""})
seed = rpc_connection.query_key({"key_type":"mnemonic"})["key"]
try:
os.remove("restored_wallet")
os.remove("restored_wallet.keys")
os.remove("restored_wallet_sync")
os.remove("restored_wallet_sync.keys")
os.remove("restored_wallet_sync_custom")
os.remove("restored_wallet_sync_custom.keys")
os.remove("restored_wallet_sync_custom.background")
os.remove("restored_wallet_sync_custom.background.keys")
except: pass
restore_wallet("restored_wallet",seed)
rpc_connection.store()
restore_wallet("restored_wallet_sync",seed,1)
rpc_connection.store()
restore_wallet("restored_wallet_sync_custom",seed,2)
rpc_connection.store()
if __name__ == "__main__":
main()
'''
Creating wallet: restored_wallet
Sync mode: off
Current height:1461977
Target height:1476995
Current height:1468970
Target height:1476995
Current height:1475963
Target height:1476995
Current height:1476995
Target height:1476995
we are fully synced
Original balance: 14588721690000
Creating wallet: restored_wallet_sync
Sync mode: reuse-wallet-password
Current height:1461977
Target height:1476996
Current height:1468970
Target height:1476996
Current height:1475963
Target height:1476996
Current height:1476996
Target height:1476996
we are fully synced
Balance before sync disabled: 44687988760000
Balance after sync disabled: 14588721690000
Creating wallet: restored_wallet_sync_custom
Sync mode: custom-background-password
Current height:1461977
Target height:1476996
Current height:1468970
Target height:1476996
Current height:1475963
Target height:1476996
Current height:1476996
Target height:1476997
Current height:1476996
Target height:1476997
Current height:1476997
Target height:1476997
we are fully synced
Balance before sync disabled: 44687988760000
Balance after sync disabled: 14588721690000
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment