on 100b34b help sign in
bridge/src/lib/wallet.ts
49 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 this.address = bufferedPublicKeyToAddress(this.publicKey);
17export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
18 return await kg.generateWallet({
19 ticket: ticket,
20 ship: point,
21 passphrase: passphrase,
22 });
23};
25export const walletFromMnemonic = (
26 mnemonic,
27 hdpath,
28 passphrase,
29 skipMnemonicCheck
30) => {
31 const seed =
32 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
33 ? Just(bip39.mnemonicToSeedSync(mnemonic, passphrase))
34 : Nothing();
36 const toWallet = (sd, path) => {
37 let wal;
38 const hd = bip32.fromSeed(sd);
39 wal = hd.derivePath(path);
40 wal.address = bufferedPublicKeyToAddress(wal.publicKey);
41 wal.passphrase = passphrase || '';
42 wal = Just(wal);
43 return wal;
44 };
46 const wallet = seed.chain(sd => toWallet(sd, hdpath));
48 return wallet;
49};