Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added --solana-key flag to derive destination address #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: added --solana-key flag to derive destination address
Description
-----------
- introduced --solana-key flag
- added support for reading Solana keypair files
  and deriving public keys
- implemented validation to prevent using both
  --solana-key and --destination
- added test suite for the introduced feat
- improved error handling for invalid key files

Usage
-----
Clone this PR branch and install dependencies and make sure you
followed the readme to derive eth key `private-key.txt`
and solana key `key.json` and then run
```
node bin/cli.js -k private-key.txt --solana-key key.json -a 0.002 --sepolia
```

Testing the introduced feat
---------------------------
From the root direcoty, run:
```
yarn test
```

chore: format
  • Loading branch information
0xObsidian committed Dec 17, 2024
commit ecaf738cffbf5eacf4ba2bfbf446bfea08d14853
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -7,3 +7,5 @@ yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
private-key.txt
node_modules
key.json
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -46,28 +46,35 @@ TODO

## Create a Deposit

1. Run the CLI tool with the necessary options:
1. Run the CLI tool with one of the following options:

**Using a Solana address directly:**
```bash
node bin/cli.js -k <path_to_private_key> -d <solana_destination_address> -a <amount_in_ether> --mainnet|--sepolia
```

**Using a Solana key.json file:**
```bash
node bin/cli.js -k <path_to_private_key> --solana-key <path_to_solana_key.json> -a <amount_in_ether> --mainnet|--sepolia
```

For example:

**Mainnet Deposit:**
**Mainnet Deposit with address:**
```bash
node bin/cli.js -k private-key.txt -d 6g8wB6cJbodeYaEb5aD9QYqhdxiS8igfcHpz36oHY7p8 -a 0.002 --mainnet
```

**Sepolia Testnet Deposit:**
**Sepolia Testnet Deposit with key.json:**
```bash
node bin/cli.js -k private-key.txt -d 6g8wB6cJbodeYaEb5aD9QYqhdxiS8igfcHpz36oHY7p8 -a 0.002 --sepolia
node bin/cli.js -k private-key.txt --solana-key my-wallet.json -a 0.002 --sepolia
```
-

- The `-k, --key-file` option specifies the path to the Ethereum private key file.
- The `-d, --destination` option specifies the Solana destination address on the rollup (base58 encoded).
- The `--solana-key` option specifies the path to a Solana key.json file (alternative to -d).
- The `-a, --amount` option specifies the amount of Ether to deposit.
- Use `--mainnet` or `--sepolia` to select the network. The tool will use different contract addresses depending on the network.
- The `-r, --rpc-url` option is optional and allows overriding the default JSON RPC URL.
- Use `--mainnet` or `--sepolia` to select the network.

## Security Note

64 changes: 43 additions & 21 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -3,19 +3,16 @@
/**
* Script to deposit funds to the Eclipse rollup.
*
* Usage:
* # To deposit on Sepolia
* node bin/cli.js --address [Solana Address] --amount [Amount Ether] --key-file [Private Key File] --sepolia
*
* # To deposit on Mainnet
* node bin/cli.js --address [Solana Address] --amount [Amount Ether] --key-file [Private Key File] --mainnet
*
* Example on Sepolia:
* > node bin/cli.js --address EAjFK3iWqYdRbCAuDhfCNHo2EMj3S7eg5QrU7DMcNEXD --amount 0.002 --key-file private-key.txt --sepolia
* > Transaction hash: 0x335c067c7280aa3bd2d688cd4c8695c86b3f7fe785be5379c5d98731db0269cf
* Usage with direct address:
* node bin/cli.js -k private-key.txt -d [Solana Address] -a [Amount Ether] --mainnet|--sepolia
*
* Usage with Solana key.json:
* node bin/cli.js -k private-key.txt --solana-key [key.json] -a [Amount Ether] --mainnet|--sepolia
*/
import { Command } from 'commander';
import { runDeposit } from '../src/lib.js'; // Note the `.js` extension
import { runDeposit } from '../src/lib.js';
import fs from 'fs';
import { Keypair } from '@solana/web3.js';

const program = new Command();

@@ -26,7 +23,8 @@ program

program
.description('Deposit Ether into the Eclipse rollup')
.requiredOption('-d, --destination <address>', 'Destination address on the rollup (base58 encoded)')
.option('-d, --destination <address>', 'Destination address on the rollup (base58 encoded)')
.option('--solana-key <path>', 'Path to Solana key.json file (alternative to --destination)')
.requiredOption('-a, --amount <ether>', 'Amount in Ether to deposit')
.option('--mainnet', 'Use Ethereum Mainnet')
.option('--sepolia', 'Use Sepolia test network')
@@ -36,18 +34,42 @@ program
console.error('Error: You must specify either --mainnet or --sepolia');
process.exit(1);
}
let chainName = '';
if (options.mainnet) {
chainName = 'mainnet'
} else if (options.sepolia) {
chainName = 'sepolia'
} else {
throw new Error("Invalid chain name");

if (!options.destination && !options.solanaKey) {
console.error('Error: You must specify either --destination or --solana-key');
process.exit(1);
}

if (options.destination && options.solanaKey) {
console.error('Error: Cannot specify both --destination and --solana-key');
process.exit(1);
}

let destination = options.destination;
if (options.solanaKey) {
try {
// Reads the keypair from the JSON file
const keyData = JSON.parse(fs.readFileSync(options.solanaKey, 'utf8'));

// Creates a Keypair from the array of bytes
const keypair = Keypair.fromSecretKey(new Uint8Array(keyData));

// Gets the public key in base58 format
destination = keypair.publicKey.toBase58();

console.log(`Using Solana public key: ${destination}`);
} catch (error) {
console.error(`Error reading/processing Solana key file: ${error.message}`);
process.exit(1);
}
}

let chainName = options.mainnet ? 'mainnet' : 'sepolia';

runDeposit({
destination: options.destination,
destination,
amount: options.amount,
chainName: chainName,
chainName,
keyFile: options.keyFile
});
});
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -20,13 +20,24 @@
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.8.0",
"@jest/globals": "^29.7.0",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.3.3"
"jest": "^29.7.0",
"prettier": "^3.3.3",
"glob": "^10.3.10"
},
"scripts": {
"lint": "eslint 'src/**/*.js'",
"format": "prettier --write 'src/**/*.js'"
"format": "prettier --write 'src/**/*.js'",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js test/cli.test.js"
},
"jest": {
"transform": {},
"testEnvironment": "node",
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
}
}
Loading