Skip to content

Commit 1e4f19a

Browse files
authored
Merge pull request #1889 from nervosnetwork/rc/v0.33.0
Rc/v0.33.0
2 parents 42e0fb2 + cf3f5b5 commit 1e4f19a

File tree

7 files changed

+161
-53
lines changed

7 files changed

+161
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0.33.0 (2020-10-12)
1+
# 0.33.0 (2020-10-14)
22

33
[CKB v0.35.1](https://github.com/nervosnetwork/ckb/releases/tag/v0.35.1) was released on Sept. 14th, 2020. This version of CKB node is now bundled and preconfigured in Neuron.
44

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Entity, Column, PrimaryGeneratedColumn, Index } from 'typeorm'
2+
3+
@Entity()
4+
export default class AddressDescription {
5+
@PrimaryGeneratedColumn()
6+
id!: number
7+
8+
@Column({
9+
type: 'varchar',
10+
})
11+
@Index()
12+
walletId!: string
13+
14+
@Column({
15+
type: 'varchar',
16+
})
17+
@Index()
18+
address!: string
19+
20+
@Column({
21+
type: 'varchar',
22+
})
23+
description!: string
24+
}

packages/neuron-wallet/src/database/chain/entities/hd-public-key-info.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ export default class HdPublicKeyInfo {
2626
})
2727
publicKeyInBlake160!: string
2828

