Skip to content

Commit

Permalink
feat: integrate shapeshift snap
Browse files Browse the repository at this point in the history
  • Loading branch information
RyukTheCoder committed Jan 14, 2024
1 parent 25ef097 commit 795d29c
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 50 deletions.
183 changes: 152 additions & 31 deletions wallets/provider-shapeshift-snap/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,33 @@ import {
} from '@rango-dev/wallets-shared';

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

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

const snapNetworksConfig: {
chainId: Networks;
chainId: string;
method: string;
params: {
addressParams: {
coin: string;
addressNList: number[];
scriptType: string;
coin?: string;
addressNList?: number[];
scriptType?: string;
};
};
}[] = [
{
chainId: Networks.BTC,
method: 'btc_getPublicKeys',
method: 'btc_getAddress',
params: {
addressParams: {
coin: 'Bitcoin',
Expand All @@ -42,7 +46,7 @@ const snapNetworksConfig: {
},
{
chainId: Networks.BCH,
method: 'bch_getPublicKeys',
method: 'bch_getAddress',
params: {
addressParams: {
coin: 'BitcoinCash',
Expand All @@ -54,7 +58,7 @@ const snapNetworksConfig: {
},
{
chainId: Networks.LTC,
method: 'ltc_getPublicKeys',
method: 'ltc_getAddress',
params: {
addressParams: {
coin: 'Litecoin',
Expand All @@ -64,18 +68,111 @@ const snapNetworksConfig: {
},
},
},
/*
* {
* 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.DOGE,
method: 'doge_getPublicKeys',
chainId: Networks.COSMOS,
method: 'cosmos_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',
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() {
Expand All @@ -98,31 +195,55 @@ export const installSnap = async (instance: any) => {
return walletRequestSnaps({
instance,
snapId: DEFAULT_SNAP_ID,
version: '1.0.0',
version: DEFAULT_SNAP_VERSION,
});
};

export const getAccounts = async (
instance: any
): Promise<ProviderConnectResult[]> => {
const accountPromises = snapNetworksConfig.map(async (item) => {
const address = await walletInvokeSnap({
instance,
params: {
snapId: DEFAULT_SNAP_ID,
request: {
method: item.method,
params: item.params,
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);

return {
accounts: [address?.[0]?.xpub],
chainId: item.chainId,
};
});
availableAccountForChains.forEach((result) => {
if (result.status === 'fulfilled') {
const { accounts, chainId } = result.value;
resolvedAccounts.push({ accounts, chainId });
}
});

index += MAX_CONCURRENT_REQUESTS;
}

const accounts = await Promise.all(accountPromises);
return accounts;
return resolvedAccounts;
};
21 changes: 4 additions & 17 deletions wallets/provider-shapeshift-snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {
CanEagerConnect,
Connect,
Networks,
ProviderConnectResult,
Expand Down Expand Up @@ -33,27 +32,15 @@ export const connect: Connect = async ({ instance }: { instance: any }) => {
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 canEagerConnect: CanEagerConnect = async ({
instance,
}: {
instance: any;
}) => {
try {
const accounts = await getAccounts(instance);
if (accounts.length) {
return true;
}
return false;
} catch (error) {
return false;
}
};

export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
allBlockChains
) => {
Expand Down
13 changes: 11 additions & 2 deletions wallets/provider-shapeshift-snap/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
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.EVM, new DefaultEvmSigner(provider));
signers.registerSigner(
TxType.TRANSFER,
new ShapeShiftSnapBaseSigner(provider)
);
signers.registerSigner(
TxType.COSMOS,
new ShapeShiftSnapCosmosSigner(provider)
);
return signers;
}
Loading

0 comments on commit 795d29c

Please sign in to comment.