Skip to content

Commit

Permalink
feat: prase sign coin spend
Browse files Browse the repository at this point in the history
  • Loading branch information
ahwei committed Mar 3, 2023
1 parent 55e5724 commit d9a1d67
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/api/extension/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export const permission = {
[RequestMethodEnum.TRANSFER]: RequestMethodEnum.TRANSFER,
[RequestMethodEnum.SEND_TRANSACTION]:
RequestMethodEnum.SEND_TRANSACTION,
[RequestMethodEnum.SIGN_COIN_SPENDS]:
RequestMethodEnum.SIGN_COIN_SPENDS,
},
}
23 changes: 19 additions & 4 deletions src/popup/components/spendBundleInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import AssetIcon from '~/components/AssetIcon'
import MemoDisplay from '~/components/Transaction/MemoDisplay'
import rootStore from '~/store'
import { ISpendBundleParse } from '~/types/api'
import { MethodEnum } from '~/types/extension'
import { MethodEnum, RequestMethodEnum } from '~/types/extension'
import { shortenHash } from '~/utils'
import { mojoToCat, mojoToXch } from '~/utils/CoinConverter'
import { add0x } from '~/utils/encryption'

import { IPopupPageProps } from '../types'
function spendBundleInfo({ request }: IPopupPageProps<MethodEnum.REQUEST>) {
Expand Down Expand Up @@ -46,19 +47,33 @@ function spendBundleInfo({ request }: IPopupPageProps<MethodEnum.REQUEST>) {
}
}, [])

const onGetParseSpendBundle = async () => {
const onGetParseSpendBundle = async (spendBundle) => {
try {
const res = await getParseSpendBundle({
data: {
spend_bundle: request?.data?.params?.spendBundle,
spend_bundle: spendBundle,
},
})
setParseBundle(res?.data.data)
} catch (error) {}
}

useLayoutEffect(() => {
onGetParseSpendBundle()
let spendBundle = {}
if (request.data?.method === RequestMethodEnum.SEND_TRANSACTION) {
spendBundle = request?.data?.params?.spendBundle
}

if (request.data?.method === RequestMethodEnum.SIGN_COIN_SPENDS) {
spendBundle = {
coin_spends: request?.data?.params?.coinSpends,
aggregated_signature: add0x(
'0c' + Array(191).fill('0').join('')
),
}
}

onGetParseSpendBundle(spendBundle)
}, [request?.data?.params?.spendBundle])

return (
Expand Down
3 changes: 3 additions & 0 deletions src/popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const App = observer(() => {
case RequestMethodEnum.SEND_TRANSACTION:
navigate('/transaction')
break
case RequestMethodEnum.SIGN_COIN_SPENDS:
navigate('/transaction')
break
default:
returnData({
data: true,
Expand Down
22 changes: 19 additions & 3 deletions src/popup/pages/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useTranslation } from 'react-i18next'
import { sendTx } from '~/api/api'
import { ErrorPopup } from '~/components/Popup'
import OfferInfo from '~/popup/components/offerInfo'
import PushTxInfo from '~/popup/components/spendBundleInfo'
import SpendBundleInfo from '~/popup/components/spendBundleInfo'
import TransferInfo from '~/popup/components/transferInfo'
import rootStore from '~/store'
import {
Expand All @@ -23,6 +23,11 @@ import Offer from '~/utils/Offer'
import InfoIcon from '~icons/hoogii/info.jsx'

import { IPopupPageProps } from '../types'
const withoutFee = [
RequestMethodEnum.SEND_TRANSACTION,
RequestMethodEnum.SIGN_COIN_SPENDS,
]

const Transaction = ({
controller,
request,
Expand Down Expand Up @@ -117,6 +122,12 @@ const Transaction = ({
}
window.close()
}
if (request.data?.method === RequestMethodEnum.SIGN_COIN_SPENDS) {
controller.returnData({
data: true,
})
window.close()
}
}

useLayoutEffect(() => {
Expand Down Expand Up @@ -154,9 +165,14 @@ const Transaction = ({
)}

{request.data?.method === RequestMethodEnum.SEND_TRANSACTION && (
<PushTxInfo request={request} controller={controller} />
<SpendBundleInfo request={request} controller={controller} />
)}

{request.data?.method === RequestMethodEnum.SIGN_COIN_SPENDS && (
<SpendBundleInfo request={request} controller={controller} />
)}
{request.data?.method !== RequestMethodEnum.SEND_TRANSACTION && (

{!withoutFee.some((method) => request.data?.method === method) && (
<div className="w-max">
<div className="mb-3 text-left text-caption text-primary-100">
{t('send-fee-description')}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ export const decrypt = async (
)
return new TextDecoder().decode(plainText)
}
export const add0x = (str?: string): string => {
if (!str) {
return '0x'
}

if (str.startsWith('0x')) {
return str
} else {
return `0x${str}`
}
}

0 comments on commit d9a1d67

Please sign in to comment.