1import * as bip32 from 'bip32'
2import * as bip39 from 'bip39'
3import keccak from 'keccak'
4import * as lodash from 'lodash'
5import Maybe from 'folktale/maybe'
6import * as secp256k1 from 'secp256k1'
8const DEFAULT_HD_PATH = "m/44'/60'/0'/0/0"
10const ETH_ZERO_ADDR = '0x0000000000000000000000000000000000000000'
11const CURVE_ZERO_ADDR = "0x0000000000000000000000000000000000000000000000000000000000000000"
14 MNEMONIC: Symbol('MNEMONIC'),
15 TICKET: Symbol('TICKET'),
16 SHARDS: Symbol('SHARDS'),
17 LEDGER: Symbol('LEDGER'),
18 TREZOR: Symbol('TREZOR'),
19 PRIVATE_KEY: Symbol('PRIVATE_KEY'),
20 KEYSTORE: Symbol('KEYSTORE')
23function EthereumWallet(privateKey) {
24 this.privateKey = privateKey
25 this.publicKey = secp256k1.publicKeyCreate(this.privateKey)
26 const pub = this.publicKey.toString('hex')
27 this.address = addressFromSecp256k1Public(pub)
30const renderWalletType = (wallet) =>
31 wallet === WALLET_NAMES.MNEMONIC
33 : wallet === WALLET_NAMES.TICKET
35 : wallet === WALLET_NAMES.SHARDS
37 : wallet === WALLET_NAMES.LEDGER
39 : wallet === WALLET_NAMES.TREZOR
41 : wallet === WALLET_NAMES.PRIVATE_KEY
43 : wallet === WALLET_NAMES.KEYSTORE
47const addressFromSecp256k1Public = pub => {
48 const compressed = false
49 const uncompressed = secp256k1.publicKeyConvert(
50 Buffer.from(pub, 'hex'),
53 const chopped = uncompressed.slice(1) // chop parity byte
54 const hashed = keccak256(chopped)
55 const addr = addHexPrefix(hashed.slice(-20).toString('hex'))
56 return toChecksumAddress(addr)
59const addHexPrefix = hex =>
60 hex.slice(0, 2) === '0x'
64const stripHexPrefix = hex =>
65 hex.slice(0, 2) === '0x'
69const keccak256 = str =>
70 keccak('keccak256').update(str).digest()
72const isValidAddress = (address) =>
73 /^0x[0-9a-fA-F]{40}$/.test(address)
75const toChecksumAddress = (address) => {
76 const addr = stripHexPrefix(address).toLowerCase()
77 const hash = keccak256(addr).toString('hex')
79 return lodash.reduce(addr, (acc, char, idx) =>
80 parseInt(hash[idx], 16) >= 8
81 ? acc + char.toUpperCase()
86const eqAddr = (addr0, addr1) =>
87 toChecksumAddress(addr0) === toChecksumAddress(addr1)
89const walletFromMnemonic = (mnemonic, hdpath) => {
91 bip39.validateMnemonic(mnemonic)
92 ? Maybe.Just(bip39.mnemonicToSeed(mnemonic))
95 const toWallet = (sd, path) => {
98 const hd = bip32.fromSeed(sd)
99 wal = Maybe.Just(hd.derivePath(path))
101 wal = Maybe.Nothing()
106 const wallet = seed.chain(sd => toWallet(sd, hdpath))
118 addressFromSecp256k1Public,
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list