Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Jan 4, 2025
1 parent 2f12303 commit 2e55f73
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 20 deletions.
10 changes: 9 additions & 1 deletion packages/neuron-ui/src/components/Receive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ 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 @@ -92,7 +94,13 @@ export const AddressQrCodeWithCopyZone = ({
</div>
</div>

{showViewPrivateKey && <ViewPrivateKey address={showAddress} onClose={() => setShowViewPrivateKey(false)} />}
{showViewPrivateKey && (
<ViewPrivateKey
address={showAddress}
assetAccountId={assetAccountId}
onClose={() => setShowViewPrivateKey(false)}
/>
)}
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const SUDTAccountList = () => {
break
}
setReceiveData({
acccountId: account.accountId,

Check warning on line 126 in packages/neuron-ui/src/components/SUDTAccountList/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".

Check warning on line 126 in packages/neuron-ui/src/components/SUDTAccountList/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".
address: account.address,
accountName: account.accountName ?? DEFAULT_SUDT_FIELDS.accountName,
tokenName: account.tokenName ?? DEFAULT_SUDT_FIELDS.tokenName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { DEFAULT_SUDT_FIELDS } = CONSTANTS

export interface DataProps {
address: string
acccountId: string

Check warning on line 16 in packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".

Check warning on line 16 in packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".
accountName: string
tokenName: string
symbol: string
Expand All @@ -21,7 +22,7 @@ export interface DataProps {
const SUDTReceiveDialog = ({ data, onClose }: { data: DataProps; onClose?: () => void }) => {
const [t] = useTranslation()
const [isInShortFormat, setIsInShortFormat] = useState(false)
const { address, accountName, tokenName, symbol } = data
const { address, acccountId, accountName, tokenName, symbol } = data

Check warning on line 25 in packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".

Check warning on line 25 in packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".

const displayedAddr = isInShortFormat ? addressToAddress(address, { deprecated: true }) : address

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

<AddressQrCodeWithCopyZone
showAddress={displayedAddr}
assetAccountId={acccountId}

Check warning on line 56 in packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".

Check warning on line 56 in packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".
isInShortFormat={isInShortFormat}
onClick={() => setIsInShortFormat(is => !is)}
/>
Expand Down
11 changes: 10 additions & 1 deletion packages/neuron-ui/src/components/ViewPrivateKey/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import { Attention, Copy } from 'widgets/Icons/icon'
import { getPrivateKeyByAddress } from 'services/remote'
import styles from './viewPrivateKey.module.scss'

const ViewPrivateKey = ({ onClose, address }: { onClose?: () => void; address?: string }) => {
const ViewPrivateKey = ({
onClose,
address,
assetAccountId,
}: {
onClose?: () => void
address?: string
assetAccountId?: string
}) => {
const [t] = useTranslation()
const [password, setPassword] = useState('')
const [error, setError] = useState('')
Expand Down Expand Up @@ -45,6 +53,7 @@ const ViewPrivateKey = ({ onClose, address }: { onClose?: () => void; address?:
setIsLoading(true)
getPrivateKeyByAddress({
walletID,
assetAccountId,
address,
password,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Story = StoryObj<typeof SUDTReceiveDialog>
export const Default: Story = {
args: {
data: {
acccountId: '1',

Check warning on line 17 in packages/neuron-ui/src/stories/SUDTReceiveDialog.stories.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".

Check warning on line 17 in packages/neuron-ui/src/stories/SUDTReceiveDialog.stories.tsx

View workflow job for this annotation

GitHub Actions / Check spell

"acccount" should be "account".
address: 'ckt1q9gry5zg8stq8ruq5wfz3lm5wn2k7qw3ulsfmdhe98f2j1',
accountName: 'account name',
tokenName: 'token name',
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/types/Controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ declare namespace Controller {

interface GetPrivateKeyParams {
walletID: string
assetAccountId?: string
address?: string
password: string
}
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,10 @@ export default class ApiController {
return this.#walletsController.getAllAddresses(id)
})

handle('get-private-key-by-address', async (_, { walletID, password, address }) => {
handle('get-private-key-by-address', async (_, { walletID, assetAccountId, password, address }) => {
return this.#walletsController.getPrivateKeyByAddress({
walletID,
assetAccountId,
password,
address,
})
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,15 +702,18 @@ 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
12 changes: 8 additions & 4 deletions packages/neuron-wallet/src/services/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,25 @@ 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 (!addr) {
const usedBlake160s = new Set(await AssetAccountService.blake160sOfAssetAccounts())
addr = addresses.find(a => !usedBlake160s.has(a.blake160))!
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) {
Expand Down
64 changes: 52 additions & 12 deletions packages/neuron-wallet/tests/services/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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'
Expand All @@ -12,8 +13,7 @@ import { closeConnection, getConnection, initConnection } from '../setupAndTeard
import { NetworkType } from '../../src/models/network'
import SignMessage from '../../src/services/sign-message'
import WalletService from '../../src/services/wallets'

jest.setTimeout(15000)
import AssetAccountService from '../../src/services/asset-account-service'

const { AddressType, AccountExtendedPublicKey } = hd

Expand Down Expand Up @@ -720,11 +720,13 @@ describe('integration tests for AddressService', () => {
const message = 'Hello World'
let address = 'ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwe5xyvcxv95l22x8c5ra8tkc0jgxhvrqsda3p3k'

beforeEach(() => {
let walletID = ''

beforeAll(() => {
jest.setTimeout(15000)

walletService.clearAll()
})

it('return private key', async () => {
const seed = hd.mnemonic.mnemonicToSeedSync(mnemonic)
const masterKeychain = hd.Keychain.fromSeed(seed)
const extendedKey = new hd.ExtendedPrivateKey(
Expand All @@ -746,9 +748,11 @@ describe('integration tests for AddressService', () => {
extendedKey: accountExtendedPublicKey.serialize(),
keystore,
})
walletID = wallet.id
})

const addresses = await wallet.checkAndGenerateAddresses(false, 5, 5)

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)
Expand All @@ -757,22 +761,58 @@ describe('integration tests for AddressService', () => {
}

const privateKey = await AddressService.getPrivateKeyByAddress({
walletID: wallet.id,
password: password,
walletID,
password,
address,
})

// @ts-ignore: Private method
const sig = SignMessage.signByPrivateKey(privateKey, message)
const signature = await SignMessage.sign({
walletID: wallet.id,
password: password,
message: message,
walletID,
password,
message,
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)
}
})
})
})
})

1 comment on commit 2e55f73

@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 12612119531

Please sign in to comment.