@portkey/providers
Advanced tools
Comparing version 0.0.1-alpha.7 to 0.0.1-alpha.8
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
import { EventId, IProvider, DappEvents, ConsoleLike, BaseProviderOptions, IDappInteractionStream, IResponseInfo, RequestOption } from '@portkey/provider-types'; | ||
type Chain = string; | ||
type IAccounts = { | ||
[x: Chain]: string[]; | ||
}; | ||
import { EventId, IProvider, DappEvents, ConsoleLike, BaseProviderOptions, IResponseInfo, ChainIdRequestResponse, GetWalletStateRequestResponse, Accounts, SendTransactionParams, RequestAccountsRequestResponse } from '@portkey/provider-types'; | ||
import { ChainsInfoRequestResponse } from '@portkey/provider-types'; | ||
import { TransactionRequestResponse } from '@portkey/provider-types'; | ||
export interface BaseProviderState { | ||
accounts: null | IAccounts; | ||
accounts: null | Accounts; | ||
isConnected: boolean; | ||
@@ -20,11 +19,28 @@ isUnlocked: boolean; | ||
constructor({ connectionStream, logger, maxEventListeners }: BaseProviderOptions); | ||
private _onData; | ||
on(event: DappEvents, listener: (...args: any[]) => void): this; | ||
once(event: DappEvents | EventId, listener: (...args: any[]) => void): this; | ||
addListener(eventName: DappEvents, listener: (...args: any[]) => void): this; | ||
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this; | ||
protected _onData: (buffer: Buffer) => void; | ||
on(event: string, listener: (...args: any[]) => void): this; | ||
once(event: string, listener: (...args: any[]) => void): this; | ||
addListener(event: string, listener: (...args: any[]) => void): this; | ||
removeListener(event: string, listener: (...args: any[]) => void): this; | ||
emit(event: DappEvents | EventId, response: IResponseInfo | any): boolean; | ||
request: <T = any>(params: RequestOption) => Promise<T>; | ||
request<T = ChainIdRequestResponse>(params: { | ||
method: 'chainId'; | ||
}): Promise<T>; | ||
request<T = ChainIdRequestResponse>(params: { | ||
method: 'chainIds'; | ||
}): Promise<T>; | ||
request<T = ChainsInfoRequestResponse>(params: { | ||
method: 'chainsInfo'; | ||
}): Promise<T>; | ||
request<T = RequestAccountsRequestResponse>(params: { | ||
method: 'requestAccounts'; | ||
}): Promise<T>; | ||
request<T = GetWalletStateRequestResponse>(params: { | ||
method: 'wallet_getWalletState'; | ||
}): Promise<T>; | ||
request<T = TransactionRequestResponse>(params: { | ||
method: 'sendTransaction'; | ||
payload: SendTransactionParams; | ||
}): Promise<T>; | ||
protected methodCheck: (method: string) => method is string; | ||
setupStream: (_companionStream: IDappInteractionStream) => void; | ||
onConnectionDisconnect: (error: Error) => void; | ||
@@ -35,6 +51,6 @@ protected getEventName: (seed?: number) => string; | ||
protected handleDisconnect(response: IResponseInfo): void; | ||
protected handleAccountsChanged(response: IResponseInfo<IAccounts>): void; | ||
protected handleAccountsChanged(response: IResponseInfo<Accounts>): void; | ||
protected handleNetworkChanged(response: IResponseInfo): void; | ||
protected handleMessage(response: IResponseInfo): void; | ||
} | ||
export {}; | ||
//# sourceMappingURL=BaseProvider.d.ts.map |
@@ -18,29 +18,40 @@ "use strict"; | ||
super(); | ||
this.request = (params) => __awaiter(this, void 0, void 0, function* () { | ||
this._log.log(params, 'request,=======params'); | ||
const eventName = this.getEventName(); | ||
const { method, payload } = params || {}; | ||
this._companionStream.write(JSON.stringify({ | ||
method, | ||
payload, | ||
eventName, | ||
})); | ||
return new Promise((resolve, reject) => { | ||
this.once(eventName, (response) => { | ||
const { code, data } = response || {}; | ||
if (code == provider_types_1.ResponseCode.SUCCESS) { | ||
resolve(data); | ||
this._onData = (buffer) => { | ||
try { | ||
const { eventName, info } = JSON.parse(buffer === null || buffer === void 0 ? void 0 : buffer.toString()); | ||
if ((0, utils_1.isNotificationEvents)(eventName)) { | ||
switch (eventName) { | ||
case provider_types_1.NotificationEvents.CONNECTED: | ||
this.handleConnect(info); | ||
return; | ||
case provider_types_1.NotificationEvents.DISCONNECTED: | ||
this.handleDisconnect(info); | ||
return; | ||
case provider_types_1.NotificationEvents.ACCOUNTS_CHANGED: | ||
this.handleAccountsChanged(info); | ||
return; | ||
case provider_types_1.NotificationEvents.NETWORK_CHANGED: | ||
this.handleNetworkChanged(info); | ||
return; | ||
case provider_types_1.NotificationEvents.MESSAGE: | ||
this.handleMessage(info); | ||
return; | ||
default: | ||
if (eventName && (info === null || info === void 0 ? void 0 : info.data)) | ||
this.emit(eventName, info.data); | ||
break; | ||
} | ||
else { | ||
reject(new provider_types_1.ProviderError(`${response.msg}`, code)); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
else { | ||
if (eventName && info) | ||
this.emit(eventName, info); | ||
} | ||
} | ||
catch (error) { | ||
this._log.log(error, '====error'); | ||
} | ||
}; | ||
this.methodCheck = (method) => { | ||
return (0, utils_1.isRPCMethodsBase)(method) || (0, utils_1.isRPCMethodsUnimplemented)(method); | ||
}; | ||
this.setupStream = (_companionStream) => { | ||
this._companionStream = _companionStream; | ||
}; | ||
this.onConnectionDisconnect = (error) => { | ||
@@ -66,38 +77,6 @@ console.warn('connection disconnected, please re-open this webpage!', error); | ||
this._log = logger; | ||
this._companionStream.on('data', this._onData.bind(this)); | ||
this._companionStream.on('data', this._onData); | ||
this.state = BaseProvider._defaultState; | ||
this.request = this.request.bind(this); | ||
} | ||
_onData(buffer) { | ||
console.log(buffer, JSON.parse(buffer.toString()), '=====buffer'); | ||
try { | ||
const { eventName, info } = JSON.parse(buffer.toString()); | ||
if ((0, utils_1.isNotificationEvents)(eventName)) { | ||
switch (eventName) { | ||
case provider_types_1.NotificationEvents.CONNECTED: | ||
this.handleConnect(info); | ||
return; | ||
case provider_types_1.NotificationEvents.DISCONNECTED: | ||
this.handleDisconnect(info); | ||
return; | ||
case provider_types_1.NotificationEvents.ACCOUNTS_CHANGED: | ||
this.handleAccountsChanged(info); | ||
return; | ||
case provider_types_1.NotificationEvents.NETWORK_CHANGED: | ||
this.handleNetworkChanged(info); | ||
return; | ||
default: | ||
if (eventName && (info === null || info === void 0 ? void 0 : info.data)) | ||
this.emit(eventName, info.data); | ||
break; | ||
} | ||
} | ||
else { | ||
if (eventName && info) | ||
this.emit(eventName, info); | ||
} | ||
} | ||
catch (error) { | ||
this._log.log(error, '====error'); | ||
} | ||
} | ||
on(event, listener) { | ||
@@ -111,7 +90,7 @@ super.on(event, listener); | ||
} | ||
addListener(eventName, listener) { | ||
return this.on(eventName, listener); | ||
addListener(event, listener) { | ||
return this.on(event, listener); | ||
} | ||
removeListener(eventName, listener) { | ||
super.removeListener(eventName, listener); | ||
removeListener(event, listener) { | ||
super.removeListener(event, listener); | ||
return this; | ||
@@ -122,2 +101,32 @@ } | ||
} | ||
request(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this._log.log(params, 'request,=======params'); | ||
if (!params || typeof params !== 'object' || Array.isArray(params)) | ||
throw new provider_types_1.ProviderError('Expected a single, non-array, object argument.', provider_types_1.ResponseCode.ERROR_IN_PARAMS); | ||
const eventName = this.getEventName(); | ||
const { method, payload } = params || {}; | ||
if (!this.methodCheck(method)) { | ||
throw new provider_types_1.ProviderError(provider_types_1.ResponseMessagePreset['UNKNOWN_METHOD'], provider_types_1.ResponseCode.UNKNOWN_METHOD); | ||
} | ||
if (payload !== undefined && typeof payload !== 'object' && payload !== null) | ||
throw new provider_types_1.ProviderError(`'params.payload' must be an object if provided.`, provider_types_1.ResponseCode.UNKNOWN_METHOD); | ||
this._companionStream.write(JSON.stringify({ | ||
method, | ||
payload, | ||
eventName, | ||
})); | ||
return new Promise((resolve, reject) => { | ||
this.once(eventName, (response) => { | ||
const { code, data } = response || {}; | ||
if (code == provider_types_1.ResponseCode.SUCCESS) { | ||
resolve(data); | ||
} | ||
else { | ||
reject(new provider_types_1.ProviderError(`${response.msg}`, code)); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
handleConnect(response) { | ||
@@ -150,2 +159,6 @@ if (!this.state.isConnected) { | ||
} | ||
handleMessage(response) { | ||
var _a; | ||
this.emit(provider_types_1.NotificationEvents.MESSAGE, (_a = response === null || response === void 0 ? void 0 : response.data) !== null && _a !== void 0 ? _a : response === null || response === void 0 ? void 0 : response.msg); | ||
} | ||
} | ||
@@ -152,0 +165,0 @@ exports.default = BaseProvider; |
@@ -6,3 +6,4 @@ /// <reference types="node" /> | ||
constructor(); | ||
createSubStream: (_name: String) => never; | ||
private _subStreamMap; | ||
createSubStream: (name: string) => SubStream; | ||
_read: (_size?: number | undefined) => void; | ||
@@ -14,7 +15,8 @@ createMessageEvent: (msg: string) => void; | ||
export declare class SubStream extends Duplex { | ||
private parentStream; | ||
name: string; | ||
private parentStream; | ||
constructor(parentStream: Duplex, name: string); | ||
_read(_size: number): void; | ||
_read: (_size: number) => void; | ||
_write(chunk: any, _encoding: BufferEncoding, callback: (error?: Error | null | undefined) => void): void; | ||
private connect; | ||
} | ||
@@ -21,0 +23,0 @@ export interface StreamData { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,11 +9,23 @@ exports.SubStream = exports.DappInteractionStream = void 0; | ||
const readable_stream_1 = require("readable-stream"); | ||
const object_multiplex_1 = __importDefault(require("@metamask/object-multiplex")); | ||
const pump_1 = __importDefault(require("pump")); | ||
class DappInteractionStream extends readable_stream_1.Duplex { | ||
constructor() { | ||
super(); | ||
this.createSubStream = (_name) => { | ||
throw new Error('not implemented yet'); | ||
this._subStreamMap = new Map(); | ||
this.createSubStream = (name) => { | ||
var _a; | ||
if (this._subStreamMap.has(name)) { | ||
return (_a = this._subStreamMap.get(name)) !== null && _a !== void 0 ? _a : new SubStream(this, name); | ||
} | ||
const subStream = new SubStream(this, name); | ||
this._subStreamMap.set(name, subStream); | ||
return subStream; | ||
}; | ||
this._read = (_size) => { }; | ||
this.createMessageEvent = (msg) => { | ||
this.push({ eventName: provider_types_1.NotificationEvents.MESSAGE, info: { code: provider_types_1.ResponseCode.INTERNAL_ERROR, msg } }); | ||
this.write(JSON.stringify({ | ||
eventName: provider_types_1.NotificationEvents.MESSAGE, | ||
info: { code: provider_types_1.ResponseCode.SUCCESS, msg, data: msg }, | ||
})); | ||
}; | ||
@@ -26,10 +41,18 @@ } | ||
super(); | ||
this._read = (_size) => { }; | ||
this.connect = () => { | ||
const duplex = new object_multiplex_1.default(); | ||
(0, pump_1.default)(this.parentStream, duplex, this.parentStream, e => { | ||
console.error(`SubStream ${this.name} disconnected:`, e); | ||
}); | ||
(0, pump_1.default)(duplex, this, duplex); | ||
}; | ||
this.parentStream = parentStream; | ||
this.name = name; | ||
this.parentStream = parentStream; | ||
this.connect(); | ||
} | ||
_read(_size) { } | ||
_write(chunk, _encoding, callback) { | ||
var _a; | ||
(_a = this.parentStream) === null || _a === void 0 ? void 0 : _a.push(Object.assign({}, chunk, { name: this.name })); | ||
callback(); | ||
(_a = this.parentStream) === null || _a === void 0 ? void 0 : _a.write(chunk.toString()); | ||
return callback(); | ||
} | ||
@@ -36,0 +59,0 @@ } |
@@ -25,6 +25,4 @@ "use strict"; | ||
_onMessage(event) { | ||
console.log(event, '======event'); | ||
console.log(this._postWindow, '======this._postWindow'); | ||
console.log(this._origin, '======this._origin'); | ||
console.log(this._name, '======this._name'); | ||
console.log(event.data, '======this._name'); | ||
try { | ||
@@ -31,0 +29,0 @@ const msg = event.data; |
@@ -26,3 +26,3 @@ "use strict"; | ||
if (!chainInfos) | ||
throw new provider_types_1.ProviderError('This chainId is not supported', 40002); | ||
throw new provider_types_1.ProviderError('This chainId is not supported', provider_types_1.ResponseCode.ERROR_IN_PARAMS); | ||
return new chain_1.Chain({ | ||
@@ -29,0 +29,0 @@ request: this.request, |
{ | ||
"name": "@portkey/providers", | ||
"version": "0.0.1-alpha.7", | ||
"version": "0.0.1-alpha.8", | ||
"description": "", | ||
@@ -34,9 +34,14 @@ "author": "", | ||
"dependencies": { | ||
"@portkey/chain": "^0.0.1-alpha.7", | ||
"@portkey/provider-types": "^0.0.1-alpha.7", | ||
"@portkey/provider-utils": "^0.0.1-alpha.7", | ||
"@metamask/object-multiplex": "^1.1.0", | ||
"@portkey/chain": "^0.0.1-alpha.8", | ||
"@portkey/provider-types": "^0.0.1-alpha.8", | ||
"@portkey/provider-utils": "^0.0.1-alpha.8", | ||
"@types/readable-stream": "^2.3.15", | ||
"pump": "^3.0.0", | ||
"readable-stream": "^4.4.0" | ||
}, | ||
"gitHead": "cb493d3b24a0029934fdf8b800752b9cf1d5a23f" | ||
"devDependencies": { | ||
"@types/pump": "^1.1.1" | ||
}, | ||
"gitHead": "4ef8103abed2d4e8c7de4dbdfb5a9417971dd30b" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78071
531
7
1
+ Addedpump@^3.0.0
+ Added@metamask/object-multiplex@1.3.0(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedpump@3.0.2(transitive)