29-
@Column({
30-
type: 'varchar',
31-
default: null
32-
})
33-
description?: string
34-
3529
@CreateDateColumn({
3630
type: "varchar",
3731
default: () => "CURRENT_TIMESTAMP"
@@ -45,7 +39,6 @@ export default class HdPublicKeyInfo {
4539
publicKeyInfo.addressType = model.addressType
4640
publicKeyInfo.addressIndex = model.addressIndex
4741
publicKeyInfo.publicKeyInBlake160 = model.publicKeyInBlake160
48-
publicKeyInfo.description = model.description
4942

5043
return publicKeyInfo
5144
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {MigrationInterface, QueryRunner} from "typeorm";
2+
import fs from "fs";
3+
import env from "env";
4+
import path from "path";
5+
import NetworksService from "services/networks";
6+
import AddressMeta from "database/address/meta";
7+
8+
export class AddAddressDescription1602543179168 implements MigrationInterface {
9+
name = 'AddAddressDescription1602543179168'
10+
11+
public async up(queryRunner: QueryRunner): Promise<any> {
12+
await queryRunner.query(`CREATE TABLE "address_description" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "walletId" varchar NOT NULL, "address" varchar NOT NULL, "description" varchar NOT NULL)`, undefined);
13+
await queryRunner.query(`CREATE INDEX "IDX_eb54f769e9f6d23d51b8d02d1d" ON "address_description" ("walletId") `, undefined);
14+
await queryRunner.query(`CREATE INDEX "IDX_bc3931239490edba9ad200ce29" ON "address_description" ("address") `, undefined);
15+
16+
await queryRunner.dropColumn(`hd_public_key_info`, 'description');
17+
18+
//migration from the legacy address json store
19+
const fileBasePath = env.fileBasePath
20+
const legacyAddressPath = path.join(fileBasePath, 'addresses/index.json')
21+
22+
if (!fs.existsSync(legacyAddressPath)) {
23+
return
24+
}
25+
26+
const json = fs.readFileSync(legacyAddressPath, {encoding: 'utf8'})
27+
const {addresses} = JSON.parse(json)
28+
29+
const currentChain = NetworksService.getInstance().getCurrent().chain
30+
31+
let addressesByNetwork = []
32+
if (currentChain === 'ckb') {
33+
addressesByNetwork = addresses.filter((addr:AddressMeta) => addr.version === 'mainnet' && addr.description)
34+
}
35+
else {
36+
addressesByNetwork = addresses.filter((addr:AddressMeta) => addr.version !== 'mainnet' && addr.description)
37+
}
38+
for (const {walletId, address, description} of addressesByNetwork) {
39+
await queryRunner.query(`INSERT INTO "address_description" ("walletId", "address", "description") values('${walletId}', '${address}', '${description}')`, undefined);
40+
}
41+
}
42+
43+
public async down(): Promise<any> {}
44+
45+
}

packages/neuron-wallet/src/database/chain/ormconfig.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import AssetAccount from './entities/asset-account'
1414
import SudtTokenInfo from './entities/sudt-token-info'
1515
import IndexerTxHashCache from './entities/indexer-tx-hash-cache'
1616
import TxDescription from './entities/tx-description'
17+
import AddressDescription from './entities/address-description'
1718

1819
import { InitMigration1566959757554 } from './migrations/1566959757554-InitMigration'
1920
import { AddTypeAndHasData1567144517514 } from './migrations/1567144517514-AddTypeAndHasData'
@@ -38,6 +39,7 @@ import { AddIndexerTxHashCache1592727615004 } from './migrations/1592727615004-A
3839
import { HDPublicKeyInfo1598087517643 } from './migrations/1598087517643-HDPublicKeyInfo'
3940
import { TxDescription1599441769473 } from './migrations/1599441769473-TxDescription'
4041
import { RemoveKeyInfoAddress1601447406035 } from './migrations/1601447406035-RemoveKeyInfoAddress'
42+
import { AddAddressDescription1602543179168 } from './migrations/1602543179168-AddAddressDescription'
4143

4244
export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'
4345

@@ -66,7 +68,8 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
6668
SyncInfo,
6769
AssetAccount,
6870
SudtTokenInfo,
69-
IndexerTxHashCache
71+
IndexerTxHashCache,
72+
AddressDescription,
7073
],
7174
migrations: [
7275
InitMigration1566959757554,
@@ -92,6 +95,7 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
9295
HDPublicKeyInfo1598087517643,
9396
TxDescription1599441769473,
9497
RemoveKeyInfoAddress1601447406035,
98+
AddAddressDescription1602543179168,
9599
],
96100
logger: 'simple-console',
97101
logging,

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CellsService from './cells'
1111
import SystemScriptInfo from 'models/system-script-info'
1212
import Script from 'models/chain/script'
1313
import HdPublicKeyInfo from 'database/chain/entities/hd-public-key-info'
14+
import AddressDescription from 'database/chain/entities/address-description'
1415
import { ChildProcess } from 'utils/worker'
1516
import AddressDbChangedSubject from 'models/subjects/address-db-changed-subject'
1617
import AddressMeta from 'database/address/meta'
@@ -291,12 +292,21 @@ export default class AddressService {
291292
.where({walletId})
292293
.getMany()
293294

295+
const addressDescriptions = await getConnection()
296+
.getRepository(AddressDescription)
297+
.createQueryBuilder()
298+
.where({walletId})
299+
.getMany()
300+
294301
return publicKeyInfos.sort((lhs, rhs) => {
295302
return lhs.addressType - rhs.addressType || lhs.addressIndex - rhs.addressIndex
296303
})
297-
.map(publicKeyInfo => (
298-
AddressMeta.fromHdPublicKeyInfoModel(publicKeyInfo.toModel()))
299-
)
304+
.map(publicKeyInfo => {
305+
const keyInfoModel = AddressMeta.fromHdPublicKeyInfoModel(publicKeyInfo.toModel());
306+
const found = addressDescriptions.find(addrDesc => addrDesc.address === keyInfoModel.address)
307+
keyInfoModel.description = found?.description
308+
return keyInfoModel
309+
})
300310
}
301311

302312
public static async getAddressesWithBalancesByWalletId (walletId: string): Promise<AddressInterface[]> {
@@ -330,16 +340,25 @@ export default class AddressService {
330340
}
331341

332342
public static async updateDescription (walletId: string, address: string, description: string) {
343+
const addressDescription = await getConnection()
344+
.getRepository(AddressDescription)
345+
.createQueryBuilder()
346+
.where({walletId, address})
347+
.getOne()
348+
349+
if (addressDescription) {
350+
addressDescription.description = description
351+
await getConnection()
352+
.manager.save(addressDescription)
353+
354+
return
355+
}
356+
333357
await getConnection()
358+
.getRepository(AddressDescription)
334359
.createQueryBuilder()
335-
.update(HdPublicKeyInfo)
336-
.set({
337-
description
338-
})
339-
.where({
340-
walletId,
341-
address
342-
})
360+
.insert()
361+
.values({walletId, address, description})
343362
.execute()
344363
}
345364

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

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -538,43 +538,66 @@ describe('integration tests for AddressService', () => {
538538
const walletId1 = '1'
539539
const walletId2 = '2'
540540

541-
beforeEach(async () => {
542-
await AddressService.checkAndGenerateSave(
543-
walletId1,
544-
extendedKey,
545-
isImporting,
546-
receivingAddressCount,
547-
changeAddressCount
548-
)
549-
await AddressService.checkAndGenerateSave(
550-
walletId2,
551-
extendedKey,
552-
isImporting,
553-
receivingAddressCount,
554-
changeAddressCount
555-
)
541+
describe('when saved description', () => {
542+
beforeEach(async () => {
543+
await AddressService.checkAndGenerateSave(
544+
walletId1,
545+
extendedKey,
546+
isImporting,
547+
receivingAddressCount,
548+
changeAddressCount
549+
)
550+
await AddressService.checkAndGenerateSave(
551+
walletId2,
552+
extendedKey,
553+
isImporting,
554+
receivingAddressCount,
555+
changeAddressCount
556+
)
556557

557-
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
558-
addressToUpdate = generatedAddresses1[0]
559-
await AddressService.updateDescription(walletId1, addressToUpdate.address, description)
560-
})
561-
it('saves description for a public key info matching with the address', async () => {
562-
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
558+
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
559+
addressToUpdate = generatedAddresses1[0]
560+
await AddressService.updateDescription(walletId1, addressToUpdate.address, description)
561+
})
562+
it('returns description for an address', async () => {
563+
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
563564

564-
const wallet1Addr = generatedAddresses1.find(
565-
(addr: any) => addr.walletId === walletId1 && addr.address === addressToUpdate.address
566-
)
567-
expect(wallet1Addr!.description).toEqual(description)
565+
const wallet1Addr = generatedAddresses1.filter(
566+
(addr: any) => addr.walletId === walletId1 && addr.description === description
567+
)
568568

569-
})
570-
it('should not description to the public key under other wallets even if the address is the same', async () => {
571-
const generatedAddresses2 = await AddressService.getAddressesByWalletId(walletId2)
569+
expect(wallet1Addr.length).toEqual(1)
570+
expect(wallet1Addr[0].address).toEqual(addressToUpdate.address)
571+
})
572+
it('should not return description for an address under other wallets even if the address is the same', async () => {
573+
const generatedAddresses2 = await AddressService.getAddressesByWalletId(walletId2)
572574

573-
const wallet1Addr = generatedAddresses2.find(
574-
(addr: any) => addr.walletId === walletId2 && addr.address === addressToUpdate.address
575-
)
576-
expect(wallet1Addr!.description).toEqual(null)
577-
})
575+
const wallet1Addr = generatedAddresses2.find(
576+
(addr: any) => addr.walletId === walletId2 && addr.address === addressToUpdate.address
577+
)
578+
expect(wallet1Addr!.description).toEqual(undefined)
579+
})
580+
describe('when updated an existing description', () => {
581+
const newDescription = 'new desc'
582+
beforeEach(async () => {
583+
await AddressService.updateDescription(walletId1, addressToUpdate.address, newDescription)
584+
})
585+
it('overrides description for an address', async () => {
586+
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
587+
588+
const originals = generatedAddresses1.filter(
589+
(addr: any) => addr.walletId === walletId1 && addr.description === description
590+
)
591+
expect(originals.length).toEqual(0)
592+
593+
const overrides = generatedAddresses1.filter(
594+
(addr: any) => addr.walletId === walletId1 && addr.description === newDescription
595+
)
596+
expect(overrides.length).toEqual(1)
597+
expect(overrides[0].address).toEqual(addressToUpdate.address)
598+
})
599+
});
600+
});
578601
});
579602
})
580603
});

0 commit comments

Comments
 (0)