Skip to content

Commit

Permalink
Fix the formatting (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
allen-at-work authored and GitHub Enterprise committed Sep 21, 2021
1 parent 92500e8 commit f927053
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 60 deletions.
147 changes: 88 additions & 59 deletions js/src/relay/WalletLinkRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@

import bind from "bind-decorator"
import { Observable, of } from "rxjs"
import { catchError, distinctUntilChanged, filter, map, mergeMap, timeout } from 'rxjs/operators';
import {
catchError,
distinctUntilChanged,
filter,
map,
mergeMap,
timeout
} from "rxjs/operators"
import { ServerMessageEvent } from "../connection/ServerMessage"
import { WalletLinkAnalytics } from "../connection/WalletLinkAnalytics"
import { WalletLinkConnection } from "../connection/WalletLinkConnection"
import { WalletLinkAnalyticsAbstract } from "../init"
import { ScopedLocalStorage } from "../lib/ScopedLocalStorage"
import { WalletLinkUI, WalletLinkUIOptions } from "../provider/WalletLinkUI"
import { AddressString, IntNumber, RegExpString } from "../types"
import { bigIntStringFromBN, hexStringFromBuffer, randomBytesHex } from '../util';
import {
bigIntStringFromBN,
hexStringFromBuffer,
randomBytesHex
} from "../util"
import * as aes256gcm from "./aes256gcm"
import { EthereumTransactionParams } from "./EthereumTransactionParams"
import { RelayMessage } from "./RelayMessage"
import { Session } from "./Session"
import {
WalletLinkRelayAbstract,
WALLET_USER_NAME_KEY
} from "./WalletLinkRelayAbstract"
import { WalletLinkRelayEventManager } from "./WalletLinkRelayEventManager"
import { Web3Method } from "./Web3Method"
import {
AddEthereumChainRequest,
Expand All @@ -27,8 +45,8 @@ import {
SignEthereumTransactionRequest,
SubmitEthereumTransactionRequest,
SwitchEthereumChainRequest,
Web3Request,
} from './Web3Request';
Web3Request
} from "./Web3Request"
import { Web3RequestCanceledMessage } from "./Web3RequestCanceledMessage"
import { Web3RequestMessage } from "./Web3RequestMessage"
import {
Expand All @@ -44,17 +62,12 @@ import {
SignEthereumTransactionResponse,
SubmitEthereumTransactionResponse,
SwitchEthereumChainResponse,
Web3Response,
} from './Web3Response';
Web3Response
} from "./Web3Response"
import {
isWeb3ResponseMessage,
Web3ResponseMessage
} from "./Web3ResponseMessage"
import { WalletLinkUI, WalletLinkUIOptions } from "../provider/WalletLinkUI"
import { WalletLinkRelayEventManager } from "./WalletLinkRelayEventManager"
import { WalletLinkRelayAbstract, WALLET_USER_NAME_KEY } from "./WalletLinkRelayAbstract"
import { EthereumTransactionParams } from "./EthereumTransactionParams"


export interface WalletLinkRelayOptions {
walletLinkUrl: string
Expand Down Expand Up @@ -132,13 +145,15 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
mergeMap(c =>
aes256gcm.decrypt(c.metadata.ChainId!, this._session.secret)
)
)
.pipe(distinctUntilChanged())
.subscribe({ next: chainId => {
if (this.chainIdCallback) {
this.chainIdCallback(chainId!)
.subscribe({
next: chainId => {
if (this.chainIdCallback) {
this.chainIdCallback(chainId!)
}
}
}
})
})

this.connection.sessionConfig$
.pipe(filter(c => c.metadata && c.metadata.JsonRpcUrl !== undefined))
Expand All @@ -151,10 +166,10 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
.subscribe({
next: jsonRpcURl => {
if (this.jsonRpcUrlCallback) {
this.jsonRpcUrlCallback(jsonRpcURl!);
this.jsonRpcUrlCallback(jsonRpcURl!)
}
},
});
}
})

this.connection.sessionConfig$
.pipe(filter(c => c.metadata && c.metadata.EthereumAddress !== undefined))
Expand All @@ -178,7 +193,9 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
).forEach(id => {
const message = Web3ResponseMessage({
id,
response: RequestEthereumAccountsResponse([selectedAddress as AddressString])
response: RequestEthereumAccountsResponse([
selectedAddress as AddressString
])
})
this.invokeCallback({ ...message, id })
})
Expand All @@ -187,7 +204,7 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
}
})

