Skip to content

Commit

Permalink
Add How to Build a Crypto Wallet example (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejrakic authored Jun 7, 2022
1 parent 8cfda9b commit 5005eab
Show file tree
Hide file tree
Showing 5 changed files with 643 additions and 0 deletions.
67 changes: 67 additions & 0 deletions my-crypto-wallet/01_newAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const {
generateMnemonic,
mnemonicToEntropy,
} = require("ethereum-cryptography/bip39");
const { wordlist } = require("ethereum-cryptography/bip39/wordlists/english");
const { HDKey } = require("ethereum-cryptography/hdkey");
const { getPublicKey, ecdsaSign } = require("ethereum-cryptography/secp256k1");
const { keccak256 } = require("ethereum-cryptography/keccak");
const { bytesToHex } = require("ethereum-cryptography/utils");
const { writeFileSync } = require("fs");

function _generateMnemonic() {
const strength = 256; // 256 bits, 24 words; default is 128 bits, 12 words
const mnemonic = generateMnemonic(wordlist, strength);
const entropy = mnemonicToEntropy(mnemonic, wordlist);
return { mnemonic, entropy };
}

function _getHdRootKey(_mnemonic) {
return HDKey.fromMasterSeed(_mnemonic);
}

function _generatePrivateKey(_hdRootKey, _accountIndex) {
return _hdRootKey.deriveChild(_accountIndex).privateKey;
}

function _getPublicKey(_privateKey) {
return getPublicKey(_privateKey);
}

function _getEthAddress(_publicKey) {
return keccak256(_publicKey).slice(-20);
}

function _store(_privateKey, _publicKey, _address) {
const accountOne = {
privateKey: _privateKey,
publicKey: _publicKey,
address: _address,
};
const accountOneData = JSON.stringify(accountOne);
writeFileSync("account 2.json", accountOneData);
}

async function main() {
const { mnemonic, entropy } = _generateMnemonic();
console.log(`WARNING! Never disclose your Seed Phrase:\n ${mnemonic}`);

const hdRootKey = _getHdRootKey(entropy);

const accountOneIndex = 0;
const accountOnePrivateKey = _generatePrivateKey(hdRootKey, accountOneIndex);

const accountOnePublicKey = _getPublicKey(accountOnePrivateKey);

const accountOneAddress = _getEthAddress(accountOnePublicKey);
console.log(`Account One Wallet Address: 0x${bytesToHex(accountOneAddress)}`);

_store(accountOnePrivateKey, accountOnePublicKey, accountOneAddress);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
31 changes: 31 additions & 0 deletions my-crypto-wallet/02_restoreWallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { mnemonicToEntropy } = require("ethereum-cryptography/bip39");
const { wordlist } = require("ethereum-cryptography/bip39/wordlists/english");
const { HDKey } = require("ethereum-cryptography/hdkey");
const { getPublicKey } = require("ethereum-cryptography/secp256k1");
const { keccak256 } = require("ethereum-cryptography/keccak");
const { bytesToHex } = require("ethereum-cryptography/utils");
const { writeFileSync } = require("fs");

async function main(_mnemonic) {
const entropy = mnemonicToEntropy(_mnemonic, wordlist);
const hdRootKey = HDKey.fromMasterSeed(entropy);
const privateKey = hdRootKey.deriveChild(0).privateKey;
const publicKey = getPublicKey(privateKey);
const address = keccak256(publicKey).slice(-20);
console.log(`Account One Wallet Address: 0x${bytesToHex(address)}`);

const accountOne = {
privateKey: privateKey,
publicKey: publicKey,
address: address,
};
const accountOneData = JSON.stringify(accountOne);
writeFileSync("account 1.json", accountOneData);
}

main(process.argv[2])
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
28 changes: 28 additions & 0 deletions my-crypto-wallet/03_send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { getDefaultProvider, Wallet, utils } = require("ethers");
const { readFileSync } = require("fs");

async function main(_receiverAddress, _ethAmount) {
const network = "goerli";
const provider = getDefaultProvider(network);

const accountRawData = readFileSync("account 1.json", "utf8");
const accountData = JSON.parse(accountRawData);

const privateKey = Object.values(accountData.privateKey);

const signer = new Wallet(privateKey, provider);

const transaction = await signer.sendTransaction({
to: _receiverAddress,
value: utils.parseEther(_ethAmount),
});

console.log(transaction);
}

main(process.argv[2], process.argv[3])
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
10 changes: 10 additions & 0 deletions my-crypto-wallet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "my-crypto-wallet",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ethereum-cryptography": "^1.0.3",
"ethers": "^5.6.8"
}
}
Loading

0 comments on commit 5005eab

Please sign in to comment.