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 const NONCUSTODIAL_WALLETS = new Set([
26 WALLET_TYPES.METAMASK,
31export function EthereumWallet(privateKey) {
32 this.privateKey = privateKey;
33 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
34 const pub = this.publicKey.toString('hex');
35 this.address = addressFromSecp256k1Public(pub);
38export const addressFromSecp256k1Public = pub => {
39 const compressed = false;
40 const uncompressed = secp256k1.publicKeyConvert(
41 Buffer.from(pub, 'hex'),
44 const chopped = uncompressed.slice(1); // chop parity byte
45 const hashed = keccak256(chopped);
46 const addr = addHexPrefix(hashed.slice(-20).toString('hex'));
47 return toChecksumAddress(addr);
50// TODO: move all of these generic crypto helpers to another file
51export const addHexPrefix = hex =>
52 hex.slice(0, 2) === '0x' ? hex : '0x' + hex;
54export const stripHexPrefix = hex =>
55 hex.slice(0, 2) === '0x' ? hex.slice(2) : hex;
57export const keccak256 = str =>
62export const isValidAddress = a => '0x0' === a || isAddress(a);
64export const isZeroAddress = a =>
65 a === ETH_ZERO_ADDR || a === ETH_ZERO_ADDR_SHORT;
67export const toChecksumAddress = address => {
68 const addr = stripHexPrefix(address).toLowerCase();
69 const hash = keccak256(addr).toString('hex');
74 parseInt(hash[idx], 16) >= 8 ? acc + char.toUpperCase() : acc + char,
79export const eqAddr = (addr0, addr1) =>
80 toChecksumAddress(addr0) === toChecksumAddress(addr1);
82export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
83 return await kg.generateWallet({
86 passphrase: passphrase,
90export const walletFromMnemonic = (
97 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
98 ? Just(bip39.mnemonicToSeed(mnemonic, passphrase))
101 const toWallet = (sd, path) => {
104 const hd = bip32.fromSeed(sd);
105 wal = hd.derivePath(path);
106 wal.address = addressFromSecp256k1Public(wal.publicKey);
107 wal.passphrase = passphrase || '';
115 const wallet = seed.chain(sd => toWallet(sd, hdpath));
119export const abbreviateAddress = address =>
120 `${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