this.ui = options.walletLinkUIConstructor({
this.ui = options.walletLinkUIConstructor({
walletLinkUrl: options.walletLinkUrl,
version: options.version,
darkMode: options.darkMode,
Expand Down Expand Up @@ -401,10 +418,7 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
iconUrls?: string[],
nativeCurrency?: { name: string; symbol: string; decimals: number }
): Promise<AddEthereumChainResponse> {
return this.sendRequest<
AddEthereumChainRequest,
AddEthereumChainResponse
>({
return this.sendRequest<AddEthereumChainRequest, AddEthereumChainResponse>({
method: Web3Method.addEthereumChain,
params: {
chainId,
Expand All @@ -426,12 +440,13 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
* we don't wait for a response from the mobile client, we continue as soon as we know the server
* has received the message. Hence why we have a separate method here.
*/
private sendChildRequest(request: ChildRequestEthereumAccountsRequest): Promise<ChildRequestEthereumAccountsResponse> {
private sendChildRequest(
request: ChildRequestEthereumAccountsRequest
): Promise<ChildRequestEthereumAccountsResponse> {
return new Promise((resolve, reject) => {
const id = randomBytesHex(8)
const message = Web3RequestMessage({ id, request })
this.publishEvent("Web3Request", message, true)
.subscribe({
this.publishEvent("Web3Request", message, true).subscribe({
next: ret => {
resolve(ChildRequestEthereumAccountsResponse(ret))
},
Expand All @@ -448,8 +463,10 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
return new Promise((resolve, reject) => {
let hideSnackbarItem: (() => void) | null = null
const id = randomBytesHex(8)
const isRequestAccounts = request.method === Web3Method.requestEthereumAccounts
const isSwitchEthereumChain = request.method === Web3Method.switchEthereumChain
const isRequestAccounts =
request.method === Web3Method.requestEthereumAccounts
const isSwitchEthereumChain =
request.method === Web3Method.switchEthereumChain
const cancel = () => {
this.publishWeb3RequestCanceledEvent(id)
this.handleWeb3ResponseMessage(
Expand Down Expand Up @@ -493,35 +510,38 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
}

WalletLinkRelay.accountRequestCallbackIds.add(id)
} else if (request.method === Web3Method.switchEthereumChain || request.method === Web3Method.addEthereumChain) {
} else if (
request.method === Web3Method.switchEthereumChain ||
request.method === Web3Method.addEthereumChain
) {
const cancel = () => {
this.handleWeb3ResponseMessage(
Web3ResponseMessage({
id,
response: SwitchEthereumChainResponse(false),
}),
);
};
response: SwitchEthereumChainResponse(false)
})
)
}
const approve = () => {
this.handleWeb3ResponseMessage(
Web3ResponseMessage({
id,
response: SwitchEthereumChainResponse(true),
}),
);
};
response: SwitchEthereumChainResponse(true)
})
)
}

this.ui.switchEthereumChain({
onCancel: cancel,
onApprove: approve,
chainId: (request as SwitchEthereumChainRequest).params.chainId,
});
chainId: (request as SwitchEthereumChainRequest).params.chainId
})

if (!this.ui.inlineSwitchEthereumChain()) {
hideSnackbarItem = this.ui.showConnecting({
onCancel: cancel,
onResetConnection: this.resetAndReload,
});
onResetConnection: this.resetAndReload
})
}
} else {
hideSnackbarItem = this.ui.showConnecting({
Expand All @@ -540,8 +560,10 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
resolve(response as U)
})

if ((isRequestAccounts && this.ui.inlineAccountsResponse()) ||
(isSwitchEthereumChain && this.ui.inlineSwitchEthereumChain())) {
if (
(isRequestAccounts && this.ui.inlineAccountsResponse()) ||
(isSwitchEthereumChain && this.ui.inlineSwitchEthereumChain())
) {
return
}

Expand All @@ -553,15 +575,17 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
this.ui.setConnectDisabled(disabled)
}

public setAccountsCallback(accountsCallback: (accounts: [string]) => void) {
public setAccountsCallback(accountsCallback: (accounts: [string]) => void) {
this.accountsCallback = accountsCallback
}

public setChainIdCallback(chainIdCallback: (chainId: string) => void) {
this.chainIdCallback = chainIdCallback
}

public setJsonRpcUrlCallback(jsonRpcUrlCallback: (jsonRpcUrl: string) => void) {
public setJsonRpcUrlCallback(
jsonRpcUrlCallback: (jsonRpcUrl: string) => void
) {
this.jsonRpcUrlCallback = jsonRpcUrlCallback
}

Expand Down Expand Up @@ -593,23 +617,28 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
callWebhook: boolean
): Observable<string> {
const secret = this.session.secret
return new Observable<string>((subscriber) => {
aes256gcm.encrypt(
JSON.stringify({ ...message, origin: location.origin }),
secret
).then((encrypted: string) => {
subscriber.next(encrypted)
subscriber.complete()
return new Observable<string>(subscriber => {
aes256gcm
.encrypt(
JSON.stringify({ ...message, origin: location.origin }),
secret
)
.then((encrypted: string) => {
subscriber.next(encrypted)
subscriber.complete()
})
}).pipe(
mergeMap((encrypted: string) => {
return this.connection.publishEvent(event, encrypted, callWebhook)
})
}).pipe(mergeMap((encrypted: string) => {
return this.connection.publishEvent(event, encrypted, callWebhook)
}))
)
}

@bind
private handleIncomingEvent(event: ServerMessageEvent): void {
try {
aes256gcm.decrypt(event.data, this.session.secret)
aes256gcm
.decrypt(event.data, this.session.secret)
.pipe(map(c => JSON.parse(c)))
.subscribe({
next: json => {
Expand Down Expand Up @@ -652,7 +681,7 @@ export class WalletLinkRelay implements WalletLinkRelayAbstract {
return this.sendRequest<
SwitchEthereumChainRequest,
SwitchEthereumChainResponse
>({
>({
method: Web3Method.switchEthereumChain,
params: {
chainId
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wallet/walletlink",
"version": "2.1.12",
"version": "2.1.13",
"description": "WalletLink JavaScript SDK Internal",
"keywords": [
"cipher",
Expand Down

0 comments on commit f927053

Please sign in to comment.