1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use evm_rpc::Hex;
use evm_state::FromKey;
use log::*;
use solana_client::rpc_client::RpcClient;
use solana_evm_loader_program::scope::*;
use solana_sdk::{
    commitment_config::CommitmentConfig,
    message::Message,
    native_token::lamports_to_sol,
    signature::{read_keypair_file, Signer},
};
use std::fs::File;
use std::io::{Read, Write};

// With the "paw" feature enabled in structopt
#[derive(Debug, structopt::StructOpt)]
enum SubCommands {
    /// Broadcast raw ethereum transaction.
    SendRawTx {
        /// A path to a file where raw transaction is stored in bincode encoding.
        raw_tx: String,
    },
    /// Transfer native chain token to EVM world.
    TransferToEvm {
        /// Amount in plancks
        amount: u64,
        /// Address in EVM, that will receive tokens
        ether_address: Hex<evm::Address>,
    },
    ///
    /// At some point in our history, in database was found incorrect blocks (native chain slots was changed).
    /// In order to recover that blocks from database, we found a solution.
    ///
    FindBlockHeader {
        #[structopt(long = "expected-blockhash")]
        expected_block_hash: Hex<evm::H256>,
        #[structopt(long = "blocks-range")]
        range: u64,
        #[structopt(long = "file", default_value = "-")]
        file: String,
    },

    /// Print EVM address.
    PrintEvmAddress {
        /// HEX representated private key.
        secret_key: evm::SecretKey,
    },
    /// Print EVM address.
    GetEvmBalance {
        /// HEX representated private key.
        secret_key: Option<evm::SecretKey>,
        #[structopt(short = "a", long = "address")]
        address: Option<Hex<evm::Address>>,
    },

    /// DEBUG: Create dummy "CREATE" transaction.
    CreateDummy {
        tx_file: String,
        #[structopt(short = "c", long = "code")]
        contract_code: Option<String>,
    },
    /// DEBUG: Create dummy "CALL" transaction.
    CallDummy {
        create_tx: String,
        tx_file: String,
        #[structopt(short = "a", long = "abi")]
        abi: Option<String>,
    },

    /// DEBUG: Parse binary array as hex/utf8.
    ParseArray { array: String },
}

#[derive(Debug, structopt::StructOpt)]
struct Args {
    #[structopt(short = "r", long = "rpc")]
    rpc_address: Option<String>,
    #[structopt(subcommand)]
    subcommand: SubCommands,
}

const SECRET_KEY_DUMMY: [u8; 32] = [1; 32];
use env_logger::Env;

