on master 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 kg from 'urbit-key-generation';
6import BridgeWallet from './types/BridgeWallet';
7import { publicToAddress } from './utils/address';
9export const urbitWalletFromTicket = async (
10 ticket: string,
11 point: number,
12 passphrase?: string
13) => {
14 return await kg.generateWallet({
15 ticket: ticket,
16 ship: point,
17 passphrase: passphrase,
18 });
19};
21export const walletFromMnemonic = (
22 mnemonic: string,
23 hdpath: string,
24 passphrase?: string,
25 skipMnemonicCheck?: boolean
26) => {
27 const seed =
28 skipMnemonicCheck || bip39.validateMnemonic(mnemonic)
29 ? Just(bip39.mnemonicToSeedSync(mnemonic, passphrase))
30 : Nothing();
32 const toWallet = (sd: Buffer, path: string) => {
33 let wal: BridgeWallet;
34 const hd = bip32.fromSeed(sd);
35 // tsc complains because address is not available; it is set on the next line
36 //@ts-ignore
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: Buffer) => toWallet(sd, hdpath));
46 return wallet;
47};