All files / src 9_second_signature_transaction.ts

100% Statements 39/39
80% Branches 8/10
100% Functions 10/10
100% Lines 39/39

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                            5x 5x   5x         5x 5x   5x           5x                     5x   5x 5x     43x 43x       43x       83x   83x       1x                   3x     3x       1x                 2x       2x         2x           3x 3x   3x 1x               3x         3x   3x           2x 2x             2x   2x       41x 41x 41x 41x       41x       41x      
/*
 * 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 { hash, hexToBuffer, signData } from '@liskhq/lisk-cryptography';
import { validator } from '@liskhq/lisk-validator';
 
import {
	BaseTransaction,
	StateStore,
	StateStorePrepare,
} from './base_transaction';
import { SIGNATURE_FEE } from './constants';
import { convertToAssetError, TransactionError } from './errors';
import { TransactionJSON } from './transaction_types';
import { getId } from './utils';
 
export interface SecondSignatureAsset {
	readonly publicKey: string;
}
 
export const secondSignatureAssetFormatSchema = {
	type: 'object',
	required: ['publicKey'],
	properties: {
		publicKey: {
			type: 'string',
			format: 'publicKey',
		},
	},
};
 
export class SecondSignatureTransaction extends BaseTransaction {
	public readonly asset: SecondSignatureAsset;
	public static TYPE = 9;
	public static FEE = SIGNATURE_FEE.toString();
 
	public constructor(rawTransaction: unknown) {
		super(rawTransaction);
		const tx = (typeof rawTransaction === 'object' && rawTransaction !== null
			? rawTransaction
			: {}) as Partial<TransactionJSON>;
		// tslint:disable-next-line no-object-literal-type-assertion
		this.asset = (tx.asset || { signature: {} }) as SecondSignatureAsset;
	}
 
	protected assetToBytes(): Buffer {
		const { publicKey } = this.asset;
 
		return hexToBuffer(publicKey);
	}
 
	public async prepare(store: StateStorePrepare): Promise<void> {
		await store.account.cache([
			{
				address: this.senderId,
			},
		]);
	}
 
	protected verifyAgainstTransactions(
		transactions: ReadonlyArray<TransactionJSON>,
	): ReadonlyArray<TransactionError> {
		return transactions
			.filter(
				tx =>
					tx.type === this.type && tx.senderPublicKey === this.senderPublicKey,
			)
			.map(
				tx =>
					new TransactionError(
						'Register second signature only allowed once per account.',
						tx.id,
						'.asset.signature',
					),
			);
	}
 
	protected validateAsset(): ReadonlyArray<TransactionError> {
		const schemaErrors = validator.validate(
			secondSignatureAssetFormatSchema,
			this.asset,
		);
		const errors = convertToAssetError(
			this.id,
			schemaErrors,
		) as TransactionError[];
 
		return errors;
	}
 
	protected async applyAsset(
		store: StateStore,
	): Promise<ReadonlyArray<TransactionError>> {
		const errors: TransactionError[] = [];
		const sender = await store.account.get(this.senderId);
		// Check if secondPublicKey already exists on account
		if (sender.secondPublicKey) {
			errors.push(
				new TransactionError(
					'Register second signature only allowed once per account.',
					this.id,
					'.secondPublicKey',
				),
			);
		}
		const updatedSender = {
			...sender,
			secondPublicKey: this.asset.publicKey,
			secondSignature: 1,
		};
		store.account.set(updatedSender.address, updatedSender);
 
		return errors;
	}
 
	protected async undoAsset(
		store: StateStore,
	): Promise<ReadonlyArray<TransactionError>> {
		const sender = await store.account.get(this.senderId);
		const resetSender = {
			...sender,
			// tslint:disable-next-line no-null-keyword - Exception for compatibility with Core 1.4
			secondPublicKey: null,
			secondSignature: 0,
		};
 
		store.account.set(resetSender.address, resetSender);
 
		return [];
	}
 
	public sign(passphrase: string): void {
		this._signature = undefined;
		this._signSignature = undefined;
		const networkIdentifierBytes = hexToBuffer(this._networkIdentifier);
		const transactionWithNetworkIdentifierBytes = Buffer.concat([
			networkIdentifierBytes,
			this.getBytes(),
		]);
		this._signature = signData(
			hash(transactionWithNetworkIdentifierBytes),
			passphrase,
		);
		this._id = getId(this.getBytes());
	}
}