Skip to content

Instantly share code, notes, and snippets.

@JakeLin
Last active March 21, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakeLin/07143172e565fedf37aaf71568f8204e to your computer and use it in GitHub Desktop.
Save JakeLin/07143172e565fedf37aaf71568f8204e to your computer and use it in GitHub Desktop.
const findPrivateKeyForAddress = (privateKeyStr, addressStr) => {
if (typeof privateKeyStr !== 'string') {
throw new TypeError('param privateKeyStr must be of String');
} else if (typeof addressStr !== 'string') {
throw new TypeError('param addressStr must be of String');
}
const hexStr = '0123456789abcdef'
const targetAddress = Buffer.from(addressStr, 'hex')
const privateKeyLength = privateKeyStr.length
// Place the first blue stick from first to the second last of the hex
for (let i = 0; i < privateKeyLength - 1; i++) {
// Swap the first blue stick with a new hex (from `0` to `f`)
for (const c1 of hexStr) {
// Place the second red stick from one right of the placed blue stick the last of the hex
for (let j = i + 1; j < privateKeyLength ; j++) {
// Swap the second red stick with a new hex (from `0` to `f`)
for (const c2 of hexStr) {
const privateKeyInChars = privateKeyStr.split('')
privateKeyInChars[i] = c1
privateKeyInChars[j] = c2
// Generate the Ethereum address from the swapped private key
const privateKey = Buffer.from(privateKeyInChars.join(''), 'hex')
const address = getAddressFromPrivateKey(privateKey)
// Compare the generated address with the target address
if (address.equals(targetAddress)) {
// Bingo, we found it.
console.log('💥💥💥 PRIVATE KEY FOUND 💥💥💥')
return privateKey.toString('hex')
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment