Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit f13167d

Browse files
committed
restored files from the merge - refactor out configs and errors
1 parent c58cee9 commit f13167d

File tree

3 files changed

+164
-26
lines changed

3 files changed

+164
-26
lines changed

configs.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// this is the default node host used for the library, so you don't have to configure it
2+
3+
// infura/cloudflare/your-own-node.example...
4+
5+
const RPC_HOST = "34.246.182.38"
6+
7+
// use this if you're running your own node on the same machine (your "dev box"):
8+
9+
// const RPC_HOST = "localhost"
10+
11+
const CONFIGS = {
12+
RPC_HOST: RPC_HOST
13+
}
14+
15+
module.export = CONFIGS

eth-keychain-errors.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class KeychainError extends Error { }
2+
3+
class PrivateKeyLoadError extends KeychainError {
4+
constructor() { super("PrivateKeyLoadError: Failed to load Private key") }
5+
}
6+
7+
module.exports = {
8+
KeychainError: KeychainError,
9+
PrivateKeyLoadError: PrivateKeyLoadError,
10+
}

ethereum-keychain.js

+139-26
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,157 @@
11
// imports
22

3-
// const lib = require('ehtereum-keychain-lib')
4-
const { gasPriceDefault } = require('ehtereum-keychain-lib')
3+
const { gasPriceDefault } = require('./eth-keychain-lib')
54
const bitcoin = require('bitcoinjs-lib') // we use btc keys and bip39 (btc keys are a subset of all possible ethereum keys)
65
const bip39 = require("bip39")
76
const wif = require('wif')
87
// init web3
98
const Web3 = require("web3")
109
// const { inspect } = require('util')
1110

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')
3012

3113
// exception definition
3214

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')
3816

3917
// main
4018

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+
42155

43156
module.exports = {
44157
Keychain: Keychain

0 commit comments

Comments
 (0)