diff --git a/package.json b/package.json index 6907f8be8977251381fa05cf49dedb418216bd42..a98e9a57844c2c092b7f6cb7e07dab77b9a08461 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 df481a548e8e2767f417c92bcdf0a49cdc2ee62b..81764aebcb642273c3b3504583af0ef416a58195 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 d9a443e64daa7fa5b20e15628655558452271e78..110938f1744e6563f0cc9efd0af5e285e1d70616 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 85531d596726cea9d5b484c14039a9789448494c..e422d56572780ef48a8ff020a3a1b442ec732e75 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 1f1831abb28494d21d96108a5c30c4ba4b661f62..102fe8bc613ec4d131b703d4b4accc7b8a250352 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 032b8cde48aca70b2b14c6b500663f7f85e1fff0..d83b1a9d6dfefeb39c3e0d22bd4ca195b385828d 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 206f9e87914f41481cb16f4a9b9b682d140224b9..0d184b572cf71ded77eac7ea58de679db925c578 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 7d8b856bce48fad04cdb0bafa5cbbcc6858f9c8f..04a5e43014d45f34f05eba302341a05672d8c40d 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();