xchainjs-lib icon indicating copy to clipboard operation
xchainjs-lib copied to clipboard

ADD: Node Recovery Tool

Open thorchain-admin opened this issue 3 years ago • 9 comments

Nodes Need A Specialised XChainJs tool

Repl.it script (nodeJs in the browser) https://replit.com

  1. Input of 24 word phrase const phrase = ""
  2. Input of ASGARD Address const ASGARD = ""
  3. Input of MEMO const MEMO = ""

When run "start"

For each of the 5 chains:

  1. Generate addresse on the THOR derivation path (non-standard) 44/931/0/0/0
  2. Inspect balance
  3. compose transaction to ASGARD with MEMO sending the full balance

This is an example of the SCCN recovery script: https://replit.com/@thorchain/yggdrasil-rescue

thorchain-admin avatar May 25 '21 08:05 thorchain-admin

so just getting started...i think i get the idea of what this script is supposed to do...

i'm going to write up the script to work against MCTN first, since i think it should be portable to MCCN pretty easily

mikewyszinski avatar May 25 '21 18:05 mikewyszinski

i've had to do some research into this one to make sure i get the approach correct, here is the pseudo code:

const nodePhrase = '24 seed words'
const asgardDestinationAddress = ''
const memo = ''
const derivationPath = "44'/931'/0'/0/0"

const thorNodeKeyPairAndAddress = generateKeyPairAndAdress(nodePhrase,derivationPath)

// this will find the yggvault.pub_key === thorNodeKeyPairAndAddress.address
const yggVault = findYggVaultForPubKey(thorNodeKeyPairAndAddress.address)
for (coin of yggVault.coins) {
   const { coinSymbol ,chain, assetAddress, amount } = parseAsset(coin.asset)
    let tx 
    if(chain === 'BTC'){
          tx = btcClient.sendAllYggToAsgard(thorNodeKeyPairAndAddress, amount,  asgardDestinationAddress,memo)
    }
    if(chain === 'BCH'){
          tx = bchClient.sendAllYggToAsgard(thorNodeKeyPairAndAddress, amount,  asgardDestinationAddress,memo)
    }
    if(chain === 'ETH'){
        console.log(`moving ${coinSymbol} from ygg to asgard ${asgardDestinationAddress}`)
         tx = ethClient.sendAllYggToAsgard(thorNodeKeyPairAndAddress, amount, assetAddress, assetAddress, asgardDestinationAddress,memo)
    }
    // TODO add more chains here....
    //print tx in test mode 
    // or execute tx in prod mode


}

i still have some doubts about generating the correct private key to actually be able to sign txs to move funds from ygg--> asgard, but i think i can work that out...

@thorchain-admin does the pseudo code look right? any ideas how to test this? is it possible to get the seed words for a testnet node? any other way to validate this script against my assumptions?

mikewyszinski avatar May 29 '21 03:05 mikewyszinski

@thorchain-admin work started here: https://replit.com/@hodrthemediocre/MCCN-Node-Fund-Recovery-Tool

So rather than re-invent the wheel, i think i am going to try to use xchainjs itself to handle the chain specific stuff. Tha means i'm going to have to fix https://github.com/xchainjs/xchainjs-lib/issues/321 so that xchain can be used with nodejs

mikewyszinski avatar May 31 '21 04:05 mikewyszinski

yes your approach is correct.

  1. derive the addresses from the phrase on non-standard paths
  2. get the owed coins
  3. send all to asgard with the correct memo
  • note, the correct asgard can also be queried on https://thornode.thorchain.info/thorchain/inbound_addresses one less thing to add, now it's just phrase and memo

Any 24word phrase is correct, THORChain uses BIP44/BIP39 on the path specified above.

thorchain-admin avatar May 31 '21 05:05 thorchain-admin

holy cr*p, this is turning out to be quite the pain.....

i have 2 code bases going....

github project - working but need help getting pub_key

i wrote the initial code locally to speed up testing and just now created a github project ---> https://github.com/mikewyszinski/mccnrecovery

@thorchain-admin as i mentioned in the telegram chat, i can't figure out how to generate the correct node.pub_key from the seed phrase....i've tried several things...but really not sure how to replicate the code that the GO codebase is doing to generate a pubkey like --> tthorpub1addwnpepq077pmuntdg8x26cr37t6p2ypd27t4qaja0hxpcrwr2taj9khnhzgulrmyx

sample output

PubKeySecp256k1 {
  pubKey: <Buffer 03 f6 5c 2e df 8b 7e 80 b1 64 85 8f ea 5c ca 35 49 c0 51 0d be 91 d7 8d a6 06 a0 98 28 94 19 7f 25>
}
tthorpub1q0m9ctkl3dlgpvtysk875hx2x4yuq5gdh6ga0rdxq6sfs2y5r9lj25p3qpv
{
  prefix: 'tthorpub',
  words: [
    29, 13, 13, 14, 19,  1, 25,  1,  0, 13,  8, 18,
    26, 25, 26, 25, 11, 22,  3, 25, 27,  7, 28, 12,
    20, 30, 31, 17, 16, 31, 16,  2, 17, 27, 15,  1,
     9, 25, 13, 17, 23, 19, 24, 26,  6, 22,  2, 26,
    20, 21, 31, 26,  5,  3,  2, 21,  5,  3,  9, 24,
    20
  ]
}
tthor1mmr22249lu5qzv8wkfsfguvdq5q2tns5fzu6t9
                             Recovery Tanasctions To Execute                             
