Last active
March 21, 2018 22:42
-
-
Save JakeLin/07143172e565fedf37aaf71568f8204e to your computer and use it in GitHub Desktop.
Ethereum Riddle - https://github.com/JakeLin/ethereum-riddle
This file contains hidden or 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
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