Skip to content

Instantly share code, notes, and snippets.

@mgiachetti
Last active January 22, 2018 14:02
Show Gist options
  • Save mgiachetti/30ebe495b41db1c24fa034fbb0564188 to your computer and use it in GitHub Desktop.
Save mgiachetti/30ebe495b41db1c24fa034fbb0564188 to your computer and use it in GitHub Desktop.
Paper wallets generator. Gift cards. (Warning: wallets.json is overwritten in every run and creates new wallets. Be carefull and save it before the next run)
// 100,000,000 satoshi = 1 btc
// 100,000 satoshi = 0.001 btc = 1e5
const bitcoin = require('bitcoinjs-lib');
const network = bitcoin.networks.bitcoin;
const blockchainInfoNetwork = network === bitcoin.networks.bitcoin ? 0 : 3; // bitcoin or testnet
const blockexplorer = require('blockchain.info/blockexplorer').usingNetwork(blockchainInfoNetwork);
const pushtx = require('blockchain.info/pushtx').usingNetwork(blockchainInfoNetwork);
const sourceWIF = ''; // put private key here
const sourceWallet = bitcoin.ECPair.fromWIF(sourceWIF, network);
const sourceAddr = sourceWallet.getAddress();
const wallets = [];
for (let i = 0; i < 400; i++) {
wallets.push(bitcoin.ECPair.makeRandom({ network }));
}
require('fs').writeFile("./wallets.json", JSON.stringify(wallets.map(w => w.toWIF())), (err) => {
if (!err) {
console.log('wallets.json saved');
} else {
console.log('Cannot save wallets.json');
}
});
const amount = 1e5; // 1mBTC
const fee = 6e5;
const amountNeeded = wallets.length * amount + fee;
let transactionAmount = 0;
blockexplorer.getAddress(sourceAddr).then(res => {
var txb = new bitcoin.TransactionBuilder(network);
let inputSize = 0;
let index = 0;
while (transactionAmount < amountNeeded && index < res.txs.length) {
let tx = res.txs[index++];
let out = tx.out.find(o => o.addr === sourceAddr);
if (out.spent) {
continue;
}
console.log(`addIput(${tx.hash}, ${out.n}) => amount ${out.value}`);
txb.addInput(tx.hash, out.n);
transactionAmount += out.value;
inputSize++;
}
if (transactionAmount < amountNeeded) {
console.log(`ERROR: Not enought amount!\navailable${transactionAmount}\nneeded: ${amountNeeded}`);
return;
}
wallets.forEach(w => {
txb.addOutput(w.getAddress(), amount);
});
txb.addOutput(sourceAddr, transactionAmount - amountNeeded); // transfer the rest of the transaction to the original wallet
for (let i = 0; i < inputSize; i++) {
txb.sign(i, sourceWallet);
}
var tx = txb.build();
console.log('length: ', tx.toHex().length / 2);
// console.log(tx.toHex());
pushtx.pushtx(tx.toHex()).then(res => {
console.log('TRANSFER Complete', res);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment