You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.0 KiB
TypeScript

2 years ago
import { mkdirSync, readdirSync, readFileSync, writeFileSync, rmSync, existsSync } from 'fs'
2 years ago
import { ethers } from 'ethers'
2 years ago
import { Command } from 'commander'
2 years ago
import bip39 from 'bip39'
2 years ago
import inquirer from 'inquirer'
2 years ago
import parse from 'parse-git-config'
import { importActions, generateActions } from './actions.js'
2 years ago
import abis from "../config/abis.js"
import network from "../config/evm-network.js"
2 years ago
const program = new Command()
program
.name('git3')
.description('git3 mangement tool')
.version('0.1.0')
program.command('generate')
.alias('gen')
.alias('new')
.description('generate a cryto wallet to use git3')
.action(() => {
inquirer.prompt(generateActions).then(answers => {
const { keyType, name } = answers
const walletType = keyType === 'private key' ? 'privateKey' : 'mnemonic'
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
2 years ago
2 years ago
if (readdirSync(keyPath).includes(name)) {
console.error(`wallet ${name} already exists`)
return
}
2 years ago
2 years ago
const mnemonic = bip39.generateMnemonic()
const wallet = keyType === 'private key'
? ethers.Wallet.createRandom()
: ethers.Wallet.fromMnemonic(mnemonic)
2 years ago
2 years ago
const content = `${walletType}\n${keyType === 'private key' ? wallet.privateKey : mnemonic}\n`
writeFileSync(`${keyPath}/${name}`, content)
return
2 years ago
})
2 years ago
})
2 years ago
2 years ago
program.command('list', { isDefault: true })
.alias('ls')
.description('list all wallets in user folder ~/.git3/keys')
2 years ago
.option('-r, --raw', 'output raw wallet data with pravate key / mnemonic')
.action(params => {
2 years ago
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
const wallets = readdirSync(keyPath)
if (wallets.length === 0) {
2 years ago
console.log('No wallet found, you can generate one use <git3 new>')
2 years ago
}
wallets.forEach(file => {
const content = readFileSync(`${keyPath}/${file}`).toString()
2 years ago
if (params.raw) {
console.log(`[${file}]`)
console.log(` ${content.split('\n')[0]} - ${content.split('\n')[1]}`)
console.log('\t')
return
}
2 years ago
console.log(`[${file}]`)
2 years ago
const [walletType, key] = content.split('\n')
const etherWallet = walletType === 'privateKey'
? new ethers.Wallet(key)
: ethers.Wallet.fromMnemonic(key)
const address = etherWallet.address
console.log(`address: ${address}`)
console.log('\t')
2 years ago
})
})
program.command('import')
.description('import a wallet from a private key or mnemonic')
.action(() => {
inquirer.prompt(importActions).then(answers => {
const { keyType, key, name } = answers
const walletType = keyType === 'private key' ? 'privateKey' : 'mnemonic'
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
2 years ago
2 years ago
if (readdirSync(keyPath).includes(name)) {
console.error(`wallet ${name} already exists`)
return
}
2 years ago
2 years ago
const content = `${walletType}\n${key}\n`
writeFileSync(`${keyPath}/${name}`, content)
return
2 years ago
})
2 years ago
})
program.command('delete')
.description('delete a wallet')
.action(() => {
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
const wallets = readdirSync(keyPath)
if (wallets.length === 0) {
console.error('No wallet found, you can generate one with `git3 generate`')
return
}
inquirer.prompt([
{
type: 'list',
name: 'wallet',
message: 'Select wallet to delete',
choices: wallets
}
]).then(answers => {
const { wallet } = answers
rmSync(`${keyPath}/${wallet}`)
})
})
program.command('create')
.argument('[wallet]', 'wallet to use', 'default')
.argument('[repo]', 'repo name to create')
.description('create a new repo')
.action((wallet, repo) => {
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
const content = readFileSync(`${keyPath}/${wallet}`).toString()
const [walletType, key] = content.split('\n')
const provider = new ethers.providers.JsonRpcProvider('https://galileo.web3q.io:8545');
let etherWallet = walletType === 'privateKey'
? new ethers.Wallet(key)
: ethers.Wallet.fromMnemonic(key)
etherWallet = etherWallet.connect(provider)
2 years ago
let net = network[3334]
const contract = new ethers.Contract(
2 years ago
net.contracts.git3,
abis.ETHStorage,
2 years ago
etherWallet)
contract.repoNameToOwner(Buffer.from(repo))
2 years ago
.then((res: any) => { console.log(res) })
.catch((err: any) => { console.error(err) })
contract.createRepo(Buffer.from(repo))
2 years ago
.then((res: any) => { console.log(res) })
.catch((err: any) => { console.error(err) })
})
2 years ago
program.command('info')
.argument('[wallet]', 'wallet you want to get info', 'default')
.description('get info of a wallet')
.action(wallet => {
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
2 years ago
2 years ago
const content = readFileSync(`${keyPath}/${wallet}`).toString()
const [walletType, key] = content.split('\n')
const provider = new ethers.providers.JsonRpcProvider('https://galileo.web3q.io:8545');
let etherWallet = walletType === 'privateKey'
? new ethers.Wallet(key)
: ethers.Wallet.fromMnemonic(key)
etherWallet = etherWallet.connect(provider)
const address = etherWallet.address
etherWallet.getBalance()
.then(balance => {
2 years ago
console.log(`wallet: ${wallet}`)
console.log(`address: ${address}`)
console.log(`balance: ${ethers.utils.formatUnits(balance)} eth`)
2 years ago
})
.catch(err => {
console.error(err)
return
})
})
program.command('set-wallet')
.alias('set')
.argument('<git3>', 'git3 remote')
.argument('[wallet]', 'wallet you want to bind', 'default')
.description('bind git3 remotes with a wallet')
.action((git3, wallet) => {
const currentConfig = parse.sync()
const existingRemote = currentConfig[`remote "${git3}"`]
2 years ago
const keyPath = process.env.HOME + "/.git3/keys"
mkdirSync(keyPath, { recursive: true })
2 years ago
if (!existsSync(`${keyPath}/${wallet}`)) {
2 years ago
console.error(`wallet ${wallet} not found, use <git3 new> to generate one`)
return
}
2 years ago
if (existingRemote) {
const newConfig = {
...currentConfig,
[`remote "${git3}"`]: {
...existingRemote,
wallet
}
}
// console.log(newConfig)
// const writer = createWriteStream('config', 'w')
let newConfigText = ''
Object.keys(newConfig).forEach(key => {
newConfigText += `[${key}]\n`
Object.keys(newConfig[key]).forEach(subKey => {
newConfigText += `\t${subKey} = ${newConfig[key][subKey]}\n`
})
})
2 years ago
let path = parse.resolveConfigPath("global") || ""
writeFileSync(path, newConfigText)
2 years ago
} else {
console.error(`remote ${git3} not found`)
console.error('you can add a remote with `git remote add <name> <url>')
}
})
program.parse()