Skip to content

Commit

Permalink
fix: get node network
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin committed Jan 19, 2022
1 parent 74a8acd commit eed5e50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/server/modules/api/node/node.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { toWithError } from 'src/server/utils/async';
import { FetchService } from '../../fetch/fetch.service';
import { ConfigService } from '@nestjs/config';
import { gql } from 'graphql-tag';
import { getNetwork } from 'src/server/utils/network';

@Resolver(LightningBalance)
export class LightningBalanceResolver {
Expand Down Expand Up @@ -148,18 +149,23 @@ export class NodeFieldResolver {
return null;
}

const { data, error } = await this.fetchService.graphqlFetchWithProxy(
this.configService.get('urls.amboss'),
gql`
query GetNodeAlias($pubkey: String!) {
getNodeAlias(pubkey: $pubkey)
}
`,
{ pubkey: publicKey }
);

if (data?.getNodeAlias && !error) {
return { alias: data.getNodeAlias, public_key: publicKey };
const nodeInfo = await this.nodeService.getWalletInfo(id);
const network = getNetwork(nodeInfo?.chains?.[0] || '');

if (network === 'btc') {
const { data, error } = await this.fetchService.graphqlFetchWithProxy(
this.configService.get('urls.amboss'),
gql`
query GetNodeAlias($pubkey: String!) {
getNodeAlias(pubkey: $pubkey)
}
`,
{ pubkey: publicKey }
);

if (data?.getNodeAlias && !error) {
return { alias: data.getNodeAlias, public_key: publicKey };
}
}

const [info, nodeError] = await toWithError(
Expand Down
1 change: 1 addition & 0 deletions src/server/modules/node/lnd/lnd.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export type GetWalletInfoType = {
public_key: string;
version: string;
current_block_height: number;
chains: string[];
};

export type DiffieHellmanComputeSecretType = {
Expand Down
21 changes: 21 additions & 0 deletions src/server/utils/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { reversedBytes } from './string';

const chains = {
btc: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
btcregtest:
'0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
btctestnet:
'000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943',
};

export const getNetwork = (chain: string) => {
if (!chain) {
return undefined;
}

const network = Object.keys(chains).find(network => {
return chain === reversedBytes(chains[network]);
});

return network;
};
3 changes: 3 additions & 0 deletions src/server/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export const shorten = (text: string): string => {

return `${beginning}...${end}`;
};

export const reversedBytes = hex =>
Buffer.from(hex, 'hex').reverse().toString('hex');

0 comments on commit eed5e50

Please sign in to comment.