From 35ea5d2dce81085f786eca1836b0294462241f25 Mon Sep 17 00:00:00 2001
From: mtyszczak <mateusz.tyszczak@gmail.com>
Date: Thu, 13 Mar 2025 14:22:49 +0100
Subject: [PATCH] Allow signing transactions for a custom chain id

---
 package.json                                |  2 +-
 snap.manifest.json                          |  4 ++--
 src/hive/wax.ts                             | 12 ++++++------
 src/index.ts                                |  2 +-
 src/rpc.ts                                  |  1 +
 src/snap/dialogs/ConfirmTransactionSign.tsx | 14 ++++++++++----
 src/snap/encodeBuffer.ts                    |  2 +-
 src/snap/signTransaction.ts                 |  8 ++++----
 8 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/package.json b/package.json
index 6907f8b..a98e9a5 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@hiveio/metamask-snap",
-  "version": "1.2.1",
+  "version": "1.3.1",
   "description": "Hive wallet extension allowing you to sign transactions using keys derived from your Metamask wallet",
   "main": "./dist/bundle.js",
   "files": [
diff --git a/snap.manifest.json b/snap.manifest.json
index df481a5..81764ae 100644
--- a/snap.manifest.json
+++ b/snap.manifest.json
@@ -1,5 +1,5 @@
 {
-  "version": "1.2.1",
+  "version": "1.3.1",
   "description": "Hive wallet extension allowing you to sign transactions using keys derived from your Metamask wallet",
   "proposedName": "Hive Wallet",
   "repository": {
@@ -7,7 +7,7 @@
     "url": "git+https://gitlab.syncad.com/hive/metamask-snap.git"
   },
   "source": {
-    "shasum": "VxtYK0n+vdiIHkFdTswCQng05NPFRb1wZ78mBBP5dbk=",
+    "shasum": "8OiCG07i8jcpwdDezZyvzyBWJftYgWno1hcNL/gUWjc=",
     "location": {
       "npm": {
         "filePath": "dist/bundle.js",
diff --git a/src/hive/wax.ts b/src/hive/wax.ts
index d9a443e..110938f 100644
--- a/src/hive/wax.ts
+++ b/src/hive/wax.ts
@@ -1,9 +1,9 @@
-import { createWaxFoundation, type IWaxBaseInterface } from "@hiveio/wax";
+import { createWaxFoundation, DEFAULT_WAX_OPTIONS, type IWaxBaseInterface } from "@hiveio/wax";
 
-let _wax: undefined | IWaxBaseInterface;
-export const getWax = async (): Promise<IWaxBaseInterface> => {
-  if (!_wax)
-    _wax = await createWaxFoundation();
+const waxInstances: Record<string, IWaxBaseInterface> = {};
+export const getWax = async (chainId: string = DEFAULT_WAX_OPTIONS.chainId): Promise<IWaxBaseInterface> => {
+  if (!waxInstances[chainId])
+    waxInstances[chainId] = await createWaxFoundation(chainId !== undefined ? { chainId } : undefined);
 
-  return _wax;
+  return waxInstances[chainId];
 };
diff --git a/src/index.ts b/src/index.ts
index 85531d5..e422d56 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -27,7 +27,7 @@ export const onRpcRequest = async ({
 
     case 'hive_signTransaction':
       return {
-        signatures: await signTransaction(origin, request.params.transaction, request.params.keys)
+        signatures: await signTransaction(origin, request.params.transaction, request.params.keys, request.params.chainId)
       };
 
     case 'hive_decrypt':
diff --git a/src/rpc.ts b/src/rpc.ts
index 1f1831a..102fe8b 100644
--- a/src/rpc.ts
+++ b/src/rpc.ts
@@ -20,6 +20,7 @@ export type SignTransactionRequest = {
   method: 'hive_signTransaction';
   params: {
     transaction: string;
+    chainId?: string;
     keys: KeyIndex[];
   };
 }
diff --git a/src/snap/dialogs/ConfirmTransactionSign.tsx b/src/snap/dialogs/ConfirmTransactionSign.tsx
index 032b8cd..d83b1a9 100644
--- a/src/snap/dialogs/ConfirmTransactionSign.tsx
+++ b/src/snap/dialogs/ConfirmTransactionSign.tsx
@@ -1,17 +1,23 @@
-import { Bold, Copyable, Text, Box } from "@metamask/snaps-sdk/jsx";
+import { Bold, Copyable, Text, Box, Banner, Italic } from "@metamask/snaps-sdk/jsx";
 import { KeyIndex } from "../../rpc";
 import { KeyTypeNotice } from "./components/KeyTypeNotice";
+import { DEFAULT_WAX_OPTIONS } from "@hiveio/wax";
 
-export const ConfirmTransactionSign = (origin: string, transaction: string, keys: KeyIndex[]) => snap.request({
+export const ConfirmTransactionSign = (origin: string, transaction: string, keys: KeyIndex[], chainId?: string) => snap.request({
   method: 'snap_dialog',
   params: {
     type: 'confirmation',
     content: (
       <Box>
         <Text>
-          <Bold>{ origin }</Bold> asked to sign a transaction:
+          <Bold>{origin}</Bold> asked to sign a transaction:
         </Text>
-        <Copyable value={ transaction } />
+        <Copyable value={transaction} />
+        {chainId && chainId !== DEFAULT_WAX_OPTIONS.chainId && <Banner title="Custom chain signing" severity="warning">
+          <Text>
+            <Bold>Warning:</Bold> You are signing this transaction for a custom chain. Make sure you trust this chain: <Italic>{chainId}</Italic>
+          </Text>
+        </Banner>}
         <Text>
           Confirm if you want to sign it using your:
         </Text>
diff --git a/src/snap/encodeBuffer.ts b/src/snap/encodeBuffer.ts
index 206f9e8..0d184b5 100644
--- a/src/snap/encodeBuffer.ts
+++ b/src/snap/encodeBuffer.ts
@@ -8,7 +8,7 @@ export const encodeBuffer = async (origin: string, buffer: string, firstKey: Key
   const confirmDecode = await ConfirmBufferSign(origin, buffer, firstKey, secondKey);
 
   if(!confirmDecode)
-    throw new Error('User denied the buffer decode');
+    throw new Error('User denied the buffer encode');
 
   // The order is important: First create wax, then create wallet
   const wax = await getWax();
diff --git a/src/snap/signTransaction.ts b/src/snap/signTransaction.ts
index 7d8b856..04a5e43 100644
--- a/src/snap/signTransaction.ts
+++ b/src/snap/signTransaction.ts
@@ -5,17 +5,17 @@ import { getTempWallet } from "../hive/beekeeper";
 import { ConfirmTransactionSign } from "./dialogs/ConfirmTransactionSign";
 import type { THexString } from "@hiveio/wax";
 
-export const signTransaction = async (origin: string, transaction: string, keys: KeyIndex[]): Promise<THexString[]> => {
+export const signTransaction = async (origin: string, transaction: string, keys: KeyIndex[], chainId?: string): Promise<THexString[]> => {
   if (keys.length < 1)
     throw new Error('No keys provided');
 
-  const confirmSign = await ConfirmTransactionSign(origin, transaction, keys);
+  const confirmSign = await ConfirmTransactionSign(origin, transaction, keys, chainId);
 
   if(!confirmSign)
-    throw new Error('User denied the transaction');
+    throw new Error('User denied the transaction signing');
 
   // The order is important: First create wax, then transaction and if all success then create wallet
-  const wax = await getWax();
+  const wax = await getWax(chainId);
   const tx = wax.createTransactionFromJson(transaction);
   const wallet = await getTempWallet();
 
-- 
GitLab