From 3f782a6da0b4173a9b07555d8b5a78b59b521a78 Mon Sep 17 00:00:00 2001 From: Harpalsinh Jadeja Date: Mon, 29 Apr 2024 22:54:18 +0530 Subject: [PATCH] feat: snap working --- packages/snap/snap.manifest.json | 7 +-- packages/snap/src/index.ts | 93 ++++++++++++++++---------------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index 99afbdc..1ac305d 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -1,13 +1,13 @@ { "version": "0.1.0", - "description": "An example Snap written in TypeScript.", - "proposedName": "TypeScript Example", + "description": "Name resolution snap that uses SocialConnect.", + "proposedName": "SocialConnect Snap", "repository": { "type": "git", "url": "https://github.com/MetaMask/template-snap-monorepo.git" }, "source": { - "shasum": "fqEE3NBrSt4mUdKBBcrSMMMr1t0F0LAQnokYOpef13Q=", + "shasum": "D2JRdLb8M9JLAizHljspgwqYhcOYy9sUMgGo+IJ8J9c=", "location": { "npm": { "filePath": "dist/bundle.js", @@ -18,6 +18,7 @@ }, "initialPermissions": { "snap_dialog": {}, + "endowment:network-access": {}, "endowment:name-lookup": { "chains": [ "eip155:42220" diff --git a/packages/snap/src/index.ts b/packages/snap/src/index.ts index cc522b2..3f31069 100644 --- a/packages/snap/src/index.ts +++ b/packages/snap/src/index.ts @@ -1,42 +1,11 @@ -import type { - OnNameLookupHandler, - OnRpcRequestHandler, -} from '@metamask/snaps-sdk'; -import { ChainDisconnectedError, panel, text } from '@metamask/snaps-sdk'; - -/** - * Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`. - * - * @param args - The request handler args as object. - * @param args.origin - The origin of the request, e.g., the website that - * invoked the snap. - * @param args.request - A validated JSON-RPC request object. - * @returns The result of `snap_dialog`. - * @throws If the request method is not valid for this snap. - */ -export const onRpcRequest: OnRpcRequestHandler = async ({ - origin, - request, -}) => { - switch (request.method) { - case 'hello': - return snap.request({ - method: 'snap_dialog', - params: { - type: 'confirmation', - content: panel([ - text(`Hello, **${origin}**!`), - text('This custom confirmation is just for display purposes.'), - text( - 'But you can edit the snap source code to make it do something, if you want to!', - ), - ]), - }, - }); - default: - throw new Error('Method not found.'); - } -}; +import type { OnNameLookupHandler } from '@metamask/snaps-sdk'; + +const E164_REGEX = /^\+[1-9][0-9]{1,14}$/; + +export function isE164Number(phoneNumber: string | undefined) { + if (phoneNumber) return E164_REGEX.test(phoneNumber); + return false; +} export const onNameLookup: OnNameLookupHandler = async ({ chainId, @@ -45,12 +14,46 @@ export const onNameLookup: OnNameLookupHandler = async ({ }) => { if (domain && chainId === 'eip155:42220') { if (domain) { - const resolvedAddress = '0xc0ffee254729296a45a3885639AC7E10F9d54979'; - return { - resolvedAddresses: [ - { resolvedAddress, protocol: 'Unstoppable Domains' }, - ], - }; + const domainSplit = domain.split(':'); + const issuer = domainSplit[0]; + const identifier = domainSplit[1]; + + if ( + domainSplit.length > 1 && + issuer && + identifier && + identifier.length >= 12 && + isE164Number(identifier) + ) { + console.log('All good'); + + console.log(identifier); + + console.log(JSON.stringify({ handle: identifier })); + + const response = await fetch( + `https://minipay-lookup-service-react-app.vercel.app/api/socialconnect/lookup?handle=${encodeURIComponent( + identifier, + )}`, + ); + + const data = await response.json(); + + const { accounts } = data; + + console.log(accounts); + + return { + resolvedAddresses: [ + { + resolvedAddress: accounts[0], + protocol: 'MiniPay SocialConnect', + }, + ], + }; + } else { + return null; + } } if (address) {