Skip to content

Commit

Permalink
Merge branch 'develop' into fix-332
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan authored May 17, 2024
2 parents 8c0dae6 + b8bad46 commit 79e8481
Show file tree
Hide file tree
Showing 66 changed files with 274 additions and 3,571 deletions.
2 changes: 1 addition & 1 deletion packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"sqlite3": "5.1.6",
"subleveldown": "4.1.4",
"tslib": "2.6.2",
"typeorm": "0.2.45",
"typeorm": "0.3.17",
"undici": "5.28.4",
"uuid": "8.3.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default class IndexerCacheService {
cacheBlockNumberEntity =
(await getConnection()
.getRepository(SyncInfoEntity)
.findOne({ name: SyncInfoEntity.getLastCachedKey(blake160) })) ??
.findOneBy({ name: SyncInfoEntity.getLastCachedKey(blake160) })) ??
SyncInfoEntity.fromObject({
name: SyncInfoEntity.getLastCachedKey(blake160),
value: '0x0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class TxAddressFinder {
let shouldSync = false
for (const input of inputs) {
const outPoint: OutPoint = input.previousOutput!
const output = await getConnection().getRepository(OutputEntity).findOne({
const output = await getConnection().getRepository(OutputEntity).findOneBy({
outPointTxHash: outPoint.txHash,
outPointIndex: outPoint.index,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/controllers/hardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DeviceInfo, ExtendedPublicKey, PublicKey } from '../services/hardware/c
import { ResponseCode } from '../utils/const'
import HardwareWalletService from '../services/hardware'
import { connectDeviceFailed } from '../exceptions'
import { AccountExtendedPublicKey } from '../models/keys/key'
import { AccountExtendedPublicKey } from '@ckb-lumos/hd'

export default class HardwareController {
public async connectDevice(deviceInfo: DeviceInfo): Promise<Controller.Response<void>> {
Expand Down
22 changes: 11 additions & 11 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs'
import { t } from 'i18next'
import { prefixWith0x } from '../utils/scriptAndAddress'
import { dialog, SaveDialogReturnValue, BrowserWindow, OpenDialogReturnValue } from 'electron'
import WalletsService, { Wallet, WalletProperties, FileKeystoreWallet } from '../services/wallets'
import NetworksService from '../services/networks'
import Keystore from '../models/keys/keystore'
import Keychain from '../models/keys/keychain'
import { validateMnemonic, mnemonicToSeedSync } from '../models/keys/mnemonic'
import { AccountExtendedPublicKey, ExtendedPrivateKey, generateMnemonic } from '../models/keys/key'
import { bytes } from '@ckb-lumos/codec'
import { Keychain, Keystore, ExtendedPrivateKey, AccountExtendedPublicKey } from '@ckb-lumos/hd'
import { generateMnemonic, validateMnemonic, mnemonicToSeedSync } from '@ckb-lumos/hd/lib/mnemonic'
import CommandSubject from '../models/subjects/command'
import { ResponseCode } from '../utils/const'
import {
Expand Down Expand Up @@ -106,15 +106,15 @@ export default class WalletsController {
throw new InvalidMnemonic()
}
const extendedKey = new ExtendedPrivateKey(
masterKeychain.privateKey.toString('hex'),
masterKeychain.chainCode.toString('hex')
bytes.hexify(masterKeychain.privateKey),
bytes.hexify(masterKeychain.chainCode)
)
const keystore = Keystore.create(extendedKey, password)

const accountKeychain = masterKeychain.derivePath(AccountExtendedPublicKey.ckbAccountPath)
const accountExtendedPublicKey = new AccountExtendedPublicKey(
accountKeychain.publicKey.toString('hex'),
accountKeychain.chainCode.toString('hex')
bytes.hexify(accountKeychain.publicKey),
bytes.hexify(accountKeychain.chainCode)
)

const walletsService = WalletsService.getInstance()
Expand Down Expand Up @@ -173,8 +173,8 @@ export default class WalletsController {
)
const accountKeychain = masterKeychain.derivePath(AccountExtendedPublicKey.ckbAccountPath)
const accountExtendedPublicKey = new AccountExtendedPublicKey(
accountKeychain.publicKey.toString('hex'),
accountKeychain.chainCode.toString('hex')
bytes.hexify(accountKeychain.publicKey),
bytes.hexify(accountKeychain.chainCode)
)

const walletsService = WalletsService.getInstance()
Expand Down Expand Up @@ -273,7 +273,7 @@ export default class WalletsController {
walletName,
}: ExtendedPublicKey & { walletName: string }): Promise<Controller.Response<Wallet>> {
const device = HardwareWalletService.getInstance().getCurrent()!
const accountExtendedPublicKey = new AccountExtendedPublicKey(publicKey, chainCode)
const accountExtendedPublicKey = new AccountExtendedPublicKey(prefixWith0x(publicKey), prefixWith0x(chainCode))
const walletsService = WalletsService.getInstance()
const wallet = walletsService.create({
device: device.deviceInfo,
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/database/address/meta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytes } from '@ckb-lumos/codec'
import { Address, AddressVersion } from '../../models/address'
import { AddressType } from '../../models/keys/address'
import { AddressType } from '@ckb-lumos/hd'
import Script from '../../models/chain/script'
import SystemScriptInfo from '../../models/system-script-info'
import AssetAccountInfo from '../../models/asset-account-info'
Expand Down
6 changes: 2 additions & 4 deletions packages/neuron-wallet/src/database/chain/connection.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { getConnection as originGetConnection } from 'typeorm'
import { ConnectionName, dataSource } from './ormconfig'
import { NetworkType } from '../../models/network'
import NetworksService from '../../services/networks'

export type ConnectionName = 'light' | 'full'

export function getCurrentConnectionName(): ConnectionName {
return NetworksService.getInstance().getCurrent()?.type === NetworkType.Light ? 'light' : 'full'
}

export function getConnection(connectionName: ConnectionName = getCurrentConnectionName()) {
const connection = originGetConnection(connectionName)
const connection = dataSource[connectionName]
if (!connection) {
throw new Error(`The connection ${connectionName} should be initialized before use`)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, Column, PrimaryGeneratedColumn, Index, CreateDateColumn } from 'typeorm'
import HdPublicKeyInfoModel from '../../../models/keys/hd-public-key-info'
import { AddressType } from '../../../models/keys/address'
import { AddressType } from '@ckb-lumos/hd'

@Entity()
export default class HdPublicKeyInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default class MultisigConfig {
blake160s!: string[]

@Column()
alias!: string
alias: string = ''

@Column()
lastestBlockNumber!: string
lastestBlockNumber: string = ''

public static fromModel(model: MultisigConfigModel): MultisigConfig {
const multisigConfig = new MultisigConfig()
Expand Down
79 changes: 59 additions & 20 deletions packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createConnection, getConnectionOptions, getConnection } from 'typeorm'
import { AbstractLogger, DataSource, LogLevel, LogMessage } from 'typeorm'
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions'
import path from 'path'
import fs from 'fs'
Expand Down Expand Up @@ -63,32 +63,65 @@ import { CreateCellLocalInfo1701234043432 } from './migrations/1701234043432-Cre
import { RenameSyncProgress1702781527414 } from './migrations/1702781527414-RenameSyncProgress'
import { RemoveAddressInIndexerCache1704357651876 } from './migrations/1704357651876-RemoveAddressInIndexerCache'
import { AmendTransaction1709008125088 } from './migrations/1709008125088-AmendTransaction'
import { ConnectionName } from './connection'
import AddressSubscribe from './subscriber/address-subscriber'
import MultisigConfigSubscribe from './subscriber/multisig-config-subscriber'
import TxDescriptionSubscribe from './subscriber/tx-description-subscriber'
import SudtTokenInfoSubscribe from './subscriber/sudt-token-info-subscriber'
import AssetAccountSubscribe from './subscriber/asset-account-subscriber'

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'
export type ConnectionName = 'light' | 'full'

const dbPath = (name: string, connectionName: string): string => {
const filename = `${connectionName}-${name}.sqlite`
return path.join(env.fileBasePath, 'cells', filename)
}

const connectOptions = async (
genesisBlockHash: string,
connectionName: ConnectionName
): Promise<SqliteConnectionOptions> => {
const connectionOptions = await getConnectionOptions()
class TypeormLogger extends AbstractLogger {
/**
* Write log to specific output.
*/
protected writeLog(level: LogLevel, logMessage: LogMessage | LogMessage[]) {
const messages = this.prepareLogMessages(logMessage, {
highlightSql: false,
})

for (let message of messages) {
switch (message.type ?? level) {
case 'log':
case 'schema-build':
case 'migration':
case 'info':
case 'query':
case 'warn':
case 'query-slow':
if (message.prefix) {
console.info(message.prefix, message.message)
} else {
console.info(message.message)
}
break

case 'error':
case 'query-error':
if (message.prefix) {
console.error(message.prefix, message.message)
} else {
console.error(message.message)
}
break
}
}
}
}

const getConnectionOptions = (genesisBlockHash: string, connectionName: ConnectionName): SqliteConnectionOptions => {
const database = env.isTestMode ? ':memory:' : dbPath(genesisBlockHash, connectionName)

const logging: boolean | ('query' | 'schema' | 'error' | 'warn' | 'info' | 'log' | 'migration')[] = ['warn', 'error']
// (env.isDevMode) ? ['warn', 'error', 'log', 'info', 'schema', 'migration'] : ['warn', 'error']

return {
...connectionOptions,
name: connectionName,
type: 'sqlite',
database,
Expand Down Expand Up @@ -159,35 +192,38 @@ const connectOptions = async (
SudtTokenInfoSubscribe,
TxDescriptionSubscribe,
],
logger: 'simple-console',
logger: new TypeormLogger(),
logging,
migrationsRun: true,
maxQueryExecutionTime: 30,
}
}

export const dataSource: Record<ConnectionName, DataSource | null> = {
light: null,
full: null,
}

const initConnectionWithType = async (genesisBlockHash: string, connectionName: ConnectionName) => {
// try to close connection, if not exist, will throw ConnectionNotFoundError when call getConnection()
try {
await getConnection(connectionName).close()
await dataSource[connectionName]?.destroy()
} catch (err) {
dataSource[connectionName] = null
// do nothing
}
const connectionOptions = await connectOptions(genesisBlockHash, connectionName)
const connectionOptions = getConnectionOptions(genesisBlockHash, connectionName)
dataSource[connectionName] = new DataSource(connectionOptions)

try {
await createConnection(connectionOptions)
await getConnection(connectionName).manager.query(`PRAGMA busy_timeout = 3000;`)
await getConnection(connectionName).manager.query(`PRAGMA temp_store = MEMORY;`)
await dataSource[connectionName]?.initialize()
await dataSource[connectionName]?.manager.query(`PRAGMA busy_timeout = 3000;`)
await dataSource[connectionName]?.manager.query(`PRAGMA temp_store = MEMORY;`)
} catch (err) {
logger.error(err.message)
}
}

export async function initConnection(genesisBlockHash: string) {
await initConnectionWithType(genesisBlockHash, 'full')
await initConnectionWithType(genesisBlockHash, 'light')
}

export function migrateDBFile(genesisBlockHash: string) {
const originDBFile = dbPath(genesisBlockHash, 'cell')
const currentFullDBFile = dbPath(genesisBlockHash, 'full')
Expand All @@ -203,4 +239,7 @@ export function migrateDBFile(genesisBlockHash: string) {
}
}

export default initConnection
export default async function initConnection(genesisBlockHash: string) {
await initConnectionWithType(genesisBlockHash, 'full')
await initConnectionWithType(genesisBlockHash, 'light')
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export default class AssetAccountSubscribe extends UserSettingSubscriber<AssetAc
async afterInsert(event: InsertEvent<AssetAccount>): Promise<AssetAccount | void> {
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository(AssetAccount)
if (repo && event.entity) {
const exist = await repo.findOne({ tokenID: event.entity.tokenID, blake160: event.entity.blake160 })
const exist = await repo.findOneBy({ tokenID: event.entity.tokenID, blake160: event.entity.blake160 })
if (exist) {
await repo.upsert(AssetAccount.fromModel(event.entity.toModel()), this.unionKeys)
await repo.update(exist.id, AssetAccount.fromModel(event.entity.toModel()))
} else {
await repo.save(event.entity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default class SudtTokenInfoSubscribe extends UserSettingSubscriber<SudtTo
const repo = this.getNeedSyncConnection(event.connection.name)?.getRepository(SudtTokenInfo)
if (repo && event.entity) {
let mergeEntity: SudtTokenInfo | undefined = undefined
const existEntity = await event.connection.getRepository(SudtTokenInfo).findOne(event.entity.tokenID)
const existEntity = await event.connection
.getRepository(SudtTokenInfo)
.findOneBy({ tokenID: event.entity.tokenID })
if (existEntity) {
mergeEntity = new SudtTokenInfo()
mergeEntity.tokenID = event.entity.tokenID || existEntity.tokenID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EntitySubscriberInterface, InsertEvent, RemoveEvent, UpdateEvent } from 'typeorm'
import { ConnectionName, getConnection, getCurrentConnectionName } from '../connection'
import { getConnection, getCurrentConnectionName } from '../connection'
import { ConnectionName } from '../ormconfig'

type Constructor<T> = new (...args: unknown[]) => T

Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/models/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressType } from '../models/keys/address'
import { AddressType } from '@ckb-lumos/hd'

export enum AddressVersion {
Testnet = 'testnet',
Expand Down
51 changes: 0 additions & 51 deletions packages/neuron-wallet/src/models/keys/address.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/models/keys/hd-public-key-info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AddressType, AccountExtendedPublicKey } from '@ckb-lumos/hd'
import { scriptToAddress } from '../../utils/scriptAndAddress'
import SystemScriptInfo from '../../models/system-script-info'
import NetworksService from '../../services/networks'
import Address, { AddressType } from './address'

export default class HdPublicKeyInfoModel {
public walletId: string
Expand All @@ -22,7 +22,7 @@ export default class HdPublicKeyInfoModel {
}

public get path(): string {
return Address.pathFor(this.addressType, this.addressIndex)
return AccountExtendedPublicKey.pathFor(this.addressType, this.addressIndex)
}

constructor(
Expand Down
Loading

1 comment on commit 79e8481

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

Please sign in to comment.