Skip to content

Commit

Permalink
feat: address to lock hashes now include data and type
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jul 29, 2019
1 parent bb36ba2 commit 02e3bca
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
6 changes: 1 addition & 5 deletions packages/neuron-wallet/src/controllers/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ export default class TransactionsController {
throw new CurrentWalletNotSet()
}
const addresses: string[] = (await AddressesService.allAddressesByWalletId(wallet.id)).map(addr => addr.address)
const lockHashes: string[] = await Promise.all(
addresses.map(async addr => {
return LockUtils.addressToLockHash(addr)
})
)
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)

const outputCapacities: bigint = transaction
.outputs!.filter(o => lockHashes.includes(o.lockHash!))
Expand Down
8 changes: 4 additions & 4 deletions packages/neuron-wallet/src/database/address/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export default class AddressDao {
addressEntities.map(async entity => {
const addressEntity = entity
addressEntity.txCount = txCount
const lockHash: string = await LockUtils.addressToLockHash(addressEntity.address)
addressEntity.liveBalance = await CellsService.getBalance([lockHash], OutputStatus.Live)
addressEntity.sentBalance = await CellsService.getBalance([lockHash], OutputStatus.Sent)
addressEntity.pendingBalance = await CellsService.getBalance([lockHash], OutputStatus.Pending)
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(addressEntity.address)
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live)
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending)
return addressEntity
})
)
Expand Down
27 changes: 22 additions & 5 deletions packages/neuron-wallet/src/models/lock-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export default class LockUtils {
static lockScriptToHash = (lock: Script) => {
const codeHash: string = lock!.codeHash!
const args: string[] = lock.args!
const { hashType } = lock
// TODO: should support ScriptHashType.Type in the future
const lockHash: string = core.utils.lockScriptToHash({
codeHash,
args,
hashType: ScriptHashType.Data,
hashType,
})

if (lockHash.startsWith('0x')) {
Expand All @@ -87,24 +89,39 @@ export default class LockUtils {
return `0x${lockHash}`
}

static async addressToLockScript(address: string): Promise<Script> {
static async addressToLockScript(address: string, hashType: ScriptHashType = ScriptHashType.Data): Promise<Script> {
const systemScript = await this.systemScript()

const lock: Script = {
codeHash: systemScript.codeHash,
args: [LockUtils.addressToBlake160(address)],
hashType: ScriptHashType.Data,
hashType,
}
return lock
}

static async addressToLockHash(address: string): Promise<string> {
const lock: Script = await this.addressToLockScript(address)
static async addressToLockHash(address: string, hashType: ScriptHashType = ScriptHashType.Data): Promise<string> {
const lock: Script = await this.addressToLockScript(address, hashType)
const lockHash: string = await this.lockScriptToHash(lock)

return lockHash
}

static async addressToAllLockHashes(address: string): Promise<string[]> {
const dataLockHash = await LockUtils.addressToLockHash(address, ScriptHashType.Data)
const typeLockHash = await LockUtils.addressToLockHash(address, ScriptHashType.Type)
return [dataLockHash, typeLockHash]
}

static async addressesToAllLockHashes(addresses: string[]): Promise<string[]> {
const lockHashes: string[] = (await Promise.all(
addresses.map(async addr => {
return LockUtils.addressToAllLockHashes(addr)
})
)).reduce((acc, val) => acc.concat(val), [])
return lockHashes
}

static lockScriptToAddress(lock: Script): string {
const blake160: string = lock.args![0]
return this.blake160ToAddress(blake160)
Expand Down
43 changes: 20 additions & 23 deletions packages/neuron-wallet/src/services/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export default class TransactionsService {
return base
}
if (type === SearchType.Address) {
const lockHash = await LockUtils.addressToLockHash(value)
return ['input.lockHash = :lockHash OR output.lockHash = :lockHash', { lockHash }]
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(value)
return ['input.lockHash IN (:...lockHashes) OR output.lockHash IN (:...lockHashes)', { lockHashes }]
}
if (type === SearchType.TxHash) {
return [`${base[0]} AND tx.hash = :hash`, { lockHashes: params.lockHashes, hash: value }]
Expand Down Expand Up @@ -225,12 +225,7 @@ export default class TransactionsService {
params: TransactionsByAddressesParam,
searchValue: string = ''
): Promise<PaginationResult<Transaction>> => {
const lockHashes: string[] = await Promise.all(
params.addresses.map(async addr => {
const lockHash: string = await LockUtils.addressToLockHash(addr)
return lockHash
})
)
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(params.addresses)

return TransactionsService.getAll(
{
Expand All @@ -246,13 +241,12 @@ export default class TransactionsService {
params: TransactionsByPubkeysParams,
searchValue: string = ''
): Promise<PaginationResult<Transaction>> => {
const lockHashes: string[] = await Promise.all(
params.pubkeys.map(async pubkey => {
const addr = core.utils.pubkeyToAddress(pubkey)
const lockHash = await LockUtils.addressToLockHash(addr)
return lockHash
})
)
const addresses: string[] = params.pubkeys.map(pubkey => {
const addr = core.utils.pubkeyToAddress(pubkey)
return addr
})

const lockHashes = await LockUtils.addressesToAllLockHashes(addresses)

return TransactionsService.getAll(
{
Expand Down Expand Up @@ -580,27 +574,30 @@ export default class TransactionsService {
}

// tx count with one lockHash and status
public static getCountByLockHashAndStatus = async (
lockHash: string,
public static getCountByLockHashesAndStatus = async (
lockHashes: string[],
status: TransactionStatus[]
): Promise<number> => {
const count: number = await getConnection()
.getRepository(TransactionEntity)
.createQueryBuilder('tx')
.leftJoinAndSelect('tx.inputs', 'input')
.leftJoinAndSelect('tx.outputs', 'output')
.where(`(input.lockHash = :lockHash OR output.lockHash = :lockHash) AND tx.status IN (:...status)`, {
lockHash,
status,
})
.where(
`(input.lockHash IN (:...lockHashes) OR output.lockHash IN (:...lockHashes)) AND tx.status IN (:...status)`,
{
lockHashes,
status,
}
)
.getCount()

return count
}

public static getCountByAddressAndStatus = async (address: string, status: TransactionStatus[]): Promise<number> => {
const lockHash: string = await LockUtils.addressToLockHash(address)
return TransactionsService.getCountByLockHashAndStatus(lockHash, status)
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(address)
return TransactionsService.getCountByLockHashesAndStatus(lockHashes, status)
}

public static pendings = async (): Promise<TransactionEntity[]> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export default class WalletService {

const addresses: string[] = addressInfos.map(info => info.address)

const lockHashes: string[] = await Promise.all(addresses.map(async addr => LockUtils.addressToLockHash(addr)))
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)

const targetOutputs = items.map(item => ({
...item,
Expand Down
6 changes: 1 addition & 5 deletions packages/neuron-wallet/src/startup/sync-block-task/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export const stopLoopSubject = new Subject()
// load all addresses and convert to lockHashes
export const loadAddressesAndConvert = async (): Promise<string[]> => {
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address)
const lockHashes: string[] = await Promise.all(
addresses.map(async addr => {
return LockUtils.addressToLockHash(addr)
})
)
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
return lockHashes
}

Expand Down

0 comments on commit 02e3bca

Please sign in to comment.