Skip to content

Commit 02e3bca

Browse files
committed
feat: address to lock hashes now include data and type
1 parent bb36ba2 commit 02e3bca

6 files changed

Lines changed: 49 additions & 43 deletions

File tree

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ export default class TransactionsController {
9898
throw new CurrentWalletNotSet()
9999
}
100100
const addresses: string[] = (await AddressesService.allAddressesByWalletId(wallet.id)).map(addr => addr.address)
101-
const lockHashes: string[] = await Promise.all(
102-
addresses.map(async addr => {
103-
return LockUtils.addressToLockHash(addr)
104-
})
105-
)
101+
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
106102

107103
const outputCapacities: bigint = transaction
108104
.outputs!.filter(o => lockHashes.includes(o.lockHash!))

packages/neuron-wallet/src/database/address/dao.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ export default class AddressDao {
6464
addressEntities.map(async entity => {
6565
const addressEntity = entity
6666
addressEntity.txCount = txCount
67-
const lockHash: string = await LockUtils.addressToLockHash(addressEntity.address)
68-
addressEntity.liveBalance = await CellsService.getBalance([lockHash], OutputStatus.Live)
69-
addressEntity.sentBalance = await CellsService.getBalance([lockHash], OutputStatus.Sent)
70-
addressEntity.pendingBalance = await CellsService.getBalance([lockHash], OutputStatus.Pending)
67+
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(addressEntity.address)
68+
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live)
69+
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
70+
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending)
7171
return addressEntity
7272
})
7373
)

