Skip to content

Commit

Permalink
all the coins
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Oct 3, 2024
1 parent 7df95ed commit 9a17efe
Show file tree
Hide file tree
Showing 31 changed files with 1,280 additions and 303 deletions.
6 changes: 4 additions & 2 deletions chrome-extension/public/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
litecoin: createWalletObject('litecoin'),
thorchain: createWalletObject('thorchain'),
mayachain: createWalletObject('mayachain'),
solana: createWalletObject('solana'),
// solana: createWalletObject('solana'),
};

const keepkey = {
Expand All @@ -238,11 +238,13 @@
dogecoin: createWalletObject('dogecoin'),
dash: createWalletObject('dash'),
ethereum: ethereum,
osmosis: createWalletObject('osmosis'),
cosmos: createWalletObject('cosmos'),
litecoin: createWalletObject('litecoin'),
thorchain: createWalletObject('thorchain'),
mayachain: createWalletObject('mayachain'),
solana: createWalletObject('solana'),
ripple: createWalletObject('ripple'),
// solana: createWalletObject('solana'),
};

const handler = {
Expand Down
20 changes: 6 additions & 14 deletions chrome-extension/src/background/chains/bitcoinCashHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export const handleBitcoinCashRequest = async (
console.log(tag, 'caip: ', caip);
const networkId = caipToNetworkId(caip);
requestInfo.id = uuidv4();
console.log(tag, 'assetContext: ', KEEPKEY_WALLET);
//push event to ux
chrome.runtime.sendMessage({
action: 'TRANSACTION_CONTEXT_UPDATED',
id: requestInfo.id,
});

// eslint-disable-next-line no-constant-condition
if (!KEEPKEY_WALLET.assetContext) {
// Set context to the chain, defaults to ETH
Expand All @@ -78,24 +83,11 @@ export const handleBitcoinCashRequest = async (
console.log(tag, 'pubkeys: ', pubkeys);
if (!pubkeys || pubkeys.length === 0) throw Error('Failed to locate pubkeys for chain ' + Chain.BitcoinCash);

console.log(tag, 'params[0]: ', params[0]);
const assetString = 'BCH.BCH';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
const assetValue = await AssetValue.fromString(assetString, parseFloat(params[0].amount.amount));

const wallet = await KEEPKEY_WALLET.swapKit.getWallet(Chain.BitcoinCash);
if (!wallet) throw new Error('Failed to init swapkit');
const walletAddress = await wallet.getAddress();
console.log(tag, 'walletAddress: ', walletAddress);

const sendPayload = {
from: walletAddress, // Select preference change address
assetValue,
memo: params[0].memo || '',
recipient: params[0].recipient,
};

const buildTx = async function () {
try {
const utxos = [];
Expand Down
19 changes: 5 additions & 14 deletions chrome-extension/src/background/chains/bitcoinHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export const handleBitcoinRequest = async (
console.log(tag, 'caip: ', caip);
const networkId = caipToNetworkId(caip);
requestInfo.id = uuidv4();
console.log(tag, 'assetContext: ', KEEPKEY_WALLET);
//push event to ux
chrome.runtime.sendMessage({
action: 'TRANSACTION_CONTEXT_UPDATED',
id: requestInfo.id,
});
// eslint-disable-next-line no-constant-condition
if (!KEEPKEY_WALLET.assetContext) {
// Set context to the chain, defaults to ETH
Expand All @@ -82,24 +86,11 @@ export const handleBitcoinRequest = async (
console.log(tag, 'pubkeys: ', pubkeys);
if (!pubkeys || pubkeys.length === 0) throw Error('Failed to locate pubkeys for chain ' + Chain.Bitcoin);

console.log(tag, 'params[0]: ', params[0]);
const assetString = 'BTC.BTC';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
const assetValue = await AssetValue.fromString(assetString, parseFloat(params[0].amount.amount));

const wallet = await KEEPKEY_WALLET.swapKit.getWallet(Chain.Bitcoin);
if (!wallet) throw new Error('Failed to init swapkit');
const walletAddress = await wallet.getAddress();
console.log(tag, 'walletAddress: ', walletAddress);

const sendPayload = {
from: walletAddress, // Select preference change address
assetValue,
memo: params[0].memo || '',
recipient: params[0].recipient,
};

const buildTx = async function () {
try {
const utxos = [];
Expand Down
42 changes: 33 additions & 9 deletions chrome-extension/src/background/chains/cosmosHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const TAG = ' | cosmosHandler | ';
import { JsonRpcProvider } from 'ethers';
import { Chain } from '@coinmasters/types';
import { AssetValue } from '@pioneer-platform/helpers';
import { requestStorage, web3ProviderStorage, assetContextStorage } from '@extension/storage';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
Expand Down Expand Up @@ -63,17 +64,19 @@ export const handleCosmosRequest = async (
const caip = shortListSymbolToCaip['ATOM'];
console.log(tag, 'caip: ', caip);
const networkId = caipToNetworkId(caip);
requestInfo.id = uuidv4();
console.log(tag, 'requestInfo: ', requestInfo);
//push event to ux
chrome.runtime.sendMessage({
action: 'TRANSACTION_CONTEXT_UPDATED',
id: requestInfo.id,
});
//verify context is bitcoin
if (!KEEPKEY_WALLET.assetContext) {
// Set context to the chain, defaults to ETH
const caip = shortListSymbolToCaip['BTC'];
await KEEPKEY_WALLET.setAssetContext({ caip });
}
// Require user approval
const result = await requireApproval(networkId, requestInfo, 'bitcoin', method, params[0]);
console.log(tag, 'result:', result);
//send tx
console.log(tag, 'params[0]: ', params[0]);

let assetString = 'GAIA.ATOM';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
Expand All @@ -84,9 +87,30 @@ export const handleCosmosRequest = async (
memo: params[0].memo || '',
recipient: params[0].recipient,
};
const txHash = await KEEPKEY_WALLET.swapKit.transfer(sendPayload);
console.log(tag, 'txHash: ', txHash);
return txHash;
console.log(tag, 'sendPayload: ', sendPayload);
requestInfo.unsignedTx = sendPayload;
// Require user approval
const result = await requireApproval(networkId, requestInfo, 'cosmos', method, params[0]);
console.log(tag, 'result:', result);
//send tx
console.log(tag, 'params[0]: ', params[0]);

if (result.success) {
const txHash = await KEEPKEY_WALLET.swapKit.transfer(sendPayload);
console.log(tag, 'txHash: ', txHash);

const response = await requestStorage.getEventById(requestInfo.id);
console.log(tag, 'response: ', response);
response.txid = txHash;
await requestStorage.updateEventById(requestInfo.id, response);
chrome.runtime.sendMessage({
action: 'transaction_complete',
txHash: txHash,
});
return txHash;
} else {
throw createProviderRpcError(4200, 'User denied transaction');
}
}
default: {
console.log(tag, `Method ${method} not supported`);
Expand Down
30 changes: 13 additions & 17 deletions chrome-extension/src/background/chains/dashHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ export const handleDashRequest = async (
return [balance];
}
case 'transfer': {
//Xdefi compataiblity layer
if (!params[0]?.amount?.amount) throw Error('Invalid transfer params!');

const caip = shortListSymbolToCaip['DASH'];
console.log(tag, 'caip: ', caip);
const networkId = caipToNetworkId(caip);
requestInfo.id = uuidv4();
console.log(tag, 'assetContext: ', KEEPKEY_WALLET);
//push event to ux
chrome.runtime.sendMessage({
action: 'TRANSACTION_CONTEXT_UPDATED',
id: requestInfo.id,
});

// eslint-disable-next-line no-constant-condition
if (!KEEPKEY_WALLET.assetContext) {
// Set context to the chain, defaults to ETH
Expand All @@ -79,24 +87,11 @@ export const handleDashRequest = async (
console.log(tag, 'pubkeys: ', pubkeys);
if (!pubkeys || pubkeys.length === 0) throw Error('Failed to locate pubkeys for chain ' + Chain.Dash);

console.log(tag, 'params[0]: ', params[0]);
const assetString = 'DASH.DASH';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
const assetValue = await AssetValue.fromString(assetString, parseFloat(params[0].amount.amount));

const wallet = await KEEPKEY_WALLET.swapKit.getWallet(Chain.Dash);
if (!wallet) throw new Error('Failed to init swapkit');
const walletAddress = await wallet.getAddress();
console.log(tag, 'walletAddress: ', walletAddress);

const sendPayload = {
from: walletAddress, // Select preference change address
assetValue,
memo: params[0].memo || '',
recipient: params[0].recipient,
};

const buildTx = async function () {
try {
const utxos = [];
Expand Down Expand Up @@ -139,7 +134,6 @@ export const handleDashRequest = async (
utxo.value = Number(utxo.value);
}
console.log('utxos: ', utxos);

const amountOut: number = Math.floor(Number(params[0].amount.amount) * 1e8);

console.log(tag, 'amountOut: ', amountOut);
Expand Down Expand Up @@ -198,8 +192,10 @@ export const handleDashRequest = async (
response.signedTx = signedTx;
await requestStorage.updateEventById(requestInfo.id, response);

const txHash = await wallet.broadcastTx(signedTx);

let txHash = await wallet.broadcastTx(signedTx);
console.log(tag, 'txHash: ', txHash);
if (txHash.txHash) txHash = txHash.txHash;
if (txHash.txid) txHash = txHash.txid;
response.txid = txHash;
await requestStorage.updateEventById(requestInfo.id, response);

Expand Down
19 changes: 5 additions & 14 deletions chrome-extension/src/background/chains/dogecoinHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export const handleDogecoinRequest = async (
console.log(tag, 'caip: ', caip);
const networkId = caipToNetworkId(caip);
requestInfo.id = uuidv4();
console.log(tag, 'assetContext: ', KEEPKEY_WALLET);
//push event to ux
chrome.runtime.sendMessage({
action: 'TRANSACTION_CONTEXT_UPDATED',
id: requestInfo.id,
});
// eslint-disable-next-line no-constant-condition
if (!KEEPKEY_WALLET.assetContext) {
// Set context to the chain, defaults to ETH
Expand All @@ -76,24 +80,11 @@ export const handleDogecoinRequest = async (
console.log(tag, 'pubkeys: ', pubkeys);
if (!pubkeys || pubkeys.length === 0) throw Error('Failed to locate pubkeys for chain ' + Chain.Dogecoin);

console.log(tag, 'params[0]: ', params[0]);
const assetString = 'DOGE.DOGE';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
const assetValue = await AssetValue.fromString(assetString, parseFloat(params[0].amount.amount));

const wallet = await KEEPKEY_WALLET.swapKit.getWallet(Chain.Dogecoin);
if (!wallet) throw new Error('Failed to init swapkit');
const walletAddress = await wallet.getAddress();
console.log(tag, 'walletAddress: ', walletAddress);

const sendPayload = {
from: walletAddress, // Select preference change address
assetValue,
memo: params[0].memo || '',
recipient: params[0].recipient,
};

const buildTx = async function () {
try {
const utxos = [];
Expand Down
12 changes: 11 additions & 1 deletion chrome-extension/src/background/chains/ethereumHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ const handleSigningMethods = async (method, params, requestInfo, ADDRESS, KEEPKE
}
};

const convertToHex = (amountInEther: string) => {
const weiMultiplier = BigInt(1e18); // 1 Ether = 1e18 Wei
const amountInWei = BigInt(parseFloat(amountInEther || '0') * 1e18); // Convert Ether to Wei

// Convert the amount in Wei to a hex string
return '0x' + amountInWei.toString(16);
};

// For 'transfer', build transaction info before calling requireApproval
const handleTransfer = async (params, requestInfo, ADDRESS, KEEPKEY_WALLET, requireApproval) => {
const tag = TAG + ' | handleTransfer | ';
Expand All @@ -335,7 +343,9 @@ const handleTransfer = async (params, requestInfo, ADDRESS, KEEPKEY_WALLET, requ

// Build transaction info before requireApproval
const transaction = params[0];

console.log(tag, 'transaction:', transaction);
transaction.value = convertToHex(transaction.amount.amount);
delete transaction.amount;
// Ensure 'from' is set
transaction.from = transaction.from || ADDRESS;

Expand Down
67 changes: 45 additions & 22 deletions chrome-extension/src/background/chains/mayaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export const handleMayaRequest = async (
switch (method) {
case 'request_accounts': {
//Unsigned TX
let pubkeys = KEEPKEY_WALLET.pubkeys.filter((e: any) => e.networks.includes(ChainToNetworkId[Chain.Mayachain]));
let accounts = [];
const pubkeys = KEEPKEY_WALLET.pubkeys.filter((e: any) => e.networks.includes(ChainToNetworkId[Chain.Mayachain]));
const accounts = [];
for (let i = 0; i < pubkeys.length; i++) {
let pubkey = pubkeys[i];
let address = pubkey.master || pubkey.address;
const pubkey = pubkeys[i];
const address = pubkey.master || pubkey.address;
accounts.push(address);
}
console.log(tag, 'accounts: ', accounts);
Expand All @@ -51,39 +51,62 @@ export const handleMayaRequest = async (
}
case 'request_balance': {
//get sum of all pubkeys configured
let pubkeys = await KEEPKEY_WALLET.swapKit.getBalance(Chain.Litecoin);
const pubkeys = await KEEPKEY_WALLET.swapKit.getBalance(Chain.Litecoin);
console.log(tag, 'pubkeys: ', pubkeys);
return [pubkeys];
}
case 'transfer': {
const caip = shortListSymbolToCaip['MAYA'];
console.log(tag, 'caip: ', caip);
await KEEPKEY_WALLET.setAssetContext({ caip });
const networkId = caipToNetworkId(caip);
requestInfo.id = uuidv4();
console.log(tag, 'requestInfo: ', requestInfo);
//push event to ux
chrome.runtime.sendMessage({
action: 'TRANSACTION_CONTEXT_UPDATED',
id: requestInfo.id,
});

//verify context is bitcoin
if (!KEEPKEY_WALLET.assetContext) {
// Set context to the chain, defaults to ETH
await KEEPKEY_WALLET.setAssetContext({ caip });
}
// Require user approval
const result = await requireApproval(networkId, requestInfo, 'bitcoin', method, params[0]);
const result = await requireApproval(networkId, requestInfo, 'mayachain', method, params[0]);
console.log(tag, 'result:', result);

//send tx
console.log(tag, 'params[0]: ', params[0]);
let assetString = 'MAYA.CACAO';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
let assetValue = await AssetValue.fromString(assetString, parseFloat(params[0].amount.amount));
let sendPayload = {
from: params[0].from,
assetValue,
memo: params[0].memo || '',
recipient: params[0].recipient,
};
console.log(tag, 'sendPayload: ', sendPayload);
const txHash = await KEEPKEY_WALLET.swapKit.transfer(sendPayload);
console.log(tag, 'txHash: ', txHash);
return txHash;
if (result.success) {
//send tx
console.log(tag, 'params[0]: ', params[0]);
const assetString = 'MAYA.CACAO';
await AssetValue.loadStaticAssets();
console.log(tag, 'params[0].amount.amount: ', params[0].amount.amount);
const assetValue = await AssetValue.fromString(assetString, parseFloat(params[0].amount.amount));
const sendPayload = {
from: params[0].from,
assetValue,
memo: params[0].memo || '',
recipient: params[0].recipient,
};
console.log(tag, 'sendPayload: ', sendPayload);
const txHash = await KEEPKEY_WALLET.swapKit.transfer(sendPayload);
console.log(tag, 'txHash: ', txHash);

const response = await requestStorage.getEventById(requestInfo.id);
console.log(tag, 'response: ', response);
response.txid = txHash;
await requestStorage.updateEventById(requestInfo.id, response);
chrome.runtime.sendMessage({
action: 'transaction_complete',
txHash: txHash,
});

return txHash;
} else {
throw createProviderRpcError(4200, 'User denied transaction');
}
}
default: {
console.log(tag, `Method ${method} not supported`);
Expand Down
Loading

0 comments on commit 9a17efe

Please sign in to comment.