dequanto 0.1.39 → 0.1.45
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/lib/cjs/clients/ClientPool.js +23 -2
- package/lib/cjs/clients/ClientPool.js.map +1 -1
- package/lib/cjs/clients/WalletClient.js +98 -0
- package/lib/cjs/clients/WalletClient.js.map +1 -0
- package/lib/cjs/clients/Web3Client.js +39 -10
- package/lib/cjs/clients/Web3Client.js.map +1 -1
- package/lib/cjs/clients/Web3ClientFactory.js +7 -37
- package/lib/cjs/clients/Web3ClientFactory.js.map +1 -1
- package/lib/cjs/config/Config.js +4 -1
- package/lib/cjs/config/Config.js.map +1 -1
- package/lib/cjs/config/ConfigDefaults.js +3 -6
- package/lib/cjs/config/ConfigDefaults.js.map +1 -1
- package/lib/cjs/config/ConfigProvider.js +5 -1
- package/lib/cjs/config/ConfigProvider.js.map +1 -1
- package/lib/cjs/contracts/ContractWriter.js.map +1 -1
- package/lib/cjs/contracts/deploy/storage/DeploymentsStorage.js +10 -1
- package/lib/cjs/contracts/deploy/storage/DeploymentsStorage.js.map +1 -1
- package/lib/cjs/explorer/BlockchainExplorer.js +65 -43
- package/lib/cjs/explorer/BlockchainExplorer.js.map +1 -1
- package/lib/cjs/explorer/BlockchainExplorerFactory.js +6 -4
- package/lib/cjs/explorer/BlockchainExplorerFactory.js.map +1 -1
- package/lib/cjs/explorer/verifiers/FsHtmlVerifier.js +5 -5
- package/lib/cjs/explorer/verifiers/FsHtmlVerifier.js.map +1 -1
- package/lib/cjs/json/JsonArrayMultiStore.js.map +1 -1
- package/lib/cjs/json/JsonArrayStore.js +1 -1
- package/lib/cjs/json/JsonArrayStore.js.map +1 -1
- package/lib/cjs/json/JsonObjectStore.js +2 -1
- package/lib/cjs/json/JsonObjectStore.js.map +1 -1
- package/lib/cjs/json/JsonStoreFs.js +84 -18
- package/lib/cjs/json/JsonStoreFs.js.map +1 -1
- package/lib/cjs/models/TChain.js +4 -0
- package/lib/cjs/models/TChain.js.map +1 -0
- package/lib/cjs/models/TExplorer.js +4 -0
- package/lib/cjs/models/TExplorer.js.map +1 -0
- package/lib/cjs/models/TPlatform.js +1 -0
- package/lib/cjs/models/TPlatform.js.map +1 -1
- package/lib/cjs/rpc/transports/ITransport.js.map +1 -1
- package/lib/cjs/rpc/transports/RpcTransport.js +4 -0
- package/lib/cjs/rpc/transports/RpcTransport.js.map +1 -1
- package/lib/cjs/rpc/transports/compatibility/EIP1193Transport.js +14 -2
- package/lib/cjs/rpc/transports/compatibility/EIP1193Transport.js.map +1 -1
- package/lib/cjs/rpc/transports/compatibility/IEIP6963Provider.js +4 -0
- package/lib/cjs/rpc/transports/compatibility/IEIP6963Provider.js.map +1 -0
- package/lib/cjs/safe/GnosisSafeHandler.js.map +1 -1
- package/lib/cjs/tokens/TokenProviders/TPChain.js +5 -1
- package/lib/cjs/tokens/TokenProviders/TPChain.js.map +1 -1
- package/lib/cjs/txs/TxWriter.js +8 -1
- package/lib/cjs/txs/TxWriter.js.map +1 -1
- package/lib/cjs/utils/$buffer.js.map +1 -1
- package/lib/cjs/utils/$config.js +67 -2
- package/lib/cjs/utils/$config.js.map +1 -1
- package/lib/cjs/utils/$dependency.js +4 -0
- package/lib/cjs/utils/$dependency.js.map +1 -1
- package/lib/cjs/utils/$path.js +9 -0
- package/lib/cjs/utils/$path.js.map +1 -1
- package/lib/cjs/utils/$ref.js +30 -0
- package/lib/cjs/utils/$ref.js.map +1 -0
- package/lib/cjs/utils/$secret.js +15 -2
- package/lib/cjs/utils/$secret.js.map +1 -1
- package/lib/cjs/utils/$sig.js +18 -9
- package/lib/cjs/utils/$sig.js.map +1 -1
- package/lib/cjs/wallets/EIP6963ProviderFactory.js +192 -0
- package/lib/cjs/wallets/EIP6963ProviderFactory.js.map +1 -0
- package/lib/cjs/wallets/Signer.js +30 -0
- package/lib/cjs/wallets/Signer.js.map +1 -0
- package/lib/esm/clients/ClientPool.js.map +1 -1
- package/lib/esm/clients/ClientPool.mjs +23 -2
- package/lib/esm/clients/WalletClient.js.map +1 -0
- package/lib/esm/clients/WalletClient.mjs +91 -0
- package/lib/esm/clients/Web3Client.js.map +1 -1
- package/lib/esm/clients/Web3Client.mjs +39 -10
- package/lib/esm/clients/Web3ClientFactory.js.map +1 -1
- package/lib/esm/clients/Web3ClientFactory.mjs +7 -37
- package/lib/esm/config/Config.js.map +1 -1
- package/lib/esm/config/Config.mjs +4 -1
- package/lib/esm/config/ConfigDefaults.js.map +1 -1
- package/lib/esm/config/ConfigDefaults.mjs +3 -6
- package/lib/esm/config/ConfigProvider.js.map +1 -1
- package/lib/esm/config/ConfigProvider.mjs +5 -1
- package/lib/esm/contracts/ContractWriter.js.map +1 -1
- package/lib/esm/contracts/deploy/storage/DeploymentsStorage.js.map +1 -1
- package/lib/esm/contracts/deploy/storage/DeploymentsStorage.mjs +10 -1
- package/lib/esm/explorer/BlockchainExplorer.js.map +1 -1
- package/lib/esm/explorer/BlockchainExplorer.mjs +65 -43
- package/lib/esm/explorer/BlockchainExplorerFactory.js.map +1 -1
- package/lib/esm/explorer/BlockchainExplorerFactory.mjs +6 -4
- package/lib/esm/explorer/verifiers/FsHtmlVerifier.js.map +1 -1
- package/lib/esm/explorer/verifiers/FsHtmlVerifier.mjs +5 -5
- package/lib/esm/json/JsonArrayMultiStore.js.map +1 -1
- package/lib/esm/json/JsonArrayStore.js.map +1 -1
- package/lib/esm/json/JsonArrayStore.mjs +1 -1
- package/lib/esm/json/JsonObjectStore.js.map +1 -1
- package/lib/esm/json/JsonObjectStore.mjs +2 -1
- package/lib/esm/json/JsonStoreFs.js.map +1 -1
- package/lib/esm/json/JsonStoreFs.mjs +84 -18
- package/lib/esm/models/TChain.js.map +1 -0
- package/lib/esm/models/TChain.mjs +3 -0
- package/lib/esm/models/TExplorer.js.map +1 -0
- package/lib/esm/models/TExplorer.mjs +3 -0
- package/lib/esm/models/TPlatform.js.map +1 -1
- package/lib/esm/models/TPlatform.mjs +1 -0
- package/lib/esm/rpc/transports/ITransport.js.map +1 -1
- package/lib/esm/rpc/transports/RpcTransport.js.map +1 -1
- package/lib/esm/rpc/transports/RpcTransport.mjs +4 -0
- package/lib/esm/rpc/transports/compatibility/EIP1193Transport.js.map +1 -1
- package/lib/esm/rpc/transports/compatibility/EIP1193Transport.mjs +14 -2
- package/lib/esm/rpc/transports/compatibility/IEIP6963Provider.js.map +1 -0
- package/lib/esm/rpc/transports/compatibility/IEIP6963Provider.mjs +3 -0
- package/lib/esm/safe/GnosisSafeHandler.js.map +1 -1
- package/lib/esm/tokens/TokenProviders/TPChain.js.map +1 -1
- package/lib/esm/tokens/TokenProviders/TPChain.mjs +5 -1
- package/lib/esm/txs/TxWriter.js.map +1 -1
- package/lib/esm/txs/TxWriter.mjs +8 -1
- package/lib/esm/utils/$buffer.js.map +1 -1
- package/lib/esm/utils/$config.js.map +1 -1
- package/lib/esm/utils/$config.mjs +64 -2
- package/lib/esm/utils/$dependency.js.map +1 -1
- package/lib/esm/utils/$dependency.mjs +4 -0
- package/lib/esm/utils/$path.js.map +1 -1
- package/lib/esm/utils/$path.mjs +9 -0
- package/lib/esm/utils/$ref.js.map +1 -0
- package/lib/esm/utils/$ref.mjs +27 -0
- package/lib/esm/utils/$secret.js.map +1 -1
- package/lib/esm/utils/$secret.mjs +15 -2
- package/lib/esm/utils/$sig.js.map +1 -1
- package/lib/esm/utils/$sig.mjs +18 -9
- package/lib/esm/wallets/EIP6963ProviderFactory.js.map +1 -0
- package/lib/esm/wallets/EIP6963ProviderFactory.mjs +185 -0
- package/lib/esm/wallets/Signer.js.map +1 -0
- package/lib/esm/wallets/Signer.mjs +26 -0
- package/lib/types/clients/ClientPool.d.ts +13 -5
- package/lib/types/clients/WalletClient.d.ts +28 -0
- package/lib/types/clients/Web3Client.d.ts +7 -4
- package/lib/types/clients/Web3ClientFactory.d.ts +1 -1
- package/lib/types/clients/interfaces/IWeb3Client.d.ts +2 -5
- package/lib/types/config/AConfigBase.d.ts +0 -7
- package/lib/types/config/interface/IConfigData.d.ts +5 -2
- package/lib/types/explorer/BlockchainExplorer.d.ts +7 -4
- package/lib/types/explorer/BlockchainExplorerFactory.d.ts +7 -3
- package/lib/types/explorer/verifiers/FsHtmlVerifier.d.ts +3 -3
- package/lib/types/json/JsonArrayMultiStore.d.ts +2 -2
- package/lib/types/json/JsonArrayStore.d.ts +4 -3
- package/lib/types/json/JsonObjectStore.d.ts +5 -3
- package/lib/types/json/JsonStoreFs.d.ts +3 -2
- package/lib/types/models/TAccount.d.ts +9 -0
- package/lib/types/models/TChain.d.ts +45 -0
- package/lib/types/models/TEth.d.ts +8 -0
- package/lib/types/models/TExplorer.d.ts +31 -0
- package/lib/types/rpc/transports/ITransport.d.ts +2 -1
- package/lib/types/rpc/transports/compatibility/EIP1193Transport.d.ts +5 -1
- package/lib/types/rpc/transports/compatibility/IEIP6963Provider.d.ts +21 -0
- package/lib/types/utils/$config.d.ts +5 -0
- package/lib/types/utils/$ref.d.ts +11 -0
- package/lib/types/utils/$secret.d.ts +1 -0
- package/lib/types/utils/$sig.d.ts +1 -0
- package/lib/types/wallets/EIP6963ProviderFactory.d.ts +47 -0
- package/lib/types/wallets/Signer.d.ts +15 -0
- package/package.json +1 -1
- package/src/clients/ClientPool.ts +49 -15
- package/src/clients/WalletClient.ts +115 -0
- package/src/clients/Web3Client.ts +47 -16
- package/src/clients/Web3ClientFactory.ts +8 -39
- package/src/clients/interfaces/IWeb3Client.ts +2 -7
- package/src/config/AConfigBase.ts +3 -11
- package/src/config/Config.ts +6 -1
- package/src/config/ConfigDefaults.ts +3 -6
- package/src/config/ConfigProvider.ts +5 -1
- package/src/config/interface/IConfigData.ts +9 -4
- package/src/contracts/ContractWriter.ts +1 -0
- package/src/contracts/deploy/storage/DeploymentsStorage.ts +13 -3
- package/src/explorer/BlockchainExplorer.ts +82 -51
- package/src/explorer/BlockchainExplorerFactory.ts +19 -6
- package/src/explorer/verifiers/FsHtmlVerifier.ts +8 -8
- package/src/json/JsonArrayMultiStore.ts +2 -2
- package/src/json/JsonArrayStore.ts +5 -2
- package/src/json/JsonObjectStore.ts +7 -4
- package/src/json/JsonStoreFs.ts +103 -18
- package/src/models/TAccount.ts +10 -2
- package/src/models/TChain.ts +56 -0
- package/src/models/TEth.ts +9 -3
- package/src/models/TExplorer.ts +35 -0
- package/src/models/TPlatform.ts +1 -0
- package/src/rpc/transports/ITransport.ts +2 -1
- package/src/rpc/transports/RpcTransport.ts +4 -0
- package/src/rpc/transports/compatibility/EIP1193Transport.ts +19 -6
- package/src/rpc/transports/compatibility/IEIP6963Provider.ts +22 -0
- package/src/safe/GnosisSafeHandler.ts +1 -1
- package/src/tokens/TokenProviders/TPChain.ts +5 -1
- package/src/txs/TxWriter.ts +8 -2
- package/src/utils/$buffer.ts +1 -1
- package/src/utils/$config.ts +73 -2
- package/src/utils/$dependency.ts +5 -0
- package/src/utils/$path.ts +9 -0
- package/src/utils/$ref.ts +40 -0
- package/src/utils/$secret.ts +16 -3
- package/src/utils/$sig.ts +19 -10
- package/src/wallets/EIP6963ProviderFactory.ts +252 -0
- package/src/wallets/Signer.ts +32 -0
- package/lib/cjs/wallets/WalletBsc.js +0 -19
- package/lib/cjs/wallets/WalletBsc.js.map +0 -1
- package/lib/esm/wallets/WalletBsc.js.map +0 -1
- package/lib/esm/wallets/WalletBsc.mjs +0 -12
- package/lib/types/wallets/WalletBsc.d.ts +0 -10
- package/src/wallets/WalletBsc.ts +0 -13
package/src/utils/$secret.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { $machine } from './$machine';
|
|
2
2
|
|
|
3
3
|
export namespace $secret {
|
|
4
|
-
export async function getPin(parameters?: { pin?: string }) {
|
|
4
|
+
export async function getPin(parameters?: { pin?: string, machineKey?: string }) {
|
|
5
5
|
|
|
6
6
|
let pin = parameters?.pin ?? getPinFromCli() ?? getPinFromEnv();
|
|
7
7
|
if (pin == null || pin.length === 0) {
|
|
8
8
|
return null;
|
|
9
9
|
}
|
|
10
|
-
let
|
|
11
|
-
return `${
|
|
10
|
+
let machineKey = parameters?.machineKey ?? getMachineKeyFromCli() ?? getMachineKeyFromEnv() ?? await $machine.id();
|
|
11
|
+
return `${machineKey}:${pin}`;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
function getPinFromCli () {
|
|
@@ -24,4 +24,17 @@ export namespace $secret {
|
|
|
24
24
|
function getPinFromEnv () {
|
|
25
25
|
return process.env.PIN;
|
|
26
26
|
}
|
|
27
|
+
function getMachineKeyFromCli () {
|
|
28
|
+
let args = process.argv;
|
|
29
|
+
for (let i = 0; i < args.length - 1; i++) {
|
|
30
|
+
let key = args[i].replace(/^\-+/, '');
|
|
31
|
+
if (key === 'machineKey' || key ==='machine-key') {
|
|
32
|
+
return args[i + 1];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function getMachineKeyFromEnv () {
|
|
38
|
+
return process.env.MACHINE_KEY;
|
|
39
|
+
}
|
|
27
40
|
}
|
package/src/utils/$sig.ts
CHANGED
|
@@ -24,30 +24,29 @@ export namespace $sig {
|
|
|
24
24
|
if (account.key != null) {
|
|
25
25
|
return await KeyUtils.withKey(account, account => $ec.$eip191.signTypedData(typedData, account));
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
let signer = await $rpc.ensureRpcSigner(account, rpc);
|
|
28
|
+
return $rpc.signTypedData(signer, typedData, account);
|
|
28
29
|
}
|
|
29
30
|
export async function sign(message: string | Uint8Array, account: TEth.EoAccount, rpc?: Rpc): Promise<TSignature> {
|
|
30
31
|
if (account.key != null) {
|
|
31
32
|
return await KeyUtils.withKey(account, account => $ec.sign(message, account));
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
+
let signer = await $rpc.ensureRpcSigner(account, rpc);
|
|
35
|
+
return $rpc.sign(signer, message, account);
|
|
34
36
|
}
|
|
35
37
|
export async function signMessage(message: string | Uint8Array, account: TEth.EoAccount, mix?: Rpc | Web3Client): Promise<TSignature> {
|
|
36
38
|
if (account.key != null) {
|
|
37
39
|
return await KeyUtils.withKey(account, account => $ec.$eip191.signMessage(message, account));
|
|
38
40
|
}
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
: mix;
|
|
42
|
-
return $rpc.signMessage(rpc, message, account);
|
|
41
|
+
let signer = await $rpc.ensureRpcSigner(account, mix);
|
|
42
|
+
return $rpc.signMessage(signer, message, account);
|
|
43
43
|
}
|
|
44
44
|
export async function signTx(tx: TEth.TxLike, account: TEth.EoAccount, rpc?: Rpc): Promise<TEth.Hex> {
|
|
45
45
|
tx.from ??= account.address;
|
|
46
46
|
|
|
47
47
|
if ($hex.isEmpty(account.key)) {
|
|
48
|
-
$
|
|
49
|
-
|
|
50
|
-
return $rpc.signTx(rpc, tx);
|
|
48
|
+
let signer = await $rpc.ensureRpcSigner(account, rpc);
|
|
49
|
+
return $rpc.signTx(signer, tx);
|
|
51
50
|
}
|
|
52
51
|
return await KeyUtils.withKey(account, account => $ec.signTx(tx, account));
|
|
53
52
|
}
|
|
@@ -70,6 +69,16 @@ export namespace $sig {
|
|
|
70
69
|
}
|
|
71
70
|
|
|
72
71
|
export namespace $rpc {
|
|
72
|
+
|
|
73
|
+
export async function ensureRpcSigner (account: TEth.EoAccount, mix: Rpc | Web3Client): Promise<Rpc> {
|
|
74
|
+
if (account.signer) {
|
|
75
|
+
return account.signer as any;
|
|
76
|
+
}
|
|
77
|
+
$require.notNull(mix, `Rpc signer is not provided for ${account.address}`);
|
|
78
|
+
let rpc = 'getRpc' in mix ? await mix.getRpc() : mix;
|
|
79
|
+
return rpc;
|
|
80
|
+
}
|
|
81
|
+
|
|
73
82
|
export async function signTx(rpc: Rpc, tx: TEth.TxLike): Promise<TEth.Hex> {
|
|
74
83
|
let body = {
|
|
75
84
|
type: $hex.ensure(tx.type),
|
|
@@ -264,7 +273,7 @@ export namespace $sig {
|
|
|
264
273
|
let r = '0x' + signature.substring(2, 2 + 64);
|
|
265
274
|
let s = '0x' + signature.substring(2 + 64, 2 + 64 + 64);
|
|
266
275
|
let v = '0x' + signature.substring(2 + 64 + 64);
|
|
267
|
-
return { r, s, v } as TSignature;
|
|
276
|
+
return { r, s, v, signature } as TSignature;
|
|
268
277
|
}
|
|
269
278
|
|
|
270
279
|
export function toUint8Array(message: string | Uint8Array, opts?: { encoding?: 'utf8' | 'hex' }): Uint8Array {
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import alot from 'alot';
|
|
2
|
+
import { TAddress } from '@dequanto/models/TAddress';
|
|
3
|
+
import { TEth } from '@dequanto/models/TEth';
|
|
4
|
+
import { $address } from '@dequanto/utils/$address';
|
|
5
|
+
import { $ref, TGlobal } from '@dequanto/utils/$ref';
|
|
6
|
+
import { $require } from '@dequanto/utils/$require';
|
|
7
|
+
import { class_EventEmitter } from 'atma-utils';
|
|
8
|
+
import { IEIP6963Provider } from '@dequanto/rpc/transports/compatibility/IEIP6963Provider';
|
|
9
|
+
|
|
10
|
+
// Interface for provider information following EIP-6963.
|
|
11
|
+
interface EIP6963ProviderInfo {
|
|
12
|
+
rdns?: string; // Unique identifier for the wallet e.g io.metamask, io.metamask.flask
|
|
13
|
+
uuid: string; // Globally unique ID to differentiate between provider sessions for the lifetime of the page
|
|
14
|
+
name: string; // Human-readable name of the wallet
|
|
15
|
+
icon?: string; // URL to the wallet's icon
|
|
16
|
+
chainId?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Interface for Ethereum providers based on the EIP-1193 standard.
|
|
20
|
+
|
|
21
|
+
// Interface detailing the structure of provider information and its Ethereum provider.
|
|
22
|
+
export interface EIP6963ProviderDetail {
|
|
23
|
+
info: EIP6963ProviderInfo; // The provider's info
|
|
24
|
+
provider: IEIP6963Provider; // The EIP-1193 compatible provider
|
|
25
|
+
accounts?: TEth.Address[]; // Optional: Array of connected Ethereum accounts
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Type representing the event structure for announcing a provider based on EIP-6963.
|
|
29
|
+
type EIP6963AnnounceProviderEvent = {
|
|
30
|
+
detail: EIP6963ProviderDetail
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
interface IProviderEvents {
|
|
35
|
+
onProviderRegistered: (detail: EIP6963ProviderDetail) => void;
|
|
36
|
+
onProviderConnected: (detail: EIP6963ProviderDetail, chainId: number) => void;
|
|
37
|
+
|
|
38
|
+
onAccountsConnected: (detail: EIP6963ProviderDetail, accounts: TAddress[]) => void;
|
|
39
|
+
onAccountsDisconnected: (detail: EIP6963ProviderDetail) => void;
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
onChainChanged: (detail: EIP6963ProviderDetail, chainId: number) => void;
|
|
43
|
+
onAccountsChanged: (detail: EIP6963ProviderDetail, accounts: TEth.Address[]) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export class EIP6963ProviderFactory extends class_EventEmitter<IProviderEvents> {
|
|
48
|
+
|
|
49
|
+
private listeners = {} as Record<string, {
|
|
50
|
+
accountsChanged,
|
|
51
|
+
chainChanged,
|
|
52
|
+
connect,
|
|
53
|
+
disconnect
|
|
54
|
+
}>
|
|
55
|
+
|
|
56
|
+
providers: EIP6963ProviderDetail[] = []
|
|
57
|
+
selected: EIP6963ProviderDetail;
|
|
58
|
+
|
|
59
|
+
global: TGlobal = $ref.getGlobal()
|
|
60
|
+
|
|
61
|
+
constructor () {
|
|
62
|
+
super();
|
|
63
|
+
|
|
64
|
+
this.onAnnounceProvider = this.onAnnounceProvider.bind(this);
|
|
65
|
+
this.listenToNewProvider();
|
|
66
|
+
this.requestProvider();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
isConnected (address?: TEth.Address) {
|
|
70
|
+
let has = this.providers.some(x => {
|
|
71
|
+
return address != null
|
|
72
|
+
? x.accounts?.some(account => $address.eq(account, address) )
|
|
73
|
+
: x.accounts?.length > 0;
|
|
74
|
+
});
|
|
75
|
+
return has;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async connect (id?: string) {
|
|
79
|
+
let provider = this.getProviderOrFirst(id);
|
|
80
|
+
$require.notNull(provider, `Wallet not found`);
|
|
81
|
+
this.selected = provider;
|
|
82
|
+
let accounts = await this.requestAccounts();
|
|
83
|
+
return accounts;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
disconnect () {
|
|
87
|
+
let selected = this.selected;
|
|
88
|
+
this.selected = null;
|
|
89
|
+
this.emit('onAccountsDisconnected', selected);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
useProvider (id: string) {
|
|
93
|
+
this.selected = this.getProvider(id);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getProviders () {
|
|
97
|
+
return this.providers;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async requestAccounts (id?: string): Promise<TEth.Address[]> {
|
|
101
|
+
let provider = await this.getProvider(id);
|
|
102
|
+
let accounts: TEth.Address[] = await this.selected.provider.request({ method: 'eth_requestAccounts' });
|
|
103
|
+
if (accounts?.length > 0) {
|
|
104
|
+
let arr = [
|
|
105
|
+
...(provider.accounts ?? []),
|
|
106
|
+
...(accounts),
|
|
107
|
+
]
|
|
108
|
+
.filter(Boolean)
|
|
109
|
+
.map(String) as TEth.Address[];
|
|
110
|
+
|
|
111
|
+
provider.accounts = alot(arr).distinct().toArray();
|
|
112
|
+
this.emit('onAccountsConnected', provider, provider.accounts);
|
|
113
|
+
}
|
|
114
|
+
return provider.accounts;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private requestProvider () {
|
|
118
|
+
if (typeof this.global.ethereum?.request === 'function') {
|
|
119
|
+
this.onAnnounceProvider({
|
|
120
|
+
detail: {
|
|
121
|
+
info: {
|
|
122
|
+
rdns: 'injected',
|
|
123
|
+
uuid: 'injected',
|
|
124
|
+
name: 'Browser Wallet'
|
|
125
|
+
},
|
|
126
|
+
provider: this.global.ethereum,
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
this.global.dispatchEvent(new CustomEvent('eip6963:requestProvider'));
|
|
131
|
+
|
|
132
|
+
}
|
|
133
|
+
private listenToNewProvider () {
|
|
134
|
+
this.global.addEventListener('eip6963:announceProvider', this.onAnnounceProvider as any);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private async onAnnounceProvider (event: EIP6963AnnounceProviderEvent) {
|
|
138
|
+
let injectedProvider = this.providers.find(x => x.info?.rdns === 'injected');
|
|
139
|
+
if (injectedProvider) {
|
|
140
|
+
// Remove directly injected provider
|
|
141
|
+
this.providers = this.providers.filter(x => x.info?.rdns !== 'injected');
|
|
142
|
+
this.removeEventListeners(injectedProvider);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let { detail } = event;
|
|
146
|
+
let { info, provider } = detail;
|
|
147
|
+
if (this.providers.some(x => this.getId(x.info) === this.getId(info))) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let providerDetails = { info, provider, accounts: [] };
|
|
152
|
+
this.providers.push(providerDetails);
|
|
153
|
+
this.addEventListeners(providerDetails);
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const accounts = await provider.request({ method: 'eth_accounts' })
|
|
157
|
+
if (accounts?.length > 0) {
|
|
158
|
+
providerDetails.accounts = accounts;
|
|
159
|
+
this.emit('onAccountsConnected', providerDetails, accounts);
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
// We load the accounts just in case if user has already connected previously, otherwise silently ignore the error
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const chainId = await provider.request({ method: 'eth_chainId' })
|
|
166
|
+
providerDetails.info.chainId = Number(chainId);
|
|
167
|
+
} catch (error) {
|
|
168
|
+
// silently ignore the error
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.emit('onProviderRegistered', providerDetails);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
getProviderOrFirst (id?: string) {
|
|
175
|
+
if (id == null) {
|
|
176
|
+
return this.providers[0];
|
|
177
|
+
}
|
|
178
|
+
let provider = this.providers.find(x => this.getId(x.info) === id);
|
|
179
|
+
return provider;
|
|
180
|
+
}
|
|
181
|
+
getProvider (id?: string, optional?: boolean) {
|
|
182
|
+
if (id == null) {
|
|
183
|
+
optional !== true && $require.notNull(this.selected, `Wallet is not connected`);
|
|
184
|
+
return this.selected;
|
|
185
|
+
}
|
|
186
|
+
let provider = this.providers.find(x => this.getId(x.info) === id);
|
|
187
|
+
optional !== true && $require.notNull(provider, `Wallet is not found by ID ${id}`);
|
|
188
|
+
return provider;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private getId (info: EIP6963ProviderInfo | null) {
|
|
192
|
+
return info?.rdns ?? info?.name;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private addEventListeners (providerDetails: EIP6963ProviderDetail) {
|
|
196
|
+
let id = this.getId(providerDetails.info);
|
|
197
|
+
if (id in this.listeners) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const fns = {
|
|
201
|
+
accountsChanged: accounts => {
|
|
202
|
+
providerDetails.accounts = accounts;
|
|
203
|
+
this.emit('onAccountsChanged', providerDetails, accounts as TEth.Address[]);
|
|
204
|
+
},
|
|
205
|
+
chainChanged: chainId => {
|
|
206
|
+
this.emit('onChainChanged', providerDetails, Number(chainId));
|
|
207
|
+
},
|
|
208
|
+
disconnect: () => {
|
|
209
|
+
this.emit('onAccountsDisconnected', providerDetails);
|
|
210
|
+
},
|
|
211
|
+
connect: (info) => {
|
|
212
|
+
this.emit('onProviderConnected', providerDetails, info.chainId);
|
|
213
|
+
},
|
|
214
|
+
}
|
|
215
|
+
let boundFnKey: 'on' | 'addEventListener';
|
|
216
|
+
let provider = providerDetails.provider;
|
|
217
|
+
|
|
218
|
+
if (typeof provider.on === 'function') {
|
|
219
|
+
boundFnKey = 'on';
|
|
220
|
+
} else if (typeof (provider as any).addEventListener === 'function') {
|
|
221
|
+
boundFnKey = 'addEventListener';
|
|
222
|
+
}
|
|
223
|
+
if (boundFnKey == null) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
for (let event in fns) {
|
|
227
|
+
provider[boundFnKey](event, fns[event]);
|
|
228
|
+
}
|
|
229
|
+
this.listeners[id] = fns;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private removeEventListeners (providerDetails: EIP6963ProviderDetail) {
|
|
233
|
+
let id = this.getId(providerDetails.info);
|
|
234
|
+
if (id in this.listeners === false) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let unboundFnKey: 'removeListener' | 'removeEventListener';
|
|
239
|
+
let provider = providerDetails.provider;
|
|
240
|
+
if (typeof provider.removeListener === 'function') {
|
|
241
|
+
unboundFnKey = 'removeListener';
|
|
242
|
+
} else if (typeof (provider as any).removeEventListener === 'function') {
|
|
243
|
+
unboundFnKey = 'removeEventListener';
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let fns = this.listeners[id];
|
|
247
|
+
for (let event in fns) {
|
|
248
|
+
provider[unboundFnKey](event as any, fns[event]);
|
|
249
|
+
}
|
|
250
|
+
delete this.listeners[id];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { TEth } from '@dequanto/models/TEth';
|
|
2
|
+
import { Rpc, RpcTypes } from '@dequanto/rpc/Rpc';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class Signer implements TEth.IRpcSigner {
|
|
6
|
+
constructor(public opts: {
|
|
7
|
+
rpc?: Rpc,
|
|
8
|
+
} & Partial<TEth.IRpcSigner> ) {
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
eth_sign(account: TEth.Address, data: TEth.Hex): Promise<TEth.Hex> {
|
|
12
|
+
return this.call('eth_sign', [account, data]);
|
|
13
|
+
}
|
|
14
|
+
eth_signTransaction(tx: TEth.TxLike): Promise<TEth.Hex> {
|
|
15
|
+
return this.call('eth_signTransaction', [tx]);
|
|
16
|
+
}
|
|
17
|
+
eth_signTypedData_v4(address: TEth.Address, typedData: Partial<RpcTypes.TypedData>): Promise<TEth.Hex> {
|
|
18
|
+
return this.call('eth_signTypedData_v4', [address, typedData]);
|
|
19
|
+
}
|
|
20
|
+
personal_sign(challenge: string, address: TEth.Address): Promise<TEth.Hex> {
|
|
21
|
+
return this.call('personal_sign', [challenge, address]);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected call (method: keyof TEth.IRpcSigner, params: any[]): Promise<any> {
|
|
25
|
+
let fn = this.opts[method] ?? this.opts.rpc[method];
|
|
26
|
+
|
|
27
|
+
if (typeof fn === 'function') {
|
|
28
|
+
return (fn as any)(...params);
|
|
29
|
+
}
|
|
30
|
+
throw new Error(`Method ${method} not found in Signer`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -1,19 +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.WalletBsc = void 0;
|
|
7
|
-
const a_di_1 = __importDefault(require("a-di"));
|
|
8
|
-
const BscWeb3Client_1 = require("../clients/BscWeb3Client.js");
|
|
9
|
-
const Wallet_1 = require("./Wallet.js");
|
|
10
|
-
const Bscscan_1 = require("../explorer/Bscscan.js");
|
|
11
|
-
/** @deprecated use generic Wallet.ts */
|
|
12
|
-
class WalletBsc extends Wallet_1.Wallet {
|
|
13
|
-
constructor(account) {
|
|
14
|
-
super(account, a_di_1.default.resolve(BscWeb3Client_1.BscWeb3Client), a_di_1.default.resolve(Bscscan_1.Bscscan));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
exports.WalletBsc = WalletBsc;
|
|
18
|
-
//# sourceMappingURL=WalletBsc.js.map
|
|
19
|
-
//# sourceMappingURL=WalletBsc.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WalletBsc.js","sourceRoot":"","sources":["../../../src/wallets/WalletBsc.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAsB;AACtB,mEAAgE;AAChE,qCAAkC;AAClC,wDAAqD;AAIrD,wCAAwC;AACxC,MAAa,SAAU,SAAQ,eAAM;IACjC,YAAa,OAAmD;QAC5D,KAAK,CAAC,OAAO,EAAE,cAAE,CAAC,OAAO,CAAC,6BAAa,CAAC,EAAE,cAAE,CAAC,OAAO,CAAC,iBAAO,CAAC,CAAC,CAAC;IACnE,CAAC;CACJ;AAJD,8BAIC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"WalletBsc.js","sourceRoot":"","sources":["../../../src/wallets/WalletBsc.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAsB;AACtB,mEAAgE;AAChE,qCAAkC;AAClC,wDAAqD;AAIrD,wCAAwC;AACxC,MAAa,SAAU,SAAQ,eAAM;IACjC,YAAa,OAAmD;QAC5D,KAAK,CAAC,OAAO,EAAE,cAAE,CAAC,OAAO,CAAC,6BAAa,CAAC,EAAE,cAAE,CAAC,OAAO,CAAC,iBAAO,CAAC,CAAC,CAAC;IACnE,CAAC;CACJ;AAJD,8BAIC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import di from 'a-di';
|
|
2
|
-
import { BscWeb3Client } from '../clients/BscWeb3Client.mjs';
|
|
3
|
-
import { Wallet } from './Wallet.mjs';
|
|
4
|
-
import { Bscscan } from '../explorer/Bscscan.mjs';
|
|
5
|
-
/** @deprecated use generic Wallet.ts */
|
|
6
|
-
export class WalletBsc extends Wallet {
|
|
7
|
-
constructor(account) {
|
|
8
|
-
super(account, di.resolve(BscWeb3Client), di.resolve(Bscscan));
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=WalletBsc.js.map
|
|
12
|
-
//# sourceMappingURL=WalletBsc.ts.map
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Wallet } from './Wallet';
|
|
2
|
-
import { TPlatform } from '../models/TPlatform';
|
|
3
|
-
import { TAddress } from '../models/TAddress';
|
|
4
|
-
/** @deprecated use generic Wallet.ts */
|
|
5
|
-
export declare class WalletBsc extends Wallet {
|
|
6
|
-
constructor(account: {
|
|
7
|
-
platform: TPlatform;
|
|
8
|
-
address: TAddress;
|
|
9
|
-
});
|
|
10
|
-
}
|
package/src/wallets/WalletBsc.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import di from 'a-di';
|
|
2
|
-
import { BscWeb3Client } from '@dequanto/clients/BscWeb3Client';
|
|
3
|
-
import { Wallet } from './Wallet';
|
|
4
|
-
import { Bscscan } from '@dequanto/explorer/Bscscan';
|
|
5
|
-
import { TPlatform } from '@dequanto/models/TPlatform';
|
|
6
|
-
import { TAddress } from '@dequanto/models/TAddress';
|
|
7
|
-
|
|
8
|
-
/** @deprecated use generic Wallet.ts */
|
|
9
|
-
export class WalletBsc extends Wallet {
|
|
10
|
-
constructor (account: { platform: TPlatform, address: TAddress },) {
|
|
11
|
-
super(account, di.resolve(BscWeb3Client), di.resolve(Bscscan));
|
|
12
|
-
}
|
|
13
|
-
}
|