From dad51149879e2425ec98944eed5fc90b5c151d9a Mon Sep 17 00:00:00 2001 From: Himalayan Dev <72465553+himalayan-dev@users.noreply.github.com> Date: Thu, 10 Oct 2024 12:56:45 -0500 Subject: [PATCH] Added memo to home page (#731) Signed-off-by: Himalayan Dev --- .../packages/site/package.json | 2 +- .../packages/snap/CHANGELOG.md | 14 ++++++ .../packages/snap/package.json | 2 +- .../packages/snap/snap.manifest.json | 4 +- .../packages/snap/src/custom-ui/onHome.ts | 43 ++++++++++++------- .../snap/src/custom-ui/onUserInput.ts | 10 +++-- .../packages/snap/src/snap/SnapAccounts.ts | 27 ++++++++++-- .../packages/snap/src/utils/HederaUtils.ts | 4 +- 8 files changed, 78 insertions(+), 28 deletions(-) diff --git a/packages/hedera-wallet-snap/packages/site/package.json b/packages/hedera-wallet-snap/packages/site/package.json index a1381ef6..a382d146 100644 --- a/packages/hedera-wallet-snap/packages/site/package.json +++ b/packages/hedera-wallet-snap/packages/site/package.json @@ -1,6 +1,6 @@ { "name": "@hashgraph/hedera-wallet-snap-site", - "version": "0.6.0", + "version": "0.6.1", "private": true, "license": "Apache-2.0", "scripts": { diff --git a/packages/hedera-wallet-snap/packages/snap/CHANGELOG.md b/packages/hedera-wallet-snap/packages/snap/CHANGELOG.md index 60f38a45..fc01dcc1 100644 --- a/packages/hedera-wallet-snap/packages/snap/CHANGELOG.md +++ b/packages/hedera-wallet-snap/packages/snap/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [0.6.1](https://github.com/hashgraph/hedera-metamask-snaps/compare/v0.6.1...v0.6.1) (2024-10-10) + +### :page_with_curl: Documentation + +- Refer to the [Hedera Wallet Snap Wiki](https://docs.tuum.tech/hedera-wallet-snap/) for more info on how the snap works and how to integrate it into your own application. + +### :rocket: Features + +- [Added memo support to snap home page](https://github.com/hashgraph/hedera-metamask-snaps/issues/730) + +### :bug: Bug Fixes + +- [Fixed an issue whereby snap wasn't able to retrieve lazy-created account](https://github.com/hashgraph/hedera-metamask-snaps/issues/732) + ## [0.6.0](https://github.com/hashgraph/hedera-metamask-snaps/compare/v0.6.0...v0.6.0) (2024-09-09) ### :page_with_curl: Documentation diff --git a/packages/hedera-wallet-snap/packages/snap/package.json b/packages/hedera-wallet-snap/packages/snap/package.json index bbad35cb..2ad40397 100644 --- a/packages/hedera-wallet-snap/packages/snap/package.json +++ b/packages/hedera-wallet-snap/packages/snap/package.json @@ -1,6 +1,6 @@ { "name": "@hashgraph/hedera-wallet-snap", - "version": "0.6.0", + "version": "0.6.1", "description": "Hedera Wallet Snap unlocks wallet functionality via Metamask that any other apps can interact with, thereby turning Metamask into a native Hedera wallet without relying on Hedera JSON-RPC Relay.", "keywords": [ "MetaMask", diff --git a/packages/hedera-wallet-snap/packages/snap/snap.manifest.json b/packages/hedera-wallet-snap/packages/snap/snap.manifest.json index ee471a59..33f7d348 100644 --- a/packages/hedera-wallet-snap/packages/snap/snap.manifest.json +++ b/packages/hedera-wallet-snap/packages/snap/snap.manifest.json @@ -1,5 +1,5 @@ { - "version": "0.6.0", + "version": "0.6.1", "description": "Hedera Wallet unlocks wallet functionality via Metamask that any other apps can interact with, thereby turning Metamask into a native Hedera wallet without relying on Hedera JSON-RPC Relay.", "proposedName": "Hedera Wallet", "repository": { @@ -7,7 +7,7 @@ "url": "git+https://github.com/hashgraph/hedera-metamask-snaps.git" }, "source": { - "shasum": "4Cq3R8uZ7z77wWYfS0sOQKhOl/TOd9Obh+konXYj7Yo=", + "shasum": "3upIALuaGlohk4pbtltQLT4Do8+gMcff90QDRYvvkug=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onHome.ts b/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onHome.ts index bf8be573..d602b2f2 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onHome.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onHome.ts @@ -53,22 +53,22 @@ export const OnHomePageUI: OnHomePageHandler = async () => { // Get connected metamask account const connectedAddress = await SnapAccounts.getCurrentMetamaskAccount(); - try { - // Set current account - await SnapAccounts.setCurrentAccount( - origin, - state, - null, - network, - mirrorNodeUrl, - false, - ); + const snapAddress = await SnapAccounts.setCurrentAccount( + origin, + state, + null, + network, + mirrorNodeUrl, + false, + true, + ); - const walletSnapParams: WalletSnapParams = { - origin, - state, - }; + const walletSnapParams: WalletSnapParams = { + origin, + state, + }; + try { const accountInfo: AccountInfo = await GetAccountInfoFacade.getAccountInfo( walletSnapParams, {} as GetAccountInfoRequestParams, @@ -102,6 +102,10 @@ export const OnHomePageUI: OnHomePageHandler = async () => { name: 'amount', placeholder: 'Amount', }), + input({ + name: 'memo', + placeholder: 'Memo(Needed for exchange transfers)', + }), button({ value: 'Send Hbar', buttonType: 'submit', @@ -138,11 +142,20 @@ export const OnHomePageUI: OnHomePageHandler = async () => { text(`**Mirror Node URL**: ${mirrorNodeUrl}`), divider(), text(`**Account Id**: Could not fetch account id.`), - text(`**Account EVM Address**: Could not fetch EVM address.`), + text(`**Account EVM Address**:`), + copyable(snapAddress), text(`**Associated Metamask Address**:`), copyable(connectedAddress), divider(), + button({ + value: 'Export Snap Account Private Key', + name: 'btn-export-snap-account-private-key', + }), + divider(), text(`**FAQs**:`), + text( + `**Why is my MetaMask address different from my Hedera EVM address?**: Since MetaMask does not allow Snaps to access the private keys of MetaMask accounts, Hedera Wallet Snap creates a new account with its own private key. However, this new account is still associated with the MetaMask account so you can easily get back to your account on any device as long as you're connected to the same MetaMask account.`, + ), text( `**Why is my account not activated?**: On Hedera, until a transaction is sent to the network, the account is not activated. Please visit [Pulse](https://pulse.tuum.tech) to activate your account for free.`, ), diff --git a/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onUserInput.ts b/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onUserInput.ts index 8976f6ae..ca64482c 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onUserInput.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/custom-ui/onUserInput.ts @@ -44,13 +44,14 @@ export const onUserInputUI: OnUserInputHandler = async ({ event }) => { const { network, mirrorNodeUrl } = HederaUtils.getNetworkInfoFromUser(null); // Set current account - await SnapAccounts.setCurrentAccount( + const snapAddress = await SnapAccounts.setCurrentAccount( origin, state, null, network, mirrorNodeUrl, false, + true, ); const walletSnapParams: WalletSnapParams = { @@ -72,6 +73,9 @@ export const onUserInputUI: OnUserInputHandler = async ({ event }) => { } as SimpleTransfer, ], } as TransferCryptoRequestParams; + if (event.value.memo) { + params.memo = event.value.memo as string; + } result = await TransferCryptoFacade.transferCrypto( walletSnapParams, params, @@ -101,9 +105,7 @@ export const onUserInputUI: OnUserInputHandler = async ({ event }) => { 'Warning: Never disclose this key. Anyone with your private keys can steal any assets held in your account.', ), copyable( - state.accountState[state.currentAccount.hederaEvmAddress][ - state.currentAccount.network - ].keyStore.privateKey, + state.accountState[snapAddress][network].keyStore.privateKey, ), ]), }, diff --git a/packages/hedera-wallet-snap/packages/snap/src/snap/SnapAccounts.ts b/packages/hedera-wallet-snap/packages/snap/src/snap/SnapAccounts.ts index 761abc93..109c6056 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/snap/SnapAccounts.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/snap/SnapAccounts.ts @@ -96,6 +96,7 @@ export class SnapAccounts { * @param network - Hedera network. * @param mirrorNodeUrl - Hedera mirror node URL. * @param isExternalAccount - Whether this is a metamask or a non-metamask account. + * @param returnEarly - Whether to return early. * @returns Nothing. */ public static async setCurrentAccount( @@ -105,7 +106,8 @@ export class SnapAccounts { network: string, mirrorNodeUrl: string, isExternalAccount: boolean, - ): Promise { + returnEarly = false, + ): Promise { let metamaskEvmAddress = ''; let externalEvmAddress = ''; let connectedAddress = ''; @@ -189,7 +191,7 @@ export class SnapAccounts { await SnapAccounts.initAccountState(state, network, connectedAddress); } - await SnapAccounts.importMetaMaskAccount( + return await SnapAccounts.importMetaMaskAccount( origin, state, network, @@ -198,6 +200,7 @@ export class SnapAccounts { metamaskEvmAddress, externalEvmAddress, keyStore, + returnEarly, ); } @@ -546,6 +549,8 @@ export class SnapAccounts { * @param metamaskEvmAddress - Metamask EVM address. * @param externalEvmAddress - External EVM address. * @param keyStore - Keystore for private, public keys and EVM address. + * @param returnEarly - Whether to return early. + * @returns Result. */ public static async importMetaMaskAccount( _origin: string, @@ -556,7 +561,8 @@ export class SnapAccounts { metamaskEvmAddress: string, externalEvmAddress: string, keyStore: KeyStore, - ): Promise { + returnEarly = false, + ): Promise { const { curve, privateKey, publicKey, address } = keyStore; console.log('Retrieving account info from Hedera Mirror node'); @@ -567,6 +573,19 @@ export class SnapAccounts { if (_.isEmpty(accountInfo)) { const errMessage = `Could not get account info from Hedera Mirror Node on '${network}'. Address: ${address}. Please try again.`; console.error(errMessage); + if (returnEarly) { + // eslint-disable-next-line require-atomic-updates + state.accountState[connectedAddress][network].keyStore = { + curve, + privateKey, + publicKey, + address, + hederaAccountId: '', + }; + + await SnapState.updateState(state); + return address; + } throw rpcErrors.resourceNotFound({ message: errMessage, data: address, @@ -600,5 +619,7 @@ export class SnapAccounts { }; await SnapState.updateState(state); + + return address; } } diff --git a/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts b/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts index 25c48851..f1fc6884 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts @@ -2576,8 +2576,8 @@ export class HederaUtils { result.memo = mirrorNodeData.memo; result.evmAddress = mirrorNodeData.evm_address; result.key = { - type: mirrorNodeData?.key?._type, - key: mirrorNodeData?.key?.key, + type: mirrorNodeData?.key?._type ?? '', + key: mirrorNodeData?.key?.key ?? '', }; result.autoRenewPeriod = String(mirrorNodeData.auto_renew_period); result.ethereumNonce = String(mirrorNodeData.ethereum_nonce);