#!/usr/bin/env node /** * JesusOS MAIN · original Android Termux · BTC ACCESSIBLE CREATOR V6. * Fixes V1 screen clearing AND V5 rejecting the natural phrase 'back up'. * * No automatic screen clear; recovery words remain visible on original private * Termux display until USER decides to clear them AFTER handwriting and * verification. No wallet is saved before owner confirms BACK UP, 3 checked words, * and SAVE. Refuses to overwrite existing watch.json (even symlink). * * No network, mining changes, external login, server API, seed on disk, * signing, spending, automatic pool binding, or screenshots. * Requires locally installed @scure/bip39, @scure/bip32, @scure/base, @noble. * This is an experimental receive-only wallet; no independent security audit. */ import fs from 'node:fs'; import path from 'node:path'; import {createInterface} from 'node:readline/promises'; import {generateMnemonic,validateMnemonic,mnemonicToSeedSync} from '@scure/bip39'; import {wordlist} from '@scure/bip39/wordlists/english.js'; import {HDKey} from '@scure/bip32'; import {bech32} from '@scure/base'; import {sha256} from '@noble/hashes/sha2.js'; import {ripemd160} from '@noble/hashes/legacy.js'; const HOME=process.env.HOME||''; const folder=path.join(HOME,'jesusos-sovereign-wallet'); const manifest=path.join(folder,'watch.json'); const rootPath="m/84'/0'/0'"; const mode=process.argv[2]||'--help'; function status(){ if(!HOME||HOME==='/')throw Error('Original Termux HOME unavailable.'); const directory=fs.lstatSync(folder); if(directory.isSymbolicLink()||!directory.isDirectory()||(directory.mode&0o077)!==0) throw Error('Original wallet directory must be owner-only and not a symlink.'); try{ const file=fs.lstatSync(manifest); if(file.isSymbolicLink()||!file.isFile()||(file.mode&0o077)!==0||file.size>8192) throw Error('Original watch-only wallet exists but requires security review; not overwriting.'); const obj=JSON.parse(fs.readFileSync(manifest,'utf8')); if(obj.schema!=='jesusos.btc.watchonly.bip84.v1'||obj.path!==rootPath||obj.network!=='mainnet'|| typeof obj.accountXpub!=='string'||typeof obj.firstAddress!=='string') throw Error('Original wallet manifest does not match expected format.'); const key=HDKey.fromExtendedKey(obj.accountXpub); const expected=deriveReceive(key,0); if(key.privateKey||obj.firstAddress!==expected)throw Error('Existing public wallet xpub and receiving address do not match.'); return {state:'EXISTING_VALID_WATCH_ONLY_WALLET',maskedAddress:expected.slice(0,7)+'…'+expected.slice(-6), backupVerified:typeof obj.backupVerifiedAt==='string',nextIndex:obj.nextIndex}; }catch(e){ if(e.code==='ENOENT')return {state:'NO_PUBLIC_WALLET_MANIFEST'}; throw e; } } function deriveReceive(publicKey,index){ const k=publicKey.derive('m/0/'+index).publicKey; if(!k||k.length!==33)throw Error('BIP84 compressed public key not available.'); return bech32.encode('bc',[0,...bech32.toWords(ripemd160(sha256(k)))]); } function deriveMnemonic(mnemonic){ if(!validateMnemonic(mnemonic,wordlist)||mnemonic.split(' ').length!==24) throw Error('Generated 24-word BIP39 checksum invalid'); const seed=mnemonicToSeedSync(mnemonic); try{ const account=HDKey.fromMasterSeed(seed).derive(rootPath); const xpub=account.publicExtendedKey; return {xpub,firstAddress:deriveReceive(HDKey.fromExtendedKey(xpub),0)}; }finally{seed.fill(0)} } function checkPublishedVector(){ const phrase='abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; const seed=mnemonicToSeedSync(phrase); try{ const pub=HDKey.fromMasterSeed(seed).derive(rootPath).publicExtendedKey; const zero=deriveReceive(HDKey.fromExtendedKey(pub),0); if(zero!=='bc1qcr8te4kr609gcawutmrza0j4xv80jy8z306fyu') throw Error('Official BIP84 public vector mismatch'); }finally{seed.fill(0)} console.log('PASS: published BIP84 mainnet receiving-address test vector. NO wallet generated.'); } function storeWatch(obj){ const temp=path.join(folder,'.btc-v5-'+process.pid+'.tmp'); let fd; try{ fd=fs.openSync(temp,'wx',0o600); fs.writeFileSync(fd,JSON.stringify(obj,null,2)+'\n'); fs.fsyncSync(fd); fs.closeSync(fd);fd=undefined; // Atomic link creates watch.json only if absent; NEVER replace an // existing wallet, even if it appeared while owner was handwriting. fs.linkSync(temp,manifest); }finally{ if(fd!==undefined)fs.closeSync(fd); try{fs.unlinkSync(temp)}catch{} } } async function create(){ if(!process.stdin.isTTY||!process.stdout.isTTY){ console.error('ORIGINAL interactive Android Termux session required. No wallet created.'); process.exitCode=2;return; } const initial=status(); if(initial.state==='EXISTING_VALID_WATCH_ONLY_WALLET'){ console.log('STOP: a valid original wallet is already present. Nothing generated or overwritten.'); console.log('Its first PUBLIC address: '+initial.maskedAddress); console.log('Use V2 paper recovery verification or V4 watch-only audit; never replace existing keys.'); return; } checkPublishedVector(); console.log('\n=== JESUSOS · ACCESSIBLE BTC WALLET · V6 ==='); console.log('The old script automatically cleared the screen on cancel. THIS version NEVER clears it.'); console.log('Recovery words are displayed only on the PRIVATE original Termux terminal.'); console.log('To make the words EASIER TO READ, spread two fingers apart on the Termux screen to ZOOM IN.'); console.log('This is an unaudited RECEIVE-ONLY wallet. Do not deposit significant funds.'); console.log('No exchange login, server custody, fees, Bitcoin sending, or mining changes.'); const rl=createInterface({input:process.stdin,output:process.stdout}); let mnemonic='', derived; let created=false; try{ const ready=(await rl.question('Have PAPER ready, no screenshots? Type CREATE to generate words, or press Enter to cancel: ')).trim().toLowerCase(); if(!['create','start','new'].includes(ready)){console.log('CANCELED BEFORE GENERATION. No wallet or phrase created. Screen stays visible.');return} mnemonic=generateMnemonic(wordlist,256); derived=deriveMnemonic(mnemonic); const words=mnemonic.split(' '); console.log('\n'); console.log('============================================================='); console.log(' PRIVATE JESUSOS BITCOIN RECOVERY WORDS — WRITE ON PAPER NOW'); console.log('============================================================='); for(let i=0;i