1import * as bip32 from 'bip32';
2import * as bip39 from 'bip39';
3import keccak from 'keccak';
4import { reduce } from 'lodash';
5import { Just, Nothing } from 'folktale/maybe';
6import * as secp256k1 from 'secp256k1';
7import * as kg from 'urbit-key-generation';
8import { isAddress } from 'web3-utils';
10export const DEFAULT_HD_PATH = "m/44'/60'/0'/0/0";
11export const ETH_ZERO_ADDR = '0x0000000000000000000000000000000000000000';
12export const ETH_ZERO_ADDR_SHORT = '0x0';
14export const WALLET_TYPES = {
15 MNEMONIC: Symbol('MNEMONIC'),
16 TICKET: Symbol('TICKET'),
17 SHARDS: Symbol('SHARDS'),
18 LEDGER: Symbol('LEDGER'),
19 TREZOR: Symbol('TREZOR'),
20 PRIVATE_KEY: Symbol('PRIVATE_KEY'),
21 KEYSTORE: Symbol('KEYSTORE'),
22 METAMASK: Symbol('METAMASK'),
25export function EthereumWallet(privateKey) {
26 this.privateKey = privateKey;
27 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
28 const pub = this.publicKey.toString('hex');
29 this.address = addressFromSecp256k1Public(pub);
32export const addressFromSecp256k1Public = pub => {
33 const compressed = false;
34 const uncompressed = secp256k1.publicKeyConvert(
35 Buffer.from(pub, 'hex'),
38 const chopped = uncompressed.slice(1); // chop parity byte
39 const hashed = keccak256(chopped);
40 const addr = addHexPrefix(hashed.slice(-20).toString('hex'));
41 return toChecksumAddress(addr);
44// TODO: move all of these generic crypto helpers to another file
45export const addHexPrefix = hex =>
46 hex.slice(0, 2) === '0x' ? hex : '0x' + hex;
48export const stripHexPrefix = hex =>
49 hex.slice(0, 2) === '0x' ? hex.slice(2) : hex;
51export const keccak256 = str =>
56export const isValidAddress = a => '0x0' === a || isAddress(a);
58export const isZeroAddress = a =>
59 a === ETH_ZERO_ADDR || a === ETH_ZERO_ADDR_SHORT;
61export const toChecksumAddress = address => {
62 const addr = stripHexPrefix(address).toLowerCase();
63 const hash = keccak256(addr).toString('hex');
68 parseInt(hash[idx], 16) >= 8 ? acc + char.toUpperCase() : acc + char,
73export const eqAddr = (addr0, addr1) =>
74 toChecksumAddress(addr0) === toChecksumAddress(addr1);
76export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
77 return await kg.generateWallet({
80 passphrase: passphrase,
84export const walletFromMnemonic = (
91 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
92 ? Just(bip39.mnemonicToSeed(mnemonic, passphrase))
95 const toWallet = (sd, path) => {
98 const hd = bip32.fromSeed(sd);
99 wal = hd.derivePath(path);
100 wal.address = addressFromSecp256k1Public(wal.publicKey);
101 wal.passphrase = passphrase || '';
109 const wallet = seed.chain(sd => toWallet(sd, hdpath));
113export const abbreviateAddress = address =>
114 `${address.slice(0, 6)}…${address.slice(-4)}`;
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