Skip to content

Commit

Permalink
feat(rpc): add flag for connext to unlock response
Browse files Browse the repository at this point in the history
This adds a flag to the `UnlockNode` response to indicate whether
a Connext client is connected and ready to accept calls. In the cli,
`ETH` gets added to the list of wallets that are unlocked and ready when
this new flag is true.

Closes #1932.
  • Loading branch information
sangaman committed Nov 10, 2020
1 parent c1af693 commit ac18c97
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 308 deletions.
1 change: 1 addition & 0 deletions docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions lib/cli/commands/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ export const describe = 'unlock local xud node';

export const builder = {};

const formatOutput = (response: UnlockNodeResponse.AsObject) => {
export const formatOutput = (response: UnlockNodeResponse.AsObject) => {
console.log('xud was unlocked successfully');
if (response.unlockedLndsList.length) {
console.log(`The following wallets were unlocked: ${response.unlockedLndsList.join(', ')}`);
const readyClients = response.unlockedLndsList;
if (response.connextReady) {
readyClients.push('ETH');
}
if (readyClients.length) {
console.log(`The following wallets are unlocked and ready: ${readyClients.join(', ')}`);
}
if (response.lockedLndsList.length) {
console.log(`The following wallets could not be unlocked: ${response.lockedLndsList.join(', ')}`);
Expand Down
1 change: 1 addition & 0 deletions lib/grpc/GrpcInitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class GrpcInitService {
const response = new xudrpc.UnlockNodeResponse();
response.setUnlockedLndsList(unlockNodeResult.unlockedLndClients);
response.setLockedLndsList(unlockNodeResult.lockedLndClients);
response.setConnextReady(unlockNodeResult.connextReady);

callback(null, response);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/proto/annotations_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/proto/xudp2p_grpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/proto/xudrpc.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/proto/xudrpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion lib/proto/xudrpc_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 28 additions & 41 deletions lib/swaps/SwapClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Level, Loggers } from '../Logger';
import NodeKey from '../nodekey/NodeKey';
import { Currency, OwnLimitOrder } from '../orderbook/types';
import Peer from '../p2p/Peer';
import { encipher } from '../utils/seedutil';
import { UnitConverter } from '../utils/UnitConverter';
import errors from './errors';
import SwapClient, { ClientStatus } from './SwapClient';
Expand Down Expand Up @@ -144,9 +143,8 @@ class SwapClientManager extends EventEmitter {
* Throws an error if any lnd clients remain unreachable.
*/
public waitForLnd = async () => {
const lndClients = this.getLndClientsMap().values();
const lndAvailablePromises: Promise<void>[] = [];
for (const lndClient of lndClients) {
for (const lndClient of this.lndClients.values()) {
lndAvailablePromises.push(new Promise<void>((resolve, reject) => {
if (lndClient.isDisconnected() || lndClient.isNotInitialized()) {
const onAvailable = () => {
Expand Down Expand Up @@ -343,49 +341,38 @@ class SwapClientManager extends EventEmitter {
const unlockWalletPromises: Promise<any>[] = [];
const unlockedLndClients: string[] = [];
const lockedLndClients: string[] = [];
let connextReady = false;

for (const lndClient of this.lndClients.values()) {
if (lndClient.isWaitingUnlock()) {
const unlockWalletPromise = lndClient.unlockWallet(walletPassword).then(() => {
unlockedLndClients.push(lndClient.currency);
}).catch((err) => {
lockedLndClients.push(lndClient.currency);
lndClient.logger.debug(`could not unlock wallet: ${err.message}`);
});
unlockWalletPromises.push(unlockWalletPromise);
} else if (lndClient.isConnected()) {
// if the swap client is already unlocked, we add it to the list
unlockedLndClients.push(lndClient.currency);
} else if (lndClient.isDisconnected() || lndClient.isMisconfigured() || lndClient.isNotInitialized()) {
// if the swap client is not connected, we treat it as locked since lnd will likely be locked when it comes online
lockedLndClients.push(lndClient.currency);
}
}

for (const swapClient of this.swapClients.values()) {
if (isLndClient(swapClient)) {
if (swapClient.isWaitingUnlock()) {
const unlockWalletPromise = swapClient.unlockWallet(walletPassword).then(() => {
unlockedLndClients.push(swapClient.currency);
}).catch(async (err) => {
let walletCreated = false;
if (err.details === 'wallet not found') {
// this wallet hasn't been initialized, so we will try to initialize it now
const decipheredSeed = nodeKey.privKey.slice(0, 19);
const decipheredSeedHex = decipheredSeed.toString('hex');
const seedMnemonic = await encipher(decipheredSeedHex);

try {
await swapClient.initWallet(this.walletPassword ?? '', seedMnemonic);
walletCreated = true;
} catch (err) {
swapClient.logger.error('could not initialize lnd wallet', err);
}
}

if (!walletCreated) {
lockedLndClients.push(swapClient.currency);
swapClient.logger.debug(`could not unlock wallet: ${err.message}`);
}
});
unlockWalletPromises.push(unlockWalletPromise);
} else if (swapClient.isDisconnected() || swapClient.isMisconfigured() || swapClient.isNotInitialized()) {
// if the swap client is not connected, we treat it as locked since lnd will likely be locked when it comes online
lockedLndClients.push(swapClient.currency);
}
} else if (isConnextClient(swapClient)) {
// TODO(connext): unlock Connext using connextSeed
await this.initConnext(
nodeKey.childSeed(SwapClientType.Connext),
);
if (this.connextClient) {
await this.initConnext(
nodeKey.childSeed(SwapClientType.Connext),
);
if (this.connextClient.isConnected()) {
connextReady = true;
}
}

await Promise.all(unlockWalletPromises);

return { unlockedLndClients, lockedLndClients };
return { unlockedLndClients, lockedLndClients, connextReady };
}

/**
Expand Down Expand Up @@ -461,7 +448,7 @@ class SwapClientManager extends EventEmitter {
* Gets a map of all lnd clients.
* @returns A map of currencies to lnd clients.
*/
public getLndClientsMap = () => {
public get lndClients() {
const lndClients: Map<string, LndClient> = new Map();
this.swapClients.forEach((swapClient, currency) => {
if (isLndClient(swapClient)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class Swaps extends EventEmitter {
}

public init = async () => {
// update pool with current lnd & connext pubkeys
this.swapClientManager.getLndClientsMap().forEach(({ pubKey, chain, currency, uris }) => {
// update pool with lnd pubkeys
this.swapClientManager.lndClients.forEach(({ pubKey, chain, currency, uris }) => {
if (pubKey && chain) {
this.pool.updateLndState({
currency,
Expand Down
2 changes: 2 additions & 0 deletions proto/xudrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ message UnlockNodeResponse {
repeated string unlocked_lnds = 1;
// The list of lnd clients that could not be unlocked.
repeated string locked_lnds = 3;
// Whether the Connext client is connected and ready to accept calls.
bool connext_ready = 4;
}

message WithdrawRequest {
Expand Down

0 comments on commit ac18c97

Please sign in to comment.