0

I want to know a way to know the public address used for funds transfer from the private key. (As myetherwallet do when you get your wallet info by entering your private key)

I don't care with wich programming language (even if I prefer javascript, if it's possible).

2

Are you sure you want to write this without using a library? It's practically impossible to do cryptographic stuff securely, even with a background in cryptography.

You have been warned. But to give you as little rope to hang yourself as possible, all you have to do is reimplement a couple of functions described in this blog post. It's up to you to figure out how far you want to go to eliminate libraries (write your own bignum library? Write your program to run in baremetal assembly to avoid using possibly compromised OS libraries?) You can also look at the code used to implement the functionality in a library. Everything you need for Python is open source.

  • It's mostly for educationnal purpose. I won't implement it anywhere but in a test environment. – Gille Q. Sep 25 '17 at 11:36
2

As was said by @lungj it is really not a good idea not to use libraries. Because you need to use elliptic curve cryptography, which rellies heavily on mathematical properties. Also you will need sha3 for hashing.

If you can use the libraries elliptic for elliptic curve cryptography and js-sha3 for sha3 hashing, here's a short example

const assert = require('assert');
const EC = require('elliptic').ec;
const keccak256 = require('js-sha3').keccak256;

async function main() {
  try {
    const ec = new EC('secp256k1');

    const key = ec.keyFromPrivate('208065a247edbe5df4d86fbdc0171303f23a76961be9f6013850dd2bdc759bbb', 'hex');

    const privateKey = key.getPrivate();
    const publicKey = key.getPublic().encode('hex').slice(2);
    assert.equal(publicKey, '836b35a026743e823a90a0ee3b91bf615c6a757e2b60b9e1dc1826fd0dd16106f7bc1e8179f665015f43c6c81f39062fc2086ed849625c06e04697698b21855e');

    const address = keccak256(Buffer.from(publicKey, 'hex')).slice(64 - 40);
    assert.equal(address, '0bed7abd61247635c1973eb38474a2516ed1d884');

    console.log(`Private Key: 0x${privateKey}`);
    console.log(`Public Key: 0x${publicKey}`);
    console.log(`Address: 0x${address.toString()}`);
  } catch (err) {
    console.log(err);
  }
}

main();

Should output

Private Key: 0x14700869030423518308970518253936238282279417230371408086143856620903190600635

Public Key: 0x836b35a026743e823a90a0ee3b91bf615c6a757e2b60b9e1dc1826fd0dd16106f7bc1e8179f665015f43c6c81f39062fc2086ed849625c06e04697698b21855e

Address: 0x0bed7abd61247635c1973eb38474a2516ed1d884

  • 1
    Your try-catch block around main() won't catch any errors, because if main has an error, it will just return a promise that rejects. Instead of the try-catch block, you should use this: main().catch(err => { console.error(err); }); – Macil Sep 26 '17 at 22:59
  • You are right, the try/catch should be inside main(). – Ismael Sep 27 '17 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.