Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CON-597 Add multi-node failover functionality to demo WalletConnect Hedera dApp and wallet #2

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@hashgraph/hedera-wallet-connect": "^1.4.2",
"@hashgraph/proto": "^2.15.0",
"@hashgraph/sdk": "^2.56.0",
"@reown/walletkit": "^1.1.2",
"@walletconnect/core": "^2.17.3",
Expand Down
57 changes: 43 additions & 14 deletions src/lib/hip820-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AccountId,
Transaction,
Query,
PrecheckStatusError,
} from "@hashgraph/sdk";

import {
Expand All @@ -14,28 +15,29 @@ import {
base64StringToQuery,
Uint8ArrayToBase64String,
stringToSignerMessage,
signatureMapToBase64String,
signerSignaturesToSignatureMap,
base64StringToTransaction,
getHederaError,
GetNodeAddressesResult,
ExecuteTransactionResult,
SignAndExecuteQueryResult,
SignMessageResult,
SignAndExecuteTransactionResult,
SignTransactionResult,
base64StringToTransaction,
signatureMapToBase64String,
} from "@hashgraph/hedera-wallet-connect";
import { proto } from "@hashgraph/proto";
import Provider from "@hashgraph/hedera-wallet-connect/dist/lib/wallet/provider";

import { RequestEventArgs } from "../types/common";
import {
formatJsonRpcError,
formatJsonRpcResult,
JsonRpcError,
JsonRpcResult,
} from "@walletconnect/jsonrpc-utils";

import { RequestEventArgs } from "../types/common";

interface IInitArgs {
chainId: HederaChainId;
accountId: AccountId | string;
Expand All @@ -57,7 +59,7 @@ export interface HIP820WalletInterface {
[HederaJsonRpcMethod.ExecuteTransaction](
id: number,
body: Transaction,
): Promise<ExecuteTransactionResult>;
): Promise<ExecuteTransactionResult | JsonRpcError>;
[HederaJsonRpcMethod.SignMessage](
id: number,
body: string,
Expand All @@ -69,7 +71,7 @@ export interface HIP820WalletInterface {
[HederaJsonRpcMethod.SignAndExecuteTransaction](
id: number,
body: Transaction,
): Promise<SignAndExecuteTransactionResult>;
): Promise<SignAndExecuteTransactionResult | JsonRpcError>;
[HederaJsonRpcMethod.SignTransaction](
id: number,
body: Uint8Array,
Expand Down Expand Up @@ -216,7 +218,7 @@ export class HIP820Wallet implements HIP820WalletInterface {

public async approveSessionRequest(
event: RequestEventArgs,
): Promise<JsonRpcResult<any>> {
): Promise<JsonRpcResult<any> | JsonRpcError> {
const { method, id, body } = this.parseSessionRequest(event);
const response = await this[method](id, body);
console.log({ response });
Expand Down Expand Up @@ -249,8 +251,18 @@ export class HIP820Wallet implements HIP820WalletInterface {
}

// 2. hedera_executeTransaction
public async hedera_executeTransaction(id: number, body: Transaction) {
return formatJsonRpcResult(id, (await this.wallet.call(body)).toJSON());
public async hedera_executeTransaction(id: number, signedTransaction: Transaction): Promise<ExecuteTransactionResult | JsonRpcError> {
try {
const response = await signedTransaction.executeWithSigner(this.wallet);
return formatJsonRpcResult(id, response.toJSON());
}
catch (e) {
if (e instanceof PrecheckStatusError) {
// HIP-820 error format
return formatJsonRpcError(id, { code: 9000, message: e.message, data: e.status._code.toString() })
}
return formatJsonRpcError(id, { code: 9000, message: "Unknown Error" })
}
}
// 3. hedera_signMessage
public async hedera_signMessage(id: number, body: string) {
Expand Down Expand Up @@ -293,12 +305,29 @@ export class HIP820Wallet implements HIP820WalletInterface {
}

// 5. hedera_signAndExecuteTransaction
public async hedera_signAndExecuteTransaction(id: number, body: Transaction) {
const signedTransaction = await this.wallet.signTransaction(body);
return formatJsonRpcResult(
id,
(await this.wallet.call(signedTransaction)).toJSON(),
);
public async hedera_signAndExecuteTransaction(id: number, transaction: Transaction) {
console.log({ inputTx: JSON.parse(JSON.stringify(transaction)) })
// check transaction is incomplete (HIP-745)
if (!transaction.isFrozen()) {
// set multiple nodeAccountIds and transactionId if not present
await transaction.freezeWithSigner(this.wallet);
}

const signedTransaction = await transaction.signWithSigner(this.wallet);
try {
const response = await signedTransaction.executeWithSigner(this.wallet);
return formatJsonRpcResult(
id,
response.toJSON(),
);
}
catch (e) {
if (e instanceof PrecheckStatusError) {
// HIP-820 error format
return formatJsonRpcError(id, { code: 9000, message: e.message, data: e.status._code.toString() })
}
return formatJsonRpcError(id, { code: 9000, message: "Unknown Error" })
}
}

// 6. hedera_signTransaction
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@
long "^5.2.3"
protobufjs "7.2.5"

"@hashgraph/proto@^2.15.0":
version "2.15.0"
resolved "https://registry.yarnpkg.com/@hashgraph/proto/-/proto-2.15.0.tgz#10c94825acea256373ba896c6229d22f6de9c19b"
integrity sha512-ULSNIwQZIroTssrEfNoUcIcWEJ9BIwKZiAsaRvJ2+Rr3XIr+np7UXv6sEkJU+jSyzk97LrTdiRAoc/hJO9Vx8Q==
dependencies:
long "^4.0.0"
protobufjs "^7.2.5"

"@hashgraph/sdk@^2.56.0":
version "2.56.0"
resolved "https://registry.yarnpkg.com/@hashgraph/sdk/-/sdk-2.56.0.tgz#a73ab4fa52ff1ffeea54fea03d242e1991427075"
Expand Down Expand Up @@ -3411,6 +3419,11 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==

long@^5.0.0, long@^5.2.3:
version "5.2.3"
resolved "https://registry.yarnpkg.com/long/-/long-5.2.3.tgz#a3ba97f3877cf1d778eccbcb048525ebb77499e1"
Expand Down