Skip to content

Commit

Permalink
fix: comment
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Jan 5, 2025
1 parent 2da3fb3 commit 68d2c2c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 98 deletions.
10 changes: 1 addition & 9 deletions packages/neuron-ui/src/components/Receive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -94,13 +92,7 @@ export const AddressQrCodeWithCopyZone = ({
</div>
</div>

{showViewPrivateKey && (
<ViewPrivateKey
address={showAddress}
assetAccountId={assetAccountId}
onClose={() => setShowViewPrivateKey(false)}
/>
)}
{showViewPrivateKey && <ViewPrivateKey address={showAddress} onClose={() => setShowViewPrivateKey(false)} />}
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -53,7 +53,6 @@ const SUDTReceiveDialog = ({ data, onClose }: { data: DataProps; onClose?: () =>

<AddressQrCodeWithCopyZone
showAddress={displayedAddr}
assetAccountId={accountId}
isInShortFormat={isInShortFormat}
onClick={() => setIsInShortFormat(is => !is)}
/>
Expand Down
11 changes: 1 addition & 10 deletions packages/neuron-ui/src/components/ViewPrivateKey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down Expand Up @@ -53,7 +45,6 @@ const ViewPrivateKey = ({
setIsLoading(true)
getPrivateKeyByAddress({
walletID,
assetAccountId,
address,
password,
})
Expand Down
1 change: 0 additions & 1 deletion packages/neuron-ui/src/types/Controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ declare namespace Controller {

interface GetPrivateKeyParams {
walletID: string
assetAccountId?: string
address?: string
password: string
}
Expand Down
3 changes: 1 addition & 2 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
3 changes: 0 additions & 3 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
14 changes: 4 additions & 10 deletions packages/neuron-wallet/src/services/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -452,25 +451,20 @@ export default class AddressService {

public static async getPrivateKeyByAddress({
walletID,
assetAccountId,
password,
address,
}: {
walletID: string
assetAccountId?: string
password: string
address?: string
}): Promise<string> {
const wallet = WalletService.getInstance().get(walletID)
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) {
Expand Down
80 changes: 19 additions & 61 deletions packages/neuron-wallet/tests/services/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = ''

Expand Down Expand Up @@ -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)
})
})
})
Expand Down

1 comment on commit 68d2c2c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 12621742703

Please sign in to comment.