@coinbase/wallet-sdk
Advanced tools
Comparing version 4.0.0-rc.2 to 4.0.0-rc.3
@@ -10,3 +10,2 @@ import EventEmitter from 'eventemitter3'; | ||
protected signHandler: SignHandler; | ||
private filterHandler; | ||
constructor(params: Readonly<ConstructorOptions>); | ||
@@ -20,10 +19,2 @@ get connected(): boolean; | ||
state: (request: RequestArguments) => number | AddressString | AddressString[]; | ||
filter: (request: RequestArguments) => Promise<{ | ||
error: { | ||
code: number; | ||
message: string; | ||
}; | ||
} | { | ||
result: unknown; | ||
}>; | ||
deprecated: ({ method }: RequestArguments) => never; | ||
@@ -30,0 +21,0 @@ unsupported: ({ method }: RequestArguments) => never; |
@@ -14,3 +14,2 @@ "use strict"; | ||
const method_1 = require("./core/provider/method"); | ||
const FilterPolyfill_1 = require("./util/FilterPolyfill"); | ||
class CoinbaseWalletProvider extends eventemitter3_1.default { | ||
@@ -67,3 +66,2 @@ constructor(params) { | ||
}, | ||
filter: (request) => this.filterHandler.request(request), | ||
deprecated: ({ method }) => { | ||
@@ -99,3 +97,2 @@ throw error_1.standardErrors.rpc.methodNotSupported(`Method ${method} is deprecated.`); | ||
this.signHandler = new SignHandler_1.SignHandler(Object.assign(Object.assign({}, params), { listener: this.updateListener })); | ||
this.filterHandler = new FilterPolyfill_1.FilterPolyfill(this.handlers.fetch); | ||
} | ||
@@ -102,0 +99,0 @@ get connected() { |
@@ -1,16 +0,2 @@ | ||
import { CrossDomainCommunicator } from '../communicator/CrossDomainCommunicator'; | ||
import { ConfigUpdateMessage, Message } from '../message'; | ||
export declare class PopUpCommunicator extends CrossDomainCommunicator { | ||
private resolveConnection?; | ||
private onConfigUpdateMessage; | ||
constructor(params: { | ||
url: string; | ||
onConfigUpdateMessage: (_: ConfigUpdateMessage) => void; | ||
}); | ||
protected onConnect(): Promise<void>; | ||
protected onEvent(event: MessageEvent<Message>): void; | ||
protected onDisconnect(): void; | ||
private handleIncomingConfigUpdate; | ||
private openFixedSizePopUpWindow; | ||
private closeChildWindow; | ||
} | ||
export declare function openPopup(url: URL): Window; | ||
export declare function closePopup(popup: Window | null): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PopUpCommunicator = void 0; | ||
const version_1 = require("../../version"); | ||
const CrossDomainCommunicator_1 = require("../communicator/CrossDomainCommunicator"); | ||
exports.closePopup = exports.openPopup = void 0; | ||
const error_1 = require("../error"); | ||
const message_1 = require("../message"); | ||
const POPUP_WIDTH = 420; | ||
const POPUP_HEIGHT = 540; | ||
class PopUpCommunicator extends CrossDomainCommunicator_1.CrossDomainCommunicator { | ||
constructor(params) { | ||
super(); | ||
this.url = new URL(params.url); | ||
this.onConfigUpdateMessage = params.onConfigUpdateMessage; | ||
// Window Management | ||
function openPopup(url) { | ||
const left = (window.innerWidth - POPUP_WIDTH) / 2 + window.screenX; | ||
const top = (window.innerHeight - POPUP_HEIGHT) / 2 + window.screenY; | ||
const popup = window.open(url, 'Smart Wallet', `width=${POPUP_WIDTH}, height=${POPUP_HEIGHT}, left=${left}, top=${top}`); | ||
popup === null || popup === void 0 ? void 0 : popup.focus(); | ||
if (!popup) { | ||
throw error_1.standardErrors.rpc.internal('Pop up window failed to open'); | ||
} | ||
onConnect() { | ||
return new Promise((resolve) => { | ||
this.resolveConnection = resolve; | ||
this.openFixedSizePopUpWindow(); | ||
}); | ||
return popup; | ||
} | ||
exports.openPopup = openPopup; | ||
function closePopup(popup) { | ||
if (popup && !popup.closed) { | ||
popup.close(); | ||
} | ||
onEvent(event) { | ||
const message = event.data; | ||
if ((0, message_1.isConfigUpdateMessage)(message)) { | ||
this.handleIncomingConfigUpdate(message); | ||
} | ||
} | ||
onDisconnect() { | ||
this.closeChildWindow(); | ||
} | ||
handleIncomingConfigUpdate(message) { | ||
var _a; | ||
switch (message.event) { | ||
case message_1.ConfigEvent.PopupLoaded: | ||
// Handshake Step 2: After receiving PopupHello from popup, Dapp sends DappHello | ||
// to FE to help FE confirm the origin of the Dapp, as well as SDK version. | ||
this.postMessage({ | ||
requestId: message.id, | ||
data: { version: version_1.LIB_VERSION }, | ||
}); | ||
(_a = this.resolveConnection) === null || _a === void 0 ? void 0 : _a.call(this); | ||
this.resolveConnection = undefined; | ||
break; | ||
case message_1.ConfigEvent.PopupUnload: | ||
this.disconnect(); | ||
break; | ||
default: // handle non-popup config update messages | ||
this.onConfigUpdateMessage(message); | ||
} | ||
} | ||
// Window Management | ||
openFixedSizePopUpWindow() { | ||
var _a; | ||
const left = (window.innerWidth - POPUP_WIDTH) / 2 + window.screenX; | ||
const top = (window.innerHeight - POPUP_HEIGHT) / 2 + window.screenY; | ||
if (!this.url) { | ||
throw error_1.standardErrors.rpc.internal('No url provided in PopUpCommunicator'); | ||
} | ||
const popupUrl = new URL(this.url); | ||
this.peerWindow = window.open(popupUrl, 'Smart Wallet', `width=${POPUP_WIDTH}, height=${POPUP_HEIGHT}, left=${left}, top=${top}`); | ||
(_a = this.peerWindow) === null || _a === void 0 ? void 0 : _a.focus(); | ||
if (!this.peerWindow) { | ||
throw error_1.standardErrors.rpc.internal('Pop up window failed to open'); | ||
} | ||
} | ||
closeChildWindow() { | ||
if (this.peerWindow && !this.peerWindow.closed) { | ||
this.peerWindow.close(); | ||
} | ||
this.peerWindow = null; | ||
} | ||
} | ||
exports.PopUpCommunicator = PopUpCommunicator; | ||
exports.closePopup = closePopup; |
@@ -1,18 +0,6 @@ | ||
import { Message, MessageID } from './Message'; | ||
export interface ConfigUpdateMessage extends Message { | ||
import { Message } from './Message'; | ||
export interface ConfigMessage extends Message { | ||
event: ConfigEvent; | ||
data?: unknown; | ||
} | ||
export declare function isConfigUpdateMessage(message: Message): message is ConfigUpdateMessage; | ||
export interface ConfigResponseMessage extends Message { | ||
requestId: MessageID; | ||
data?: unknown; | ||
} | ||
export declare enum ConfigEvent { | ||
PopupLoaded = "PopupLoaded", | ||
PopupUnload = "PopupUnload", | ||
WalletLinkSessionRequest = "WalletLinkSessionRequest", | ||
SelectSignerType = "selectSignerType", | ||
WalletLinkUpdate = "WalletLinkUpdate" | ||
} | ||
export type ConfigEvent = 'PopupLoaded' | 'PopupUnload' | 'selectSignerType' | 'WalletLinkSessionRequest' | 'WalletLinkUpdate'; | ||
export type SignerType = 'scw' | 'walletlink' | 'extension'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ConfigEvent = exports.isConfigUpdateMessage = void 0; | ||
function isConfigUpdateMessage(message) { | ||
return message.event !== undefined; | ||
} | ||
exports.isConfigUpdateMessage = isConfigUpdateMessage; | ||
var ConfigEvent; | ||
(function (ConfigEvent) { | ||
ConfigEvent["PopupLoaded"] = "PopupLoaded"; | ||
ConfigEvent["PopupUnload"] = "PopupUnload"; | ||
ConfigEvent["WalletLinkSessionRequest"] = "WalletLinkSessionRequest"; | ||
ConfigEvent["SelectSignerType"] = "selectSignerType"; | ||
ConfigEvent["WalletLinkUpdate"] = "WalletLinkUpdate"; | ||
})(ConfigEvent || (exports.ConfigEvent = ConfigEvent = {})); |
@@ -5,6 +5,5 @@ /// <reference types="node" /> | ||
export interface Message { | ||
id: MessageID; | ||
id?: MessageID; | ||
requestId?: MessageID; | ||
data?: unknown; | ||
} | ||
export type MessageWithOptionalId<T extends Message> = Omit<T, 'id'> & Partial<Pick<T, 'id'>>; | ||
export declare function createMessage<T extends Message>(params: MessageWithOptionalId<T>): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createMessage = void 0; | ||
function createMessage(params) { | ||
var _a; | ||
return Object.assign(Object.assign({}, params), { id: (_a = params.id) !== null && _a !== void 0 ? _a : crypto.randomUUID() }); | ||
} | ||
exports.createMessage = createMessage; |
@@ -5,2 +5,3 @@ import { Message, MessageID } from './Message'; | ||
interface RPCMessage extends Message { | ||
id: MessageID; | ||
sender: string; | ||
@@ -7,0 +8,0 @@ content: unknown; |
@@ -5,3 +5,2 @@ declare const mapping: { | ||
readonly state: readonly ["eth_chainId", "eth_accounts", "eth_coinbase", "net_version"]; | ||
readonly filter: readonly ["eth_newFilter", "eth_newBlockFilter", "eth_newPendingTransactionFilter", "eth_getFilterChanges", "eth_getFilterLogs", "eth_uninstallFilter"]; | ||
readonly deprecated: readonly ["eth_sign", "eth_signTypedData_v2"]; | ||
@@ -8,0 +7,0 @@ readonly unsupported: readonly ["eth_subscribe", "eth_unsubscribe"]; |
@@ -22,10 +22,8 @@ "use strict"; | ||
], | ||
state: ['eth_chainId', 'eth_accounts', 'eth_coinbase', 'net_version'], | ||
filter: [ | ||
'eth_newFilter', | ||
'eth_newBlockFilter', | ||
'eth_newPendingTransactionFilter', | ||
'eth_getFilterChanges', | ||
'eth_getFilterLogs', | ||
'eth_uninstallFilter', | ||
state: [ | ||
// internal state | ||
'eth_chainId', | ||
'eth_accounts', | ||
'eth_coinbase', | ||
'net_version', | ||
], | ||
@@ -32,0 +30,0 @@ deprecated: ['eth_sign', 'eth_signTypedData_v2'], |
import { Signer, StateUpdateListener } from '../interface'; | ||
import { PopUpCommunicator } from '../../core/communicator/PopUpCommunicator'; | ||
import { KeysPopupCommunicator } from '../../core/communicator/KeysPopupCommunicator'; | ||
import { AppMetadata, RequestArguments } from '../../core/provider/interface'; | ||
@@ -7,3 +7,3 @@ import { AddressString } from '../../core/type'; | ||
private readonly metadata; | ||
private readonly popupCommunicator; | ||
private readonly postMessageToPopup; | ||
private readonly keyManager; | ||
@@ -13,3 +13,3 @@ private readonly stateManager; | ||
metadata: AppMetadata; | ||
popupCommunicator: PopUpCommunicator; | ||
postMessageToPopup: KeysPopupCommunicator['postMessage']; | ||
updateListener: StateUpdateListener; | ||
@@ -16,0 +16,0 @@ }); |
@@ -7,3 +7,2 @@ "use strict"; | ||
const error_1 = require("../../core/error"); | ||
const message_1 = require("../../core/message"); | ||
const util_1 = require("../../core/type/util"); | ||
@@ -14,3 +13,3 @@ const cipher_1 = require("../../util/cipher"); | ||
this.metadata = params.metadata; | ||
this.popupCommunicator = params.popupCommunicator; | ||
this.postMessageToPopup = params.postMessageToPopup; | ||
this.keyManager = new SCWKeyManager_1.SCWKeyManager(); | ||
@@ -27,3 +26,2 @@ this.stateManager = new SCWStateManager_1.SCWStateManager({ | ||
async handshake() { | ||
await this.popupCommunicator.connect(); | ||
const handshakeMessage = await this.createRequestMessage({ | ||
@@ -35,3 +33,3 @@ handshake: { | ||
}); | ||
const response = (await this.popupCommunicator.postMessageForResponse(handshakeMessage)); | ||
const response = await this.postMessageToPopup(handshakeMessage); | ||
// store peer's public key | ||
@@ -56,3 +54,2 @@ if ('failure' in response.content) | ||
} | ||
await this.popupCommunicator.connect(); | ||
const response = await this.sendEncryptedRequest(request); | ||
@@ -106,12 +103,12 @@ const decrypted = await this.decryptResponseMessage(response); | ||
const message = await this.createRequestMessage({ encrypted }); | ||
const response = (await this.popupCommunicator.postMessageForResponse(message)); | ||
return response; | ||
return this.postMessageToPopup(message); | ||
} | ||
async createRequestMessage(content) { | ||
const publicKey = await (0, cipher_1.exportKeyToHexString)('public', await this.keyManager.getOwnPublicKey()); | ||
return (0, message_1.createMessage)({ | ||
return { | ||
id: crypto.randomUUID(), | ||
sender: publicKey, | ||
content, | ||
timestamp: new Date(), | ||
}); | ||
}; | ||
} | ||
@@ -118,0 +115,0 @@ async decryptResponseMessage(message) { |
import { StateUpdateListener } from './interface'; | ||
import { KeysPopupCommunicator } from '../core/communicator/KeysPopupCommunicator'; | ||
import { ConstructorOptions, RequestArguments } from '../core/provider/interface'; | ||
@@ -6,3 +7,3 @@ type SignerConfiguratorOptions = ConstructorOptions & { | ||
}; | ||
export declare class SignHandler { | ||
export declare class SignHandler extends KeysPopupCommunicator { | ||
private signer; | ||
@@ -12,3 +13,2 @@ private readonly metadata; | ||
private readonly listener; | ||
private readonly popupCommunicator; | ||
private readonly storage; | ||
@@ -20,8 +20,7 @@ constructor(params: Readonly<SignerConfiguratorOptions>); | ||
private loadSigner; | ||
private selectSigner; | ||
private requestSignerSelection; | ||
private initSigner; | ||
private walletlinkSigner?; | ||
private handleConfigUpdateMessage; | ||
private listenForWalletLinkSessionRequest; | ||
} | ||
export {}; |
@@ -17,25 +17,22 @@ "use strict"; | ||
const WLSigner_1 = require("./walletlink/WLSigner"); | ||
const PopUpCommunicator_1 = require("../core/communicator/PopUpCommunicator"); | ||
const constants_1 = require("../core/constants"); | ||
const message_1 = require("../core/message"); | ||
const KeysPopupCommunicator_1 = require("../core/communicator/KeysPopupCommunicator"); | ||
const ScopedLocalStorage_1 = require("../util/ScopedLocalStorage"); | ||
const SIGNER_TYPE_KEY = 'SignerType'; | ||
class SignHandler { | ||
class SignHandler extends KeysPopupCommunicator_1.KeysPopupCommunicator { | ||
constructor(params) { | ||
const _a = params.preference, { keysUrl } = _a, preferenceWithoutKeysUrl = __rest(_a, ["keysUrl"]); | ||
super(keysUrl); | ||
this.storage = new ScopedLocalStorage_1.ScopedLocalStorage('CBWSDK', 'SignerConfigurator'); | ||
this.preference = preferenceWithoutKeysUrl; | ||
this.metadata = params.metadata; | ||
this.listener = params.listener; | ||
const _a = params.preference, { keysUrl } = _a, preferenceWithoutKeysUrl = __rest(_a, ["keysUrl"]); | ||
this.preference = preferenceWithoutKeysUrl; | ||
this.popupCommunicator = new PopUpCommunicator_1.PopUpCommunicator({ | ||
url: keysUrl !== null && keysUrl !== void 0 ? keysUrl : constants_1.CB_KEYS_URL, | ||
onConfigUpdateMessage: this.handleConfigUpdateMessage.bind(this), | ||
}); | ||
this.signer = this.loadSigner(); | ||
} | ||
async handshake() { | ||
if (!this.signer) { | ||
this.signer = await this.selectSigner(); | ||
} | ||
return this.signer.handshake(); | ||
const signerType = await this.requestSignerSelection(); | ||
const signer = this.initSigner(signerType); | ||
const accounts = await signer.handshake(); | ||
this.storage.setItem(SIGNER_TYPE_KEY, signerType); | ||
this.signer = signer; | ||
return accounts; | ||
} | ||
@@ -58,19 +55,16 @@ async request(request) { | ||
} | ||
async selectSigner() { | ||
const signerType = await this.requestSignerSelection(); | ||
this.storage.setItem(SIGNER_TYPE_KEY, signerType); | ||
if (signerType === 'walletlink' && this.walletlinkSigner) | ||
return this.walletlinkSigner; | ||
return this.initSigner(signerType); | ||
} | ||
async requestSignerSelection() { | ||
await this.popupCommunicator.connect(); | ||
const message = (0, message_1.createMessage)({ | ||
event: message_1.ConfigEvent.SelectSignerType, | ||
this.listenForWalletLinkSessionRequest(); | ||
const request = { | ||
id: crypto.randomUUID(), | ||
event: 'selectSignerType', | ||
data: this.preference, | ||
}); | ||
const response = await this.popupCommunicator.postMessageForResponse(message); | ||
return response.data; | ||
}; | ||
const { data } = await this.postMessage(request); | ||
return data; | ||
} | ||
initSigner(signerType) { | ||
if (signerType === 'walletlink' && this.walletlinkSigner) { | ||
return this.walletlinkSigner; | ||
} | ||
const SignerClasses = { | ||
@@ -83,17 +77,15 @@ scw: SCWSigner_1.SCWSigner, | ||
metadata: this.metadata, | ||
popupCommunicator: this.popupCommunicator, | ||
postMessageToPopup: this.postMessage.bind(this), | ||
updateListener: this.listener, | ||
}); | ||
} | ||
async handleConfigUpdateMessage(message) { | ||
switch (message.event) { | ||
case message_1.ConfigEvent.WalletLinkSessionRequest: | ||
if (!this.walletlinkSigner) { | ||
this.walletlinkSigner = this.initSigner('walletlink'); | ||
} | ||
await this.walletlinkSigner.handleWalletLinkSessionRequest(); | ||
break; | ||
} | ||
listenForWalletLinkSessionRequest() { | ||
this.onMessage(({ event }) => event === 'WalletLinkSessionRequest').then(async () => { | ||
if (!this.walletlinkSigner) { | ||
this.walletlinkSigner = this.initSigner('walletlink'); | ||
} | ||
await this.walletlinkSigner.handleWalletLinkSessionRequest(); | ||
}); | ||
} | ||
} | ||
exports.SignHandler = SignHandler; |
@@ -14,3 +14,3 @@ import { StateUpdateListener } from '../../interface'; | ||
private hasMadeFirstChainChangedEmission; | ||
private updateListener; | ||
private updateListener?; | ||
constructor(options: { | ||
@@ -20,3 +20,3 @@ appName: string; | ||
walletlinkUrl: string; | ||
updateListener: StateUpdateListener; | ||
updateListener?: StateUpdateListener; | ||
}); | ||
@@ -23,0 +23,0 @@ getWalletLinkSession(): import("./type/WalletLinkSession").WalletLinkSession; |
@@ -21,2 +21,3 @@ "use strict"; | ||
constructor(options) { | ||
var _a, _b; | ||
this._relay = null; | ||
@@ -37,3 +38,3 @@ this._addresses = []; | ||
this._addresses = addresses.map((address) => (0, util_1.ensureAddressString)(address)); | ||
this.updateListener.onAccountsUpdate({ | ||
(_a = this.updateListener) === null || _a === void 0 ? void 0 : _a.onAccountsUpdate({ | ||
accounts: this._addresses, | ||
@@ -46,3 +47,3 @@ source: 'storage', | ||
if (cachedChainId) { | ||
this.updateListener.onChainUpdate({ | ||
(_b = this.updateListener) === null || _b === void 0 ? void 0 : _b.onChainUpdate({ | ||
chain: { | ||
@@ -72,2 +73,3 @@ id: this.getChainId(), | ||
updateProviderInfo(jsonRpcUrl, chainId) { | ||
var _a; | ||
this.jsonRpcUrl = jsonRpcUrl; | ||
@@ -79,3 +81,3 @@ // emit chainChanged event if necessary | ||
if (chainChanged || !this.hasMadeFirstChainChangedEmission) { | ||
this.updateListener.onChainUpdate({ | ||
(_a = this.updateListener) === null || _a === void 0 ? void 0 : _a.onChainUpdate({ | ||
chain: { id: chainId, rpcUrl: jsonRpcUrl }, | ||
@@ -182,2 +184,3 @@ source: 'wallet', | ||
_setAddresses(addresses, _) { | ||
var _a; | ||
if (!Array.isArray(addresses)) { | ||
@@ -191,3 +194,3 @@ throw new Error('addresses is not an array'); | ||
this._addresses = newAddresses; | ||
this.updateListener.onAccountsUpdate({ | ||
(_a = this.updateListener) === null || _a === void 0 ? void 0 : _a.onAccountsUpdate({ | ||
accounts: newAddresses, | ||
@@ -194,0 +197,0 @@ source: 'wallet', |
import { Signer, StateUpdateListener } from '../interface'; | ||
import { PopUpCommunicator } from '../../core/communicator/PopUpCommunicator'; | ||
import { KeysPopupCommunicator } from '../../core/communicator/KeysPopupCommunicator'; | ||
import { AppMetadata, RequestArguments } from '../../core/provider/interface'; | ||
import { AddressString } from '../../core/type'; | ||
export declare class WLSigner implements Signer { | ||
private readonly popupCommunicator; | ||
private readonly postMessageToPopup; | ||
private readonly adapter; | ||
constructor(params: { | ||
metadata: AppMetadata; | ||
popupCommunicator: PopUpCommunicator; | ||
updateListener: StateUpdateListener; | ||
postMessageToPopup: KeysPopupCommunicator['postMessage']; | ||
updateListener?: StateUpdateListener; | ||
}); | ||
@@ -13,0 +13,0 @@ handshake(): Promise<AddressString[]>; |
@@ -6,7 +6,6 @@ "use strict"; | ||
const constants_1 = require("../../core/constants"); | ||
const message_1 = require("../../core/message"); | ||
class WLSigner { | ||
constructor(params) { | ||
const { appName, appLogoUrl } = params.metadata; | ||
this.popupCommunicator = params.popupCommunicator; | ||
this.postMessageToPopup = params.postMessageToPopup; | ||
this.adapter = new WLRelayAdapter_1.WLRelayAdapter({ | ||
@@ -40,6 +39,7 @@ appName, | ||
postWalletLinkUpdate(data) { | ||
this.popupCommunicator.postMessage({ | ||
event: message_1.ConfigEvent.WalletLinkUpdate, | ||
const update = { | ||
event: 'WalletLinkUpdate', | ||
data, | ||
}); | ||
}; | ||
this.postMessageToPopup(update); | ||
} | ||
@@ -46,0 +46,0 @@ async disconnect() { |
@@ -1,1 +0,1 @@ | ||
export declare const LIB_VERSION = "4.0.0-rc.2"; | ||
export declare const LIB_VERSION = "4.0.0-rc.3"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.LIB_VERSION = void 0; | ||
exports.LIB_VERSION = '4.0.0-rc.2'; | ||
exports.LIB_VERSION = '4.0.0-rc.3'; |
{ | ||
"name": "@coinbase/wallet-sdk", | ||
"version": "4.0.0-rc.2", | ||
"version": "4.0.0-rc.3", | ||
"description": "Coinbase Wallet JavaScript SDK", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
247518
130
5819