Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Everything about working with the Ethereum blockchain is found here at DecypherTV videos

These are notes for the second video

Install Node

Go to nodejs.org and install the latest version of Node.
This will also install NPM.

Install the following packages into the working directory.

Method 1

At the commandline cd into the working directory, type the following and hit enter for each command.
npm init Then follow the prompts to create a package.json file.
npm install web3 --save
npm install ethereumjs-util --save
These will install the latest versions of the packages

Method 2: Jordan recomends the following install proceedure and specifies the version so that we are all working with the exact same package. I had a bit of trouble with this method so I went with the method 1.

Create a file with the following contents, name it "package.json", and save it in the working directory.

{
  "dependencies": {
    "web3": "0.17.0-alpha",
    "ethereumjs-util": "4.5.0"
  }
}

At the commandline cd into the working directory, type the following and hit enter. npm install

Working with Node at the command line

Repeat string function "a123".repeat(16)

Clearing the Node console ctrl + L

Initializing the Environment

At the OS commandline start the Node console. Type the following and hit Enter.

node

You are no longer interacting with your operating system but rather with the NodeJS JavaScript command line interpreter.

Make Node aware of the web3 library. Type the following and hit Enter.

var Web3 = require("web3");

Create a handle for accessing objects in the web3 library. Type the following and hit Enter.

var web3 = new Web3;

Type web3 at the node commandline and hit Enter to see the API and confirm that Node can use the library.

Obtaining the sha3 hash of any text

Continuing at the node console type the following and hit Enter.
web3.sha3("password")
The following 64 character sha3 hash will be the result: '0xb68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b'
If a character string different than password is used, a different sha3 hash will result.

Notes about the sha3 hash function

  • The sha3 hash function takes a text string of any length as input and produces a character string 64 characters long of the characters 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.
  • One use is to prove that two documents are the same. This is accomplished by taking the sha3 hash of two documents and comparing the hashes if they are the same then the documents are the same.
  • Another use is to prove that a document sent is the same as that received. This is accomplished by taking the sha3 hash of a document and then distributing the hash first via a secure and separate means than that used to transmit the document. Then the user hashes the received document and compares the hash generated to the hash recieved by the sender. If the hashes are the same then the document received is the same as the document sent and has not been switched out by a bad actor.
  • Another use is creating a brain wallet. This is a system for creating passwords that are easy to remember and so do not need to be writen down. The trick is to sha3 hash a phrase. In this way only the phrase need be remembered and the private key can be hashed from the phrase when needed. Caution: Think of a very obscure phrase or you will likely get hacked and lose all your ether.

Notes about keys

Private Keys
  • An Ethereum private Key is a 64 character string using the following characters 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.
  • The key is not case sensitive so AAAA... and aaaa... will function as the same private key.
  • If other characters are used or if the string is less than 64 characters in length a security risk is exposed as explained near the end of this document. For this reason it would be wise to run any private key candidate through the sha3 hashing function in order to ensure the proper length and that only valid characters are used.
Public Keys
  • An Ethereum public key is determined by a function (see further below)
  • The public key is a hexadecimal number produced by the function (Only 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f)
  • An Ethereum public address or wallet address is the last 40 digits produced by the function.
  • 0x in front of a public address denotes that the address is in hexadecimal format - It is not part of the number.
  • Given the private key you can always get the wallet address but the reverse is not possible. For this reason, it should only be necessary to store the private key.
  • It must be possible for several different private keys to unlock a particular public wallet although they must be nearly impossible to find, even with quantum computer (according to the NIST). This is because while it is possible to derive an unknown public address from a private key, only trial and error over a bizilion private keys would be required to find one that corresponds to a particular public address.

Interacting with NodeJS Using Scripts and Command line Arguments

Create a file in your working directory called ArgTest.js for this example.
Paste the following JavaScript command into the file and save it.

