-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add How to Build a Crypto Wallet example (#11)
- Loading branch information
1 parent
8cfda9b
commit 5005eab
Showing
5 changed files
with
643 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
}); |
This file contains 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
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); | ||
}); |
This file contains 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
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); | ||
}); |
This file contains 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
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" | ||
} | ||
} |
Oops, something went wrong.