on a54b5ac help sign in
bridge/src/lib/wallet.js
121 lines 3.0 kB history raw
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"
13const WALLET_NAMES = {
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')
24 this.privateKey = sec
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
32 ? 'Mnemonic'
33 : wallet === WALLET_NAMES.TICKET
34 ? 'Ticket'
35 : wallet === WALLET_NAMES.SHARDS
36 ? 'Ticket Shards'
37 : wallet === WALLET_NAMES.LEDGER
38 ? 'Ledger'
39 : wallet === WALLET_NAMES.TREZOR
40 ? 'Trezor'
41 : wallet === WALLET_NAMES.PRIVATE_KEY
42 ? 'Private Key'
43 : 'Wallet'
45const addressFromSecp256k1Public = pub => {
46 const compressed = false
47 const uncompressed = secp256k1.publicKeyConvert(
48 Buffer.from(pub, 'hex'),
49 compressed
50 )
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'
59 ? hex
60 : '0x' + hex
62const stripHexPrefix = hex =>
63 hex.slice(0, 2) === '0x'
64 ? hex.slice(2)
65 : hex
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()
80 : acc + char,
81 '0x')
84const eqAddr = (addr0, addr1) =>
85 toChecksumAddress(addr0) === toChecksumAddress(addr1)
87const walletFromMnemonic = (mnemonic, hdpath) => {
88 const seed =
89 bip39.validateMnemonic(mnemonic)
90 ? Maybe.Just(bip39.mnemonicToSeed(mnemonic))
91 : Maybe.Nothing()
93 const toWallet = (sd, path) => {
94 let wal
95 try {
96 const hd = bip32.fromSeed(sd)
97 wal = Maybe.Just(hd.derivePath(path))
98 } catch (_) {
99 wal = Maybe.Nothing()
100 }
101 return wal
102 }
104 const wallet = seed.chain(sd => toWallet(sd, hdpath))
105 return wallet
108export {
109 DEFAULT_HD_PATH,
110 WALLET_NAMES,
111 ETH_ZERO_ADDR,
112 CURVE_ZERO_ADDR,
113 renderWalletType,
114 isValidAddress,
115 toChecksumAddress,
116 addressFromSecp256k1Public,
117 eqAddr,
118 walletFromMnemonic,
119 addHexPrefix,
120 EthereumWallet