@onekeyfe/onekey-scdo-provider 2.1.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/LICENSE.md +51 -0
- package/README.md +1 -0
- package/dist/OnekeyScdoProvider.d.ts +109 -0
- package/dist/OnekeyScdoProvider.js +131 -0
- package/dist/ProviderScdoBase.d.ts +8 -0
- package/dist/ProviderScdoBase.js +12 -0
- package/dist/cjs/OnekeyScdoProvider.js +135 -0
- package/dist/cjs/ProviderScdoBase.js +15 -0
- package/dist/cjs/index.js +17 -0
- package/dist/cjs/type-utils.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/type-utils.d.ts +4 -0
- package/dist/type-utils.js +1 -0
- package/package.json +37 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# ONEKEY STANDARD SOURCE LICENSE (O-SSL)
|
|
2
|
+
|
|
3
|
+
This license governs use of the accompanying software. If you use the software,
|
|
4
|
+
you accept this license. If you do not accept the license, do not use the
|
|
5
|
+
software.
|
|
6
|
+
|
|
7
|
+
## 1. Definitions
|
|
8
|
+
|
|
9
|
+
The terms "reproduce," "reproduction" and "distribution" have the same meaning
|
|
10
|
+
here as under Hong Kong copyright law.
|
|
11
|
+
|
|
12
|
+
"You" means the licensee of the software.
|
|
13
|
+
|
|
14
|
+
"Your company" means the company you worked for when you downloaded the
|
|
15
|
+
software.
|
|
16
|
+
|
|
17
|
+
"Reference use" means use of the software within your company as a reference,
|
|
18
|
+
in read only form, for the sole purposes of debugging your products,
|
|
19
|
+
maintaining your products, or enhancing the interoperability of your products
|
|
20
|
+
with the software, and specifically excludes the right to distribute the
|
|
21
|
+
software outside of your company.
|
|
22
|
+
|
|
23
|
+
"Licensed patents" means any Licensor patent claims which read directly on the
|
|
24
|
+
software as distributed by the Licensor under this license.
|
|
25
|
+
|
|
26
|
+
## 2. Grant of Rights
|
|
27
|
+
|
|
28
|
+
(A) Copyright Grant - Subject to the terms of this license, the Licensor grants
|
|
29
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free copyright
|
|
30
|
+
license to reproduce the software for reference use.
|
|
31
|
+
|
|
32
|
+
(B) Patent Grant - Subject to the terms of this license, the Licensor grants
|
|
33
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free patent license
|
|
34
|
+
under licensed patents for reference use.
|
|
35
|
+
|
|
36
|
+
## 3. Limitations
|
|
37
|
+
|
|
38
|
+
(A) No Trademark License - This license does not grant you any rights to use
|
|
39
|
+
the Licensor's name, logo, or trademarks.
|
|
40
|
+
|
|
41
|
+
(B) If you begin patent litigation against the Licensor over patents that you
|
|
42
|
+
think may apply to the software (including a cross-claim or counterclaim in
|
|
43
|
+
a lawsuit), your license to the software ends automatically.
|
|
44
|
+
|
|
45
|
+
(C) The software is licensed "as-is." You bear the risk of using it. The
|
|
46
|
+
Licensor gives no express warranties, guarantees or conditions. You may have
|
|
47
|
+
additional consumer rights under your local laws which this license cannot
|
|
48
|
+
change. To the extent permitted under your local laws, the Licensor excludes
|
|
49
|
+
the implied warranties of merchantability, fitness for a particular purpose and
|
|
50
|
+
non-infringement.This license agreement is governed by the laws of Hong Kong,
|
|
51
|
+
and any disputes related to this license agreement shall be resolved in accordance with Hong Kong law.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# cross-inpage-provider
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
|
|
2
|
+
import { ProviderScdoBase } from './ProviderScdoBase';
|
|
3
|
+
import type { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';
|
|
4
|
+
export declare enum PROVIDER_EVENTS {
|
|
5
|
+
accountsChanged = "accountsChanged",
|
|
6
|
+
disconnect = "disconnect",
|
|
7
|
+
message_low_level = "message_low_level"
|
|
8
|
+
}
|
|
9
|
+
type ScdoProviderEventsMap = {
|
|
10
|
+
[PROVIDER_EVENTS.accountsChanged]: (accounts: string[]) => void;
|
|
11
|
+
[PROVIDER_EVENTS.disconnect]: () => void;
|
|
12
|
+
[PROVIDER_EVENTS.message_low_level]: (payload: IJsonRpcRequest) => void;
|
|
13
|
+
};
|
|
14
|
+
export declare enum ScdoRequestMethods {
|
|
15
|
+
scdo_requestAccounts = "scdo_requestAccounts",
|
|
16
|
+
scdo_disconnect = "scdo_disconnect",
|
|
17
|
+
scdo_getAccounts = "scdo_getAccounts",
|
|
18
|
+
scdo_getBalance = "scdo_getBalance",
|
|
19
|
+
scdo_signTransaction = "scdo_signTransaction",
|
|
20
|
+
scdo_estimateGas = "scdo_estimateGas",
|
|
21
|
+
scdo_sendTransaction = "scdo_sendTransaction"
|
|
22
|
+
}
|
|
23
|
+
export interface Tx {
|
|
24
|
+
from: string;
|
|
25
|
+
to: string;
|
|
26
|
+
amount?: number;
|
|
27
|
+
accountNonce?: number;
|
|
28
|
+
gasPrice?: number;
|
|
29
|
+
gasLimit?: number;
|
|
30
|
+
timestamp?: number;
|
|
31
|
+
payload?: string;
|
|
32
|
+
hash?: string;
|
|
33
|
+
signature?: {
|
|
34
|
+
Sig: string;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface RawTransaction {
|
|
38
|
+
Type: number;
|
|
39
|
+
From: string;
|
|
40
|
+
To: string;
|
|
41
|
+
Amount: number;
|
|
42
|
+
AccountNonce: number;
|
|
43
|
+
GasPrice: number;
|
|
44
|
+
GasLimit: number;
|
|
45
|
+
Timestamp: number;
|
|
46
|
+
Payload: string;
|
|
47
|
+
}
|
|
48
|
+
export interface SignedTx {
|
|
49
|
+
Data: RawTransaction;
|
|
50
|
+
Hash: string;
|
|
51
|
+
Signature: {
|
|
52
|
+
Sig: string;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
type ScdoRequest = {
|
|
56
|
+
[ScdoRequestMethods.scdo_requestAccounts]: () => string[];
|
|
57
|
+
[ScdoRequestMethods.scdo_disconnect]: () => void;
|
|
58
|
+
[ScdoRequestMethods.scdo_getAccounts]: () => string[];
|
|
59
|
+
[ScdoRequestMethods.scdo_getBalance]: (address: string, blockHash?: string, blockHeight?: number) => string;
|
|
60
|
+
[ScdoRequestMethods.scdo_signTransaction]: (tx: Tx) => SignedTx;
|
|
61
|
+
[ScdoRequestMethods.scdo_estimateGas]: (tx: Tx) => string;
|
|
62
|
+
[ScdoRequestMethods.scdo_sendTransaction]: (tx: Tx) => SignedTx;
|
|
63
|
+
};
|
|
64
|
+
type ScdoRequestParams = Parameters<ScdoRequest[ScdoRequestMethods]>;
|
|
65
|
+
type ScdoRequestResponse = ReturnType<ScdoRequest[ScdoRequestMethods]>;
|
|
66
|
+
interface ScdoRequestProps {
|
|
67
|
+
method: ScdoRequestMethods;
|
|
68
|
+
params: ScdoRequestParams;
|
|
69
|
+
}
|
|
70
|
+
export type IProviderScdo = {
|
|
71
|
+
on: <E extends keyof ScdoProviderEventsMap>(event: E, cb: ScdoProviderEventsMap[E]) => void;
|
|
72
|
+
removeListener: <E extends keyof ScdoProviderEventsMap>(event: E, cb: ScdoProviderEventsMap[E]) => void;
|
|
73
|
+
request: (props: ScdoRequestProps) => Promise<ScdoRequestResponse>;
|
|
74
|
+
};
|
|
75
|
+
export type OneKeyScdoProviderProps = IInpageProviderConfig & {
|
|
76
|
+
timeout?: number;
|
|
77
|
+
};
|
|
78
|
+
export declare class ProviderScdo extends ProviderScdoBase implements IProviderScdo {
|
|
79
|
+
private _account?;
|
|
80
|
+
accounts: string[];
|
|
81
|
+
constructor(props: OneKeyScdoProviderProps);
|
|
82
|
+
private _registerEvents;
|
|
83
|
+
private _callBridge;
|
|
84
|
+
private _handleDisconnected;
|
|
85
|
+
isAccountsChanged(account: string | undefined): boolean;
|
|
86
|
+
private _handleAccountChange;
|
|
87
|
+
on<E extends keyof ScdoProviderEventsMap>(event: E, listener: ScdoProviderEventsMap[E]): this;
|
|
88
|
+
emit<E extends keyof ScdoProviderEventsMap>(event: E, ...args: Parameters<ScdoProviderEventsMap[E]>): boolean;
|
|
89
|
+
removeListener<E extends keyof ScdoProviderEventsMap>(eventName: E, listener: ScdoProviderEventsMap[E]): this;
|
|
90
|
+
private _transformTx;
|
|
91
|
+
request(props: ScdoRequestProps): Promise<string | void | string[] | {
|
|
92
|
+
Data: {
|
|
93
|
+
Type: number;
|
|
94
|
+
From: string;
|
|
95
|
+
To: string;
|
|
96
|
+
Amount: number;
|
|
97
|
+
AccountNonce: number;
|
|
98
|
+
GasPrice: number;
|
|
99
|
+
GasLimit: number;
|
|
100
|
+
Timestamp: number;
|
|
101
|
+
Payload: string;
|
|
102
|
+
};
|
|
103
|
+
Hash: string;
|
|
104
|
+
Signature: {
|
|
105
|
+
Sig: string;
|
|
106
|
+
};
|
|
107
|
+
}>;
|
|
108
|
+
}
|
|
109
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
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 { getOrCreateExtInjectedJsBridge } from '@onekeyfe/extension-bridge-injected';
|
|
11
|
+
import { ProviderScdoBase } from './ProviderScdoBase';
|
|
12
|
+
export var PROVIDER_EVENTS;
|
|
13
|
+
(function (PROVIDER_EVENTS) {
|
|
14
|
+
PROVIDER_EVENTS["accountsChanged"] = "accountsChanged";
|
|
15
|
+
PROVIDER_EVENTS["disconnect"] = "disconnect";
|
|
16
|
+
PROVIDER_EVENTS["message_low_level"] = "message_low_level";
|
|
17
|
+
})(PROVIDER_EVENTS || (PROVIDER_EVENTS = {}));
|
|
18
|
+
export var ScdoRequestMethods;
|
|
19
|
+
(function (ScdoRequestMethods) {
|
|
20
|
+
ScdoRequestMethods["scdo_requestAccounts"] = "scdo_requestAccounts";
|
|
21
|
+
ScdoRequestMethods["scdo_disconnect"] = "scdo_disconnect";
|
|
22
|
+
ScdoRequestMethods["scdo_getAccounts"] = "scdo_getAccounts";
|
|
23
|
+
ScdoRequestMethods["scdo_getBalance"] = "scdo_getBalance";
|
|
24
|
+
ScdoRequestMethods["scdo_signTransaction"] = "scdo_signTransaction";
|
|
25
|
+
ScdoRequestMethods["scdo_estimateGas"] = "scdo_estimateGas";
|
|
26
|
+
ScdoRequestMethods["scdo_sendTransaction"] = "scdo_sendTransaction";
|
|
27
|
+
})(ScdoRequestMethods || (ScdoRequestMethods = {}));
|
|
28
|
+
function isWalletEventMethodMatch({ method, name }) {
|
|
29
|
+
return method === `wallet_events_${name}`;
|
|
30
|
+
}
|
|
31
|
+
export class ProviderScdo extends ProviderScdoBase {
|
|
32
|
+
constructor(props) {
|
|
33
|
+
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
|
|
34
|
+
this.accounts = [];
|
|
35
|
+
this._registerEvents();
|
|
36
|
+
}
|
|
37
|
+
_registerEvents() {
|
|
38
|
+
window.addEventListener('onekey_bridge_disconnect', () => {
|
|
39
|
+
this._handleDisconnected();
|
|
40
|
+
});
|
|
41
|
+
this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
|
|
42
|
+
if (!payload)
|
|
43
|
+
return;
|
|
44
|
+
const { method, params } = payload;
|
|
45
|
+
if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.accountsChanged })) {
|
|
46
|
+
this._handleAccountChange(params);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
_callBridge(params) {
|
|
51
|
+
return this.bridgeRequest(params);
|
|
52
|
+
}
|
|
53
|
+
_handleDisconnected(options = { emit: true }) {
|
|
54
|
+
this._account = undefined;
|
|
55
|
+
if (options.emit && this.isConnectionStatusChanged('disconnected')) {
|
|
56
|
+
this.connectionStatus = 'disconnected';
|
|
57
|
+
this.emit(PROVIDER_EVENTS.disconnect);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
isAccountsChanged(account) {
|
|
61
|
+
if (!account)
|
|
62
|
+
return false;
|
|
63
|
+
if (!this._account)
|
|
64
|
+
return true;
|
|
65
|
+
return account !== this._account;
|
|
66
|
+
}
|
|
67
|
+
// trigger by bridge account change event
|
|
68
|
+
_handleAccountChange(payload) {
|
|
69
|
+
const account = payload;
|
|
70
|
+
if (this.isAccountsChanged(account) && account) {
|
|
71
|
+
this.emit(PROVIDER_EVENTS.accountsChanged, [account]);
|
|
72
|
+
}
|
|
73
|
+
if (!account) {
|
|
74
|
+
this._handleDisconnected();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
on(event, listener) {
|
|
79
|
+
return super.on(event, listener);
|
|
80
|
+
}
|
|
81
|
+
emit(event, ...args) {
|
|
82
|
+
return super.emit(event, ...args);
|
|
83
|
+
}
|
|
84
|
+
removeListener(eventName, listener) {
|
|
85
|
+
return super.removeListener(eventName, listener);
|
|
86
|
+
}
|
|
87
|
+
_transformTx(tx) {
|
|
88
|
+
var _a, _b, _c, _d, _e, _f;
|
|
89
|
+
return {
|
|
90
|
+
Type: 0,
|
|
91
|
+
From: tx.from,
|
|
92
|
+
To: tx.to,
|
|
93
|
+
Amount: (_a = tx.amount) !== null && _a !== void 0 ? _a : 0,
|
|
94
|
+
AccountNonce: (_b = tx.accountNonce) !== null && _b !== void 0 ? _b : 0,
|
|
95
|
+
GasPrice: (_c = tx.gasPrice) !== null && _c !== void 0 ? _c : 1,
|
|
96
|
+
GasLimit: (_d = tx.gasLimit) !== null && _d !== void 0 ? _d : 0,
|
|
97
|
+
Timestamp: (_e = tx.timestamp) !== null && _e !== void 0 ? _e : 0,
|
|
98
|
+
Payload: (_f = tx.payload) !== null && _f !== void 0 ? _f : '',
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
request(props) {
|
|
102
|
+
var _a, _b;
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
const reqParams = props.params;
|
|
105
|
+
let params;
|
|
106
|
+
if (props.method === ScdoRequestMethods.scdo_estimateGas) {
|
|
107
|
+
const tx = reqParams[0];
|
|
108
|
+
params = [{
|
|
109
|
+
Data: this._transformTx(tx),
|
|
110
|
+
Hash: (_a = tx.hash) !== null && _a !== void 0 ? _a : '',
|
|
111
|
+
Signature: (_b = tx.signature) !== null && _b !== void 0 ? _b : { Sig: '' },
|
|
112
|
+
}];
|
|
113
|
+
}
|
|
114
|
+
else if (props.method === ScdoRequestMethods.scdo_signTransaction || props.method === ScdoRequestMethods.scdo_sendTransaction) {
|
|
115
|
+
params = [this._transformTx(props.params[0])];
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
params = reqParams;
|
|
119
|
+
}
|
|
120
|
+
const res = yield this._callBridge({
|
|
121
|
+
method: props.method,
|
|
122
|
+
params,
|
|
123
|
+
});
|
|
124
|
+
if (props.method === ScdoRequestMethods.scdo_getAccounts) {
|
|
125
|
+
this.accounts.length = 0;
|
|
126
|
+
this.accounts.push(...res);
|
|
127
|
+
}
|
|
128
|
+
return res;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IInjectedProviderNames } from '@onekeyfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderBase, IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
|
|
3
|
+
declare class ProviderScdoBase extends ProviderBase {
|
|
4
|
+
constructor(props: IInpageProviderConfig);
|
|
5
|
+
protected providerName: IInjectedProviderNames;
|
|
6
|
+
request(data: unknown): Promise<unknown>;
|
|
7
|
+
}
|
|
8
|
+
export { ProviderScdoBase };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IInjectedProviderNames } from '@onekeyfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderBase } from '@onekeyfe/cross-inpage-provider-core';
|
|
3
|
+
class ProviderScdoBase extends ProviderBase {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
super(props);
|
|
6
|
+
this.providerName = IInjectedProviderNames.scdo;
|
|
7
|
+
}
|
|
8
|
+
request(data) {
|
|
9
|
+
return this.bridgeRequest(data);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export { ProviderScdoBase };
|
|
@@ -0,0 +1,135 @@
|
|
|
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
|
+
exports.ProviderScdo = exports.ScdoRequestMethods = exports.PROVIDER_EVENTS = void 0;
|
|
13
|
+
const extension_bridge_injected_1 = require("@onekeyfe/extension-bridge-injected");
|
|
14
|
+
const ProviderScdoBase_1 = require("./ProviderScdoBase");
|
|
15
|
+
var PROVIDER_EVENTS;
|
|
16
|
+
(function (PROVIDER_EVENTS) {
|
|
17
|
+
PROVIDER_EVENTS["accountsChanged"] = "accountsChanged";
|
|
18
|
+
PROVIDER_EVENTS["disconnect"] = "disconnect";
|
|
19
|
+
PROVIDER_EVENTS["message_low_level"] = "message_low_level";
|
|
20
|
+
})(PROVIDER_EVENTS = exports.PROVIDER_EVENTS || (exports.PROVIDER_EVENTS = {}));
|
|
21
|
+
var ScdoRequestMethods;
|
|
22
|
+
(function (ScdoRequestMethods) {
|
|
23
|
+
ScdoRequestMethods["scdo_requestAccounts"] = "scdo_requestAccounts";
|
|
24
|
+
ScdoRequestMethods["scdo_disconnect"] = "scdo_disconnect";
|
|
25
|
+
ScdoRequestMethods["scdo_getAccounts"] = "scdo_getAccounts";
|
|
26
|
+
ScdoRequestMethods["scdo_getBalance"] = "scdo_getBalance";
|
|
27
|
+
ScdoRequestMethods["scdo_signTransaction"] = "scdo_signTransaction";
|
|
28
|
+
ScdoRequestMethods["scdo_estimateGas"] = "scdo_estimateGas";
|
|
29
|
+
ScdoRequestMethods["scdo_sendTransaction"] = "scdo_sendTransaction";
|
|
30
|
+
})(ScdoRequestMethods = exports.ScdoRequestMethods || (exports.ScdoRequestMethods = {}));
|
|
31
|
+
function isWalletEventMethodMatch({ method, name }) {
|
|
32
|
+
return method === `wallet_events_${name}`;
|
|
33
|
+
}
|
|
34
|
+
class ProviderScdo extends ProviderScdoBase_1.ProviderScdoBase {
|
|
35
|
+
constructor(props) {
|
|
36
|
+
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout: props.timeout }) }));
|
|
37
|
+
this.accounts = [];
|
|
38
|
+
this._registerEvents();
|
|
39
|
+
}
|
|
40
|
+
_registerEvents() {
|
|
41
|
+
window.addEventListener('onekey_bridge_disconnect', () => {
|
|
42
|
+
this._handleDisconnected();
|
|
43
|
+
});
|
|
44
|
+
this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
|
|
45
|
+
if (!payload)
|
|
46
|
+
return;
|
|
47
|
+
const { method, params } = payload;
|
|
48
|
+
if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.accountsChanged })) {
|
|
49
|
+
this._handleAccountChange(params);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
_callBridge(params) {
|
|
54
|
+
return this.bridgeRequest(params);
|
|
55
|
+
}
|
|
56
|
+
_handleDisconnected(options = { emit: true }) {
|
|
57
|
+
this._account = undefined;
|
|
58
|
+
if (options.emit && this.isConnectionStatusChanged('disconnected')) {
|
|
59
|
+
this.connectionStatus = 'disconnected';
|
|
60
|
+
this.emit(PROVIDER_EVENTS.disconnect);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
isAccountsChanged(account) {
|
|
64
|
+
if (!account)
|
|
65
|
+
return false;
|
|
66
|
+
if (!this._account)
|
|
67
|
+
return true;
|
|
68
|
+
return account !== this._account;
|
|
69
|
+
}
|
|
70
|
+
// trigger by bridge account change event
|
|
71
|
+
_handleAccountChange(payload) {
|
|
72
|
+
const account = payload;
|
|
73
|
+
if (this.isAccountsChanged(account) && account) {
|
|
74
|
+
this.emit(PROVIDER_EVENTS.accountsChanged, [account]);
|
|
75
|
+
}
|
|
76
|
+
if (!account) {
|
|
77
|
+
this._handleDisconnected();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
on(event, listener) {
|
|
82
|
+
return super.on(event, listener);
|
|
83
|
+
}
|
|
84
|
+
emit(event, ...args) {
|
|
85
|
+
return super.emit(event, ...args);
|
|
86
|
+
}
|
|
87
|
+
removeListener(eventName, listener) {
|
|
88
|
+
return super.removeListener(eventName, listener);
|
|
89
|
+
}
|
|
90
|
+
_transformTx(tx) {
|
|
91
|
+
var _a, _b, _c, _d, _e, _f;
|
|
92
|
+
return {
|
|
93
|
+
Type: 0,
|
|
94
|
+
From: tx.from,
|
|
95
|
+
To: tx.to,
|
|
96
|
+
Amount: (_a = tx.amount) !== null && _a !== void 0 ? _a : 0,
|
|
97
|
+
AccountNonce: (_b = tx.accountNonce) !== null && _b !== void 0 ? _b : 0,
|
|
98
|
+
GasPrice: (_c = tx.gasPrice) !== null && _c !== void 0 ? _c : 1,
|
|
99
|
+
GasLimit: (_d = tx.gasLimit) !== null && _d !== void 0 ? _d : 0,
|
|
100
|
+
Timestamp: (_e = tx.timestamp) !== null && _e !== void 0 ? _e : 0,
|
|
101
|
+
Payload: (_f = tx.payload) !== null && _f !== void 0 ? _f : '',
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
request(props) {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const reqParams = props.params;
|
|
108
|
+
let params;
|
|
109
|
+
if (props.method === ScdoRequestMethods.scdo_estimateGas) {
|
|
110
|
+
const tx = reqParams[0];
|
|
111
|
+
params = [{
|
|
112
|
+
Data: this._transformTx(tx),
|
|
113
|
+
Hash: (_a = tx.hash) !== null && _a !== void 0 ? _a : '',
|
|
114
|
+
Signature: (_b = tx.signature) !== null && _b !== void 0 ? _b : { Sig: '' },
|
|
115
|
+
}];
|
|
116
|
+
}
|
|
117
|
+
else if (props.method === ScdoRequestMethods.scdo_signTransaction || props.method === ScdoRequestMethods.scdo_sendTransaction) {
|
|
118
|
+
params = [this._transformTx(props.params[0])];
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
params = reqParams;
|
|
122
|
+
}
|
|
123
|
+
const res = yield this._callBridge({
|
|
124
|
+
method: props.method,
|
|
125
|
+
params,
|
|
126
|
+
});
|
|
127
|
+
if (props.method === ScdoRequestMethods.scdo_getAccounts) {
|
|
128
|
+
this.accounts.length = 0;
|
|
129
|
+
this.accounts.push(...res);
|
|
130
|
+
}
|
|
131
|
+
return res;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.ProviderScdo = ProviderScdo;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProviderScdoBase = void 0;
|
|
4
|
+
const cross_inpage_provider_types_1 = require("@onekeyfe/cross-inpage-provider-types");
|
|
5
|
+
const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
|
|
6
|
+
class ProviderScdoBase extends cross_inpage_provider_core_1.ProviderBase {
|
|
7
|
+
constructor(props) {
|
|
8
|
+
super(props);
|
|
9
|
+
this.providerName = cross_inpage_provider_types_1.IInjectedProviderNames.scdo;
|
|
10
|
+
}
|
|
11
|
+
request(data) {
|
|
12
|
+
return this.bridgeRequest(data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.ProviderScdoBase = ProviderScdoBase;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./OnekeyScdoProvider"), exports);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './OnekeyScdoProvider';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './OnekeyScdoProvider';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onekeyfe/onekey-scdo-provider",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"cross-inpage-provider"
|
|
6
|
+
],
|
|
7
|
+
"author": "dev-fe@onekey.so",
|
|
8
|
+
"repository": "https://github.com/OneKeyHQ/cross-inpage-provider",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/*"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/cjs/index.js"
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"main": "./dist/cjs/index.js",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"prebuild": "rm -rf dist",
|
|
27
|
+
"build": "tsc && tsc --project tsconfig.cjs.json",
|
|
28
|
+
"start": "tsc --watch"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@onekeyfe/cross-inpage-provider-core": "2.1.0",
|
|
32
|
+
"@onekeyfe/cross-inpage-provider-errors": "2.1.0",
|
|
33
|
+
"@onekeyfe/cross-inpage-provider-types": "2.1.0",
|
|
34
|
+
"@onekeyfe/extension-bridge-injected": "2.1.0"
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "6c27b10b87edbd6d6c839566276c7f6f9cb4940c"
|
|
37
|
+
}
|