@hashgraph/hedera-wallet-connect 2.0.1-canary.240df79.0 → 2.0.1-canary.25d7f01.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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/dapp/DAppSigner.d.ts +2 -0
- package/dist/lib/dapp/DAppSigner.js +16 -2
- package/dist/lib/dapp/index.d.ts +7 -5
- package/dist/lib/dapp/index.js +36 -4
- package/dist/lib/shared/accountInfo.d.ts +30 -0
- package/dist/lib/shared/accountInfo.js +1 -0
- package/dist/lib/shared/index.d.ts +2 -0
- package/dist/lib/shared/index.js +2 -0
- package/dist/lib/shared/mirrorNode.d.ts +3 -0
- package/dist/lib/shared/mirrorNode.js +17 -0
- package/dist/lib/shared/payloads.d.ts +1 -1
- package/dist/lib/shared/utils.d.ts +1 -2
- package/dist/lib/shared/utils.js +2 -3
- package/dist/reown/adapter.d.ts +37 -0
- package/dist/reown/adapter.js +255 -0
- package/dist/reown/connectors/HederaConnector.d.ts +29 -0
- package/dist/reown/connectors/HederaConnector.js +35 -0
- package/dist/reown/connectors/index.d.ts +1 -0
- package/dist/reown/connectors/index.js +1 -0
- package/dist/reown/index.d.ts +4 -0
- package/dist/reown/index.js +4 -0
- package/dist/reown/providers/EIP155Provider.d.ts +33 -0
- package/dist/reown/providers/EIP155Provider.js +187 -0
- package/dist/reown/providers/HIP820Provider.d.ts +26 -0
- package/dist/reown/providers/HIP820Provider.js +69 -0
- package/dist/reown/providers/HederaProvider.d.ts +164 -0
- package/dist/reown/providers/HederaProvider.js +471 -0
- package/dist/reown/providers/index.d.ts +3 -0
- package/dist/reown/providers/index.js +3 -0
- package/dist/reown/utils/chains.d.ts +18 -0
- package/dist/reown/utils/chains.js +152 -0
- package/dist/reown/utils/constants.d.ts +16 -0
- package/dist/reown/utils/constants.js +18 -0
- package/dist/reown/utils/helpers.d.ts +12 -0
- package/dist/reown/utils/helpers.js +25 -0
- package/dist/reown/utils/index.d.ts +4 -0
- package/dist/reown/utils/index.js +4 -0
- package/dist/reown/utils/types.d.ts +9 -0
- package/dist/reown/utils/types.js +1 -0
- package/dist/reown/wallets/EIP155Wallet.d.ts +46 -0
- package/dist/reown/wallets/EIP155Wallet.js +124 -0
- package/dist/reown/wallets/HIP820Wallet.d.ts +53 -0
- package/dist/reown/wallets/HIP820Wallet.js +236 -0
- package/dist/reown/wallets/index.d.ts +2 -0
- package/dist/reown/wallets/index.js +2 -0
- package/package.json +5 -3
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -8,6 +8,7 @@ export declare class DAppSigner implements Signer {
|
|
8
8
|
private readonly ledgerId;
|
9
9
|
readonly extensionId?: string | undefined;
|
10
10
|
private logger;
|
11
|
+
private publicKey;
|
11
12
|
constructor(accountId: AccountId, signClient: ISignClient, topic: string, ledgerId?: LedgerId, extensionId?: string | undefined, logLevel?: LogLevel);
|
12
13
|
/**
|
13
14
|
* Sets the logging level for the DAppSigner
|
@@ -22,6 +23,7 @@ export declare class DAppSigner implements Signer {
|
|
22
23
|
}): Promise<T>;
|
23
24
|
getAccountId(): AccountId;
|
24
25
|
getAccountKey(): Key;
|
26
|
+
getAccountKeyAsync(): Promise<Key | null>;
|
25
27
|
getLedgerId(): LedgerId;
|
26
28
|
getNetwork(): {
|
27
29
|
[key: string]: string | AccountId;
|
@@ -19,7 +19,7 @@
|
|
19
19
|
*/
|
20
20
|
import { AccountBalance, AccountInfo, LedgerId, SignerSignature, Transaction, TransactionRecord, Client, PublicKey, TransactionId, TransactionResponse, Query, AccountRecordsQuery, AccountInfoQuery, AccountBalanceQuery, TransactionReceiptQuery, TransactionReceipt, TransactionRecordQuery, } from '@hashgraph/sdk';
|
21
21
|
import { proto } from '@hashgraph/proto';
|
22
|
-
import { HederaJsonRpcMethod, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, Uint8ArrayToBase64String, Uint8ArrayToString, } from '../shared';
|
22
|
+
import { HederaJsonRpcMethod, base64StringToSignatureMap, base64StringToUint8Array, ledgerIdToCAIPChainId, queryToBase64String, transactionBodyToBase64String, transactionToBase64String, transactionToTransactionBody, extensionOpen, Uint8ArrayToBase64String, Uint8ArrayToString, getAccountInfo, } from '../shared';
|
23
23
|
import { DefaultLogger } from '../shared/logger';
|
24
24
|
import { SessionNotFoundError } from './SessionNotFoundError';
|
25
25
|
const clients = {};
|
@@ -31,6 +31,11 @@ export class DAppSigner {
|
|
31
31
|
this.ledgerId = ledgerId;
|
32
32
|
this.extensionId = extensionId;
|
33
33
|
this.logger = new DefaultLogger(logLevel);
|
34
|
+
this.publicKey = null;
|
35
|
+
// cache public key from mirror node
|
36
|
+
this.getAccountKeyAsync()
|
37
|
+
.then((key) => (this.publicKey = key))
|
38
|
+
.catch((error) => this.logger.error('Error when receiving a public key:', error.message));
|
34
39
|
}
|
35
40
|
/**
|
36
41
|
* Sets the logging level for the DAppSigner
|
@@ -79,7 +84,16 @@ export class DAppSigner {
|
|
79
84
|
return this.accountId;
|
80
85
|
}
|
81
86
|
getAccountKey() {
|
82
|
-
|
87
|
+
if (this.publicKey == null) {
|
88
|
+
throw new Error('No key was received from the mirror node');
|
89
|
+
}
|
90
|
+
return this.publicKey;
|
91
|
+
}
|
92
|
+
async getAccountKeyAsync() {
|
93
|
+
const accountInfo = await getAccountInfo(this.ledgerId, this.accountId.toString());
|
94
|
+
if (!(accountInfo === null || accountInfo === void 0 ? void 0 : accountInfo.key))
|
95
|
+
return null;
|
96
|
+
return PublicKey.fromString(accountInfo.key.key);
|
83
97
|
}
|
84
98
|
getLedgerId() {
|
85
99
|
return this.ledgerId;
|
package/dist/lib/dapp/index.d.ts
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
import { AccountId, LedgerId, Transaction } from '@hashgraph/sdk';
|
2
|
-
import { SessionTypes, SignClientTypes } from '@walletconnect/types';
|
2
|
+
import { EngineTypes, SessionTypes, SignClientTypes } from '@walletconnect/types';
|
3
3
|
import { WalletConnectModal } from '@walletconnect/modal';
|
4
4
|
import SignClient from '@walletconnect/sign-client';
|
5
5
|
import { LogLevel } from '../shared/logger';
|
6
6
|
import { GetNodeAddressesResult, ExecuteTransactionParams, ExecuteTransactionResult, SignMessageParams, SignMessageResult, SignAndExecuteQueryResult, SignAndExecuteQueryParams, SignAndExecuteTransactionParams, SignAndExecuteTransactionResult, SignTransactionParams, SignTransactionResult, ExtensionData } from '../shared';
|
7
7
|
import { DAppSigner } from './DAppSigner';
|
8
|
+
import { JsonRpcResult } from '@walletconnect/jsonrpc-types';
|
8
9
|
export * from './DAppSigner';
|
9
10
|
export { SessionNotFoundError } from './SessionNotFoundError';
|
10
11
|
type BaseLogger = 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'fatal';
|
11
12
|
export declare class DAppConnector {
|
12
13
|
private logger;
|
14
|
+
readonly storagePrefix = "hedera-wc/dapp-connector/";
|
13
15
|
dAppMetadata: SignClientTypes.Metadata;
|
14
16
|
network: LedgerId;
|
15
17
|
projectId: string;
|
@@ -106,7 +108,7 @@ export declare class DAppConnector {
|
|
106
108
|
private createSigners;
|
107
109
|
private onSessionConnected;
|
108
110
|
private connectURI;
|
109
|
-
|
111
|
+
request<Req extends EngineTypes.RequestParams, Res extends JsonRpcResult>({ method, params, }: Req['request']): Promise<Res>;
|
110
112
|
/**
|
111
113
|
* Retrieves the node addresses associated with the current Hedera network.
|
112
114
|
*
|
@@ -192,9 +194,7 @@ export declare class DAppConnector {
|
|
192
194
|
*
|
193
195
|
* @param {SignTransactionParams} params - The parameters of type {@link SignTransactionParams | `SignTransactionParams`} required for `Transaction` signing.
|
194
196
|
* @param {string} params.signerAccountId - a signer Hedera Account identifier in {@link https://hips.hedera.com/hip/hip-30 | HIP-30} (`<nework>:<shard>.<realm>.<num>`) form.
|
195
|
-
* @param {Transaction
|
196
|
-
* HIP-820 calls for a base64 encoded proto.TransactionBody and many wallets support a serialized Transaction object generated by the Hedera Javascript SDK.
|
197
|
-
* Both options are supported here for backwards compatibility.
|
197
|
+
* @param {Transaction} params.transactionBody - a Transaction object built with the @hashgraph/sdk
|
198
198
|
* @returns Promise\<{@link SignTransactionResult}\>
|
199
199
|
* @example
|
200
200
|
* ```ts
|
@@ -212,5 +212,7 @@ export declare class DAppConnector {
|
|
212
212
|
private handleSessionUpdate;
|
213
213
|
private handleSessionDelete;
|
214
214
|
private handlePairingDelete;
|
215
|
+
private handleRelayConnected;
|
216
|
+
private verifyLastConnectedInstance;
|
215
217
|
}
|
216
218
|
export default DAppConnector;
|
package/dist/lib/dapp/index.js
CHANGED
@@ -20,7 +20,8 @@
|
|
20
20
|
import { LedgerId, Transaction } from '@hashgraph/sdk';
|
21
21
|
import { WalletConnectModal } from '@walletconnect/modal';
|
22
22
|
import SignClient from '@walletconnect/sign-client';
|
23
|
-
import { getSdkError } from '@walletconnect/utils';
|
23
|
+
import { getSdkError, isOnline } from '@walletconnect/utils';
|
24
|
+
import { RELAYER_EVENTS } from '@walletconnect/core';
|
24
25
|
import { DefaultLogger } from '../shared/logger';
|
25
26
|
import { HederaJsonRpcMethod, accountAndLedgerFromSession, networkNamespaces, extensionConnect, findExtensions, } from '../shared';
|
26
27
|
import { DAppSigner } from './DAppSigner';
|
@@ -38,6 +39,7 @@ export class DAppConnector {
|
|
38
39
|
* @param logLevel - Logging level for the DAppConnector (optional).
|
39
40
|
*/
|
40
41
|
constructor(metadata, network, projectId, methods, events, chains, logLevel = 'debug') {
|
42
|
+
this.storagePrefix = 'hedera-wc/dapp-connector/';
|
41
43
|
this.network = LedgerId.TESTNET;
|
42
44
|
this.supportedMethods = [];
|
43
45
|
this.supportedEvents = [];
|
@@ -110,6 +112,9 @@ export class DAppConnector {
|
|
110
112
|
this.signers = existingSessions.flatMap((session) => this.createSigners(session));
|
111
113
|
else
|
112
114
|
this.checkIframeConnect();
|
115
|
+
//manual call after init before relayer connect event handler is attached
|
116
|
+
this.handleRelayConnected();
|
117
|
+
this.walletConnectClient.core.relayer.on(RELAYER_EVENTS.connect, this.handleRelayConnected.bind(this));
|
113
118
|
this.walletConnectClient.on('session_event', this.handleSessionEvent.bind(this));
|
114
119
|
this.walletConnectClient.on('session_update', this.handleSessionUpdate.bind(this));
|
115
120
|
this.walletConnectClient.on('session_delete', this.handleSessionDelete.bind(this));
|
@@ -379,6 +384,7 @@ export class DAppConnector {
|
|
379
384
|
if (!signer) {
|
380
385
|
throw new Error('There is no active session. Connect to the wallet at first.');
|
381
386
|
}
|
387
|
+
await this.verifyLastConnectedInstance();
|
382
388
|
this.logger.debug(`Using signer: ${signer.getAccountId().toString()}: ${signer.topic} - about to request.`);
|
383
389
|
return await signer.request({
|
384
390
|
method: method,
|
@@ -495,9 +501,7 @@ export class DAppConnector {
|
|
495
501
|
*
|
496
502
|
* @param {SignTransactionParams} params - The parameters of type {@link SignTransactionParams | `SignTransactionParams`} required for `Transaction` signing.
|
497
503
|
* @param {string} params.signerAccountId - a signer Hedera Account identifier in {@link https://hips.hedera.com/hip/hip-30 | HIP-30} (`<nework>:<shard>.<realm>.<num>`) form.
|
498
|
-
* @param {Transaction
|
499
|
-
* HIP-820 calls for a base64 encoded proto.TransactionBody and many wallets support a serialized Transaction object generated by the Hedera Javascript SDK.
|
500
|
-
* Both options are supported here for backwards compatibility.
|
504
|
+
* @param {Transaction} params.transactionBody - a Transaction object built with the @hashgraph/sdk
|
501
505
|
* @returns Promise\<{@link SignTransactionResult}\>
|
502
506
|
* @example
|
503
507
|
* ```ts
|
@@ -576,5 +580,33 @@ export class DAppConnector {
|
|
576
580
|
}
|
577
581
|
this.logger.info('Pairing deleted by wallet');
|
578
582
|
}
|
583
|
+
// Store the last connected randomSessionIdentifier
|
584
|
+
async handleRelayConnected() {
|
585
|
+
if (!this.walletConnectClient) {
|
586
|
+
this.logger.error('walletConnectClient not found');
|
587
|
+
return;
|
588
|
+
}
|
589
|
+
const core = this.walletConnectClient.core;
|
590
|
+
const instanceId = core.crypto.randomSessionIdentifier;
|
591
|
+
await core.storage.setItem(this.storagePrefix + 'last-connected-instance', instanceId);
|
592
|
+
}
|
593
|
+
// In the event of another tab being connected after the current one,
|
594
|
+
// the current tab will be forcibly reconnected to the relay so that
|
595
|
+
// a response to the request can be received.
|
596
|
+
// https://github.com/hashgraph/hedera-wallet-connect/issues/387
|
597
|
+
async verifyLastConnectedInstance() {
|
598
|
+
if (!this.walletConnectClient) {
|
599
|
+
this.logger.error('walletConnectClient not found');
|
600
|
+
return;
|
601
|
+
}
|
602
|
+
const core = this.walletConnectClient.core;
|
603
|
+
const instanceId = core.crypto.randomSessionIdentifier;
|
604
|
+
const isOnlineStatus = await isOnline();
|
605
|
+
const lastConnectedInstanceId = await core.storage.getItem(this.storagePrefix + 'last-connected-instance');
|
606
|
+
if (lastConnectedInstanceId != instanceId && isOnlineStatus) {
|
607
|
+
this.logger.info('Forced reconnecting to the relay');
|
608
|
+
await core.relayer.restartTransport();
|
609
|
+
}
|
610
|
+
}
|
579
611
|
}
|
580
612
|
export default DAppConnector;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
export interface AccountInfo {
|
2
|
+
account: string;
|
3
|
+
alias: string;
|
4
|
+
auto_renew_period: number;
|
5
|
+
balance: Balance;
|
6
|
+
created_timestamp: string;
|
7
|
+
decline_reward: boolean;
|
8
|
+
deleted: boolean;
|
9
|
+
ethereum_nonce: number;
|
10
|
+
evm_address: string;
|
11
|
+
expiry_timestamp: string;
|
12
|
+
key: Key | null;
|
13
|
+
max_automatic_token_associations: number;
|
14
|
+
memo: string;
|
15
|
+
pending_reward: number;
|
16
|
+
receiver_sig_required: boolean;
|
17
|
+
}
|
18
|
+
export interface Balance {
|
19
|
+
balance: number;
|
20
|
+
timestamp: string;
|
21
|
+
tokens: Token[];
|
22
|
+
}
|
23
|
+
export interface Token {
|
24
|
+
token_id: string;
|
25
|
+
balance: number;
|
26
|
+
}
|
27
|
+
export interface Key {
|
28
|
+
_type: string;
|
29
|
+
key: string;
|
30
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/dist/lib/shared/index.js
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
function getMirrorNodeUrl(ledgerId) {
|
2
|
+
return `https://${ledgerId.toString()}.mirrornode.hedera.com`;
|
3
|
+
}
|
4
|
+
export async function getAccountInfo(ledgerId, address) {
|
5
|
+
const mirrorNodeApiUrl = getMirrorNodeUrl(ledgerId);
|
6
|
+
const url = `${mirrorNodeApiUrl}/api/v1/accounts/${address}`;
|
7
|
+
const result = await fetch(url, {
|
8
|
+
headers: {
|
9
|
+
accept: 'application/json',
|
10
|
+
},
|
11
|
+
});
|
12
|
+
if (result.status !== 200) {
|
13
|
+
return null;
|
14
|
+
}
|
15
|
+
const response = await result.json();
|
16
|
+
return response;
|
17
|
+
}
|
@@ -84,7 +84,7 @@ export interface SignAndExecuteTransactionResponse extends EngineTypes.RespondPa
|
|
84
84
|
}
|
85
85
|
export interface SignTransactionParams {
|
86
86
|
signerAccountId: string;
|
87
|
-
transactionBody: Transaction
|
87
|
+
transactionBody: Transaction;
|
88
88
|
}
|
89
89
|
export interface SignTransactionRequest extends EngineTypes.RequestParams {
|
90
90
|
request: {
|
@@ -29,10 +29,9 @@ export declare function transactionToBase64String<T extends Transaction>(transac
|
|
29
29
|
export declare function base64StringToTransaction<T extends Transaction>(transactionBytes: string): T;
|
30
30
|
/**
|
31
31
|
* @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
|
32
|
-
* @param nodeAccountId - an optional `AccountId` to set the node account ID for the transaction
|
33
32
|
* @returns `string`
|
34
33
|
* */
|
35
|
-
export declare function transactionToTransactionBody<T extends Transaction>(transaction: T
|
34
|
+
export declare function transactionToTransactionBody<T extends Transaction>(transaction: T): any;
|
36
35
|
export declare function transactionBodyToBase64String(transactionBody: proto.ITransactionBody): string;
|
37
36
|
/**
|
38
37
|
* @param transactionList - a proto.TransactionList object
|
package/dist/lib/shared/utils.js
CHANGED
@@ -54,13 +54,12 @@ export function base64StringToTransaction(transactionBytes) {
|
|
54
54
|
}
|
55
55
|
/**
|
56
56
|
* @param transaction - a base64 encoded string of proto.TransactionBody.encode().finish()
|
57
|
-
* @param nodeAccountId - an optional `AccountId` to set the node account ID for the transaction
|
58
57
|
* @returns `string`
|
59
58
|
* */
|
60
|
-
export function transactionToTransactionBody(transaction
|
59
|
+
export function transactionToTransactionBody(transaction) {
|
61
60
|
// This is a private function, though provides the capabilities to construct a proto.TransactionBody
|
62
61
|
//@ts-ignore
|
63
|
-
return transaction._makeTransactionBody(
|
62
|
+
return transaction._makeTransactionBody(null);
|
64
63
|
}
|
65
64
|
export function transactionBodyToBase64String(transactionBody) {
|
66
65
|
return Uint8ArrayToBase64String(proto.TransactionBody.encode(transactionBody).finish());
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { type ChainNamespace } from '@reown/appkit-common';
|
2
|
+
import { AdapterBlueprint } from '@reown/appkit/adapters';
|
3
|
+
type UniversalProvider = Parameters<AdapterBlueprint['setUniversalProvider']>[0];
|
4
|
+
export declare class HederaAdapter extends AdapterBlueprint {
|
5
|
+
constructor(params: AdapterBlueprint.Params);
|
6
|
+
setUniversalProvider(universalProvider: UniversalProvider): void;
|
7
|
+
connect(params: AdapterBlueprint.ConnectParams): Promise<AdapterBlueprint.ConnectResult>;
|
8
|
+
disconnect(): Promise<void>;
|
9
|
+
getAccounts({ namespace, }: AdapterBlueprint.GetAccountsParams & {
|
10
|
+
namespace: ChainNamespace;
|
11
|
+
}): Promise<AdapterBlueprint.GetAccountsResult>;
|
12
|
+
syncConnectors(): Promise<void>;
|
13
|
+
getBalance(params: AdapterBlueprint.GetBalanceParams): Promise<AdapterBlueprint.GetBalanceResult>;
|
14
|
+
signMessage(params: AdapterBlueprint.SignMessageParams): Promise<AdapterBlueprint.SignMessageResult>;
|
15
|
+
estimateGas(params: AdapterBlueprint.EstimateGasTransactionArgs): Promise<AdapterBlueprint.EstimateGasTransactionResult>;
|
16
|
+
sendTransaction(params: AdapterBlueprint.SendTransactionParams): Promise<AdapterBlueprint.SendTransactionResult>;
|
17
|
+
writeContract(params: AdapterBlueprint.WriteContractParams): Promise<AdapterBlueprint.WriteContractResult>;
|
18
|
+
getEnsAddress(params: AdapterBlueprint.GetEnsAddressParams): Promise<AdapterBlueprint.GetEnsAddressResult>;
|
19
|
+
parseUnits(params: AdapterBlueprint.ParseUnitsParams): AdapterBlueprint.ParseUnitsResult;
|
20
|
+
formatUnits(params: AdapterBlueprint.FormatUnitsParams): AdapterBlueprint.FormatUnitsResult;
|
21
|
+
getCapabilities(params: AdapterBlueprint.GetCapabilitiesParams): Promise<unknown>;
|
22
|
+
getProfile(): Promise<AdapterBlueprint.GetProfileResult>;
|
23
|
+
grantPermissions(): Promise<unknown>;
|
24
|
+
revokePermissions(): Promise<`0x${string}`>;
|
25
|
+
syncConnection(params: AdapterBlueprint.SyncConnectionParams): Promise<{
|
26
|
+
id: string;
|
27
|
+
type: "WALLET_CONNECT";
|
28
|
+
chainId: string | number;
|
29
|
+
provider: UniversalProvider;
|
30
|
+
address: string;
|
31
|
+
}>;
|
32
|
+
switchNetwork(params: AdapterBlueprint.SwitchNetworkParams): Promise<void>;
|
33
|
+
protected getWalletConnectConnector(): ReturnType<AdapterBlueprint['getWalletConnectConnector']>;
|
34
|
+
getWalletConnectProvider(): UniversalProvider;
|
35
|
+
walletGetAssets(_params: AdapterBlueprint.WalletGetAssetsParams): Promise<AdapterBlueprint.WalletGetAssetsResponse>;
|
36
|
+
}
|
37
|
+
export {};
|
@@ -0,0 +1,255 @@
|
|
1
|
+
import { CoreHelperUtil, WcHelpersUtil } from '@reown/appkit';
|
2
|
+
import { isReownName } from '@reown/appkit-common';
|
3
|
+
import { AdapterBlueprint } from '@reown/appkit/adapters';
|
4
|
+
import { ProviderUtil } from '@reown/appkit/store';
|
5
|
+
import { LedgerId } from '@hashgraph/sdk';
|
6
|
+
import { BrowserProvider, Contract, formatUnits, JsonRpcSigner, parseUnits } from 'ethers';
|
7
|
+
import { HederaConnector } from './connectors';
|
8
|
+
import { hederaNamespace } from './utils';
|
9
|
+
import { getAccountInfo } from '..';
|
10
|
+
export class HederaAdapter extends AdapterBlueprint {
|
11
|
+
constructor(params) {
|
12
|
+
var _a, _b;
|
13
|
+
if (params.namespace !== hederaNamespace && params.namespace !== 'eip155') {
|
14
|
+
throw new Error('Namespace must be "hedera" or "eip155"');
|
15
|
+
}
|
16
|
+
if (params.namespace == 'eip155') {
|
17
|
+
if ((_a = params.networks) === null || _a === void 0 ? void 0 : _a.some((n) => n.chainNamespace != 'eip155')) {
|
18
|
+
throw new Error('Invalid networks for eip155 namespace');
|
19
|
+
}
|
20
|
+
}
|
21
|
+
else {
|
22
|
+
if ((_b = params.networks) === null || _b === void 0 ? void 0 : _b.some((n) => n.chainNamespace != hederaNamespace)) {
|
23
|
+
throw new Error('Invalid networks for hedera namespace');
|
24
|
+
}
|
25
|
+
}
|
26
|
+
super(Object.assign({}, params));
|
27
|
+
}
|
28
|
+
setUniversalProvider(universalProvider) {
|
29
|
+
this.addConnector(new HederaConnector({
|
30
|
+
provider: universalProvider,
|
31
|
+
caipNetworks: this.getCaipNetworks() || [],
|
32
|
+
namespace: this.namespace,
|
33
|
+
}));
|
34
|
+
}
|
35
|
+
async connect(params) {
|
36
|
+
return Promise.resolve({
|
37
|
+
id: 'WALLET_CONNECT',
|
38
|
+
type: 'WALLET_CONNECT',
|
39
|
+
chainId: Number(params.chainId),
|
40
|
+
provider: this.provider,
|
41
|
+
address: '',
|
42
|
+
});
|
43
|
+
}
|
44
|
+
async disconnect() {
|
45
|
+
try {
|
46
|
+
const connector = this.getWalletConnectConnector();
|
47
|
+
await connector.disconnect();
|
48
|
+
}
|
49
|
+
catch (error) {
|
50
|
+
console.warn('UniversalAdapter:disconnect - error', error);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
async getAccounts({ namespace, }) {
|
54
|
+
var _a, _b, _c, _d;
|
55
|
+
const provider = this.provider;
|
56
|
+
const addresses = (((_d = (_c = (_b = (_a = provider === null || provider === void 0 ? void 0 : provider.session) === null || _a === void 0 ? void 0 : _a.namespaces) === null || _b === void 0 ? void 0 : _b[namespace]) === null || _c === void 0 ? void 0 : _c.accounts) === null || _d === void 0 ? void 0 : _d.map((account) => {
|
57
|
+
const [, , address] = account.split(':');
|
58
|
+
return address;
|
59
|
+
}).filter((address, index, self) => self.indexOf(address) === index)) || []);
|
60
|
+
return Promise.resolve({
|
61
|
+
accounts: addresses.map((address) => CoreHelperUtil.createAccount(namespace, address, 'eoa')),
|
62
|
+
});
|
63
|
+
}
|
64
|
+
async syncConnectors() {
|
65
|
+
return Promise.resolve();
|
66
|
+
}
|
67
|
+
async getBalance(params) {
|
68
|
+
const { address, caipNetwork } = params;
|
69
|
+
if (!caipNetwork) {
|
70
|
+
return Promise.resolve({
|
71
|
+
balance: '0',
|
72
|
+
decimals: 0,
|
73
|
+
symbol: '',
|
74
|
+
});
|
75
|
+
}
|
76
|
+
const accountInfo = await getAccountInfo(caipNetwork.testnet ? LedgerId.TESTNET : LedgerId.MAINNET, address);
|
77
|
+
return Promise.resolve({
|
78
|
+
balance: (accountInfo === null || accountInfo === void 0 ? void 0 : accountInfo.balance)
|
79
|
+
? formatUnits(accountInfo.balance.balance, 8).toString()
|
80
|
+
: '0',
|
81
|
+
decimals: caipNetwork.nativeCurrency.decimals,
|
82
|
+
symbol: caipNetwork.nativeCurrency.symbol,
|
83
|
+
});
|
84
|
+
}
|
85
|
+
async signMessage(params) {
|
86
|
+
const { provider, message, address } = params;
|
87
|
+
if (!provider) {
|
88
|
+
throw new Error('Provider is undefined');
|
89
|
+
}
|
90
|
+
const hederaProvider = provider;
|
91
|
+
let signature = '';
|
92
|
+
if (this.namespace === hederaNamespace) {
|
93
|
+
const response = await hederaProvider.hedera_signMessage({
|
94
|
+
signerAccountId: address,
|
95
|
+
message,
|
96
|
+
});
|
97
|
+
signature = response.signatureMap;
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
signature = await hederaProvider.eth_signMessage(message, address);
|
101
|
+
}
|
102
|
+
return { signature };
|
103
|
+
}
|
104
|
+
async estimateGas(params) {
|
105
|
+
const { provider, caipNetwork, address } = params;
|
106
|
+
if (this.namespace !== 'eip155') {
|
107
|
+
throw new Error('Namespace is not eip155');
|
108
|
+
}
|
109
|
+
if (!provider) {
|
110
|
+
throw new Error('Provider is undefined');
|
111
|
+
}
|
112
|
+
const hederaProvider = provider;
|
113
|
+
const result = await hederaProvider.eth_estimateGas({
|
114
|
+
data: params.data,
|
115
|
+
to: params.to,
|
116
|
+
address: address,
|
117
|
+
}, address, Number(caipNetwork === null || caipNetwork === void 0 ? void 0 : caipNetwork.id));
|
118
|
+
return { gas: result };
|
119
|
+
}
|
120
|
+
async sendTransaction(params) {
|
121
|
+
var _a;
|
122
|
+
if (!params.provider) {
|
123
|
+
throw new Error('Provider is undefined');
|
124
|
+
}
|
125
|
+
const hederaProvider = params.provider;
|
126
|
+
if (this.namespace == 'eip155') {
|
127
|
+
const tx = await hederaProvider.eth_sendTransaction({
|
128
|
+
value: params.value,
|
129
|
+
to: params.to,
|
130
|
+
data: params.data,
|
131
|
+
gas: params.gas,
|
132
|
+
gasPrice: params.gasPrice,
|
133
|
+
address: params.address,
|
134
|
+
}, params.address, Number((_a = params.caipNetwork) === null || _a === void 0 ? void 0 : _a.id));
|
135
|
+
return { hash: tx };
|
136
|
+
}
|
137
|
+
else {
|
138
|
+
throw new Error('Namespace is not eip155');
|
139
|
+
}
|
140
|
+
}
|
141
|
+
async writeContract(params) {
|
142
|
+
if (!params.provider) {
|
143
|
+
throw new Error('Provider is undefined');
|
144
|
+
}
|
145
|
+
if (this.namespace !== 'eip155') {
|
146
|
+
throw new Error('Namespace is not eip155');
|
147
|
+
}
|
148
|
+
const { provider, caipNetwork, caipAddress, abi, tokenAddress, method, args } = params;
|
149
|
+
const browserProvider = new BrowserProvider(provider, Number(caipNetwork === null || caipNetwork === void 0 ? void 0 : caipNetwork.id));
|
150
|
+
const signer = new JsonRpcSigner(browserProvider, caipAddress);
|
151
|
+
const contract = new Contract(tokenAddress, abi, signer);
|
152
|
+
if (!contract || !method) {
|
153
|
+
throw new Error('Contract method is undefined');
|
154
|
+
}
|
155
|
+
const contractMethod = contract[method];
|
156
|
+
if (contractMethod) {
|
157
|
+
const result = await contractMethod(...args);
|
158
|
+
return { hash: result };
|
159
|
+
}
|
160
|
+
else
|
161
|
+
throw new Error('Contract method is undefined');
|
162
|
+
}
|
163
|
+
async getEnsAddress(params) {
|
164
|
+
if (this.namespace !== 'eip155') {
|
165
|
+
throw new Error('Namespace is not eip155');
|
166
|
+
}
|
167
|
+
const { name, caipNetwork } = params;
|
168
|
+
if (caipNetwork) {
|
169
|
+
if (isReownName(name)) {
|
170
|
+
return {
|
171
|
+
address: (await WcHelpersUtil.resolveReownName(name)) || false,
|
172
|
+
};
|
173
|
+
}
|
174
|
+
}
|
175
|
+
return { address: false };
|
176
|
+
}
|
177
|
+
parseUnits(params) {
|
178
|
+
return parseUnits(params.value, params.decimals);
|
179
|
+
}
|
180
|
+
formatUnits(params) {
|
181
|
+
return formatUnits(params.value, params.decimals);
|
182
|
+
}
|
183
|
+
async getCapabilities(params) {
|
184
|
+
var _a, _b;
|
185
|
+
if (this.namespace !== 'eip155') {
|
186
|
+
throw new Error('Namespace is not eip155');
|
187
|
+
}
|
188
|
+
const provider = ProviderUtil.getProvider('eip155');
|
189
|
+
if (!provider) {
|
190
|
+
throw new Error('Provider is undefined');
|
191
|
+
}
|
192
|
+
const walletCapabilitiesString = (_b = (_a = provider.session) === null || _a === void 0 ? void 0 : _a.sessionProperties) === null || _b === void 0 ? void 0 : _b['capabilities'];
|
193
|
+
if (walletCapabilitiesString) {
|
194
|
+
try {
|
195
|
+
const walletCapabilities = JSON.parse(walletCapabilitiesString);
|
196
|
+
const accountCapabilities = walletCapabilities[params];
|
197
|
+
if (accountCapabilities) {
|
198
|
+
return accountCapabilities;
|
199
|
+
}
|
200
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
201
|
+
}
|
202
|
+
catch (error) {
|
203
|
+
throw new Error('Error parsing wallet capabilities');
|
204
|
+
}
|
205
|
+
}
|
206
|
+
return await provider.request({
|
207
|
+
method: 'wallet_getCapabilities',
|
208
|
+
params: [params],
|
209
|
+
});
|
210
|
+
}
|
211
|
+
// Not supported
|
212
|
+
async getProfile() {
|
213
|
+
return Promise.resolve({
|
214
|
+
profileImage: '',
|
215
|
+
profileName: '',
|
216
|
+
});
|
217
|
+
}
|
218
|
+
// Not supported
|
219
|
+
async grantPermissions() {
|
220
|
+
return Promise.resolve({});
|
221
|
+
}
|
222
|
+
// Not supported
|
223
|
+
async revokePermissions() {
|
224
|
+
return Promise.resolve('0x');
|
225
|
+
}
|
226
|
+
async syncConnection(params) {
|
227
|
+
return Promise.resolve({
|
228
|
+
id: 'WALLET_CONNECT',
|
229
|
+
type: 'WALLET_CONNECT',
|
230
|
+
chainId: params.chainId,
|
231
|
+
provider: this.provider,
|
232
|
+
address: '',
|
233
|
+
});
|
234
|
+
}
|
235
|
+
async switchNetwork(params) {
|
236
|
+
const { caipNetwork } = params;
|
237
|
+
const connector = this.getWalletConnectConnector();
|
238
|
+
connector.provider.setDefaultChain(caipNetwork.caipNetworkId);
|
239
|
+
}
|
240
|
+
getWalletConnectConnector() {
|
241
|
+
const connector = this.connectors.find((c) => c.type == 'WALLET_CONNECT');
|
242
|
+
if (!connector) {
|
243
|
+
throw new Error('WalletConnectConnector not found');
|
244
|
+
}
|
245
|
+
return connector;
|
246
|
+
}
|
247
|
+
getWalletConnectProvider() {
|
248
|
+
const connector = this.connectors.find((c) => c.type === 'WALLET_CONNECT');
|
249
|
+
const provider = connector === null || connector === void 0 ? void 0 : connector.provider;
|
250
|
+
return provider;
|
251
|
+
}
|
252
|
+
async walletGetAssets(_params) {
|
253
|
+
return Promise.resolve({});
|
254
|
+
}
|
255
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import type { SessionTypes } from '@walletconnect/types';
|
2
|
+
import { CaipNetwork, ChainNamespace } from '@reown/appkit-common';
|
3
|
+
import { AdapterBlueprint, type ChainAdapterConnector } from '@reown/appkit/adapters';
|
4
|
+
type UniversalProvider = Parameters<AdapterBlueprint['setUniversalProvider']>[0];
|
5
|
+
export declare class HederaConnector implements ChainAdapterConnector {
|
6
|
+
readonly id: "walletConnect";
|
7
|
+
readonly name: string;
|
8
|
+
readonly type = "WALLET_CONNECT";
|
9
|
+
readonly imageId: string;
|
10
|
+
readonly chain: ChainNamespace;
|
11
|
+
provider: UniversalProvider;
|
12
|
+
protected caipNetworks: CaipNetwork[];
|
13
|
+
constructor({ provider, caipNetworks, namespace }: HederaConnector.Options);
|
14
|
+
get chains(): CaipNetwork[];
|
15
|
+
connectWalletConnect(): Promise<{
|
16
|
+
clientId: string;
|
17
|
+
session: SessionTypes.Struct;
|
18
|
+
}>;
|
19
|
+
disconnect(): Promise<void>;
|
20
|
+
authenticate(): Promise<boolean>;
|
21
|
+
}
|
22
|
+
export declare namespace HederaConnector {
|
23
|
+
type Options = {
|
24
|
+
provider: UniversalProvider;
|
25
|
+
caipNetworks: CaipNetwork[];
|
26
|
+
namespace: 'hedera' | 'eip155';
|
27
|
+
};
|
28
|
+
}
|
29
|
+
export {};
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { ConstantsUtil } from '@reown/appkit-common';
|
2
|
+
import { PresetsUtil } from '@reown/appkit-utils';
|
3
|
+
import { createNamespaces } from '../utils';
|
4
|
+
export class HederaConnector {
|
5
|
+
constructor({ provider, caipNetworks, namespace }) {
|
6
|
+
this.id = ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT;
|
7
|
+
this.name = PresetsUtil.ConnectorNamesMap[ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT];
|
8
|
+
this.type = 'WALLET_CONNECT';
|
9
|
+
this.imageId = PresetsUtil.ConnectorImageIds[ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT];
|
10
|
+
this.caipNetworks = caipNetworks;
|
11
|
+
this.provider = provider;
|
12
|
+
this.chain = namespace;
|
13
|
+
}
|
14
|
+
get chains() {
|
15
|
+
return this.caipNetworks;
|
16
|
+
}
|
17
|
+
async connectWalletConnect() {
|
18
|
+
const isAuthenticated = await this.authenticate();
|
19
|
+
if (!isAuthenticated) {
|
20
|
+
await this.provider.connect({
|
21
|
+
optionalNamespaces: createNamespaces(this.caipNetworks),
|
22
|
+
});
|
23
|
+
}
|
24
|
+
return {
|
25
|
+
clientId: await this.provider.client.core.crypto.getClientId(),
|
26
|
+
session: this.provider.session,
|
27
|
+
};
|
28
|
+
}
|
29
|
+
async disconnect() {
|
30
|
+
await this.provider.disconnect();
|
31
|
+
}
|
32
|
+
async authenticate() {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
}
|