Skip to content

Commit

Permalink
fix: review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
weatherstar committed May 26, 2024
1 parent 362ee6a commit 52d3d40
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
38 changes: 3 additions & 35 deletions packages/core/src/chains/ckb/CoreChainSoftware.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { blockchain } from '@ckb-lumos/base';
import { sealTransaction } from '@ckb-lumos/helpers';
import { bytesToHex } from '@noble/hashes/utils';

import backgroundApiProxy from '@onekeyhq/kit/src/background/instance/backgroundApiProxy';
import ClientCkb from '@onekeyhq/kit-bg/src/vaults/impls/ckb/sdkCkb/ClientCkb';
import { pubkeyToAddress } from '@onekeyhq/kit-bg/src/vaults/impls/ckb/utils/address';
import { getConfig } from '@onekeyhq/kit-bg/src/vaults/impls/ckb/utils/config';
import {
convertTxToTxSkeleton,
serializeTransactionMessage,
} from '@onekeyhq/kit-bg/src/vaults/impls/ckb/utils/transaction';
import { OneKeyInternalError } from '@onekeyhq/shared/src/errors';
import bufferUtils from '@onekeyhq/shared/src/utils/bufferUtils';
import hexUtils from '@onekeyhq/shared/src/utils/hexUtils';

import { CoreChainApiBase } from '../../base/CoreChainApiBase';

import type { IEncodedTxCkb } from './types';
import type {
ICoreApiGetAddressItem,
ICoreApiGetAddressQueryImported,
Expand All @@ -26,7 +15,6 @@ import type {
ICoreApiGetAddressesResult,
ICoreApiPrivateKeysMap,
ICoreApiSignBasePayload,
ICoreApiSignMsgPayload,
ICoreApiSignTxPayload,
ICurveName,
ISignedTxPro,
Expand All @@ -49,47 +37,27 @@ export default class CoreChainSoftware extends CoreChainApiBase {
payload: ICoreApiSignTxPayload,
): Promise<ISignedTxPro> {
const { unsignedTx } = payload;
const encodedTx = unsignedTx.encodedTx as IEncodedTxCkb;
const message = unsignedTx.rawTxUnsigned as string;
const signer = await this.baseGetSingleSigner({
payload,
curve,
});

const client = new ClientCkb({
backgroundApi: backgroundApiProxy,
networkId: payload.networkInfo.networkId,
});

const txSkeleton = await convertTxToTxSkeleton({
client,
transaction: encodedTx.tx,
});

const { txSkeleton: txSkeletonWithMessage, message } =
serializeTransactionMessage(txSkeleton);

if (!message) {
throw new OneKeyInternalError('Unable to serialize transaction message.');
}

const [signature, recoveryParam] = await signer.sign(
Buffer.from(hexUtils.stripHexPrefix(message), 'hex'),
);

const recoveryParamHex = recoveryParam.toString(16).padStart(2, '0');
const sig = hexUtils.addHexPrefix(bytesToHex(signature) + recoveryParamHex);

const tx = sealTransaction(txSkeletonWithMessage, [sig]);
const signedTx = blockchain.Transaction.pack(tx);

return {
txid: '',
rawTx: bytesToHex(signedTx),
rawTx: sig,
encodedTx: unsignedTx.encodedTx,
};
}

override async signMessage(payload: ICoreApiSignMsgPayload): Promise<string> {
override async signMessage(): Promise<string> {
throw new Error('Method not implemented.');
}

Expand Down
45 changes: 44 additions & 1 deletion packages/kit-bg/src/vaults/impls/ckb/KeyringHd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { blockchain } from '@ckb-lumos/base';
import { sealTransaction } from '@ckb-lumos/helpers';

import type { IEncodedTxCkb } from '@onekeyhq/core/src/chains/ckb/types';
import coreChainApi from '@onekeyhq/core/src/instance/coreChainApi';
import type { ISignedTxPro } from '@onekeyhq/core/src/types';
import { OneKeyInternalError } from '@onekeyhq/shared/src/errors';
import bufferUtils from '@onekeyhq/shared/src/utils/bufferUtils';

import { KeyringHdBase } from '../../base/KeyringHdBase';

import {
convertTxToTxSkeleton,
serializeTransactionMessage,
} from './utils/transaction';

import type IVaultCkb from './Vault';
import type { IDBAccount } from '../../../dbs/local/types';
import type {
IGetPrivateKeysParams,
Expand All @@ -29,7 +41,38 @@ export class KeyringHd extends KeyringHdBase {
override async signTransaction(
params: ISignTransactionParams,
): Promise<ISignedTxPro> {
return this.baseSignTransaction(params);
const { unsignedTx } = params;
const encodedTx = unsignedTx.encodedTx as IEncodedTxCkb;

const client = await (this.vault as IVaultCkb).getClient();

const txSkeleton = await convertTxToTxSkeleton({
client,
transaction: encodedTx.tx,
});

const { txSkeleton: txSkeletonWithMessage, message } =
serializeTransactionMessage(txSkeleton);

if (!message) {
throw new OneKeyInternalError('Unable to serialize transaction message.');
}

const result = await this.baseSignTransaction({
...params,
unsignedTx: {
...params.unsignedTx,
rawTxUnsigned: message,
},
});

const tx = sealTransaction(txSkeletonWithMessage, [result.rawTx]);
const signedTx = blockchain.Transaction.pack(tx);

return {
...result,
rawTx: bufferUtils.bytesToHex(signedTx),
};
}

override async signMessage(): Promise<string[]> {
Expand Down

0 comments on commit 52d3d40

Please sign in to comment.