Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async applyTransaction (transaction) {
const transactionData = transaction.data
const recipientId = transactionData.recipientId
const sender = this.getWalletByPublicKey(transactionData.senderPublicKey)
let recipient = recipientId ? this.getWalletByAddress(recipientId) : null
if (!recipient && recipientId) { // cold wallet
recipient = new Wallet(recipientId)
this.walletsByAddress[recipientId] = recipient
emitter.emit('wallet:cold:created', recipient)
} else if (transactionData.type === TRANSACTION_TYPES.DELEGATE_REGISTRATION && this.walletsByUsername[transactionData.asset.delegate.username.toLowerCase()]) {
logger.error(`Delegate transction sent by ${sender.address}`, JSON.stringify(transactionData))
throw new Error(`Can't apply transaction ${transactionData.id}: delegate name already taken`)
} else if (transactionData.type === TRANSACTION_TYPES.VOTE && !this.walletsByPublicKey[transactionData.asset.votes[0].slice(1)].username) {
logger.error(`Vote transaction sent by ${sender.address}`, JSON.stringify(transactionData))
throw new Error(`Can't apply transaction ${transactionData.id}: voted delegate does not exist`)
} else if (config.network.exceptions[transactionData.id]) {
logger.warn('Transaction forcibly applied because it has been added as an exception:', transactionData)
} else if (!sender.canApply(transactionData)) {
logger.error(`Can't apply transaction for ${sender.address}`, JSON.stringify(transactionData))
logger.debug('Audit', JSON.stringify(sender.auditApply(transactionData), null, 2))
throw new Error(`Can't apply transaction ${transactionData.id}`)
}
sender.applyTransactionToSender(transactionData)
if (recipient && transactionData.type === TRANSACTION_TYPES.TRANSFER) {
recipient.applyTransactionToRecipient(transactionData)
`[PoolWalletManager] Can't apply transaction ${
data.id
}: delegate name already taken.`,
JSON.stringify(data),
)
throw new Error(
`[PoolWalletManager] Can't apply transaction ${
data.id
}: delegate name already taken.`,
)
// NOTE: We use the vote public key, because vote transactions
// have the same sender and recipient.
} else if (
type === TRANSACTION_TYPES.VOTE &&
!database.walletManager.__isDelegate(asset.votes[0].slice(1))
) {
logger.error(
`[PoolWalletManager] Can't apply vote transaction: delegate ${
asset.votes[0]
} does not exist.`,
JSON.stringify(data),
)
throw new Error(
`[PoolWalletManager] Can't apply transaction ${data.id}: delegate ${
asset.votes[0]
} does not exist.`,
)
} else if (this.__isException(data)) {
logger.warn(
const address = testAddress || crypto.getAddress(crypto.getKeys(passphrase).publicKey)
let builder
if (type === TRANSACTION_TYPES.TRANSFER) {
builder = client.getBuilder().transfer()
.recipientId(address)
.amount(amount)
.vendorField(`Test Transaction ${i + 1}`)
} else if (type === TRANSACTION_TYPES.SECOND_SIGNATURE) {
builder = client.getBuilder().secondSignature()
.signatureAsset(passphrase)
} else if (type === TRANSACTION_TYPES.DELEGATE_REGISTRATION) {
const username = superheroes.random().toLowerCase().replace(/[^a-z0-9]/g, '_')
builder = client.getBuilder().delegateRegistration()
.usernameAsset(username)
} else if (type === TRANSACTION_TYPES.VOTE) {
const publicKey = crypto.getKeys(config.passphrase).publicKey
builder = client.getBuilder().vote()
.votesAsset([`+${publicKey}`])
} else {
continue
}
const transaction = builder
.sign(passphrase)
.build()
transactions.push(transaction)
}
return transactions
}
module.exports = (network, type, testWallet, testAddress, amount = 2, quantity = 10) => {
network = network || 'testnet'
type = type || TRANSACTION_TYPES.TRANSFER
amount = amount * Math.pow(10, 8)
assert.ok(['mainnet', 'devnet', 'testnet'].includes(network), 'Invalid network')
assert.ok([
TRANSACTION_TYPES.TRANSFER,
TRANSACTION_TYPES.SECOND_SIGNATURE,
TRANSACTION_TYPES.DELEGATE_REGISTRATION,
TRANSACTION_TYPES.VOTE
].includes(type), 'Invalid transaction type')
client.getConfigManager().setFromPreset('ark', network)
const transactions = []
for (let i = 0; i < quantity; i++) {
const passphrase = testWallet ? testWallet.passphrase : config.passphrase
const address = testAddress || crypto.getAddress(crypto.getKeys(passphrase).publicKey)
let builder
if (type === TRANSACTION_TYPES.TRANSFER) {
builder = client.getBuilder().transfer()
.recipientId(address)
.amount(amount)
.vendorField(`Test Transaction ${i + 1}`)
} else if (type === TRANSACTION_TYPES.SECOND_SIGNATURE) {
async allVotesBySender(senderPublicKey, parameters = {}) {
return this.findAll({
...{ senderPublicKey, type: TRANSACTION_TYPES.VOTE },
...parameters,
})
}
const index = async request => {
const transactions = await transactionsRepository.findAllByType(
TRANSACTION_TYPES.VOTE,
{
...request.query,
...utils.paginate(request),
},
)
return utils.toPagination(request, transactions, 'transaction')
}
async handler (request, h) {
let transactions = await database.transactions.findAllByDateAndType(TRANSACTION_TYPES.VOTE, request.query.from, request.query.to)
transactions = transactions.filter(transaction => transaction.asset.votes[0].startsWith('-'))
return {
data: {
count: transactions.length,
amount: _.sumBy(transactions, 'amount'),
fees: _.sumBy(transactions, 'fee')
}
}
},
options: {
async allVotesBySender (senderPublicKey, params = {}) {
return this.findAll({...{senderPublicKey, type: TRANSACTION_TYPES.VOTE}, ...params})
}
async handler (request, h) {
let transactions = await database.transactions.findAllByDateAndType(TRANSACTION_TYPES.VOTE, request.query.from, request.query.to)
transactions = transactions.filter(transaction => transaction.asset.votes[0].startsWith('+'))
return {
data: {
count: transactions.length,
amount: _.sumBy(transactions, 'amount'),
fees: _.sumBy(transactions, 'fee')
}
}
},
options: {
async __buildVotes () {
const data = await this.query
.select('sender_public_key', 'serialized')
.from('transactions')
.where('type', TRANSACTION_TYPES.VOTE)
.orderBy('created_at', 'DESC')
.all()
data.forEach(row => {
const wallet = this.walletManager.findByPublicKey(row.senderPublicKey)
if (!wallet.voted) {
const vote = Transaction.deserialize(row.serialized.toString('hex')).asset.votes[0]
if (vote.startsWith('+')) {
wallet.vote = vote.slice(1)
}
wallet.voted = true
}
})
this.walletManager.updateDelegates()