┌───────┬─────────────────────┬──────────────────────────────────────────────┬──────────┐
│ Chain │               Asset │                               Asgard Address │ Gas Rate │
├───────┼─────────────────────┼──────────────────────────────────────────────┼──────────┤
│   BCH │        18922841 BCH │   qp9ny3nwqhuh5dljvulksq3pk2jkfynehgg477xgrn │        3 │
│   BNB │        34117202 BNB │  tbnb1fveyvms9l9ar0un88a5qygdj54jfy7d68x0lxs │    11250 │
│   BNB │   178576683854 BUSD │  tbnb1fveyvms9l9ar0un88a5qygdj54jfy7d68x0lxs │    11250 │
│   BNB │          264003 HEM │  tbnb1fveyvms9l9ar0un88a5qygdj54jfy7d68x0lxs │    11250 │
│   BTC │         4783854 BTC │   tb1qfveyvms9l9ar0un88a5qygdj54jfy7d6htu0c3 │       82 │
│   ETH │     1053482200 USDT │   0x4a39624e646b408d4590c05e8a84d277145ca276 │       30 │
│   ETH │       493617643 ETH │   0x4a39624e646b408d4590c05e8a84d277145ca276 │       30 │
│   ETH │      4528007744 DAI │   0x4a39624e646b408d4590c05e8a84d277145ca276 │       30 │
│   ETH │ 2625970920828 XRUNE │   0x4a39624e646b408d4590c05e8a84d277145ca276 │       30 │
│   LTC │       411840420 LTC │ tltc1qfveyvms9l9ar0un88a5qygdj54jfy7d6wr73gc │      150 │
└───────┴─────────────────────┴──────────────────────────────────────────────┴──────────┘
done

@thorchain-admin can you find anyone to shed insight on how to generate that pubkey from a seed in typpescript/javascript?

replit - not working webassembly error

i ported the code to replit --> https://replit.com/@hodrthemediocre/MCCN-Node-Recovery-Script#index.ts getting some kind of webassembly error ...assuming it something about running in replit...

not so worried about this yet....hopfully it's just plumbing issues i can overcome later

const wasmModule = new WebAssembly.Module(bytes);
                   ^
CompileError: WebAssembly.Module(): Wasm decoding failed: mutable globals cannot be exported @+534
    at Object.<anonymous> (/home/runner/MCCN-Node-Recovery-Script/node_modules/sr25519/sr25519.js:218:20)
  

mikewyszinski avatar Jun 03 '21 02:06 mikewyszinski

@thorchain-admin btc ready to test to make sure the tx generation and signing is correct with a vailid testnet node.

FYI I had to replicate parts of xchain js because of the unique needs of this script.

ALso, i'm still working locally for now until it functions properly, thn will port back to replit.

To Test with a real seed:

git clone [email protected]:mikewyszinski/mccnrecovery.git
cd mccnrecovery
npm install 
// edit index.ts to add real seed phrase
npm run build && node generated/index.js

mikewyszinski avatar Jun 07 '21 00:06 mikewyszinski

semi complete version here: https://replit.com/@hodrthemediocre/mccnrecovery just open up index.ts and change the seedPhrase and netwrok and try it out

outstanding issues

  1. ETH available balance reading with incorrect exponent 14384548490000000000 vs. thorchain -> 1438454849
  2. ETH Erc 20 balances reported as 0 , example below. perhaps this has something to do with the router contract? not sure
    ============================================================
                        USDT-0XA3910454BF2CB59B8B3A401589A3BACC5CA42306                    
    ============================================================
      From YggAddress: 0x5f7499cb53194542992f44151c9bea3f9fe8b163
    To Asgard Address: 0x4a39624e646b408d4590c05e8a84d277145ca276
                 memo: YGGDRASIL-:186678
             Available:  0
    Amount To Send: -3459139000
           Gas To Use: -10000
                              --------------------
                    Leftover:  -3459149000
       Ready To Send?: ❌
    
  3. As discussed with JP, the better way to generate all the recovery transactions is to hit /thorchain/queue/outbound, since the YGGDRASIL-: transactions for the given node should all be listed there, and they will list out the max_gas and the balance the node is expected to move to asgard... confirm the calculation wallet.availableBalance >= tranasaction.amounttosend + tranasaction.max_gas
  4. still need to refactor all xchain clients to be able to move funds with a specific total gas instead of current gas/byte calculation with coinselect

mikewyszinski avatar Jun 09 '21 02:06 mikewyszinski

remaining issues

  1. the eth balance still seems off, not sure if i am fetching it the correct way, do i need to use vaultAllowance for ETH itself?
      ============================================================
                          ETH                    
      ============================================================
        From YggAddress: 0xcf12e7d8b6f46e687de043cdad2aada104cae29a
      To Asgard Address: 0xaa3cbb4ebc745c7fff7cfada52f39f404a214167
                   memo: YGGDRASIL-:49835
              Available: 2798623543419828376
         Amount To Send: 2801271980000000000
     Ready To Send?: ❌
    
  2. does anything similar to the ERC20 need to be done for BEP2 assets on BNB? i could not find a router so i do not think so
  3. still puzzled on how to calculate/specifiy gas rates, so right now it's just using default xchainjsclient.transfer(), hoping this will just work

@thorchain-admin other than 1. above i think it's ready to test with a real seed https://replit.com/@hodrthemediocre/mccnrecovery#readme.md

mikewyszinski avatar Jun 18 '21 21:06 mikewyszinski

just a quick note for anyone tuned in. currently this script is being tested on testnet with the help of a node operator, will advise when testing complete.

mikewyszinski avatar Jul 05 '21 23:07 mikewyszinski

since ygg vauklts have been retired going to close this issue for now, the code can still be found : https://replit.com/@hodrthemediocre/mccnrecovery#readme.md

mikewyszinski avatar Oct 28 '22 03:10 mikewyszinski