Skip to content

Commit

Permalink
fix: JWT auth (#1218)
Browse files Browse the repository at this point in the history
* Change / add node auth types and integrate into existing function signatures

* Remove extra commas from TS code

* Remove unnecessary code
  • Loading branch information
maxwellmattryan committed Jun 22, 2021
1 parent ad770a5 commit 80aa2da
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 47 deletions.
5 changes: 3 additions & 2 deletions packages/backend/bindings/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
getLegacySeedChecksum as _getLegacySeedChecksum
} from '../../../shared/lib/typings/wallet'
import { ClientOptions } from '../../../shared/lib/typings/client'
import { NodeAuth } from '../../../shared/lib/typings/node'

const addon = require('../index.node')
const mailbox = []
Expand Down Expand Up @@ -220,8 +221,8 @@ export const api = {
((__ids: CommunicationIds) => Promise<string>) {
return (__ids: CommunicationIds) => _sendMigrationBundle(sendMessage, __ids, nodes, bundleHash, mwm)
},
getNodeInfo: function (accountId: AccountIdentifier, url?: string): ((__ids: CommunicationIds) => Promise<string>) {
return (__ids: CommunicationIds) => _getNodeInfo(sendMessage, __ids, accountId, url)
getNodeInfo: function (accountId: AccountIdentifier, url?: string, auth?: NodeAuth): ((__ids: CommunicationIds) => Promise<string>) {
return (__ids: CommunicationIds) => _getNodeInfo(sendMessage, __ids, accountId, url, auth)
},

// Event emitters
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/lib/network.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Node } from './typings/client'
import type { Node } from './typings/node'
import { isValidHttpsUrl, isValidUrl } from './utils'

const DEFAULT_NETWORK = 'chrysalis-mainnet'

// TODO: Update default nodes
const DEFAULT_NODES: Node[] = [
'https://chrysalis-nodes.iota.org',
'https://chrysalis-nodes.iota.cafe'
].map((url) => ({
url,
auth: {
jwt: '',
username: '',
password: ''
}
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/lib/networkStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export async function fetchNetworkStatus(): Promise<void> {
const node = clientOptions.node ?? getOfficialNodes()[0]

try {
// TODO add user/pass support when implemented in wallet.rs
const response = await asyncGetNodeInfo(account0.id, node.url)
const response = await asyncGetNodeInfo(account0.id, node.url, node.auth)

const timeSinceLastMsInMinutes = (Date.now() - (response.nodeinfo.latestMilestoneTimestamp * 1000)) / 60000;
let health = 0; //bad
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/lib/profile.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AvailableExchangeRates } from 'shared/lib/currency'
import { persistent } from 'shared/lib/helpers'
import { generateRandomId } from 'shared/lib/utils'
import { asyncRemoveStorage, destroyActor, getStoragePath, getWalletStoragePath } from 'shared/lib/wallet'
import { destroyActor, getStoragePath, getWalletStoragePath } from 'shared/lib/wallet'
import { derived, get, Readable, writable } from 'svelte/store'
import type { ChartSelectors } from './chart'
import { Electron } from './electron'
import {
HistoryDataProps
} from './marketData'
import type { Node } from './typings/client'
import type { Node } from './typings/node'

export interface MigratedTransaction {
address: string;
Expand Down
7 changes: 4 additions & 3 deletions packages/shared/lib/typings/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Bridge, CommunicationIds } from './bridge'
import type { Message } from './message'
import type { Address } from './address'
import type { ClientOptions } from './client'
import type { NodeAuth } from './node'

export enum MessageType {}

Expand Down Expand Up @@ -161,7 +162,7 @@ function _callAccountMethod(
accountId,
method: {
name: AccountMethod[methodName],
data,
...data
},
},
})
Expand Down Expand Up @@ -222,6 +223,6 @@ export function syncAccount(
return _callAccountMethod(bridge, __ids, AccountMethod.SyncAccount, accountId, options || {})
}

export function getNodeInfo(bridge: Bridge, __ids: CommunicationIds, accountId: AccountIdentifier, url?: string): Promise<string> {
return _callAccountMethod(bridge, __ids, AccountMethod.GetNodeInfo, accountId, url)
export function getNodeInfo(bridge: Bridge, __ids: CommunicationIds, accountId: AccountIdentifier, url?: string, auth?: NodeAuth): Promise<string> {
return _callAccountMethod(bridge, __ids, AccountMethod.GetNodeInfo, accountId, { url: url, auth: auth })
}
4 changes: 2 additions & 2 deletions packages/shared/lib/typings/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { Account, AccountIdentifier, Balance, SyncedAccount } from './accou
import type { Address } from './address'
import type { ErrorEventPayload } from './events'
import type { Message } from './message'
import type { NodeInfo } from './node'
import type { MigrationBundle, MigrationData, SendMigrationBundleResponse } from './migration'
import type { NodeInfo, StrongholdStatus } from './wallet'
import type { StrongholdStatus } from './wallet'

export interface Actor {
destroy(): void
Expand Down Expand Up @@ -149,7 +150,6 @@ export type MessageResponse =
| SetAliasResponse
| DeleteStorageResponse
| LockStrongholdResponse
| StrongholdStatusResponse
| UpdatedAllClientOptions
| LegacySeedChecksum
// Migration types
Expand Down
9 changes: 1 addition & 8 deletions packages/shared/lib/typings/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
export interface Node {
url: string
auth?: {
password: string
username: string
},
disabled?: boolean
}
import type { Node } from './node';

export interface ClientOptions {
nodes?: Node[]
Expand Down
31 changes: 31 additions & 0 deletions packages/shared/lib/typings/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export interface NodeAuth {
jwt?: string,
username?: string,
password?: string
}

export interface Node {
url: string
auth?: NodeAuth
disabled?: boolean
}

export interface NodeInfo {
nodeinfo: {
name: string;
version: string;
isHealthy: boolean;
networkId: string;
minPoWScore: number;
bech32HRP: string;
latestMilestoneIndex: number;
latestMilestoneTimestamp: number;
confirmedMilestoneIndex: number;
pruningIndex: number;
features: string[];
messagesPerSecond: number;
referencedMessagesPerSecond: number;
referencedRate: number;
}
url: string
}
20 changes: 0 additions & 20 deletions packages/shared/lib/typings/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ export interface Duration {
nanos: number
}

export interface NodeInfo {
nodeinfo: {
name: string;
version: string;
isHealthy: boolean;
networkId: string;
minPoWScore: number;
bech32HRP: string;
latestMilestoneIndex: number;
latestMilestoneTimestamp: number;
confirmedMilestoneIndex: number;
pruningIndex: number;
features: string[];
messagesPerSecond: number;
referencedMessagesPerSecond: number;
referencedRate: number;
}
url: string
}

export function backup(bridge: Bridge, __ids: CommunicationIds, destinationPath: string, password: string) {
return bridge({
actorId: __ids.actorId,
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { Address } from './typings/address'
import type { MessageResponse } from './typings/bridge'
import { ResponseTypes } from './typings/bridge'
import type { Message } from './typings/message'
import type { NodeInfo, StrongholdStatus } from './typings/wallet'
import type { NodeInfo } from './typings/node'
import type { StrongholdStatus } from './typings/wallet'

type Validators =
| IdValidator
Expand Down
17 changes: 12 additions & 5 deletions packages/shared/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { HistoryDataProps } from 'shared/lib/marketData'
import { getOfficialNetwork, getOfficialNodes } from 'shared/lib/network'
import { showAppNotification, showSystemNotification } from 'shared/lib/notifications'
import { activeProfile, isStrongholdLocked, updateProfile } from 'shared/lib/profile'
import type { Account, Account as BaseAccount, AccountToCreate, Balance, SyncedAccount } from 'shared/lib/typings/account'
import type {
Account,
Account as BaseAccount,
AccountToCreate,
Balance,
SyncedAccount
} from 'shared/lib/typings/account'
import type { Address } from 'shared/lib/typings/address'
import type { Actor } from 'shared/lib/typings/bridge'
import type {
Expand All @@ -26,7 +32,8 @@ import type { MigrationBundle, MigrationData, SendMigrationBundleResponse } from
import { formatUnitBestMatch } from 'shared/lib/units'
import { get, writable, Writable } from 'svelte/store'
import type { ClientOptions } from './typings/client'
import type { Duration, NodeInfo, StrongholdStatus } from './typings/wallet'
import type { NodeAuth, NodeInfo } from './typings/node'
import type { Duration, StrongholdStatus } from './typings/wallet'

const ACCOUNT_COLORS = ['turquoise', 'green', 'orange', 'yellow', 'purple', 'pink']

Expand Down Expand Up @@ -185,7 +192,7 @@ export const api: {
removeStorage(callbacks: { onSuccess: (response: Event<void>) => void, onError: (err: ErrorEventPayload) => void })
setClientOptions(clientOptions: ClientOptions, callbacks: { onSuccess: (response: Event<void>) => void, onError: (err: ErrorEventPayload) => void })
setStrongholdPasswordClearInterval(interval: Duration, callbacks: { onSuccess: (response: Event<void>) => void, onError: (err: ErrorEventPayload) => void })
getNodeInfo(accountId: string, url: string | undefined, callbacks: { onSuccess: (response: Event<NodeInfo>) => void, onError: (err: ErrorEventPayload) => void })
getNodeInfo(accountId: string, url: string | undefined, auth: NodeAuth | undefined, callbacks: { onSuccess: (response: Event<NodeInfo>) => void, onError: (err: ErrorEventPayload) => void })

// Legacy seed APIs
getLegacySeedChecksum(seed: string, callbacks: { onSuccess: (response: Event<string>) => void, onError: (err: ErrorEventPayload) => void })
Expand Down Expand Up @@ -484,9 +491,9 @@ export const asyncSyncAccounts = (addressIndex?, gapLimit?, accountDiscoveryThre
})
}

export const asyncGetNodeInfo = (accountId, url) => {
export const asyncGetNodeInfo = (accountId: string, url?: string, auth?: NodeAuth) => {
return new Promise<NodeInfo>((resolve, reject) => {
api.getNodeInfo(accountId, url, {
api.getNodeInfo(accountId, url, auth, {
onSuccess(response) {
resolve(response.payload)
},
Expand Down

0 comments on commit 80aa2da

Please sign in to comment.