The original experiment generated a key pair, constructed legacy and wrapped-SegWit addresses, and queried an explorer for each address. Those are separate operations: generating a key locally does not require an explorer to validate ownership.
The earlier dependencies
npm i bitcoinjs-lib --save
npm i request --save
npm i request-promise-native --save
npm i @types/bitcoinjs-lib --save-dev
npm i @types/request --save-dev
npm i @types/request-promise-native --save-dev
The recorded implementation
/**
* Generate new test wallet address
* Test faucet:
* https://coinfaucet.eu/en/btc-testnet/
*/
async generateWallet(isTestNet: boolean = true) {
console.log(`Generate ${isTestNet ? 'Test' : 'Main'} Wallet`);
const {net, api} = isTestNet
? {net: bitcoin.networks.testnet, api: 'https://test-insight.swap.online/insight-api/addr/'}
: {net: bitcoin.networks.bitcoin, api: 'https://blockchain.info/rawaddr/'};
const keyPair = bitcoin.ECPair.makeRandom({network: net});
console.log(`WIF: ${keyPair.toWIF()}`);
const legacyAddress = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey, network: net}).address;
console.log(`Legacy address: ${legacyAddress}`);
const legacyAddressInfo = await this.getApi(api + legacyAddress);
console.log(`Legacy address info:`, JSON.parse(legacyAddressInfo));
const segwitAddress = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({pubkey: keyPair.publicKey, network: net}),
network: net
}).address;
console.log(`SegWit address: ${segwitAddress}`);
const segwitAddressInfo = await this.getApi(api + segwitAddress);
console.log(`SegWit address info:`, JSON.parse(segwitAddressInfo));
// WIF: REDACTED_RECORDED_TESTNET_WIF
// Legacy address: n4SDWuBPAzS9cDzxLu7jsKfuX23zEH1xQM
// Legacy address info: {
// addrStr: 'n4SDWuBPAzS9cDzxLu7jsKfuX23zEH1xQM',
// balance: 0,
// balanceSat: 0,
// totalReceived: 0,
// totalReceivedSat: 0,
// totalSent: 0,
// totalSentSat: 0,
// unconfirmedBalance: 0,
// unconfirmedBalanceSat: 0,
// unconfirmedTxApperances: 0,
// txApperances: 0,
// transactions: []
// }
// SegWit address: 2NCEaH3KH9uzvsB9PpYkBzQSJKZVwLpGVXn
// SegWit address info: {
// addrStr: '2NCEaH3KH9uzvsB9PpYkBzQSJKZVwLpGVXn',
// balance: 0,
// balanceSat: 0,
// totalReceived: 0,
// totalReceivedSat: 0,
// totalSent: 0,
// totalSentSat: 0,
// unconfirmedBalance: 0,
// unconfirmedBalanceSat: 0,
// unconfirmedTxApperances: 0,
// txApperances: 0,
// transactions: []
// }
}
The getApi helper and bitcoin import come from surrounding code. The second address is P2SH-wrapped P2WPKH, not native SegWit.
The old implementation prints its WIF; that exposes signing material to logs. The recorded WIF in the comment has been removed. Explorer endpoints, response shapes, and the older bitcoin.ECPair API are not verified as current. No wallet or network request was created by executing this code.
Source: original Gist revision.