Skip to content

Commit

Permalink
Merge pull request #1889 from nervosnetwork/rc/v0.33.0
Browse files Browse the repository at this point in the history
Rc/v0.33.0
  • Loading branch information
kellyshang authored Oct 14, 2020
2 parents 42e0fb2 + cf3f5b5 commit 1e4f19a
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 0.33.0 (2020-10-12)
# 0.33.0 (2020-10-14)

[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.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Entity, Column, PrimaryGeneratedColumn, Index } from 'typeorm'

@Entity()
export default class AddressDescription {
@PrimaryGeneratedColumn()
id!: number

@Column({
type: 'varchar',
})
@Index()
walletId!: string

@Column({
type: 'varchar',
})
@Index()
address!: string

@Column({
type: 'varchar',
})
description!: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ export default class HdPublicKeyInfo {
})
publicKeyInBlake160!: string

@Column({
type: 'varchar',
default: null
})
description?: string

@CreateDateColumn({
type: "varchar",
default: () => "CURRENT_TIMESTAMP"
Expand All @@ -45,7 +39,6 @@ export default class HdPublicKeyInfo {
publicKeyInfo.addressType = model.addressType
publicKeyInfo.addressIndex = model.addressIndex
publicKeyInfo.publicKeyInBlake160 = model.publicKeyInBlake160
publicKeyInfo.description = model.description

return publicKeyInfo
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {MigrationInterface, QueryRunner} from "typeorm";
import fs from "fs";
import env from "env";
import path from "path";
import NetworksService from "services/networks";
import AddressMeta from "database/address/meta";

export class AddAddressDescription1602543179168 implements MigrationInterface {
name = 'AddAddressDescription1602543179168'

public async up(queryRunner: QueryRunner): Promise<any> {
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);
await queryRunner.query(`CREATE INDEX "IDX_eb54f769e9f6d23d51b8d02d1d" ON "address_description" ("walletId") `, undefined);
await queryRunner.query(`CREATE INDEX "IDX_bc3931239490edba9ad200ce29" ON "address_description" ("address") `, undefined);

await queryRunner.dropColumn(`hd_public_key_info`, 'description');

//migration from the legacy address json store
const fileBasePath = env.fileBasePath
const legacyAddressPath = path.join(fileBasePath, 'addresses/index.json')

if (!fs.existsSync(legacyAddressPath)) {
return
}

const json = fs.readFileSync(legacyAddressPath, {encoding: 'utf8'})
const {addresses} = JSON.parse(json)

const currentChain = NetworksService.getInstance().getCurrent().chain

let addressesByNetwork = []
if (currentChain === 'ckb') {
addressesByNetwork = addresses.filter((addr:AddressMeta) => addr.version === 'mainnet' && addr.description)
}
else {
addressesByNetwork = addresses.filter((addr:AddressMeta) => addr.version !== 'mainnet' && addr.description)
}
for (const {walletId, address, description} of addressesByNetwork) {
await queryRunner.query(`INSERT INTO "address_description" ("walletId", "address", "description") values('${walletId}', '${address}', '${description}')`, undefined);
}
}

public async down(): Promise<any> {}

}
6 changes: 5 additions & 1 deletion packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import AssetAccount from './entities/asset-account'
import SudtTokenInfo from './entities/sudt-token-info'
import IndexerTxHashCache from './entities/indexer-tx-hash-cache'
import TxDescription from './entities/tx-description'
import AddressDescription from './entities/address-description'

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

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

Expand Down Expand Up @@ -66,7 +68,8 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
SyncInfo,
AssetAccount,
SudtTokenInfo,
IndexerTxHashCache
IndexerTxHashCache,
AddressDescription,
],
migrations: [
InitMigration1566959757554,
Expand All @@ -92,6 +95,7 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
HDPublicKeyInfo1598087517643,
TxDescription1599441769473,
RemoveKeyInfoAddress1601447406035,
AddAddressDescription1602543179168,
],
logger: 'simple-console',
logging,
Expand Down
41 changes: 30 additions & 11 deletions packages/neuron-wallet/src/services/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CellsService from './cells'
import SystemScriptInfo from 'models/system-script-info'
import Script from 'models/chain/script'
import HdPublicKeyInfo from 'database/chain/entities/hd-public-key-info'
import AddressDescription from 'database/chain/entities/address-description'
import { ChildProcess } from 'utils/worker'
import AddressDbChangedSubject from 'models/subjects/address-db-changed-subject'
import AddressMeta from 'database/address/meta'
Expand Down Expand Up @@ -291,12 +292,21 @@ export default class AddressService {
.where({walletId})
.getMany()

const addressDescriptions = await getConnection()
.getRepository(AddressDescription)
.createQueryBuilder()
.where({walletId})
.getMany()

return publicKeyInfos.sort((lhs, rhs) => {
return lhs.addressType - rhs.addressType || lhs.addressIndex - rhs.addressIndex
})
.map(publicKeyInfo => (
AddressMeta.fromHdPublicKeyInfoModel(publicKeyInfo.toModel()))
)
.map(publicKeyInfo => {
const keyInfoModel = AddressMeta.fromHdPublicKeyInfoModel(publicKeyInfo.toModel());
const found = addressDescriptions.find(addrDesc => addrDesc.address === keyInfoModel.address)
keyInfoModel.description = found?.description
return keyInfoModel
})
}

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

public static async updateDescription (walletId: string, address: string, description: string) {
const addressDescription = await getConnection()
.getRepository(AddressDescription)
.createQueryBuilder()
.where({walletId, address})
.getOne()

if (addressDescription) {
addressDescription.description = description
await getConnection()
.manager.save(addressDescription)

return
}

await getConnection()
.getRepository(AddressDescription)
.createQueryBuilder()
.update(HdPublicKeyInfo)
.set({
description
})
.where({
walletId,
address
})
.insert()
.values({walletId, address, description})
.execute()
}

Expand Down
89 changes: 56 additions & 33 deletions packages/neuron-wallet/tests/services/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,43 +538,66 @@ describe('integration tests for AddressService', () => {
const walletId1 = '1'
const walletId2 = '2'

beforeEach(async () => {
await AddressService.checkAndGenerateSave(
walletId1,
extendedKey,
isImporting,
receivingAddressCount,
changeAddressCount
)
await AddressService.checkAndGenerateSave(
walletId2,
extendedKey,
isImporting,
receivingAddressCount,
changeAddressCount
)
describe('when saved description', () => {
beforeEach(async () => {
await AddressService.checkAndGenerateSave(
walletId1,
extendedKey,
isImporting,
receivingAddressCount,
changeAddressCount
)
await AddressService.checkAndGenerateSave(
walletId2,
extendedKey,
isImporting,
receivingAddressCount,
changeAddressCount
)

const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
addressToUpdate = generatedAddresses1[0]
await AddressService.updateDescription(walletId1, addressToUpdate.address, description)
})
it('saves description for a public key info matching with the address', async () => {
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)
addressToUpdate = generatedAddresses1[0]
await AddressService.updateDescription(walletId1, addressToUpdate.address, description)
})
it('returns description for an address', async () => {
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)

const wallet1Addr = generatedAddresses1.find(
(addr: any) => addr.walletId === walletId1 && addr.address === addressToUpdate.address
)
expect(wallet1Addr!.description).toEqual(description)
const wallet1Addr = generatedAddresses1.filter(
(addr: any) => addr.walletId === walletId1 && addr.description === description
)

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

const wallet1Addr = generatedAddresses2.find(
(addr: any) => addr.walletId === walletId2 && addr.address === addressToUpdate.address
)
expect(wallet1Addr!.description).toEqual(null)
})
const wallet1Addr = generatedAddresses2.find(
(addr: any) => addr.walletId === walletId2 && addr.address === addressToUpdate.address
)
expect(wallet1Addr!.description).toEqual(undefined)
})
describe('when updated an existing description', () => {
const newDescription = 'new desc'
beforeEach(async () => {
await AddressService.updateDescription(walletId1, addressToUpdate.address, newDescription)
})
it('overrides description for an address', async () => {
const generatedAddresses1 = await AddressService.getAddressesByWalletId(walletId1)

const originals = generatedAddresses1.filter(
(addr: any) => addr.walletId === walletId1 && addr.description === description
)
expect(originals.length).toEqual(0)

const overrides = generatedAddresses1.filter(
(addr: any) => addr.walletId === walletId1 && addr.description === newDescription
)
expect(overrides.length).toEqual(1)
expect(overrides[0].address).toEqual(addressToUpdate.address)
})
});
});
});
})
});

0 comments on commit 1e4f19a

Please sign in to comment.