diff --git a/package.json b/package.json index 0d3d0083..545e7f18 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "https://multi.bitmatrix.app", "private": true, "dependencies": { - "@bitmatrix/lib": "^0.1.16", + "@bitmatrix/lib": "git+https://github.com/louisinger/bitmatrix-lib.git#update-psetv2", "@rsuite/icons": "^1.0.2", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.3.0", @@ -24,9 +24,10 @@ "@visx/shape": "^2.12.2", "@visx/tooltip": "^2.10.0", "@visx/xychart": "^2.12.2", + "bip32": "^4.0.0", "decimal.js": "^10.4.0", - "ldk": "^0.5.0", - "marina-provider": "^1.5.0", + "liquidjs-lib": "^6.0.2-liquid.27", + "marina-provider": "^2.0.0", "numeral": "^2.0.6", "react": "^18.2.0", "react-dom": "^18.2.0", @@ -38,7 +39,7 @@ "rsuite": "^5.17.1", "sass": "^1.54.4", "socket.io-client": "^4.5.1", - "tiny-secp256k1": "^1.1.6", + "tiny-secp256k1": "^2.2.1", "typescript": "^4.1.2", "web-vitals": "^1.0.1" }, diff --git a/src/components/WalletListModal/WalletListModal.tsx b/src/components/WalletListModal/WalletListModal.tsx index 017bfd25..e926cebd 100644 --- a/src/components/WalletListModal/WalletListModal.tsx +++ b/src/components/WalletListModal/WalletListModal.tsx @@ -1,8 +1,7 @@ import React, { useState } from 'react'; import { Loader, Modal } from 'rsuite'; import { useWalletContext } from '../../context'; -import { UnblindedOutput } from 'ldk'; -import { AddressInterface } from 'marina-provider'; +import { Address, UnblindedOutput } from 'marina-provider'; import MarinaIcon from '../base/Svg/Icons/Marina'; import AquaIcon from '../base/Svg/Icons/Aqua'; import JadeIcon from '../base/Svg/Icons/Jade'; @@ -15,7 +14,7 @@ type Props = { wallet?: Wallet; // walletOnClick: (walletName: WALLET_NAME) => void; close: () => void; - setNewAddress?: (newAddress: AddressInterface) => void; + setNewAddress?: (newAddress: Address) => void; setUtxos?: (utxos: UnblindedOutput[]) => void; }; diff --git a/src/helper/index.ts b/src/helper/index.ts index 60b3a94d..73462727 100644 --- a/src/helper/index.ts +++ b/src/helper/index.ts @@ -1,3 +1,4 @@ +import { AssetHash, ElementsValue } from 'liquidjs-lib'; import { useEffect, useRef } from 'react'; import { esplora } from '@bitmatrix/lib'; import { BmConfig, Pool, PAsset, CALL_METHOD } from '@bitmatrix/models'; @@ -208,9 +209,34 @@ export const padTo2Digits = (num: number): string => { return num.toString().padStart(2, '0'); }; -export const getMyPoolsChartData = async (coins: Utxo[] | undefined, assetHash?: string): Promise => { - if (coins && coins?.length > 0) { - const filteredCoins = coins.filter((coin) => coin.asset === assetHash); +const assetFilter = (assetHash: string) => (coin: Utxo) => { + if (coin.blindingData) { + return coin.blindingData.asset === assetHash; + } + if (coin.witnessUtxo) { + const asset = AssetHash.fromBytes(coin.witnessUtxo.asset); + if (asset.isConfidential) return false; + return asset.hex === assetHash; + } + + return false; +} + +const getCoinValue = (coin: Utxo) => { + if (coin.blindingData) { + return coin.blindingData.value; + } + if (coin.witnessUtxo) { + const value = ElementsValue.fromBytes(coin.witnessUtxo.value); + if (value.isConfidential) return undefined; + return value.number; + } + return undefined; +} + +export const getMyPoolsChartData = async (coins?: Utxo[], assetHash?: string): Promise => { + if (coins && coins?.length > 0 && assetHash) { + const filteredCoins = coins.filter(assetFilter(assetHash)); const outSpendsPromises = filteredCoins.map((coin) => esplora.txDetailPromise(coin.txid)); const outSpends = await Promise.all(outSpendsPromises); @@ -234,7 +260,7 @@ export const getMyPoolsChartData = async (coins: Utxo[] | undefined, assetHash?: const d: ChartData = { date: dateTime, - close: spent ? (coin.value || 0) * -1 : coin.value || 0, + close: spent ? (getCoinValue(coin) || 0) * -1 : getCoinValue(coin) || 0, }; return d; diff --git a/src/lib/liquid-dev/index.txt b/src/lib/liquid-dev/index.txt deleted file mode 100644 index 7743ff97..00000000 --- a/src/lib/liquid-dev/index.txt +++ /dev/null @@ -1,36 +0,0 @@ -import { fetchAndUnblindUtxos, UnblindedOutput } from 'ldk'; -import { AddressInterface } from 'marina-provider'; - -export const ESPLORA_API_URL = 'https://electrs.bitmatrix-aggregate.com'; - -//... - -// Get all addresses and blinding keys from marina -// const addrs = await window.marina.getAddresses(); - -export const fetchUTXOS = async (addresses: AddressInterface[]): Promise => { - // fetch and unblind all utxos for given array of address - // - // THIS CAN TAKE A LOT OF TIME TO COMPLETE! - // use fetchAndUnblindUtxosGenerator instead - const utxos = await fetchAndUnblindUtxos(addresses, ESPLORA_API_URL); - - // It will return an array of unblinded utxos - // we suggest to cache unblindData in order to speed-up - // future transaction building and blinding. - console.log(utxos); - /* -[ - { - txid: string; - vout: number; - asset?: string; - value?: number; - prevout?: TxOutput; - unblindData?: confidential.UnblindOutputResult; - } -] -*/ - - return utxos; -}; diff --git a/src/pages/Swap/Swap.tsx b/src/pages/Swap/Swap.tsx index 66d98014..71b2375f 100644 --- a/src/pages/Swap/Swap.tsx +++ b/src/pages/Swap/Swap.tsx @@ -1,3 +1,5 @@ +import * as ecc from 'tiny-secp256k1'; +import { BIP32Factory } from 'bip32'; import { useCallback, useEffect, useState } from 'react'; import Decimal from 'decimal.js'; import { Button, Content } from 'rsuite'; @@ -327,8 +329,14 @@ export const Swap: React.FC = ({ checkTxStatusWithIds }): JSX.Element => setLoading(true); const addressInformation = await walletContext.marina.getNextChangeAddress(); + const { masterXPub } = await walletContext.marina.getAccountInfo(addressInformation.accountName); + + + if (addressInformation.derivationPath) { + const addressPublicKey = BIP32Factory(ecc) + .fromBase58(masterXPub) + .derivePath(addressInformation.derivationPath.replace('m/', '')).publicKey.toString('hex') // remove m/ from path - if (addressInformation.publicKey) { // setSwapWay(undefined); setSelectedFromAmountPercent(undefined); @@ -342,7 +350,7 @@ export const Swap: React.FC = ({ checkTxStatusWithIds }): JSX.Element => numberToAmount, currentPool, testnetConfig, - addressInformation.publicKey, + addressPublicKey, lbtcAsset.assetHash, true, ); @@ -357,7 +365,7 @@ export const Swap: React.FC = ({ checkTxStatusWithIds }): JSX.Element => numberToAmount, currentPool, testnetConfig, - addressInformation.publicKey, + addressPublicKey, lbtcAsset.assetHash, true, );