@onekeyfe/onekey-near-provider 0.0.4 → 0.0.7-alpha.0
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/dist/OneKeyNearProvider.d.ts +30 -43
- package/dist/OneKeyNearProvider.js +79 -26
- package/dist/cjs/OneKeyNearProvider.js +78 -25
- package/package.json +6 -6
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { ProviderNearBase } from './ProviderNearBase';
|
|
3
2
|
import { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';
|
|
4
|
-
import { Account } from 'near-api-js';
|
|
5
|
-
import type { Action as NearTransactionAction } from 'near-api-js/lib/transaction';
|
|
6
3
|
import { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
|
|
4
|
+
import { Account, Connection, transactions } from 'near-api-js';
|
|
5
|
+
import type { Action as NearTransactionAction, Transaction as NearTransaction } from 'near-api-js/lib/transaction';
|
|
6
|
+
import type { AccessKeyInfoView, FinalExecutionOutcome } from 'near-api-js/lib/providers/provider';
|
|
7
|
+
import { ProviderNearBase } from './ProviderNearBase';
|
|
7
8
|
export declare type NearAccountInfo = {
|
|
8
9
|
accountId: string;
|
|
9
10
|
publicKey: string;
|
|
10
11
|
allKeys?: string[];
|
|
11
12
|
};
|
|
12
|
-
export declare type
|
|
13
|
+
export declare type NearNetworkInfo = {
|
|
13
14
|
networkId: string;
|
|
14
15
|
nodeUrls?: string[];
|
|
15
16
|
};
|
|
17
|
+
export declare type NearNetworkChangedPayload = NearNetworkInfo;
|
|
16
18
|
export declare type NearProviderState = {
|
|
17
19
|
accounts: Array<NearAccountInfo>;
|
|
18
20
|
network: NearNetworkChangedPayload;
|
|
@@ -23,20 +25,7 @@ export declare type NearAccountsChangedPayload = {
|
|
|
23
25
|
export declare type NearUnlockChangedPayload = {
|
|
24
26
|
isUnlocked: boolean;
|
|
25
27
|
};
|
|
26
|
-
export declare type
|
|
27
|
-
hash: string;
|
|
28
|
-
};
|
|
29
|
-
export declare type NearBlockInfo = {
|
|
30
|
-
header: NearBlockInfoHeader;
|
|
31
|
-
};
|
|
32
|
-
export declare type NearConnectionProvider = {
|
|
33
|
-
sendJsonRpc: (method: string, params: object) => Promise<any>;
|
|
34
|
-
block: (params: object) => Promise<NearBlockInfo>;
|
|
35
|
-
query: (...args: any) => Promise<any>;
|
|
36
|
-
};
|
|
37
|
-
export declare type NearConnection = {
|
|
38
|
-
provider: NearConnectionProvider;
|
|
39
|
-
};
|
|
28
|
+
export declare type NearConnection = Connection;
|
|
40
29
|
export declare type TransactionCreatorParams = {
|
|
41
30
|
accountId: string;
|
|
42
31
|
publicKey: string;
|
|
@@ -47,8 +36,8 @@ export declare type TransactionCreatorParams = {
|
|
|
47
36
|
};
|
|
48
37
|
export declare type TransactionCreator = (params: TransactionCreatorParams) => any;
|
|
49
38
|
export declare type OneKeyNearWalletProps = {
|
|
50
|
-
connection
|
|
51
|
-
networkId
|
|
39
|
+
connection?: NearConnection | any;
|
|
40
|
+
networkId?: string;
|
|
52
41
|
connectEagerly?: boolean;
|
|
53
42
|
enablePageReload?: boolean;
|
|
54
43
|
timeout?: number;
|
|
@@ -60,31 +49,29 @@ export declare type OneKeyWalletAccountProps = {
|
|
|
60
49
|
connection: unknown;
|
|
61
50
|
accountId: string;
|
|
62
51
|
};
|
|
52
|
+
export declare type CommonOptionsMeta = unknown | string | object;
|
|
63
53
|
export declare type SignInOptions = {
|
|
64
54
|
contractId?: string;
|
|
65
55
|
methodNames?: string[];
|
|
66
56
|
successUrl?: string;
|
|
67
57
|
failureUrl?: string;
|
|
68
58
|
};
|
|
69
|
-
export declare type
|
|
70
|
-
encode: () => Uint8Array;
|
|
71
|
-
};
|
|
72
|
-
export declare type NearAccountAccessKey = {
|
|
73
|
-
nonce: number;
|
|
74
|
-
};
|
|
75
|
-
export declare type NearAccountAccessKeyInfo = {
|
|
76
|
-
public_key: string;
|
|
77
|
-
access_key: NearAccountAccessKey;
|
|
78
|
-
};
|
|
59
|
+
export declare type SignInResult = NearAccountsChangedPayload;
|
|
79
60
|
export declare type SignTransactionsOptions = {
|
|
80
61
|
transactions: NearTransaction[];
|
|
81
62
|
callbackUrl?: string;
|
|
82
|
-
meta?:
|
|
63
|
+
meta?: CommonOptionsMeta;
|
|
83
64
|
send?: boolean;
|
|
84
65
|
};
|
|
66
|
+
export declare type SignTransactionsResult = {
|
|
67
|
+
transactionHashes: string[];
|
|
68
|
+
};
|
|
85
69
|
export declare type SignMessagesOptions = {
|
|
86
70
|
messages: string[];
|
|
87
|
-
meta?:
|
|
71
|
+
meta?: CommonOptionsMeta;
|
|
72
|
+
};
|
|
73
|
+
export declare type SignMessagesResult = {
|
|
74
|
+
signatures: string[];
|
|
88
75
|
};
|
|
89
76
|
export declare type CreateTransactionOptions = {
|
|
90
77
|
receiverId: string;
|
|
@@ -94,7 +81,7 @@ export declare type CreateTransactionOptions = {
|
|
|
94
81
|
export declare type SignAndSendTransactionOptions = {
|
|
95
82
|
receiverId: string;
|
|
96
83
|
actions: NearTransactionAction[];
|
|
97
|
-
meta?:
|
|
84
|
+
meta?: CommonOptionsMeta;
|
|
98
85
|
callbackUrl?: string;
|
|
99
86
|
};
|
|
100
87
|
declare function serializeTransaction({ transaction }: {
|
|
@@ -140,7 +127,7 @@ declare class OneKeyNearProvider extends ProviderNearBase {
|
|
|
140
127
|
_isInstalled: boolean;
|
|
141
128
|
_isInstalledDetected: boolean;
|
|
142
129
|
_isUnlocked: boolean;
|
|
143
|
-
constructor({ connection, networkId, enablePageReload, connectEagerly, timeout, logger, keyPrefix, transactionCreator, bridge, shouldSendMetadata, maxEventListeners, }
|
|
130
|
+
constructor({ connection, networkId, enablePageReload, connectEagerly, timeout, logger, keyPrefix, transactionCreator, bridge, shouldSendMetadata, maxEventListeners, }?: OneKeyNearWalletProps);
|
|
144
131
|
_initializedEmitted: boolean;
|
|
145
132
|
detectWalletInstalled(): Promise<boolean>;
|
|
146
133
|
_registerEvents(): void;
|
|
@@ -166,15 +153,14 @@ declare class OneKeyNearProvider extends ProviderNearBase {
|
|
|
166
153
|
getAccountId(): string;
|
|
167
154
|
getPublicKey(): string;
|
|
168
155
|
getAccountInfo(): NearAccountInfo;
|
|
169
|
-
|
|
170
|
-
getSelectedNetwork(): NearNetworkChangedPayload;
|
|
156
|
+
getNetworkInfo(): NearNetworkInfo;
|
|
171
157
|
_saveAuthData(data: NearAccountInfo): void;
|
|
172
|
-
requestSignIn(signInOptions?: SignInOptions): Promise<
|
|
173
|
-
requestSignTransactions(signTransactionsOptions: SignTransactionsOptions): Promise<
|
|
174
|
-
requestSignMessages({ messages, meta }: SignMessagesOptions): Promise<
|
|
158
|
+
requestSignIn(signInOptions?: SignInOptions): Promise<SignInResult>;
|
|
159
|
+
requestSignTransactions(signTransactionsOptions: SignTransactionsOptions): Promise<SignTransactionsResult>;
|
|
160
|
+
requestSignMessages({ messages, meta, }: SignMessagesOptions): Promise<SignMessagesResult>;
|
|
175
161
|
request({ method, params }?: IJsonRpcRequest): Promise<unknown>;
|
|
176
|
-
sendJsonRpc(method: string, params: object): Promise<
|
|
177
|
-
createTransaction({ receiverId, actions, nonceOffset }: CreateTransactionOptions): Promise<
|
|
162
|
+
sendJsonRpc(method: string, params: object): Promise<unknown>;
|
|
163
|
+
createTransaction({ receiverId, actions, nonceOffset }: CreateTransactionOptions): Promise<transactions.Transaction>;
|
|
178
164
|
_clearAuthData(): void;
|
|
179
165
|
signOut(): void;
|
|
180
166
|
account(): OneKeyWalletAccount;
|
|
@@ -182,7 +168,8 @@ declare class OneKeyNearProvider extends ProviderNearBase {
|
|
|
182
168
|
declare class OneKeyWalletAccount extends Account {
|
|
183
169
|
_wallet: OneKeyNearProvider;
|
|
184
170
|
constructor({ wallet, connection, accountId }: OneKeyWalletAccountProps);
|
|
185
|
-
signAndSendTransaction(signAndSendTransactionOptions: SignAndSendTransactionOptions): Promise<
|
|
171
|
+
signAndSendTransaction(signAndSendTransactionOptions: SignAndSendTransactionOptions): Promise<FinalExecutionOutcome>;
|
|
172
|
+
getAccessKeys(): Promise<AccessKeyInfoView[]>;
|
|
186
173
|
_fetchAccountAccessKey({ publicKey, accountId }: {
|
|
187
174
|
publicKey: string;
|
|
188
175
|
accountId: string;
|
|
@@ -191,6 +178,6 @@ declare class OneKeyWalletAccount extends Account {
|
|
|
191
178
|
publicKey: string;
|
|
192
179
|
accountId: string;
|
|
193
180
|
}>;
|
|
194
|
-
createTransaction({ receiverId, actions, nonceOffset }: CreateTransactionOptions): Promise<NearTransaction>;
|
|
181
|
+
createTransaction({ receiverId, actions, nonceOffset, }: CreateTransactionOptions): Promise<NearTransaction>;
|
|
195
182
|
}
|
|
196
183
|
export { OneKeyNearProvider, OneKeyWalletAccount, serializeTransaction };
|
|
@@ -8,19 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import depd from 'depd';
|
|
11
|
-
import {
|
|
11
|
+
import { getOrCreateExtInjectedJsBridge } from '@onekeyfe/extension-bridge-injected';
|
|
12
|
+
import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
|
|
12
13
|
import entries from 'lodash/entries';
|
|
13
14
|
import isString from 'lodash/isString';
|
|
14
|
-
import { baseDecode } from 'borsh';
|
|
15
|
+
import { baseEncode, baseDecode } from 'borsh';
|
|
15
16
|
import { Account, utils, transactions } from 'near-api-js';
|
|
16
|
-
import {
|
|
17
|
-
import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
|
|
17
|
+
import { ProviderNearBase } from './ProviderNearBase';
|
|
18
18
|
function serializeTransaction({ transaction }) {
|
|
19
19
|
if (isString(transaction)) {
|
|
20
20
|
return transaction;
|
|
21
21
|
}
|
|
22
22
|
const message = transaction.encode();
|
|
23
|
-
|
|
23
|
+
// const hash = new Uint8Array(sha256.sha256.array(message));
|
|
24
|
+
if (typeof Buffer !== 'undefined' && Buffer.from) {
|
|
25
|
+
return Buffer.from(message).toString('base64');
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
return message.toString('base64');
|
|
24
30
|
}
|
|
25
31
|
const DEFAULT_AUTH_DATA = {
|
|
26
32
|
accountId: '',
|
|
@@ -33,14 +39,16 @@ const DEFAULT_NETWORK_INFO = {
|
|
|
33
39
|
};
|
|
34
40
|
const PROVIDER_METHODS = {
|
|
35
41
|
near_accounts: 'near_accounts',
|
|
42
|
+
near_network: 'near_network',
|
|
36
43
|
near_networkInfo: 'near_networkInfo',
|
|
44
|
+
near_requestAccounts: 'near_requestAccounts',
|
|
37
45
|
near_requestSignIn: 'near_requestSignIn',
|
|
38
46
|
near_signOut: 'near_signOut',
|
|
39
47
|
near_requestSignTransactions: 'near_requestSignTransactions',
|
|
40
48
|
near_sendTransactions: 'near_sendTransactions',
|
|
41
49
|
near_signTransactions: 'near_signTransactions',
|
|
42
|
-
near_requestSignMessages: 'near_requestSignMessages',
|
|
43
50
|
near_signMessages: 'near_signMessages',
|
|
51
|
+
near_requestSignMessages: 'near_requestSignMessages',
|
|
44
52
|
};
|
|
45
53
|
const PROVIDER_EVENTS = {
|
|
46
54
|
accountsChanged: 'accountsChanged',
|
|
@@ -66,7 +74,7 @@ function defaultTransactionCreator({ accountId, publicKey, receiverId, nonce, ac
|
|
|
66
74
|
// @ts-ignore
|
|
67
75
|
class OneKeyNearProvider extends ProviderNearBase {
|
|
68
76
|
// TODO package.json version (process.env.npm_package_version)
|
|
69
|
-
constructor({ connection, networkId, enablePageReload, connectEagerly =
|
|
77
|
+
constructor({ connection, networkId, enablePageReload, connectEagerly = true, timeout, logger, keyPrefix = '', transactionCreator, bridge, shouldSendMetadata = true, maxEventListeners, } = {}) {
|
|
70
78
|
super({
|
|
71
79
|
bridge: bridge || getOrCreateExtInjectedJsBridge({ timeout }),
|
|
72
80
|
logger,
|
|
@@ -74,7 +82,7 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
74
82
|
maxEventListeners,
|
|
75
83
|
});
|
|
76
84
|
this._enablePageReload = false;
|
|
77
|
-
this._connectEagerly =
|
|
85
|
+
this._connectEagerly = true;
|
|
78
86
|
this._authData = DEFAULT_AUTH_DATA;
|
|
79
87
|
this._authDataKey = '@OneKeyNearWalletAuthData';
|
|
80
88
|
this._networkId = '';
|
|
@@ -84,13 +92,13 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
84
92
|
this._isUnlocked = false;
|
|
85
93
|
this._initializedEmitted = false;
|
|
86
94
|
if (!networkId) {
|
|
87
|
-
throw new Error('OneKeyNearWallet init error: networkId required.');
|
|
95
|
+
// throw new Error('OneKeyNearWallet init error: networkId required.');
|
|
88
96
|
}
|
|
89
97
|
this._authDataKey = keyPrefix + this._authDataKey;
|
|
90
98
|
this._enablePageReload = enablePageReload;
|
|
91
99
|
this._connectEagerly = connectEagerly;
|
|
92
100
|
this._connection = connection;
|
|
93
|
-
this._networkId = networkId;
|
|
101
|
+
this._networkId = networkId || '';
|
|
94
102
|
this._transactionCreator = transactionCreator || defaultTransactionCreator;
|
|
95
103
|
this._initAuthDataFromStorage();
|
|
96
104
|
this._registerEvents();
|
|
@@ -209,6 +217,7 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
209
217
|
_handleUnlockStateChanged(payload) {
|
|
210
218
|
const isUnlocked = payload === null || payload === void 0 ? void 0 : payload.isUnlocked;
|
|
211
219
|
if (typeof isUnlocked !== 'boolean') {
|
|
220
|
+
// TODO log same error only once
|
|
212
221
|
console.error('Received invalid isUnlocked parameter. Please report this bug.');
|
|
213
222
|
return;
|
|
214
223
|
}
|
|
@@ -276,8 +285,8 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
276
285
|
return this.bridgeRequest(Object.assign(Object.assign({}, payload), { requestInfo: {
|
|
277
286
|
accountId: this.getAccountId(),
|
|
278
287
|
publicKey: this.getPublicKey(),
|
|
279
|
-
networkId: this.
|
|
280
|
-
selectedNetworkId: ((_a = this.
|
|
288
|
+
networkId: this._networkId,
|
|
289
|
+
selectedNetworkId: ((_a = this.getNetworkInfo()) === null || _a === void 0 ? void 0 : _a.networkId) || '',
|
|
281
290
|
} }));
|
|
282
291
|
}
|
|
283
292
|
isInstalled() {
|
|
@@ -300,11 +309,8 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
300
309
|
getAccountInfo() {
|
|
301
310
|
return this._authData || DEFAULT_AUTH_DATA;
|
|
302
311
|
}
|
|
303
|
-
|
|
304
|
-
return this.
|
|
305
|
-
}
|
|
306
|
-
getSelectedNetwork() {
|
|
307
|
-
return this._selectedNetwork;
|
|
312
|
+
getNetworkInfo() {
|
|
313
|
+
return this._selectedNetwork || DEFAULT_NETWORK_INFO;
|
|
308
314
|
}
|
|
309
315
|
_saveAuthData(data) {
|
|
310
316
|
localStorage.setItem(this._authDataKey, JSON.stringify(data));
|
|
@@ -394,7 +400,7 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
394
400
|
return res;
|
|
395
401
|
});
|
|
396
402
|
}
|
|
397
|
-
requestSignMessages({ messages = [], meta = {} }) {
|
|
403
|
+
requestSignMessages({ messages = [], meta = {}, }) {
|
|
398
404
|
return __awaiter(this, void 0, void 0, function* () {
|
|
399
405
|
const res = yield this._callBridgeRequest({
|
|
400
406
|
method: PROVIDER_METHODS.near_requestSignMessages,
|
|
@@ -408,7 +414,11 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
408
414
|
return __awaiter(this, void 0, void 0, function* () {
|
|
409
415
|
const paramsArr = [].concat(params);
|
|
410
416
|
const paramObj = paramsArr[0];
|
|
411
|
-
if (method === PROVIDER_METHODS.
|
|
417
|
+
if (method === PROVIDER_METHODS.near_network) {
|
|
418
|
+
method = PROVIDER_METHODS.near_networkInfo;
|
|
419
|
+
}
|
|
420
|
+
if (method === PROVIDER_METHODS.near_requestAccounts ||
|
|
421
|
+
method === PROVIDER_METHODS.near_requestSignIn) {
|
|
412
422
|
return this.requestSignIn(paramObj);
|
|
413
423
|
}
|
|
414
424
|
if (method === PROVIDER_METHODS.near_sendTransactions ||
|
|
@@ -424,6 +434,9 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
424
434
|
method === PROVIDER_METHODS.near_requestSignMessages) {
|
|
425
435
|
return this.requestSignMessages(paramObj);
|
|
426
436
|
}
|
|
437
|
+
if (method === PROVIDER_METHODS.near_signOut) {
|
|
438
|
+
return this.signOut();
|
|
439
|
+
}
|
|
427
440
|
return yield this._callBridgeRequest({
|
|
428
441
|
method,
|
|
429
442
|
params,
|
|
@@ -435,7 +448,6 @@ class OneKeyNearProvider extends ProviderNearBase {
|
|
|
435
448
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return
|
|
436
449
|
return provider.sendJsonRpc(method, params);
|
|
437
450
|
}
|
|
438
|
-
// SpecialWallet required
|
|
439
451
|
createTransaction({ receiverId, actions, nonceOffset = 1 }) {
|
|
440
452
|
return this.account().createTransaction({
|
|
441
453
|
receiverId,
|
|
@@ -474,9 +486,12 @@ class OneKeyWalletAccount extends Account {
|
|
|
474
486
|
super(connection, accountId);
|
|
475
487
|
this._wallet = wallet;
|
|
476
488
|
}
|
|
489
|
+
// TODO
|
|
490
|
+
// state()
|
|
477
491
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
478
492
|
// @ts-ignore
|
|
479
493
|
signAndSendTransaction(signAndSendTransactionOptions) {
|
|
494
|
+
var _a;
|
|
480
495
|
return __awaiter(this, arguments, void 0, function* () {
|
|
481
496
|
let options = signAndSendTransactionOptions;
|
|
482
497
|
// eslint-disable-next-line prefer-rest-params
|
|
@@ -487,6 +502,7 @@ class OneKeyWalletAccount extends Account {
|
|
|
487
502
|
actions: args[1],
|
|
488
503
|
};
|
|
489
504
|
}
|
|
505
|
+
// TODO walletMeta, walletCallbackUrl
|
|
490
506
|
const { receiverId, actions, meta, callbackUrl } = options;
|
|
491
507
|
const transaction = yield this.createTransaction({
|
|
492
508
|
receiverId,
|
|
@@ -494,12 +510,44 @@ class OneKeyWalletAccount extends Account {
|
|
|
494
510
|
});
|
|
495
511
|
const txHashList = yield this._wallet.requestSignTransactions({
|
|
496
512
|
transactions: [transaction],
|
|
497
|
-
meta
|
|
513
|
+
meta,
|
|
498
514
|
callbackUrl,
|
|
499
515
|
});
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
516
|
+
const txHash = (_a = txHashList === null || txHashList === void 0 ? void 0 : txHashList.transactionHashes) === null || _a === void 0 ? void 0 : _a[0];
|
|
517
|
+
if (txHash) {
|
|
518
|
+
// near-api-js/lib/providers/json-rpc-provider.js
|
|
519
|
+
// async txStatus(txHash, accountId)
|
|
520
|
+
const txHashStr = typeof txHash === 'string' ? txHash : baseEncode(txHash);
|
|
521
|
+
const res = (yield this._wallet.request({
|
|
522
|
+
method: 'tx',
|
|
523
|
+
params: [txHashStr, this.accountId],
|
|
524
|
+
}));
|
|
525
|
+
return res;
|
|
526
|
+
}
|
|
527
|
+
throw web3Errors.rpc.internal({
|
|
528
|
+
message: 'Transaction sign and send failed: transactionHash not found',
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
getAccessKeys() {
|
|
533
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
534
|
+
// near-api-js/lib/account.js
|
|
535
|
+
// async getAccessKeys() { ... }
|
|
536
|
+
const response = yield this._wallet.request({
|
|
537
|
+
method: 'query',
|
|
538
|
+
params: {
|
|
539
|
+
request_type: 'view_access_key_list',
|
|
540
|
+
account_id: this.accountId,
|
|
541
|
+
finality: 'optimistic',
|
|
542
|
+
},
|
|
543
|
+
});
|
|
544
|
+
// A breaking API change introduced extra information into the
|
|
545
|
+
// response, so it now returns an object with a `keys` field instead
|
|
546
|
+
// of an array: https://github.com/nearprotocol/nearcore/pull/1789
|
|
547
|
+
if (Array.isArray(response)) {
|
|
548
|
+
return response;
|
|
549
|
+
}
|
|
550
|
+
return response.keys;
|
|
503
551
|
});
|
|
504
552
|
}
|
|
505
553
|
_fetchAccountAccessKey({ publicKey, accountId }) {
|
|
@@ -517,7 +565,7 @@ class OneKeyWalletAccount extends Account {
|
|
|
517
565
|
throw new Error(`near account access key not found for: ${accountId}`);
|
|
518
566
|
});
|
|
519
567
|
}
|
|
520
|
-
createTransaction({ receiverId, actions, nonceOffset = 1 }) {
|
|
568
|
+
createTransaction({ receiverId, actions, nonceOffset = 1, }) {
|
|
521
569
|
return __awaiter(this, void 0, void 0, function* () {
|
|
522
570
|
const _authData = this._wallet._authData;
|
|
523
571
|
const publicKey = _authData.publicKey;
|
|
@@ -531,7 +579,12 @@ class OneKeyWalletAccount extends Account {
|
|
|
531
579
|
publicKey,
|
|
532
580
|
});
|
|
533
581
|
const nonce = accessKeyInfo.accessKey.nonce + nonceOffset;
|
|
534
|
-
|
|
582
|
+
// near-api-js/lib/providers/json-rpc-provider.js
|
|
583
|
+
// async block(blockQuery) {...}
|
|
584
|
+
const block = (yield this._wallet.request({
|
|
585
|
+
method: 'block',
|
|
586
|
+
params: { block_id: undefined, finality: 'final' },
|
|
587
|
+
}));
|
|
535
588
|
const blockHash = baseDecode(block.header.hash);
|
|
536
589
|
const transaction = this._wallet._transactionCreator({
|
|
537
590
|
accountId,
|
|
@@ -14,19 +14,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.serializeTransaction = exports.OneKeyWalletAccount = exports.OneKeyNearProvider = void 0;
|
|
16
16
|
const depd_1 = __importDefault(require("depd"));
|
|
17
|
-
const
|
|
17
|
+
const extension_bridge_injected_1 = require("@onekeyfe/extension-bridge-injected");
|
|
18
|
+
const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
|
|
18
19
|
const entries_1 = __importDefault(require("lodash/entries"));
|
|
19
20
|
const isString_1 = __importDefault(require("lodash/isString"));
|
|
20
21
|
const borsh_1 = require("borsh");
|
|
21
22
|
const near_api_js_1 = require("near-api-js");
|
|
22
|
-
const
|
|
23
|
-
const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
|
|
23
|
+
const ProviderNearBase_1 = require("./ProviderNearBase");
|
|
24
24
|
function serializeTransaction({ transaction }) {
|
|
25
25
|
if ((0, isString_1.default)(transaction)) {
|
|
26
26
|
return transaction;
|
|
27
27
|
}
|
|
28
28
|
const message = transaction.encode();
|
|
29
|
-
|
|
29
|
+
// const hash = new Uint8Array(sha256.sha256.array(message));
|
|
30
|
+
if (typeof Buffer !== 'undefined' && Buffer.from) {
|
|
31
|
+
return Buffer.from(message).toString('base64');
|
|
32
|
+
}
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
return message.toString('base64');
|
|
30
36
|
}
|
|
31
37
|
exports.serializeTransaction = serializeTransaction;
|
|
32
38
|
const DEFAULT_AUTH_DATA = {
|
|
@@ -40,14 +46,16 @@ const DEFAULT_NETWORK_INFO = {
|
|
|
40
46
|
};
|
|
41
47
|
const PROVIDER_METHODS = {
|
|
42
48
|
near_accounts: 'near_accounts',
|
|
49
|
+
near_network: 'near_network',
|
|
43
50
|
near_networkInfo: 'near_networkInfo',
|
|
51
|
+
near_requestAccounts: 'near_requestAccounts',
|
|
44
52
|
near_requestSignIn: 'near_requestSignIn',
|
|
45
53
|
near_signOut: 'near_signOut',
|
|
46
54
|
near_requestSignTransactions: 'near_requestSignTransactions',
|
|
47
55
|
near_sendTransactions: 'near_sendTransactions',
|
|
48
56
|
near_signTransactions: 'near_signTransactions',
|
|
49
|
-
near_requestSignMessages: 'near_requestSignMessages',
|
|
50
57
|
near_signMessages: 'near_signMessages',
|
|
58
|
+
near_requestSignMessages: 'near_requestSignMessages',
|
|
51
59
|
};
|
|
52
60
|
const PROVIDER_EVENTS = {
|
|
53
61
|
accountsChanged: 'accountsChanged',
|
|
@@ -73,7 +81,7 @@ function defaultTransactionCreator({ accountId, publicKey, receiverId, nonce, ac
|
|
|
73
81
|
// @ts-ignore
|
|
74
82
|
class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
75
83
|
// TODO package.json version (process.env.npm_package_version)
|
|
76
|
-
constructor({ connection, networkId, enablePageReload, connectEagerly =
|
|
84
|
+
constructor({ connection, networkId, enablePageReload, connectEagerly = true, timeout, logger, keyPrefix = '', transactionCreator, bridge, shouldSendMetadata = true, maxEventListeners, } = {}) {
|
|
77
85
|
super({
|
|
78
86
|
bridge: bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout }),
|
|
79
87
|
logger,
|
|
@@ -81,7 +89,7 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
81
89
|
maxEventListeners,
|
|
82
90
|
});
|
|
83
91
|
this._enablePageReload = false;
|
|
84
|
-
this._connectEagerly =
|
|
92
|
+
this._connectEagerly = true;
|
|
85
93
|
this._authData = DEFAULT_AUTH_DATA;
|
|
86
94
|
this._authDataKey = '@OneKeyNearWalletAuthData';
|
|
87
95
|
this._networkId = '';
|
|
@@ -91,13 +99,13 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
91
99
|
this._isUnlocked = false;
|
|
92
100
|
this._initializedEmitted = false;
|
|
93
101
|
if (!networkId) {
|
|
94
|
-
throw new Error('OneKeyNearWallet init error: networkId required.');
|
|
102
|
+
// throw new Error('OneKeyNearWallet init error: networkId required.');
|
|
95
103
|
}
|
|
96
104
|
this._authDataKey = keyPrefix + this._authDataKey;
|
|
97
105
|
this._enablePageReload = enablePageReload;
|
|
98
106
|
this._connectEagerly = connectEagerly;
|
|
99
107
|
this._connection = connection;
|
|
100
|
-
this._networkId = networkId;
|
|
108
|
+
this._networkId = networkId || '';
|
|
101
109
|
this._transactionCreator = transactionCreator || defaultTransactionCreator;
|
|
102
110
|
this._initAuthDataFromStorage();
|
|
103
111
|
this._registerEvents();
|
|
@@ -216,6 +224,7 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
216
224
|
_handleUnlockStateChanged(payload) {
|
|
217
225
|
const isUnlocked = payload === null || payload === void 0 ? void 0 : payload.isUnlocked;
|
|
218
226
|
if (typeof isUnlocked !== 'boolean') {
|
|
227
|
+
// TODO log same error only once
|
|
219
228
|
console.error('Received invalid isUnlocked parameter. Please report this bug.');
|
|
220
229
|
return;
|
|
221
230
|
}
|
|
@@ -283,8 +292,8 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
283
292
|
return this.bridgeRequest(Object.assign(Object.assign({}, payload), { requestInfo: {
|
|
284
293
|
accountId: this.getAccountId(),
|
|
285
294
|
publicKey: this.getPublicKey(),
|
|
286
|
-
networkId: this.
|
|
287
|
-
selectedNetworkId: ((_a = this.
|
|
295
|
+
networkId: this._networkId,
|
|
296
|
+
selectedNetworkId: ((_a = this.getNetworkInfo()) === null || _a === void 0 ? void 0 : _a.networkId) || '',
|
|
288
297
|
} }));
|
|
289
298
|
}
|
|
290
299
|
isInstalled() {
|
|
@@ -307,11 +316,8 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
307
316
|
getAccountInfo() {
|
|
308
317
|
return this._authData || DEFAULT_AUTH_DATA;
|
|
309
318
|
}
|
|
310
|
-
|
|
311
|
-
return this.
|
|
312
|
-
}
|
|
313
|
-
getSelectedNetwork() {
|
|
314
|
-
return this._selectedNetwork;
|
|
319
|
+
getNetworkInfo() {
|
|
320
|
+
return this._selectedNetwork || DEFAULT_NETWORK_INFO;
|
|
315
321
|
}
|
|
316
322
|
_saveAuthData(data) {
|
|
317
323
|
localStorage.setItem(this._authDataKey, JSON.stringify(data));
|
|
@@ -401,7 +407,7 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
401
407
|
return res;
|
|
402
408
|
});
|
|
403
409
|
}
|
|
404
|
-
requestSignMessages({ messages = [], meta = {} }) {
|
|
410
|
+
requestSignMessages({ messages = [], meta = {}, }) {
|
|
405
411
|
return __awaiter(this, void 0, void 0, function* () {
|
|
406
412
|
const res = yield this._callBridgeRequest({
|
|
407
413
|
method: PROVIDER_METHODS.near_requestSignMessages,
|
|
@@ -415,7 +421,11 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
415
421
|
return __awaiter(this, void 0, void 0, function* () {
|
|
416
422
|
const paramsArr = [].concat(params);
|
|
417
423
|
const paramObj = paramsArr[0];
|
|
418
|
-
if (method === PROVIDER_METHODS.
|
|
424
|
+
if (method === PROVIDER_METHODS.near_network) {
|
|
425
|
+
method = PROVIDER_METHODS.near_networkInfo;
|
|
426
|
+
}
|
|
427
|
+
if (method === PROVIDER_METHODS.near_requestAccounts ||
|
|
428
|
+
method === PROVIDER_METHODS.near_requestSignIn) {
|
|
419
429
|
return this.requestSignIn(paramObj);
|
|
420
430
|
}
|
|
421
431
|
if (method === PROVIDER_METHODS.near_sendTransactions ||
|
|
@@ -431,6 +441,9 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
431
441
|
method === PROVIDER_METHODS.near_requestSignMessages) {
|
|
432
442
|
return this.requestSignMessages(paramObj);
|
|
433
443
|
}
|
|
444
|
+
if (method === PROVIDER_METHODS.near_signOut) {
|
|
445
|
+
return this.signOut();
|
|
446
|
+
}
|
|
434
447
|
return yield this._callBridgeRequest({
|
|
435
448
|
method,
|
|
436
449
|
params,
|
|
@@ -442,7 +455,6 @@ class OneKeyNearProvider extends ProviderNearBase_1.ProviderNearBase {
|
|
|
442
455
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return
|
|
443
456
|
return provider.sendJsonRpc(method, params);
|
|
444
457
|
}
|
|
445
|
-
// SpecialWallet required
|
|
446
458
|
createTransaction({ receiverId, actions, nonceOffset = 1 }) {
|
|
447
459
|
return this.account().createTransaction({
|
|
448
460
|
receiverId,
|
|
@@ -482,9 +494,12 @@ class OneKeyWalletAccount extends near_api_js_1.Account {
|
|
|
482
494
|
super(connection, accountId);
|
|
483
495
|
this._wallet = wallet;
|
|
484
496
|
}
|
|
497
|
+
// TODO
|
|
498
|
+
// state()
|
|
485
499
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
486
500
|
// @ts-ignore
|
|
487
501
|
signAndSendTransaction(signAndSendTransactionOptions) {
|
|
502
|
+
var _a;
|
|
488
503
|
return __awaiter(this, arguments, void 0, function* () {
|
|
489
504
|
let options = signAndSendTransactionOptions;
|
|
490
505
|
// eslint-disable-next-line prefer-rest-params
|
|
@@ -495,6 +510,7 @@ class OneKeyWalletAccount extends near_api_js_1.Account {
|
|
|
495
510
|
actions: args[1],
|
|
496
511
|
};
|
|
497
512
|
}
|
|
513
|
+
// TODO walletMeta, walletCallbackUrl
|
|
498
514
|
const { receiverId, actions, meta, callbackUrl } = options;
|
|
499
515
|
const transaction = yield this.createTransaction({
|
|
500
516
|
receiverId,
|
|
@@ -502,12 +518,44 @@ class OneKeyWalletAccount extends near_api_js_1.Account {
|
|
|
502
518
|
});
|
|
503
519
|
const txHashList = yield this._wallet.requestSignTransactions({
|
|
504
520
|
transactions: [transaction],
|
|
505
|
-
meta
|
|
521
|
+
meta,
|
|
506
522
|
callbackUrl,
|
|
507
523
|
});
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
524
|
+
const txHash = (_a = txHashList === null || txHashList === void 0 ? void 0 : txHashList.transactionHashes) === null || _a === void 0 ? void 0 : _a[0];
|
|
525
|
+
if (txHash) {
|
|
526
|
+
// near-api-js/lib/providers/json-rpc-provider.js
|
|
527
|
+
// async txStatus(txHash, accountId)
|
|
528
|
+
const txHashStr = typeof txHash === 'string' ? txHash : (0, borsh_1.baseEncode)(txHash);
|
|
529
|
+
const res = (yield this._wallet.request({
|
|
530
|
+
method: 'tx',
|
|
531
|
+
params: [txHashStr, this.accountId],
|
|
532
|
+
}));
|
|
533
|
+
return res;
|
|
534
|
+
}
|
|
535
|
+
throw cross_inpage_provider_errors_1.web3Errors.rpc.internal({
|
|
536
|
+
message: 'Transaction sign and send failed: transactionHash not found',
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
getAccessKeys() {
|
|
541
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
542
|
+
// near-api-js/lib/account.js
|
|
543
|
+
// async getAccessKeys() { ... }
|
|
544
|
+
const response = yield this._wallet.request({
|
|
545
|
+
method: 'query',
|
|
546
|
+
params: {
|
|
547
|
+
request_type: 'view_access_key_list',
|
|
548
|
+
account_id: this.accountId,
|
|
549
|
+
finality: 'optimistic',
|
|
550
|
+
},
|
|
551
|
+
});
|
|
552
|
+
// A breaking API change introduced extra information into the
|
|
553
|
+
// response, so it now returns an object with a `keys` field instead
|
|
554
|
+
// of an array: https://github.com/nearprotocol/nearcore/pull/1789
|
|
555
|
+
if (Array.isArray(response)) {
|
|
556
|
+
return response;
|
|
557
|
+
}
|
|
558
|
+
return response.keys;
|
|
511
559
|
});
|
|
512
560
|
}
|
|
513
561
|
_fetchAccountAccessKey({ publicKey, accountId }) {
|
|
@@ -525,7 +573,7 @@ class OneKeyWalletAccount extends near_api_js_1.Account {
|
|
|
525
573
|
throw new Error(`near account access key not found for: ${accountId}`);
|
|
526
574
|
});
|
|
527
575
|
}
|
|
528
|
-
createTransaction({ receiverId, actions, nonceOffset = 1 }) {
|
|
576
|
+
createTransaction({ receiverId, actions, nonceOffset = 1, }) {
|
|
529
577
|
return __awaiter(this, void 0, void 0, function* () {
|
|
530
578
|
const _authData = this._wallet._authData;
|
|
531
579
|
const publicKey = _authData.publicKey;
|
|
@@ -539,7 +587,12 @@ class OneKeyWalletAccount extends near_api_js_1.Account {
|
|
|
539
587
|
publicKey,
|
|
540
588
|
});
|
|
541
589
|
const nonce = accessKeyInfo.accessKey.nonce + nonceOffset;
|
|
542
|
-
|
|
590
|
+
// near-api-js/lib/providers/json-rpc-provider.js
|
|
591
|
+
// async block(blockQuery) {...}
|
|
592
|
+
const block = (yield this._wallet.request({
|
|
593
|
+
method: 'block',
|
|
594
|
+
params: { block_id: undefined, finality: 'final' },
|
|
595
|
+
}));
|
|
543
596
|
const blockHash = (0, borsh_1.baseDecode)(block.header.hash);
|
|
544
597
|
const transaction = this._wallet._transactionCreator({
|
|
545
598
|
accountId,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/onekey-near-provider",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7-alpha.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"cross-inpage-provider"
|
|
6
6
|
],
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
"near-api-js": "^0.44.2"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@onekeyfe/cross-inpage-provider-core": "^0.0.
|
|
39
|
-
"@onekeyfe/cross-inpage-provider-errors": "^0.0.
|
|
40
|
-
"@onekeyfe/cross-inpage-provider-types": "^0.0.
|
|
41
|
-
"@onekeyfe/extension-bridge-injected": "^0.0.
|
|
38
|
+
"@onekeyfe/cross-inpage-provider-core": "^0.0.7-alpha.0",
|
|
39
|
+
"@onekeyfe/cross-inpage-provider-errors": "^0.0.7-alpha.0",
|
|
40
|
+
"@onekeyfe/cross-inpage-provider-types": "^0.0.7-alpha.0",
|
|
41
|
+
"@onekeyfe/extension-bridge-injected": "^0.0.7-alpha.0",
|
|
42
42
|
"borsh": "^0.6.0",
|
|
43
43
|
"depd": "^2.0.0",
|
|
44
44
|
"tweetnacl": "^1.0.3"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "e163439202ef5f85319d7dc9bc7c18ee7d6a0b7b"
|
|
47
47
|
}
|