#[paw::main]
fn main(args: Args) -> Result<(), Box<dyn std::error::Error>> {
    let env = Env::new().default_filter_or("info");
    env_logger::init_from_env(env);

    let keypath = solana_cli_config::Config::default().keypair_path;
    info!("Loading keypair from: {}", keypath);
    let signer = Box::new(read_keypair_file(&keypath).unwrap()) as Box<dyn Signer>;

    let address = args
        .rpc_address
        .unwrap_or_else(|| "https://api.next.velas.com:8899".to_string());
    let rpc_client = RpcClient::new(address);

    match args.subcommand {
        SubCommands::SendRawTx { raw_tx } => {
            let mut file = File::open(raw_tx).unwrap();
            let mut buf = Vec::new();
            Read::read_to_end(&mut file, &mut buf).unwrap();
            let tx: evm::Transaction =
                solana_sdk::program_utils::limited_deserialize(&buf).unwrap();

            debug!("loaded tx = {:?}", tx);
            let ix = solana_evm_loader_program::send_raw_tx(signer.pubkey(), tx, None);

            let message = Message::new(&[ix], Some(&signer.pubkey()));
            let mut create_account_tx = solana::Transaction::new_unsigned(message);

            debug!("Getting block hash");
            let (blockhash, _fee_calculator, _) = rpc_client
                .get_recent_blockhash_with_commitment(CommitmentConfig::default())
                .unwrap()
                .value;

            create_account_tx.sign(&vec![&*signer], blockhash);
            debug!("Sending tx = {:?}", create_account_tx);
            let result = rpc_client.send_and_confirm_transaction_with_spinner_and_config(
                &create_account_tx,
                CommitmentConfig::default(),
                Default::default(),
            );
            debug!("Result = {:?}", result);
        }
        SubCommands::TransferToEvm {
            amount,
            ether_address,
        } => {
            let ixs = solana_evm_loader_program::transfer_native_to_eth_ixs(
                signer.pubkey(),
                amount,
                ether_address.0,
            );
            let message = Message::new(&ixs, Some(&signer.pubkey()));
            let mut create_account_tx = solana::Transaction::new_unsigned(message);

            debug!("Getting block hash");
            let (blockhash, _fee_calculator, _) = rpc_client
                .get_recent_blockhash_with_commitment(CommitmentConfig::default())
                .unwrap()
                .value;

            create_account_tx.sign(&vec![&*signer], blockhash);
            debug!("Sending tx = {:?}", create_account_tx);
            let result = rpc_client.send_and_confirm_transaction_with_spinner_and_config(
                &create_account_tx,
                CommitmentConfig::default(),
                Default::default(),
            );
            debug!("Result = {:?}", result);
            let res = result.expect("Failed to send transaction using rpc");
            println!("Transaction signature = {}", res);
        }
        SubCommands::FindBlockHeader {
            expected_block_hash,
            range,
            file,
        } => {
            use std::str::FromStr;
            let (input, file): (_, Box<dyn std::io::Read>) = if file == "-" {
                (
                    "standart input(stdin)".to_string(),
                    Box::new(std::io::stdin()),
                )
            } else {
                (
                    format!("file({})", file),
                    Box::new(std::fs::File::open(file).unwrap()),
                )
            };
            println!("Reading blockheader from: {}", input);
            let block: evm_rpc::RPCBlock = serde_json::from_reader(file).unwrap();
            let mut block: evm_state::BlockHeader = block.to_native_block(Default::default());
            let native_slot = block.native_chain_slot;
            debug!("Readed block = {:?}", block);
            for slot in native_slot - range..native_slot + range {
                let native_block = if let Ok(native_block) = rpc_client.get_confirmed_block(slot) {
                    native_block
                } else {
                    debug!("Skiped slot = {:?}, Cannot request blockhash", slot);
                    continue;
                };
                let hash: solana_sdk::hash::Hash =
                    solana_sdk::hash::Hash::from_str(&native_block.blockhash).unwrap();
                let hash = evm::H256::from_slice(&hash.0);
                block.native_chain_hash = hash;
                block.native_chain_slot = slot;
                debug!("Produced block = {:?}, hash = {:?}", block, block.hash());
                if block.hash() == expected_block_hash.0 {
                    println!("Block slot found, slot = {}", slot);
                    return Ok(());
                }
            }
            println!("Block slot not found.");
        }
        SubCommands::PrintEvmAddress { secret_key } => {
            println!("EVM Address: {:?}", secret_key.to_address());
        }
        SubCommands::GetEvmBalance {
            secret_key,
            address,
        } => {
            let address = address.map(|a| a.0).unwrap_or_else(|| {
                secret_key
                    .expect("Expected secret_key, or address in arguments")
                    .to_address()
            });
            let balance = rpc_client
                .get_evm_balance(&address)
                .expect("Cannot parse request");
            let lamports = evm::gweis_to_lamports(balance).0; // ignore dust
            let vlx = lamports_to_sol(lamports);
            println!(
                "EVM Address: {:?}, balance: {} ({} in hex)",
                address,
                vlx,
                Hex(balance)
            );
        }
        SubCommands::CreateDummy {
            tx_file,
            contract_code,
        } => {
            let secret_key = evm::SecretKey::from_slice(&SECRET_KEY_DUMMY).unwrap();
            let tx_create = evm::UnsignedTransaction {
                nonce: 0.into(),
                gas_price: 0.into(),
                gas_limit: 300000.into(),
                action: evm::TransactionAction::Create,
                value: 0.into(),
                input: hex::decode(
                    contract_code
                        .as_deref()
                        .unwrap_or(evm_state::HELLO_WORLD_CODE),
                )
                .unwrap()
                .to_vec(),
            };
            let tx_create = tx_create.sign(&secret_key, None);

            let mut file = File::create(tx_file).unwrap();
            Write::write_all(&mut file, &bincode::serialize(&tx_create).unwrap()).unwrap();
        }
        SubCommands::CallDummy {
            tx_file,
            create_tx,
            abi,
        } => {
            let mut file = File::open(create_tx).unwrap();
            let mut buf = Vec::new();
            Read::read_to_end(&mut file, &mut buf).unwrap();
            let evm_tx: evm::Transaction =
                solana_sdk::program_utils::limited_deserialize(&buf).unwrap();
            let tx_address = evm_tx.address().unwrap();

            let secret_key = evm::SecretKey::from_slice(&SECRET_KEY_DUMMY).unwrap();
            let tx_call = evm::UnsignedTransaction {
                nonce: 0.into(),
                gas_price: 0.into(),
                gas_limit: 300000.into(),
                action: evm::TransactionAction::Call(tx_address),
                value: 0.into(),
                input: hex::decode(abi.as_deref().unwrap_or(evm_state::HELLO_WORLD_ABI))
                    .unwrap()
                    .to_vec(),
            };

            let tx_call = tx_call.sign(&secret_key, None);

            let mut file = File::create(tx_file).unwrap();
            Write::write_all(&mut file, &bincode::serialize(&tx_call).unwrap()).unwrap();
        }
        SubCommands::ParseArray { array } => {
            let bytes: Vec<u8> = serde_json::from_str(&array).unwrap();
            println!("Resulting data HEX = {}", hex::encode(&bytes));
            println!("Resulting data utf8 = {}", String::from_utf8_lossy(&bytes));
        }
    }
    Ok(())
}