Skip to content

Commit

Permalink
node: use different wallet api key (kyokan#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
rithvikvibhu authored and Falci committed Jun 22, 2022
1 parent 5c2bb34 commit 83b523e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
19 changes: 13 additions & 6 deletions app/background/node/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import { ConnectionTypes, getConnection, getCustomRPC } from '../connections/ser
import FullNode from 'hsd/lib/node/fullnode';
import SPVNode from 'hsd/lib/node/spvnode';
import plugin from 'hsd/lib/wallet/plugin';
import Address from 'hsd/lib/primitives/address';
const blake2b = require('bcrypto/lib/blake2b');
const secp256k1 = require('bcrypto/lib/secp256k1');
const {safeEqual} = require('bcrypto/lib/safe');
import pkg from '../../../package.json';
import { prefixHash } from '../../db/names';
import { get, put } from '../db/service';
Expand All @@ -37,6 +33,7 @@ const { USER_AGENT: HSD_AGENT } = require('hsd/lib/net/common')
const MIN_FEE = new BigNumber(0.01);
const DEFAULT_BLOCK_TIME = 10 * 60 * 1000;
const HSD_PREFIX_DIR_KEY = 'hsdPrefixDir';
const WALLET_API_KEY = 'walletApiKey';
const NODE_API_KEY = 'nodeApiKey';
const NODE_NO_DNS = 'nodeNoDns1';
const SPV_MODE = 'nodeSpvMode';
Expand All @@ -58,6 +55,15 @@ export class NodeService extends EventEmitter {
return newKey;
}

async getWalletAPIKey(nodeApiKey) {
const apiKey = await get(WALLET_API_KEY);
if (apiKey) return apiKey;

// Fallback to node's api key
await put(WALLET_API_KEY, nodeApiKey);
return nodeApiKey;
}

async getNoDns() {
const noDns = await get(NODE_NO_DNS);
if (noDns !== null) {
Expand Down Expand Up @@ -196,6 +202,7 @@ export class NodeService extends EventEmitter {

const dir = await this.getDir();
const spv = await this.getSpvMode();
const walletApiKey = await this.getWalletAPIKey(this.apiKey);

const Node = spv ? SPVNode : FullNode;

Expand All @@ -215,7 +222,7 @@ export class NodeService extends EventEmitter {
indexAddress: true,
indexTX: true,
apiKey: this.apiKey,
walletApiKey: this.apiKey,
walletApiKey: walletApiKey,
cors: true,
rsPort: 9892,
nsPort: 9891,
Expand All @@ -230,7 +237,7 @@ export class NodeService extends EventEmitter {

await this.hsd.ensure();
await this.hsd.open();
this.emit('start local', this.hsd.get('walletdb'), this.apiKey);
this.emit('start local', this.hsd.get('walletdb'), walletApiKey);
await this.hsd.connect();
await this.hsd.startSync();

Expand Down
6 changes: 4 additions & 2 deletions app/background/wallet/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ class WalletService {
this.networkName = this.network.type;
this.walletApiKey = apiKey;

await this.setAPIKey(apiKey);

dispatchToMainWindow({
type: SET_WALLET_NETWORK,
payload: this.networkName,
});
dispatchToMainWindow({
type: SET_API_KEY,
payload: this.walletApiKey,
});

// TODO: This may not work because the plugin is open() already by now
this.node.http.post('/unsafe-update-account-depth', this.handleUnsafeUpdateAccountDepth);
Expand Down
24 changes: 21 additions & 3 deletions app/pages/Settings/APIKeyModal.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import MiniModal from "../../components/Modal/MiniModal";
import React, {Component} from "react";
import {connect} from "react-redux";
import {withRouter} from "react-router-dom";
import PropTypes from "prop-types";
import crypto from "crypto";
import Alert from "../../components/Alert";
import {I18nContext} from "../../utils/i18n";
import * as nodeActions from '../../ducks/node';

@withRouter
@connect(
(state) => ({}),
dispatch => ({
stopNode: () => dispatch(nodeActions.stop()),
startNode: () => dispatch(nodeActions.start()),
}),
)
export default class APIKeyModal extends Component {
static propTypes = {
history: PropTypes.shape({
push: PropTypes.func.isRequired
}).isRequired,
stopNode: PropTypes.func.isRequired,
startNode: PropTypes.func.isRequired,
title: PropTypes.string.isRequired,
closeRoute: PropTypes.string.isRequired,
apiKey: PropTypes.string.isRequired,
Expand Down Expand Up @@ -39,9 +55,14 @@ export default class APIKeyModal extends Component {
});
}

await this.props.stopNode();
await this.props.startNode();

this.setState({
saving: false,
});

this.props.history.goBack();
};

render() {
Expand All @@ -58,9 +79,6 @@ export default class APIKeyModal extends Component {
closeRoute={closeRoute}
title={title}
>
<Alert type="warning">
{t('settingAPIKeyWarning')}
</Alert>
<div className="settings__input-row">
<div className="settings__input-title">
{t('apiKey')}
Expand Down
1 change: 0 additions & 1 deletion locales/XXXXX.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@
"setReminder": "XXXXX",
"settingAPIKeyCTA": "XXXXX",
"settingAPIKeyDesc": "XXXXX",
"settingAPIKeyWarning": "XXXXX",
"settingBackupListingDesc": "XXXXX",
"settingBackupListingTitle": "XXXXX",
"settingBackupWDBDesc": "XXXXX",
Expand Down
1 change: 0 additions & 1 deletion locales/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@
"setReminder": "Establir recordatori",
"settingAPIKeyCTA": "Mostra la clau de l'API",
"settingAPIKeyDesc": "Clau API per a hs-client. Assegurat de seleccionar l'identificador del wallet \"%s\".",
"settingAPIKeyWarning": "La clau de l'API només es farà efectiva després de reiniciar Bob.",
"settingBackupListingDesc": "Baixa una còpia de seguretat de totes les teves fitxes i acompliments",
"settingBackupListingTitle": "Llista de còpia de seguretat",
"settingBackupWDBDesc": "Fes una còpia de seguretat de la base de dades i dels fitxers de la wallet a un directori.",
Expand Down
1 change: 0 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@
"setReminder": "Set reminder",
"settingAPIKeyCTA": "View API Key",
"settingAPIKeyDesc": "API key for hs-client. Make sure you select the wallet id \"%s\".",
"settingAPIKeyWarning": "API Key will only take effect after you restart Bob.",
"settingBackupListingDesc": "Download backup of all your listings and fulfillments",
"settingBackupListingTitle": "Backup listing",
"settingBackupWDBDesc": "Back up wallet database and files to a directory.",
Expand Down
1 change: 0 additions & 1 deletion locales/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@
"setReminder": "Establecer recordatorio",
"settingAPIKeyCTA": "Muestra la clave de la API",
"settingAPIKeyDesc": "Clave API para hs-client. Asegúrese de seleccionar la identificación de la wallet \"%s\".",
"settingAPIKeyWarning": "La clave de la API sólo se hará efectiva después de reiniciar Bob.",
"settingBackupListingDesc": "Baja una copia de seguridad de todas tus fichas y cumplimientos",
"settingBackupListingTitle": "Lista de copia de seguridad",
"settingBackupWDBDesc": "Haz una copia de seguridad de la base de datos y de los archivos de la wallet a un directorio.",
Expand Down
1 change: 0 additions & 1 deletion locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@
"sendTxTimeLabel": "Transaction time",
"setReminder": "Set reminder",
"settingAPIKeyCTA": "View API Key",
"settingAPIKeyWarning": "API Key will only take effect after you restart Bob.",
"settingBackupListingDesc": "Download backup of all your listings and fulfillments",
"settingBackupListingTitle": "Backup listing",
"settingBackupWDBDesc": "Back up wallet database and files to a directory.",
Expand Down

0 comments on commit 83b523e

Please sign in to comment.