on b78a856 help sign in
bridge/src/lib/wallet.ts
50 lines 1.4 kB history raw
1// import * as bip32 from 'bip32';
2import * as bip39 from 'bip39';
3import { bip32 } from 'bitcoinjs-lib';
4import { Just, Nothing } from 'folktale/maybe';
5import * as secp256k1 from 'secp256k1';
6import * as kg from 'urbit-key-generation';
7import { publicToAddress } from 'ethereumjs-util';
8import { toHex } from 'web3-utils';
9import { bufferedPublicKeyToAddress } from './utils/crypto';
11export function EthereumWallet(privateKey) {
12 this.privateKey = privateKey;
13 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
14 const pub = this.publicKey.toString('hex');
15 this.address = publicToAddress(pub).toString('hex');
18export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
19 return await kg.generateWallet({
20 ticket: ticket,
21 ship: point,
22 passphrase: passphrase,
23 });
24};
26export const walletFromMnemonic = (
27 mnemonic,
28 hdpath,
29 passphrase,
30 skipMnemonicCheck
31) => {
32 const seed =
33 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
34 ? Just(bip39.mnemonicToSeedSync(mnemonic, passphrase))
35 : Nothing();
37 const toWallet = (sd, path) => {
38 let wal;
39 const hd = bip32.fromSeed(sd);
40 wal = hd.derivePath(path);
41 wal.address = bufferedPublicKeyToAddress(wal.publicKey);
42 wal.passphrase = passphrase || '';
43 wal = Just(wal);
44 return wal;
45 };
47 const wallet = seed.chain(sd => toWallet(sd, hdpath));
49 return wallet;
50};