Last active
March 17, 2020 20:01
-
-
Save bitgord/7cc3b4269b22765613a1340d6695865e to your computer and use it in GitHub Desktop.
Create a bitcoin address with Nodejs and Bitcoinjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You must have node and npm downloaded on your computer | |
// Download bitcoinjs library | |
npm install bitcoinjs-lib | |
// Require bitcoinjs-lib | |
var bitcoin = require("bitcoinjs-lib"); | |
// Make variable for keyPair | |
var keyPair = bitcoin.ECPair.makeRandom(); | |
// Test by logging address to console and save to variable // A valid bitcoin address should be returned | |
console.log(keyPair.getAddress()); | |
var address = keyPair.getAddress(); | |
// Console Log Wallet Import Format / Private Key and save to variable (WIF) | |
console.log(keyPair.toWIF()); | |
var pkey = keyPair.toWIF(); | |
// Create a custom bitcoin address | |
var bitcoin = require("bitcoinjs-lib"); | |
var hit = 0; // When it hits the correct custom address | |
var tryN = 0; // Number of tries | |
while(hit < 1) // Test addresses | |
{ | |
var keyPair = bitcoin.ECPair.makeRandom(); | |
var address = keyPair.getAddress(); | |
var pkey = keyPair.toWIF(); | |
var custom = address.substring(0,3); // First 3 characters will be custom | |
console.log(tryN + " " + custom); | |
if(custom == 1Di" || custom == "1D1"){ | |
console.log(address + " " + pkey); | |
hit = 2 // So that the while loop stops | |
} | |
tryN++; | |
} | |
Hey you just forgot a quotation mark before 1Di on line 32
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @bitgord, did you have an idea of how big the Bitcoinjs module is?