forked from ConsenSys-Academy/ethereum-address-generator-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
110 lines (94 loc) · 2.53 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Add imports here
const BIP39 = require("bip39");
const hdkey = require("ethereumjs-wallet/hdkey");
const Wallet = require("ethereumjs-wallet");
const keccak256 = require("js-sha3").keccak256;
const EthereumTx = require("ethereumjs-tx");
// Add functions here
const generateMnemonic = () => {
return BIP39.generateMnemonic();
};
let Mnemonic = generateMnemonic();
console.log(Mnemonic);
const isValidMnemonic = BIP39.validateMnemonic(Mnemonic);
if (isValidMnemonic) {
console.log("The mnemonic is a valid mnemonic");
}
const generateSeed = (mnemonic) => {
return BIP39.mnemonicToSeed(mnemonic);
};
let seed = generateSeed(Mnemonic);
console.log(seed);
const generatePrivKey = (mnemonic) => {
let seed = generateSeed(mnemonic);
return hdkey
.fromMasterSeed(seed)
.derivePath(`m/44'/60'/0'/0/0`)
.getWallet()
.getPrivateKey();
};
let privateKey = generatePrivKey(Mnemonic);
console.log(privateKey);
const derivePubKey = (privKey) => {
let wallet = Wallet.fromPrivateKey(privKey);
return wallet.getPublicKey();
};
const deriveEthAddress = (pubKey) => {
let Address = keccak256(pubKey);
return "0x" + Address.substring(Address.length - 40, Address.length);
};
const signTx = (privKey, txData) => {
let tx = new EthereumTx(txData);
tx.sign(privKey);
return tx;
};
const getSignerAddress = (signedTx) => {
return "0x" + signedTx.getSenderAddress().toString("hex");
};
/*
Do not edit code below this line.
*/
var mnemonicVue = new Vue({
el: "#app",
data: {
mnemonic: "",
privKey: "",
pubKey: "",
ETHaddress: "",
sampleTransaction: {
nonce: "0x00",
gasPrice: "0x09184e72a000",
gasLimit: "0x2710",
to: "0x31c1c0fec59ceb9cbe6ec474c31c1dc5b66555b6",
value: "0x10",
data: "0x7f7465737432000000000000000000000000000000000000000000000000000000600057",
chainId: 3,
},
signedSample: {},
recoveredAddress: "",
},
methods: {
generateNew: function () {
this.mnemonic = generateMnemonic();
},
signSampleTx: function () {
this.signedSample = signTx(this.privKey, this.sampleTransaction);
console.log("signed Sample", this.signedSample);
},
},
watch: {
mnemonic: function (val) {
this.privKey = generatePrivKey(val);
},
privKey: function (val) {
this.pubKey = derivePubKey(val);
},
pubKey: function (val) {
this.ETHaddress = deriveEthAddress(val);
this.recoveredAddress = "";
},
signedSample: function (val) {
this.recoveredAddress = getSignerAddress(val);
},
},
});