Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!WIP feat: integrate shapeshift snap #492

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": [
"javascript",
Expand Down
1 change: 1 addition & 0 deletions wallets/provider-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@rango-dev/provider-phantom": "^0.25.1-next.1",
"@rango-dev/provider-safe": "^0.18.1-next.1",
"@rango-dev/provider-safepal": "^0.25.1-next.1",
"@rango-dev/provider-shapeshift-snap": "^0.25.1-next.1",
"@rango-dev/provider-taho": "^0.25.1-next.1",
"@rango-dev/provider-tokenpocket": "^0.25.1-next.1",
"@rango-dev/provider-tron-link": "^0.25.1-next.1",
Expand Down
2 changes: 2 additions & 0 deletions wallets/provider-all/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as okx from '@rango-dev/provider-okx';
import * as phantom from '@rango-dev/provider-phantom';
import * as safe from '@rango-dev/provider-safe';
import * as safepal from '@rango-dev/provider-safepal';
import * as shapeShiftSnap from '@rango-dev/provider-shapeshift-snap';
import * as taho from '@rango-dev/provider-taho';
import * as tokenpocket from '@rango-dev/provider-tokenpocket';
import * as tronLink from '@rango-dev/provider-tron-link';
Expand Down Expand Up @@ -59,5 +60,6 @@ export const allProviders = (enviroments?: Enviroments) => {
frontier,
taho,
braavos,
shapeShiftSnap,
];
};
Empty file.
31 changes: 31 additions & 0 deletions wallets/provider-shapeshift-snap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@rango-dev/provider-shapeshift-snap",
"version": "0.25.1-next.1",
"license": "MIT",
"type": "module",
"source": "./src/index.ts",
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js"
},
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"scripts": {
"build": "node ../../scripts/build/command.mjs --path wallets/provider-shapeshift-snap",
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
"clean": "rimraf dist",
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'",
"lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore"
},
"dependencies": {
"@rango-dev/signer-evm": "^0.25.0",
"@rango-dev/wallets-shared": "^0.25.1-next.1",
"rango-types": "^0.1.57"
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions wallets/provider-shapeshift-snap/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @rango-dev/provider-shapeshift-snap
249 changes: 249 additions & 0 deletions wallets/provider-shapeshift-snap/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
import type { ProviderConnectResult } from '@rango-dev/wallets-shared';

import {
getCoinbaseInstance,
Networks,
walletInvokeSnap,
walletRequestSnaps,
} from '@rango-dev/wallets-shared';

export const DEFAULT_SNAP_ID = 'npm:@shapeshiftoss/metamask-snaps';
export const DEFAULT_SNAP_VERSION = '1.0.0';

export const SHAPESHIFT_SNAP_SUPPORTED_CHAINS = [
Networks.BTC,
Networks.BCH,
Networks.COSMOS,
Networks.OSMOSIS,
Networks.DOGE,
Networks.ETHEREUM,
Networks.LTC,
Networks.THORCHAIN,
];

const snapNetworksConfig: {
chainId: string;
method: string;
params: {
addressParams: {
coin?: string;
addressNList?: number[];
scriptType?: string;
};
};
}[] = [
{
chainId: Networks.BTC,
method: 'btc_getAddress',
params: {
addressParams: {
coin: 'Bitcoin',
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
addressNList: [0x80000000 + 44, 0x80000000 + 0, 0x80000000 + 0, 0, 0],
scriptType: 'p2pkh',
},
},
},
{
chainId: Networks.BCH,
method: 'bch_getAddress',
params: {
addressParams: {
coin: 'BitcoinCash',
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
addressNList: [0x80000000 + 44, 0x80000000 + 145, 0x80000000 + 0, 0, 0],
scriptType: 'p2pkh',
},
},
},
{
chainId: Networks.LTC,
method: 'ltc_getAddress',
params: {
addressParams: {
coin: 'Litecoin',
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
addressNList: [0x80000000 + 44, 0x80000000 + 2, 0x80000000 + 0, 0, 0],
scriptType: 'p2pkh',
},
},
},
/*
* {
* chainId: Networks.DOGE,
* method: 'doge_getAddress',
* params: {
* addressParams: {
* coin: 'Dogecoin',
* // eslint-disable-next-line @typescript-eslint/no-magic-numbers
* addressNList: [0x80000000 + 44, 0x80000000 + 3, 0x80000000 + 0, 0, 0],
* scriptType: 'p2pkh',
* },
* },
* },
*/
/*
* {
* chainId: Networks.BINANCE,
* method: 'binance_getAddress',
* params: {
* addressParams: {
* // eslint-disable-next-line @typescript-eslint/no-magic-numbers
* addressNList: [0x80000000 + 44, 0x80000000 + 714, 0x80000000 + 0, 0, 0],
* },
* },
* },
*/

{
chainId: Networks.COSMOS,
method: 'cosmos_getAddress',
params: {
addressParams: {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
addressNList: [0x80000000 + 44, 0x80000000 + 118, 0x80000000 + 0, 0, 0],
},
},
},
/*
* {
* chainId: Networks.ETHEREUM,
* method: 'eth_getAddress',
* params: {
* addressParams: {
* // eslint-disable-next-line @typescript-eslint/no-magic-numbers
* addressNList: [0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0],
* },
* },
* },
*/
{
chainId: 'osmosis-1',
method: 'osmosis_getAddress',
params: {
addressParams: {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
addressNList: [0x80000000 + 44, 0x80000000 + 118, 0x80000000 + 0, 0, 0],
},
},
},
/*
* {
* chainId: Networks.SECRET,
* method: 'secret_getAddress',
* params: {
* addressParams: {
* // eslint-disable-next-line @typescript-eslint/no-magic-numbers
* addressNList: [0x80000000 + 44, 0x80000000 + 529, 0x80000000 + 0, 0, 0],
* },
* },
* },
*/
/*
* {
* chainId: Networks.TERRA,
* method: 'terra_getAddress',
* params: {
* addressParams: {
* // eslint-disable-next-line @typescript-eslint/no-magic-numbers
* addressNList: [0x80000000 + 44, 0x80000000 + 330, 0x80000000 + 0, 0, 0],
* },
* },
* },
*/
{
chainId: Networks.THORCHAIN,
method: 'thorchain_getAddress',
params: {
addressParams: {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
addressNList: [0x80000000 + 44, 0x80000000 + 931, 0x80000000 + 0, 0, 0],
},
},
},
/*
* {
* chainId: Networks.AVAX_CCHAIN,
* method: 'avax_getAddress',
* params: {
* addressParams: {
* // eslint-disable-next-line @typescript-eslint/no-magic-numbers
* addressNList: [0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0],
* },
* },
* },
*/
];

export function metamask() {
const isCoinbaseWalletAvailable = !!getCoinbaseInstance();
const { ethereum } = window;

// Some wallets overriding the metamask. So we need to get it properly.
if (isCoinbaseWalletAvailable) {
// Getting intance from overrided structure from coinbase.
return getCoinbaseInstance('metamask');
}
if (!!ethereum && ethereum.isMetaMask) {
return ethereum;
}

return null;
}

export const installSnap = async (instance: any) => {
return walletRequestSnaps({
instance,
snapId: DEFAULT_SNAP_ID,
version: DEFAULT_SNAP_VERSION,
});
};

export const getAccounts = async (
instance: any
): Promise<ProviderConnectResult[]> => {
const MAX_CONCURRENT_REQUESTS = 4; // Implemented this way to revent following error: "Exceeds maximum number of requests waiting to be resolved"

const resolvedAccounts: ProviderConnectResult[] = [];
let index = 0;

while (index < snapNetworksConfig.length) {
const batchConfigs = snapNetworksConfig.slice(
index,
index + MAX_CONCURRENT_REQUESTS
);
const batchPromises = batchConfigs.map(async (item) => {
let address: string = await walletInvokeSnap({
instance,
params: {
snapId: DEFAULT_SNAP_ID,
request: {
method: item.method,
params: item.params,
},
},
});

if (address.includes(':')) {
address = address.split(':')[1]; // this line is here because bitcoin cash address returns like "bitcoincash:..."
}

return {
accounts: [address],
chainId: item.chainId,
};
});
const availableAccountForChains = await Promise.allSettled(batchPromises);

availableAccountForChains.forEach((result) => {
if (result.status === 'fulfilled') {
const { accounts, chainId } = result.value;
resolvedAccounts.push({ accounts, chainId });
}
});

index += MAX_CONCURRENT_REQUESTS;
}

return resolvedAccounts;
};
63 changes: 63 additions & 0 deletions wallets/provider-shapeshift-snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type {
Connect,
Networks,
ProviderConnectResult,
WalletInfo,
} from '@rango-dev/wallets-shared';
import type { BlockchainMeta, SignerFactory } from 'rango-types';

import { WalletTypes } from '@rango-dev/wallets-shared';

import {
getAccounts,
installSnap,
metamask as metamask_instance,
SHAPESHIFT_SNAP_SUPPORTED_CHAINS,
} from './helpers';
import signer from './signer';

const WALLET = WalletTypes.SHAPESHIFT_SNAP;

export const config = {
type: WALLET,
};

export const getInstance = metamask_instance;
export const connect: Connect = async ({ instance }: { instance: any }) => {
let accounts: ProviderConnectResult[] = [];

const installed = await installSnap(instance);

if (!!installed) {
accounts = await getAccounts(instance);
}

if (!accounts?.length) {
throw new Error('Please make sure ShapeShift Snap is installed.');
}

return accounts;
};

export const getSigners: (provider: any) => SignerFactory = signer;

export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
allBlockChains
) => {
return {
name: 'ShapeShift Snap',
img: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/metamask/icon.svg',
installLink: {
CHROME: 'https://shapeshift.com/snap',
BRAVE: 'https://shapeshift.com/snap',

FIREFOX: 'https://shapeshift.com/snap',
EDGE: 'https://shapeshift.com/snap',
DEFAULT: 'https://shapeshift.com/snap',
},
color: '#dac7ae',
supportedChains: allBlockChains.filter((blockchainMeta) =>
SHAPESHIFT_SNAP_SUPPORTED_CHAINS.includes(blockchainMeta.name as Networks)
),
};
};
19 changes: 19 additions & 0 deletions wallets/provider-shapeshift-snap/src/signer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { SignerFactory } from 'rango-types';

import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

import ShapeShiftSnapBaseSigner from './signer/shapeShiftSnapBaseSigner';
import ShapeShiftSnapCosmosSigner from './signer/shapeShiftSnapCosmosSigner';

export default function getSigners(provider: any): SignerFactory {
const signers = new DefaultSignerFactory();
signers.registerSigner(
TxType.TRANSFER,
new ShapeShiftSnapBaseSigner(provider)
);
signers.registerSigner(
TxType.COSMOS,
new ShapeShiftSnapCosmosSigner(provider)
);
return signers;
}
Loading