on 42e34eb help sign in
bridge/src/lib/wallet.ts
51 lines 1.3 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 './utils/address';
9export function EthereumWallet(privateKey) {
10 this.privateKey = privateKey;
11 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
12 this.address = publicToAddress(this.publicKey);
15export const urbitWalletFromTicket = async (
16 ticket: string,
17 point: number,
18 passphrase?: string
19) => {
20 return await kg.generateWallet({
21 ticket: ticket,
22 ship: point,
23 passphrase: passphrase,
24 });
25};
27export const walletFromMnemonic = (
28 mnemonic: string,
29 hdpath: string,
30 passphrase?: string,
31 skipMnemonicCheck?: boolean
32) => {
33 const seed =
34 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
35 ? Just(bip39.mnemonicToSeedSync(mnemonic, passphrase))
36 : Nothing();
38 const toWallet = (sd, path) => {
39 let wal;
40 const hd = bip32.fromSeed(sd);
41 wal = hd.derivePath(path);
42 wal.address = publicToAddress(wal.publicKey);
43 wal.passphrase = passphrase || '';
44 wal = Just(wal);
45 return wal;
46 };
48 const wallet = seed.chain(sd => toWallet(sd, hdpath));
50 return wallet;
51};