Skip to content

Commit

Permalink
Add method signPsbt for bitcoin provider #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiendekaco committed Jun 13, 2024
1 parent 73b2069 commit cac1d05
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/extension-base/src/koni/background/handlers/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,8 @@ export default class KoniState {
throw new BitcoinProviderError(BitcoinProviderErrorType.INVALID_PARAMS, t('Not found payload to sign'));
}

if (!isHex(psbt)) {
throw new BitcoinProviderError(BitcoinProviderErrorType.INVALID_PARAMS, t('Psbt to be signed must be base64-encoded'));
if (!isHex(`0x${psbt}`)) {
throw new BitcoinProviderError(BitcoinProviderErrorType.INVALID_PARAMS, t('Psbt to be signed must be hex-encoded'));
}

let canSign = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { ConfirmationDefinitionsBitcoin, ConfirmationsQueueBitcoin, ConfirmationsQueueItemOptions, ConfirmationTypeBitcoin, RequestConfirmationCompleteBitcoin, SignMessageBitcoinResult, SignPsbtBitcoinResult } from '@subwallet/extension-base/background/KoniTypes';
import { BitcoinProviderError } from '@subwallet/extension-base/background/errors/BitcoinProviderError';
import { BitcoinProviderErrorType, ConfirmationDefinitionsBitcoin, ConfirmationsQueueBitcoin, ConfirmationsQueueItemOptions, ConfirmationTypeBitcoin, RequestConfirmationCompleteBitcoin, SignMessageBitcoinResult, SignPsbtBitcoinResult } from '@subwallet/extension-base/background/KoniTypes';
import { ConfirmationRequestBase, Resolver } from '@subwallet/extension-base/background/types';
import RequestService from '@subwallet/extension-base/services/request-service';
import { isInternalRequest } from '@subwallet/extension-base/utils/request';
Expand Down Expand Up @@ -196,6 +197,10 @@ export default class BitcoinRequestHandler {
const { accounts, payload } = request.payload;
const { psbt, signingIndexes } = payload;

if (accounts.length === 0) {
throw new BitcoinProviderError(BitcoinProviderErrorType.INVALID_PARAMS, 'Please connect to Wallet to try this request');
}

const psbtList = accounts.map(({ address }) => {
const pair = keyring.getPair(address);

Expand All @@ -204,16 +209,13 @@ export default class BitcoinRequestHandler {
keyring.unlockPair(pair.address);
}

// Finalize all inputs in the Psbt

// Sign the Psbt using the pair's bitcoin object
const signedTransaction = pair.bitcoin.signTransaction(psbt, signingIndexes[address]);

return signedTransaction.finalizeAllInputs();
});

const psbtCombine = new Psbt().combine(...psbtList);

const psbtCombine = psbtList[0].combine(...psbtList);
const transactionObj = psbt.extractTransaction();

return {
Expand Down Expand Up @@ -258,12 +260,16 @@ export default class BitcoinRequestHandler {
}

if (isApproved) {
// Fill signature for some special type
await this.decorateResultBitcoin(type, confirmation, result);
const error = validator && validator(result);

if (error) {
resolver.reject(error);
try {
// Fill signature for some special type
await this.decorateResultBitcoin(type, confirmation, result);
const error = validator && validator(result);

if (error) {
resolver.reject(error);
}
} catch (e) {
resolver.reject(e as Error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const Component: React.FC<Props> = (props: Props) => {
const chain = useGetChainInfoByChainId(chainId);
const checkUnlock = useUnlockChecker();

const signMode = useMemo(() => getSignMode(account || accounts[0]?.address), [account, accounts]);
const signMode = useMemo(() => getSignMode(account || accounts[0]), [account, accounts]);
const isLedger = useMemo(() => signMode === AccountSignMode.LEDGER, [signMode]);
const isMessage = isBitcoinMessage(payload);

Expand Down Expand Up @@ -147,7 +147,7 @@ const Component: React.FC<Props> = (props: Props) => {
setLoading(true);

setTimeout(() => {
const signPromise = isMessage ? ledgerSignMessage(u8aToU8a(hashPayload), account.accountIndex, account.addressOffset) : ledgerSignTransaction(hexToU8a(hashPayload), account.accountIndex, account.addressOffset);
const signPromise = isMessage ? ledgerSignMessage(u8aToU8a(hashPayload), account?.accountIndex, account?.addressOffset) : ledgerSignTransaction(hexToU8a(hashPayload), account?.accountIndex, account?.addressOffset);

signPromise
.then(({ signature }) => {
Expand All @@ -158,7 +158,7 @@ const Component: React.FC<Props> = (props: Props) => {
setLoading(false);
});
});
}, [account.accountIndex, account.addressOffset, hashPayload, isLedgerConnected, isMessage, ledger, ledgerSignMessage, ledgerSignTransaction, onApproveSignature, refreshLedger]);
}, [account?.accountIndex, account?.addressOffset, hashPayload, isLedgerConnected, isMessage, ledger, ledgerSignMessage, ledgerSignTransaction, onApproveSignature, refreshLedger]);

const onConfirmInject = useCallback(() => {
console.error('Not implemented yet');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props extends ThemeProps {
function Component ({ className, request, type }: Props) {
const { id, payload } = request;
const { t } = useTranslation();
const { account } = payload;
const { accounts } = payload;

const onClickDetail = useOpenDetailModal();

Expand All @@ -36,14 +36,16 @@ function Component ({ className, request, type }: Props) {
<div className='description'>
{t('You are approving a request with the following account')}
</div>
<AccountItemWithName
accountName={account.name}
address={account.address}
avatarSize={24}
className='account-item'
isSelected={true}
proxyId={account.proxyId}
/>
{accounts.map((account) =>
<AccountItemWithName
accountName={account.name}
address={account.address}
avatarSize={24}
className='account-item'
isSelected={true}
key={account.address}
proxyId={account.proxyId}
/>)}
<div>
<Button
icon={<ViewDetailIcon />}
Expand All @@ -64,7 +66,10 @@ function Component ({ className, request, type }: Props) {
title={t('Message details')}
>
<MetaInfo.Data>

{JSON.stringify(request.payload.payload.txInput)}
</MetaInfo.Data>
<MetaInfo.Data>
{JSON.stringify(request.payload.payload.txOutput)}
</MetaInfo.Data>
</BaseDetailModal>
</>
Expand Down

0 comments on commit cac1d05

Please sign in to comment.