Skip to content

Commit ad77232

Browse files
committed
fix(neuron-wallet): check min capacity
1 parent ba79b60 commit ad77232

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

packages/neuron-wallet/src/exceptions/transaction.ts

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export class TransactionNotFound extends Error {
66
}
77
}
88

9+
export class CapacityTooSmall extends Error {
10+
constructor() {
11+
super(i18n.t('messages.capacity-too-small'))
12+
}
13+
}
14+
915
export default {
1016
TransactionNotFound,
17+
CapacityTooSmall,
1118
}

packages/neuron-wallet/src/locales/en.ts

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export default {
8585
'invalid-mnemonic': 'Mnemonic is invalid',
8686
'unsupported-cipher': 'Unsupported cipher',
8787
'capacity-not-enough': 'Capacity is not enough',
88+
'capacity-too-small': 'Capacity less than min',
8889
'should-be-type-of': '{{field}} should be type of {{type}}',
8990
},
9091
contextMenu: {

packages/neuron-wallet/src/locales/zh.ts

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export default {
8484
'invalid-mnemonic': '助记词不合法',
8585
'unsupported-cipher': '不支持的 Cipher',
8686
'capacity-not-enough': '余额不足',
87+
'capacity-too-small': '金额小于最低金额',
8788
'should-be-type-of': '{{field}} 应该为 {{type}} 类型',
8889
},
8990
contextMenu: {

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Cell, OutPoint, Input } from '../types/cell-types'
44
import { CapacityNotEnough } from '../exceptions'
55
import { OutputStatus } from './transactions'
66

7-
const MIN_CELL_CAPACITY = '40'
7+
export const MIN_CELL_CAPACITY = '6000000000'
88

99
/* eslint @typescript-eslint/no-unused-vars: "warn" */
1010
/* eslint no-await-in-loop: "warn" */
@@ -51,12 +51,18 @@ export default class CellsService {
5151
// gather inputs for generateTx
5252
public static gatherInputs = async (
5353
capacity: string,
54-
lockHashes: string[]
54+
lockHashes: string[],
55+
fee: string = '0'
5556
): Promise<{
5657
inputs: Input[]
5758
capacities: string
5859
}> => {
5960
const capacityInt = BigInt(capacity)
61+
const feeInt = BigInt(fee)
62+
const totalCapacities: bigint = capacityInt + feeInt
63+
64+
// use min secp size (without data)
65+
const minChangeCapacity = BigInt(MIN_CELL_CAPACITY)
6066

6167
if (capacityInt < BigInt(MIN_CELL_CAPACITY)) {
6268
throw new Error(`capacity can't be less than ${MIN_CELL_CAPACITY}`)
@@ -92,7 +98,9 @@ export default class CellsService {
9298
}
9399
inputs.push(input)
94100
inputCapacities += BigInt(cell.capacity)
95-
if (inputCapacities > capacityInt) {
101+
102+
const diff = inputCapacities - totalCapacities
103+
if (diff >= minChangeCapacity || diff === BigInt(0)) {
96104
return false
97105
}
98106
return true

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { getConnection, In, ObjectLiteral } from 'typeorm'
22
import { ReplaySubject } from 'rxjs'
33
import { OutPoint, Transaction, TransactionWithoutHash, Input, Cell, TransactionStatus } from '../types/cell-types'
4-
import CellsService from './cells'
4+
import CellsService, { MIN_CELL_CAPACITY } from './cells'
55
import InputEntity from '../database/chain/entities/input'
66
import OutputEntity from '../database/chain/entities/output'
77
import TransactionEntity from '../database/chain/entities/transaction'
88
import NodeService from './node'
99
import LockUtils from '../models/lock-utils'
10+
import { CapacityTooSmall } from '../exceptions'
1011

1112
const { core } = NodeService.getInstance()
1213

@@ -504,11 +505,15 @@ export default class TransactionsService {
504505
.map(o => BigInt(o.capacity))
505506
.reduce((result, c) => result + c, BigInt(0))
506507

507-
const { inputs, capacities } = await CellsService.gatherInputs(needCapacities.toString(), lockHashes)
508+
const minCellCapacity = BigInt(MIN_CELL_CAPACITY)
508509

509510
const outputs: Cell[] = targetOutputs.map(o => {
510511
const { capacity, address } = o
511512

513+
if (BigInt(capacity) < minCellCapacity) {
514+
throw new CapacityTooSmall()
515+
}
516+
512517
const blake160: string = LockUtils.addressToBlake160(address)
513518

514519
const output: Cell = {
@@ -523,6 +528,8 @@ export default class TransactionsService {
523528
return output
524529
})
525530

531+
const { inputs, capacities } = await CellsService.gatherInputs(needCapacities.toString(), lockHashes, fee)
532+
526533
// change
527534
if (BigInt(capacities) > needCapacities + BigInt(fee)) {
528535
const changeBlake160: string = LockUtils.addressToBlake160(changeAddress)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import LockUtils from '../models/lock-utils'
1010
import { Witness, TransactionWithoutHash, Input } from '../types/cell-types'
1111
import ConvertTo from '../types/convert-to'
1212
import Blake2b from '../utils/blake2b'
13-
import { CurrentWalletNotSet, WalletNotFound, IsRequired, UsedName } from '../exceptions'
13+
import { WalletNotFound, IsRequired, UsedName } from '../exceptions'
1414
import AddressService from './addresses'
1515
import { Address as AddressInterface } from '../database/address/dao'
1616
import Keychain from '../models/keys/keychain'
@@ -305,7 +305,7 @@ export default class WalletService {
305305
) => {
306306
const wallet = await this.get(walletID)
307307
if (!wallet) {
308-
throw new CurrentWalletNotSet()
308+
throw new WalletNotFound(walletID)
309309
}
310310

311311
if (password === '') {
@@ -320,7 +320,7 @@ export default class WalletService {
320320

321321
const targetOutputs = items.map(item => ({
322322
...item,
323-
capacity: (BigInt(item.capacity) * BigInt(1)).toString(),
323+
capacity: BigInt(item.capacity).toString(),
324324
}))
325325

326326
const changeAddress: string = await this.getChangeAddress()

0 commit comments

Comments
 (0)