Skip to content

Commit

Permalink
fix(neuron-wallet): check min capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jul 25, 2019
1 parent ba79b60 commit ad77232
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
7 changes: 7 additions & 0 deletions packages/neuron-wallet/src/exceptions/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export class TransactionNotFound extends Error {
}
}

export class CapacityTooSmall extends Error {
constructor() {
super(i18n.t('messages.capacity-too-small'))
}
}

export default {
TransactionNotFound,
CapacityTooSmall,
}
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default {
'invalid-mnemonic': 'Mnemonic is invalid',
'unsupported-cipher': 'Unsupported cipher',
'capacity-not-enough': 'Capacity is not enough',
'capacity-too-small': 'Capacity less than min',
'should-be-type-of': '{{field}} should be type of {{type}}',
},
contextMenu: {
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default {
'invalid-mnemonic': '助记词不合法',
'unsupported-cipher': '不支持的 Cipher',
'capacity-not-enough': '余额不足',
'capacity-too-small': '金额小于最低金额',
'should-be-type-of': '{{field}} 应该为 {{type}} 类型',
},
contextMenu: {
Expand Down
14 changes: 11 additions & 3 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Cell, OutPoint, Input } from '../types/cell-types'
import { CapacityNotEnough } from '../exceptions'
import { OutputStatus } from './transactions'

const MIN_CELL_CAPACITY = '40'
export const MIN_CELL_CAPACITY = '6000000000'

/* eslint @typescript-eslint/no-unused-vars: "warn" */
/* eslint no-await-in-loop: "warn" */
Expand Down Expand Up @@ -51,12 +51,18 @@ export default class CellsService {
// gather inputs for generateTx
public static gatherInputs = async (
capacity: string,
lockHashes: string[]
lockHashes: string[],
fee: string = '0'
): Promise<{
inputs: Input[]
capacities: string
}> => {
const capacityInt = BigInt(capacity)
const feeInt = BigInt(fee)
const totalCapacities: bigint = capacityInt + feeInt

// use min secp size (without data)
const minChangeCapacity = BigInt(MIN_CELL_CAPACITY)

if (capacityInt < BigInt(MIN_CELL_CAPACITY)) {
throw new Error(`capacity can't be less than ${MIN_CELL_CAPACITY}`)
Expand Down Expand Up @@ -92,7 +98,9 @@ export default class CellsService {
}
inputs.push(input)
inputCapacities += BigInt(cell.capacity)
if (inputCapacities > capacityInt) {

const diff = inputCapacities - totalCapacities
if (diff >= minChangeCapacity || diff === BigInt(0)) {
return false
}
return true
Expand Down
11 changes: 9 additions & 2 deletions packages/neuron-wallet/src/services/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { getConnection, In, ObjectLiteral } from 'typeorm'
import { ReplaySubject } from 'rxjs'
import { OutPoint, Transaction, TransactionWithoutHash, Input, Cell, TransactionStatus } from '../types/cell-types'
import CellsService from './cells'
import CellsService, { MIN_CELL_CAPACITY } from './cells'
import InputEntity from '../database/chain/entities/input'
import OutputEntity from '../database/chain/entities/output'
import TransactionEntity from '../database/chain/entities/transaction'
import NodeService from './node'
import LockUtils from '../models/lock-utils'
import { CapacityTooSmall } from '../exceptions'

const { core } = NodeService.getInstance()

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

const { inputs, capacities } = await CellsService.gatherInputs(needCapacities.toString(), lockHashes)
const minCellCapacity = BigInt(MIN_CELL_CAPACITY)

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

if (BigInt(capacity) < minCellCapacity) {
throw new CapacityTooSmall()
}

const blake160: string = LockUtils.addressToBlake160(address)

const output: Cell = {
Expand All @@ -523,6 +528,8 @@ export default class TransactionsService {
return output
})

const { inputs, capacities } = await CellsService.gatherInputs(needCapacities.toString(), lockHashes, fee)

// change
if (BigInt(capacities) > needCapacities + BigInt(fee)) {
const changeBlake160: string = LockUtils.addressToBlake160(changeAddress)
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import LockUtils from '../models/lock-utils'
import { Witness, TransactionWithoutHash, Input } from '../types/cell-types'
import ConvertTo from '../types/convert-to'
import Blake2b from '../utils/blake2b'
import { CurrentWalletNotSet, WalletNotFound, IsRequired, UsedName } from '../exceptions'
import { WalletNotFound, IsRequired, UsedName } from '../exceptions'
import AddressService from './addresses'
import { Address as AddressInterface } from '../database/address/dao'
import Keychain from '../models/keys/keychain'
Expand Down Expand Up @@ -305,7 +305,7 @@ export default class WalletService {
) => {
const wallet = await this.get(walletID)
if (!wallet) {
throw new CurrentWalletNotSet()
throw new WalletNotFound(walletID)
}

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

const targetOutputs = items.map(item => ({
...item,
capacity: (BigInt(item.capacity) * BigInt(1)).toString(),
capacity: BigInt(item.capacity).toString(),
}))

const changeAddress: string = await this.getChangeAddress()
Expand Down

0 comments on commit ad77232

Please sign in to comment.