Skip to content

Commit

Permalink
Update license and readme, lint cleanup (#28)
Browse files Browse the repository at this point in the history
* Update license, readme, reverseLookupBaseDomain

* Update snap version to 0.3.1

* Lint cleanup
  • Loading branch information
kaisung authored Jul 5, 2024
1 parent c3f9222 commit 85740a9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"json.schemas": [
{
"fileMatch": ["snap.manifest.json"],
Expand Down
34 changes: 25 additions & 9 deletions packages/snap/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# TypeScript Example Snap
# D3 Connect Snap

This snap demonstrates how to develop a snap with TypeScript. It is a simple
snap that displays a confirmation dialog when the `hello` JSON-RPC method is
called.
The D3 Connect Snap is a Metamask extension that enables custom name resolution using [D3 Connect](https://docs.d3.app/integrating-d3-infrastructure)

## Testing
MetaMask Snaps is a system that allows anyone to safely expand the capabilities
of MetaMask. A _snap_ is a program that we run in an isolated environment that
can customize the wallet experience.

The snap comes with some basic tests, to demonstrate how to write tests for
snaps. To test the snap, run `yarn test` in this directory. This will use
[`@metamask/snaps-jest`](https://github.com/MetaMask/snaps/tree/main/packages/snaps-jest)
to run the tests in `src/index.test.ts`.
## Snaps is pre-release software

To interact with the D3 Connect Snap, you will need to install [MetaMask Flask](https://metamask.io/flask/),
a canary distribution for developers that provides access to upcoming features.

## Getting Started

Clone the [d3-connect-snap repository](https://github.com/MetaMask/template-snap-monorepo/generate)
and set up the development environment:

```shell
yarn install && yarn start
```

This runs a local development server listening on http://localhost:8000. Navigate to http://localhost:8000/ in your browser where MetaMask Flask is installed and click the Connect button. This will open a prompt within MetaMask Flask to install the D3 Connect Snap extension.

Once installed, you can send crypto to any wallet using a D3 name (e.g. `d3connect.core`) that is mapped to a wallet address. To purchase your D3 name, go to the [D3 App
Marketplace](https://d3.app).

<img width="356" alt="d3connect-core-screenshot" src="https://github.com/d3-inc/d3-connect-snap/assets/629097/c62e0ebe-5613-4b48-9bd9-9ffd531faec0">
4 changes: 2 additions & 2 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@d3-inc/d3connect-snap",
"version": "0.3.0",
"version": "0.3.1",
"description": "Resolve domain/addresses using D3 Connect",
"repository": {
"type": "git",
"url": "https://github.com/d3-inc/d3-connect-snap.git"
},
"license": "UNLICENSED",
"license": "(MIT-0 OR Apache-2.0)",
"main": "./dist/bundle.js",
"files": [
"dist/",
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.3.0",
"version": "0.3.1",
"description": "Resolve domain/addresses using D3 Connect",
"proposedName": "D3 Connect",
"repository": {
"type": "git",
"url": "https://github.com/d3-inc/d3-connect-snap.git"
},
"source": {
"shasum": "goV5kXSgpbLNhi2xxHZIANhQFiXiv69O7XXZusLomGg=",
"shasum": "v63BIuG50TQdF3KMGt1xFz2CgPW3WIr2yeWq85cI/hE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
55 changes: 35 additions & 20 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { OnNameLookupHandler, OnRpcRequestHandler } from '@metamask/snaps-sdk';
import { panel, text } from '@metamask/snaps-sdk';
import { D3Connect } from '@d3-inc/d3connect'
import { D3Connect } from '@d3-inc/d3connect';
import type { OnNameLookupHandler } from '@metamask/snaps-sdk';

type ChainMetadata = {
name: string;
chain: string;
icon: string;
rpc: string[],
rpc: string[];
nativeCurrency: {
name: string;
symbol: string;
Expand All @@ -21,53 +20,69 @@ type ChainMetadata = {
url: string;
icon: string;
standard: string;
}[]
}
}[];
};

const d3Connect = new D3Connect({
dns: {
forwarderDomain: 'vana',
reverseLookupBaseDomain: 'web3-addr.vana',
reverseLookupBaseDomain: 'wallet.vana',
dnssecVerification: true,
}
},
});

async function fetchChainInformation(chainId: string) {
const chainMatch = chainId.match(/^eip155:(?<chainId>\d+)$/);
/**
* Fetches chain information from https://chainid.network/chains.json and returns ChainMetadata
* @param chainId
* @returns ChainMetadata
*/
async function fetchChainInformation(
chainId: string,
): Promise<ChainMetadata | undefined> {
const chainMatch = chainId.match(/^eip155:(?<chainId>\d+)$/u);
const id = chainMatch?.groups?.chainId;
if (!id) { return }
if (!id) {
return;
}
/* TODO: Cache results */
const response = await fetch('https://chainid.network/chains.json');
const data: ChainMetadata[] = await response.json();
return data.find(chain => chain.chainId === parseInt(id))
return data.find((chain) => chain.chainId === parseInt(id));
}

export const onNameLookup: OnNameLookupHandler = async (request) => {
console.log('onNameLookup1', request);
if (request.domain) {
const chainInfo = await fetchChainInformation(request.chainId);

if (!chainInfo) {
console.log(`Can't load chain info for ${request.chainId}`)
console.log(`Can't load chain info for ${request.chainId}`);
return null;
}

const resolvedAddress = await d3Connect.resolve(request.domain, chainInfo?.nativeCurrency?.symbol);
const resolvedAddress = await d3Connect.resolve(
request.domain,
chainInfo?.nativeCurrency?.symbol,
);

if (resolvedAddress) {
return {
resolvedAddresses: [{resolvedAddress, protocol: 'D3 Connect'}]
resolvedAddresses: [{ resolvedAddress, protocol: 'D3 Connect' }],
};
}
}

if (request.address) {
const resolvedDomain = await d3Connect.reverseResolve(request.address, 'ETH')
const resolvedDomain = await d3Connect.reverseResolve(
request.address,
'ETH',
);
if (resolvedDomain) {
return {
resolvedDomains: [{resolvedDomain, protocol: 'D3 Connect'}]
}
return {
resolvedDomains: [{ resolvedDomain, protocol: 'D3 Connect' }],
};
}
}

return null;
}
};

0 comments on commit 85740a9

Please sign in to comment.