@portal-hq/provider 0.2.8 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/providers/index.ts +1 -9
- package/src/signers/abstract.ts +3 -5
- package/src/signers/http.ts +6 -6
- package/src/signers/mpc.ts +6 -3
- package/types.d.ts +0 -7
- package/lib/commonjs/index.js +0 -8
- package/lib/commonjs/providers/index.js +0 -384
- package/lib/commonjs/requesters/http.js +0 -52
- package/lib/commonjs/requesters/index.js +0 -8
- package/lib/commonjs/signers/abstract.js +0 -19
- package/lib/commonjs/signers/http.js +0 -56
- package/lib/commonjs/signers/index.js +0 -12
- package/lib/commonjs/signers/mpc.js +0 -75
- package/lib/esm/index.js +0 -1
- package/lib/esm/providers/index.js +0 -382
- package/lib/esm/requesters/http.js +0 -50
- package/lib/esm/requesters/index.js +0 -1
- package/lib/esm/signers/abstract.js +0 -17
- package/lib/esm/signers/http.js +0 -54
- package/lib/esm/signers/index.js +0 -3
- package/lib/esm/signers/mpc.js +0 -73
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portal-hq/provider",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/esm/index",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"prepare:esm": "tsc --outDir lib/esm --module es2015 --target es2015"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@portal-hq/utils": "^0.2.
|
|
20
|
+
"@portal-hq/utils": "^0.2.9"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"typescript": "^4.8.4"
|
package/src/providers/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ class Provider {
|
|
|
43
43
|
public chainId: number
|
|
44
44
|
public isMPC?: boolean
|
|
45
45
|
|
|
46
|
-
private _address: string
|
|
46
|
+
private _address: string = ''
|
|
47
47
|
private apiUrl: string
|
|
48
48
|
private autoApprove?: boolean
|
|
49
49
|
private events: Record<string, RegisteredEventHandler[]>
|
|
@@ -388,14 +388,6 @@ class Provider {
|
|
|
388
388
|
})
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
-
private async dispatchDisconnect(): Promise<void> {
|
|
392
|
-
this.emit('disconnect', {
|
|
393
|
-
error: new ProviderRpcError({
|
|
394
|
-
code: RpcErrorCodes.Disconnected,
|
|
395
|
-
}),
|
|
396
|
-
})
|
|
397
|
-
}
|
|
398
|
-
|
|
399
391
|
/**
|
|
400
392
|
* Determines the RPC URL to be used for the current chain
|
|
401
393
|
*
|
package/src/signers/abstract.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SigningRequestArguments } from '@portal-hq/utils'
|
|
2
|
+
import { type SignResult } from '../../types'
|
|
2
3
|
|
|
3
4
|
abstract class Signer {
|
|
4
|
-
public async sign(
|
|
5
|
-
message: SigningRequestArguments,
|
|
6
|
-
provider?: any,
|
|
7
|
-
): Promise<SignResult> {
|
|
5
|
+
public async sign(_: SigningRequestArguments, __?: any): Promise<SignResult> {
|
|
8
6
|
throw new Error(
|
|
9
7
|
'[Portal] sign() method must be implemented in a child of BaseSigner',
|
|
10
8
|
)
|
package/src/signers/http.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { KeychainAdapter, MissingOptionError } from '@portal-hq/utils'
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from '
|
|
2
|
+
KeychainAdapter,
|
|
3
|
+
MissingOptionError,
|
|
4
|
+
SigningRequestArguments,
|
|
5
|
+
} from '@portal-hq/utils'
|
|
6
|
+
|
|
7
|
+
import { type HttpSignerOptions, type SignResult } from '../../types'
|
|
8
8
|
|
|
9
9
|
import Signer from './abstract'
|
|
10
10
|
import { Provider } from '../index'
|
package/src/signers/mpc.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { NativeModules } from 'react-native'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
KeychainAdapter,
|
|
4
|
+
MpcSigningError,
|
|
5
|
+
type SigningRequestArguments,
|
|
6
|
+
} from '@portal-hq/utils'
|
|
3
7
|
|
|
4
8
|
import { Provider } from '../index'
|
|
5
9
|
import Signer from './abstract'
|
|
@@ -7,12 +11,11 @@ import Signer from './abstract'
|
|
|
7
11
|
import {
|
|
8
12
|
type MpcSignerOptions,
|
|
9
13
|
type PortalMobileMpc,
|
|
10
|
-
type SigningRequestArguments,
|
|
11
14
|
type SigningResponse,
|
|
12
15
|
} from '../../types'
|
|
13
16
|
|
|
14
17
|
class MpcSigner implements Signer {
|
|
15
|
-
public _address: string
|
|
18
|
+
public _address: string = ''
|
|
16
19
|
private keychain: KeychainAdapter
|
|
17
20
|
private mpc: PortalMobileMpc
|
|
18
21
|
private mpcUrl: string
|
package/types.d.ts
CHANGED
|
@@ -78,13 +78,6 @@ export interface SignResult {
|
|
|
78
78
|
|
|
79
79
|
export interface SignerOptions {}
|
|
80
80
|
|
|
81
|
-
export interface SigningRequestArguments {
|
|
82
|
-
readonly chainId?: number
|
|
83
|
-
readonly method: string
|
|
84
|
-
readonly params?: unknown[] | SigningRequestParams
|
|
85
|
-
readonly requestId?: string
|
|
86
|
-
}
|
|
87
|
-
|
|
88
81
|
export interface SigningRequestParams {
|
|
89
82
|
gas?: string
|
|
90
83
|
}
|
package/lib/commonjs/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Provider = void 0;
|
|
7
|
-
var providers_1 = require("./providers");
|
|
8
|
-
Object.defineProperty(exports, "Provider", { enumerable: true, get: function () { return __importDefault(providers_1).default; } });
|
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const utils_1 = require("@portal-hq/utils");
|
|
13
|
-
const requesters_1 = require("../requesters");
|
|
14
|
-
const signers_1 = require("../signers");
|
|
15
|
-
const passiveSignerMethods = [
|
|
16
|
-
'eth_accounts',
|
|
17
|
-
'eth_chainId',
|
|
18
|
-
'eth_requestAccounts',
|
|
19
|
-
];
|
|
20
|
-
const signerMethods = [
|
|
21
|
-
'eth_accounts',
|
|
22
|
-
'eth_chainId',
|
|
23
|
-
'eth_requestAccounts',
|
|
24
|
-
'eth_sendTransaction',
|
|
25
|
-
'eth_sign',
|
|
26
|
-
'eth_signTransaction',
|
|
27
|
-
'eth_signTypedData',
|
|
28
|
-
'personal_sign',
|
|
29
|
-
];
|
|
30
|
-
class Provider {
|
|
31
|
-
constructor({
|
|
32
|
-
// Required options
|
|
33
|
-
apiKey, chainId, keychain,
|
|
34
|
-
// Optional options
|
|
35
|
-
apiUrl = 'api.portalhq.io', autoApprove = false, enableMpc = true, mpcUrl = 'mpc.portalhq.io', gatewayConfig = {}, }) {
|
|
36
|
-
// Handle required fields
|
|
37
|
-
if (!apiKey || apiKey.length === 0) {
|
|
38
|
-
throw new utils_1.InvalidApiKeyError();
|
|
39
|
-
}
|
|
40
|
-
if (!chainId) {
|
|
41
|
-
throw new utils_1.InvalidChainIdError();
|
|
42
|
-
}
|
|
43
|
-
if (!gatewayConfig) {
|
|
44
|
-
throw new utils_1.InvalidGatewayConfigError();
|
|
45
|
-
}
|
|
46
|
-
// Handle the stuff we can auto-set
|
|
47
|
-
this.apiKey = apiKey;
|
|
48
|
-
this.apiUrl = apiUrl;
|
|
49
|
-
this.autoApprove = autoApprove;
|
|
50
|
-
this.chainId = chainId;
|
|
51
|
-
this.events = {};
|
|
52
|
-
this.isMPC = enableMpc;
|
|
53
|
-
this.keychain = keychain;
|
|
54
|
-
this.log = console;
|
|
55
|
-
this.mpcUrl = mpcUrl;
|
|
56
|
-
this.portal = new requesters_1.HttpRequester({
|
|
57
|
-
baseUrl: this.apiUrl,
|
|
58
|
-
});
|
|
59
|
-
// Handle RPC Initialization
|
|
60
|
-
this.gatewayConfig = gatewayConfig;
|
|
61
|
-
this.rpc = new requesters_1.HttpRequester({
|
|
62
|
-
baseUrl: this.getRpcUrl(),
|
|
63
|
-
});
|
|
64
|
-
if (this.isMPC) {
|
|
65
|
-
// If MPC is enabled, initialize an MpcSigner
|
|
66
|
-
this.signer = new signers_1.MpcSigner({
|
|
67
|
-
mpcUrl: this.mpcUrl,
|
|
68
|
-
keychain: this.keychain,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
// If MPC is disabled, initialize an HttpSigner, talking to whatever httpHost was provided
|
|
73
|
-
this.signer = new signers_1.HttpSigner({
|
|
74
|
-
keychain: this.keychain,
|
|
75
|
-
portal: this.portal,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
this.dispatchConnect();
|
|
79
|
-
}
|
|
80
|
-
get address() {
|
|
81
|
-
return this._address;
|
|
82
|
-
}
|
|
83
|
-
set address(value) {
|
|
84
|
-
this._address = value;
|
|
85
|
-
if (this.signer && this.isMPC) {
|
|
86
|
-
;
|
|
87
|
-
this.signer._address = value;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
get rpcUrl() {
|
|
91
|
-
return this.getRpcUrl();
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Invokes all registered event handlers with the data provided
|
|
95
|
-
* - If any `once` handlers exist, they are removed after all handlers are invoked
|
|
96
|
-
*
|
|
97
|
-
* @param event The name of the event to be handled
|
|
98
|
-
* @param data The data to be passed to registered event handlers
|
|
99
|
-
* @returns BaseProvider
|
|
100
|
-
*/
|
|
101
|
-
emit(event, data) {
|
|
102
|
-
// Grab the registered event handlers if any are available
|
|
103
|
-
const handlers = this.events[event] || [];
|
|
104
|
-
// Execute every event handler
|
|
105
|
-
for (const registeredEventHandler of handlers) {
|
|
106
|
-
registeredEventHandler.handler(data);
|
|
107
|
-
}
|
|
108
|
-
// Remove any registered event handlers with the `once` flag
|
|
109
|
-
this.events[event] = handlers.filter((handler) => !handler.once);
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Registers an event handler for the provided event
|
|
114
|
-
*
|
|
115
|
-
* @param event The event name to add a handler to
|
|
116
|
-
* @param callback The callback to be invoked when the event is emitted
|
|
117
|
-
* @returns BaseProvider
|
|
118
|
-
*/
|
|
119
|
-
on(event, callback) {
|
|
120
|
-
// If no handlers are registered for this event, create an entry for the event
|
|
121
|
-
if (!this.events[event]) {
|
|
122
|
-
this.events[event] = [];
|
|
123
|
-
}
|
|
124
|
-
// Register event handler with the rudimentary event bus
|
|
125
|
-
if (typeof callback !== 'undefined') {
|
|
126
|
-
this.events[event].push({
|
|
127
|
-
handler: callback,
|
|
128
|
-
once: false,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Registers a single-execution event handler for the provided event
|
|
135
|
-
*
|
|
136
|
-
* @param event The event name to add a handler to
|
|
137
|
-
* @param callback The callback to be invoked the next time the event is emitted
|
|
138
|
-
* @returns BaseProvider
|
|
139
|
-
*/
|
|
140
|
-
once(event, callback) {
|
|
141
|
-
// If no handlers are registered for this event, create an entry for the event
|
|
142
|
-
if (!this.events[event]) {
|
|
143
|
-
this.events[event] = [];
|
|
144
|
-
}
|
|
145
|
-
// Register event handler with the rudimentary event bus
|
|
146
|
-
if (typeof callback !== 'undefined') {
|
|
147
|
-
this.events[event].push({
|
|
148
|
-
handler: callback,
|
|
149
|
-
once: true,
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
removeEventListener(event, listenerToRemove) {
|
|
155
|
-
if (!this.events[event]) {
|
|
156
|
-
this.log.info(`[PortalProvider] Attempted to remove a listener from unregistered event '${event}'. Ignoring.`);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
if (!listenerToRemove) {
|
|
160
|
-
this.events[event] = [];
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
const filterEventHandlers = (registeredEventHandler) => {
|
|
164
|
-
return registeredEventHandler.handler !== listenerToRemove;
|
|
165
|
-
};
|
|
166
|
-
this.events[event] = this.events[event].filter(filterEventHandlers);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Handles request routing in compliance with the EIP-1193 Ethereum Javascript Provider API
|
|
171
|
-
* - See here for more info: https://eips.ethereum.org/EIPS/eip-1193
|
|
172
|
-
*
|
|
173
|
-
* @param args The arguments of the request being made
|
|
174
|
-
* @returns Promise<any>
|
|
175
|
-
*/
|
|
176
|
-
request({ method, params }) {
|
|
177
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
if (method === 'eth_chainId') {
|
|
179
|
-
return this.chainId;
|
|
180
|
-
}
|
|
181
|
-
const isSignerMethod = signerMethods.includes(method);
|
|
182
|
-
let result;
|
|
183
|
-
if (!isSignerMethod && !method.startsWith('wallet_')) {
|
|
184
|
-
// Send to Gateway for RPC calls
|
|
185
|
-
const response = yield this.handleGatewayRequests({ method, params });
|
|
186
|
-
this.emit('portal_signatureReceived', {
|
|
187
|
-
method,
|
|
188
|
-
params,
|
|
189
|
-
signature: response,
|
|
190
|
-
});
|
|
191
|
-
if (response.error) {
|
|
192
|
-
throw new utils_1.ProviderRpcError(response.error);
|
|
193
|
-
}
|
|
194
|
-
result = response.result;
|
|
195
|
-
}
|
|
196
|
-
else if (isSignerMethod) {
|
|
197
|
-
// Handle signing
|
|
198
|
-
const transactionHash = yield this.handleSigningRequests({
|
|
199
|
-
method,
|
|
200
|
-
params,
|
|
201
|
-
});
|
|
202
|
-
if (transactionHash) {
|
|
203
|
-
console.log(`Received transaction hash: `, transactionHash);
|
|
204
|
-
this.emit('portal_signatureReceived', {
|
|
205
|
-
method,
|
|
206
|
-
params,
|
|
207
|
-
signature: transactionHash,
|
|
208
|
-
});
|
|
209
|
-
result = transactionHash;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
// Unsupported method
|
|
214
|
-
throw new utils_1.ProviderRpcError({
|
|
215
|
-
code: utils_1.RpcErrorCodes.UnsupportedMethod,
|
|
216
|
-
data: {
|
|
217
|
-
method,
|
|
218
|
-
params,
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
return result;
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Updates the chainId of this instance and builds a new RPC HttpRequester for
|
|
227
|
-
* the gateway used for the new chain
|
|
228
|
-
*
|
|
229
|
-
* @param chainId A hex string of the chainId to use for this connection
|
|
230
|
-
* @returns BaseProvider
|
|
231
|
-
*/
|
|
232
|
-
updateChainId(chainId) {
|
|
233
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
234
|
-
this.chainId = Number(`${chainId}`);
|
|
235
|
-
this.rpc = new requesters_1.HttpRequester({
|
|
236
|
-
baseUrl: this.getRpcUrl(),
|
|
237
|
-
});
|
|
238
|
-
this.emit('chainChanged', { chainId });
|
|
239
|
-
return this;
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Kicks off the approval flow for a given request
|
|
244
|
-
*
|
|
245
|
-
* @param args The arguments of the request being made
|
|
246
|
-
*/
|
|
247
|
-
getApproval({ method, params, }) {
|
|
248
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
-
// If autoApprove is enabled, just resolve to true
|
|
250
|
-
if (this.autoApprove) {
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
if (!this.events['portal_signingRequested']) {
|
|
254
|
-
throw new Error(`[PortalProvider] Auto-approve is disabled. Cannot perform signing requests without an event handler for the 'portal_signingRequested' event.`);
|
|
255
|
-
}
|
|
256
|
-
return new Promise((resolve) => {
|
|
257
|
-
// Remove already used listeners
|
|
258
|
-
this.removeEventListener('portal_signingApproved');
|
|
259
|
-
this.removeEventListener('portal_signingRejected');
|
|
260
|
-
// If the signing has been approved, resolve to true
|
|
261
|
-
this.once('portal_signingApproved', ({ method: approvedMethod, params: approvedParams }) => {
|
|
262
|
-
console.log(`[PortalProvider] Signing Approved`, method, params);
|
|
263
|
-
// Remove already used listeners
|
|
264
|
-
this.removeEventListener('portal_signingApproved');
|
|
265
|
-
this.removeEventListener('portal_signingRejected');
|
|
266
|
-
// First verify that this is the same signing request
|
|
267
|
-
if (method === approvedMethod &&
|
|
268
|
-
JSON.stringify(params) === JSON.stringify(approvedParams)) {
|
|
269
|
-
resolve(true);
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
// If the signing request has been rejected, resolve to false
|
|
273
|
-
this.once('portal_signingRejected', ({ method: rejectedMethod, params: rejectedParams }) => {
|
|
274
|
-
console.log(`[PortalProvider] Signing Approved`, method, params);
|
|
275
|
-
// Remove already used listeners
|
|
276
|
-
this.removeEventListener('portal_signingApproved');
|
|
277
|
-
this.removeEventListener('portal_signingRejected');
|
|
278
|
-
// First verify that this is the same signing request
|
|
279
|
-
if (method === rejectedMethod &&
|
|
280
|
-
JSON.stringify(params) === JSON.stringify(rejectedParams)) {
|
|
281
|
-
resolve(false);
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
// Tell any listening clients that signing has been requested
|
|
285
|
-
this.emit('portal_signingRequested', {
|
|
286
|
-
method,
|
|
287
|
-
params,
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
dispatchConnect() {
|
|
293
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
294
|
-
console.log(`[PortalProvider] Connected on chainId: 0x${this.chainId.toString(16)}`);
|
|
295
|
-
this.emit('connect', {
|
|
296
|
-
chainId: `0x${this.chainId.toString(16)}`,
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
dispatchDisconnect() {
|
|
301
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
302
|
-
this.emit('disconnect', {
|
|
303
|
-
error: new utils_1.ProviderRpcError({
|
|
304
|
-
code: utils_1.RpcErrorCodes.Disconnected,
|
|
305
|
-
}),
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Determines the RPC URL to be used for the current chain
|
|
311
|
-
*
|
|
312
|
-
* @returns string
|
|
313
|
-
*/
|
|
314
|
-
getRpcUrl() {
|
|
315
|
-
if (typeof this.gatewayConfig === 'string') {
|
|
316
|
-
// If the gatewayConfig is just a static URL, return that
|
|
317
|
-
return this.gatewayConfig;
|
|
318
|
-
}
|
|
319
|
-
else if (typeof this.gatewayConfig === 'object' &&
|
|
320
|
-
!this.gatewayConfig.hasOwnProperty(this.chainId)) {
|
|
321
|
-
// If there's no explicit mapping for the current chainId, error out
|
|
322
|
-
throw new Error(`[PortalProvider] No RPC endpoint configured for chainId: ${this.chainId}`);
|
|
323
|
-
}
|
|
324
|
-
// Get the entry for the current chainId from the gatewayConfig
|
|
325
|
-
const config = this.gatewayConfig[this.chainId];
|
|
326
|
-
if (typeof config === 'string') {
|
|
327
|
-
return config;
|
|
328
|
-
}
|
|
329
|
-
// If we got this far, there's no way to support the chain with the current config
|
|
330
|
-
throw new Error(`[PortalProvider] Could not find a valid gatewayConfig entry for chainId: ${this.chainId}`);
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Sends the provided request payload along to the RPC HttpRequester
|
|
334
|
-
*
|
|
335
|
-
* @param args The arguments of the request being made
|
|
336
|
-
* @returns Promise<any>
|
|
337
|
-
*/
|
|
338
|
-
handleGatewayRequests({ method, params, }) {
|
|
339
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
340
|
-
// Pass request off to the gateway
|
|
341
|
-
return yield this.rpc.post('', {
|
|
342
|
-
body: {
|
|
343
|
-
jsonrpc: '2.0',
|
|
344
|
-
id: this.chainId,
|
|
345
|
-
method,
|
|
346
|
-
params,
|
|
347
|
-
},
|
|
348
|
-
});
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Sends the provided request payload along to the Signer
|
|
353
|
-
*
|
|
354
|
-
* @param args The arguments of the request being made
|
|
355
|
-
* @returns Promise<any>
|
|
356
|
-
*/
|
|
357
|
-
handleSigningRequests({ method, params, }) {
|
|
358
|
-
var _a;
|
|
359
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
360
|
-
const isApproved = passiveSignerMethods.includes(method)
|
|
361
|
-
? true
|
|
362
|
-
: yield this.getApproval({ method, params });
|
|
363
|
-
if (!isApproved) {
|
|
364
|
-
this.log.info(`[PortalProvider] Request for signing method '${method}' could not be completed because it was not approved by the user.`);
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
switch (method) {
|
|
368
|
-
case 'eth_chainId':
|
|
369
|
-
return `0x${this.chainId.toString(16)}`;
|
|
370
|
-
case 'eth_accounts':
|
|
371
|
-
case 'eth_requestAccounts':
|
|
372
|
-
case 'eth_sendTransaction':
|
|
373
|
-
case 'eth_sign':
|
|
374
|
-
case 'eth_signTransaction':
|
|
375
|
-
case 'eth_signTypedData':
|
|
376
|
-
case 'personal_sign':
|
|
377
|
-
return yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.sign({ chainId: this.chainId, method, params }, this));
|
|
378
|
-
default:
|
|
379
|
-
throw new Error('[PortalProvider] Method "' + method + '" not supported');
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
exports.default = Provider;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const utils_1 = require("@portal-hq/utils");
|
|
13
|
-
class HttpRequester {
|
|
14
|
-
constructor({ baseUrl }) {
|
|
15
|
-
this.baseUrl = baseUrl.startsWith('https://')
|
|
16
|
-
? baseUrl
|
|
17
|
-
: `https://${baseUrl}`;
|
|
18
|
-
}
|
|
19
|
-
get(path, options) {
|
|
20
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
const requestOptions = {
|
|
22
|
-
method: 'GET',
|
|
23
|
-
url: `${this.baseUrl}${path}`,
|
|
24
|
-
};
|
|
25
|
-
if (options && options.headers) {
|
|
26
|
-
requestOptions.headers = this.buildHeaders(options.headers);
|
|
27
|
-
}
|
|
28
|
-
const request = new utils_1.HttpRequest(requestOptions);
|
|
29
|
-
const response = (yield request.send());
|
|
30
|
-
return response;
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
post(path, options) {
|
|
34
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
const requestOptions = {
|
|
36
|
-
method: 'POST',
|
|
37
|
-
url: `${this.baseUrl}${path}`,
|
|
38
|
-
};
|
|
39
|
-
requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
|
|
40
|
-
if (options && options.body) {
|
|
41
|
-
requestOptions.body = options.body;
|
|
42
|
-
}
|
|
43
|
-
const request = new utils_1.HttpRequest(requestOptions);
|
|
44
|
-
const response = (yield request.send());
|
|
45
|
-
return response;
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
buildHeaders(headers) {
|
|
49
|
-
return Object.assign({ 'Content-Type': 'application/json' }, headers);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
exports.default = HttpRequester;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.HttpRequester = void 0;
|
|
7
|
-
var http_1 = require("./http");
|
|
8
|
-
Object.defineProperty(exports, "HttpRequester", { enumerable: true, get: function () { return __importDefault(http_1).default; } });
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
class Signer {
|
|
13
|
-
sign(message, provider) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
throw new Error('[Portal] sign() method must be implemented in a child of BaseSigner');
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
exports.default = Signer;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const utils_1 = require("@portal-hq/utils");
|
|
13
|
-
class HttpSigner {
|
|
14
|
-
constructor(opts) {
|
|
15
|
-
if (!opts.portal) {
|
|
16
|
-
throw new utils_1.MissingOptionError({
|
|
17
|
-
className: 'HttpSigner',
|
|
18
|
-
option: 'portal',
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
this.keychain = opts.keychain;
|
|
22
|
-
this.portal = opts.portal;
|
|
23
|
-
}
|
|
24
|
-
sign(message, provider) {
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const address = yield this.keychain.getAddress();
|
|
27
|
-
const { chainId, method, params } = message;
|
|
28
|
-
switch (method) {
|
|
29
|
-
case 'eth_requestAccounts':
|
|
30
|
-
return [address];
|
|
31
|
-
case 'eth_accounts':
|
|
32
|
-
return [address];
|
|
33
|
-
default:
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
console.log(`[Portal:HttpSigner] Requesting signature from exchange for:`, JSON.stringify({
|
|
37
|
-
chainId,
|
|
38
|
-
method,
|
|
39
|
-
params: JSON.stringify([params]),
|
|
40
|
-
}, null, 2));
|
|
41
|
-
const signatureResponse = yield this.portal.post('/api/v1/clients/transactions/sign', {
|
|
42
|
-
body: {
|
|
43
|
-
chainId,
|
|
44
|
-
method,
|
|
45
|
-
params: JSON.stringify([params]),
|
|
46
|
-
},
|
|
47
|
-
headers: {
|
|
48
|
-
Authorization: `Bearer ${provider.apiKey}`,
|
|
49
|
-
'Content-Type': 'application/json',
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
return signatureResponse;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
exports.default = HttpSigner;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.MpcSigner = exports.HttpSigner = exports.Signer = void 0;
|
|
7
|
-
var abstract_1 = require("./abstract");
|
|
8
|
-
Object.defineProperty(exports, "Signer", { enumerable: true, get: function () { return __importDefault(abstract_1).default; } });
|
|
9
|
-
var http_1 = require("./http");
|
|
10
|
-
Object.defineProperty(exports, "HttpSigner", { enumerable: true, get: function () { return __importDefault(http_1).default; } });
|
|
11
|
-
var mpc_1 = require("./mpc");
|
|
12
|
-
Object.defineProperty(exports, "MpcSigner", { enumerable: true, get: function () { return __importDefault(mpc_1).default; } });
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const react_native_1 = require("react-native");
|
|
13
|
-
const utils_1 = require("@portal-hq/utils");
|
|
14
|
-
class MpcSigner {
|
|
15
|
-
constructor(opts) {
|
|
16
|
-
this.buildParams = (method, txParams) => {
|
|
17
|
-
let params = txParams;
|
|
18
|
-
switch (method) {
|
|
19
|
-
case 'eth_sign':
|
|
20
|
-
case 'personal_sign':
|
|
21
|
-
if (!Array.isArray(txParams)) {
|
|
22
|
-
params = [txParams];
|
|
23
|
-
}
|
|
24
|
-
break;
|
|
25
|
-
default:
|
|
26
|
-
if (Array.isArray(txParams)) {
|
|
27
|
-
params = txParams[0];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return params;
|
|
31
|
-
};
|
|
32
|
-
this.keychain = opts.keychain;
|
|
33
|
-
this.mpc = react_native_1.NativeModules.PortalMobileMpc;
|
|
34
|
-
this.mpcUrl = opts.mpcUrl;
|
|
35
|
-
if (!this.mpc) {
|
|
36
|
-
throw new Error(`[Portal.Provider.MpcSigner] The MPC module could not be found by the signer. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
get address() {
|
|
40
|
-
return (() => __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
if (this._address) {
|
|
42
|
-
return this._address;
|
|
43
|
-
}
|
|
44
|
-
const address = yield this.keychain.getAddress();
|
|
45
|
-
this._address = address;
|
|
46
|
-
return address;
|
|
47
|
-
}))();
|
|
48
|
-
}
|
|
49
|
-
sign(message, provider) {
|
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
const address = yield this.keychain.getAddress();
|
|
52
|
-
const apiKey = provider.apiKey;
|
|
53
|
-
const { method, params } = message;
|
|
54
|
-
switch (method) {
|
|
55
|
-
case 'eth_requestAccounts':
|
|
56
|
-
return [address];
|
|
57
|
-
case 'eth_accounts':
|
|
58
|
-
return [address];
|
|
59
|
-
default:
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
console.log(`[Portal:MpcSigner] Requesting signature from PortalMobileMpc for:`, JSON.stringify(message, null, 2));
|
|
63
|
-
const dkg = yield this.keychain.getDkgResult();
|
|
64
|
-
console.log(`[PortalMpcSigner] RPC URL: ${provider.rpcUrl}`);
|
|
65
|
-
const result = yield this.mpc.sign(apiKey, this.mpcUrl, dkg, message.method, JSON.stringify(this.buildParams(method, params)), provider.rpcUrl, provider.chainId.toString());
|
|
66
|
-
console.log(`[PortalMpcSigner] Result: `, result);
|
|
67
|
-
const { data, error } = JSON.parse(String(result));
|
|
68
|
-
if (error && error.length) {
|
|
69
|
-
throw new utils_1.MpcSigningError(error);
|
|
70
|
-
}
|
|
71
|
-
return data;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
exports.default = MpcSigner;
|
package/lib/esm/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as Provider } from './providers';
|
|
@@ -1,382 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { InvalidApiKeyError, InvalidChainIdError, InvalidGatewayConfigError, ProviderRpcError, RpcErrorCodes, } from '@portal-hq/utils';
|
|
11
|
-
import { HttpRequester } from '../requesters';
|
|
12
|
-
import { HttpSigner, MpcSigner } from '../signers';
|
|
13
|
-
const passiveSignerMethods = [
|
|
14
|
-
'eth_accounts',
|
|
15
|
-
'eth_chainId',
|
|
16
|
-
'eth_requestAccounts',
|
|
17
|
-
];
|
|
18
|
-
const signerMethods = [
|
|
19
|
-
'eth_accounts',
|
|
20
|
-
'eth_chainId',
|
|
21
|
-
'eth_requestAccounts',
|
|
22
|
-
'eth_sendTransaction',
|
|
23
|
-
'eth_sign',
|
|
24
|
-
'eth_signTransaction',
|
|
25
|
-
'eth_signTypedData',
|
|
26
|
-
'personal_sign',
|
|
27
|
-
];
|
|
28
|
-
class Provider {
|
|
29
|
-
constructor({
|
|
30
|
-
// Required options
|
|
31
|
-
apiKey, chainId, keychain,
|
|
32
|
-
// Optional options
|
|
33
|
-
apiUrl = 'api.portalhq.io', autoApprove = false, enableMpc = true, mpcUrl = 'mpc.portalhq.io', gatewayConfig = {}, }) {
|
|
34
|
-
// Handle required fields
|
|
35
|
-
if (!apiKey || apiKey.length === 0) {
|
|
36
|
-
throw new InvalidApiKeyError();
|
|
37
|
-
}
|
|
38
|
-
if (!chainId) {
|
|
39
|
-
throw new InvalidChainIdError();
|
|
40
|
-
}
|
|
41
|
-
if (!gatewayConfig) {
|
|
42
|
-
throw new InvalidGatewayConfigError();
|
|
43
|
-
}
|
|
44
|
-
// Handle the stuff we can auto-set
|
|
45
|
-
this.apiKey = apiKey;
|
|
46
|
-
this.apiUrl = apiUrl;
|
|
47
|
-
this.autoApprove = autoApprove;
|
|
48
|
-
this.chainId = chainId;
|
|
49
|
-
this.events = {};
|
|
50
|
-
this.isMPC = enableMpc;
|
|
51
|
-
this.keychain = keychain;
|
|
52
|
-
this.log = console;
|
|
53
|
-
this.mpcUrl = mpcUrl;
|
|
54
|
-
this.portal = new HttpRequester({
|
|
55
|
-
baseUrl: this.apiUrl,
|
|
56
|
-
});
|
|
57
|
-
// Handle RPC Initialization
|
|
58
|
-
this.gatewayConfig = gatewayConfig;
|
|
59
|
-
this.rpc = new HttpRequester({
|
|
60
|
-
baseUrl: this.getRpcUrl(),
|
|
61
|
-
});
|
|
62
|
-
if (this.isMPC) {
|
|
63
|
-
// If MPC is enabled, initialize an MpcSigner
|
|
64
|
-
this.signer = new MpcSigner({
|
|
65
|
-
mpcUrl: this.mpcUrl,
|
|
66
|
-
keychain: this.keychain,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
// If MPC is disabled, initialize an HttpSigner, talking to whatever httpHost was provided
|
|
71
|
-
this.signer = new HttpSigner({
|
|
72
|
-
keychain: this.keychain,
|
|
73
|
-
portal: this.portal,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
this.dispatchConnect();
|
|
77
|
-
}
|
|
78
|
-
get address() {
|
|
79
|
-
return this._address;
|
|
80
|
-
}
|
|
81
|
-
set address(value) {
|
|
82
|
-
this._address = value;
|
|
83
|
-
if (this.signer && this.isMPC) {
|
|
84
|
-
;
|
|
85
|
-
this.signer._address = value;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
get rpcUrl() {
|
|
89
|
-
return this.getRpcUrl();
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Invokes all registered event handlers with the data provided
|
|
93
|
-
* - If any `once` handlers exist, they are removed after all handlers are invoked
|
|
94
|
-
*
|
|
95
|
-
* @param event The name of the event to be handled
|
|
96
|
-
* @param data The data to be passed to registered event handlers
|
|
97
|
-
* @returns BaseProvider
|
|
98
|
-
*/
|
|
99
|
-
emit(event, data) {
|
|
100
|
-
// Grab the registered event handlers if any are available
|
|
101
|
-
const handlers = this.events[event] || [];
|
|
102
|
-
// Execute every event handler
|
|
103
|
-
for (const registeredEventHandler of handlers) {
|
|
104
|
-
registeredEventHandler.handler(data);
|
|
105
|
-
}
|
|
106
|
-
// Remove any registered event handlers with the `once` flag
|
|
107
|
-
this.events[event] = handlers.filter((handler) => !handler.once);
|
|
108
|
-
return this;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Registers an event handler for the provided event
|
|
112
|
-
*
|
|
113
|
-
* @param event The event name to add a handler to
|
|
114
|
-
* @param callback The callback to be invoked when the event is emitted
|
|
115
|
-
* @returns BaseProvider
|
|
116
|
-
*/
|
|
117
|
-
on(event, callback) {
|
|
118
|
-
// If no handlers are registered for this event, create an entry for the event
|
|
119
|
-
if (!this.events[event]) {
|
|
120
|
-
this.events[event] = [];
|
|
121
|
-
}
|
|
122
|
-
// Register event handler with the rudimentary event bus
|
|
123
|
-
if (typeof callback !== 'undefined') {
|
|
124
|
-
this.events[event].push({
|
|
125
|
-
handler: callback,
|
|
126
|
-
once: false,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Registers a single-execution event handler for the provided event
|
|
133
|
-
*
|
|
134
|
-
* @param event The event name to add a handler to
|
|
135
|
-
* @param callback The callback to be invoked the next time the event is emitted
|
|
136
|
-
* @returns BaseProvider
|
|
137
|
-
*/
|
|
138
|
-
once(event, callback) {
|
|
139
|
-
// If no handlers are registered for this event, create an entry for the event
|
|
140
|
-
if (!this.events[event]) {
|
|
141
|
-
this.events[event] = [];
|
|
142
|
-
}
|
|
143
|
-
// Register event handler with the rudimentary event bus
|
|
144
|
-
if (typeof callback !== 'undefined') {
|
|
145
|
-
this.events[event].push({
|
|
146
|
-
handler: callback,
|
|
147
|
-
once: true,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
removeEventListener(event, listenerToRemove) {
|
|
153
|
-
if (!this.events[event]) {
|
|
154
|
-
this.log.info(`[PortalProvider] Attempted to remove a listener from unregistered event '${event}'. Ignoring.`);
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (!listenerToRemove) {
|
|
158
|
-
this.events[event] = [];
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
const filterEventHandlers = (registeredEventHandler) => {
|
|
162
|
-
return registeredEventHandler.handler !== listenerToRemove;
|
|
163
|
-
};
|
|
164
|
-
this.events[event] = this.events[event].filter(filterEventHandlers);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Handles request routing in compliance with the EIP-1193 Ethereum Javascript Provider API
|
|
169
|
-
* - See here for more info: https://eips.ethereum.org/EIPS/eip-1193
|
|
170
|
-
*
|
|
171
|
-
* @param args The arguments of the request being made
|
|
172
|
-
* @returns Promise<any>
|
|
173
|
-
*/
|
|
174
|
-
request({ method, params }) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
if (method === 'eth_chainId') {
|
|
177
|
-
return this.chainId;
|
|
178
|
-
}
|
|
179
|
-
const isSignerMethod = signerMethods.includes(method);
|
|
180
|
-
let result;
|
|
181
|
-
if (!isSignerMethod && !method.startsWith('wallet_')) {
|
|
182
|
-
// Send to Gateway for RPC calls
|
|
183
|
-
const response = yield this.handleGatewayRequests({ method, params });
|
|
184
|
-
this.emit('portal_signatureReceived', {
|
|
185
|
-
method,
|
|
186
|
-
params,
|
|
187
|
-
signature: response,
|
|
188
|
-
});
|
|
189
|
-
if (response.error) {
|
|
190
|
-
throw new ProviderRpcError(response.error);
|
|
191
|
-
}
|
|
192
|
-
result = response.result;
|
|
193
|
-
}
|
|
194
|
-
else if (isSignerMethod) {
|
|
195
|
-
// Handle signing
|
|
196
|
-
const transactionHash = yield this.handleSigningRequests({
|
|
197
|
-
method,
|
|
198
|
-
params,
|
|
199
|
-
});
|
|
200
|
-
if (transactionHash) {
|
|
201
|
-
console.log(`Received transaction hash: `, transactionHash);
|
|
202
|
-
this.emit('portal_signatureReceived', {
|
|
203
|
-
method,
|
|
204
|
-
params,
|
|
205
|
-
signature: transactionHash,
|
|
206
|
-
});
|
|
207
|
-
result = transactionHash;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
// Unsupported method
|
|
212
|
-
throw new ProviderRpcError({
|
|
213
|
-
code: RpcErrorCodes.UnsupportedMethod,
|
|
214
|
-
data: {
|
|
215
|
-
method,
|
|
216
|
-
params,
|
|
217
|
-
},
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
return result;
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Updates the chainId of this instance and builds a new RPC HttpRequester for
|
|
225
|
-
* the gateway used for the new chain
|
|
226
|
-
*
|
|
227
|
-
* @param chainId A hex string of the chainId to use for this connection
|
|
228
|
-
* @returns BaseProvider
|
|
229
|
-
*/
|
|
230
|
-
updateChainId(chainId) {
|
|
231
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
-
this.chainId = Number(`${chainId}`);
|
|
233
|
-
this.rpc = new HttpRequester({
|
|
234
|
-
baseUrl: this.getRpcUrl(),
|
|
235
|
-
});
|
|
236
|
-
this.emit('chainChanged', { chainId });
|
|
237
|
-
return this;
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Kicks off the approval flow for a given request
|
|
242
|
-
*
|
|
243
|
-
* @param args The arguments of the request being made
|
|
244
|
-
*/
|
|
245
|
-
getApproval({ method, params, }) {
|
|
246
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
247
|
-
// If autoApprove is enabled, just resolve to true
|
|
248
|
-
if (this.autoApprove) {
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
if (!this.events['portal_signingRequested']) {
|
|
252
|
-
throw new Error(`[PortalProvider] Auto-approve is disabled. Cannot perform signing requests without an event handler for the 'portal_signingRequested' event.`);
|
|
253
|
-
}
|
|
254
|
-
return new Promise((resolve) => {
|
|
255
|
-
// Remove already used listeners
|
|
256
|
-
this.removeEventListener('portal_signingApproved');
|
|
257
|
-
this.removeEventListener('portal_signingRejected');
|
|
258
|
-
// If the signing has been approved, resolve to true
|
|
259
|
-
this.once('portal_signingApproved', ({ method: approvedMethod, params: approvedParams }) => {
|
|
260
|
-
console.log(`[PortalProvider] Signing Approved`, method, params);
|
|
261
|
-
// Remove already used listeners
|
|
262
|
-
this.removeEventListener('portal_signingApproved');
|
|
263
|
-
this.removeEventListener('portal_signingRejected');
|
|
264
|
-
// First verify that this is the same signing request
|
|
265
|
-
if (method === approvedMethod &&
|
|
266
|
-
JSON.stringify(params) === JSON.stringify(approvedParams)) {
|
|
267
|
-
resolve(true);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
// If the signing request has been rejected, resolve to false
|
|
271
|
-
this.once('portal_signingRejected', ({ method: rejectedMethod, params: rejectedParams }) => {
|
|
272
|
-
console.log(`[PortalProvider] Signing Approved`, method, params);
|
|
273
|
-
// Remove already used listeners
|
|
274
|
-
this.removeEventListener('portal_signingApproved');
|
|
275
|
-
this.removeEventListener('portal_signingRejected');
|
|
276
|
-
// First verify that this is the same signing request
|
|
277
|
-
if (method === rejectedMethod &&
|
|
278
|
-
JSON.stringify(params) === JSON.stringify(rejectedParams)) {
|
|
279
|
-
resolve(false);
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
// Tell any listening clients that signing has been requested
|
|
283
|
-
this.emit('portal_signingRequested', {
|
|
284
|
-
method,
|
|
285
|
-
params,
|
|
286
|
-
});
|
|
287
|
-
});
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
dispatchConnect() {
|
|
291
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
292
|
-
console.log(`[PortalProvider] Connected on chainId: 0x${this.chainId.toString(16)}`);
|
|
293
|
-
this.emit('connect', {
|
|
294
|
-
chainId: `0x${this.chainId.toString(16)}`,
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
dispatchDisconnect() {
|
|
299
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
300
|
-
this.emit('disconnect', {
|
|
301
|
-
error: new ProviderRpcError({
|
|
302
|
-
code: RpcErrorCodes.Disconnected,
|
|
303
|
-
}),
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Determines the RPC URL to be used for the current chain
|
|
309
|
-
*
|
|
310
|
-
* @returns string
|
|
311
|
-
*/
|
|
312
|
-
getRpcUrl() {
|
|
313
|
-
if (typeof this.gatewayConfig === 'string') {
|
|
314
|
-
// If the gatewayConfig is just a static URL, return that
|
|
315
|
-
return this.gatewayConfig;
|
|
316
|
-
}
|
|
317
|
-
else if (typeof this.gatewayConfig === 'object' &&
|
|
318
|
-
!this.gatewayConfig.hasOwnProperty(this.chainId)) {
|
|
319
|
-
// If there's no explicit mapping for the current chainId, error out
|
|
320
|
-
throw new Error(`[PortalProvider] No RPC endpoint configured for chainId: ${this.chainId}`);
|
|
321
|
-
}
|
|
322
|
-
// Get the entry for the current chainId from the gatewayConfig
|
|
323
|
-
const config = this.gatewayConfig[this.chainId];
|
|
324
|
-
if (typeof config === 'string') {
|
|
325
|
-
return config;
|
|
326
|
-
}
|
|
327
|
-
// If we got this far, there's no way to support the chain with the current config
|
|
328
|
-
throw new Error(`[PortalProvider] Could not find a valid gatewayConfig entry for chainId: ${this.chainId}`);
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Sends the provided request payload along to the RPC HttpRequester
|
|
332
|
-
*
|
|
333
|
-
* @param args The arguments of the request being made
|
|
334
|
-
* @returns Promise<any>
|
|
335
|
-
*/
|
|
336
|
-
handleGatewayRequests({ method, params, }) {
|
|
337
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
338
|
-
// Pass request off to the gateway
|
|
339
|
-
return yield this.rpc.post('', {
|
|
340
|
-
body: {
|
|
341
|
-
jsonrpc: '2.0',
|
|
342
|
-
id: this.chainId,
|
|
343
|
-
method,
|
|
344
|
-
params,
|
|
345
|
-
},
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Sends the provided request payload along to the Signer
|
|
351
|
-
*
|
|
352
|
-
* @param args The arguments of the request being made
|
|
353
|
-
* @returns Promise<any>
|
|
354
|
-
*/
|
|
355
|
-
handleSigningRequests({ method, params, }) {
|
|
356
|
-
var _a;
|
|
357
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
358
|
-
const isApproved = passiveSignerMethods.includes(method)
|
|
359
|
-
? true
|
|
360
|
-
: yield this.getApproval({ method, params });
|
|
361
|
-
if (!isApproved) {
|
|
362
|
-
this.log.info(`[PortalProvider] Request for signing method '${method}' could not be completed because it was not approved by the user.`);
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
switch (method) {
|
|
366
|
-
case 'eth_chainId':
|
|
367
|
-
return `0x${this.chainId.toString(16)}`;
|
|
368
|
-
case 'eth_accounts':
|
|
369
|
-
case 'eth_requestAccounts':
|
|
370
|
-
case 'eth_sendTransaction':
|
|
371
|
-
case 'eth_sign':
|
|
372
|
-
case 'eth_signTransaction':
|
|
373
|
-
case 'eth_signTypedData':
|
|
374
|
-
case 'personal_sign':
|
|
375
|
-
return yield ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.sign({ chainId: this.chainId, method, params }, this));
|
|
376
|
-
default:
|
|
377
|
-
throw new Error('[PortalProvider] Method "' + method + '" not supported');
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
export default Provider;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { HttpRequest, } from '@portal-hq/utils';
|
|
11
|
-
class HttpRequester {
|
|
12
|
-
constructor({ baseUrl }) {
|
|
13
|
-
this.baseUrl = baseUrl.startsWith('https://')
|
|
14
|
-
? baseUrl
|
|
15
|
-
: `https://${baseUrl}`;
|
|
16
|
-
}
|
|
17
|
-
get(path, options) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
const requestOptions = {
|
|
20
|
-
method: 'GET',
|
|
21
|
-
url: `${this.baseUrl}${path}`,
|
|
22
|
-
};
|
|
23
|
-
if (options && options.headers) {
|
|
24
|
-
requestOptions.headers = this.buildHeaders(options.headers);
|
|
25
|
-
}
|
|
26
|
-
const request = new HttpRequest(requestOptions);
|
|
27
|
-
const response = (yield request.send());
|
|
28
|
-
return response;
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
post(path, options) {
|
|
32
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
const requestOptions = {
|
|
34
|
-
method: 'POST',
|
|
35
|
-
url: `${this.baseUrl}${path}`,
|
|
36
|
-
};
|
|
37
|
-
requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
|
|
38
|
-
if (options && options.body) {
|
|
39
|
-
requestOptions.body = options.body;
|
|
40
|
-
}
|
|
41
|
-
const request = new HttpRequest(requestOptions);
|
|
42
|
-
const response = (yield request.send());
|
|
43
|
-
return response;
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
buildHeaders(headers) {
|
|
47
|
-
return Object.assign({ 'Content-Type': 'application/json' }, headers);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
export default HttpRequester;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as HttpRequester } from './http';
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
class Signer {
|
|
11
|
-
sign(message, provider) {
|
|
12
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
-
throw new Error('[Portal] sign() method must be implemented in a child of BaseSigner');
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export default Signer;
|
package/lib/esm/signers/http.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { MissingOptionError } from '@portal-hq/utils';
|
|
11
|
-
class HttpSigner {
|
|
12
|
-
constructor(opts) {
|
|
13
|
-
if (!opts.portal) {
|
|
14
|
-
throw new MissingOptionError({
|
|
15
|
-
className: 'HttpSigner',
|
|
16
|
-
option: 'portal',
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
this.keychain = opts.keychain;
|
|
20
|
-
this.portal = opts.portal;
|
|
21
|
-
}
|
|
22
|
-
sign(message, provider) {
|
|
23
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
const address = yield this.keychain.getAddress();
|
|
25
|
-
const { chainId, method, params } = message;
|
|
26
|
-
switch (method) {
|
|
27
|
-
case 'eth_requestAccounts':
|
|
28
|
-
return [address];
|
|
29
|
-
case 'eth_accounts':
|
|
30
|
-
return [address];
|
|
31
|
-
default:
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
console.log(`[Portal:HttpSigner] Requesting signature from exchange for:`, JSON.stringify({
|
|
35
|
-
chainId,
|
|
36
|
-
method,
|
|
37
|
-
params: JSON.stringify([params]),
|
|
38
|
-
}, null, 2));
|
|
39
|
-
const signatureResponse = yield this.portal.post('/api/v1/clients/transactions/sign', {
|
|
40
|
-
body: {
|
|
41
|
-
chainId,
|
|
42
|
-
method,
|
|
43
|
-
params: JSON.stringify([params]),
|
|
44
|
-
},
|
|
45
|
-
headers: {
|
|
46
|
-
Authorization: `Bearer ${provider.apiKey}`,
|
|
47
|
-
'Content-Type': 'application/json',
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
return signatureResponse;
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
export default HttpSigner;
|
package/lib/esm/signers/index.js
DELETED
package/lib/esm/signers/mpc.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { NativeModules } from 'react-native';
|
|
11
|
-
import { MpcSigningError } from '@portal-hq/utils';
|
|
12
|
-
class MpcSigner {
|
|
13
|
-
constructor(opts) {
|
|
14
|
-
this.buildParams = (method, txParams) => {
|
|
15
|
-
let params = txParams;
|
|
16
|
-
switch (method) {
|
|
17
|
-
case 'eth_sign':
|
|
18
|
-
case 'personal_sign':
|
|
19
|
-
if (!Array.isArray(txParams)) {
|
|
20
|
-
params = [txParams];
|
|
21
|
-
}
|
|
22
|
-
break;
|
|
23
|
-
default:
|
|
24
|
-
if (Array.isArray(txParams)) {
|
|
25
|
-
params = txParams[0];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return params;
|
|
29
|
-
};
|
|
30
|
-
this.keychain = opts.keychain;
|
|
31
|
-
this.mpc = NativeModules.PortalMobileMpc;
|
|
32
|
-
this.mpcUrl = opts.mpcUrl;
|
|
33
|
-
if (!this.mpc) {
|
|
34
|
-
throw new Error(`[Portal.Provider.MpcSigner] The MPC module could not be found by the signer. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
get address() {
|
|
38
|
-
return (() => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
if (this._address) {
|
|
40
|
-
return this._address;
|
|
41
|
-
}
|
|
42
|
-
const address = yield this.keychain.getAddress();
|
|
43
|
-
this._address = address;
|
|
44
|
-
return address;
|
|
45
|
-
}))();
|
|
46
|
-
}
|
|
47
|
-
sign(message, provider) {
|
|
48
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
-
const address = yield this.keychain.getAddress();
|
|
50
|
-
const apiKey = provider.apiKey;
|
|
51
|
-
const { method, params } = message;
|
|
52
|
-
switch (method) {
|
|
53
|
-
case 'eth_requestAccounts':
|
|
54
|
-
return [address];
|
|
55
|
-
case 'eth_accounts':
|
|
56
|
-
return [address];
|
|
57
|
-
default:
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
console.log(`[Portal:MpcSigner] Requesting signature from PortalMobileMpc for:`, JSON.stringify(message, null, 2));
|
|
61
|
-
const dkg = yield this.keychain.getDkgResult();
|
|
62
|
-
console.log(`[PortalMpcSigner] RPC URL: ${provider.rpcUrl}`);
|
|
63
|
-
const result = yield this.mpc.sign(apiKey, this.mpcUrl, dkg, message.method, JSON.stringify(this.buildParams(method, params)), provider.rpcUrl, provider.chainId.toString());
|
|
64
|
-
console.log(`[PortalMpcSigner] Result: `, result);
|
|
65
|
-
const { data, error } = JSON.parse(String(result));
|
|
66
|
-
if (error && error.length) {
|
|
67
|
-
throw new MpcSigningError(error);
|
|
68
|
-
}
|
|
69
|
-
return data;
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
export default MpcSigner;
|