Skip to content

Commit 2e55f73

Browse files
committed
fix: test
1 parent 2f12303 commit 2e55f73

File tree

10 files changed

+90
-20
lines changed

10 files changed

+90
-20
lines changed

packages/neuron-ui/src/components/Receive/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { useCopyAndDownloadQrCode, useSwitchAddress } from './hooks'
1515

1616
type AddressTransformWithCopyZoneProps = {
1717
showAddress: string
18+
assetAccountId?: string
1819
isInShortFormat: boolean
1920
onClick: () => void
2021
}
2122

2223
export const AddressQrCodeWithCopyZone = ({
2324
showAddress,
25+
assetAccountId,
2426
isInShortFormat,
2527
onClick,
2628
}: AddressTransformWithCopyZoneProps) => {
@@ -92,7 +94,13 @@ export const AddressQrCodeWithCopyZone = ({
9294
</div>
9395
</div>
9496

95-
{showViewPrivateKey && <ViewPrivateKey address={showAddress} onClose={() => setShowViewPrivateKey(false)} />}
97+
{showViewPrivateKey && (
98+
<ViewPrivateKey
99+
address={showAddress}
100+
assetAccountId={assetAccountId}
101+
onClose={() => setShowViewPrivateKey(false)}
102+
/>
103+
)}
96104
</div>
97105
)
98106
}

packages/neuron-ui/src/components/SUDTAccountList/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ const SUDTAccountList = () => {
123123
break
124124
}
125125
setReceiveData({
126+
acccountId: account.accountId,
126127
address: account.address,
127128
accountName: account.accountName ?? DEFAULT_SUDT_FIELDS.accountName,
128129
tokenName: account.tokenName ?? DEFAULT_SUDT_FIELDS.tokenName,

packages/neuron-ui/src/components/SUDTReceiveDialog/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const { DEFAULT_SUDT_FIELDS } = CONSTANTS
1313

1414
export interface DataProps {
1515
address: string
16+
acccountId: string
1617
accountName: string
1718
tokenName: string
1819
symbol: string
@@ -21,7 +22,7 @@ export interface DataProps {
2122
const SUDTReceiveDialog = ({ data, onClose }: { data: DataProps; onClose?: () => void }) => {
2223
const [t] = useTranslation()
2324
const [isInShortFormat, setIsInShortFormat] = useState(false)
24-
const { address, accountName, tokenName, symbol } = data
25+
const { address, acccountId, accountName, tokenName, symbol } = data
2526

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

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

5354
<AddressQrCodeWithCopyZone
5455
showAddress={displayedAddr}
56+
assetAccountId={acccountId}
5557
isInShortFormat={isInShortFormat}
5658
onClick={() => setIsInShortFormat(is => !is)}
5759
/>

packages/neuron-ui/src/components/ViewPrivateKey/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ import { Attention, Copy } from 'widgets/Icons/icon'
99
import { getPrivateKeyByAddress } from 'services/remote'
1010
import styles from './viewPrivateKey.module.scss'
1111

12-
const ViewPrivateKey = ({ onClose, address }: { onClose?: () => void; address?: string }) => {
12+
const ViewPrivateKey = ({
13+
onClose,
14+
address,
15+
assetAccountId,
16+
}: {
17+
onClose?: () => void
18+
address?: string
19+
assetAccountId?: string
20+
}) => {
1321
const [t] = useTranslation()
1422
const [password, setPassword] = useState('')
1523
const [error, setError] = useState('')
@@ -45,6 +53,7 @@ const ViewPrivateKey = ({ onClose, address }: { onClose?: () => void; address?:
4553
setIsLoading(true)
4654
getPrivateKeyByAddress({
4755
walletID,
56+
assetAccountId,
4857
address,
4958
password,
5059
})

packages/neuron-ui/src/stories/SUDTReceiveDialog.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Story = StoryObj<typeof SUDTReceiveDialog>
1414
export const Default: Story = {
1515
args: {
1616
data: {
17+
acccountId: '1',
1718
address: 'ckt1q9gry5zg8stq8ruq5wfz3lm5wn2k7qw3ulsfmdhe98f2j1',
1819
accountName: 'account name',
1920
tokenName: 'token name',

packages/neuron-ui/src/types/Controller/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ declare namespace Controller {
186186

187187
interface GetPrivateKeyParams {
188188
walletID: string
189+
assetAccountId?: string
189190
address?: string
190191
password: string
191192
}

packages/neuron-wallet/src/controllers/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,10 @@ export default class ApiController {
440440
return this.#walletsController.getAllAddresses(id)
441441
})
442442

443-
handle('get-private-key-by-address', async (_, { walletID, password, address }) => {
443+
handle('get-private-key-by-address', async (_, { walletID, assetAccountId, password, address }) => {
444444
return this.#walletsController.getPrivateKeyByAddress({
445445
walletID,
446+
assetAccountId,
446447
password,
447448
address,
448449
})

packages/neuron-wallet/src/controllers/wallets.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,15 +702,18 @@ export default class WalletsController {
702702

703703
public async getPrivateKeyByAddress({
704704
walletID,
705+
assetAccountId,
705706
password,
706707
address,
707708
}: {
708709
walletID: string
710+
assetAccountId?: string
709711
password: string
710712
address?: string
711713
}) {
712714
const privateKey = await AddressService.getPrivateKeyByAddress({
713715
walletID,
716+
assetAccountId,
714717
password,
715718
address,
716719
})

packages/neuron-wallet/src/services/addresses.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,25 @@ export default class AddressService {
452452

453453
public static async getPrivateKeyByAddress({
454454
walletID,
455+
assetAccountId,
455456
password,
456457
address,
457458
}: {
458459
walletID: string
460+
assetAccountId?: string
459461
password: string
460462
address?: string
461463
}): Promise<string> {
462464
const wallet = WalletService.getInstance().get(walletID)
463-
464465
const addresses = await AddressService.getAddressesByWalletId(walletID)
465466
let addr = address ? addresses.find(addr => addr.address === address) : addresses[0]
466467

467-
if (!addr) {
468-
const usedBlake160s = new Set(await AssetAccountService.blake160sOfAssetAccounts())
469-
addr = addresses.find(a => !usedBlake160s.has(a.blake160))!
468+
if (assetAccountId) {
469+
const assetAccount = await AssetAccountService.getAccount({ walletID, id: Number(assetAccountId) })
470+
if (!assetAccount) {
471+
throw new AddressNotFound()
472+
}
473+
addr = addresses.find(a => a.blake160 === assetAccount.blake160)
470474
}
471475

472476
if (!addr) {

packages/neuron-wallet/tests/services/address.test.ts

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import OutputEntity from '../../src/database/chain/entities/output'
44
import { bytes } from '@ckb-lumos/lumos/codec'
55
import { hd } from '@ckb-lumos/lumos'
66
import { Address } from '../../src/models/address'
7+
import AssetAccount from '../../src/models/asset-account'
78
import Transaction from '../../src/database/chain/entities/transaction'
89
import { TransactionStatus } from '../../src/models/chain/transaction'
910
import { when } from 'jest-when'
@@ -12,8 +13,7 @@ import { closeConnection, getConnection, initConnection } from '../setupAndTeard
1213
import { NetworkType } from '../../src/models/network'
1314
import SignMessage from '../../src/services/sign-message'
1415
import WalletService from '../../src/services/wallets'
15-
16-
jest.setTimeout(15000)
16+
import AssetAccountService from '../../src/services/asset-account-service'
1717

1818
const { AddressType, AccountExtendedPublicKey } = hd
1919

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

723-
beforeEach(() => {
723+
let walletID = ''
724+
725+
beforeAll(() => {
726+
jest.setTimeout(15000)
727+
724728
walletService.clearAll()
725-
})
726729

727-
it('return private key', async () => {
728730
const seed = hd.mnemonic.mnemonicToSeedSync(mnemonic)
729731
const masterKeychain = hd.Keychain.fromSeed(seed)
730732
const extendedKey = new hd.ExtendedPrivateKey(
@@ -746,9 +748,11 @@ describe('integration tests for AddressService', () => {
746748
extendedKey: accountExtendedPublicKey.serialize(),
747749
keystore,
748750
})
751+
walletID = wallet.id
752+
})
749753

750-
const addresses = await wallet.checkAndGenerateAddresses(false, 5, 5)
751-
754+
it('hdWallet address', async () => {
755+
const addresses = await walletService.get(walletID).checkAndGenerateAddresses(false, 5, 5)
752756
if (addresses) {
753757
const crypto = require('crypto')
754758
const randomIndex = crypto.randomInt(addresses.length)
@@ -757,22 +761,58 @@ describe('integration tests for AddressService', () => {
757761
}
758762

759763
const privateKey = await AddressService.getPrivateKeyByAddress({
760-
walletID: wallet.id,
761-
password: password,
764+
walletID,
765+
password,
762766
address,
763767
})
764768

765769
// @ts-ignore: Private method
766770
const sig = SignMessage.signByPrivateKey(privateKey, message)
767771
const signature = await SignMessage.sign({
768-
walletID: wallet.id,
769-
password: password,
770-
message: message,
772+
walletID,
773+
password,
774+
message,
771775
address,
772776
})
773777

774778
expect(sig).toEqual(signature)
775779
})
780+
781+
it('asset account address', async () => {
782+
const addresses = await walletService.get(walletID).getNextReceivingAddresses()
783+
const usedBlake160s = new Set(await AssetAccountService.blake160sOfAssetAccounts())
784+
const addrObj = addresses.find(a => !usedBlake160s.has(a.blake160))
785+
786+
if (addrObj) {
787+
const assetAccount = AssetAccount.fromObject({
788+
tokenID: 'CKBytes',
789+
symbol: 'ckb',
790+
tokenName: 'ckb',
791+
decimal: '0',
792+
balance: '0',
793+
accountName: 'ckb',
794+
blake160: addrObj.blake160,
795+
})
796+
797+
const privateKey = await AddressService.getPrivateKeyByAddress({
798+
walletID,
799+
accountId: assetAccount.id,
800+
password,
801+
address,
802+
})
803+
804+
// @ts-ignore: Private method
805+
const sig = SignMessage.signByPrivateKey(privateKey, message)
806+
const signature = await SignMessage.sign({
807+
walletID,
808+
password,
809+
message,
810+
address,
811+
})
812+
813+
expect(sig).toEqual(signature)
814+
}
815+
})
776816
})
777817
})
778818
})

0 commit comments

Comments
 (0)