From 68d2c2c99156472e89cf7a983e857b1e3fb301a5 Mon Sep 17 00:00:00 2001 From: devchenyan Date: Mon, 6 Jan 2025 01:58:08 +0800 Subject: [PATCH] fix: comment --- .../src/components/Receive/index.tsx | 10 +-- .../components/SUDTReceiveDialog/index.tsx | 3 +- .../src/components/ViewPrivateKey/index.tsx | 11 +-- .../neuron-ui/src/types/Controller/index.d.ts | 1 - packages/neuron-wallet/src/controllers/api.ts | 3 +- .../neuron-wallet/src/controllers/wallets.ts | 3 - .../neuron-wallet/src/services/addresses.ts | 14 +--- .../tests/services/address.test.ts | 80 +++++-------------- 8 files changed, 27 insertions(+), 98 deletions(-) diff --git a/packages/neuron-ui/src/components/Receive/index.tsx b/packages/neuron-ui/src/components/Receive/index.tsx index be1578ec4..a182a1ea2 100644 --- a/packages/neuron-ui/src/components/Receive/index.tsx +++ b/packages/neuron-ui/src/components/Receive/index.tsx @@ -15,14 +15,12 @@ import { useCopyAndDownloadQrCode, useSwitchAddress } from './hooks' type AddressTransformWithCopyZoneProps = { showAddress: string - assetAccountId?: string isInShortFormat: boolean onClick: () => void } export const AddressQrCodeWithCopyZone = ({ showAddress, - assetAccountId, isInShortFormat, onClick, }: AddressTransformWithCopyZoneProps) => { @@ -94,13 +92,7 @@ export const AddressQrCodeWithCopyZone = ({ - {showViewPrivateKey && ( - setShowViewPrivateKey(false)} - /> - )} + {showViewPrivateKey && setShowViewPrivateKey(false)} />} ) } diff --git a/packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx b/packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx index 0a2ff7c42..36eaf810b 100644 --- a/packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx +++ b/packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx @@ -22,7 +22,7 @@ export interface DataProps { const SUDTReceiveDialog = ({ data, onClose }: { data: DataProps; onClose?: () => void }) => { const [t] = useTranslation() const [isInShortFormat, setIsInShortFormat] = useState(false) - const { address, accountId, accountName, tokenName, symbol } = data + const { address, accountName, tokenName, symbol } = data const displayedAddr = isInShortFormat ? addressToAddress(address, { deprecated: true }) : address @@ -53,7 +53,6 @@ const SUDTReceiveDialog = ({ data, onClose }: { data: DataProps; onClose?: () => setIsInShortFormat(is => !is)} /> diff --git a/packages/neuron-ui/src/components/ViewPrivateKey/index.tsx b/packages/neuron-ui/src/components/ViewPrivateKey/index.tsx index 963621fe4..d1d63d5e1 100644 --- a/packages/neuron-ui/src/components/ViewPrivateKey/index.tsx +++ b/packages/neuron-ui/src/components/ViewPrivateKey/index.tsx @@ -9,15 +9,7 @@ import { Attention, Copy } from 'widgets/Icons/icon' import { getPrivateKeyByAddress } from 'services/remote' import styles from './viewPrivateKey.module.scss' -const ViewPrivateKey = ({ - onClose, - address, - assetAccountId, -}: { - onClose?: () => void - address?: string - assetAccountId?: string -}) => { +const ViewPrivateKey = ({ onClose, address }: { onClose?: () => void; address?: string }) => { const [t] = useTranslation() const [password, setPassword] = useState('') const [error, setError] = useState('') @@ -53,7 +45,6 @@ const ViewPrivateKey = ({ setIsLoading(true) getPrivateKeyByAddress({ walletID, - assetAccountId, address, password, }) diff --git a/packages/neuron-ui/src/types/Controller/index.d.ts b/packages/neuron-ui/src/types/Controller/index.d.ts index 0898da1ab..3df4fe678 100644 --- a/packages/neuron-ui/src/types/Controller/index.d.ts +++ b/packages/neuron-ui/src/types/Controller/index.d.ts @@ -186,7 +186,6 @@ declare namespace Controller { interface GetPrivateKeyParams { walletID: string - assetAccountId?: string address?: string password: string } diff --git a/packages/neuron-wallet/src/controllers/api.ts b/packages/neuron-wallet/src/controllers/api.ts index d2f2b88c6..f8cf5e4e5 100644 --- a/packages/neuron-wallet/src/controllers/api.ts +++ b/packages/neuron-wallet/src/controllers/api.ts @@ -440,10 +440,9 @@ export default class ApiController { return this.#walletsController.getAllAddresses(id) }) - handle('get-private-key-by-address', async (_, { walletID, assetAccountId, password, address }) => { + handle('get-private-key-by-address', async (_, { walletID, password, address }) => { return this.#walletsController.getPrivateKeyByAddress({ walletID, - assetAccountId, password, address, }) diff --git a/packages/neuron-wallet/src/controllers/wallets.ts b/packages/neuron-wallet/src/controllers/wallets.ts index b36370013..2307c6be4 100644 --- a/packages/neuron-wallet/src/controllers/wallets.ts +++ b/packages/neuron-wallet/src/controllers/wallets.ts @@ -702,18 +702,15 @@ export default class WalletsController { public async getPrivateKeyByAddress({ walletID, - assetAccountId, password, address, }: { walletID: string - assetAccountId?: string password: string address?: string }) { const privateKey = await AddressService.getPrivateKeyByAddress({ walletID, - assetAccountId, password, address, }) diff --git a/packages/neuron-wallet/src/services/addresses.ts b/packages/neuron-wallet/src/services/addresses.ts index 4004a5c8b..85b8cb4ff 100644 --- a/packages/neuron-wallet/src/services/addresses.ts +++ b/packages/neuron-wallet/src/services/addresses.ts @@ -2,8 +2,7 @@ import { hd } from '@ckb-lumos/lumos' import { bytes } from '@ckb-lumos/lumos/codec' import WalletService from './wallets' import { AddressNotFound } from '../exceptions' -import { publicKeyToAddress, DefaultAddressNumber } from '../utils/scriptAndAddress' -import AssetAccountService from './asset-account-service' +import { publicKeyToAddress, DefaultAddressNumber, addressToScript } from '../utils/scriptAndAddress' import { Address as AddressInterface } from '../models/address' import AddressCreatedSubject from '../models/subjects/address-created-subject' import NetworksService from '../services/networks' @@ -452,12 +451,10 @@ export default class AddressService { public static async getPrivateKeyByAddress({ walletID, - assetAccountId, password, address, }: { walletID: string - assetAccountId?: string password: string address?: string }): Promise { @@ -465,12 +462,9 @@ export default class AddressService { const addresses = await AddressService.getAddressesByWalletId(walletID) let addr = address ? addresses.find(addr => addr.address === address) : addresses[0] - if (assetAccountId) { - const assetAccount = await AssetAccountService.getAccount({ walletID, id: Number(assetAccountId) }) - if (!assetAccount) { - throw new AddressNotFound() - } - addr = addresses.find(a => a.blake160 === assetAccount.blake160) + if (!addr && address) { + const lock = addressToScript(address) + addr = addresses.find(a => a.blake160 === lock.args) } if (!addr) { diff --git a/packages/neuron-wallet/tests/services/address.test.ts b/packages/neuron-wallet/tests/services/address.test.ts index 46eeef17c..00ebda11f 100644 --- a/packages/neuron-wallet/tests/services/address.test.ts +++ b/packages/neuron-wallet/tests/services/address.test.ts @@ -4,16 +4,13 @@ import OutputEntity from '../../src/database/chain/entities/output' import { bytes } from '@ckb-lumos/lumos/codec' import { hd } from '@ckb-lumos/lumos' import { Address } from '../../src/models/address' -import AssetAccount from '../../src/models/asset-account' import Transaction from '../../src/database/chain/entities/transaction' import { TransactionStatus } from '../../src/models/chain/transaction' import { when } from 'jest-when' import HdPublicKeyInfo from '../../src/database/chain/entities/hd-public-key-info' import { closeConnection, getConnection, initConnection } from '../setupAndTeardown' import { NetworkType } from '../../src/models/network' -import SignMessage from '../../src/services/sign-message' import WalletService from '../../src/services/wallets' -import AssetAccountService from '../../src/services/asset-account-service' const { AddressType, AccountExtendedPublicKey } = hd @@ -717,8 +714,20 @@ describe('integration tests for AddressService', () => { const walletService = WalletService.getInstance() const mnemonic = 'tank planet champion pottery together intact quick police asset flower sudden question' const password = '1234abc~' - const message = 'Hello World' - let address = 'ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwe5xyvcxv95l22x8c5ra8tkc0jgxhvrqsda3p3k' + + const addressObj = { + walletId: '5af2473e-78f5-4799-a193-d2b1c2989838', + address: 'ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqvfewjgc69nj783sh03nuckxjacwr55vwgngf9dr', + path: "m/44'/309'/0'/0/0", + addressType: 0, + addressIndex: 0, + blake160: '0x89cba48c68b3978f185df19f31634bb870e94639', + } + + const privateKey = '0x848422863825f69e66dc7f48a3302459ec845395370c23578817456ad6b04b14' + + const AddressService = require('../../src/services/addresses').default + const getAddressesByWalletIdMock = jest.spyOn(AddressService, 'getAddressesByWalletId') let walletID = '' @@ -751,67 +760,16 @@ describe('integration tests for AddressService', () => { walletID = wallet.id }) - it('hdWallet address', async () => { - const addresses = await walletService.get(walletID).checkAndGenerateAddresses(false, 5, 5) - if (addresses) { - const crypto = require('crypto') - const randomIndex = crypto.randomInt(addresses.length) - const obj = addresses[randomIndex] - address = obj.address - } - - const privateKey = await AddressService.getPrivateKeyByAddress({ - walletID, - password, - address, - }) + it('getPrivateKeyByAddress', async () => { + getAddressesByWalletIdMock.mockReturnValueOnce([addressObj]) - // @ts-ignore: Private method - const sig = SignMessage.signByPrivateKey(privateKey, message) - const signature = await SignMessage.sign({ + const pk = await AddressService.getPrivateKeyByAddress({ walletID, password, - message, - address, + address: addressObj.address, }) - expect(sig).toEqual(signature) - }) - - it('asset account address', async () => { - const addresses = await walletService.get(walletID).getNextReceivingAddresses() - const usedBlake160s = new Set(await AssetAccountService.blake160sOfAssetAccounts()) - const addrObj = addresses.find(a => !usedBlake160s.has(a.blake160)) - - if (addrObj) { - const assetAccount = AssetAccount.fromObject({ - tokenID: 'CKBytes', - symbol: 'ckb', - tokenName: 'ckb', - decimal: '0', - balance: '0', - accountName: 'ckb', - blake160: addrObj.blake160, - }) - - const privateKey = await AddressService.getPrivateKeyByAddress({ - walletID, - accountId: assetAccount.id, - password, - address, - }) - - // @ts-ignore: Private method - const sig = SignMessage.signByPrivateKey(privateKey, message) - const signature = await SignMessage.sign({ - walletID, - password, - message, - address, - }) - - expect(sig).toEqual(signature) - } + expect(pk).toEqual(privateKey) }) }) })