@codegame.dev/wallet-cli
Version:
A CLI tool for managing wallets across multiple blockchains, supporting operations like wallet creation, balance checking, transfers, and fee estimation.
122 lines (121 loc) • 4.17 kB
JavaScript
import { coinGeckoPlatformToTrustWalletCoinMap } from "../assets/coingecko.js";
export const getCoin = (trustCore, coinId, coingeckoPlatform, required = true) => {
const { CoinType } = trustCore;
if (required && !coinId && !coingeckoPlatform) {
return [
null,
{
success: false,
code: 'empty-coin',
message: "Set coin name"
}
];
}
const coinFromCoinGecko = coingeckoPlatform
? coinGeckoPlatformToTrustWalletCoinMap[coingeckoPlatform]
: null;
if (coingeckoPlatform && !coinFromCoinGecko) {
return [
null,
{
success: false,
code: 'invalid-coingecko-platform',
message: "This coingecko platform not supported"
}
];
}
const coin = Object.keys(CoinType).slice(1).find(v => v == (coinFromCoinGecko || coinId));
if (coinId && !coin) {
return [
null,
{
success: false,
code: 'invalid-coin',
message: "This coin is not supported"
}
];
}
return [coin || null, null];
};
export default (program, trustCore) => {
const { HDWallet, CoinType } = trustCore;
program.command('coin-list')
.description('show list of all supported coins')
.option('-s <text>', 'search in the list')
.option('--json', 'output will be in json format')
.action((values) => {
const list = Object.keys(CoinType).slice(1).filter(v => v.toLowerCase().includes(values.s || ''));
if (values.json) {
console.log(JSON.stringify(list));
}
else {
console.log(list.join('\n'));
}
});
program.command('get-address')
.description('get public address of a coin')
.option('--mnemonic <mnemonic>', 'set an existing mnemonic')
.option('--coin <coin>', 'set coin name based on trustwallet core')
.option('--coingecko-platform <platform>', 'set platform name based on coingecko api')
.option('--json', 'output will be in json format')
.action((values) => {
const [coin, error] = getCoin(trustCore, values.coin, values.coingeckoPlatform, true);
if (error || !coin) {
program.error(JSON.stringify({
...error,
json: values.json
}));
}
let mnemonic = values.mnemonic || '';
let address;
const wallet = HDWallet.createWithMnemonic(mnemonic, "");
mnemonic = wallet.mnemonic();
address = wallet.getAddressForCoin(CoinType[coin]);
if (values.json) {
console.log(JSON.stringify({
success: true,
coin,
mnemonic,
address
}));
}
else {
console.log('Address:', address);
}
});
program.command('create-wallet')
.description('create a new mnemonic')
.option('--coin <coin>', 'set coin name based on trustwallet core')
.option('--coingecko-platform <platform>', 'set platform name based on coingecko api')
.option('--json', 'output will be in json format')
.action((values) => {
const [coin, error] = getCoin(trustCore, values.coin, values.coingeckoPlatform, false);
if (error) {
program.error(JSON.stringify({
...error,
json: values.json
}));
}
let mnemonic;
let address = null;
const wallet = HDWallet.create(128, "");
mnemonic = wallet.mnemonic();
if (coin) {
address = wallet.getAddressForCoin(CoinType[coin]);
}
if (values.json) {
console.log(JSON.stringify({
success: true,
coin,
mnemonic,
address
}));
}
else {
console.log('Mnemonic:', mnemonic);
if (address) {
console.log('Address:', address);
}
}
});
};