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/dist';
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'),
24export function EthereumWallet(privateKey) {
25 this.privateKey = privateKey;
26 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
27 const pub = this.publicKey.toString('hex');
28 this.address = addressFromSecp256k1Public(pub);
31export const addressFromSecp256k1Public = pub => {
32 const compressed = false;
33 const uncompressed = secp256k1.publicKeyConvert(
34 Buffer.from(pub, 'hex'),
37 const chopped = uncompressed.slice(1); // chop parity byte
38 const hashed = keccak256(chopped);
39 const addr = addHexPrefix(hashed.slice(-20).toString('hex'));
40 return toChecksumAddress(addr);
43// TODO: move all of these generic crypto helpers to another file
44export const addHexPrefix = hex =>
45 hex.slice(0, 2) === '0x' ? hex : '0x' + hex;
47export const stripHexPrefix = hex =>
48 hex.slice(0, 2) === '0x' ? hex.slice(2) : hex;
50export const keccak256 = str =>
55export const isValidAddress = a => '0x0' === a || isAddress(a);
57export const isZeroAddress = a =>
58 a === ETH_ZERO_ADDR || a === ETH_ZERO_ADDR_SHORT;
60export const toChecksumAddress = address => {
61 const addr = stripHexPrefix(address).toLowerCase();
62 const hash = keccak256(addr).toString('hex');
67 parseInt(hash[idx], 16) >= 8 ? acc + char.toUpperCase() : acc + char,
72export const eqAddr = (addr0, addr1) =>
73 toChecksumAddress(addr0) === toChecksumAddress(addr1);
75export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
76 return await kg.generateWallet({
79 passphrase: passphrase,
83export const walletFromMnemonic = (mnemonic, hdpath, passphrase) => {
84 const seed = bip39.validateMnemonic(mnemonic)
85 ? Just(bip39.mnemonicToSeed(mnemonic, passphrase))
88 const toWallet = (sd, path) => {
91 const hd = bip32.fromSeed(sd);
92 wal = hd.derivePath(path);
93 wal.address = addressFromSecp256k1Public(wal.publicKey);
94 wal.passphrase = passphrase || '';
102 const wallet = seed.chain(sd => toWallet(sd, hdpath));
106export const abbreviateAddress = address =>
107 `${address.slice(0, 4)}…${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