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.
Files changed (204) hide show
  1. package/lib/cjs/clients/ClientPool.js +23 -2
  2. package/lib/cjs/clients/ClientPool.js.map +1 -1
  3. package/lib/cjs/clients/WalletClient.js +98 -0
  4. package/lib/cjs/clients/WalletClient.js.map +1 -0
  5. package/lib/cjs/clients/Web3Client.js +39 -10
  6. package/lib/cjs/clients/Web3Client.js.map +1 -1
  7. package/lib/cjs/clients/Web3ClientFactory.js +7 -37
  8. package/lib/cjs/clients/Web3ClientFactory.js.map +1 -1
  9. package/lib/cjs/config/Config.js +4 -1
  10. package/lib/cjs/config/Config.js.map +1 -1
  11. package/lib/cjs/config/ConfigDefaults.js +3 -6
  12. package/lib/cjs/config/ConfigDefaults.js.map +1 -1
  13. package/lib/cjs/config/ConfigProvider.js +5 -1
  14. package/lib/cjs/config/ConfigProvider.js.map +1 -1
  15. package/lib/cjs/contracts/ContractWriter.js.map +1 -1
  16. package/lib/cjs/contracts/deploy/storage/DeploymentsStorage.js +10 -1
  17. package/lib/cjs/contracts/deploy/storage/DeploymentsStorage.js.map +1 -1
  18. package/lib/cjs/explorer/BlockchainExplorer.js +65 -43
  19. package/lib/cjs/explorer/BlockchainExplorer.js.map +1 -1
  20. package/lib/cjs/explorer/BlockchainExplorerFactory.js +6 -4
  21. package/lib/cjs/explorer/BlockchainExplorerFactory.js.map +1 -1
  22. package/lib/cjs/explorer/verifiers/FsHtmlVerifier.js +5 -5
  23. package/lib/cjs/explorer/verifiers/FsHtmlVerifier.js.map +1 -1
  24. package/lib/cjs/json/JsonArrayMultiStore.js.map +1 -1
  25. package/lib/cjs/json/JsonArrayStore.js +1 -1
  26. package/lib/cjs/json/JsonArrayStore.js.map +1 -1
  27. package/lib/cjs/json/JsonObjectStore.js +2 -1
  28. package/lib/cjs/json/JsonObjectStore.js.map +1 -1
  29. package/lib/cjs/json/JsonStoreFs.js +84 -18
  30. package/lib/cjs/json/JsonStoreFs.js.map +1 -1
  31. package/lib/cjs/models/TChain.js +4 -0
  32. package/lib/cjs/models/TChain.js.map +1 -0
  33. package/lib/cjs/models/TExplorer.js +4 -0
  34. package/lib/cjs/models/TExplorer.js.map +1 -0
  35. package/lib/cjs/models/TPlatform.js +1 -0
  36. package/lib/cjs/models/TPlatform.js.map +1 -1
  37. package/lib/cjs/rpc/transports/ITransport.js.map +1 -1
  38. package/lib/cjs/rpc/transports/RpcTransport.js +4 -0
  39. package/lib/cjs/rpc/transports/RpcTransport.js.map +1 -1
  40. package/lib/cjs/rpc/transports/compatibility/EIP1193Transport.js +14 -2
  41. package/lib/cjs/rpc/transports/compatibility/EIP1193Transport.js.map +1 -1
  42. package/lib/cjs/rpc/transports/compatibility/IEIP6963Provider.js +4 -0
  43. package/lib/cjs/rpc/transports/compatibility/IEIP6963Provider.js.map +1 -0
  44. package/lib/cjs/safe/GnosisSafeHandler.js.map +1 -1
  45. package/lib/cjs/tokens/TokenProviders/TPChain.js +5 -1
  46. package/lib/cjs/tokens/TokenProviders/TPChain.js.map +1 -1
  47. package/lib/cjs/txs/TxWriter.js +8 -1
  48. package/lib/cjs/txs/TxWriter.js.map +1 -1
  49. package/lib/cjs/utils/$buffer.js.map +1 -1
  50. package/lib/cjs/utils/$config.js +67 -2
  51. package/lib/cjs/utils/$config.js.map +1 -1
  52. package/lib/cjs/utils/$dependency.js +4 -0
  53. package/lib/cjs/utils/$dependency.js.map +1 -1
  54. package/lib/cjs/utils/$path.js +9 -0
  55. package/lib/cjs/utils/$path.js.map +1 -1
  56. package/lib/cjs/utils/$ref.js +30 -0
  57. package/lib/cjs/utils/$ref.js.map +1 -0
  58. package/lib/cjs/utils/$secret.js +15 -2
  59. package/lib/cjs/utils/$secret.js.map +1 -1
  60. package/lib/cjs/utils/$sig.js +18 -9
  61. package/lib/cjs/utils/$sig.js.map +1 -1
  62. package/lib/cjs/wallets/EIP6963ProviderFactory.js +192 -0
  63. package/lib/cjs/wallets/EIP6963ProviderFactory.js.map +1 -0
  64. package/lib/cjs/wallets/Signer.js +30 -0
  65. package/lib/cjs/wallets/Signer.js.map +1 -0
  66. package/lib/esm/clients/ClientPool.js.map +1 -1
  67. package/lib/esm/clients/ClientPool.mjs +23 -2
  68. package/lib/esm/clients/WalletClient.js.map +1 -0
  69. package/lib/esm/clients/WalletClient.mjs +91 -0
  70. package/lib/esm/clients/Web3Client.js.map +1 -1
  71. package/lib/esm/clients/Web3Client.mjs +39 -10
  72. package/lib/esm/clients/Web3ClientFactory.js.map +1 -1
  73. package/lib/esm/clients/Web3ClientFactory.mjs +7 -37
  74. package/lib/esm/config/Config.js.map +1 -1
  75. package/lib/esm/config/Config.mjs +4 -1
  76. package/lib/esm/config/ConfigDefaults.js.map +1 -1
  77. package/lib/esm/config/ConfigDefaults.mjs +3 -6
  78. package/lib/esm/config/ConfigProvider.js.map +1 -1
  79. package/lib/esm/config/ConfigProvider.mjs +5 -1
  80. package/lib/esm/contracts/ContractWriter.js.map +1 -1
  81. package/lib/esm/contracts/deploy/storage/DeploymentsStorage.js.map +1 -1
  82. package/lib/esm/contracts/deploy/storage/DeploymentsStorage.mjs +10 -1
  83. package/lib/esm/explorer/BlockchainExplorer.js.map +1 -1
  84. package/lib/esm/explorer/BlockchainExplorer.mjs +65 -43
  85. package/lib/esm/explorer/BlockchainExplorerFactory.js.map +1 -1
  86. package/lib/esm/explorer/BlockchainExplorerFactory.mjs +6 -4
  87. package/lib/esm/explorer/verifiers/FsHtmlVerifier.js.map +1 -1
  88. package/lib/esm/explorer/verifiers/FsHtmlVerifier.mjs +5 -5
  89. package/lib/esm/json/JsonArrayMultiStore.js.map +1 -1
  90. package/lib/esm/json/JsonArrayStore.js.map +1 -1
  91. package/lib/esm/json/JsonArrayStore.mjs +1 -1
  92. package/lib/esm/json/JsonObjectStore.js.map +1 -1
  93. package/lib/esm/json/JsonObjectStore.mjs +2 -1
  94. package/lib/esm/json/JsonStoreFs.js.map +1 -1
  95. package/lib/esm/json/JsonStoreFs.mjs +84 -18
  96. package/lib/esm/models/TChain.js.map +1 -0
  97. package/lib/esm/models/TChain.mjs +3 -0
  98. package/lib/esm/models/TExplorer.js.map +1 -0
  99. package/lib/esm/models/TExplorer.mjs +3 -0
  100. package/lib/esm/models/TPlatform.js.map +1 -1
  101. package/lib/esm/models/TPlatform.mjs +1 -0
  102. package/lib/esm/rpc/transports/ITransport.js.map +1 -1
  103. package/lib/esm/rpc/transports/RpcTransport.js.map +1 -1
  104. package/lib/esm/rpc/transports/RpcTransport.mjs +4 -0
  105. package/lib/esm/rpc/transports/compatibility/EIP1193Transport.js.map +1 -1
  106. package/lib/esm/rpc/transports/compatibility/EIP1193Transport.mjs +14 -2
  107. package/lib/esm/rpc/transports/compatibility/IEIP6963Provider.js.map +1 -0
  108. package/lib/esm/rpc/transports/compatibility/IEIP6963Provider.mjs +3 -0
  109. package/lib/esm/safe/GnosisSafeHandler.js.map +1 -1
  110. package/lib/esm/tokens/TokenProviders/TPChain.js.map +1 -1
  111. package/lib/esm/tokens/TokenProviders/TPChain.mjs +5 -1
  112. package/lib/esm/txs/TxWriter.js.map +1 -1
  113. package/lib/esm/txs/TxWriter.mjs +8 -1
  114. package/lib/esm/utils/$buffer.js.map +1 -1
  115. package/lib/esm/utils/$config.js.map +1 -1
  116. package/lib/esm/utils/$config.mjs +64 -2
  117. package/lib/esm/utils/$dependency.js.map +1 -1
  118. package/lib/esm/utils/$dependency.mjs +4 -0
  119. package/lib/esm/utils/$path.js.map +1 -1
  120. package/lib/esm/utils/$path.mjs +9 -0
  121. package/lib/esm/utils/$ref.js.map +1 -0
  122. package/lib/esm/utils/$ref.mjs +27 -0
  123. package/lib/esm/utils/$secret.js.map +1 -1
  124. package/lib/esm/utils/$secret.mjs +15 -2
  125. package/lib/esm/utils/$sig.js.map +1 -1
  126. package/lib/esm/utils/$sig.mjs +18 -9
  127. package/lib/esm/wallets/EIP6963ProviderFactory.js.map +1 -0
  128. package/lib/esm/wallets/EIP6963ProviderFactory.mjs +185 -0
  129. package/lib/esm/wallets/Signer.js.map +1 -0
  130. package/lib/esm/wallets/Signer.mjs +26 -0
  131. package/lib/types/clients/ClientPool.d.ts +13 -5
  132. package/lib/types/clients/WalletClient.d.ts +28 -0
  133. package/lib/types/clients/Web3Client.d.ts +7 -4
  134. package/lib/types/clients/Web3ClientFactory.d.ts +1 -1
  135. package/lib/types/clients/interfaces/IWeb3Client.d.ts +2 -5
  136. package/lib/types/config/AConfigBase.d.ts +0 -7
  137. package/lib/types/config/interface/IConfigData.d.ts +5 -2
  138. package/lib/types/explorer/BlockchainExplorer.d.ts +7 -4
  139. package/lib/types/explorer/BlockchainExplorerFactory.d.ts +7 -3
  140. package/lib/types/explorer/verifiers/FsHtmlVerifier.d.ts +3 -3
  141. package/lib/types/json/JsonArrayMultiStore.d.ts +2 -2
  142. package/lib/types/json/JsonArrayStore.d.ts +4 -3
  143. package/lib/types/json/JsonObjectStore.d.ts +5 -3
  144. package/lib/types/json/JsonStoreFs.d.ts +3 -2
  145. package/lib/types/models/TAccount.d.ts +9 -0
  146. package/lib/types/models/TChain.d.ts +45 -0
  147. package/lib/types/models/TEth.d.ts +8 -0
  148. package/lib/types/models/TExplorer.d.ts +31 -0
  149. package/lib/types/rpc/transports/ITransport.d.ts +2 -1
  150. package/lib/types/rpc/transports/compatibility/EIP1193Transport.d.ts +5 -1
  151. package/lib/types/rpc/transports/compatibility/IEIP6963Provider.d.ts +21 -0
  152. package/lib/types/utils/$config.d.ts +5 -0
  153. package/lib/types/utils/$ref.d.ts +11 -0
  154. package/lib/types/utils/$secret.d.ts +1 -0
  155. package/lib/types/utils/$sig.d.ts +1 -0
  156. package/lib/types/wallets/EIP6963ProviderFactory.d.ts +47 -0
  157. package/lib/types/wallets/Signer.d.ts +15 -0
  158. package/package.json +1 -1
  159. package/src/clients/ClientPool.ts +49 -15
  160. package/src/clients/WalletClient.ts +115 -0
  161. package/src/clients/Web3Client.ts +47 -16
  162. package/src/clients/Web3ClientFactory.ts +8 -39
  163. package/src/clients/interfaces/IWeb3Client.ts +2 -7
  164. package/src/config/AConfigBase.ts +3 -11
  165. package/src/config/Config.ts +6 -1
  166. package/src/config/ConfigDefaults.ts +3 -6
  167. package/src/config/ConfigProvider.ts +5 -1
  168. package/src/config/interface/IConfigData.ts +9 -4
  169. package/src/contracts/ContractWriter.ts +1 -0
  170. package/src/contracts/deploy/storage/DeploymentsStorage.ts +13 -3
  171. package/src/explorer/BlockchainExplorer.ts +82 -51
  172. package/src/explorer/BlockchainExplorerFactory.ts +19 -6
  173. package/src/explorer/verifiers/FsHtmlVerifier.ts +8 -8
  174. package/src/json/JsonArrayMultiStore.ts +2 -2
  175. package/src/json/JsonArrayStore.ts +5 -2
  176. package/src/json/JsonObjectStore.ts +7 -4
  177. package/src/json/JsonStoreFs.ts +103 -18
  178. package/src/models/TAccount.ts +10 -2
  179. package/src/models/TChain.ts +56 -0
  180. package/src/models/TEth.ts +9 -3
  181. package/src/models/TExplorer.ts +35 -0
  182. package/src/models/TPlatform.ts +1 -0
  183. package/src/rpc/transports/ITransport.ts +2 -1
  184. package/src/rpc/transports/RpcTransport.ts +4 -0
  185. package/src/rpc/transports/compatibility/EIP1193Transport.ts +19 -6
  186. package/src/rpc/transports/compatibility/IEIP6963Provider.ts +22 -0
  187. package/src/safe/GnosisSafeHandler.ts +1 -1
  188. package/src/tokens/TokenProviders/TPChain.ts +5 -1
  189. package/src/txs/TxWriter.ts +8 -2
  190. package/src/utils/$buffer.ts +1 -1
  191. package/src/utils/$config.ts +73 -2
  192. package/src/utils/$dependency.ts +5 -0
  193. package/src/utils/$path.ts +9 -0
  194. package/src/utils/$ref.ts +40 -0
  195. package/src/utils/$secret.ts +16 -3
  196. package/src/utils/$sig.ts +19 -10
  197. package/src/wallets/EIP6963ProviderFactory.ts +252 -0
  198. package/src/wallets/Signer.ts +32 -0
  199. package/lib/cjs/wallets/WalletBsc.js +0 -19
  200. package/lib/cjs/wallets/WalletBsc.js.map +0 -1
  201. package/lib/esm/wallets/WalletBsc.js.map +0 -1
  202. package/lib/esm/wallets/WalletBsc.mjs +0 -12
  203. package/lib/types/wallets/WalletBsc.d.ts +0 -10
  204. package/src/wallets/WalletBsc.ts +0 -13
@@ -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 id = await $machine.id();
11
- return `${id}:${pin}`;
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
- return $rpc.signTypedData(rpc, typedData, account);
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
- return $rpc.sign(rpc, message, account);
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 rpc = 'getRpc' in mix
40
- ? await mix.getRpc()
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
- $require.notNull(rpc, `The account has no private key locally, and the RPC handler is not provided`);
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
- }
@@ -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
- }