All files / src/utils verify.ts

100% Statements 37/37
97.37% Branches 37/38
100% Functions 6/6
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                            17x 17x   17x   17x         17x         6x                   17x         12x                   17x           8x 8x 1x                 7x     17x           10x 1x           9x 5x   4x 1x   3x           3x 2x     1x               17x 23x           17x           12x 1x                       11x 4x           7x               7x 3x           4x         3x           1x          
/*
 * Copyright © 2019 Lisk Foundation
 *
 * See the LICENSE file at the top-level directory of this distribution
 * for licensing information.
 *
 * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
 * no part of this software, including this file, may be copied, modified,
 * propagated, or distributed except according to the terms contained in the
 * LICENSE file.
 *
 * Removal or modification of this copyright notice is prohibited.
 *
 */
import { MultisignatureStatus } from '../base_transaction';
import { TransactionError, TransactionPendingError } from '../errors';
import { Account } from '../transaction_types';
import { convertBeddowsToLSK } from '../utils/format';
 
import {
	validateMultisignatures,
	validateSignature,
} from './sign_and_validate';
 
export const verifySenderPublicKey = (
	id: string,
	sender: Account,
	publicKey: string,
): TransactionError | undefined =>
	sender.publicKey && sender.publicKey !== publicKey
		? new TransactionError(
				'Invalid sender publicKey',
				id,
				'.senderPublicKey',
				publicKey,
				sender.publicKey,
		  )
		: undefined;
 
export const verifyBalance = (
	id: string,
	account: Account,
	amount: bigint,
): TransactionError | undefined =>
	BigInt(account.balance) < BigInt(amount)
		? new TransactionError(
				`Account does not have enough LSK: ${
					account.address
				}, balance: ${convertBeddowsToLSK(account.balance.toString())}`,
				id,
				'.balance',
		  )
		: undefined;
 
export const verifyAmountBalance = (
	id: string,
	account: Account,
	amount: bigint,
	fee: bigint,
): TransactionError | undefined => {
	const balance = BigInt(account.balance);
	if (balance >= BigInt(0) && balance < BigInt(amount)) {
		return new TransactionError(
			`Account does not have enough LSK: ${
				account.address
			}, balance: ${convertBeddowsToLSK((balance + fee).toString())}`,
			id,
			'.balance',
		);
	}
 
	return undefined;
};
 
export const verifySecondSignature = (
	id: string,
	sender: Account,
	signSignature: string | undefined,
	transactionBytes: Buffer,
): TransactionError | undefined => {
	if (!sender.secondPublicKey && signSignature) {
		return new TransactionError(
			'Sender does not have a secondPublicKey',
			id,
			'.signSignature',
		);
	}
	if (!sender.secondPublicKey) {
		return undefined;
	}
	if (!signSignature) {
		return new TransactionError('Missing signSignature', id, '.signSignature');
	}
	const { valid, error } = validateSignature(
		sender.secondPublicKey,
		signSignature,
		transactionBytes,
		id,
	);
	if (valid) {
		return undefined;
	}
 
	return error;
};
 
export interface VerifyMultiSignatureResult {
	readonly status: MultisignatureStatus;
	readonly errors: ReadonlyArray<TransactionError>;
}
 
const isMultisignatureAccount = (account: Account): boolean =>
	!!(
		account.membersPublicKeys &&
		account.membersPublicKeys.length > 0 &&
		account.multiMin
	);
 
export const verifyMultiSignatures = (
	id: string,
	sender: Account,
	signatures: ReadonlyArray<string>,
	transactionBytes: Buffer,
): VerifyMultiSignatureResult => {
	if (!isMultisignatureAccount(sender) && signatures.length > 0) {
		return {
			status: MultisignatureStatus.FAIL,
			errors: [
				new TransactionError(
					'Sender is not a multisignature account',
					id,
					'.signatures',
				),
			],
		};
	}
 
	if (!isMultisignatureAccount(sender)) {
		return {
			status: MultisignatureStatus.NONMULTISIGNATURE,
			errors: [],
		};
	}
 
	const { valid, errors } = validateMultisignatures(
		sender.membersPublicKeys as ReadonlyArray<string>,
		signatures,
		sender.multiMin as number,
		transactionBytes,
		id,
	);
 
	if (valid) {
		return {
			status: MultisignatureStatus.READY,
			errors: [],
		};
	}
 
	if (
		errors &&
		errors.length === 1 &&
		errors[0] instanceof TransactionPendingError
	) {
		return {
			status: MultisignatureStatus.PENDING,
			errors,
		};
	}
 
	return {
		status: MultisignatureStatus.FAIL,
		errors: errors || [],
	};
};