{"size": 1447, "ext": "js", "max_stars_repo_path": "getBtcAddress.js", "max_stars_repo_name": "EuroHsu/keytool", "max_stars_repo_stars_event_min_datetime": "2020-04-05T08:02:26.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-28T06:04:32.000Z", "max_issues_repo_path": "getBtcAddress.js", "max_issues_repo_name": "EuroHsu/keytool", "max_issues_repo_issues_event_min_datetime": "2020-10-27T07:18:07.000Z", "max_issues_repo_issues_event_max_datetime": "2020-10-27T07:18:07.000Z", "max_forks_repo_path": "getBtcAddress.js", "max_forks_repo_name": "EuroHsu/keytool", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 43.8484848485, "max_line_length": 102, "alphanum_fraction": 0.7118175536}
const argv = require('minimist')(process.argv.slice(2), { string: ['test', 'key', 'p2sh', 'bech32'] })
const bitcoin = require('bitcoinjs-lib')
const Bech32 = require('bech32')
const crypto = require('crypto')
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
const ask = question => new Promise(resolve => rl.question(question, resolve))

const test = Object.prototype.hasOwnProperty.call(argv, 'test')
const network = test ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
const p2sh = Object.prototype.hasOwnProperty.call(argv, 'p2sh')
const bech32 = Object.prototype.hasOwnProperty.call(argv, 'bech32')
let privKey = argv.key
const init = async () => {
  if (!privKey) privKey = await ask('請輸入私鑰: ')
  const keyPair = new bitcoin.ECPair.fromWIF(privKey, network)
  if (bech32) {
    const hash256 = crypto.createHash('sha256').update(keyPair.publicKey).digest()
    const hash160 = crypto.createHash('ripemd160').update(hash256).digest()
    const words = Bech32.toWords(hash160)
    words.unshift(0)
    const encode = test ? 'tb' : 'bc'
    console.log(Bech32.encode(encode, words))
  } else if (p2sh) {
    const p2wpkh = bitcoin.payments.p2wpkh({pubkey: keyPair.publicKey, network})
    console.log(bitcoin.payments.p2sh({redeem: p2wpkh, network}).address)
  } else {
    console.log(bitcoin.payments.p2pkh({ network, pubkey: keyPair.publicKey }).address)
  }
  process.exit(0)
}

init()
