on 69a57bc help sign in
bridge/src/lib/wallet.js
114 lines 3.2 kB history raw
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'),
23};
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'),
36 compressed
37 );
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);
42};
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 =>
52 keccak('keccak256')
53 .update(str)
54 .digest();
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');
65 return reduce(
66 addr,
67 (acc, char, idx) =>
68 parseInt(hash[idx], 16) >= 8 ? acc + char.toUpperCase() : acc + char,
69 '0x'
70 );
71};
73export const eqAddr = (addr0, addr1) =>
74 toChecksumAddress(addr0) === toChecksumAddress(addr1);
76export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
77 return await kg.generateWallet({
78 ticket: ticket,
79 ship: point,
80 passphrase: passphrase,
81 });
82};
84export const walletFromMnemonic = (
85 mnemonic,
86 hdpath,
87 passphrase,
88 skipMnemonicCheck
89) => {
90 const seed =
91 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
92 ? Just(bip39.mnemonicToSeed(mnemonic, passphrase))
93 : Nothing();
95 const toWallet = (sd, path) => {
96 let wal;
97 try {
98 const hd = bip32.fromSeed(sd);
99 wal = hd.derivePath(path);
100 wal.address = addressFromSecp256k1Public(wal.publicKey);
101 wal.passphrase = passphrase || '';
102 wal = Just(wal);
103 } catch (_) {
104 wal = Nothing();
105 }
106 return wal;
107 };
109 const wallet = seed.chain(sd => toWallet(sd, hdpath));
110 return wallet;
111};
113export const abbreviateAddress = address =>
114 `${address.slice(0, 6)}…${address.slice(-4)}`;