packages/neuron-wallet/src/models/lock-utils.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ export default class LockUtils {
7474
static lockScriptToHash = (lock: Script) => {
7575
const codeHash: string = lock!.codeHash!
7676
const args: string[] = lock.args!
77+
const { hashType } = lock
78+
// TODO: should support ScriptHashType.Type in the future
7779
const lockHash: string = core.utils.lockScriptToHash({
7880
codeHash,
7981
args,
80-
hashType: ScriptHashType.Data,
82+
hashType,
8183
})
8284

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

90-
static async addressToLockScript(address: string): Promise<Script> {
92+
static async addressToLockScript(address: string, hashType: ScriptHashType = ScriptHashType.Data): Promise<Script> {
9193
const systemScript = await this.systemScript()
9294

9395
const lock: Script = {
9496
codeHash: systemScript.codeHash,
9597
args: [LockUtils.addressToBlake160(address)],
96-
hashType: ScriptHashType.Data,
98+
hashType,
9799
}
98100
return lock
99101
}
100102

101-
static async addressToLockHash(address: string): Promise<string> {
102-
const lock: Script = await this.addressToLockScript(address)
103+
static async addressToLockHash(address: string, hashType: ScriptHashType = ScriptHashType.Data): Promise<string> {
104+
const lock: Script = await this.addressToLockScript(address, hashType)
103105
const lockHash: string = await this.lockScriptToHash(lock)
104106

105107
return lockHash
106108
}
107109

110+
static async addressToAllLockHashes(address: string): Promise<string[]> {
111+
const dataLockHash = await LockUtils.addressToLockHash(address, ScriptHashType.Data)
112+
const typeLockHash = await LockUtils.addressToLockHash(address, ScriptHashType.Type)
113+
return [dataLockHash, typeLockHash]
114+
}
115+
116+
static async addressesToAllLockHashes(addresses: string[]): Promise<string[]> {
117+
const lockHashes: string[] = (await Promise.all(
118+
addresses.map(async addr => {
119+
return LockUtils.addressToAllLockHashes(addr)
120+
})
121+
)).reduce((acc, val) => acc.concat(val), [])
122+
return lockHashes
123+
}
124+
108125
static lockScriptToAddress(lock: Script): string {
109126
const blake160: string = lock.args![0]
110127
return this.blake160ToAddress(blake160)

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ export default class TransactionsService {
105105
return base
106106
}
107107
if (type === SearchType.Address) {
108-
const lockHash = await LockUtils.addressToLockHash(value)
109-
return ['input.lockHash = :lockHash OR output.lockHash = :lockHash', { lockHash }]
108+
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(value)
109+
return ['input.lockHash IN (:...lockHashes) OR output.lockHash IN (:...lockHashes)', { lockHashes }]
110110
}
111111
if (type === SearchType.TxHash) {
112112
return [`${base[0]} AND tx.hash = :hash`, { lockHashes: params.lockHashes, hash: value }]
@@ -225,12 +225,7 @@ export default class TransactionsService {
225225
params: TransactionsByAddressesParam,
226226
searchValue: string = ''
227227
): Promise<PaginationResult<Transaction>> => {
228-
const lockHashes: string[] = await Promise.all(
229-
params.addresses.map(async addr => {
230-
const lockHash: string = await LockUtils.addressToLockHash(addr)
231-
return lockHash
232-
})
233-
)
228+
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(params.addresses)
234229

235230
return TransactionsService.getAll(
236231
{
@@ -246,13 +241,12 @@ export default class TransactionsService {
246241
params: TransactionsByPubkeysParams,
247242
searchValue: string = ''
248243
): Promise<PaginationResult<Transaction>> => {
249-
const lockHashes: string[] = await Promise.all(
250-
params.pubkeys.map(async pubkey => {
251-
const addr = core.utils.pubkeyToAddress(pubkey)
252-
const lockHash = await LockUtils.addressToLockHash(addr)
253-
return lockHash
254-
})
255-
)
244+
const addresses: string[] = params.pubkeys.map(pubkey => {
245+
const addr = core.utils.pubkeyToAddress(pubkey)
246+
return addr
247+
})
248+
249+
const lockHashes = await LockUtils.addressesToAllLockHashes(addresses)
256250

257251
return TransactionsService.getAll(
258252
{
@@ -580,27 +574,30 @@ export default class TransactionsService {
580574
}
581575

582576
// tx count with one lockHash and status
583-
public static getCountByLockHashAndStatus = async (
584-
lockHash: string,
577+
public static getCountByLockHashesAndStatus = async (
578+
lockHashes: string[],
585579
status: TransactionStatus[]
586580
): Promise<number> => {
587581
const count: number = await getConnection()
588582
.getRepository(TransactionEntity)
589583
.createQueryBuilder('tx')
590584
.leftJoinAndSelect('tx.inputs', 'input')
591585
.leftJoinAndSelect('tx.outputs', 'output')
592-
.where(`(input.lockHash = :lockHash OR output.lockHash = :lockHash) AND tx.status IN (:...status)`, {
593-
lockHash,
594-
status,
595-
})
586+
.where(
587+
`(input.lockHash IN (:...lockHashes) OR output.lockHash IN (:...lockHashes)) AND tx.status IN (:...status)`,
588+
{
589+
lockHashes,
590+
status,
591+
}
592+
)
596593
.getCount()
597594

598595
return count
599596
}
600597

601598
public static getCountByAddressAndStatus = async (address: string, status: TransactionStatus[]): Promise<number> => {
602-
const lockHash: string = await LockUtils.addressToLockHash(address)
603-
return TransactionsService.getCountByLockHashAndStatus(lockHash, status)
599+
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(address)
600+
return TransactionsService.getCountByLockHashesAndStatus(lockHashes, status)
604601
}
605602

606603
public static pendings = async (): Promise<TransactionEntity[]> => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export default class WalletService {
316316

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

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

321321
const targetOutputs = items.map(item => ({
322322
...item,

packages/neuron-wallet/src/startup/sync-block-task/task.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ export const stopLoopSubject = new Subject()
2727
// load all addresses and convert to lockHashes
2828
export const loadAddressesAndConvert = async (): Promise<string[]> => {
2929
const addresses: string[] = (await AddressService.allAddresses()).map(addr => addr.address)
30-
const lockHashes: string[] = await Promise.all(
31-
addresses.map(async addr => {
32-
return LockUtils.addressToLockHash(addr)
33-
})
34-
)
30+
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
3531
return lockHashes
3632
}
3733

0 commit comments

Comments
 (0)