🌳
pt0/donateapp/binF/ethwalF.mjs
1import fs from 'fs'
2import { HDNodeWallet } from 'ethers'
8const donateEthwalSecretName = 'millionpx_ethwal'
9const donateEthwalSecretPath = pathDownJoin(secretsDir, donateEthwalSecretName)
11const getOrCreateMnemonic = ({createIfMissing = true} = {}) => {
12 try {
13 return fs.readFileSync(donateEthwalSecretPath, 'utf-8').trim()
14 } catch (err) {
15 if (/** @type {any} */ (err).code !== 'ENOENT') throw err
16 if (!createIfMissing) return null
17 const wallet = HDNodeWallet.createRandom()
18 const {mnemonic} = wallet
19 assertDefined(mnemonic, {walletAddr: wallet.address})
20 fs.mkdirSync(secretsDir, {recursive: true})
21 fs.writeFileSync(donateEthwalSecretPath, mnemonic.phrase, 'utf-8')
22 console.log(`Created new donate wallet secret at ${shortPtPath(donateEthwalSecretPath)}`)
23 return mnemonic.phrase
24 }
27export const genDonateWallet = (opts) => {
28 const mnemonic = getOrCreateMnemonic(opts)
29 return mnemonic ? HDNodeWallet.fromPhrase(mnemonic) : null
32export const genDonateEthPay = (opts) => {
33 const wallet = genDonateWallet(opts)
34 if (!wallet) return { ethPaymentAddr: null, ethPaymentAddrA: [] }
35 const ethPaymentAddr = wallet.address
36 return { ethPaymentAddr, ethPaymentAddrA: [ethPaymentAddr] }