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')
22function EthereumWallet(privateKey) {
23 this.privateKey = privateKey
24 this.publicKey = secp256k1.publicKeyCreate(this.privateKey)
25 const pub = this.publicKey.toString('hex')
26 this.address = addressFromSecp256k1Public(pub)
29const renderWalletType = (wallet) =>
30 wallet === WALLET_NAMES.MNEMONIC
32 : wallet === WALLET_NAMES.TICKET
34 : wallet === WALLET_NAMES.SHARDS
36 : wallet === WALLET_NAMES.LEDGER
38 : wallet === WALLET_NAMES.TREZOR
40 : wallet === WALLET_NAMES.PRIVATE_KEY
44const addressFromSecp256k1Public = pub => {
45 const compressed = false
46 const uncompressed = secp256k1.publicKeyConvert(
47 Buffer.from(pub, 'hex'),
50 const chopped = uncompressed.slice(1) // chop parity byte
51 const hashed = keccak256(chopped)
52 const addr = addHexPrefix(hashed.slice(-20).toString('hex'))
53 return toChecksumAddress(addr)
56const addHexPrefix = hex =>
57 hex.slice(0, 2) === '0x'
61const stripHexPrefix = hex =>
62 hex.slice(0, 2) === '0x'
66const keccak256 = str =>
67 keccak('keccak256').update(str).digest()
69const isValidAddress = (address) =>
70 /^0x[0-9a-fA-F]{40}$/.test(address)
72const toChecksumAddress = (address) => {
73 const addr = stripHexPrefix(address).toLowerCase()
74 const hash = keccak256(addr).toString('hex')
76 return lodash.reduce(addr, (acc, char, idx) =>
77 parseInt(hash[idx], 16) >= 8
78 ? acc + char.toUpperCase()
83const eqAddr = (addr0, addr1) =>
84 toChecksumAddress(addr0) === toChecksumAddress(addr1)
86const walletFromMnemonic = (mnemonic, hdpath) => {
88 bip39.validateMnemonic(mnemonic)
89 ? Maybe.Just(bip39.mnemonicToSeed(mnemonic))
92 const toWallet = (sd, path) => {
95 const hd = bip32.fromSeed(sd)
96 wal = Maybe.Just(hd.derivePath(path))
103 const wallet = seed.chain(sd => toWallet(sd, hdpath))
115 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