on master help sign in
refactor: minor changes from self-code-review
commit60ad7ac16eaff06e616f921222fe00145a111c50 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 16 Jul 2019, 22:36 (7 years ago) parente7c1371
12 files changed+70−55
src/components/InlineEthereumTransaction.js+1−1 src/lib/constants.js+8 src/lib/error.js+2 src/lib/invite.js+11−6 src/lib/keys.js+4−4 src/lib/need.js+3−3 src/lib/proxy.js+2−3 src/lib/useEthereumTransaction.js+29−27 src/lib/useKeyfileGenerator.js+3−2 src/lib/validators.js+5−5 src/views/Admin/AdminNetworkingKeys.js+1−3 src/views/Login/Keystore.js+1−1
src/components/InlineEthereumTransaction.js+1−1
@@ -87,7 +87,7 @@ export default function InlineEthereumTransaction({
8787 as={ForwardButton}
8888 solid
8989 success
90− disable={broadcasted}
90+ disabled={broadcasted}
9191 loading={broadcasted}
9292 onClick={() => broadcast()}>
9393 Send Transaction
src/lib/constants.js+8
@@ -18,9 +18,13 @@ const GALAXY_ENTROPY_BITS = 384;
1818
1919const SEED_ENTROPY_BITS = 128;
2020
21+// the curve param for the network keys
22+const NETWORK_KEY_CURVE_PARAMETER = '42';
2123// the null result if a key is unset within azimuth
2224const ZERO_KEY =
2325 '0x0000000000000000000000000000000000000000000000000000000000000000';
26+// the current crypto suite version
27+const CRYPTO_SUITE_VERSION = 1;
2428
2529//TODO move into azimuth-js
2630const GAS_LIMITS = {
@@ -37,6 +41,8 @@ const GAS_LIMITS = {
3741 //
3842 GIFT_PLANET: 400000, //NOTE low sample size
3943 //
44+ SEND_ETH: 21000,
45+ //
4046 DEFAULT: 550000,
4147};
4248
@@ -89,5 +95,7 @@ export {
8995 GALAXY_ENTROPY_BITS,
9096 SEED_ENTROPY_BITS,
9197 ZOD,
98+ NETWORK_KEY_CURVE_PARAMETER,
9299 ZERO_KEY,
100+ CRYPTO_SUITE_VERSION,
93101};
src/lib/error.js+2
@@ -7,6 +7,8 @@ const BRIDGE_ERROR = {
77 MISSING_MNEMONIC: 'no authentication mnemonic found',
88 MISSING_URBIT_WALLET: 'no urbit wallet found',
99 MISSING_POINT: 'no point found',
10+ MISSING_AUTH_MNEMONIC: 'no mnemonic found',
11+ MISSING_POINT_DETAILS: 'no details of point found',
1012 INVALID_ROUTE: 'invalid route',
1113 INVALID_NETWORK_TYPE: 'invalid network type',
1214 INVALID_WALLET_TYPE: 'invalid wallet type',
src/lib/invite.js+11−6
@@ -10,7 +10,12 @@ import { hexify } from './txn';
1010import { deriveNetworkSeedFromUrbitWallet, deriveNetworkKeys } from './keys';
1111import { addHexPrefix } from './wallet';
1212import { sendAndAwaitAll } from './txn';
13−import { GAS_LIMITS } from './constants';
13+import {
14+ GAS_LIMITS,
15+ DEFAULT_GAS_PRICE_GWEI,
16+ CRYPTO_SUITE_VERSION,
17+} from './constants';
18+import { toWei } from 'web3-utils';
1419
1520// the initial network key revision is always 1
1621const INITIAL_NETWORK_KEY_REVISION = 1;
@@ -138,8 +143,8 @@ export async function reticketPointBetweenWallets({
138143 point,
139144 addHexPrefix(networkKeys.crypt.public),
140145 addHexPrefix(networkKeys.auth.public),
141− 1,
142− false
146+ CRYPTO_SUITE_VERSION,
147+ /* discontinuous */ false
143148 );
144149 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
145150
@@ -201,7 +206,7 @@ export async function reticketPointBetweenWallets({
201206 txs = txs.map(tx => {
202207 tx.from = fromWallet.address;
203208 tx.nonce = inviteNonce++;
204− tx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
209+ tx.gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'), 10);
205210 totalCost = totalCost + tx.gasPrice * tx.gas;
206211 return tx;
207212 });
@@ -247,8 +252,8 @@ export async function reticketPointBetweenWallets({
247252
248253 // if non-trivial eth left in invite wallet, transfer to new ownership
249254 let balance = await web3.eth.getBalance(fromWallet.address);
250− const gasPrice = 20000000000;
251− const gasLimit = 21000;
255+ const gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'));
256+ const gasLimit = GAS_LIMITS.SEND_ETH;
252257 const sendEthCost = gasPrice * gasLimit;
253258 if (transferEth && balance > sendEthCost) {
254259 const value = balance - sendEthCost;
src/lib/keys.js+4−4
@@ -6,8 +6,7 @@ import * as serial from '../nockjs/serial';
66import * as kg from 'urbit-key-generation/dist';
77
88import { eqAddr, addHexPrefix, CURVE_ZERO_ADDR } from './wallet';
9−
10−const NETWORK_KEY_CURVE_PARAMETER = '42';
9+import { NETWORK_KEY_CURVE_PARAMETER } from './constants';
1110
1211// ctsy joemfb
1312const b64 = buf => {
@@ -164,11 +163,12 @@ export const keysMatchChain = (pair, details) => {
164163};
165164
166165export const segmentNetworkKey = hex => {
167− const sl = i => hex.slice(i, i + 4);
168− const rowFrom = i => `${sl(i)}.${sl(i + 4)}.${sl(i + 8)}.${sl(i + 12)}`;
169166 if (hex === CURVE_ZERO_ADDR) {
170167 return null;
171168 }
172169
170+ const sl = i => hex.slice(i, i + 4);
171+ const rowFrom = i => `${sl(i)}.${sl(i + 4)}.${sl(i + 8)}.${sl(i + 12)}`;
172+
173173 return [rowFrom(2), rowFrom(18), rowFrom(34), rowFrom(50)];
174174};
src/lib/need.js+3−3
@@ -18,15 +18,15 @@ const needBuilder = fn => obj => {
1818export const value = (obj, fn) => needBuilder(fn)(obj);
1919
2020export const details = needBuilder(() => {
21− throw new Error('need details of point');
21+ throw new Error(BRIDGE_ERROR.MISSING_POINT_DETAILS);
2222});
2323
2424export const authMnemonic = needBuilder(() => {
25− throw new Error('need auth mnemonic');
25+ throw new Error(BRIDGE_ERROR.MISSING_AUTH_MNEMONIC);
2626});
2727
2828export const web3 = needBuilder(() => {
29− throw new Error(BRIDGE_ERROR.MISSING_WEB3.message);
29+ throw new Error(BRIDGE_ERROR.MISSING_WEB3);
3030});
3131
3232export const contracts = needBuilder(() => {
src/lib/proxy.js+2−3
@@ -23,10 +23,9 @@ export const proxyTypeToHuman = proxyType => {
2323export const proxyTypeToHumanDescription = proxyType => {
2424 switch (proxyType) {
2525 case PROXY_TYPE.MANAGEMENT:
26− // TODO: is the below copy actually true?
27− return "The management address proves your point's identity to Arvo.";
26+ return 'The management proxy is allowed to manage networking keys.';
2827 case PROXY_TYPE.SPAWN:
29− return 'The spawn proxy is allowed to spawn points from this point.';
28+ return 'The spawn proxy is allowed to spawn points.';
3029 case PROXY_TYPE.TRANSFER:
3130 return 'The transfer proxy is allowed to transfer this point.';
3231 case PROXY_TYPE.VOTING:
src/lib/useEthereumTransaction.js+29−27
@@ -1,4 +1,5 @@
1−import { useCallback, useEffect, useState } from 'react';
1+import { useCallback, useState } from 'react';
2+import { fromWei } from 'web3-utils';
23
34import { useNetwork } from 'store/network';
45import { useWallet } from 'store/wallet';
@@ -9,7 +10,8 @@ import {
910 waitForTransactionConfirm,
1011} from './txn';
1112import * as need from './need';
12−import { fromWei } from 'web3-utils';
13+import { GAS_LIMITS } from './constants';
14+import useLifecycle from './useLifecycle';
1315
1416const STATE = {
1517 NONE: 'NONE',
@@ -22,7 +24,10 @@ const STATE = {
2224 * manage the state around sending and confirming an ethereum transaction
2325 * @param {number} initialGasLimit
2426 */
25−export default function useEthereumTransaction(initialGasLimit = 0) {
27+export default function useEthereumTransaction(
28+ initialGasLimit = GAS_LIMITS.DEFAULT,
29+ initialGasPrice = 20
30+) {
2631 const { wallet, walletType, walletHdPath } = useWallet();
2732 const { web3, networkType } = useNetwork();
2833 const _web3 = need.web3(web3);
@@ -41,7 +46,7 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
4146 const [state, setState] = useState(STATE.NONE);
4247 const [chainId, setChainId] = useState();
4348 const [nonce, setNonce] = useState();
44− const [gasPrice, setGasPrice] = useState(20); // gwei
49+ const [gasPrice, setGasPrice] = useState(initialGasPrice); // gwei
4550 const [gasLimit] = useState(initialGasLimit);
4651 const [unsignedTransaction, setUnsignedTransaction] = useState();
4752 const [signedTransaction, setSignedTransaction] = useState();
@@ -53,8 +58,10 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
5358 const broadcasted = state === STATE.BROADCASTED;
5459 const confirmed = state === STATE.CONFIRMED;
5560
61+ // disable the inputs when:
5662 const inputsLocked = signed || broadcasted || confirmed;
5763
64+ // we can sign when:
5865 const canSign = !initializing && constructed;
5966
6067 const construct = useCallback(txn => setUnsignedTransaction(txn), [
@@ -116,13 +123,15 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
116123 }, [_web3, setError, signedTransaction]);
117124
118125 const reset = useCallback(() => {
126+ setUnsignedTransaction(undefined);
127+ setTxHash(undefined);
128+ setSignedTransaction(undefined);
129+ setGasPrice(initialGasPrice);
119130 setState(STATE.NONE);
120131 setError(undefined);
121− setGasPrice(0);
122− setSignedTransaction(undefined);
123− }, [setError]);
132+ }, [initialGasPrice, setError]);
124133
125− useEffect(() => {
134+ useLifecycle(() => {
126135 (async () => {
127136 try {
128137 setError(undefined);
@@ -139,9 +148,9 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
139148 setError(error);
140149 }
141150 })();
142− }, [_wallet.address, _web3.eth, setError]);
151+ });
143152
144− return {
153+ const values = {
145154 initializing,
146155 construct,
147156 constructed,
@@ -154,24 +163,17 @@ export default function useEthereumTransaction(initialGasLimit = 0) {
154163 reset,
155164 error,
156165 inputsLocked,
166+ //
157167 txHash,
158168 signedTransaction,
159− bind: {
160− constructed,
161− canSign,
162− generateAndSign,
163− signed,
164− broadcast,
165− broadcasted,
166− confirmed,
167− reset,
168− error,
169− gasPrice,
170− setGasPrice,
171− txHash,
172− nonce,
173− chainId,
174− signedTransaction,
175− },
169+ gasPrice,
170+ setGasPrice,
171+ nonce,
172+ chainId,
173+ };
174+
175+ return {
176+ ...values,
177+ bind: values,
176178 };
177179}
src/lib/useKeyfileGenerator.js+3−2
@@ -52,7 +52,7 @@ export default function useKeyfileGenerator(point, manualNetworkSeed) {
5252
5353 if (Nothing.hasInstance(networkSeed)) {
5454 setGenerating(false);
55− console.log('nondeterministic seed!');
55+ console.log('The network seed is nondeterministic — unable to derive.');
5656 // TODO: tell user their seed is nondeterministic
5757 return;
5858 }
@@ -64,7 +64,8 @@ export default function useKeyfileGenerator(point, manualNetworkSeed) {
6464 if (!keysMatchChain(pair, _details)) {
6565 setGenerating(false);
6666 console.log('Derived networking keys do not match public keys on chain.');
67− // TODO: tell usrs their seed is nondeterministic
67+ // TODO: tell users their seed is nondeterministic
68+ return;
6869 }
6970
7071 setKeyfile(compileNetworkingKey(pair, point, networkRevision));
src/lib/validators.js+5−5
@@ -204,7 +204,7 @@ export const validateExactly = (value, error) => m =>
204204 error,
205205 });
206206
207−export const validateNotEqual = (values = []) => m =>
207+export const validateNotAny = (values = []) => m =>
208208 simpleValidatorWrapper({
209209 prevMessage: m,
210210 validator: d => !values.includes(d),
@@ -245,7 +245,7 @@ export const validateMaximumPatpByteLength = byteLength => {
245245 return validateMaximumLength(patpStringLength(byteLength));
246246};
247247
248−export const validateNotNullAddress = validateNotEqual(
249− [ETH_ZERO_ADDR, ETH_ZERO_ADDR_SHORT],
250− 'Cannot be 0x0'
251−);
248+export const validateNotNullAddress = validateNotAny([
249+ ETH_ZERO_ADDR,
250+ ETH_ZERO_ADDR_SHORT,
251+]);
src/views/Admin/AdminNetworkingKeys.js+1−3
@@ -18,7 +18,7 @@ import {
1818} from 'lib/keys';
1919import { formatDotsWithTime } from 'lib/dateFormat';
2020import useEthereumTransaction from 'lib/useEthereumTransaction';
21−import { GAS_LIMITS, ZERO_KEY } from 'lib/constants';
21+import { GAS_LIMITS, ZERO_KEY, CRYPTO_SUITE_VERSION } from 'lib/constants';
2222import { addHexPrefix } from 'lib/wallet';
2323
2424import ViewHeader from 'components/ViewHeader';
@@ -29,8 +29,6 @@ import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
2929import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
3030import NoticeBox from 'components/NoticeBox';
3131
32−const CRYPTO_SUITE_VERSION = 1;
33−
3432const chainKeyProp = name => d =>
3533 d[name] === ZERO_KEY ? Nothing() : Just(d[name]);
3634
src/views/Login/Keystore.js+1−1
@@ -23,7 +23,7 @@ export default function Keystore({ className }) {
2323 // inputs
2424 // keystore: Maybe<String>
2525 const [keystore, setKeystore] = useState(Nothing());
26− const [passphraseInput, { value: passphrase }] = usePassphraseInput({
26+ const [passphraseInput, { data: passphrase }] = usePassphraseInput({
2727 name: 'password',
2828 label: 'Keystore password',
2929 autoFocus: true,