console.log(\`0:${process.argv[0]}\n1:${process.argv[1]}\n2:${process.argv[2]}\`)

Run the script from the operating system command prompt (not from the node console).
The command to run the script is as follows:
node ./ArgTest.js Hello
"ArgTest.js" is the name of the script we want Node to run.
The "./" tells the command line interpreter to look in the current directory.
"Hello" is the third argument produced at the output (shown below) even though it was the second argument given at the input.
The first two output arguments (Node location, and file name with full path) are always defaulted in by Node.

The output is a follows:

0:C:\Program Files\nodejs\node.exe
1:C:\Users\John\BlockChain\Ethereum\WorkFolder\Decypher_TV\KeyPairs\ArgTest.js
2:Hello

Complete JavaScript program for getting a wallet address from a private key.

var EthUtil = require("ethereumjs-util")

//The following is a helper function that turns a hex value into a byte array.
var hexToBytes = function(hex) {
    for (var bytes = [], c = 0; c < hex.length; c += 2)
    bytes.push(parseInt(hex.substr(c, 2), 16));
    return bytes;
}

var privateKeyToAddress = function(privateKey) {
    return `0x${EthUtil.privateToAddress(hexToBytes(privateKey)).toString("hex")}`
}

console.log(privateKeyToAddress(process.argv[2]))

In this case the program file was named keygen.js and was called at the OS command line prompt (not the node console) as follows:
node ./keygen.js b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b
The private key used as the argument is the sha3 hash of the word password

Here is an alternate version of the program proposed by a fellow student @alwayssoda

var EthUtil = require("ethereumjs-util")

var pkey = new Buffer(process.argv[2],"hex");

var privateKeyToAddress = function(privateKey) {
    return `0x${EthUtil.privateToAddress(pkey).toString('hex')}`
}

console.log(privateKeyToAddress(process.argv[2]))

Use the following version if running on a raspberry pi:

var EthUtil = require("ethereumjs-util")

var pkey = new Buffer(process.argv[2],"hex");

var privateKeyToAddress = function(privateKey) {
    return "0x" + EthUtil.privateToAddress(pkey).toString('hex')
}

console.log(privateKeyToAddress(process.argv[2]))

Security Issue: Be sure to generate your private key with the sha3 hashing function

If you change any character in the argument a different wallet address should result but this program does produce a different result when the last character is changed.
The following is the sha3 hash of password
b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b
It produces the following wallet address when run through the keygen program:
0x9d39856f91822ff0bdc2e234bb0d40124a201677
The following arguments all produce the same wallet address. Notice that the strings are the same as above except that the last character has either been deleted or changed to something other than 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.

b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976  
b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976z  
b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976?  

The wallet address produced by the above private keys is as follows: 0x34c7588801dfc55fc1143c78a680e0c3f2ba8a3a
This is a security problem. To ensure that a unique wallet address is produced by a private key make sure that the private key is exactly 64 characters long and that it only uses the characters 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f. This is accomplished by using the sha3 hash function to produce the private key.

Importing a Private Key into your keystore.

For local blockchain users

Now that you have made a brain wallet - a private key which is hard to guess but easy to remember, you will want to import it into the local keystore so that you can use it.
Directions can be found here
I will summarize as follows:
Take the sha3 hash of any phrase.
The phrase I used is "password".
The hash produced is as follows:
'0xb68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b'
Delete the first two characters of the result because they are not part of the hash but rather only denote that the result is a hex value.
So now we have the following:
'b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b'
Next, copy and paste the above character string in to a new file called "DeleteThisFile.txt".
Save this file in the same folder where geth.exe is located.
Make sure you delete this file after you complete the import process because it contains your private key in an unencrypted format and you don't want other people to find it.
Now change the --datadir parameter of the following command so as to point to the top folder which holds all the folders accociated with your blockchain. I am talking about the folder that holds the chaindata, dapp, and keystore folders.
Change the --networkid parameter if you want to but use a two digit number because lower numbers are reserved for public blockchains.
Change the --identity parameter if you want to.
Next, execute the command at your operating system command prompt (not the Node console).

./geth --networkid 1100 --identity JohnsComputer --nodiscover --nat none --datadir C:\Users\John\BlockChain\TestChainDataFiles account import ./DeleteThisFile.txt

You will be prompted for a password.
It doesn't have to be the brain wallet phrase you used to generate the private key.
You can enter any password you want to use.
You will be prompted to repeat the password you just entered.
The operating system console will output your public address or public key.
Save this public address somewhere - you will be using it.
Don't worry if you lose it however because you can generate the public address again with the sha3 hash of your brain wallet phrase using the JavaScript file "keygen.js" show above in these notes.
This process will add an extra file in your keystore folder which contains an encrypted version of your private key.

Changing your password

Look in the keystore folder and notice that the names of each file contain a public address and a time stamp. These files hold the private key in an encrypted format. Back up these files if you do not have a brain wallet that you can use to regenerate the private key.
Change the --datadir parameter of the following command so as to point to the top folder which holds all the folders accociated with your blockchain. I am talking about the folder that holds the chaindata, dapp, and keystore folders.
Change the --networkid parameter if you want to but use a two digit number because lower numbers are reserved for public blockchains.
Change the --identity parameter if you want to.
Next, execute the command at the operating system console (not the Node console).

./geth --networkid 1100 --identity JohnsComputer --nodiscover --nat none --datadir C:\Users\John\BlockChain\TestChainDataFiles account

The console will output a list of accounts to reflect all the keys in your keystore folder and it will output the paths to each key file in the keystore folder.
Notice that each account in the output has a handle such as Account#0, Account#1 ...
Look carefully in the listing for the public key you wish to change and take careful note of it's handle.
Important! Change the following command as before but most importantly - change the very last parameter to reflect the index of the handle.

./geth --networkid 1100 --identity JohnsComputer --nodiscover --nat none --datadir C:\Users\John\BlockChain\TestChainDataFiles account update 3

The console will ask you for the current password.
Then the console will ask you for the new password.
Finally the console will ask you to repeat the new password.

Notes on Video 4 in the Series Are Found Here
More Resources for learning Etherum Are Found Here

@johnshearing

This comment has been minimized.

Copy link
Owner Author

commented Feb 19, 2017

Code blocks with back ticks and syntax highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.