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