Skip to content

Commit

Permalink
feat: snap working
Browse files Browse the repository at this point in the history
  • Loading branch information
therealharpaljadeja committed Apr 29, 2024
1 parent 30e8e4e commit 3f782a6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 48 deletions.
7 changes: 4 additions & 3 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -18,6 +18,7 @@
},
"initialPermissions": {
"snap_dialog": {},
"endowment:network-access": {},
"endowment:name-lookup": {
"chains": [
"eip155:42220"
Expand Down
93 changes: 48 additions & 45 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -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}$/;

Check failure on line 3 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Use the 'u' flag

export function isE164Number(phoneNumber: string | undefined) {

Check failure on line 5 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Missing JSDoc comment
if (phoneNumber) return E164_REGEX.test(phoneNumber);

Check failure on line 6 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Expected { after 'if' condition
return false;
}

export const onNameLookup: OnNameLookupHandler = async ({
chainId,
Expand All @@ -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 {

Check failure on line 54 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Unnecessary 'else' after 'return'
return null;
}
}

if (address) {
Expand Down

0 comments on commit 3f782a6

Please sign in to comment.