|
1 | 1 | // imports
|
2 | 2 |
|
3 |
| -// const lib = require('ehtereum-keychain-lib') |
4 |
| -const { gasPriceDefault } = require('ehtereum-keychain-lib') |
| 3 | +const { gasPriceDefault } = require('./eth-keychain-lib') |
5 | 4 | const bitcoin = require('bitcoinjs-lib') // we use btc keys and bip39 (btc keys are a subset of all possible ethereum keys)
|
6 | 5 | const bip39 = require("bip39")
|
7 | 6 | const wif = require('wif')
|
8 | 7 | // init web3
|
9 | 8 | const Web3 = require("web3")
|
10 | 9 | // const { inspect } = require('util')
|
11 | 10 |
|
12 |
| - |
13 |
| -// ---------------------------------------------------------------------------- |
14 |
| -// configs |
15 |
| -// ---------------------------------------------------------------------------- |
16 |
| - |
17 |
| -// infura/cloudflare/your-own-node.example... |
18 |
| - |
19 |
| -const RPC_HOST = "34.246.182.38" |
20 |
| - |
21 |
| -// use this if you're running your own node on the same machine (your "dev box"): |
22 |
| - |
23 |
| -// const RPC_HOST = "localhost" |
24 |
| - |
25 |
| - |
26 |
| - |
27 |
| - |
28 |
| -// ---------------------------------------------------------------------------- |
29 |
| - |
| 11 | +const { RPC_HOST } require('./configs') |
30 | 12 |
|
31 | 13 | // exception definition
|
32 | 14 |
|
33 |
| -class KeychainError extends Error { } |
34 |
| - |
35 |
| -class PrivateKeyLoadError extends KeychainError { |
36 |
| - constructor() { super("PrivateKeyLoadError: Failed to load Private key") } |
37 |
| -} |
| 15 | +const { } = require('./eth-keychain-errors') |
38 | 16 |
|
39 | 17 | // main
|
40 | 18 |
|
41 |
| -const { Keychain } = require('ethereum-keychain-keychain') |
| 19 | +class Keychain { |
| 20 | + constructor({ store }) { |
| 21 | + if (!store) console.warn("Using localstorage") |
| 22 | + this.Store = { store } |
| 23 | + this.web3 = this.initWeb3() |
| 24 | + this.eth = this.web3.eth |
| 25 | + this.web3Accounts = this.eth.accounts |
| 26 | + |
| 27 | + if (this.boom) throw new KeychainError |
| 28 | + this.initMisc() |
| 29 | + this.loadOrGeneratePrivateKey() |
| 30 | + this.web3Account = this.loadWeb3Account() |
| 31 | + this.address = this.deriveAddress() |
| 32 | + } |
| 33 | + |
| 34 | + initMisc() { |
| 35 | + this.storedKeyString = "__ethereum-keychain_" |
| 36 | + } |
| 37 | + |
| 38 | + loadWeb3Account() { |
| 39 | + console.log("TODO: load private key") |
| 40 | + // const privateKey = `${"0x"}${this.pvtKey.toString("hex")}` |
| 41 | + |
| 42 | + const privateKey = wif.decode(this.pvtKey.toWIF()).privateKey |
| 43 | + const privateKeyEth = `${"0x"}${privateKey.toString("hex")}` |
| 44 | + |
| 45 | + console.log("privateKey:", privateKey) |
| 46 | + console.log("privateKey (BTC - WIF):", this.pvtKey.toWIF()) |
| 47 | + console.log("privateKeyEth:", privateKeyEth) |
| 48 | + const account = this.web3Accounts.privateKeyToAccount(privateKey) |
| 49 | + this.pvtKeyEth = account.privateKey |
| 50 | + console.log("account:", inspect(account).slice(0, 85)) |
| 51 | + return account |
| 52 | + } |
| 53 | + |
| 54 | + deriveAddress() { |
| 55 | + return this.web3Account.address |
| 56 | + } |
| 57 | + |
| 58 | + // loadOrGeneratePrivateKey |
| 59 | + |
| 60 | + loadOrGeneratePrivateKey() { |
| 61 | + const key = this.storedKey |
| 62 | + if (key && key != '') { |
| 63 | + this.pvtKey = this.loadPrivateKey() |
| 64 | + } else { |
| 65 | + const newKey = this.generatePrivateKey() |
| 66 | + this.saveKey(newKey) |
| 67 | + this.pvtKey = newKey |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // loadOrGeneratePrivateKey "libs" |
| 72 | + |
| 73 | + get storedKey() { |
| 74 | + return this.store[this.storedKeyString] |
| 75 | + } |
| 76 | + |
| 77 | + generatePrivateKey() { |
| 78 | + return bitcoin.ECPair.makeRandom() |
| 79 | + } |
| 80 | + |
| 81 | + loadPrivateKey() { |
| 82 | + return bitcoin.ECPair.fromWIF(this.storedKey) |
| 83 | + } |
| 84 | + |
| 85 | + saveKey(key) { |
| 86 | + this.store[this.storedKeyString] = key.toWIF() |
| 87 | + } |
| 88 | + |
| 89 | + // store |
| 90 | + |
| 91 | + set Store({ store }) { |
| 92 | + this.store = store || localStorage |
| 93 | + this.store.init = true |
| 94 | + console.warn("wallet initialized") |
| 95 | + this.store |
| 96 | + } |
| 97 | + |
| 98 | + // info |
| 99 | + |
| 100 | + info() { |
| 101 | + console.log("PrivateKey:", inspect(this.pvtKey).slice(0, 85)) |
| 102 | + console.log("Address:", this.address) |
| 103 | + } |
| 104 | + |
| 105 | + async netInfo() { |
| 106 | + const info = {} |
| 107 | + // info.chainId = await this.eth.getChainId() |
| 108 | + info.address = this.address |
| 109 | + info.balance = await this.eth.getBalance(this.address) |
| 110 | + info.balanceEth = this.web3.utils.fromWei(info.balance, "ether") |
| 111 | + info.blockNum = await this.eth.getBlockNumber() |
| 112 | + const block = await this.eth.getBlock(info.blockNum) |
| 113 | + info.blockHash = block.hash |
| 114 | + console.log("Info:", JSON.stringify(info, null, 2)) |
| 115 | + } |
| 116 | + |
| 117 | + // init web3 |
| 118 | + |
| 119 | + initWeb3() { |
| 120 | + const rpcHost = RPC_HOST |
| 121 | + const rpcPort = "8545" // default |
| 122 | + const web3 = new Web3(`http://${rpcHost}:${rpcPort}`) |
| 123 | + return web3 |
| 124 | + } |
| 125 | + |
| 126 | + async send({ to, value }) { |
| 127 | + console.log("constructing tx") |
| 128 | + const txAttrs = txAttrsXDai({ to: this.address, value: 1000000000000 }) |
| 129 | + const rawTx = await this.signTx(txAttrs, this.pvtKeyEth) |
| 130 | + // console.log("rawTx:", rawTx) |
| 131 | + console.log("submitting tx...") |
| 132 | + const txHashPromise = this.eth.sendSignedTransaction(rawTx) |
| 133 | + return resolveTxHash(txHashPromise) |
| 134 | + } |
| 135 | + |
| 136 | + signTx(txAttrs, privateKey) { |
| 137 | + return new Promise((resolve, reject) => { |
| 138 | + const txCallback = (err, signedTx) => { |
| 139 | + // console.log("signedTx:", signedTx) |
| 140 | + if (err) return reject(err) |
| 141 | + if (!signedTx.rawTransaction) return reject(new Error("NoRawTransactionError")) |
| 142 | + resolve(signedTx.rawTransaction) |
| 143 | + } |
| 144 | + this.web3Accounts.signTransaction(txAttrs, privateKey, txCallback) |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + async selfTXTest() { |
| 149 | + const txHash = await this.send({ to: this.address, value: 1000000000000 }) |
| 150 | + console.log("TX:", txHash) |
| 151 | + return true |
| 152 | + } |
| 153 | +} |
| 154 | + |
42 | 155 |
|
43 | 156 | module.exports = {
|
44 | 157 | Keychain: Keychain
|
|
0 commit comments