-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
71 lines (54 loc) · 2.12 KB
/
index.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
const Web3 = require("web3");
const TonWeb = require("tonweb");
const fs = require("fs");
const {concatBytes} = require("tonweb/src/utils");
async function init() {
const nacl = TonWeb.utils.nacl;
const web3 = new Web3();
const FILE_NAME = 'keys.json';
let tonSecretKey, ethAccount;
let needSave = false;
if (fs.existsSync(FILE_NAME)) {
console.log(`Getting keys from ${FILE_NAME}..`);
const json = JSON.parse(fs.readFileSync(FILE_NAME).toString('utf-8'));
if (!json.tonPrivateKey) throw new Error('invalid ' + FILE_NAME);
if (!json.ethPrivateKey) throw new Error('invalid ' + FILE_NAME);
tonSecretKey = json.tonPrivateKey;
ethAccount = web3.eth.accounts.privateKeyToAccount(json.ethPrivateKey);
} else {
console.log('Generating TON and ETH keys..');
const newKeyPair = nacl.box.keyPair();
tonSecretKey = TonWeb.utils.bytesToHex(newKeyPair.secretKey);
ethAccount = web3.eth.accounts.create();
needSave = true;
}
// TON
const keyPair = nacl.sign.keyPair.fromSeed(TonWeb.utils.hexToBytes(tonSecretKey));
const tonweb = new TonWeb(new TonWeb.HttpProvider());
const walletClass = tonweb.wallet.all['v3R2'];
const wallet = new walletClass(tonweb.provider, {
publicKey: keyPair.publicKey,
wc: -1
});
console.log('TON Public Key = ' + TonWeb.utils.bytesToHex(keyPair.publicKey));
console.log('TON Public Key Base64 = ' + TonWeb.utils.bytesToBase64(concatBytes(new Uint8Array([0x3E, 0xE6]), keyPair.publicKey)));
const myAddress = (await wallet.getAddress()).toString(false);
console.log('TON Address = ' + myAddress);
// ETH
console.log('ETH Address = ' + ethAccount.address);
const data = {
tonPrivateKey: tonSecretKey,
ethPrivateKey: ethAccount.privateKey
};
if (needSave) {
fs.writeFile(FILE_NAME, JSON.stringify(data), (err, data) => {
if (err) {
return console.error('Write file error', err);
}
console.log('Done');
});
} else {
console.log('Done');
}
}
init();