From f7f6333511a68baced1abb540cabbb6826acc405 Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 10:00:24 +0100 Subject: [PATCH 1/8] Add name lookup example snap --- .../packages/name-lookup/.depcheckrc.json | 17 ++ .../packages/name-lookup/.eslintrc.js | 7 + .../packages/name-lookup/CHANGELOG.md | 7 + .../packages/name-lookup/LICENSE.APACHE2 | 201 ++++++++++++++++++ .../packages/name-lookup/LICENSE.MIT0 | 16 ++ .../examples/packages/name-lookup/README.md | 3 + .../packages/name-lookup/images/icon.svg | 1 + .../packages/name-lookup/package.json | 75 +++++++ .../packages/name-lookup/snap.config.js | 7 + .../packages/name-lookup/snap.manifest.json | 25 +++ .../packages/name-lookup/src/index.ts | 19 ++ .../packages/name-lookup/tsconfig.json | 21 ++ packages/test-snaps/package.json | 1 + .../test-snaps/src/features/snaps/index.ts | 3 +- .../features/snaps/name-lookup/NameLookup.tsx | 20 ++ .../features/snaps/name-lookup/constants.ts | 5 + .../src/features/snaps/name-lookup/index.ts | 1 + yarn.lock | 37 ++++ 18 files changed, 465 insertions(+), 1 deletion(-) create mode 100644 packages/examples/packages/name-lookup/.depcheckrc.json create mode 100644 packages/examples/packages/name-lookup/.eslintrc.js create mode 100644 packages/examples/packages/name-lookup/CHANGELOG.md create mode 100644 packages/examples/packages/name-lookup/LICENSE.APACHE2 create mode 100644 packages/examples/packages/name-lookup/LICENSE.MIT0 create mode 100644 packages/examples/packages/name-lookup/README.md create mode 100644 packages/examples/packages/name-lookup/images/icon.svg create mode 100644 packages/examples/packages/name-lookup/package.json create mode 100644 packages/examples/packages/name-lookup/snap.config.js create mode 100644 packages/examples/packages/name-lookup/snap.manifest.json create mode 100644 packages/examples/packages/name-lookup/src/index.ts create mode 100644 packages/examples/packages/name-lookup/tsconfig.json create mode 100644 packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx create mode 100644 packages/test-snaps/src/features/snaps/name-lookup/constants.ts create mode 100644 packages/test-snaps/src/features/snaps/name-lookup/index.ts diff --git a/packages/examples/packages/name-lookup/.depcheckrc.json b/packages/examples/packages/name-lookup/.depcheckrc.json new file mode 100644 index 0000000000..15d64e734b --- /dev/null +++ b/packages/examples/packages/name-lookup/.depcheckrc.json @@ -0,0 +1,17 @@ +{ + "ignore-patterns": ["dist", "coverage"], + "ignores": [ + "@lavamoat/allow-scripts", + "@lavamoat/preinstall-always-fail", + "@metamask/auto-changelog", + "@metamask/eslint-*", + "@types/*", + "@typescript-eslint/*", + "eslint-config-*", + "eslint-plugin-*", + "prettier-plugin-packagejson", + "ts-node", + "typedoc", + "typescript" + ] +} diff --git a/packages/examples/packages/name-lookup/.eslintrc.js b/packages/examples/packages/name-lookup/.eslintrc.js new file mode 100644 index 0000000000..dc028fb7ec --- /dev/null +++ b/packages/examples/packages/name-lookup/.eslintrc.js @@ -0,0 +1,7 @@ +module.exports = { + root: true, + + extends: ['../../.eslintrc.js'], + + ignorePatterns: ['!.eslintrc.js', 'dist/', 'build/'], +}; diff --git a/packages/examples/packages/name-lookup/CHANGELOG.md b/packages/examples/packages/name-lookup/CHANGELOG.md new file mode 100644 index 0000000000..d94e3d6c6b --- /dev/null +++ b/packages/examples/packages/name-lookup/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] diff --git a/packages/examples/packages/name-lookup/LICENSE.APACHE2 b/packages/examples/packages/name-lookup/LICENSE.APACHE2 new file mode 100644 index 0000000000..bf37d0e612 --- /dev/null +++ b/packages/examples/packages/name-lookup/LICENSE.APACHE2 @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2023 ConsenSys Software Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/examples/packages/name-lookup/LICENSE.MIT0 b/packages/examples/packages/name-lookup/LICENSE.MIT0 new file mode 100644 index 0000000000..913c577462 --- /dev/null +++ b/packages/examples/packages/name-lookup/LICENSE.MIT0 @@ -0,0 +1,16 @@ +MIT No Attribution + +Copyright 2023 ConsenSys Software Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/examples/packages/name-lookup/README.md b/packages/examples/packages/name-lookup/README.md new file mode 100644 index 0000000000..e46f7ceecf --- /dev/null +++ b/packages/examples/packages/name-lookup/README.md @@ -0,0 +1,3 @@ +# Name Lookup Test Snap + +A MetaMask Test Snap that resolves domains and Ethereum addresses. diff --git a/packages/examples/packages/name-lookup/images/icon.svg b/packages/examples/packages/name-lookup/images/icon.svg new file mode 100644 index 0000000000..f555e1ea73 --- /dev/null +++ b/packages/examples/packages/name-lookup/images/icon.svg @@ -0,0 +1 @@ + diff --git a/packages/examples/packages/name-lookup/package.json b/packages/examples/packages/name-lookup/package.json new file mode 100644 index 0000000000..e45678a736 --- /dev/null +++ b/packages/examples/packages/name-lookup/package.json @@ -0,0 +1,75 @@ +{ + "name": "@metamask/name-lookup-example-snap", + "version": "0.0.0", + "description": "MetaMask example snap demonstrating the use of the `endowment:name-lookup` permission.", + "repository": { + "type": "git", + "url": "https://github.com/MetaMask/snaps.git" + }, + "license": "(MIT-0 OR Apache-2.0)", + "sideEffects": false, + "main": "./dist/bundle.js", + "types": "dist/index.d.ts", + "files": [ + "dist/", + "images/icon.svg", + "snap.manifest.json" + ], + "scripts": { + "build": "mm-snap build", + "build:clean": "yarn clean && yarn build", + "clean": "rimraf \"dist\"", + "lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog && yarn lint:dependencies", + "lint:changelog": "../../../../scripts/validate-changelog.sh @metamask/json-rpc-example-snap", + "lint:ci": "yarn lint", + "lint:eslint": "eslint . --cache --ext js,ts,jsx,tsx", + "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", + "lint:misc": "prettier --no-error-on-unmatched-pattern --loglevel warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" \"!snap.manifest.json\" --ignore-path ../../../../.gitignore", + "start": "mm-snap watch", + "test": "yarn test:e2e", + "test:e2e": "jest", + "prepare-manifest:preview": "../../../../scripts/prepare-preview-manifest.sh", + "publish:preview": "yarn npm publish --tag preview", + "lint:dependencies": "depcheck" + }, + "dependencies": { + "@metamask/snaps-types": "workspace:^" + }, + "devDependencies": { + "@jest/globals": "^29.5.0", + "@lavamoat/allow-scripts": "^2.3.1", + "@metamask/auto-changelog": "^3.1.0", + "@metamask/eslint-config": "^12.1.0", + "@metamask/eslint-config-jest": "^12.1.0", + "@metamask/eslint-config-nodejs": "^12.1.0", + "@metamask/eslint-config-typescript": "^12.1.0", + "@metamask/snaps-cli": "workspace:^", + "@metamask/snaps-jest": "workspace:^", + "@swc/core": "1.3.78", + "@swc/jest": "^0.2.26", + "@typescript-eslint/eslint-plugin": "^5.42.1", + "@typescript-eslint/parser": "^5.42.1", + "deepmerge": "^4.2.2", + "depcheck": "^1.4.5", + "eslint": "^8.27.0", + "eslint-config-prettier": "^8.5.0", + "eslint-plugin-import": "^2.26.0", + "eslint-plugin-jest": "^27.1.5", + "eslint-plugin-jsdoc": "^39.6.2", + "eslint-plugin-n": "^15.7.0", + "eslint-plugin-prettier": "^4.2.1", + "eslint-plugin-promise": "^6.1.1", + "jest": "^29.0.2", + "prettier": "^2.7.1", + "prettier-plugin-packagejson": "^2.2.11", + "ts-node": "^10.9.1", + "typescript": "~4.8.4" + }, + "engines": { + "node": ">=16.0.0" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + } +} diff --git a/packages/examples/packages/name-lookup/snap.config.js b/packages/examples/packages/name-lookup/snap.config.js new file mode 100644 index 0000000000..eb85d4024d --- /dev/null +++ b/packages/examples/packages/name-lookup/snap.config.js @@ -0,0 +1,7 @@ +module.exports = { + cliOptions: { + port: 8023, + src: './src/index.ts', + transpilationMode: 'localOnly', + }, +}; diff --git a/packages/examples/packages/name-lookup/snap.manifest.json b/packages/examples/packages/name-lookup/snap.manifest.json new file mode 100644 index 0000000000..e6cdf80508 --- /dev/null +++ b/packages/examples/packages/name-lookup/snap.manifest.json @@ -0,0 +1,25 @@ +{ + "version": "0.0.0", + "description": "An example snap to test name lookup handlers.", + "proposedName": "Name Lookup Example Snap", + "repository": { + "type": "git", + "url": "https://github.com/MetaMask/snaps.git" + }, + "source": { + "shasum": "6/HwkaZsqTnu+dSkZcpAGVIVkyU+VUdPQdqZOfYiL1c=", + "location": { + "npm": { + "filePath": "dist/bundle.js", + "packageName": "@metamask/name-lookup-example-snap", + "registry": "https://registry.npmjs.org/" + } + } + }, + "initialPermissions": { + "endowment:name-lookup": [ + "eip155:1" + ] + }, + "manifestVersion": "0.1" +} diff --git a/packages/examples/packages/name-lookup/src/index.ts b/packages/examples/packages/name-lookup/src/index.ts new file mode 100644 index 0000000000..a1cf3bee78 --- /dev/null +++ b/packages/examples/packages/name-lookup/src/index.ts @@ -0,0 +1,19 @@ +import type { OnNameLookupHandler } from '@metamask/snaps-types'; + +export const onNameLookup: OnNameLookupHandler = async (request) => { + const { chainId, address, domain } = request; + + if (address) { + const shortAddress = `0x${address.substring(2, 5)}`; + const chainIdHex = `0x${parseInt(chainId.split(':')[1], 10).toString(16)}`; + const resolvedDomain = `Test Token - ${shortAddress} / ${chainIdHex}`; + return { resolvedDomain }; + } + + if (domain) { + const resolvedAddress = `0xc0ffee254729296a45a3885639AC7E10F9d54979`; + return { resolvedAddress }; + } + + return null; +}; diff --git a/packages/examples/packages/name-lookup/tsconfig.json b/packages/examples/packages/name-lookup/tsconfig.json new file mode 100644 index 0000000000..70fc639057 --- /dev/null +++ b/packages/examples/packages/name-lookup/tsconfig.json @@ -0,0 +1,21 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "./", + "paths": { + "@metamask/*": ["../../../*/src"] + } + }, + "include": ["src", "snap.config.ts"], + "references": [ + { + "path": "../../../snaps-types" + }, + { + "path": "../../../snaps-jest" + }, + { + "path": "../../../snaps-cli" + } + ] +} diff --git a/packages/test-snaps/package.json b/packages/test-snaps/package.json index 5406bfdce1..88f967b9b8 100644 --- a/packages/test-snaps/package.json +++ b/packages/test-snaps/package.json @@ -42,6 +42,7 @@ "@metamask/json-rpc-example-snap": "workspace:^", "@metamask/lifecycle-hooks-example-snap": "workspace:^", "@metamask/manage-state-example-snap": "workspace:^", + "@metamask/name-lookup-example-snap": "workspace:^", "@metamask/network-example-snap": "workspace:^", "@metamask/notification-example-snap": "workspace:^", "@metamask/snaps-utils": "workspace:^", diff --git a/packages/test-snaps/src/features/snaps/index.ts b/packages/test-snaps/src/features/snaps/index.ts index 47b9c15f6a..1fdad5b01c 100644 --- a/packages/test-snaps/src/features/snaps/index.ts +++ b/packages/test-snaps/src/features/snaps/index.ts @@ -6,12 +6,13 @@ export * from './errors'; export * from './ethereum-provider'; export * from './ethers-js'; export * from './get-entropy'; +export * from './get-locale'; export * from './json-rpc'; export * from './lifecycle-hooks'; export * from './manage-state'; +export * from './name-lookup'; export * from './network-access'; export * from './notifications'; export * from './transaction-insights'; export * from './updates'; export * from './wasm'; -export * from './get-locale'; diff --git a/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx b/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx new file mode 100644 index 0000000000..a4a3b83368 --- /dev/null +++ b/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx @@ -0,0 +1,20 @@ +import type { FunctionComponent } from 'react'; + +import { Snap } from '../../../components'; +import { + NAME_LOOKUP_SNAP_ID, + NAME_LOOKUP_SNAP_PORT, + NAME_LOOKUP_VERSION, +} from './constants'; + +export const NameLookup: FunctionComponent = () => { + return ( + + ); +}; diff --git a/packages/test-snaps/src/features/snaps/name-lookup/constants.ts b/packages/test-snaps/src/features/snaps/name-lookup/constants.ts new file mode 100644 index 0000000000..532652946f --- /dev/null +++ b/packages/test-snaps/src/features/snaps/name-lookup/constants.ts @@ -0,0 +1,5 @@ +import packageJson from '@metamask/name-lookup-example-snap/package.json'; + +export const NAME_LOOKUP_SNAP_ID = 'npm:@metamask/name-lookup-example-snap'; +export const NAME_LOOKUP_SNAP_PORT = 8023; +export const NAME_LOOKUP_VERSION = packageJson.version; diff --git a/packages/test-snaps/src/features/snaps/name-lookup/index.ts b/packages/test-snaps/src/features/snaps/name-lookup/index.ts new file mode 100644 index 0000000000..41c3126789 --- /dev/null +++ b/packages/test-snaps/src/features/snaps/name-lookup/index.ts @@ -0,0 +1 @@ +export * from './NameLookup'; diff --git a/yarn.lock b/yarn.lock index 24e538c257..575f8c120a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4640,6 +4640,42 @@ __metadata: languageName: unknown linkType: soft +"@metamask/name-lookup-example-snap@workspace:^, @metamask/name-lookup-example-snap@workspace:packages/examples/packages/name-lookup": + version: 0.0.0-use.local + resolution: "@metamask/name-lookup-example-snap@workspace:packages/examples/packages/name-lookup" + dependencies: + "@jest/globals": ^29.5.0 + "@lavamoat/allow-scripts": ^2.3.1 + "@metamask/auto-changelog": ^3.1.0 + "@metamask/eslint-config": ^12.1.0 + "@metamask/eslint-config-jest": ^12.1.0 + "@metamask/eslint-config-nodejs": ^12.1.0 + "@metamask/eslint-config-typescript": ^12.1.0 + "@metamask/snaps-cli": "workspace:^" + "@metamask/snaps-jest": "workspace:^" + "@metamask/snaps-types": "workspace:^" + "@swc/core": 1.3.78 + "@swc/jest": ^0.2.26 + "@typescript-eslint/eslint-plugin": ^5.42.1 + "@typescript-eslint/parser": ^5.42.1 + deepmerge: ^4.2.2 + depcheck: ^1.4.5 + eslint: ^8.27.0 + eslint-config-prettier: ^8.5.0 + eslint-plugin-import: ^2.26.0 + eslint-plugin-jest: ^27.1.5 + eslint-plugin-jsdoc: ^39.6.2 + eslint-plugin-n: ^15.7.0 + eslint-plugin-prettier: ^4.2.1 + eslint-plugin-promise: ^6.1.1 + jest: ^29.0.2 + prettier: ^2.7.1 + prettier-plugin-packagejson: ^2.2.11 + ts-node: ^10.9.1 + typescript: ~4.8.4 + languageName: unknown + linkType: soft + "@metamask/network-example-snap@workspace:^, @metamask/network-example-snap@workspace:packages/examples/packages/network-access": version: 0.0.0-use.local resolution: "@metamask/network-example-snap@workspace:packages/examples/packages/network-access" @@ -5631,6 +5667,7 @@ __metadata: "@metamask/json-rpc-example-snap": "workspace:^" "@metamask/lifecycle-hooks-example-snap": "workspace:^" "@metamask/manage-state-example-snap": "workspace:^" + "@metamask/name-lookup-example-snap": "workspace:^" "@metamask/network-example-snap": "workspace:^" "@metamask/notification-example-snap": "workspace:^" "@metamask/providers": ^11.1.1 From d95665fec29068359c246001e511ab19631c9a0c Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 10:11:16 +0100 Subject: [PATCH 2/8] Update README --- .../examples/packages/name-lookup/README.md | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/examples/packages/name-lookup/README.md b/packages/examples/packages/name-lookup/README.md index e46f7ceecf..fdceebedd0 100644 --- a/packages/examples/packages/name-lookup/README.md +++ b/packages/examples/packages/name-lookup/README.md @@ -1,3 +1,24 @@ -# Name Lookup Test Snap +# `@metamask/name-lookup-example-snap` -A MetaMask Test Snap that resolves domains and Ethereum addresses. +This snap demonstrates how to use the `onNameLookup` handler. + +## Snap manifest + +> **Note**: Using name lookup requires the `endowment:name-lookup` permission. +> Refer to [the documentation](https://docs.metamask.io/snaps/reference/permissions/#endowmentname-lookup) +> for more information. + +The manifest of this snap includes the `endowment:name-lookup` permission: + +```json +{ + "initialPermissions": { + "endowment:name-lookup": ["eip155:1"] + } +} +``` + +## Snap usage + +This snap exposes the `onNameLookup` handler. This is called when a MetaMask client +requests the resolution of a domain or address, and cannot be called manually. From bfb28738d9a846d1a3a46d1c67473b0d7778c6ab Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 11:23:52 +0100 Subject: [PATCH 3/8] Add unit tests Update example readme. Fix changelog. Add Jest config. Fix package scripts. Use updated config file. Add JSDoc. --- packages/examples/README.md | 3 ++ .../packages/name-lookup/CHANGELOG.md | 2 + .../packages/name-lookup/jest.config.js | 21 ++++++++ .../packages/name-lookup/package.json | 5 +- .../packages/name-lookup/snap.config.js | 7 --- .../packages/name-lookup/snap.config.ts | 14 +++++ .../packages/name-lookup/snap.manifest.json | 2 +- .../packages/name-lookup/src/index.test.ts | 52 +++++++++++++++++++ .../packages/name-lookup/src/index.ts | 18 +++++-- .../features/snaps/name-lookup/NameLookup.tsx | 2 +- yarn.lock | 1 + 11 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 packages/examples/packages/name-lookup/jest.config.js delete mode 100644 packages/examples/packages/name-lookup/snap.config.js create mode 100644 packages/examples/packages/name-lookup/snap.config.ts create mode 100644 packages/examples/packages/name-lookup/src/index.test.ts diff --git a/packages/examples/README.md b/packages/examples/README.md index 782e40b24b..177b258250 100644 --- a/packages/examples/README.md +++ b/packages/examples/README.md @@ -44,6 +44,9 @@ The following is a list of the snaps in this directory. - [**`packages/manage-state`**](./packages/manage-state): This snap demonstrates how to use the `snap_manageState` method to store and retrieve data in a snap. +- [**`packages/name-lookup`**](./packages/name-lookup): This snap + demonstrates how to use the `endowment:name-lookup` permission to resolve + domains and addresses in a snap. - [**`packages/network-access`**](./packages/network-access): This snap demonstrates how to use the `endowment:network-access` permission to make network requests from a snap. diff --git a/packages/examples/packages/name-lookup/CHANGELOG.md b/packages/examples/packages/name-lookup/CHANGELOG.md index d94e3d6c6b..aa399df1be 100644 --- a/packages/examples/packages/name-lookup/CHANGELOG.md +++ b/packages/examples/packages/name-lookup/CHANGELOG.md @@ -5,3 +5,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] + +[Unreleased]: https://github.com/MetaMask/snaps/ diff --git a/packages/examples/packages/name-lookup/jest.config.js b/packages/examples/packages/name-lookup/jest.config.js new file mode 100644 index 0000000000..270869ba84 --- /dev/null +++ b/packages/examples/packages/name-lookup/jest.config.js @@ -0,0 +1,21 @@ +const deepmerge = require('deepmerge'); + +const baseConfig = require('../../../../jest.config.base'); + +module.exports = deepmerge(baseConfig, { + preset: '@metamask/snaps-jest', + + // Since `@metamask/snaps-jest` runs in the browser, we can't collect + // coverage information. + collectCoverage: false, + + // This is required for the tests to run inside the `MetaMask/snaps` + // repository. You don't need this in your own project. + moduleNameMapper: { + '^@metamask/(.+)$': [ + '/../../../$1/src', + '/../../../../node_modules/@metamask/$1', + '/node_modules/@metamask/$1', + ], + }, +}); diff --git a/packages/examples/packages/name-lookup/package.json b/packages/examples/packages/name-lookup/package.json index e45678a736..33fca7ae04 100644 --- a/packages/examples/packages/name-lookup/package.json +++ b/packages/examples/packages/name-lookup/package.json @@ -20,7 +20,7 @@ "build:clean": "yarn clean && yarn build", "clean": "rimraf \"dist\"", "lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog && yarn lint:dependencies", - "lint:changelog": "../../../../scripts/validate-changelog.sh @metamask/json-rpc-example-snap", + "lint:changelog": "../../../../scripts/validate-changelog.sh @metamask/name-lookup-example-snap", "lint:ci": "yarn lint", "lint:eslint": "eslint . --cache --ext js,ts,jsx,tsx", "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", @@ -33,7 +33,8 @@ "lint:dependencies": "depcheck" }, "dependencies": { - "@metamask/snaps-types": "workspace:^" + "@metamask/snaps-types": "workspace:^", + "@metamask/utils": "^8.1.0" }, "devDependencies": { "@jest/globals": "^29.5.0", diff --git a/packages/examples/packages/name-lookup/snap.config.js b/packages/examples/packages/name-lookup/snap.config.js deleted file mode 100644 index eb85d4024d..0000000000 --- a/packages/examples/packages/name-lookup/snap.config.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - cliOptions: { - port: 8023, - src: './src/index.ts', - transpilationMode: 'localOnly', - }, -}; diff --git a/packages/examples/packages/name-lookup/snap.config.ts b/packages/examples/packages/name-lookup/snap.config.ts new file mode 100644 index 0000000000..c2f7f62178 --- /dev/null +++ b/packages/examples/packages/name-lookup/snap.config.ts @@ -0,0 +1,14 @@ +import type { SnapConfig } from '@metamask/snaps-cli'; + +const config: SnapConfig = { + bundler: 'webpack', + input: './src/index.ts', + server: { + port: 8023, + }, + stats: { + buffer: false, + }, +}; + +export default config; diff --git a/packages/examples/packages/name-lookup/snap.manifest.json b/packages/examples/packages/name-lookup/snap.manifest.json index e6cdf80508..5fa902972e 100644 --- a/packages/examples/packages/name-lookup/snap.manifest.json +++ b/packages/examples/packages/name-lookup/snap.manifest.json @@ -1,6 +1,6 @@ { "version": "0.0.0", - "description": "An example snap to test name lookup handlers.", + "description": "MetaMask example snap demonstrating the use of the `endowment:name-lookup` permission.", "proposedName": "Name Lookup Example Snap", "repository": { "type": "git", diff --git a/packages/examples/packages/name-lookup/src/index.test.ts b/packages/examples/packages/name-lookup/src/index.test.ts new file mode 100644 index 0000000000..d054e2428a --- /dev/null +++ b/packages/examples/packages/name-lookup/src/index.test.ts @@ -0,0 +1,52 @@ +import { describe, it } from '@jest/globals'; + +import { onNameLookup } from '.'; +import type { OnNameLookupArgs } from '../../../../snaps-utils/src'; + +const DOMAIN_MOCK = 'test.domain'; +const ADDRESS_MOCK = '0xc0ffee254729296a45a3885639AC7E10F9d54979'; +const CHAIN_ID_MOCK = 'eip155:1'; + +describe('onNameLookup', () => { + it('returns resolved address if domain', async () => { + const request = { + domain: DOMAIN_MOCK, + chainId: CHAIN_ID_MOCK, + }; + + expect(await onNameLookup(request)).toStrictEqual({ + resolvedAddress: '0xc0ffee254729296a45a3885639AC7E10F9d54979', + }); + }); + + it('returns resolved domain if address', async () => { + const request = { + address: ADDRESS_MOCK, + chainId: CHAIN_ID_MOCK, + }; + + expect(await onNameLookup(request)).toStrictEqual({ + resolvedDomain: 'test.domain - 0xc0f / 0x1', + }); + }); + + it('returns resolved domain if address and domain', async () => { + const request = { + address: ADDRESS_MOCK, + domain: DOMAIN_MOCK, + chainId: CHAIN_ID_MOCK, + } as any; + + expect(await onNameLookup(request)).toStrictEqual({ + resolvedDomain: 'test.domain - 0xc0f / 0x1', + }); + }); + + it('returns null if no domain or address', async () => { + const request = { + chainId: CHAIN_ID_MOCK, + } as OnNameLookupArgs; + + expect(await onNameLookup(request)).toBeNull(); + }); +}); diff --git a/packages/examples/packages/name-lookup/src/index.ts b/packages/examples/packages/name-lookup/src/index.ts index a1cf3bee78..059f21f4a9 100644 --- a/packages/examples/packages/name-lookup/src/index.ts +++ b/packages/examples/packages/name-lookup/src/index.ts @@ -1,17 +1,29 @@ import type { OnNameLookupHandler } from '@metamask/snaps-types'; +import { numberToHex } from '@metamask/utils'; +/** + * Handle incoming name lookup requests from the MetaMask clients. + * + * @param request - The request arguments. + * @param request.domain - The domain to resolve. Will be undefined if an address is provided. + * @param request.address - The address to resolve. Will be undefined if a domain is provided. + * @param request.chainId - The CAIP-2 chain ID of the associated network. + * @returns If successful, an object containing the resolvedDomain or resolvedAddress. Null otherwise. + * @see https://docs.metamask.io/snaps/reference/exports/#onnamelookup + */ export const onNameLookup: OnNameLookupHandler = async (request) => { const { chainId, address, domain } = request; if (address) { const shortAddress = `0x${address.substring(2, 5)}`; - const chainIdHex = `0x${parseInt(chainId.split(':')[1], 10).toString(16)}`; - const resolvedDomain = `Test Token - ${shortAddress} / ${chainIdHex}`; + const chainIdDecimal = parseInt(chainId.split(':')[1], 10); + const chainIdHex = numberToHex(chainIdDecimal); + const resolvedDomain = `test.domain - ${shortAddress} / ${chainIdHex}`; return { resolvedDomain }; } if (domain) { - const resolvedAddress = `0xc0ffee254729296a45a3885639AC7E10F9d54979`; + const resolvedAddress = '0xc0ffee254729296a45a3885639AC7E10F9d54979'; return { resolvedAddress }; } diff --git a/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx b/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx index a4a3b83368..254102f4a4 100644 --- a/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx +++ b/packages/test-snaps/src/features/snaps/name-lookup/NameLookup.tsx @@ -15,6 +15,6 @@ export const NameLookup: FunctionComponent = () => { port={NAME_LOOKUP_SNAP_PORT} version={NAME_LOOKUP_VERSION} testId="name-lookup" - > + /> ); }; diff --git a/yarn.lock b/yarn.lock index 575f8c120a..6d531a25f9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4654,6 +4654,7 @@ __metadata: "@metamask/snaps-cli": "workspace:^" "@metamask/snaps-jest": "workspace:^" "@metamask/snaps-types": "workspace:^" + "@metamask/utils": ^8.1.0 "@swc/core": 1.3.78 "@swc/jest": ^0.2.26 "@typescript-eslint/eslint-plugin": ^5.42.1 From c73cc2563af9c8fdfb45c119b3258eb34898620e Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 11:28:22 +0100 Subject: [PATCH 4/8] Update shasum --- packages/examples/packages/name-lookup/snap.manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/examples/packages/name-lookup/snap.manifest.json b/packages/examples/packages/name-lookup/snap.manifest.json index 5fa902972e..866ab6d9c7 100644 --- a/packages/examples/packages/name-lookup/snap.manifest.json +++ b/packages/examples/packages/name-lookup/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "6/HwkaZsqTnu+dSkZcpAGVIVkyU+VUdPQdqZOfYiL1c=", + "shasum": "2GGvHjj8BycVWAOhQboxK48YuqfpaA17q485itQ6/u4=", "location": { "npm": { "filePath": "dist/bundle.js", From 063ed95b7808b2b2286578f12b045629dc7032ce Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 11:39:46 +0100 Subject: [PATCH 5/8] Fix package --- packages/examples/packages/name-lookup/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/examples/packages/name-lookup/package.json b/packages/examples/packages/name-lookup/package.json index 33fca7ae04..58ce261169 100644 --- a/packages/examples/packages/name-lookup/package.json +++ b/packages/examples/packages/name-lookup/package.json @@ -9,7 +9,6 @@ "license": "(MIT-0 OR Apache-2.0)", "sideEffects": false, "main": "./dist/bundle.js", - "types": "dist/index.d.ts", "files": [ "dist/", "images/icon.svg", From f3dbe576b56aa81fba9e02fe2007c36df383748f Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 12:50:11 +0100 Subject: [PATCH 6/8] Update resolved domain Co-authored-by: Maarten Zuidhoorn --- packages/examples/packages/name-lookup/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/examples/packages/name-lookup/src/index.ts b/packages/examples/packages/name-lookup/src/index.ts index 059f21f4a9..ac2b4e2044 100644 --- a/packages/examples/packages/name-lookup/src/index.ts +++ b/packages/examples/packages/name-lookup/src/index.ts @@ -18,7 +18,7 @@ export const onNameLookup: OnNameLookupHandler = async (request) => { const shortAddress = `0x${address.substring(2, 5)}`; const chainIdDecimal = parseInt(chainId.split(':')[1], 10); const chainIdHex = numberToHex(chainIdDecimal); - const resolvedDomain = `test.domain - ${shortAddress} / ${chainIdHex}`; + const resolvedDomain = `example.domain - ${shortAddress} / ${chainIdHex}`; return { resolvedDomain }; } From d03ef21d537aec012337914bcd58aad072c7da6a Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 12:51:26 +0100 Subject: [PATCH 7/8] Fix unit tests --- packages/examples/packages/name-lookup/src/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/examples/packages/name-lookup/src/index.test.ts b/packages/examples/packages/name-lookup/src/index.test.ts index d054e2428a..7d5c91a912 100644 --- a/packages/examples/packages/name-lookup/src/index.test.ts +++ b/packages/examples/packages/name-lookup/src/index.test.ts @@ -26,7 +26,7 @@ describe('onNameLookup', () => { }; expect(await onNameLookup(request)).toStrictEqual({ - resolvedDomain: 'test.domain - 0xc0f / 0x1', + resolvedDomain: 'example.domain - 0xc0f / 0x1', }); }); @@ -38,7 +38,7 @@ describe('onNameLookup', () => { } as any; expect(await onNameLookup(request)).toStrictEqual({ - resolvedDomain: 'test.domain - 0xc0f / 0x1', + resolvedDomain: 'example.domain - 0xc0f / 0x1', }); }); From 46673acd08029d0b915282824714a38554aaa376 Mon Sep 17 00:00:00 2001 From: Matthew Walsh Date: Thu, 21 Sep 2023 13:17:53 +0100 Subject: [PATCH 8/8] Update sha --- packages/examples/packages/name-lookup/snap.manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/examples/packages/name-lookup/snap.manifest.json b/packages/examples/packages/name-lookup/snap.manifest.json index 866ab6d9c7..4fdbf3b28d 100644 --- a/packages/examples/packages/name-lookup/snap.manifest.json +++ b/packages/examples/packages/name-lookup/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "2GGvHjj8BycVWAOhQboxK48YuqfpaA17q485itQ6/u4=", + "shasum": "K6qCrYmB0RD9wZK8t9JQskOu9SGPiffUEkVGJijm1bc=", "location": { "npm": { "filePath": "dist/bundle.js",