-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
100 lines (85 loc) · 2.74 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
// 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
// Generate a random mnemonic (uses crypto.randomBytes under the hood), defaults to 128-bits of entropy
function generateMnemonic(){
return BIP39.generateMnemonic();
}
//validate if a string of characters is a valid mnemonics
var isValid = BIP39.validateMnemonic("Enter your mnemonic here");
// this will return false because "Enter your mnemonic here" is not a valid phrase
function generateSeed(mnemonic){
return BIP39.mnemonicToSeed(mnemonic);
}
function generatePrivKey(mnemonic){
const seed = generateSeed(mnemonic);
return hdkey.fromMasterSeed(seed).derivePath(`m/44' /60' /0' /0/0`).getWallet().getPrivateKey();
}
function derivePubKey(privKey){
const wallet = Wallet.fromPrivateKey(privKey);
return wallet.getPublicKey();
}
function deriveEthAddress(pubKey){
const address = keccak256(pubKey);//keccak256 hash of publicKey
//Get the last 20 bytes of the public key
return "0x" + address.substring(address.length - 40, address.length);
}
function signTx(privKey, txData){
const tx = new EthereumTx(txData);
tx.sign(privKey);
return tx;
}
function 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)
}
}
})