Skip to content

Commit

Permalink
Merge pull request #1110 from nervosnetwork/check-and-generate-address
Browse files Browse the repository at this point in the history
Check and generate address
  • Loading branch information
ashchan authored Nov 14, 2019
2 parents b0e2e33 + fffd2f0 commit f00b241
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
22 changes: 20 additions & 2 deletions packages/neuron-wallet/src/database/address/address-dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,28 @@ export default class AddressDao {
&& value.txCount === 0
})
return addresses.sort((lhs, rhs) => {
return lhs.addressIndex < rhs.addressIndex ? 1 : -1
return lhs.addressIndex - rhs.addressIndex
})[0]
}

public static unusedAddressesCount(walletId: string, version: AddressVersion): [number, number] {
const addresses = AddressStore.getAll()
const receivingCount = addresses.filter(value => {
return value.walletId === walletId
&& value.version === version
&& value.addressType == AddressType.Receiving
&& value.txCount === 0
}).length
const changeCount = addresses.filter(value => {
return value.walletId === walletId
&& value.version === version
&& value.addressType == AddressType.Change
&& value.txCount === 0
}).length

return [receivingCount, changeCount]
}

public static nextUnusedChangeAddress(walletId: string, version: AddressVersion): Address | undefined {
const addresses = AddressStore.getAll().filter(value => {
return value.walletId === walletId
Expand All @@ -108,7 +126,7 @@ export default class AddressDao {
&& value.txCount === 0
})
return addresses.sort((lhs, rhs) => {
return lhs.addressIndex < rhs.addressIndex ? 1 : -1
return lhs.addressIndex - rhs.addressIndex
})[0]
}

Expand Down
11 changes: 5 additions & 6 deletions packages/neuron-wallet/src/services/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@ export default class AddressService {
changeAddressCount: number = 10
) => {
const addressVersion = AddressService.getAddressVersion()
const maxIndexReceivingAddress = AddressDao.maxAddressIndex(walletId, AddressType.Receiving, addressVersion)
const maxIndexChangeAddress = AddressDao.maxAddressIndex(walletId, AddressType.Change, addressVersion)
const [unusedReceivingCount, unusedChangeCount] = AddressDao.unusedAddressesCount(walletId, addressVersion)
if (
maxIndexReceivingAddress !== undefined &&
maxIndexReceivingAddress.txCount === 0 &&
maxIndexChangeAddress !== undefined &&
maxIndexChangeAddress.txCount === 0
unusedReceivingCount > 3 &&
unusedChangeCount > 3
) {
return undefined
}
const maxIndexReceivingAddress = AddressDao.maxAddressIndex(walletId, AddressType.Receiving, addressVersion)
const maxIndexChangeAddress = AddressDao.maxAddressIndex(walletId, AddressType.Change, addressVersion)
const nextReceivingIndex = maxIndexReceivingAddress === undefined ? 0 : maxIndexReceivingAddress.addressIndex + 1
const nextChangeIndex = maxIndexChangeAddress === undefined ? 0 : maxIndexChangeAddress.addressIndex + 1
return AddressService.generateAndSave(
Expand Down
30 changes: 30 additions & 0 deletions packages/neuron-wallet/tests/database/address/dao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ describe('Address Dao tests', () => {
version: AddressVersion.Testnet,
}

const address2: Address = {
walletId: '1',
address: 'ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83',
path: "m/44'/309'/0'/0/1",
addressType: AddressType.Receiving,
addressIndex: 1,
txCount: 0,
liveBalance: '0',
sentBalance: '0',
pendingBalance: '0',
balance: '0',
blake160: '0x36c329ed630d6ce750712a477543672adab57f4c',
version: AddressVersion.Testnet,
}

const usedAddress: Address = {
walletId: '2',
address: 'ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83',
Expand Down Expand Up @@ -135,4 +150,19 @@ describe('Address Dao tests', () => {

expect(one!.address).toEqual(address.address)
})

it('unusedAddressesCount', () => {
AddressDao.create([address, changeAddress])

const counts = AddressDao.unusedAddressesCount(address.walletId, AddressVersion.Testnet)
expect(counts).toEqual([1, 1])
})

it('nextUnusedAddress', () => {
AddressDao.create([address, address2])

const next = AddressDao.nextUnusedAddress('1', AddressVersion.Testnet)

expect(next!.address).toEqual(address.address)
})
})

0 comments on commit f00b241

Please sign in to comment.