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 const sec = Buffer.from(privateKey, 'hex')
25 this.publicKey = secp256k1.publicKeyCreate(sec)
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
45const addressFromSecp256k1Public = pub => {
46 const compressed = false
47 const uncompressed = secp256k1.publicKeyConvert(
48 Buffer.from(pub, 'hex'),
51 const chopped = uncompressed.slice(1) // chop parity byte
52 const hashed = keccak256(chopped)
53 const addr = addHexPrefix(hashed.slice(-20).toString('hex'))
54 return toChecksumAddress(addr)
57const addHexPrefix = hex =>
58 hex.slice(0, 2) === '0x'
62const stripHexPrefix = hex =>
63 hex.slice(0, 2) === '0x'
67const keccak256 = str =>
68 keccak('keccak256').update(str).digest()
70const isValidAddress = (address) =>
71 /^0x[0-9a-fA-F]{40}$/.test(address)
73const toChecksumAddress = (address) => {
74 const addr = stripHexPrefix(address).toLowerCase()
75 const hash = keccak256(addr).toString('hex')
77 return lodash.reduce(addr, (acc, char, idx) =>
78 parseInt(hash[idx], 16) >= 8
79 ? acc + char.toUpperCase()
84const eqAddr = (addr0, addr1) =>
85 toChecksumAddress(addr0) === toChecksumAddress(addr1)
87const walletFromMnemonic = (mnemonic, hdpath) => {
89 bip39.validateMnemonic(mnemonic)
90 ? Maybe.Just(bip39.mnemonicToSeed(mnemonic))
93 const toWallet = (sd, path) => {
96 const hd = bip32.fromSeed(sd)
97 wal = Maybe.Just(hd.derivePath(path))
104 const wallet = seed.chain(sd => toWallet(sd, hdpath))
116 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