Skip to content

Commit

Permalink
#trivial Consolidate WLSigner and WalletLinkAdapter (#1272)
Browse files Browse the repository at this point in the history
* consolidate wlrelayadapter and wlsigner

* move

* revert moving

* min diff

* fix

* fix
  • Loading branch information
bangtoven authored May 5, 2024
1 parent 1a920ae commit 3866ee8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 86 deletions.
4 changes: 2 additions & 2 deletions packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CoinbaseWalletProvider } from './CoinbaseWalletProvider';
import { standardErrors } from './core/error';
import { fetchSignerType, loadSignerType, storeSignerType } from './sign/SignHandler';
import { fetchSignerType, loadSignerType, storeSignerType } from './sign/util';
import { AddressString } from ':core/type';

function createProvider() {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('CoinbaseWalletProvider', () => {
});
});

jest.mock('./sign/SignHandler');
jest.mock('./sign/util');
const mockFetchSignerType = fetchSignerType as jest.Mock;
const mockLoadSignerType = loadSignerType as jest.Mock;
const mockStoreSignerType = storeSignerType as jest.Mock;
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet-sdk/src/CoinbaseWalletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { AddressString, Chain, IntNumber } from './core/type';
import { areAddressArraysEqual, hexStringFromIntNumber } from './core/type/util';
import { AccountsUpdate, ChainUpdate, Signer } from './sign/interface';
import { SCWSigner } from './sign/scw/SCWSigner';
import { fetchSignerType, loadSignerType, storeSignerType } from './sign/SignHandler';
import { WLSigner } from './sign/walletlink/WLSigner';
import { fetchSignerType, loadSignerType, storeSignerType } from './sign/util';
import { WalletLinkSigner } from './sign/walletlink/WalletLinkSigner';
import { checkErrorForInvalidRequestArgs, fetchRPCRequest } from './util/provider';
import { Communicator } from ':core/communicator/Communicator';
import { SignerType } from ':core/message';
Expand Down Expand Up @@ -179,7 +179,7 @@ export class CoinbaseWalletProvider extends EventEmitter implements ProviderInte
postMessageToPopup: this.communicator.postMessage.bind(this),
});
case 'walletlink':
return new WLSigner({
return new WalletLinkSigner({
metadata: this.metadata,
updateListener: this.updateListener,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchSignerType } from './SignHandler';
import { fetchSignerType } from './util';
import { Communicator } from ':core/communicator/Communicator';
import { Preference } from ':core/provider/interface';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WLSigner } from './walletlink/WLSigner';
import { WalletLinkSigner } from './walletlink/WalletLinkSigner';
import { Communicator } from ':core/communicator/Communicator';
import { ConfigMessage, SignerType } from ':core/message';
import { AppMetadata, Preference } from ':core/provider/interface';
Expand Down Expand Up @@ -46,7 +46,7 @@ async function listenForWalletLinkSessionRequest(

// temporary walletlink signer instance to handle WalletLinkSessionRequest
// will revisit this when refactoring the walletlink signer
const walletlink = new WLSigner({
const walletlink = new WalletLinkSigner({
metadata,
});

Expand Down
37 changes: 0 additions & 37 deletions packages/wallet-sdk/src/sign/walletlink/WLSigner.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { fireEvent } from '@testing-library/preact';

import { LOCAL_STORAGE_ADDRESSES_KEY } from './constants';
import { MOCK_ADDERESS, MOCK_SIGNED_TX, MOCK_TX, MOCK_TYPED_DATA } from './mocks/fixtures';
import { mockedWalletLinkRelay } from './mocks/relay';
import { WalletLinkRelay } from './WalletLinkRelay';
import { WLRelayAdapter } from './WLRelayAdapter';
import { LOCAL_STORAGE_ADDRESSES_KEY } from './relay/constants';
import { MOCK_ADDERESS, MOCK_SIGNED_TX, MOCK_TX, MOCK_TYPED_DATA } from './relay/mocks/fixtures';
import { mockedWalletLinkRelay } from './relay/mocks/relay';
import { WalletLinkRelay } from './relay/WalletLinkRelay';
import { WalletLinkSigner } from './WalletLinkSigner';
import { WALLETLINK_URL } from ':core/constants';
import { standardErrorCodes, standardErrors } from ':core/error';
import { AddressString } from ':core/type';
import { ScopedLocalStorage } from ':util/ScopedLocalStorage';

jest.mock('./WalletLinkRelay', () => {
jest.mock('./relay/WalletLinkRelay', () => {
return {
WalletLinkRelay: mockedWalletLinkRelay,
};
});

const testWalletLinkUrl = 'http://test-url';
const testStorage = new ScopedLocalStorage('walletlink', testWalletLinkUrl);
const testStorage = new ScopedLocalStorage('walletlink', WALLETLINK_URL);

const createAdapter = (options?: { relay?: WalletLinkRelay }) => {
const adapter = new WLRelayAdapter({
appName: 'test',
appLogoUrl: null,
walletlinkUrl: testWalletLinkUrl,
const adapter = new WalletLinkSigner({
metadata: { appName: 'test', appLogoUrl: null, appChainIds: [1] },
updateListener: {
onAccountsUpdate: () => {},
onChainUpdate: () => {},
Expand Down Expand Up @@ -50,7 +48,7 @@ describe('LegacyProvider', () => {
const spy = jest.spyOn(relay, 'resetAndReload');

const provider = createAdapter({ relay });
await provider.close();
await provider.disconnect();
expect(spy).toHaveBeenCalled();
});

Expand Down Expand Up @@ -122,7 +120,7 @@ describe('LegacyProvider', () => {
});

describe('RPC Methods', () => {
let provider: WLRelayAdapter | null = null;
let provider: WalletLinkSigner | null = null;
beforeEach(() => {
testStorage.setItem(LOCAL_STORAGE_ADDRESSES_KEY, MOCK_ADDERESS.toLowerCase());
provider = createAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

// Copyright (c) 2018-2024 Coinbase, Inc. <https://www.coinbase.com/>

import eip712 from '../../../vendor-js/eth-eip712-util';
import { StateUpdateListener } from '../../interface';
import { LOCAL_STORAGE_ADDRESSES_KEY } from './constants';
import { RelayEventManager } from './RelayEventManager';
import { EthereumTransactionParams } from './type/EthereumTransactionParams';
import { JSONRPCRequest, JSONRPCResponse } from './type/JSONRPC';
import { isErrorResponse, Web3Response } from './type/Web3Response';
import { WalletLinkRelay } from './WalletLinkRelay';
import eip712 from '../../vendor-js/eth-eip712-util';
import { Signer, StateUpdateListener } from '../interface';
import { LOCAL_STORAGE_ADDRESSES_KEY } from './relay/constants';
import { RelayEventManager } from './relay/RelayEventManager';
import { EthereumTransactionParams } from './relay/type/EthereumTransactionParams';
import { JSONRPCRequest, JSONRPCResponse } from './relay/type/JSONRPC';
import { isErrorResponse, Web3Response } from './relay/type/Web3Response';
import { WalletLinkRelay } from './relay/WalletLinkRelay';
import { WALLETLINK_URL } from ':core/constants';
import { standardErrorCodes, standardErrors } from ':core/error';
import { RequestArguments } from ':core/provider/interface';
import { AppMetadata, RequestArguments } from ':core/provider/interface';
import { AddressString, IntNumber } from ':core/type';
import {
ensureAddressString,
Expand Down Expand Up @@ -53,28 +54,23 @@ interface WatchAssetParams {
};
}

export class WLRelayAdapter {
// original source: https://github.com/coinbase/coinbase-wallet-sdk/blob/v3.7.1/packages/wallet-sdk/src/provider/CoinbaseWalletProvider.ts
export class WalletLinkSigner implements Signer {
private _appName: string;
private _appLogoUrl: string | null;
private _relay: WalletLinkRelay | null = null;
private readonly _walletlinkUrl: string;
private readonly _storage: ScopedLocalStorage;
private readonly _relayEventManager: RelayEventManager;
private _jsonRpcUrlFromOpts: string;
private _addresses: AddressString[] = [];
private hasMadeFirstChainChangedEmission = false;
private updateListener?: StateUpdateListener;

constructor(options: {
appName: string;
appLogoUrl: string | null;
walletlinkUrl: string;
updateListener?: StateUpdateListener;
}) {
this._appName = options.appName;
this._appLogoUrl = options.appLogoUrl;
this._walletlinkUrl = options.walletlinkUrl;
this._storage = new ScopedLocalStorage('walletlink', this._walletlinkUrl);
constructor(options: { metadata: AppMetadata; updateListener?: StateUpdateListener }) {
const { appName, appLogoUrl } = options.metadata;
this._appName = appName;
this._appLogoUrl = appLogoUrl;
this._storage = new ScopedLocalStorage('walletlink', WALLETLINK_URL);
this.updateListener = options.updateListener;

this._relayEventManager = new RelayEventManager();
Expand Down Expand Up @@ -107,9 +103,15 @@ export class WLRelayAdapter {
this.initializeRelay();
}

getWalletLinkSession() {
getSession() {
const relay = this.initializeRelay();
return relay.getWalletLinkSession();
const { id, secret } = relay.getWalletLinkSession();
return { id, secret };
}

async handshake(): Promise<AddressString[]> {
const ethAddresses = await this.request<AddressString[]>({ method: 'eth_requestAccounts' });
return ethAddresses;
}

get selectedAddress(): AddressString | undefined {
Expand Down Expand Up @@ -229,9 +231,10 @@ export class WLRelayAdapter {
}
}

public async close() {
const relay = this.initializeRelay();
relay.resetAndReload();
public async disconnect() {
if (this._relay) {
this._relay.resetAndReload();
}
this._storage.clear();
}

Expand Down Expand Up @@ -760,7 +763,7 @@ export class WLRelayAdapter {
private initializeRelay(): WalletLinkRelay {
if (!this._relay) {
const relay = new WalletLinkRelay({
linkAPIUrl: this._walletlinkUrl,
linkAPIUrl: WALLETLINK_URL,
storage: this._storage,
});
relay.setAppInfo(this._appName, this._appLogoUrl);
Expand Down

0 comments on commit 3866ee8

Please sign in to comment.