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 | 17x 17x 17x 55x 55x 55x 17x 15x 15x 2x 13x 17x 13x 13x 13x 36x 59x 26x 33x 33x 18x 18x 13x 17x 15x 2x 13x 13x 21x 13x 8x 5x 3x | /* * 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 * as cryptography from '@liskhq/lisk-cryptography'; import { TransactionError, TransactionPendingError } from '../errors'; import { IsValidResponse, IsValidResponseWithError, } from '../transaction_types'; export const validateSignature = ( publicKey: string, signature: string, transactionBytes: Buffer, id?: string, ): IsValidResponseWithError => { const transactionHash = cryptography.hash(transactionBytes); const valid = cryptography.verifyData(transactionHash, signature, publicKey); return { valid, error: !valid ? new TransactionError( `Failed to validate signature ${signature}`, id, '.signature', ) : undefined, }; }; export const signaturesAreUnique = ( signatures: ReadonlyArray<string>, ): boolean => { const uniqueSignatures: ReadonlyArray<string> = [...new Set(signatures)]; if (uniqueSignatures.length !== signatures.length) { return false; } return true; }; export const checkPublicKeySignatureUniqueness = ( publicKeys: ReadonlyArray<string>, signatures: ReadonlyArray<string>, transactionBytes: Buffer, id?: string, ): Set<string> => { const checkedPublicKeys = new Set<string>(); const validSignatures = new Set<string>(); publicKeys.forEach(publicKey => { signatures.forEach((signature: string) => { // Avoid single key from verifying more than one signature. // See issue: https://github.com/LiskHQ/lisk/issues/2540 if (checkedPublicKeys.has(publicKey) || validSignatures.has(signature)) { return; } const { valid: signatureValid } = validateSignature( publicKey, signature, transactionBytes, id, ); if (signatureValid) { checkedPublicKeys.add(publicKey); validSignatures.add(signature); } }); }); return validSignatures; }; export const validateMultisignatures = ( publicKeys: ReadonlyArray<string>, signatures: ReadonlyArray<string>, minimumValidations: number, transactionBytes: Buffer, id?: string, ): IsValidResponse => { // Check that signatures are unique if (!signaturesAreUnique(signatures)) { return { valid: false, errors: [ new TransactionError( 'Encountered duplicate signature in transaction', id, '.signatures', ), ], }; } // Check that each PK signed only once const validSignatures = checkPublicKeySignatureUniqueness( publicKeys, signatures, transactionBytes, id, ); const invalidTransactionSignatures = signatures.filter( signature => !validSignatures.has(signature), ); // Transaction is waiting for more signatures if (signatures.length < minimumValidations) { return { valid: false, errors: [ new TransactionPendingError(`Missing signatures`, id, '.signatures'), ], }; } return { valid: validSignatures.size >= minimumValidations && invalidTransactionSignatures.length === 0, errors: invalidTransactionSignatures.length > 0 ? invalidTransactionSignatures.map( signature => new TransactionError( `Failed to validate signature ${signature}`, id, '.signatures', ), ) : [], }; }; |