@@ -8,13 +8,12 @@ import * as ob from 'urbit-ob';
8import * as kg from 'urbit-key-generation/dist';
8import * as kg from 'urbit-key-generation/dist';
9import { isAddress } from 'web3-utils';
9import { isAddress } from 'web3-utils';
11−const DEFAULT_HD_PATH = "m/44'/60'/0'/0/0";
11+export const DEFAULT_HD_PATH = "m/44'/60'/0'/0/0";
13−const ETH_ZERO_ADDR = '0x0000000000000000000000000000000000000000';
12+export const ETH_ZERO_ADDR = '0x0000000000000000000000000000000000000000';
14−const CURVE_ZERO_ADDR =
13+export const CURVE_ZERO_ADDR =
15 '0x0000000000000000000000000000000000000000000000000000000000000000';
14 '0x0000000000000000000000000000000000000000000000000000000000000000';
17−const WALLET_TYPES = {
16+export const WALLET_TYPES = {
18 MNEMONIC: Symbol('MNEMONIC'),
17 MNEMONIC: Symbol('MNEMONIC'),
19 TICKET: Symbol('TICKET'),
18 TICKET: Symbol('TICKET'),
20 SHARDS: Symbol('SHARDS'),
19 SHARDS: Symbol('SHARDS'),
@@ -24,31 +23,14 @@ const WALLET_TYPES = {
24 KEYSTORE: Symbol('KEYSTORE'),
23 KEYSTORE: Symbol('KEYSTORE'),
27−function EthereumWallet(privateKey) {
26+export function EthereumWallet(privateKey) {
28 this.privateKey = privateKey;
27 this.privateKey = privateKey;
29 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
28 this.publicKey = secp256k1.publicKeyCreate(this.privateKey);
30 const pub = this.publicKey.toString('hex');
29 const pub = this.publicKey.toString('hex');
31 this.address = addressFromSecp256k1Public(pub);
30 this.address = addressFromSecp256k1Public(pub);
34−const renderWalletType = wallet =>
35− wallet === WALLET_TYPES.MNEMONIC
37− : wallet === WALLET_TYPES.TICKET
39− : wallet === WALLET_TYPES.SHARDS
41− : wallet === WALLET_TYPES.LEDGER
43− : wallet === WALLET_TYPES.TREZOR
45− : wallet === WALLET_TYPES.PRIVATE_KEY
47− : wallet === WALLET_TYPES.KEYSTORE
51−const addressFromSecp256k1Public = pub => {
33+export const addressFromSecp256k1Public = pub => {
52 const compressed = false;
34 const compressed = false;
53 const uncompressed = secp256k1.publicKeyConvert(
35 const uncompressed = secp256k1.publicKeyConvert(
54 Buffer.from(pub, 'hex'),
36 Buffer.from(pub, 'hex'),
@@ -60,18 +42,20 @@ const addressFromSecp256k1Public = pub => {
60 return toChecksumAddress(addr);
42 return toChecksumAddress(addr);
45+export const addHexPrefix = hex =>
63−const addHexPrefix = hex => (hex.slice(0, 2) === '0x' ? hex : '0x' + hex);
46+ hex.slice(0, 2) === '0x' ? hex : '0x' + hex;
48+export const stripHexPrefix = hex =>
65−const stripHexPrefix = hex => (hex.slice(0, 2) === '0x' ? hex.slice(2) : hex);
49+ hex.slice(0, 2) === '0x' ? hex.slice(2) : hex;
67−const keccak256 = str =>
51+export const keccak256 = str =>
68 keccak('keccak256')
52 keccak('keccak256')
69 .update(str)
53 .update(str)
70 .digest();
54 .digest();
72−const isValidAddress = a => '0x0' === a || isAddress(a);
56+export const isValidAddress = a => '0x0' === a || isAddress(a);
74−const toChecksumAddress = address => {
58+export const toChecksumAddress = address => {
75 const addr = stripHexPrefix(address).toLowerCase();
59 const addr = stripHexPrefix(address).toLowerCase();
76 const hash = keccak256(addr).toString('hex');
60 const hash = keccak256(addr).toString('hex');
@@ -83,27 +67,28 @@ const toChecksumAddress = address => {
86−const eqAddr = (addr0, addr1) =>
70+export const eqAddr = (addr0, addr1) =>
87 toChecksumAddress(addr0) === toChecksumAddress(addr1);
71 toChecksumAddress(addr0) === toChecksumAddress(addr1);
89−const urbitWalletFromTicket = async (ticket, point, passphrase) => {
73+export const urbitWalletFromTicket = async (ticket, point, passphrase) => {
90 if (typeof point === 'string') {
74 if (typeof point === 'string') {
91 //TODO why not in kg?
75 //TODO why not in kg?
92 point = ob.patp2dec(point);
76 point = ob.patp2dec(point);
94− let uhdw = await kg.generateWallet({
79+ return await kg.generateWallet({
95 ticket: ticket,
80 ticket: ticket,
96 ship: point,
81 ship: point,
97 passphrase: passphrase,
82 passphrase: passphrase,
102−const ownershipWalletFromTicket = async (ticket, point, passphrase) => {
86+export const ownershipWalletFromTicket = async (ticket, point, passphrase) => {
103 if (typeof point === 'string') {
87 if (typeof point === 'string') {
104 //TODO why not in kg?
88 //TODO why not in kg?
105 point = ob.patp2dec(point);
89 point = ob.patp2dec(point);
107 return await kg.generateOwnershipWallet({
92 return await kg.generateOwnershipWallet({
109 ship: point,
94 ship: point,
@@ -111,7 +96,7 @@ const ownershipWalletFromTicket = async (ticket, point, passphrase) => {
114−const walletFromMnemonic = (mnemonic, hdpath, passphrase) => {
99+export const walletFromMnemonic = (mnemonic, hdpath, passphrase) => {
115 const seed = bip39.validateMnemonic(mnemonic)
100 const seed = bip39.validateMnemonic(mnemonic)
116 ? Maybe.Just(bip39.mnemonicToSeed(mnemonic, passphrase))
101 ? Maybe.Just(bip39.mnemonicToSeed(mnemonic, passphrase))
117 : Maybe.Nothing();
102 : Maybe.Nothing();
@@ -132,20 +117,3 @@ const walletFromMnemonic = (mnemonic, hdpath, passphrase) => {
132 const wallet = seed.chain(sd => toWallet(sd, hdpath));
117 const wallet = seed.chain(sd => toWallet(sd, hdpath));
133 return wallet;
118 return wallet;
144− addressFromSecp256k1Public,
146− urbitWalletFromTicket,
147− ownershipWalletFromTicket,