Browse Source
- address validation - total fees & Total spent calculation - not enough balance detectionmaster
Gaëtan Renaudeau
7 years ago
7 changed files with 150 additions and 9 deletions
@ -0,0 +1,63 @@ |
|||
// @flow
|
|||
|
|||
import { Observable } from 'rxjs' |
|||
import withLibcore from 'helpers/withLibcore' |
|||
import { createCommand, Command } from 'helpers/ipc' |
|||
import * as accountIdHelper from 'helpers/accountId' |
|||
import { isValidAddress } from 'helpers/libcore' |
|||
import createCustomErrorClass from 'helpers/createCustomErrorClass' |
|||
|
|||
const InvalidAddress = createCustomErrorClass('InvalidAddress') |
|||
|
|||
type BitcoinLikeTransaction = { |
|||
// TODO we rename this Transaction concept into transactionInput
|
|||
amount: number, |
|||
feePerByte: number, |
|||
recipient: string, |
|||
} |
|||
|
|||
type Input = { |
|||
accountId: string, |
|||
accountIndex: number, |
|||
transaction: BitcoinLikeTransaction, |
|||
} |
|||
|
|||
type Result = { totalFees: number } |
|||
|
|||
const cmd: Command<Input, Result> = createCommand( |
|||
'libcoreGetFees', |
|||
({ accountId, accountIndex, transaction }) => |
|||
Observable.create(o => { |
|||
let unsubscribed = false |
|||
const isCancelled = () => unsubscribed |
|||
|
|||
withLibcore(async core => { |
|||
const { walletName } = accountIdHelper.decode(accountId) |
|||
const njsWallet = await core.getWallet(walletName) |
|||
if (isCancelled()) return |
|||
const njsAccount = await njsWallet.getAccount(accountIndex) |
|||
if (isCancelled()) return |
|||
const bitcoinLikeAccount = njsAccount.asBitcoinLikeAccount() |
|||
const njsWalletCurrency = njsWallet.getCurrency() |
|||
const amount = core.createAmount(njsWalletCurrency, transaction.amount) |
|||
const feesPerByte = core.createAmount(njsWalletCurrency, transaction.feePerByte) |
|||
const transactionBuilder = bitcoinLikeAccount.buildTransaction() |
|||
if (!isValidAddress(core, njsWalletCurrency, transaction.recipient)) { |
|||
// FIXME this is a bug in libcore. later it will probably check this and we can remove this check
|
|||
throw new InvalidAddress() |
|||
} |
|||
transactionBuilder.sendToAddress(amount, transaction.recipient) |
|||
transactionBuilder.pickInputs(0, 0xffffff) |
|||
transactionBuilder.setFeesPerByte(feesPerByte) |
|||
const builded = await transactionBuilder.build() |
|||
const totalFees = builded.getFees().toLong() |
|||
o.next({ totalFees }) |
|||
}).then(() => o.complete(), e => o.error(e)) |
|||
|
|||
return () => { |
|||
unsubscribed = true |
|||
} |
|||
}), |
|||
) |
|||
|
|||
export default cmd |
@ -0,0 +1,24 @@ |
|||
// @flow
|
|||
|
|||
import { fromPromise } from 'rxjs/observable/fromPromise' |
|||
import withLibcore from 'helpers/withLibcore' |
|||
import { createCommand, Command } from 'helpers/ipc' |
|||
import { isValidAddress } from 'helpers/libcore' |
|||
|
|||
type Input = { |
|||
address: string, |
|||
currencyId: string, |
|||
} |
|||
|
|||
const cmd: Command<Input, boolean> = createCommand( |
|||
'libcoreValidAddress', |
|||
({ currencyId, address }) => |
|||
fromPromise( |
|||
withLibcore(async core => { |
|||
const currency = await core.getCurrency(currencyId) |
|||
return isValidAddress(core, currency, address) |
|||
}), |
|||
), |
|||
) |
|||
|
|||
export default cmd |
Loading…
Reference in new issue